62 4.Screen‐SpaceClassificationforEfficientDeferredShading
done in a very different way on each platform in order to make the most of their
particular strengths and weaknesses, as explained in Section 4.9.
4.6IndexBufferGeneration
Once both sets of classification results are ready, a GPU callback triggers index
buffer generation for each classification ID. There is one preallocated index buff-
er containing exactly enough indices for all tiles. On the Xbox 360, we use the
RECT primitive type, which requires three indices per tile, and on the Play-
Station 3, we use the the
QUAD primitive type, which requires four indices per tile.
The index buffer references a prebuilt vertex buffer containing a vertex for each
tile corner. At a tile resolution of
4
pixels, this equates to
21 181
vertices at
a screen resolution of
280 720
.
Index buffer generation is performed in three passes. The first pass iterates
over every tile and builds a table containing the number of tiles using each classi-
fication ID, as shown in Listing 4.4. The second pass iterates over this table and
builds a table of offsets into the index buffer for each classification ID, as shown
in Listing 4.5. The third pass fills in the index buffer by iterating over every tile,
getting the current index buffer offset for the tile’s classification ID, writing new
indices for that tile to the index buffer, and advancing the index buffer pointer.
An example using the
QUAD primitive is shown in Listing 4.6. We now have a
final index buffer containing indices for all tiles and a table of starting indices for
each classification ID. We’re ready to render!
#define SHADER_COUNT 128
#define TILE_COUNT (320 * 180)
unsigned int shaderTileCounts[SHADER_COUNT];
for (int shader = 0; shader < SHADER_COUNT; shader++)
{
shaderTileCount[shader] = 0;
}
for (int tile = 0; tile < TILE_COUNT; tile++)
{
unsigned char id = classificationData[tile];
shaderTileCount[id]++;
}
Listing 4.4. This code counts the number of tiles using each classification ID.