   Sort by Material?
  Hi all,First, I'm not sure that here is the correct place to ask this question. Sorry for that.Anyway...I'm working on my "forward" renderer and now I want to increase rendering performance. What I do is looping through all the models added to scene and calling the same shader for every subset (material) of each model. The shader looks like:Defined 5 lightsDefined material (includes diffuse, specular, emissive colors, reflectiveness/refractiveness parameter, reflection/refraction amount, bumpy or flat parameter)Defined textures (normal map, diffuse map, specular map, environment map, perlin noise, etc.)Defined # of lights (<= 5)VertexShader(){   Transform the vertex and compute all the vectors (light direction, TBN basis, etc)   If (bumpy)  CalculateTBN basis   else  CalculateOnlyNormal}PixelShader(){   Lighting calculations numOfLights times   If (bumpy)  Use TBN basis for lighting  if (reflective || refractive) Do cube texture sampling and env. mapping   else  Use N vector for lighting   blah blah blah}I've heard that setting a texture of the shader (by ID3DXEffect::SetTexture()) for every material, extremely hurts the performance. Is that true?I've also heard an approach called "sorting by material" to avoid changing states frequently. What does it exactly mean? What do they mean with "changing states"? How does it hurt the performance?How to implement and use this approach?Is this useful for a forward renderer?Sorry for long post.Thanks in advance. -R   
Quote:Original post by programci_84I've also heard an approach called "sorting by material" to avoid changing states frequently. What does it exactly mean?It means exactly what it sounds like: you rearrange (sort) the things in your vector, such that they are grouped (by) according to the material they use.Quote:What do they mean with "changing states"? How does it hurt the performance?The renderer (OpenGL or D3D) has to do some work (that you don't see directly) every time you switch to a different material. Every time you draw something, if the new thing has a different material, then that takes some time. If you put everything with the same material together, then you only switch materials once per different material. If you have everything mixed together, in the worst case you will switch before every model/polygon/whatever.   ;  If you were using C#, I would consider something like thisvar dict = new Dictionary<Material, List<GameObject>>();This way, each texture gets switched to exactly once.I'm sure there's something like that C++ with boost.   