Question

I have a main scene, and I have a scene I want to render on top of it. I want to use early depth testing for performance, so I want to render everything together, front to back. But I cannot segregate each scene to half the depth buffer because that would restrict its precision. I could render the back scene, clear the depth buffer, and render the front scene, which would draw correctly, but would incur unnecessary overdraw. Extra frame buffer plus blit would likewise cause overdraw.

The ideal solution (as answered below) is to use glDepthRange to establish multiple zones within the depth buffer, allowing normal rendering to occur in two-pass front-to-back order to minimize overdraw while preventing cross-layer occlusion.

Was it helpful?

Solution

I have a main scene, and I have a scene I want to render on top of it. I want to use early depth testing for performance, so I want to render everything together, front to back. But I cannot segregate each scene to half the depth buffer because that would restrict its precision.

Normally, you have 24 bits of depth buffer precision. Splitting this in half yields... 23 bits of precision. Somehow, I rather doubt that's going to be a major problem. And if it is, you can always reclaim that by giving less space to the front overlay. Say, only 5% of the depth range.

This is how FPS games do their "viewmodel" (the hand and gun of the player) overlays. Do what the pros do.

Can I somehow render the overlaying scene, change any value in the depth buffer to -1 that is not 1, then render the main scene?

-1 is not a valid depth buffer value. So I'll pretend you said 0 instead.

Yes, you could do that. You could render your scene to a depth texture. Then render a full-screen quad that takes the depth value from the texture, compares it with 1, and if it's not exactly 1.0, it writes 0 instead of 1.0.

But the whole point of this excercise is performance, yes? Consider what has to happen to make what I just said work.

The GPU cannot render that full-screen quad until the first scene has finished rendering. That will mean that the entire rendering pipeline has to completely flush, wasting valuable time.

Compared to the glDepthRange solution, this will likely be slower.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top