Switching Shader Programs

What’s the overhead of switching between shaders every frame? If I have a bunch of different shader programs for rendering different objects in my scene, is there a performance hit for switching between shader programs?

Everything that uses shaders does this. There are virtually no programs that never change shaders within a frame.

In short: don’t worry about it. You may want to sort by shader to make sure that you don’t go overboard, but it’s nothing much to worry about.

Unfortunately, it will very much depend on your application, the driver you are using and the hardware this runs on.

Changing shaders (and other states for that matter) takes CPU time and sometimes GPU time. If your CPU is often busy validating state changes while the GPU is waiting for more draw commands to crunch, you will see a big performance gain by minimizing state changes. If on the other hand you have very few draw calls and complex meshes to process on the GPU, the cost of changing states will be negligible.

As a general rule, it is usually better to minimize state changes between draw calls but the performance gains of accomplishing this will vary greatly depending on the application’s OpenGL usage profile.

However, sometimes it is beneficial to use more state changes than strictly necessary if for example it means rendering big occluder objects first to take advantage of depth test for the following pixels.