Graphics Reference
In-Depth Information
Since
DiffuseLight
and
SpecularLight
each have 3 components, storing the full val-
ues in our light buffer would require storing 6 individual floating-point values. This would
require writing to at least two render targets simultaneously, which is often not optimal
from a performance and memory-consumption point of view. Thus, a common optimiza-
tion is to store only the specular intensity rather than the color, which allows the light buffer
to consist of a single 4-component texture. This approach is taken in the lighting shader
code in Listing 11.5.
// Textures
Texture2D NormalTexture
:
register(
t0 );
Texture2D PositionTexture
:
register(
t1 );
//
Constants
cbuffer LightParams
{
float3 LightPos;
float3 LightColor;
float3 LightDirection;
float2 SpotlightAngles;
float4 LightRange;
};
cbuffer CameraParams
{
float3
CameraPos;
}
//
Vertex shader
for
lighting
pass
of
light prepass deferred
rendering
fioat4
VSMain(
in float4
Position
:
POSITION
) :
SV_Position
{
// Just pass
along the
position
of
the full-screen quad
return
Position;
}
//
Helper function
for extracting G-Buffer attributes
void
GetGBufferAttributes(
in
fioat2 screenPos,
out
float3 normal,
out float3 position, out float
specularPower
)
{
//
Determine
our
indices for
sampling
the texture
based
on
the
current
//
screen position
int3
samplelndices
=
int3(
screenPos.
xy,
0 );
float4
normalTex
=
NormalTexture.
Load
(
samplelndices
);
normal
=
normalTex.xyz;
specularPower
=
normalTex.w;
position
=
PositionTexture.
Load
(
samplelndices
).xyz;
}
// Calculates the lighting term
for a
single G-Buffer texel
float4
CalcLighting(
in
float3
normal,
in
float3 position,
in
float
specularPower
)
Search WWH ::
Custom Search