Graphics Reference
In-Depth Information
//
Light pixel shader
that
executes once
per
MSAA
subsample
float4
PSMainPerSample( in float4
screenPos :
SV_Position,
in
uint
subSamplelndex
:
SV_SampleIndex
) :
SV_Target0
{
float3 normal;
fioat3 position;
float
specularPower;
//
Get
the
G-Buffer
values
for
the
current
sub-sample,
and
calculate
//
the lighting
GetGBufferAttributes(
screenPos.xy, ViewRay, subSamplelndex,
normal, position, specularPower );
return
CalcLighting(
normal, position, specularPower );
}
Listing
11.18.
Per-sample light
buffer
pixel shader
code.
Naturally,
running pixel shaders
at
per-sample frequency incurs
a
higher overhead
than
running them
per-pixel. To mitigate this
effect,
the stencil mask technique mentioned
earlier can
be used to mask off pixels that don't need to be shaded per-sample.
For
the
final pass, we no
longer need to
output
separate
values per-sample. Instead,
we
want to sample
the light
buffer for
all relevant subsamples, filter the results, and finally
apply the albedo
terms. Thus,
running
the pixel
shader
at per-pixel
frequency
and relying
on the coverage and depth/stencil tests is
perfectly
adequate. However,
we must be careful
not to
inadvertently
apply lighting from
subsamples
not covered by
the
triangle currently
being rasterized,
or artifacts will
occur. To avoid
these artifacts,
we
can make use of the
input coverage
mask, much like
the
lighting
pass
of a
classic
deferred Tenderer. The code
in
Listing
11.19 demonstrates
how to
do this.
float4 PSMain(
in PSInput input,
in
uint coverageMask :
SV_Coverage
) :
SV_Target0
{
// Determine our
coordinate
for
sampling the texture based on
//
the current
screen position
int2 sampleCoord = int2( input.ScreenPos.xy );
float3 diffuse
= 0;
float3 specular = 0;
float numSamplesApplied = 0.0f;
//
Loop through
the
MSAA
samples
and modulate diffuse
and
specular
//
lighting by the albedo
for (
uint
i
=
0; i <
NUMSUBSAMPLES;
++i )
{
// We
only want to
apply a
lighting sample
if
the geometry
we've
//
rasterized
passed
the
coverage
test
for the corresponding
//
MSAA
sample
point
if ( coverageMask & ( 1 <<
i
) )
Search WWH ::
Custom Search