kbirk
kbirk

Reputation: 4022

OpenGL performance overhead from frequently swapping between two shaders

I have a situation where I have two particular shaders:

The first shader casts shadows from all objects in a scene and renders to a single fullscreen 8 bit shadow texture. The glsl code is very short.

The second shader performs deferred lighting calculations onto the g-buffer and renders to a single full screen 32bit texture buffer. It uses several full screen textures (32bit position, 16bit normal, 32bit diffuse, 8bit specular, 8bit shadow). The glsl code is also quite verbose.

As you can see, for each light, these two shaders must execute subsequently. A then B, A then B, A then B. This results in a lot of swapping.

I've read that shader swapping has some high relative overhead, but im unfamiliar with how a GPU would deal with swapping between only two shaders.

Will the two shader programs be cached effectively enough that this shouldn't be a problem?

Should I combine the two shaders and direct the output using glDrawBuffers()? If I combine them, the 5 textures that are loaded from the previous g-buffer illumination stage would be left stale for the next shadow casting stage, would this cause any performance overhead?

Upvotes: 4

Views: 2732

Answers (1)

datenwolf
datenwolf

Reputation: 162164

Will the two shader programs be cached effectively enough that this shouldn't be a problem?

The problem is not caching, but that changing the shaders flushes the GPU execution pipelines. And it takes a few dozen clock cycles for the pipelines and branch predictors to settle for the newly switched to shader.

Should I combine the two shaders and direct the output using glDrawBuffers()?

In a deferred shader setup, if you can do it without too much problems? Definitely!

Upvotes: 4

Related Questions