Disable write buffer "from the shader" ?

Hi,

I have several shaders which writes to multiple outputs, for example :

layout(location = 0)​ vec4 SceneColor
layout(location = 1)​ vec4 SceneInfos

In some situations, I want to only write to the first output. As I use a FBO, I simply disable some write buffers.

But for GLSL part what is the best way ?

  • write 2 shaders, a version with 2 outputs, a version with a single output and switch between the 2 ? (sure that’s works, but not really developper-friendly)
  • a single shader, declare the 2 outputs, and use a bool uniform to know if I should write to both outputs ?
  • a single shader, and simply disable some write buffers of the FBO ?

The third solution seems the best, but I know don’t if there is any issue or performance hit when a shader write to an output which is disabled.

Thanks.

Runtime switching of functionality is usually implemented the way you suggest: either switch based on a uniform value or select appropriate functionality with subroutines (essentially function pointers).

In your case, however, I’d try to simply enable/disable buffers you want to write to using either DrawBuffers or ColorMaski().

If you’re curious, and actually in this case the effort should be almost negligible, implement both approaches and profile the results. My guess is that you won’t see much of a difference but I can’t be sure.