min, max depth value

I need to know the minimum and maximum depth values of a rendered scene to know the extents of the actual view frustum beeing visible.

This has never been an issue with software occlusion culling. When migrating to HW-occlusion culling, I face the problem that I can’t think of an easy way to get the min and max depth values from the HW.

Any thoughts?

Most folks just pick conservative values that behave well and suit their circumstances. Beware setting the near clip plane too close because that’s what really hoses your precision. Just push it out a bit but enough that clipping isn’t a serious problem.

You can find these values but it generally involves analysis of your scene using bounding volumes.

If it’s an application like a game where clipping is unacceptable then collision detection can be used to avoid clipping at the near frustum, if it’s something like a tool some clipping is normally expected and clip distances can even be user adjustable.

I’d like to add on to what Dorbie said by saying that you can set your projection matrix such that the far clipping plane is at infinity, without much loss of precision nearby. Almost exclusively, the value you use for near plane is what matters.

I suggest starting with 0.1 if you use 24 bit depth buffer, or 1.0 for 16 bit. If it’s not good enough, start jiggling it as appropriate based on collision or user inputs, as Dorbie says.

but it’s not the problem of z-precision, but rather that I would like to limit projection textures only to the visible range.

I want to be able to tell if the player is in front of a wall so the effective view frustum is xxxx.

I used to solve this problem by finding the min and max depth value from a small (64x64) depth buffer used for software occlusion testing.

The point in using HW occlusion queries is to enable concurrency, but no longer there is a convenient accessible depth buffer :frowning:

You could use occlusion-queries for that, although it won´t be very effective:

Render some screen-aligned quads (that fill the whole screen) at some distance (depth-test enabled, depth-writes and color-writes disabled). If the occlusion-query returns more than zero pixels, render another quad a bit further away.
Do that, as long as the oq returns more than zero pixels. When it returns zero pixels, than that´s the max-value of the frustum.

You could use a “divide and conquer” method to improve it a bit.

This will give you the min and max values. However, only AFTER you already rendered the scene. So you can use them only for a post-processing effect, or so, but you cannot use those values to really speed-up the rendering, since those values are quite useless in the next frame.

Anyway, i don´t know what you want to achieve, so it still might be usefull for you.


Would it be an option to precompute these values for static geometry at some regular grid? Like a bounding box of visible geometry for each m^3 in your world, or for a leaf of quad/oct/bsp tree if you prefer.