From ea749f12a3985cadd86ae792b7005b92c39286dd Mon Sep 17 00:00:00 2001
From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com>
Date: Wed, 5 Jun 2024 23:36:41 +0800
Subject: [PATCH] Add depth function to conditional compiling
---
Source/Engine/Graphics/GPUContext.h | 19 +++++++++++++++++--
Source/Engine/Graphics/GPUDevice.cpp | 12 ++++++++++++
.../Materials/DeferredMaterialShader.cpp | 8 ++++++++
.../Materials/DeformableMaterialShader.cpp | 4 ++++
.../Materials/ForwardMaterialShader.cpp | 4 ++++
.../Materials/TerrainMaterialShader.cpp | 4 ++++
.../DirectX/DX11/GPUContextDX11.cpp | 2 +-
.../DirectX/DX11/GPUContextDX11.h | 2 +-
.../DirectX/DX12/GPUContextDX12.cpp | 2 +-
.../DirectX/DX12/GPUContextDX12.h | 2 +-
.../GraphicsDevice/Null/GPUContextNull.h | 2 +-
.../Vulkan/GPUContextVulkan.cpp | 2 +-
.../GraphicsDevice/Vulkan/GPUContextVulkan.h | 2 +-
13 files changed, 56 insertions(+), 9 deletions(-)
diff --git a/Source/Engine/Graphics/GPUContext.h b/Source/Engine/Graphics/GPUContext.h
index 6f450de1e..93e4653e7 100644
--- a/Source/Engine/Graphics/GPUContext.h
+++ b/Source/Engine/Graphics/GPUContext.h
@@ -182,11 +182,26 @@ public:
API_FUNCTION() virtual void Clear(GPUTextureView* rt, const Color& color) = 0;
///
- /// Clears depth buffer.
+ /// Clears depth buffer with custom value.
///
/// The depth buffer to clear.
/// The clear depth value.
- API_FUNCTION() virtual void ClearDepth(GPUTextureView* depthBuffer, float depthValue = 0.0f) = 0;
+ API_FUNCTION() virtual void ClearDepthCustom(GPUTextureView* depthBuffer, float depthValue) = 0;
+
+ ///
+ /// Clears depth buffer with default value.
+ ///
+ /// The depth buffer to clear.
+ /// The clear depth value.
+ API_FUNCTION() FORCE_INLINE void ClearDepth(GPUTextureView* depthBuffer) {
+
+#if FLAX_REVERSE_Z
+ ClearDepthCustom(depthBuffer, 0.0f);
+#else
+ ClearDepthCustom(depthBuffer, 0.0f);
+#endif
+
+ }
///
/// Clears an unordered access buffer with a float value.
diff --git a/Source/Engine/Graphics/GPUDevice.cpp b/Source/Engine/Graphics/GPUDevice.cpp
index 979907bf4..591be587c 100644
--- a/Source/Engine/Graphics/GPUDevice.cpp
+++ b/Source/Engine/Graphics/GPUDevice.cpp
@@ -130,7 +130,11 @@ GPUPipelineState::Description GPUPipelineState::Description::Default =
true, // DepthEnable
true, // DepthWriteEnable
true, // DepthClipEnable
+#if FLAX_REVERSE_Z
ComparisonFunc::Greater, // DepthFunc
+#else
+ ComparisonFunc::Less, // DepthFunc
+#endif
false, // StencilEnable
0xff, // StencilReadMask
0xff, // StencilWriteMask
@@ -154,7 +158,11 @@ GPUPipelineState::Description GPUPipelineState::Description::DefaultNoDepth =
false, // DepthEnable
false, // DepthWriteEnable
false, // DepthClipEnable
+#if FLAX_REVERSE_Z
ComparisonFunc::Greater, // DepthFunc
+#else
+ ComparisonFunc::Less, // DepthFunc
+#endif
false, // StencilEnable
0xff, // StencilReadMask
0xff, // StencilWriteMask
@@ -178,7 +186,11 @@ GPUPipelineState::Description GPUPipelineState::Description::DefaultFullscreenTr
false, // DepthEnable
false, // DepthWriteEnable
false, // DepthClipEnable
+#if FLAX_REVERSE_Z
ComparisonFunc::Greater, // DepthFunc
+#else
+ ComparisonFunc::Less, // DepthFunc
+#endif
false, // StencilEnable
0xff, // StencilReadMask
0xff, // StencilWriteMask
diff --git a/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp b/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp
index 1cc3abcf5..90e748e6d 100644
--- a/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp
+++ b/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp
@@ -196,7 +196,11 @@ bool DeferredMaterialShader::Load()
// Motion Vectors pass
psDesc.DepthWriteEnable = false;
psDesc.DepthEnable = true;
+#if FLAX_REVERSE_Z
psDesc.DepthFunc = ComparisonFunc::GreaterEqual;
+#else
+ psDesc.DepthFunc = ComparisonFunc::LessEqual;
+#endif
psDesc.VS = _shader->GetVS("VS");
psDesc.PS = _shader->GetPS("PS_MotionVectors");
_cache.MotionVectors.Init(psDesc);
@@ -214,7 +218,11 @@ bool DeferredMaterialShader::Load()
psDesc.DepthClipEnable = false;
psDesc.DepthWriteEnable = true;
psDesc.DepthEnable = true;
+#if FLAX_REVERSE_Z
psDesc.DepthFunc = ComparisonFunc::Greater;
+#else
+ psDesc.DepthFunc = ComparisonFunc::Less;
+#endif
psDesc.HS = nullptr;
psDesc.DS = nullptr;
GPUShaderProgramVS* instancedDepthPassVS;
diff --git a/Source/Engine/Graphics/Materials/DeformableMaterialShader.cpp b/Source/Engine/Graphics/Materials/DeformableMaterialShader.cpp
index 4f5825713..753c704c3 100644
--- a/Source/Engine/Graphics/Materials/DeformableMaterialShader.cpp
+++ b/Source/Engine/Graphics/Materials/DeformableMaterialShader.cpp
@@ -173,7 +173,11 @@ bool DeformableMaterialShader::Load()
psDesc.DepthClipEnable = false;
psDesc.DepthWriteEnable = true;
psDesc.DepthEnable = true;
+#if FLAX_REVERSE_Z
psDesc.DepthFunc = ComparisonFunc::Greater;
+#else
+ psDesc.DepthFunc = ComparisonFunc::Less;
+#endif
psDesc.HS = nullptr;
psDesc.DS = nullptr;
_cache.Depth.Init(psDesc);
diff --git a/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp b/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp
index c316daff0..852017a19 100644
--- a/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp
+++ b/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp
@@ -205,7 +205,11 @@ bool ForwardMaterialShader::Load()
psDesc.DepthClipEnable = false;
psDesc.DepthWriteEnable = true;
psDesc.DepthEnable = true;
+#if FLAX_REVERSE_Z
psDesc.DepthFunc = ComparisonFunc::Greater;
+#else
+ psDesc.DepthFunc = ComparisonFunc::Less;
+#endif
psDesc.HS = nullptr;
psDesc.DS = nullptr;
psDesc.VS = _shader->GetVS("VS");
diff --git a/Source/Engine/Graphics/Materials/TerrainMaterialShader.cpp b/Source/Engine/Graphics/Materials/TerrainMaterialShader.cpp
index b396bd418..8ea900871 100644
--- a/Source/Engine/Graphics/Materials/TerrainMaterialShader.cpp
+++ b/Source/Engine/Graphics/Materials/TerrainMaterialShader.cpp
@@ -185,7 +185,11 @@ bool TerrainMaterialShader::Load()
psDesc.DepthClipEnable = false;
psDesc.DepthWriteEnable = true;
psDesc.DepthEnable = true;
+#if FLAX_REVERSE_Z
psDesc.DepthFunc = ComparisonFunc::Greater;
+#else
+ psDesc.DepthFunc = ComparisonFunc::Less;
+#endif
psDesc.HS = nullptr;
psDesc.DS = nullptr;
// TODO: masked terrain materials (depth pass should clip holes)
diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp
index 395363357..5ebc7dea4 100644
--- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp
+++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp
@@ -166,7 +166,7 @@ void GPUContextDX11::Clear(GPUTextureView* rt, const Color& color)
}
}
-void GPUContextDX11::ClearDepth(GPUTextureView* depthBuffer, float depthValue)
+void GPUContextDX11::ClearDepthCustom(GPUTextureView* depthBuffer, float depthValue)
{
auto depthBufferDX11 = static_cast(depthBuffer);
diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h
index d22b8a4f6..02a84082e 100644
--- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h
+++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.h
@@ -113,7 +113,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 ClearDepthCustom(GPUTextureView* depthBuffer, float depthValue) 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..ac4fae8c3 100644
--- a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.cpp
+++ b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.cpp
@@ -713,7 +713,7 @@ void GPUContextDX12::Clear(GPUTextureView* rt, const Color& color)
}
}
-void GPUContextDX12::ClearDepth(GPUTextureView* depthBuffer, float depthValue)
+void GPUContextDX12::ClearDepthCustom(GPUTextureView* depthBuffer, float depthValue)
{
auto depthBufferDX12 = static_cast(depthBuffer);
diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.h b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUContextDX12.h
index 70ca799eb..1188fc397 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 ClearDepthCustom(GPUTextureView* depthBuffer, float depthValue) 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..e3a051706 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 ClearDepthCustom(GPUTextureView* depthBuffer, float depthValue) override
{
}
diff --git a/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp b/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp
index cb223124f..0e69c4208 100644
--- a/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp
+++ b/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.cpp
@@ -797,7 +797,7 @@ void GPUContextVulkan::Clear(GPUTextureView* rt, const Color& color)
}
}
-void GPUContextVulkan::ClearDepth(GPUTextureView* depthBuffer, float depthValue)
+void GPUContextVulkan::ClearDepthCustom(GPUTextureView* depthBuffer, float depthValue)
{
const auto rtVulkan = static_cast(depthBuffer);
diff --git a/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.h b/Source/Engine/GraphicsDevice/Vulkan/GPUContextVulkan.h
index e28bbc1a5..52a34cb22 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 ClearDepthCustom(GPUTextureView* depthBuffer, float depthValue) 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;