Graphics Reference
In-Depth Information
parameters that specify the image position to be evaluated. (Regardless of the
image size, parameter values between 0 and 1 specify positions from one edge of
the image to the other. Parameter values outside
[
0, 1
]
may wrap, reflect, or clamp,
as specified ahead of time.) Thus, a shader can specify coordinates that have been
interpolated across the primitive during rasterization (i.e., they can implement tra-
ditional texture correspondence) or they can choose to specify coordinates that
have been computed arbitrarily, perhaps as functions of the results of other texture
image interpolations. Such
dependent
texture image interpolation is a very pow-
erful feature of modern GPUs, but, as we will see in Section 38.7.2, it complicates
their implementation.
Listing 38.2 replaces the constant-based color attenuation of the example frag-
ment shader in Listing 38.1 with attenuation defined by a 1D texture image:
brightness_table
. Like the global variable
brightness
,
brightness_table
is
specified by the application prior to rendering and remains constant throughout
rendering. Unlike
brightness
, however, which was stored in a GPU register (e.g.,
within a
core
in Figure 38.4),
brightness_table
is bulk memory state (see Sec-
tion 38.3), stored in the off-chip GPU memory system (e.g.,
GDDR3 memory
in
Figure 38.4). Sampler
s
is a simple data structure that specifies a texture image
(
brightness_table
) and the technique to be used to evaluate it (linear interpola-
tion between texels).
Listing 38.2: Another simple HLSL fragment shader.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
texture1D brightness_table;
sampler1D s = sampler_State {
texture = brightness_table;
filter = LINEAR;
};
struct
v2f {
float4 Position : POSITION;
float4 Color : COLOR0;
float
TexCoord : TEXCOORD0;
};
struct
f2p {
float
Depth : DEPTH0;
float4 Color : COLOR0;
}
//
attenuate fragment brightness with a 1-D texture
void
main(in v2f Input, out f2p Output) {
Output.Depth = Input.Position.z;
Output.Color = tex1D(s, Input.TexCoord)
*
Input.Color;
}
The input structure
v2f
is extended with a third component,
TexCoord
, with
semantic
TEXCOORD0
, which specifies the texture location to be evaluated. Within
main
, the 1D texture interpolation function
tex1D
is called with sampler
s
(spec-
ifying that
brightness_table
is to be interpolated linearly) and coordinate
Input.TexCoord
(indicating where
brightness_table
is to be evaluated), and
it returns the floating-point result. The four-component vector
Input.Color
is
scaled by this value, and the result, an attenuated, four-component color vector, is
assigned to
Output.Color
.