Skip to main content
gamedev.net
🔒 Locked

Minimizing Render State Changes

Started by tscott1213 Jan 21, 2005 at 1:57 PM 5 replies 1k views
Original Post
tscott1213
tscott1213
I am implementing a Scene Graph into my 3D engine. I understand how to get the culling benefits using a scene graph but am struggling with how to minimize Render State changes. My first thought was to have everything in the game rendered using Fx files. Each leaf in the scene graph would contain an UINT that represents an ID for a loaded/compiled Fx effect. The render call would traverse the tree in the normal manner and nodes that are to be rendered would be added to a list. The list would be ordered first based on the fx ID and secondly on texture IDs. The renderer would then traverse the list from front to back thereby grouping together objects that share render states. Additionally, two lists could be created one for opaqe and one for transparent objects so that the opaqe objects can be rendered first. Two questions: 1) Does this seem reasonable? How have others approached this issue. 2) What if I am not using fx files? I could create a structure, that contains all of the renderstate information (vertex shader, pixel shader, culling, alpha blending, etc) then assign these to each object based on an ID and follow the same procedure outlined above. Any suggestions are appreciated. Todd
circlesoft
circlesoft
I recommend laying out your scene in a heirarchial tree, something like this:
|- Shader   |- Textures      |- Constants         |- Transformation Matrices


I have found that this is a pretty good order for sorting objects by. Try to avoid straight lists of objects, because they take a while to sort. Trees are very efficient with sorting (objects are added to the tree, they are categorized once, and then they can be retrieved very quickly).

You should probably take an all-or-nothing approach with using effects/shaders. Most graphics engines use all of their own custom shaders for rendering, except for some 2D GUI stuff (which wouldn't be in the scenegraph anyways). It's must easier if you don't mix-and-match the programmable pipeline with the fixed-function pipeline for your world objects.
MickeyMouse
MickeyMouse
This is what ATI's Game Developers presentation sais:
Quote:
In the past, we prioritized texture changes as the most important state change. Now prioritize based on the following order:
- Vertex and Pixel Shader Code
- Vertex and Pixel Shader Constants
- Textures
- Vertex and Index Buffers

Slightly differs from above priorities, but it's all discussable and might obviously vary depending on scene kind, shaders' complexity, card & drivers.
Maciej Sawitus
my blog | my games
DrGUI
DrGUI
If you have any alpha blended primitives in your scene, do you have to sort everything? (unless the alpha blend is additive or something)
I haven't done that before so I'm not sure.

I like those ideas for sorting; my 'Real-Time Terrain Rendering with C++ and DirectX 9' book [Greg Snook www.mightystudios.com] also sorted in that order, using a union to put everything into 2 words or something for quick sorting.

Radix sorting might be rather good for sorting by integers.
Does anyone know how to make a float-sorting radix sort in managed code?

Hope that helps,
tscott1213
tscott1213
This is great guys, thanks alot.

Just to make sure I understand...

I would first have a scene graph based on parent-child relationships for culling. Then, during rendering, each object that passes the cull tests is added to a hierarchical tree sorted by Shader, texture, constants...or some order like that.

The reason I ask this is I didn't understand your comment, circlesoft, that things are 'categorized once'.

Thanks
circlesoft
circlesoft
Quote:
Original post by tscott1213
I would first have a scene graph based on parent-child relationships for culling. Then, during rendering, each object that passes the cull tests is added to a hierarchical tree sorted by Shader, texture, constants...or some order like that.

The reason I ask this is I didn't understand your comment, circlesoft, that things are 'categorized once'.


Aye, yes...sorry. After you get your list of visible nodes back from the culling structure (ie quadtree, octree), add them to your scene graph. Refreshing the scenegraph every time usually isn't too performance-heavy, since the list of new nodes is restricted to those that are visible.
tscott1213
tscott1213
Thanks again guys, this was really helpful.

Dr. G, I'm going to pick up that book you referenced. I took a look through the index after you brought it to my attention. Thanks

Todd

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.