2
votes

What I'm want to do in OpenGL using C++ and GLSL: When texture has alpha (texture.a! = 1.0;) then this pixel is not written to the depth buffer. (for color buffer it is written) Write depth occurs only when a pixel texture.a == 1.0;

Discarding in shader is not a solution - then this pixel is not written to color buffer.

Any ideas?

@UPDATE: Example: I've got some UI images rendered by OpenGL. Some of them have alpha in corners. In scene rendering I have "depth prepass" to save some pixels by not calculating light on them. I want to also get UI images to that prepass - but only completely opaque pixels (alpha = 1.0).

1
Can you further explain your motivation? It sounds a little like you are trying to implement order-independent transparency. If you explain the technique you are trying to accomplish, we can give you a better solution.Andon M. Coleman
This actually does not make a lot of sense, you are trying to do this in the depth pre-pass which by definition should have color buffer writes disabled, whether you discard a fragment or not should have no affect on whether its color is written to the color buffer in a depth pre-pass. I think what you really want to do is use the stencil buffer during your GUI rendering to mask off opaque bits of the screen, then augment your depth pre-pass to write to the depth buffer at these locations (write the depth of the near plane for anything that satisfies your stencil condition).Andon M. Coleman
Alternatively, write into the same depth buffer when you do your GUI rendering as your depth pre-pass; write gl_FragDepth = 0.0 for opaque fragments, gl_FragDepth = 1.0 for anything with an alpha value != 1.0. Either approach works, but you have to re-draw the GUI every frame if you do it this way.Andon M. Coleman
It's kind of solution. But there is a way to NOT write depth for alpha pixels?Skides
Basically, no. You always have to write a depth value when you write a fragment, there is no way to selectively enable or disable this at the shader level. You can write a depth value that is guaranteed to cause it to fail a depth test under the right circumstances (this is what I was explaining in the second solution), in which case you know that the depth value will not make its way into the final framebuffer. Or if it does make its way into the framebuffer, you can be assured that the depth value you wrote is equal to the depth you cleared the buffer to so it will not make a difference.Andon M. Coleman

1 Answers

0
votes

As mentioned in the comments by Andon: "You always have to write a depth value when you write a fragment, there is no way to selectively enable or disable this at the shader level."