-
Notifications
You must be signed in to change notification settings - Fork 411
CommonStates
The CommonStates class is a static class which defines the most common combinations of Direct3D rendering states.
Related tutorials: Sprites and textures
#include <CommonStates.h>
Each state provides a D3D12_*_DESC
structure which can be used to create a Pipeline State Object (PSO):
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.RasterizerState = CommonStates::CullNone;
psoDesc.BlendState = CommonStates::Opaque;
psoDesc.DepthStencilState = CommonStates::DepthDefault;
...
DX::ThrowIfFailed(
device->CreateGraphicsPipelineState(&psoDesc,
IID_PPV_ARGS(m_pipelineState.ReleaseAndGetAddressOf())));
They are typically used with EffectPipelineStateDescription:
RenderTargetState rtState(m_deviceResources->GetBackBufferFormat(),
m_deviceResources->GetDepthBufferFormat());
EffectPipelineStateDescription pd(
&VertexPositionColor::InputLayout,
CommonStates::Opaque,
CommonStates::DepthDefault,
CommonStates::CullNone,
rtState);
- static const D3D12_BLEND_DESC Opaque;
- static const D3D12_BLEND_DESC AlphaBlend;
- static const D3D12_BLEND_DESC Additive;
- static const D3D12_BLEND_DESC NonPremultiplied;
For standard drawing, typically you should make use of Opaque
.
For drawing alpha-blended objects, you should use AlphaBlend
if using premultiplied alpha, or
NonPremultiplied
if using 'straight' alpha.
For multipass rendering, you'd typically use Additive
.
- static const D3D12_DEPTH_STENCIL_DESC DepthNone;
- static const D3D12_DEPTH_STENCIL_DESC DepthDefault;
- static const D3D12_DEPTH_STENCIL_DESC DepthRead;
For standard rendering with a z-buffer, you should use DepthDefault
.
For drawing alpha blended objects (which is typically done after all opaque objects have been drawn), use DepthRead
which will respect the existing z-buffer values, but will not update them with 'closer' pixels.
For drawing objects without any depth-sort at all, use DepthNone
.
- static const D3D12_RASTERIZER_DESC CullNone;
- static const D3D12_RASTERIZER_DESC CullClockwise;
- static const D3D12_RASTERIZER_DESC CullCounterClockwise;
- static const D3D12_RASTERIZER_DESC Wireframe;
For default geometry winding use CullCounterClockwise
. For inverted winding (typically when using assets designed for left-handed coordinates but rendering with right-handed coordinates or vice-versa), use CullClockwise
.
For "double-sided" geometry, use CullNone
. Keep in mind this is a potentially large performance hit, so use it sparingly.
Wireframe
is a wireframe rendering mode and shows both sides of the geometry.
- static const D3D12_SAMPLER_DESC PointWrap;
- static const D3D12_SAMPLER_DESC PointClamp;
- static const D3D12_SAMPLER_DESC LinearWrap;
- static const D3D12_SAMPLER_DESC LinearClamp;
- static const D3D12_SAMPLER_DESC AnisotropicWrap;
- static const D3D12_SAMPLER_DESC AnisotropicClamp;
These helpers return D3D12_STATIC_SAMPLER_DESC
versions:
- CommonStates::StaticPointWrap(shaderRegister, shaderVisbility, registerSpace);
- CommonStates::StaticPointClamp(shaderRegister, shaderVisbility, registerSpace);
- CommonStates::StaticLinearWrap(shaderRegister, shaderVisbility, registerSpace);
- CommonStates::StaticLinearClamp(shaderRegister, shaderVisbility, registerSpace);
- CommonStates::StaticAnisotropicWrap(shaderRegister, shaderVisbility, registerSpace);
- CommonStates::StaticAnisotropicClamp(shaderRegister, shaderVisbility, registerSpace);
AnisotropicWrap
is the default sampler settings for DirectX 12 and the built-in effects.
These common states are equivalent to using the following descriptors:
CD3DX12_DEFAULT def;
// Opaque
CD3DX12_BLEND_DESC desc(def);
// AlphaBlend
CD3DX12_BLEND_DESC desc(def);
desc.RenderTarget[0].BlendEnable = TRUE;
desc.RenderTarget[0].SrcBlend =
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
desc.RenderTarget[0].DestBlend =
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
// Additive
CD3DX12_BLEND_DESC desc(def);
desc.RenderTarget[0].BlendEnable = TRUE;
desc.RenderTarget[0].SrcBlend =
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend =
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
// NonPremultiplied
CD3DX12_BLEND_DESC desc(def);
desc.RenderTarget[0].BlendEnable = TRUE;
desc.RenderTarget[0].SrcBlend =
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend =
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
CD3DX12_DEFAULT def;
// DepthNone
CD3DX12_DEPTH_STENCIL_DESC desc(def);
desc.DepthEnable = FALSE;
desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
desc.DepthFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL;
// DepthDefault
CD3DX12_DEPTH_STENCIL_DESC desc(def);
desc.DepthFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL;
// DepthRead
CD3DX12_DEPTH_STENCIL_DESC desc(def);
desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
desc.DepthFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL;
// CullNone
CD3DX12_RASTERIZER_DESC desc(D3D12_FILL_MODE_SOLID, D3D12_CULL_MODE_NONE,
FALSE /* FrontCounterClockwise */,
D3D11_DEFAULT_DEPTH_BIAS,
D3D11_DEFAULT_DEPTH_BIAS_CLAMP,
D3D11_DEFAULT_SLOPE_SCALED_DEPTH_BIAS,
TRUE /* DepthClipEnable */,
TRUE /* MultisampleEnable */,
FALSE /* AntialiasedLineEnable */,
0 /* ForceSampleCount */,
D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF);
// CullClockwise
CD3DX12_RASTERIZER_DESC desc(D3D12_FILL_MODE_SOLID, D3D12_CULL_MODE_FRONT,
FALSE /* FrontCounterClockwise */,
D3D11_DEFAULT_DEPTH_BIAS,
D3D11_DEFAULT_DEPTH_BIAS_CLAMP,
D3D11_DEFAULT_SLOPE_SCALED_DEPTH_BIAS,
TRUE /* DepthClipEnable */,
TRUE /* MultisampleEnable */,
FALSE /* AntialiasedLineEnable */,
0 /* ForceSampleCount */,
D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF);
// CullCounterClockwise
CD3DX12_RASTERIZER_DESC desc(D3D12_FILL_MODE_SOLID, D3D12_CULL_MODE_BACK,
FALSE /* FrontCounterClockwise */,
D3D11_DEFAULT_DEPTH_BIAS,
D3D11_DEFAULT_DEPTH_BIAS_CLAMP,
D3D11_DEFAULT_SLOPE_SCALED_DEPTH_BIAS,
TRUE /* DepthClipEnable */,
TRUE /* MultisampleEnable */,
FALSE /* AntialiasedLineEnable */,
0 /* ForceSampleCount */,
D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF);
// Wireframe
CD3DX12_RASTERIZER_DESC desc(D3D12_FILL_MODE_WIREFRAME, D3D12_CULL_MODE_NONE,
FALSE /* FrontCounterClockwise */,
D3D11_DEFAULT_DEPTH_BIAS,
D3D11_DEFAULT_DEPTH_BIAS_CLAMP,
D3D11_DEFAULT_SLOPE_SCALED_DEPTH_BIAS,
TRUE /* DepthClipEnable */,
TRUE /* MultisampleEnable */,
FALSE /* AntialiasedLineEnable */,
0 /* ForceSampleCount */,
D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF);
// PointWrap
D3D12_SAMPLER_DESC desc = { D3D12_FILTER_MIN_MAG_MIP_POINT,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
0, D3D12_MAX_MAXANISOTROPY, D3D12_COMPARISON_FUNC_NEVER,
{ 0, 0, 0, 0 }, 0, D3D12_FLOAT32_MAX };
// PointClamp
D3D12_SAMPLER_DESC desc = { D3D12_FILTER_MIN_MAG_MIP_POINT,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
0, D3D12_MAX_MAXANISOTROPY, D3D12_COMPARISON_FUNC_NEVER,
{ 0, 0, 0, 0 }, 0, D3D12_FLOAT32_MAX };
// LinearWrap
D3D12_SAMPLER_DESC desc = { D3D12_FILTER_MIN_MAG_MIP_LINEAR,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
0, D3D12_MAX_MAXANISOTROPY, D3D12_COMPARISON_FUNC_NEVER,
{ 0, 0, 0, 0 }, 0, D3D12_FLOAT32_MAX };
// LinearClamp
D3D12_SAMPLER_DESC desc = { D3D12_FILTER_MIN_MAG_MIP_LINEAR,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
0, D3D12_MAX_MAXANISOTROPY, D3D12_COMPARISON_FUNC_NEVER,
{ 0, 0, 0, 0 }, 0, D3D12_FLOAT32_MAX };
// AnisotropicWrap
D3D12_SAMPLER_DESC desc = { D3D12_FILTER_ANISOTROPIC,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
0, D3D12_MAX_MAXANISOTROPY, D3D12_COMPARISON_FUNC_NEVER,
{ 0, 0, 0, 0 }, 0, D3D12_FLOAT32_MAX };
// AnisotropicClamp
D3D12_SAMPLER_DESC desc = { D3D12_FILTER_ANISOTROPIC,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
0, D3D12_MAX_MAXANISOTROPY, D3D12_COMPARISON_FUNC_NEVER,
{ 0, 0, 0, 0 }, 0, D3D12_FLOAT32_MAX };
All content and source code for this package are subject to the terms of the MIT License.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
- Universal Windows Platform apps
- Windows desktop apps
- Windows 11
- Windows 10
- Xbox One
- Xbox Series X|S
- x86
- x64
- ARM64
- Visual Studio 2022
- Visual Studio 2019 (16.11)
- clang/LLVM v12 - v18
- MinGW 12.2, 13.2
- CMake 3.20