Graphics Reference
In-Depth Information
For performance reasons, it is best to group properties with
a similar update frequency into the same constant buffer, for
example, those updated per frame and those updated per object.
The vertex shader structures hold the position and color component. When initializing the IA
stage, we will see how the
VertexShaderInput
structure and the input layout match up.
Ultimately, these structures define our vertex layout.
There are two shader entry points:
VSMain
represents the vertex shader and
PSMain
is
the pixel shader. The vertex shader will transform vertices from local object space into a
homogeneous projection space based on the world, view, and projection (by applying the
WVP matrix). The return value is the result of this along with the color, and it is the same
structure that is passed into the pixel shader. This shader will run for each vertex.
The pixel shader is provided with an interpolated
VertexShaderOutput
structure
by the rasterizer state—this pixel shader is doing nothing but returning the unchanged
color component.
Next, we implemented our Direct3D application class. This houses the rendering loop,
and it initializes the Direct3D pipeline and our individual renderers. We descend from
D3DApplicationDesktop
, which simply creates the
SwapChain1
instance based on
a
System.Windows.Form
, as demonstrated in
Chapter 1
,
Getting Started with Direct3D
.
We provided a compatible constructor that passes the form through to the base class.
Resource Initialization
The
CreateDeviceDependentResources
implementation that is provided creates our
device-dependent resources and initializes the IA and OM stages of the rendering pipeline.
First, we created our shader programs by compiling them. For example, we compile our vertex
shader by using
ShaderBytecode.CompileFromFile("Simple.hlsl", "
VSMain
",
"
vs_5_0
", shaderFlags)
. This compiles
Simple.hlsl
by using the
vs_5_0
shader
profile and uses
VSMain
as the entry point. If we have compiled for Debug, we are telling the
shader to include the debug information via the
shaderFlags
enumeration value. This allows
us to step through the shaders when using the graphics debugging tools in Visual Studio.
After the shaders are compiled, we prepare the input layout for the IA. This is used to tell
the IA in which memory layout the vertices can be expected when copying them to the
VertexShaderInput
structure in our shader file.
new InputLayout(..., new[] {
new InputElement("
SV_Position
",0,
Format.R32G32B32A32_Float
, 0, 0),
new InputElement("
COLOR
", 0,
Format.R32G32B32A32_Float
, 16, 0)
});