Search code examples
opengl-esdepth-buffer

glsl es 2.0 discard and depth buffer


OpenGL ES 2.0 can't write to Depth Buffer, since it doesn't have gl_FragDepth, but if I do discard - will it affect Depth Buffer (I mean, not overwrite it with CURRENT gl_FragCoord.z, but leave as is)?

Something like this:

void main() {
   if (sin(x) > 0.5 ){
      discard;
   } else {
      gl_FragColor = (1.0, 1.0, 1.0, 1.0);
   }
}

I expect to get holes in Depth Buffer, in discard cases.


Solution

  • Your statement saying "OpenGL ES 2.0 can't write to Depth Buffer" is somewhat misleading. It will write to the depth buffer, as long as there is a depth buffer in the current framebuffer, and depth buffer writes are enabled.

    Not having a writable gl_FragDepth variable in the fragment shader means that you can't modify the depth value in the fragment shader. It's alway the incoming depth value that is written to the depth buffer.

    If you discard a fragment, neither the color nor the depth value is written. So yes, if you discard a fragment, the depth buffer will not be modified for that fragment.