Introduction to 3D Game Programming with DirectX.9.0 - F. D. Luna
.pdf290Chapter 16
inout—Shortcut that denotes a parameter as both in and out. Specify inout if you wish to use a parameter for both input and output.
void square(inout float x)
{
x = x * x;
}
Here we input the number to be squared through x and also return the square of x through x.
16.7 Built-in Functions
HLSL has a rich set of built-in functions that are useful for 3D graphics. The following table is an abridged list of them. In the next two chapters we will get practice using some of these functions. For now, just get familiar with them.
Note: For further reference, the complete list of the built-in HLSL functions can be found in the DirectX documentation, under the Contents tab, then DirectX Graphics\Reference\Shader Reference\High Level Shader Language\Intrinsic Functions.
Function |
Description |
||||||||||
abs(x) |
Returns |x|. |
||||||||||
ceil(x) |
Returns the smallest integer ! x. |
||||||||||
clamp(x, a, b) |
Clamps x to the range [a, b] and returns the |
||||||||||
|
result. |
||||||||||
cos(x) |
Returns the cosine of x, where x is in radians. |
||||||||||
cross(u, v) |
Returns u v. |
||||||||||
degrees(x) |
Converts x from radians to degrees. |
||||||||||
determinant(M) |
Returns the determinant det(M). |
||||||||||
distance(u, v) |
Returns the distance |
|
|
|
v u |
|
|
|
between the points u |
||
|
|
|
|
||||||||
|
and v. |
||||||||||
dot(u, v) |
Returns u v. |
||||||||||
floor(x) |
Returns the greatest integer x. |
||||||||||
length(v) |
Returns |
|
. |
||||||||
v |
|||||||||||
lerp(u, v, t) |
Linearly interpolates between u and v based on |
||||||||||
|
the parameter t [0, 1]. |
||||||||||
log(x) |
Returns ln(x). |
||||||||||
log10(x) |
Returns log10(x). |
||||||||||
log2(x) |
Returns log2(x). |
Chapter 17
Introduction to Vertex Shaders
A vertex shader is a program executed on the graphics card’s GPU that replaces the transformation and lighting stage in the fixed function pipeline. (This is not 100 percent true, as vertex shaders can be emulated in software by the Direct3D runtime if the hardware does not support vertex shaders.) Figure 17.1 illustrates the section in the pipeline that the vertex shader replaces.
Figure 17.1: The vertex shader replaces the lighting and transformation
stage of the fixed function pipeline.
From Figure 17.1 we see that vertices are input into the vertex shader in local coordinates and the vertex shader must output lit (colored) vertices in homogeneous clip space. (We didn’t delve into the details of the projection transformation in this book for simplicity reasons. But the space to which the projection matrix transforms vertices is called homogeneous clip space. Therefore, to transform a vertex from local space to homogenous clip space, we must apply the following sequence of transformations: world transformation, view transformation, and projection transformation, which are done by the world matrix, view matrix, and projection matrix, respectively.) For point primitives, a vertex shader can also be used to manipulate the vertex size per vertex.
Because a vertex shader is a custom program that we write (in HLSL), we obtain a huge amount of flexibility in the graphical effects we can achieve. For example, with vertex shaders we can use any
293