Order of operations for state changes.

Hi,

I’m very interested in knowing which render order is most effective when reducing the state changes in OpenGL.

Which of the following procedures has the best performance in OpenGL?

  1. Update perspective view matrix
  2. Update lighting changes
  3. Update model view matrix changes
  4. Update material changes
  5. Render triangle fragments
  6. Switch to next material, and goto step 4
  7. Switch to next model view matrix, and goto step 3

Or this one… (notice lines 3 and 4 are switched around)

  1. Update perspective view matrix
  2. Update lighting changes
  3. Update material changes
  4. Update model view matrix changes
  5. Render triangle fragments
  6. Switch to next model view matrix, and goto step 4
  7. Switch to next material, and goto step 3

The reason I ask.

Which state is more common? Material changes or model view changes?

It is generally a good idea to group together as many objects as possible sharing some state in order to amortize the cost of setting that state over all the objects in the group.

in practice there’s no correct answer to your question because it depends on the scene.

in theory however, most likely you’re going to have to change the modelview matrix for each object (unless you’re rendering a bunch of objects in the same exact place…which would be weird).

Since normally each object will have to update the modelview matrix you should try and group as many objects sharing material properties as possible.

This way you amortize the cost of setting the material over all the grouped objects.

I see.

I’m still not sure which is faster. So lets see if I can work this out with some simple math?

If I have a scene with 10 models, and each model has three materials (broken into 3 different diffuse colors or smoothing groups).

If order by model view then materials, the cost would be…

model view 10 times, and materials 30 times, for a total of 40 changes.

Or…

If I order by materials then model view, the cost would bo.

materials 3 times, and model view 33 times, for a total of 36 changes.

Am I right that model view would be done in fewer changes in the second method???