i
i
i
i
i
i
i
i
3.4. The Vertex Shader 39
this object’s triangles (or lines or points) by essentially creating vertices
with positions and colors. A second object could use the same array of
positions (along with a different model transform matrix) and a different
array of colors for its representation. Data representation is discussed in
detail in Section 12.4.5. There is also support in the input assembler to
perform instancing. This allows an object to be drawn a number of times
with some varying data per instance, all with a single draw call. The use
of instancing is covered in Section 15.4.2. The input assembler in DirectX
10 also tags each instance, primitive, and vertex with an identifier number
that can be accessed by any of the shader stages that follow. For earlier
shader models, such data has to be added explicitly to the model.
A triangle mesh is represented by a set of vertices and additional infor-
mation describing which vertices form each triangle. The vertex shader is
the first stage to process the triangle mesh. The data describing what tri-
angles are formed is unavailable to the vertex shader; as its name implies,
it deals exclusively with the incoming vertices. In general terms, the vertex
shader provides a way to modify, create, or ignore values associated with
each polygon’s vertex, such as its color, normal, texture coordinates, and
position. Normally the vertex shader program transforms vertices from
model space to homogeneous clip space; at a minimum, a vertex shader
must always output this location.
This functionality was first introduced in 2001 with DirectX 8. Because
it was the first stage on the pipeline and invoked relatively infrequently, it
could be implemented on either the GPU or the CPU, which would then
send on the results to the GPU for rasterization. Doing so made the tran-
sition from older to newer hardware a matter of speed, not functionality.
All GPUs currently produced support vertex shading.
A vertex shader itself is very much the same as the common core virtual
machine described earlier in Section 3.2. Every vertex passed in is processed
by the vertex shader program, which then outputs a number of values that
are interpolated across a triangle or line.
3
The vertex shader can neither
create nor destroy vertices, and results generated by one vertex cannot be
passed on to another vertex. Since each vertex is treated independently,
any number of shader processors on the GPU can be applied in parallel to
the incoming stream of vertices.
Chapters that follow explain a number of vertex shader effects, such as
shadow volume creation, vertex blending for animating joints, and silhou-
ette rendering. Other uses for the vertex shader include:
• Lens effects, so that the screen appears fish-eyed, underwater, or
otherwise distorted.
3
Older shader models also supported output of the size of a point sprite particle
object, but sprite functionality is now part of the geometry shader.