How to improve z culling with writing only depth first

i want to render my scene in two passes to improve z-culling of “shadered” pixels:

  1. render scene, only depth writing, no shaders.
  2. render scene, only color writing, with shaders.
    (depth test always enable)

So my code in OpenGL looks like this:



//I enable my shader program
//I disable my shader program

If i render everything in one pass, all is ok. If using this 2 passes method, it seems that many pixels are z-culled when they shouldn’t. It seems as if in the second pass the same pixel has not exactly the same depth.
And i’ve tried to do both pass with fixed pipeline or with shaders, but i’ve got the same result. Please, if someone has an idea or know a better way to achieve my purpose.

Thanks in advance.


I could think about it is quite a Problem to do a 2 Step Stuff if you have translucent parts … especially if you have smoothy borders this could be a Problem, course they are mathematically calculated as half Translucently.
especially if your colormask has an alpha which is not drawn (GL_FALSE) then this could be one Problem, dependign if you use it for transparency.
BTW - why do you turn of the Depth mask? Can it overwrite the maximum at that point? For me it seems it can’t - so leave it better in ON value. Maybe that warks - as the Depth is set on the Maximum nothing will work worse as the check will turn of the writing which will call the shading.

But take a minute to think about the Transparency and smooth borders.


Thanks a lot for your reply, it helped me to find the problem. In fact, i tried your suggestion (keeping depth write on) and it works :slight_smile: . But it was odd because i tought my problem was that many pixels were z-culled when they shouldn’t, and changing depth write state don’t change that.
So finally, i put back depth write off before second pass, but put it on after the pass (just before swaping buffer), which i hadn’t done since i enable depth write at the beginning of each frame. And that was it.
It seems that depth write needs to be on at the end of each frame (or at the very beginning), there must be a reason, but i can’t find any at the moment.

Concerning transparency, i haven’t thought about how to handle it, but it is on my to do list :wink: . Right now, i was just interested to see the direct improvement with this method to limit the cost of my fragment shader.

I’m so stupid! It was so obvious, i was just trying to clear depth buffer when depth write was still off. My problem was definitely not worthy losing so much time :mad: !