Mon, Oct 26, 2009

Per pixel depth clipping

In the example above, mouse X controls the cubes X position, mouse Y controls the cubes Z position and clicking switches between viewing the output and the depth buffer.

The primary purpose of a depth buffer is to enable per pixel depth checking. Before a pixel can be drawn its depth is compared with the current depth at that point in the depth buffer. If the new depth is greater than the current depth then the new pixel is obviously occluded and thus shouldn't be drawn. Doing this prevents depth sorting issues and allows objects to intersect without the need for dynamic tessellation.

With my 11bit depth buffer technique in PV3D the primary goal was enabling post processing effects that are only achievable with a depth buffer however at the back of my mind I wondered if depth testing was in some way feasible. Initially I contemplated lowering the precision to 8bit and using BitmapData methods to create per object masks but this would involve a large number of bitmap operations.
Using custom PixelBender blend modes seemed like the perfect solution offering a similar setup minus all the bitmap operations while still keeping 11bit precision. The first blend mode does the depth testing for each depth buffer layer resulting in a clipped depth buffer. Then a second blend mode copies over the texture based on a red channel encoded object ID.