Pipeline states
- rasterizer state
- depth and stencil state
- blend state
- effects
- input layout
- output descriptionState is compiled into immutable objects, which describe the whole pipeline. They are then bound using a CommandList.
Code: Create state objects
The class let you set states independently, while caching the underlying pipeline states.
Code: Mutable pipeline state
var mutablePipelineState = new MutablePipelineState();
// Setting values and rebuilding
mutablePipelineState.State.BlendState = BlendStates.AlphaBlend
mutablePipelineState.Update
CommandList.SetPipelineState(mutablePipelineState.CurrentState);
Rasterizer state
The rasterizer can be set using the RasterizerState property. A set of predefined descriptions is held by the class. They deal with the cull mode, and should be enough for most use cases:
pipelineStateDescription.RasterizerState = RasterizerStates.CullNone;
pipelineStateDescription.RasterizerState = RasterizerStates.CullFront;
pipelineStateDescription.RasterizerState = RasterizerStates.CullBack;
You can create your own custom state. For the list of options and default values, see the API documentation.
Code: Custom rasterizer states
rasterizerStateDescription.ScissorTestEnable = true; // enables the scissor test
pipelineStateDescription.RasterizerState = rasterizerStateDescription;
Depth and stencil states
The DepthStencilState property contains the depth and stencil states. A set of commonly used states is defined by the class:
- Default: depth read and write with a less-than comparison
- : read and write with a greater-or-equals comparison
- DepthRead: read only with a less-than comparison
- : neither read nor writeCode: Setting the depth state
You can also set custom depth and stencil states. For the list of options and default values, see the DepthStencilStateDescription API documentation.
// depth test is enabled but it doesn't write
var depthStencilStateDescription = new DepthStencilStateDescription(true, false);
pipelineStateDescription.DepthStencilState = depthStencilStateDescription;
The stencil reference isn't part of the . It's set using SetStencilReference(Int32).
Code: Set the stencil reference
CommandList.SetStencilReference(2);
Blend state
The and SampleMask properties control blending. The class defines a set of commonly used blend states:
- Additive: sums the colors
- : sums using the alpha of the source on both colors
- Opaque: replaces the colorCode: Set the blend state
// Set common blend states
pipelineStateDescription.BlendState = BlendStates.Additive;
pipelineStateDescription.BlendState = BlendStates.AlphaBlend;
pipelineStateDescription.BlendState = BlendStates.NonPremultiplied;
pipelineStateDescription.BlendState = BlendStates.Opaque;
// Set the sample mask
pipelineStateDescription.SampleMask = 0xFFFFFFFF;
You can set custom depth and blend states. For a list of options and default values, see the API documentation.
Code: Custom blend state
The blend factor isn't part of the PipelineState. It's set using .
Code: Set the blend factor
CommandList.SetBlendFactor(Color4.White);
The pipeline state also includes the shaders you want to use for drawing.To bind an Effect to the pipeline, set the and RootSignature properties of the to the matching values of the effect.
The RootSignature describes the number and kind of resources expected by the effect. The next chapter covers how to to the pipeline.
Code: Bind an effect
var effectInstance = new EffectInstance(EffectSystem.LoadEffect("MyEffect").WaitForResult());
pipelineStateDescription.EffectBytecode = effectInstance.Effect.Bytecode;
pipelineStateDescription.RootSignature = effectInstance.RootSignature;
The pipeline state describes the layout in which vertices are sent to the device through the InputElements and properties.
The Draw vertices page describes how to create custom vertex buffers and their in more detail.
Code: Set an input layout
VertexDeclaration vertexDeclaration = ...
pipelineStateDescription.InputElements = vertexDeclaration.CreateInputElements();
pipelineStateDescription.PrimitiveType = PrimitiveType.TriangleStrip;
The Output property of the defines the number and PixelFormat of all bound render textures.
For information on how to bind render textures to the pipeline, see .
Code: Create an output description
Code: Capture output description
mutablePipelineState.State.Output.CaptureState(CommandList);
mutablePipelineState.Update();