Game Development Reference
Figure 2.2. The input buffers (color and depth) of the Homunculus scene, where m
represents the m th fragment sorted by depth. The depth of the fragments is stored in
the alpha channel, which is not represented in this figure. Note that white represents
empty fragments and that there can be M input buffers.
In [Schedl and Wimmer 12], rendering is done by the well-known depth-peeling
technique, which needs to render the scene several times. Therefore, in our imple-
mentation we use an A-buffer [Carpenter 84] to avoid rerendering the scene. Our
A-buffer consists of a 3D texture ( A 0 to A M − 1 )ofdepth M and a second texture
for counting fragments, C . While the scene is rendered, each processed fragment
increases the counting texture at the fragment's screen coordinate. Note that
the increasing of the counter has to be atomic, which means that a value in the
texture should not be altered in parallel. Based on the counter, the computed
color of the fragment is written into the texture position A c ,where c is the value
of the increased counter. Thus, all fragments processed by the graphics card are
stored, in an unsorted way, in the 3D texture A . The counter is initially set to
0, therefore empty fragments have the counter value 0.
Next, all fragments in A are sorted by depth and written into the input buffers
( I 0 to I M − 1 for color and Z 0 to Z M − 1 for depth), thus allowing up to M fragments
per screen position, as shown in Figure 2.2. Note that only C fragments have
to be sorted. Therefore the sorting effort depends on the scene complexity. We
use the bubble sort algorithm to sort the fragments. Our implementation of the
A-buffer is inspired by the implementation in [Crassin 10] and needs OpenGL
4.2 for atomic operations, and for image load and image store functionality. A
pseudocode on how to render into an A-buffer is shown in Listing 2.1.
The input buffers I 0 to I M − 1 are decomposed into K depth layers L 0 to L ( K − 1) .
Only C fragments have to be matted, which reduces the matting costs for simpler