From e2f0713cc28ab6458858dc1e9860388477b1c4c0 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 20 Nov 2021 20:54:52 -0800 Subject: [PATCH] softgpu: Clamp and round fog by mantissa bits. This matches hardware calculated fog values much better. --- GPU/Software/Rasterizer.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/GPU/Software/Rasterizer.cpp b/GPU/Software/Rasterizer.cpp index edf24ac7cc..5275b99d72 100644 --- a/GPU/Software/Rasterizer.cpp +++ b/GPU/Software/Rasterizer.cpp @@ -97,12 +97,21 @@ static inline Vec4 Interpolate(const float &c0, const float &c1, const fl } static inline u8 ClampFogDepth(float fogdepth) { - if (fogdepth <= 0.0f) + union FloatBits { + float f; + u32 u; + }; + FloatBits f; + f.f = fogdepth; + + u32 exp = f.u >> 23; + if ((f.u & 0x80000000) != 0 || exp <= 126 - 8) return 0; - else if (fogdepth >= 1.0f) + if (exp > 126) return 255; - else - return (u8)(u32)(fogdepth * 255.0f); + + u32 mantissa = (f.u & 0x007FFFFF) | 0x00800000; + return mantissa >> (16 + 126 - exp); } static inline int ClampUV(int v, int height) {