Game Development Reference
// Convert distances to a texture coordinate shift for filtering.
if (abs(offset.x) > abs(offset.y))
offset.x = 0;
offset.y = ((offset.y >= 0)? 0.5f : -0.5f) - offset.y;
offset.x = ((offset.x >= 0)? 0.5f : -0.5f) - offset.x;
offset.y = 0;
return BackBuffer.Sample(Linear, TexCoord + offset * PixelSize);
Listing 3.4. Final blending.
where we generate this texture and get a full pre-Z at the same time. First
we draw the scene to a depth buffer with front-face culling, then copy to the
second depth texture, and then proceed with main scene rendering, using back-
face culling as usual. The second-depth values in the buffer will in general still
be in front of whatever the frontmost surfaces cover, allowing nearly the same
culling eciency in theory.
This is the approach the sample code is using by
There are two things to note about this approach. First, a second-depth
pre-Z pass places the depth values further back than a tradition pre-Z pass.
Hierarchical-Z (Hi-Z)) hardware usually stores a low-precision but conservative
value of the most distant depth. For things that are very close to the back-face—
for instance, things behind the wall that are standing next to the wall—there
may be cases where the hardware is unable to cull those pixels because it cannot
guarantee it is completely occluded with the low-precision Hi-Z value it has. In
those cases a traditional pre-Z pass could be more effective, depending on the
hardware. There are also cases where objects intersect, so that the back-facing
depth values are actually behind the intersecting object's front faces. In those
cases we do lose the Hi-Z culling. The second thing to note, though, is that
a second-depth pre-Z pass is generally faster to render, which may or may not
result in better performance overall. The reason it is faster is because plenty
of geometry does not have anything behind it. The most distant surface never
adds anything of value to a pre-Z pass, because there is nothing behind it to cull.
For an indoor scene, such as the one in the sample application, the most distant
surface is always front-facing. For outdoor scenes this is also the case if you
consider the skybox to be a front-facing surface, which is reasonable. This means
that if you render front-faces to pre-Z, you will also render walls that have nothing
behind them to pre-Z, which is wasteful. On the other hand, realistically there
will always be something behind the back-faces. For the kind of closed geometry
in the sample application, this is a guarantee. For real game geometry there are