I want to have discard
in my fragment shader prevent that fragment from writing to the stencil buffer; I had an idea for a visual effect in my game, and it might be implementable if I can rely on that behaviour for discard
.
My fragment shader doesn't do anything very fancy; it just samples a texture, and if the alpha is near 0, it discard
otherwise it writes.
void main()
{
vec4 texColor = texture(tex, texCoord);
if(texColor.a < 0.5f)
{
discard;
}
FragColor = texColor;
}
However, it sounds like I cannot rely on discard
preventing the stencil buffer from getting written to:
If Stencil Testing is active, then discarded fragments can still affect the stencil buffer. The stencil test can modify the stencil buffer, even on a stencil or depth test failure. And since the stencil test happens before the depth test, the depth test failures cannot prevent the stencil test from updating the stencil buffer.
I do need the stencil test active while I'm rendering. Can I not rely on discard
preventing writes to the stencil buffer?