70 4.Screen‐SpaceClassificationforEfficientDeferredShading
We do this by prebuilding a lookup table that converts a 7-bit raw classifica-
tion ID into a 5-bit optimized classification ID, as shown in Listing 4.8. Each ID
is passed through this lookup table before being used for index buffer generation.
These two bits give us four possible combined properties, which is enough to
represent all possible combinations in the scene as follows:
■ 00 = solid shadow.
■ 01 = solid shadow + shadow fade + sunlight.
■ 10 = sun light (with no shadows).
■ 11 = soft shadow + shadow fade + sunlight.
The only caveat to collapsing these bits is that we’re now always calculating
shadow fade when soft shadows are enabled. However, this extra cost is negligi-
ble and is far outweighed by the benefits of reducing the shader count to 32.
#define LIGHT_SCATTERING (1 << 0)
#define MSAA_EDGE (1 << 1)
#define SKY (1 << 2)
#define SUN_0 (1 << 3)
#define SUN_1 (1 << 4)
unsigned char output[32];
for (int iCombo = 0; iCombo < 128; iCombo++)
{
// Clear output bits.
unsigned char bits = 0;
// Most combos are directly copied.
if (iCombo & RAW_LIGHT_SCATTERING) bits |= LIGHT_SCATTERING;
if (iCombo & RAW_MSAA_EDGE) bits |= MSAA_EDGE;
if (iCombo & RAW_SKY) bits |= SKY;
// If in solid shadow.
if (iCombo & RAW_SHADOW_SOLID)
{
// Set bit 0 if fading to sun.
if ((iCombo & RAW_SHADOW_FADE) &&
(iCombo & RAW_SUN_LIGHT))
bits |= SUN_0;