gl_FragDepth and writing to an FBO with only depth texture attachment

Hello,

I have an FBO with only a depth texture attached, which of course is not uncommon.

After setting draw buffers to NONE I was writing depth values to this FBO with a
minimalistic fragment shader like this:

#version 150 core

out vec4 Color;

void main(void)
{
  Color = vec4(1.0);
}

However, now I have another use case and I want to manually set the depth value by writing to gl_FragDepth.

Can I do it just like this?

#version 150 core

out float gl_FragDepth;

void main(void)
{
  gl_FragDepth = some float value;
}

Or do I still need to declare and write a color (although as before only a depth texture and no color buffer is attached)?

#version 150 core

out float gl_FragDepth;
out vec4 Color;

void main(void)
{
  Color = vec4(1.0);
  gl_FragDepth = some float value;
}

Thanks!

You don’t need to write a color if you’re not actually going to use that color later. Indeed, in the first case, you don’t even need a fragment shader. If you don’t have one, the default depth value becomes the output depth value (the color values are all undefined, which is fine since you’re not using them).

In any case, you don’t need to declare out float gl_FragDepth;, though it doesn’t hurt anything to do so. Nor do you need to declare an output color, since you’re not using it. Just write to gl_FragDepth and be done with it.

Thanks a lot Alfonse! Your answer helped me a lot.