I draw transparent figures by depth peeling, the result is ugly when I render to multisample (a grid appears)

vec4 fragColor = texelFetch(frontTexture, ivec2(gl_FragCoord.xy), 0); 
if (gl_FragCoord.z < nearestDepth) {
// Skip this depth in the peeling algorithm
fragColor += someColor;

Without "if return" everything is alright (but I need this "if"). Without multisampling everything is alright. The grid is exactly the same as when I use mipmapping with non-uniform flow control. I use Qt.

  • 2
    $\begingroup$ My fragment shader knowledge is very rusty, but might you not need a "discard" statement in there? opengl.org/sdk/docs/tutorials/ClockworkCoders/discard.php $\endgroup$ – Simon F Jan 22 at 9:10
  • 1
    $\begingroup$ Yes, returning from a fragment shader doesn't actually discard the fragment. Rather it just ends the shader with whatever fragment outputs were last written (or undefined values if none were). So either this is a mistake and you need discard instead or your shaders are actually more elaborate and you should include more details about your process/shaders, $\endgroup$ – Christian Rau Jan 22 at 12:51

(Promoting a comment) As Christian confirmed, to actually prevent the fragment from updating the pixel, you can't just use "return".

Instead, you can use the "discard" statement. More details can be found here

Note, there may still be hidden costs involved in using this facility since it may be performed at a late stage in the pipeline.

  • $\begingroup$ You may lose the early z-test when using discard $\endgroup$ – vgs Jan 23 at 15:16
  • $\begingroup$ Yes...hence "hidden costs" comment. It certainly doesn't benefit from the TBDR features of devices I've helped develop. $\endgroup$ – Simon F Jan 23 at 18:29

"Available only in the fragment language, gl_FragCoord is an input variable that contains the window relative coordinate (x, y, z, 1/w) values for the fragment. If multi-sampling, this value can be for any location within the pixel" !!!

P.S. Depth peeling use strict depth comparison, it is impossible if we get depth "for any location within the pixel". Instead of

if (gl_FragCoord.z < nearestDepth)


if (gl_FragCoord.z < nearestDepth - 0.0001)

(it is the first rough solution)


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.