# How to use multisampling with “if … return” in a fragment shader?

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
return;
}
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.

• 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 – Simon F Jan 22 at 9:10
• 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, – 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.

• You may lose the early z-test when using discard – vgs Jan 23 at 15:16
• Yes...hence "hidden costs" comment. It certainly doesn't benefit from the TBDR features of devices I've helped develop. – 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)


use

if (gl_FragCoord.z < nearestDepth - 0.0001)


(it is the first rough solution)