Game Development Reference
In-Depth Information
HLSL has a C-like syntax that should make it easy to understand, so we'll focus on
the new parts.
At the top you'll notice the cbuffer block. This is our shader representation of a
constant buffer that we create in our game. In this case we provide three matrices,
the world transform, the view transform, and the projection transform. We'll use these
to take the vertex through to screen space, ready for the pixel shader.
Next you'll notice the definition of main, with some interesting additions to the para-
meters. The POSITION part is the semantic for the variable. This tells the GPU
where to use this variable in the overall pipeline. With this you can link the inputPos
variable to the position of the vertex. At the same time, we apply the POSITION se-
mantic to the function to tell the GPU that we want to use the float3 return value
as the output POSITION .
Pixel shaders
Pixel shaders represent the final step in the rendering pipeline that you can control.
Once a vertex emerges from the vertex shader, the pipeline determines which pixels
make up the surface of the triangle, and uses the pixel shader to draw each one,
interpolating between the vertices as required to provide input to the pixel shader.
A very simple pixel shader that sets the entire model to a single color has the follow-
ing code:
float4 main(float4 pos : POSITION) : SV_TARGET
{
return float4(1.0f, 0, 0, 1.0f);
}
This is quite short; however, it illustrates what you need to provide to successfully
render something. In particular, you need to set the SV_TARGET semantic to tell the
pipeline what color to put at that position. To do this, we need to return a float4
that describes the color using the red, green, blue, and alpha channels, in that order.
Setting a value of 1.0f on a channel indicates we want 100 percent of that color in
the final mixed RGB value, and in this case we will see red in the shape of the model.
Search WWH ::




Custom Search