Sign in to follow this  
3Dgonewild

Improving texture cache

Recommended Posts

An example code of what i've been using all this time
	class CTexture
		{
			private:
				unsigned int ptexture,pname;       
			public:
				CTexture(const unsigned int& tname,const unsigned int& ttexture);
				~CTexture();
				unsigned int& texture();//implementantion returns constant reference
				unsigned int&  name();//implementantion returns constant reference
		};


	class CTextureList
		{
			private:
			std::vector<CTexture> textures;
			public:
			CTextureList();
			CTextureList();
			void add(const std::string& tfile,const unsigned int& tname);
                        void removeByName(const unsigned int& tname);
                        void removeByIndex(const size_t& tindex);
			void callByName(const unsigned int& tname);
			void callByIndex(const size_t& tindex);//direct access-no checking
		};

		void CTextureList::callByName(const unsigned int& tname)
			{
				static std::vector<CTexture>::iterator last_texture_index={textures.begin()};
				static unsigned int last_texture_id={0};
				//if already used, bind at once.....
				if( (tname == last_texture_id)&&(last_texture_index!=textures.end()))
					{
						bind texture (last->texture())
						return;
					}
				for(std::vector<CTexture>::iterator it=textures.begin(); it!=textures.end();it++)
						{
							if( it->name()!= tname ) continue;
							last_texture_index = it;
							last_texture_id = tname;
							bind texture (it->texture())
							return;
						}
			}

		

Usage:
		enum
			{
			CUBE_FACE_A,
			CUBE_FACE_B
			};

		CTextureList textures;
		textures.add("cfa.tga",CUBE_FACE_A);
		textures.add("cfb.tga",CUBE_FACE_B);

		//call CUBE a&b face a by name
		textures.callByName(CUBE_FACE_A);
		textures.callByName(CUBE_FACE_B);

		//direct access
		textures.callByIndex(0);//call texture at index 0
		

Now , my question. Is there anything else that i can do to speed up the loop? I've heard something about hash tables (or something). Do you guys have any better ideas? Thanks .

Share this post


Link to post
Share on other sites

static std::vector<CTexture>::iterator last_texture_index={textures.begin()};
static unsigned int last_texture_id={0};


What's with the static variables?


for(std::vector<CTexture>::iterator it=textures.begin(); it!=textures.end();it++)


If this is the slow loop you're referring to, then, you can either do a hash table for O(1) access, or keep the list sorted by texture name when you insert and do a binary search in O(logN) time.

Share this post


Link to post
Share on other sites
Quote:
Original post by 3Dgonewild
Now , my question.

Is there anything else that i can do to speed up the loop?
I've heard something about hash tables (or something).
Do you guys have any better ideas?

Thanks .


- Your search of a vector is inefficient, use std::map<>, or std::hash_map<> (if your compiler vendor provides implementations of standard library extensions).

Also:

- Don't return basic types (such as int) by reference, there is no point and is highly unconventional.
- Don't pass arguments which are basic types (such as int) by reference, there is no point and is highly unconventional.

Share this post


Link to post
Share on other sites

{damn i've been trying to reply 5-6 days ago , but it was impossible....the forum was lagging so much , and i was getting
sql errors/timeouts }

@clb:
I've added the static variables because i've been using some textures more than
1 time in one frame.

Also , where can i find more information on hash tables?

Thanks.


@fpsgamer:

Thanks!.
I'll check out hash map , and i will post back.

Quote:


- Don't return basic types (such as int) by reference, there is no point and is highly unconventional.
- Don't pass arguments which are basic types (such as int) by reference, there is no point and is highly unconventional.


I've read somewhere that it is efficient to pass/return types by reference if their size is >=4bytes.
But anyway , thanks! ,i'll remember that.

Share this post


Link to post
Share on other sites
Good reference for learning hash tables bottom-up is Cormen, Leiserson, Rivest. Introduction to Algorithms.. (well, a good intro to anything. ask anyone in CS and he'll know that book)

Also, googling "hash table" seems to give about 6/10 relevant results on the first page. I don't think you'd go wrong to try one of those.

fpsgamer's suggestion of std::map and std::hash_map is definitely the way to go for practical use. You don't need to know all the details about hash table theory to use them, just check the standard documentation on how their interfaces work and you'll good to go.

Quote:

I've read somewhere that it is efficient to pass/return types by reference if their size is >=4bytes.

You're about right. On a 32-bit platform using MSVC or GCC, it generally is more efficient to pass/return types by reference if their size is > 4bytes. This is because the compiler doesn't have to generate a copy of the object to pass/return in the function call.

When returning by reference, be careful about not returning a reference to a temporary object though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this