From 1b6a31b6e5005ab67c4e97c9dfadeb669d6e9bdb Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Wed, 5 Jun 2024 21:15:08 +0800 Subject: [PATCH] Finish conditional compiling --- Source/Engine/Renderer/GBufferPass.cpp | 5 ++++- Source/Shaders/Common.hlsl | 9 ++++++++- Source/Shaders/DebugDraw.shader | 4 ++++ Source/Shaders/GBuffer.hlsl | 2 +- Source/Shaders/SSR.hlsl | 4 ++++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Source/Engine/Renderer/GBufferPass.cpp b/Source/Engine/Renderer/GBufferPass.cpp index 96201807b..bf41b6117 100644 --- a/Source/Engine/Renderer/GBufferPass.cpp +++ b/Source/Engine/Renderer/GBufferPass.cpp @@ -396,7 +396,10 @@ bool GBufferPass::IsDebugView(ViewMode mode) void GBufferPass::SetInputs(const RenderView& view, GBufferData& gBuffer) { // GBuffer params: - // ViewInfo : x-1/Projection[0,0] y-1/Projection[1,1] z-(Near / (Far - Near)) w-((Far * Near) / (Far - Near) / Far) + // With reverse Z enabled: + // ViewInfo : x-1/Projection[0,0] y-1/Projection[1,1] z-(-(Near / (Far - Near)) w-((Far * Near) / (Far - Near) / Far) + // Otherwise: + // ViewInfo : x-1/Projection[0,0] y-1/Projection[1,1] z-(Far / (Far - Near)) w-(-(Far * Near) / (Far - Near) / Far) // ScreenSize : x-Width y-Height z-1 / Width w-1 / Height // ViewPos,ViewFar : x,y,z - world space view position w-Far // InvViewMatrix : inverse view matrix (4 rows by 4 columns) diff --git a/Source/Shaders/Common.hlsl b/Source/Shaders/Common.hlsl index 701546761..d0c5ee858 100644 --- a/Source/Shaders/Common.hlsl +++ b/Source/Shaders/Common.hlsl @@ -53,6 +53,9 @@ #define SHADING_MODEL_SUBSURFACE 2 #define SHADING_MODEL_FOLIAGE 3 +// Didn't figure out how to pass compilation flags via C++, so hardcode it temporarily +#define FLAX_REVERSE_Z 1 + // Detect feature level support #if FEATURE_LEVEL >= FEATURE_LEVEL_SM5 #define CAN_USE_GATHER 1 @@ -135,7 +138,11 @@ SamplerComparisonState ShadowSamplerPCF : register(s5); // Structure that contains information about GBuffer struct GBufferData { - float4 ViewInfo; // x-1/Projection[0,0], y-1/Projection[1,1], z-(Near / (Far - Near)), w-((Far * Near) / (Far - Near) / Far) + // If reverse Z enabled: + // x-1/Projection[0,0], y-1/Projection[1,1], z-(-Near / (Far - Near)), w-((Far * Near) / (Far - Near) / Far) + // Otherwise: + // x-1/Projection[0,0], y-1/Projection[1,1], z-(Far / (Far - Near)), w-(-(Far * Near) / (Far - Near) / Far) + float4 ViewInfo; float4 ScreenSize; // x-Width, y-Height, z-1/Width, w-1/Height float3 ViewPos; // view position (in world space) float ViewFar; // view far plane distance (in world space) diff --git a/Source/Shaders/DebugDraw.shader b/Source/Shaders/DebugDraw.shader index de4e079be..81b85c1a1 100644 --- a/Source/Shaders/DebugDraw.shader +++ b/Source/Shaders/DebugDraw.shader @@ -43,7 +43,11 @@ float4 PS(VS2PS input) : SV_Target { float sceneDepthDeviceZ = SceneDepthTexture.Load(int3(input.Position.xy, 0)).r; float interpolatedDeviceZ = input.Position.z; +#if FLAX_REVERSE_Z clip(interpolatedDeviceZ - sceneDepthDeviceZ); +#else + clip(sceneDepthDeviceZ - interpolatedDeviceZ); +#endif } #endif diff --git a/Source/Shaders/GBuffer.hlsl b/Source/Shaders/GBuffer.hlsl index 0dd2c4457..9378b3be5 100644 --- a/Source/Shaders/GBuffer.hlsl +++ b/Source/Shaders/GBuffer.hlsl @@ -33,7 +33,7 @@ float LinearizeZ(GBufferData gBuffer, float depth) // Convert linear depth to device depth float LinearZ2DeviceDepth(GBufferData gBuffer, float linearDepth) { - return ((gBuffer.ViewInfo.w / linearDepth) + gBuffer.ViewInfo.z); + return (gBuffer.ViewInfo.w / linearDepth) + gBuffer.ViewInfo.z; } // Get view space position at given pixel coordinate with given device depth diff --git a/Source/Shaders/SSR.hlsl b/Source/Shaders/SSR.hlsl index 0adf84a83..8cef82789 100644 --- a/Source/Shaders/SSR.hlsl +++ b/Source/Shaders/SSR.hlsl @@ -98,7 +98,11 @@ float3 TraceScreenSpaceReflection(float2 uv, GBufferSample gBuffer, Texture2D de { // Sample depth buffer and calculate depth difference currSample = SAMPLE_RT(depthBuffer, currOffset.xy).r; +#if FLAX_REVERSE_Z depthDiff = currSample - currOffset.z; +#else + depthDiff = currOffset.z - currSample; +#endif // Check intersection if (depthDiff >= 0)