From 13a04c2941e0684b21cdb440c2633ea79300e311 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 2 Apr 2024 14:56:26 +0200 Subject: [PATCH] Add `stencilValue` for stencil buffer clearing --- Source/Engine/Graphics/GPUContext.h | 3 ++- .../Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp | 6 ++---- Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h | 2 +- .../Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.cpp | 6 ++---- Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.h | 2 +- Source/Engine/GraphicsDevice/Null/GPUContextNull.h | 2 +- Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp | 5 ++--- Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.h | 2 +- 8 files changed, 12 insertions(+), 16 deletions(-) diff --git a/Source/Engine/Graphics/GPUContext.h b/Source/Engine/Graphics/GPUContext.h index a222686db..5542e63ec 100644 --- a/Source/Engine/Graphics/GPUContext.h +++ b/Source/Engine/Graphics/GPUContext.h @@ -186,7 +186,8 @@ public: /// /// The depth buffer to clear. /// The clear depth value. - API_FUNCTION() virtual void ClearDepth(GPUTextureView* depthBuffer, float depthValue = 1.0f) = 0; + /// The clear stencil value. + API_FUNCTION() virtual void ClearDepth(GPUTextureView* depthBuffer, float depthValue = 1.0f, uint8 stencilValue = 0) = 0; /// /// Clears an unordered access buffer with a float value. diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp index 1a65167dc..3dc90afd1 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp @@ -151,21 +151,19 @@ bool GPUContextDX11::IsDepthBufferBinded() void GPUContextDX11::Clear(GPUTextureView* rt, const Color& color) { auto rtDX11 = static_cast(rt); - if (rtDX11) { _context->ClearRenderTargetView(rtDX11->RTV(), color.Raw); } } -void GPUContextDX11::ClearDepth(GPUTextureView* depthBuffer, float depthValue) +void GPUContextDX11::ClearDepth(GPUTextureView* depthBuffer, float depthValue, uint8 stencilValue) { auto depthBufferDX11 = static_cast(depthBuffer); - if (depthBufferDX11) { ASSERT(depthBufferDX11->DSV()); - _context->ClearDepthStencilView(depthBufferDX11->DSV(), D3D11_CLEAR_DEPTH, depthValue, 0xff); + _context->ClearDepthStencilView(depthBufferDX11->DSV(), D3D11_CLEAR_DEPTH, depthValue, stencilValue); } } diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h index 8db265065..4540bb652 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h @@ -108,7 +108,7 @@ public: void* GetNativePtr() const override; bool IsDepthBufferBinded() override; void Clear(GPUTextureView* rt, const Color& color) override; - void ClearDepth(GPUTextureView* depthBuffer, float depthValue) override; + void ClearDepth(GPUTextureView* depthBuffer, float depthValue, uint8 stencilValue) override; void ClearUA(GPUBuffer* buf, const Float4& value) override; void ClearUA(GPUBuffer* buf, const uint32 value[4]) override; void ClearUA(GPUTexture* texture, const uint32 value[4]) override; diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.cpp b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.cpp index b64c2236e..2eb143857 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.cpp @@ -703,7 +703,6 @@ bool GPUContextDX12::IsDepthBufferBinded() void GPUContextDX12::Clear(GPUTextureView* rt, const Color& color) { auto rtDX12 = static_cast(rt); - if (rtDX12) { SetResourceState(rtDX12->GetResourceOwner(), D3D12_RESOURCE_STATE_RENDER_TARGET, rtDX12->SubresourceIndex); @@ -713,16 +712,15 @@ void GPUContextDX12::Clear(GPUTextureView* rt, const Color& color) } } -void GPUContextDX12::ClearDepth(GPUTextureView* depthBuffer, float depthValue) +void GPUContextDX12::ClearDepth(GPUTextureView* depthBuffer, float depthValue, uint8 stencilValue) { auto depthBufferDX12 = static_cast(depthBuffer); - if (depthBufferDX12) { SetResourceState(depthBufferDX12->GetResourceOwner(), D3D12_RESOURCE_STATE_DEPTH_WRITE, depthBufferDX12->SubresourceIndex); flushRBs(); - _commandList->ClearDepthStencilView(depthBufferDX12->DSV(), D3D12_CLEAR_FLAG_DEPTH, depthValue, 0xff, 0, nullptr); + _commandList->ClearDepthStencilView(depthBufferDX12->DSV(), D3D12_CLEAR_FLAG_DEPTH, depthValue, stencilValue, 0, nullptr); } } diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.h b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.h index 70ca799eb..72b6e4547 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.h +++ b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.h @@ -159,7 +159,7 @@ public: void* GetNativePtr() const override; bool IsDepthBufferBinded() override; void Clear(GPUTextureView* rt, const Color& color) override; - void ClearDepth(GPUTextureView* depthBuffer, float depthValue) override; + void ClearDepth(GPUTextureView* depthBuffer, float depthValue, uint8 stencilValue) override; void ClearUA(GPUBuffer* buf, const Float4& value) override; void ClearUA(GPUBuffer* buf, const uint32 value[4]) override; void ClearUA(GPUTexture* texture, const uint32 value[4]) override; diff --git a/Source/Engine/GraphicsDevice/Null/GPUContextNull.h b/Source/Engine/GraphicsDevice/Null/GPUContextNull.h index abc52473c..fc185a587 100644 --- a/Source/Engine/GraphicsDevice/Null/GPUContextNull.h +++ b/Source/Engine/GraphicsDevice/Null/GPUContextNull.h @@ -48,7 +48,7 @@ public: { } - void ClearDepth(GPUTextureView* depthBuffer, float depthValue) override + void ClearDepth(GPUTextureView* depthBuffer, float depthValue, uint8 stencilValue) override { } diff --git a/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp b/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp index cb223124f..38ef0eb68 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp +++ b/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp @@ -797,10 +797,9 @@ void GPUContextVulkan::Clear(GPUTextureView* rt, const Color& color) } } -void GPUContextVulkan::ClearDepth(GPUTextureView* depthBuffer, float depthValue) +void GPUContextVulkan::ClearDepth(GPUTextureView* depthBuffer, float depthValue, uint8 stencilValue) { const auto rtVulkan = static_cast(depthBuffer); - if (rtVulkan) { // TODO: detect if inside render pass and use ClearAttachments @@ -815,7 +814,7 @@ void GPUContextVulkan::ClearDepth(GPUTextureView* depthBuffer, float depthValue) VkClearDepthStencilValue clear; clear.depth = depthValue; - clear.stencil = 0; + clear.stencil = stencilValue; vkCmdClearDepthStencilImage(cmdBuffer->GetHandle(), rtVulkan->Image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clear, 1, &rtVulkan->Info.subresourceRange); } } diff --git a/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.h b/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.h index e28bbc1a5..07ffa330b 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.h +++ b/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.h @@ -151,7 +151,7 @@ public: void* GetNativePtr() const override; bool IsDepthBufferBinded() override; void Clear(GPUTextureView* rt, const Color& color) override; - void ClearDepth(GPUTextureView* depthBuffer, float depthValue) override; + void ClearDepth(GPUTextureView* depthBuffer, float depthValue, uint8 stencilValue) override; void ClearUA(GPUBuffer* buf, const Float4& value) override; void ClearUA(GPUBuffer* buf, const uint32 value[4]) override; void ClearUA(GPUTexture* texture, const uint32 value[4]) override;