   Graphics Engine Question
  Hey everyone.  I'm working on a simple 3D graphics engine for use in games.  From what I understand, binding textures, switching shader programs and performing other similar tasks are pretty expensive.  Currently I just render things on demand without concerning myself with grouping similar tasks together.  I'm wondering though, is it a good idea to store the states for each renderable object and then render everything all at once in a fashion that makes things more efficient?  From what I can see, the extra memory required to store the states of renderables would be worth it.Thanks.   
  If you want to get maximum performance of your engine you should implement a batching strategy for rendering: you should try to use the same render states (by render states I don't mean only the actual render states, but also the vertex/index buffers, textures and shaders) for as long as possible. Instance rendering can help with this job. The reason for this is because the data communication between the RAM memory in the system and the RAM memory in the video card is very slow so sending a lot of data to the video card every frame will slow down your game engine very much.   ;  Hi,The first answer is to profile your engine to check where are your bottlenecks. It is true that switching states wildly does slow down your program. However, If you have few states and/or few objects, this optimisation won't bring up much improvement.The need to batch also stems from the final display: when you have a bunch of solid and transparent objects which are light dependant and light independant, and a camera revolving around the scene, you will need to group the rendering of these objects somehow so that the display is correct whatever the scene configuration is (I am thinking about the depth buffer, transparency handling and particle systems).In my own engine, I have used a scenegraph. Each mesh/light/effect node in my scenegraph stores the states needed for correct rendering. Rendering my scenegraph used the following steps:1- check for each node visibility. If visible and the node is a mesh/light/effect node then push the node to a render list.2- sort the render list first by broad categories (unlit solid/lit solid/lit transparent/unlit transparent) then by material.As a side note, some people go as far as to sort the solid objects by increasing distance from the camera and the transparent ones by decreasing distance from the camera. This speeds the display by clever use of the depth buffer.3- display the render list.4- clear the render list.In your case, you don't need to store all the exact states in your scene node. You just need to store state information necessary for correct sorting of your render list. Note that there are no optimal render lists in a dynamic world: adding lit bump-mapped objects (tower) with solid lit objects (ground + trees) and dynamic solid objects unlit (active playing characters) and transparent objects (game UI for character state/selection, billboards) and a cube map (scenery) will not give the same render list depending on where the camera is, what objects are culled, etc...To conclude, first profile your engine to check where are your real bottlenecks. Then check wether you need to group your objects by category or not based on the camera position and the different states used for your scene. It is easier to do if you write a test case using your graphics engine (like a little game) to check out bottlenecks and display.Hope that helps.Ghostly yours,Red.   