Graphics Reference
In-Depth Information
15. Next we will define our
PerFrame
structure; the structures must be of 16 bytes that
are aligned and evenly divisible by 16, thus the extra
_padding0
property.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct PerFrame
{
public SharpDX.Vector3 CameraPosition;
float _padding0;
}
16. Within our
D3DApp
class, we must change the initialization of the shaders in
CreateDeviceDependentResources
; we must also update our code to
use the constant buffer structures we just defined.
17. Before we can compile our shaders at runtime that use include directives, we
must provide an implementation of the
ID3DInclude
interface to the HLSL
compiler. Within the sample rendering framework, this is implemented within
the class,
Common.HLSLFileIncludeHandler
. A static helper class
Common.
HLSLCompiler
wraps this logic for us.
18. Let's now compile the new shaders using the
Common.HLSLCompiler.
CompileFromFile
method; we also need to add a new private member field
for the depth pixel shader. The following code snippet shows how to compile
the depth shader using the
Common.HLSLCompiler
class:
// Compile and create the depth pixel shader
using (var bytecode = HLSLCompiler.CompileFromFile(
@"Shaders\DepthPS.hlsl", "PSMain", "vs_5_0"))
depthShader = ToDispose(
new VertexShader(device, bytecode));
...
The graphics debugger (
Alt
+
F5
) can be used to step through the
vertex and pixel shaders using the HLSL debugger in Visual Studio
2013 (any edition) or non-express versions of Visual Studio 2012.
19. Lastly, we must create the new
Buffer
instances. Within
CreateDeviceDependentResources
, initialize the new buffers. This is done
exactly as before, except here we pass the size of the appropriate structure.
perObjectBuffer
= ToDispose(new
SharpDX.Direct3D11.Buffer(device,
Utilities.SizeOf<
ConstantBuffers.PerObject
>(),
ResourceUsage.Default, BindFlags.ConstantBuffer,
CpuAccessFlags.None, ResourceOptionFlags.None, 0));