PDA

View Full Version : Switching Shader Programs



ViolentHamster
08-26-2009, 05:20 PM
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?

Alfonse Reinheart
08-26-2009, 07:48 PM
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.

bertgp
08-27-2009, 08:28 AM
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.