Sign in to follow this  

Material sorting & shader question

Recommended Posts

Hi, I know that sorting meshes on material is not a bad idea because it prevents a 'material switch' between every object that needs to be rendered. But sorting is not always that handy. For example, if there's lots of culling, the list of polygons that needs to be rendered can change every frame and so the sorting needs to be adjusted as well right? I wonder how a FPS game like Doom3 or Hl2 handles this. As far as I know, it uses something like BSP to cull. What happens to the visible polygons/walls or whatever groups? Are they sent to a 'doRender-list' or something? And is this list sorted on material each frame as well or not at all? The amount of 'the-same' materials on a screem might be pretty low so maybe its not worth to sort? I'm trying to find out the efficiency of material sorting. I made my engine in such a way that it sorts everything. It might be very good for large amounts like grass-planes but its also a pain in the *ss for culling and other stuff. How important is to sort on material? Another problem are shaders. For example, there are 10 objects with exactly the same shader and the same parameters (10 palmtrees with the same texture(s) for example). So, the engine does this: 1- pass the shader parameters 2- enable the shader 3- render all objects with this shader The modelviewmatrix is passed only 1 time at step1 so all objects have the same position while I have translated/scaled or rotated them. How to fix this? Can it be fixed by adjusting the modelViewProjectionMatrix parameter each time before render an object? Greetings, Rick

Share this post

Link to post
Share on other sites

I asked something about scenegraphes / renderer a week ago, and amongst the replies, someone suggested this to me :

On loading, pre-sort all your graphic entities by materials, shader, or whatever. So you can re-use this in runtime to know in which order you have to render your objects.

This is valid only for objects whose materials, shader, etc. don't change during runtime. So you could have a part of the renderqueue with "static" objects (and for those, you already computed the order, so you don't have to do it again) and "dynamic" objects (their render states can change during runtime, so you need to sort them) This way, you only have a small amount of object to sort by render states.
And I think you could even cache those results as long as the render states didn't change. Because between 2 frames, even with a lot of occlusion, most of the objects will still be visible.

For your shader, I would do this like that :

1- Set the parameters which are common to every entity using this shader
2- Enable the shader
3- for each entity
3a- set the shader parameter for this entity (for example, transformation)
3b- render the entity
4- Disable the shader

In DirectX, the 3a step is done via the ID3DXEffect::CommitChanges() method

I hope I helped a little ^^

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