Material manager

Started by
6 comments, last by Azzazelus 8 years, 10 months ago

Hello. I'm using Maya to export all the materials from the scene (most of them have textures) and put them in one single file. I've separated the materials from the mesh so I can assign one material to multiple meshed or to change the material of a mesh when I want to while rendering.

I have a class for Materials that keeps pointers to the resource view after I load and Create the textures from the file.

I name the materials so I can assign them to the meshes when I want to. Like Mesh1->materialpointer = Materials("chest_material");

The thing is that I don't know how to store the material information so I can aces it faster when I iterate in the materials to search for that name string ("chest_material") or even a number;

My first idea was to create an Binary Search Tree (with AVL insertion) but I don't like the idea of data being spread all over the ram when I create the nodes so the next idea was to create a simple vector and make a binary search in it to lookup for the material data.

The problem is if I want to destroy a material and a resource view (in case I don't need that material anymore and release the data from ram) or I need to load a new one (ex: new level needs 3 new materials that are not loaded initially for performance issues) I always have to sort the vector and maybe re-size it or reuse its components.

Any suggestion? I really don't want to use any library.

Advertisement

With a simple pointer.

What is a vector in this case ?, dont you have a list ?, its advanced memory managment, search for list!

S T O P C R I M E !

Visual Pro 2005 C++ DX9 Cubase VST 3.70 Working on : LevelContainer class & LevelEditor

If I assign a new material for one mesh I have to search the material vector (or a list or whatever) for the name. If I have like 500 materials I should do a binary search so I wont iterate all the materials all the time. The question was if I should make a binary search tree to organise material data for fast insertion/lookup but the texture data will be spread all over the ram (because of different allocations when I create a new material item) or to use a vector and make a binary search but then when I need to resize the vector to add another element I will always have to sort it or create it in another location in ram (as in std::vector where if the new size is bigger the whole vector must be moved in another location so the items would be in continuous memory).

If you have a list then no resizing vector.

You dont wanto compare 500 texture name strings ?, is that it ?, or maybe id numbers ?, cant do without.

S T O P C R I M E !

Visual Pro 2005 C++ DX9 Cubase VST 3.70 Working on : LevelContainer class & LevelEditor

I don't consider a list at all. Its a Binary Search Tree or a vector.

The fastest way to lookup a key match is a hashtable. A binary tree im sure is also fine.

You dont need to use the same datastructure for indexed lookups as for storing the data.

If you really prefer a certain in memory layout, then use it... And just index into that layout for fast lookup.
Sigh.

(a) You use the number 500 like it's a large number. That's a pretty small number. It's not too far out of the range where you could just use a linear search. You're probably stressing too much here.

(b) How often are you looking up names? That should be a rare operation. Likewise, loading new materials is a rare operation. Again, if it's rare enough, speed isn't super relevant.

(c) Is resizing an array of sorted elements to insert a new item really a bottleneck? memcpy'ing data is _fast_. _really fast_. So long as your types are trivially movable/copyable (which they should be) you have no reason to worry about this operation being slow.

(d) You can use swap-and-pop to efficiently remove elements from an array (that only requires moving a single element). Use a separate key indirection table to map your ID to the material data itself if the linear search isn't fast enough for you.

(e) You're already going to be traipsing all over RAM in order to dereference your strings. You have a bigger problem in how you're identifying resources at runtime than you do in the structure in which you store a few materials.

(f) The sorted vector you allude to is an efficient structure. Keep it sorted gives you O(logN) insert, delete, and find operations, which is equivalent to a tree. Inserting elements is fast; the cache efficiency (even if the types aren't trivially movable) can still more than make up for the lack of need to allocate and lack of extra pointer indirections.

Sean Middleditch – Game Systems Engineer – Join my team!

Its 500 materials but every time I construct a mesh I must search for the material. So it would be number_of_meshes * number_of_materials. If I have 500 meshes it would be 500*500 = 250.000 times searching for a string.

This topic is closed to new replies.

Advertisement