Pipeline states
Xenko gives you total control over the graphics pipeline state. This includes:
- PipelineState objects, which describe the whole pipeline. They are then bound using a CommandList.
Code: Create state objects
MutablePipelineState class let you set states independently, while caching the underlying pipeline states. Code: Mutable pipeline state// Creating pipeline state objectvar pipelineStateDescription = new PipelineStateDescription();var pipelineState = PipelineState.New(GraphicsDevice, ref pipelineStateDescription);// Applying the state to the pipelineCommandList.SetPipelineState(pipelineState);
// Creating the pipeline state objectvar mutablePipelineState = new MutablePipelineState();// Setting values and rebuildingmutablePipelineState.State.BlendState = BlendStates.AlphaBlendmutablePipelineState.Update// Applying the state to the pipelineCommandList.SetPipelineState(mutablePipelineState.CurrentState);
Rasterizer state
RasterizerState property. A set of predefined descriptions is held by the RasterizerStates class. They deal with the cull mode, and should be enough for most use cases: - CullNone: no culling
- CullFront: front-face culling
- CullBack: back-face cullingCode: Set the cull mode
RasterizerStateDescription API documentation. Code: Custom rasterizer statespipelineStateDescription.RasterizerState = RasterizerStates.CullNone;pipelineStateDescription.RasterizerState = RasterizerStates.CullFront;pipelineStateDescription.RasterizerState = RasterizerStates.CullBack;
var rasterizerStateDescription = new RasterizerStateDescription(CullMode.Front);rasterizerStateDescription.ScissorTestEnable = true; // enables the scissor testpipelineStateDescription.RasterizerState = rasterizerStateDescription;
Depth and stencil states
DepthStencilState property contains the depth and stencil states. A set of commonly used states is defined by the DepthStencilStates class: - Default: depth read and write with a less-than comparison
- DefaultInverse: read and write with a greater-or-equals comparison
- DepthRead: read only with a less-than comparison
- None: neither read nor writeCode: Setting the depth state
DepthStencilStateDescription API documentation. Code: Custom depth and stencil statepipelineStateDescription.DepthStencilState = DepthStencilStates.Default;pipelineStateDescription.DepthStencilState = DepthStencilStates.DefaultInverse;pipelineStateDescription.DepthStencilState = DepthStencilStates.DepthRead;pipelineStateDescription.DepthStencilState = DepthStencilStates.None;
PipelineState. It’s set using SetStencilReference(Int32). Code: Set the stencil reference// depth test is enabled but it doesn't writevar depthStencilStateDescription = new DepthStencilStateDescription(true, false);pipelineStateDescription.DepthStencilState = depthStencilStateDescription;
CommandList.SetStencilReference(2);
Blend state
BlendState and SampleMask properties control blending. The BlendStates class defines a set of commonly used blend states: - Additive: sums the colors
- AlphaBlend: sums the colors using the alpha of the source on the destination color
- NonPremultiplied: sums using the alpha of the source on both colors
- Opaque: replaces the colorCode: Set the blend state
BlendStateDescription API documentation. Code: Custom blend state// Set common blend statespipelineStateDescription.BlendState = BlendStates.Additive;pipelineStateDescription.BlendState = BlendStates.AlphaBlend;pipelineStateDescription.BlendState = BlendStates.NonPremultiplied;pipelineStateDescription.BlendState = BlendStates.Opaque;// Set the sample maskpipelineStateDescription.SampleMask = 0xFFFFFFFF;
PipelineState. It’s set using SetBlendFactor(Color4). Code: Set the blend factor// create the object describing the blend state per targetvar blendStateRenderTargetDescription = new BlendStateRenderTargetDescription();blendStateRenderTargetDescription.BlendEnable = true;blendStateRenderTargetDescription.ColorSourceBlend = Blend.SourceColor;// etc.// create the blendStateDescription objectvar blendStateDescription = new BlendStateDescription(Blend.SourceColor, Blend.InverseSourceColor);blendStateDescription.AlphaToCoverageEnable = true; // enable alpha to coverageblendStateDescription.RenderTargets[0] = blendStateRenderTargetDescription;pipelineStateDescription.BlendState = blendStateDescription;
CommandList.SetBlendFactor(Color4.White);
Effects
Effect to the pipeline, set the EffectBytecodeand RootSignature properties of the PipelineStateDescription to the matching values of the effect. EffectBytecode contains the actual shader programs. For more information, see Effects and Shaders. RootSignature describes the number and kind of resources expected by the effect. The next chapter covers how to bind resources to the pipeline. Code: Bind an effectvar effectInstance = new EffectInstance(EffectSystem.LoadEffect("MyEffect").WaitForResult());pipelineStateDescription.EffectBytecode = effectInstance.Effect.Bytecode;pipelineStateDescription.RootSignature = effectInstance.RootSignature;
Input layout
InputElements and PrimitiveType properties. Draw vertices page describes how to create custom vertex buffers and their VertexDeclaration in more detail. Code: Set an input layoutVertexDeclaration vertexDeclaration = ...pipelineStateDescription.InputElements = vertexDeclaration.CreateInputElements();pipelineStateDescription.PrimitiveType = PrimitiveType.TriangleStrip;
Output description
Output property of the PipelineStateDescription defines the number and PixelFormat of all bound render textures. Textures and render textures. Code: Create an output description
CaptureState(CommandList) to retrieve the output description last set on a CommandList. This is especially useful in combination with MutablePipelineState, when the render target might not be known up front. Code: Capture output descriptionvar renderOutputDescription = new RenderOutputDescription(GraphicsDevice.Presenter.BackBuffer.Format, GraphicsDevice.Presenter.DepthStencilBuffer.Format);pipelineStateDescription.Output = renderOutputDescription;
mutablePipelineState.State.Output.CaptureState(CommandList);mutablePipelineState.Update();
