From d46beab34e2fbc83d08e11324f456247951debd9 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 21 Nov 2025 17:40:06 +0100 Subject: [PATCH] fix signed integer overflow in polygon hash calculation also gives a speedup on some maps lul --- src/hardware/hw_batching.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/hardware/hw_batching.c b/src/hardware/hw_batching.c index 709b7f45e..d7f1128ad 100644 --- a/src/hardware/hw_batching.c +++ b/src/hardware/hw_batching.c @@ -126,8 +126,9 @@ void HWR_ProcessPolygon(FSurfaceInfo *pSurf, FOutVector *pOutVerts, FUINT iNumPt if (!(PolyFlags & PF_NoTexture) && !horizonSpecial) { // use FNV-1a to hash polygons for later sorting. - INT32 hash = 0x811c9dc5; -#define DIGEST(h, x) h ^= (x); h *= 0x01000193 + UINT32 hash = 0x811c9dc5; + UINT32 prime = 0x1000193; +#define DIGEST(h, x) h ^= ((UINT32)(x)); h *= prime if (current_texture) { DIGEST(hash, current_texture->downloaded); @@ -148,7 +149,7 @@ void HWR_ProcessPolygon(FSurfaceInfo *pSurf, FOutVector *pOutVerts, FUINT iNumPt } #undef DIGEST // remove the sign bit to ensure that skybox and horizon line comes first. - polygonArray[polygonArraySize-1].hash = (hash & INT32_MAX); + polygonArray[polygonArraySize-1].hash = (INT32)(hash & INT32_MAX); } memcpy(&unsortedVertexArray[unsortedVertexArraySize], pOutVerts, iNumPts * sizeof(FOutVector)); @@ -250,8 +251,6 @@ void HWR_RenderBatches(void) FSurfaceInfo currentSurfaceInfo; FSurfaceInfo nextSurfaceInfo; - const boolean useshader = HWR_UseShader(); - int i; if (!currently_batching) @@ -274,6 +273,8 @@ void HWR_RenderBatches(void) return;// nothing to draw } + const boolean useshader = HWR_UseShader(); + // init stats vars ps_hw_numpolys.value.i = polygonArraySize; ps_hw_numcalls.value.i = ps_hw_numverts.value.i = 0;