diff options
author | StapleButter <thetotalworm@gmail.com> | 2017-07-14 00:12:35 +0200 |
---|---|---|
committer | StapleButter <thetotalworm@gmail.com> | 2017-07-14 00:12:35 +0200 |
commit | 04172f47dea8b7f1f6fadcb58dc8c9a43b63a5dd (patch) | |
tree | 374baa4ca9f21a69c739cb319777ba7bea311265 | |
parent | a1401e724c73dcbcc5c9a60c010eebd405df5848 (diff) |
fix potential overflow in fog density calculation
-rw-r--r-- | src/GPU3D_Soft.cpp | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/GPU3D_Soft.cpp b/src/GPU3D_Soft.cpp index 9e0ccdb..56660c1 100644 --- a/src/GPU3D_Soft.cpp +++ b/src/GPU3D_Soft.cpp @@ -1545,15 +1545,19 @@ void ScanlineFinalPass(s32 y) } else { - z = (z - fogoffset) << fogshift; - densityid = z >> 19; + // technically: Z difference is shifted left by fog shift + // then bit 0-18 are the fractional part and bit 19-23 are the density index + // things are a little different here to avoid overflow in 32-bit range + + z -= fogoffset; + densityid = z >> (19-fogshift); if (densityid >= 32) { densityid = 32; densityfrac = 0; } else - densityfrac = z & 0x7FFFF; + densityfrac = (z << fogshift) & 0x7FFFF; } // checkme |