Graphics Reference
In-Depth Information
Figure 2.12. A sample constant buffer showing the contents of its structure.
can be tailored to more or less any combination of the basic HLSL types, as well as to
structures composed of these basic types. We will cover the HLSL data types in more de-
tail in Chapter 6, "High Level Shading Language," but we'll mention now that these types
include scalars, vectors, matrices, arrays of these types, class instances, and combinations
of each of these types within structures. A single combination of these types is depicted in
Figure 2.12.
The constant buffer is somewhat different than the other buffers that we have dis-
cussed thus far. Both the vertex buffer and the index buffer define a basic data element and
then repeat that element many times in an array-like fashion. The constant buffer defines
a basic element, but it doesn't provide more than one instance of the element—the buffer
is created large enough to fit the desired information, but not larger. This implies that the
constant buffer implements a structure instead of an array.
Using constant buffers. Each of the programmable pipeline stages can accept one or more
constant buffers. It then makes the information in the buffers available for use in the shader
program. The data is accessed as if the structure contents were declared globally in the
shader program. This means that the elements of each structure must have unique names
at this pseudo-global scope. This ability provides a significant amount of flexibility for
adding variation to any of the shader programs that are required for a particular rendering
algorithm. It is important to note that a buffer created for binding as a constant buffer may
not be bound to any other type of connection points on the pipeline. In practice, this is not a
problem, since the contents of a constant buffer are already available to all of the program-
mable pipeline stages anyway. The locations on the pipeline that are available for binding
constant buffers are highlighted in Figure 2.13.
The ability to use relatively large constant buffers does not mean that you should
automatically create one large buffer for all of the variables that are needed in a shader
program. Since constant buffers are updated between uses by the CPU, their contents must
be uploaded to the GPU after any changes. Let's assume a hypothetical situation as an ex-
ample. Suppose there are ten parameters needed by a shader program, but only two of them
change between each time that an object is rendered. In this case, we would be writing all
Search WWH ::




Custom Search