From b2ec90029c10c9ad542c1617fc14b49065e962ff Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Sun, 18 Jul 2021 14:46:41 -0700 Subject: [PATCH] Hackfix for clipping in Gromiti --- Source/Core/VideoBackends/Software/Clipper.cpp | 2 +- Source/Core/VideoCommon/UberShaderVertex.cpp | 8 +++++++- Source/Core/VideoCommon/VertexShaderGen.cpp | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Source/Core/VideoBackends/Software/Clipper.cpp b/Source/Core/VideoBackends/Software/Clipper.cpp index dc22a6a20119..89058f4eb889 100644 --- a/Source/Core/VideoBackends/Software/Clipper.cpp +++ b/Source/Core/VideoBackends/Software/Clipper.cpp @@ -90,7 +90,7 @@ static inline int CalcClipMask(const OutputVertexData* v) if (pos.y + pos.w < 0) cmask |= CLIP_NEG_Y_BIT; - if (pos.w * pos.z > 0) + if (pos.w * pos.z >= 0) cmask |= CLIP_POS_Z_BIT; if (pos.z + pos.w < 0) diff --git a/Source/Core/VideoCommon/UberShaderVertex.cpp b/Source/Core/VideoCommon/UberShaderVertex.cpp index c5a20f033c50..2b86ed5dc49f 100644 --- a/Source/Core/VideoCommon/UberShaderVertex.cpp +++ b/Source/Core/VideoCommon/UberShaderVertex.cpp @@ -269,7 +269,7 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config // own clipping. We want to clip so that -w <= z <= 0, which matches the console -1..0 range. // We adjust our depth value for clipping purposes to match the perspective projection in the // software backend, which is a hack to fix Sonic Adventure and Unleashed games. - out.Write("float clipDepth = o.pos.z * (1.0 - 1e-7);\n" + out.Write("float clipDepth = (o.pos.z + 1e-7) * (1.0 - 2e-7);\n" "float clipDist0 = clipDepth + o.pos.w;\n" // Near: z < -w "float clipDist1 = -clipDepth;\n"); // Far: z > 0 if (host_config.backend_geometry_shaders) @@ -278,6 +278,12 @@ ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config "o.clipDist1 = clipDist1;\n"); } } + else + { + // Same depth adjustment for Sonic. Without depth clamping, it unfortunately + // affects non-clipping uses of depth too. + out.Write("o.pos.z = (o.pos.z + 1e-7) * (1.0 - 2e-7);\n"); + } // Write the true depth value. If the game uses depth textures, then the pixel shader will // override it with the correct values if not then early z culling will improve speed. diff --git a/Source/Core/VideoCommon/VertexShaderGen.cpp b/Source/Core/VideoCommon/VertexShaderGen.cpp index aa28c0f89607..d5f8dbee805c 100644 --- a/Source/Core/VideoCommon/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/VertexShaderGen.cpp @@ -484,7 +484,7 @@ ShaderCode GenerateVertexShaderCode(APIType api_type, const ShaderHostConfig& ho // own clipping. We want to clip so that -w <= z <= 0, which matches the console -1..0 range. // We adjust our depth value for clipping purposes to match the perspective projection in the // software backend, which is a hack to fix Sonic Adventure and Unleashed games. - out.Write("float clipDepth = o.pos.z * (1.0 - 1e-7);\n" + out.Write("float clipDepth = (o.pos.z + 1e-7) * (1.0 - 2e-7);\n" "float clipDist0 = clipDepth + o.pos.w;\n" // Near: z < -w "float clipDist1 = -clipDepth;\n"); // Far: z > 0 @@ -498,7 +498,7 @@ ShaderCode GenerateVertexShaderCode(APIType api_type, const ShaderHostConfig& ho { // Same depth adjustment for Sonic. Without depth clamping, it unfortunately // affects non-clipping uses of depth too. - out.Write("o.pos.z = o.pos.z * (1.0 - 1e-7);\n"); + out.Write("o.pos.z = (o.pos.z + 1e-7) * (1.0 - 2e-7);\n"); } // Write the true depth value. If the game uses depth textures, then the pixel shader will