55
4
ScreenSpaceClassificationfor
EfficientDeferredShading
Balor Knight
Matthew Ritchie
George Parrish
Black Rock Studio
4.1Introduction
Deferred shading is an increasingly popular technique used in video game ren-
dering. Geometry components such as depth, normal, material color, etc., are
rendered into a geometry buffer (commonly referred to as a G-buffer), and then
deferred passes are applied in screen space using the G-buffer components as
inputs.
A particularly common and beneficial use of deferred shading is for faster
lighting. By detaching lighting from scene rendering, lights no longer affect sce-
ne complexity, shader complexity, batch count, etc. Another significant benefit of
deferred lighting is that only relevant and visible pixels are lit by each light, lead-
ing to less pixel overdraw and better performance.
The traditional deferred lighting model usually includes a fullscreen lighting
pass where global light properties, such as sun light and sun shadows, are ap-
plied. However, this lighting pass can be very expensive due to the number of
onscreen pixels and the complexity of the lighting shader required.
A more efficient approach would be to take different shader paths for differ-
ent parts of the scene according to which lighting calculations are actually re-
quired. A good example is the expensive filtering techniques needed for soft
shadow edges. It would improve performance significantly if we only performed
this filter on the areas of the screen that we know are at the edges of shadows.
56 4.ScreenSpaceClassificationforEfficientDeferredShading
This can be done using dynamic shader branches, but that can lead to poor per-
formance on current game console hardware.
Swoboda [2009] describes a technique that uses the PlayStation 3 SPUs to
analyze the depth buffer and classify screen areas for improved performance in
post-processing effects, such as depth of field. Moore and Jefferies [2009] de-
scribe a technique that uses low-resolution screen-space shadow masks to classi-
fy screen areas as in shadow, not in shadow, or on the shadow edge for improved
soft shadow rendering performance. They also describe a fast multisample anti-
aliasing (MSAA) edge detection technique that improves deferred lighting per-
formance.
These works provided the background and inspiration for this chapter, which
extends things further by classifying screen areas according to the global light
properties they require, thus minimizing shader complexity for each area. This
work has been successfully implemented with good results in Split/Second, a rac-
ing game developed by Disney’s Black Rock Studio. It is this implementation
that we cover in this chapter because it gives a practical real-world example of
how this technique can be applied.
4.2OverviewofMethod
The screen is divided into
4
4
pixel tiles. For every frame, each tile is classified
according to the minimum global light properties it requires. The seven global
light properties used on Split/Second are the following:
1. Sky. These are the fastest pixels because they don’t require any lighting cal-
culations at all. The sky color is simply copied directly from the G-buffer.
2. Sun light. Pixels facing the sun require sun and specular lighting calculations
(unless they’re fully in shadow).
3. Solid shadow. Pixels fully in shadow don’t require any shadow or sun light
calculations.
4. Soft shadow. Pixels at the edge of shadows require expensive eight-tap per-
centage closer filtering (PCF) unless they face away from the sun.
5. Shadow fade. Pixels near the end of the dynamic shadow draw distance fade
from full shadow to no shadow to avoid pops as geometry moves out of the
shadow range.
6. Light scattering. All but the nearest pixels have a light scattering calculation
applied.
7. Antialiasing. Pixels at the edges of polygons require lighting calculations for
both 2X MSAA fragments.
4.2
O
O
verviewof
M
W
stor
e
excl
u
whe
n
O
inde
sha
d
W
co
mp
Sm
a
mea
n
sha
d
exis
t
the
c
add
s
scre
e
erti
e
Fi
gu
gree
n
M
ethod
W
e calculate
e
the result i
n
u
sive for a si
n
properties
a
O
nce we’ve
x buffer for
e
d
er with the
m
W
e found th
a
p
utation tim
e
a
ller tiles me
a
n
t more lig
h
d
ers. A size
o
t
ing sc
r
een-s
p
c
lassification
s
up to 57,6
0
e
nshots from
e
s highlighte
d
u
re 4.1. A scre
n
.
which light
p
n
a 7-bit class
ngle pixel, s
u
a
re combined
generated a
e
ach ID that
p
m
inimum ligh
t
a
t a
44
til
e
e
and shader
a
nt spending
t
h
ting propert
i
o
f 44 pixe
l
p
ace shadow
code, as ex
p
0
0 tiles at a
r
the Split/Se
c
d
.
enshot from
Sp
p
roperties ar
e
ification ID.
u
ch as sky a
n
into
44
pi
x
c
lassificatio
n
p
oin
t
s to the
t
t
ing code req
u
e
size gave t
h
complexity,
t
oo much ti
m
i
es affec
t
ing
l
s also conve
mask [Moor
e
p
lained later.
F
r
esolution of
c
ond tutorial
m
Sp
lit/Second w
i
e
required fo
r
Some of the
s
n
d sunlight, b
u
x
el tiles.
n
ID for eve
r
t
iles with tha
u
ired for tho
s
h
e best balan
leading to
b
m
e classifyin
g
each tile, l
e
e
niently matc
h
e
and Jefferi
e
F
or Split/Sec
1280 720 .
F
m
ode with d
i
i
th soft shado
w
r
each
44
p
s
e properties
ut they can e
r
y tile, we th
a
t ID and ren
d
s
e light prop
e
n
ce between
c
b
est overall
p
g
the tiles, an
d
e
ading to m
o
h
es the reso
l
e
s 2009], whi
c
ond, the use
F
igures 4.1 a
n
i
fferent glob
a
w
edge pixels
h
p
ixel tile and
are mutually
e
xist
t
ogether
en create an
d
er it using a
e
rties.
c
lassification
p
erformance.
d
larger tiles
o
re complex
l
ution of our
c
h simplifies
of
44
tiles
n
d 4.2 show
a
l light prop-
h
ighlighted in
57
58
4.
3
Figure 4
.
3
Depth
Tile cla
s
the seve
n
we clas
s
screen-s
p
(320 1
8
and it i
s
shader
c
mask co
d
Mo
o
screen-s
p
pixels i
n
work re
s
not in s
h
this text
u
areas ex
c
For
t
shadow
reading
d
jections.
.
2. A screensh
o
Related
C
s
sification in
n
light prop
e
s
ify the othe
r
p
ace shado
w
8
0) texture,
w
s
also readin
g
c
omplexity i
n
d
e to perfor
m
o
re and Jeffer
p
ace shado
w
n
shadow, pi
x
s
ults in a tex
t
h
adow, and a
l
u
re for each
c
ept those ne
a
t
ile classifica
t
fade since t
h
d
epth in thes
4.Scre
e
o
t from Split/S
e
C
lassific
a
Split/Secon
d
rties during
o
r
three in a
p
w
code is
a
w
hich perfec
t
g
depths, me
a
n
the pe
r
-pix
e
m
all depth-re
l
ies [2009] e
x
mask textu
r
x
els not in s
h
t
ure containi
n
l
l other valu
e
screen-space
a
r the edges
o
t
ion, we exte
n
h
ey’re both c
a
e shaders to
r
e
nSpaceCla
s
e
cond with M
S
a
tion
d
is broken i
n
o
ur screen-sp
p
e
r
-pixel pas
a
lready gen
e
t
ly matches
o
a
ning that w
e
e
l pass by e
x
l
ated classifi
c
x
plain how w
e
r
e that conta
i
h
adow, and
p
n
g zeros for
e
s for pixels
n
position, w
e
o
f shadows t
h
n
d this code
t
a
lculated fro
m
r
econstruct
w
s
sificationfo
r
S
AA edge pix
e
n
to two parts
.
p
ace shadow
m
s
s. The reaso
e
rating a o
n
o
ur tile resol
u
e
can minim
i
x
tending the
c
ation.
e
generate a
o
i
ns three sha
p
ixels near t
h
pixels in sh
a
n
ear a shado
w
e
can avoid
e
h
at we want t
o
t
o also classi
f
m
depth alo
n
w
orld positio
n
r
EfficientDe
f
e
ls highlighted
. We classif
y
m
ask genera
t
n for this is
n
e-quarter r
e
u
tion of
4
4
i
ze texture r
e
screen-space
o
ne-quarter r
e
dow types p
e
h
e shadow e
d
a
dow, ones f
o
w
edge. By l
o
e
xpensive PC
o
be soft.
f
y light scatt
e
n
e, and we’r
e
n
for the sha
d
f
erredShadi
n
in green.
y
four of
t
ion, and
that the
e
solution
4
pixels,
e
ads and
shadow
e
solution
e
r pixel:
d
ge. This
o
r pixels
o
oking at
F for all
e
ring and
e
already
d
ow p
r
o-
n
g
4.3DepthRelatedClassification 59
float shadowType = CalcShadowType(worldPos, depth);
float lightScattering = (depth > scatteringStartDist) ? 1.0 : 0.0;
float shadowFade = (depth > shadowFadeStartDist) ? 1.0 : 0.0;
output.color = float4(shadowType, lightScattering, shadowFade, 0.0);
Listing 4.1. Classifying light scattering and shadow fade in the first-pass shadow mask shader.
Recall that the shadow mask is generated in two passes. The first pass calcu-
lates the shadow type per pixel at one-half resolution (
640 36
0
) and the second
pass conservatively expands the pixels marked as near shadow edge by down-
sampling to one-quarter resolution. Listing 4.1 shows how we add a simple light
scattering and shadow fade classification test to the first-pass shader.
Listing 4.2 shows how we extend the second expand pass to pack the classi-
fication results together into four bits so they can easily be combined with the
per-pixel classification results later on.
// Read 4 texels from 1st pass with sample offsets of 1 texel.
#define OFFSET_X (1.0 / 640.0)
#define OFFSET_Y (1.0 / 360.0)
float3 rgb = tex2D(tex, uv + float2(-OFFSET_X, -OFFSET_Y)).rgb;
rgb += tex2D(tex, uv + float2(OFFSET_X, -OFFSET_Y)).rgb;
rgb += tex2D(tex, uv + float2(-OFFSET_X, OFFSET_Y)).rgb;
rgb += tex2D(tex, uv + float2(OFFSET_X, OFFSET_Y)).rgb;
// Pack classification bits together.
#define RAW_SHADOW_SOLID (1 << 0)
#define RAW_SHADOW_SOFT (1 << 1)
#define RAW_SHADOW_FADE (1 << 2)
#define RAW_LIGHT_SCATTERING (1 << 3)
float bits = 0.0;
if (rgb.r == 0.0)
bits += RAW_SHADOW_SOLID / 255.0;
else if (rgb.r < 4.0)
bits += RAW_SHADOW_SOFT / 255.0;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset