Add **stencil buffer** support to GPUPipelineState

This commit is contained in:
Wojtek Figat
2023-06-19 13:46:37 +02:00
parent a6353c0bb9
commit f952a392de
5 changed files with 188 additions and 134 deletions

View File

@@ -9,6 +9,31 @@
#include "Engine/GraphicsDevice/DirectX/RenderToolsDX.h"
#include "Engine/Graphics/PixelFormatExtensions.h"
static D3D12_STENCIL_OP ToStencilOp(StencilOperation value)
{
switch (value)
{
case StencilOperation::Keep:
return D3D12_STENCIL_OP_KEEP;
case StencilOperation::Zero:
return D3D12_STENCIL_OP_ZERO;
case StencilOperation::Replace:
return D3D12_STENCIL_OP_REPLACE;
case StencilOperation::IncrementSaturated:
return D3D12_STENCIL_OP_INCR_SAT;
case StencilOperation::DecrementSaturated:
return D3D12_STENCIL_OP_DECR_SAT;
case StencilOperation::Invert:
return D3D12_STENCIL_OP_INVERT;
case StencilOperation::Increment:
return D3D12_STENCIL_OP_INCR;
case StencilOperation::Decrement:
return D3D12_STENCIL_OP_DECR;
default:
return D3D12_STENCIL_OP_KEEP;
};
}
GPUPipelineStateDX12::GPUPipelineStateDX12(GPUDeviceDX12* device)
: GPUResourceDX12(device, StringView::Empty)
, _states(16)
@@ -169,12 +194,14 @@ bool GPUPipelineStateDX12::Init(const Description& desc)
psDesc.DepthStencilState.DepthEnable = !!desc.DepthEnable;
psDesc.DepthStencilState.DepthWriteMask = desc.DepthWriteEnable ? D3D12_DEPTH_WRITE_MASK_ALL : D3D12_DEPTH_WRITE_MASK_ZERO;
psDesc.DepthStencilState.DepthFunc = static_cast<D3D12_COMPARISON_FUNC>(desc.DepthFunc);
psDesc.DepthStencilState.StencilEnable = FALSE;
psDesc.DepthStencilState.StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK;
psDesc.DepthStencilState.StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK;
const D3D12_DEPTH_STENCILOP_DESC defaultStencilOp = { D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_COMPARISON_FUNC_ALWAYS };
psDesc.DepthStencilState.FrontFace = defaultStencilOp;
psDesc.DepthStencilState.BackFace = defaultStencilOp;
psDesc.DepthStencilState.StencilEnable = !!desc.StencilEnable;
psDesc.DepthStencilState.StencilReadMask = desc.StencilReadMask;
psDesc.DepthStencilState.StencilWriteMask = desc.StencilWriteMask;
psDesc.DepthStencilState.FrontFace.StencilFailOp = ToStencilOp(desc.StencilFailOp);
psDesc.DepthStencilState.FrontFace.StencilDepthFailOp = ToStencilOp(desc.StencilDepthFailOp);
psDesc.DepthStencilState.FrontFace.StencilPassOp = ToStencilOp(desc.StencilPassOp);
psDesc.DepthStencilState.FrontFace.StencilFunc = static_cast<D3D12_COMPARISON_FUNC>(desc.StencilFunc);
psDesc.DepthStencilState.BackFace = psDesc.DepthStencilState.FrontFace;
// Rasterizer State
psDesc.RasterizerState.FillMode = desc.Wireframe ? D3D12_FILL_MODE_WIREFRAME : D3D12_FILL_MODE_SOLID;

View File

@@ -26,71 +26,22 @@ namespace DescriptorSet
{
// Vertex shader stage
Vertex = 0,
// Pixel shader stage
Pixel = 1,
// Geometry shader stage
Geometry = 2,
// Hull shader stage
Hull = 3,
// Domain shader stage
Domain = 4,
// Graphics pipeline stages count
GraphicsStagesCount = 5,
// Compute pipeline slot
Compute = 0,
// The maximum amount of slots for all stages
Max = 5,
};
inline Stage GetSetForFrequency(ShaderStage stage)
{
switch (stage)
{
case ShaderStage::Vertex:
return Vertex;
case ShaderStage::Hull:
return Hull;
case ShaderStage::Domain:
return Domain;
case ShaderStage::Pixel:
return Pixel;
case ShaderStage::Geometry:
return Geometry;
case ShaderStage::Compute:
return Compute;
default:
CRASH;
return Max;
}
}
inline ShaderStage GetFrequencyForGfxSet(Stage stage)
{
switch (stage)
{
case Vertex:
return ShaderStage::Vertex;
case Hull:
return ShaderStage::Hull;
case Domain:
return ShaderStage::Domain;
case Pixel:
return ShaderStage::Pixel;
case Geometry:
return ShaderStage::Geometry;
default:
CRASH;
return (ShaderStage)ShaderStage_Count;
}
}
template<typename T>
inline bool CopyAndReturnNotEqual(T& a, T b)
{

View File

@@ -9,6 +9,31 @@
#include "Engine/Core/Log.h"
#include "Engine/Profiler/ProfilerCPU.h"
static VkStencilOp ToVulkanStencilOp(const StencilOperation value)
{
switch (value)
{
case StencilOperation::Keep:
return VK_STENCIL_OP_KEEP;
case StencilOperation::Zero:
return VK_STENCIL_OP_ZERO;
case StencilOperation::Replace:
return VK_STENCIL_OP_REPLACE;
case StencilOperation::IncrementSaturated:
return VK_STENCIL_OP_INCREMENT_AND_CLAMP;
case StencilOperation::DecrementSaturated:
return VK_STENCIL_OP_DECREMENT_AND_CLAMP;
case StencilOperation::Invert:
return VK_STENCIL_OP_INVERT;
case StencilOperation::Increment:
return VK_STENCIL_OP_INCREMENT_AND_WRAP;
case StencilOperation::Decrement:
return VK_STENCIL_OP_DECREMENT_AND_WRAP;
default:
return VK_STENCIL_OP_KEEP;
}
}
GPUShaderProgramCSVulkan::~GPUShaderProgramCSVulkan()
{
if (_pipelineState)
@@ -289,6 +314,14 @@ bool GPUPipelineStateVulkan::Init(const Description& desc)
_descDepthStencil.depthTestEnable = desc.DepthEnable;
_descDepthStencil.depthWriteEnable = desc.DepthWriteEnable;
_descDepthStencil.depthCompareOp = RenderToolsVulkan::ToVulkanCompareOp(desc.DepthFunc);
_descDepthStencil.stencilTestEnable = desc.StencilEnable;
_descDepthStencil.front.compareMask = desc.StencilReadMask;
_descDepthStencil.front.writeMask = desc.StencilWriteMask;
_descDepthStencil.front.compareOp = RenderToolsVulkan::ToVulkanCompareOp(desc.StencilFunc);
_descDepthStencil.front.failOp = ToVulkanStencilOp(desc.StencilFailOp);
_descDepthStencil.front.depthFailOp = ToVulkanStencilOp(desc.StencilDepthFailOp);
_descDepthStencil.front.passOp = ToVulkanStencilOp(desc.StencilPassOp);
_descDepthStencil.front = _descDepthStencil.back;
_desc.pDepthStencilState = &_descDepthStencil;
// Rasterization