gl*ClientState every frame?


Why is it that in most OpenGL sample code that uses vertex arrays/VBO’s glEnableClientState and glDisableClientState are called every frame?

This also holds for sample code in the ARB_vertex_buffer_object extension spec. I’m my app I only call glEnableClientState once during app initialization for vertex attributes I need and just keep them enabled all the time. Aren’t state changes like this expensive in terms of performance?

Well I think that they are doing this becouse its simplier and safer. In these demo apps speed isnt usualy essential. But imho most of the engines have some state cache and enable/disable various states only when its needed.

If you do that only once per frame there won’t have any slowdown at all. The state machine is quiete fast. But if you have hundreds or more of states changes in each frame, I’m pretty sure the application will run slightly slowler or even more slowler, that turns out.

It can be called once if all of your render code is executed from one place. If multiple objects perform rendering, it is possible that one of the objects will need to disable some state. It is therefore advisable to enable states as they are needed in each render loop.

The really common calls like state management and texture switching and whatnot are usually those that are the most heavily optimized. When you might start to find trouble is when you use glEsotericFunction4dv() which is used once every 2^27 programs, and you call it once per object per frame.

I wouldn’t worry about this until you notice that things are slowing down and you can point the instruction pointer at the GL code and say, “This is the bottleneck. Die, bottleneck, die.”