From 19d83f82e4eea1d307c678ac9eea7988d2c2f425 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 1 Feb 2026 20:23:37 +0100 Subject: [PATCH] software renderer: "address" an absolute silly amount of overflowing fixed point maths honestly be better to just completely switch over to 64bit maths, or even better floats.... this is mostly just bandaids but seems to make software more "visually stable" --- src/m_fixed.h | 9 +++++++ src/r_main.cpp | 8 +++--- src/r_main.h | 4 +-- src/r_plane.cpp | 6 ++--- src/r_segs.cpp | 70 +++++++++++++++++++++++++++--------------------- src/r_things.cpp | 10 ++++--- 6 files changed, 63 insertions(+), 44 deletions(-) diff --git a/src/m_fixed.h b/src/m_fixed.h index 894e2b1cc..689c06e7a 100644 --- a/src/m_fixed.h +++ b/src/m_fixed.h @@ -20,6 +20,7 @@ extern "C" { #endif #include "doomtype.h" + #ifdef __GNUC__ #include #endif @@ -71,6 +72,14 @@ FUNCMATH FUNCINLINE static ATTRINLINE fixed_t DoubleToFixed(double f) #define FIXED_TO_FLOAT(x) FixedToFloat(x) // (((float)(x)) / ((float)FRACUNIT)) #define FLOAT_TO_FIXED(f) FloatToFixed(f) // (fixed_t)((f) * ((float)FRACUNIT)) +FUNCMATH FUNCINLINE static ATTRINLINE fixed_t FixedClamp(INT64 val) +{ +// stoopid includes +#define FCLAMP(x, y, z) ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x))) + return (fixed_t)(FCLAMP(val, (INT64)INT32_MIN, (INT64)INT32_MAX)); +#undef FCLAMP +} + /** \brief The FixedMul function \param a fixed_t number diff --git a/src/r_main.cpp b/src/r_main.cpp index 5fb224167..142d1ad52 100644 --- a/src/r_main.cpp +++ b/src/r_main.cpp @@ -519,10 +519,10 @@ void R_GetRenderBlockMapDimensions(fixed_t drawdist, INT32 *xl, INT32 *xh, INT32 const fixed_t vyright = viewy + FixedMul(drawdist, FSIN(right)); // Try to narrow the search to within only the field of view - *xl = (unsigned)(std::min(viewx, std::min(vxleft, vxright)) - bmaporgx)>>MAPBLOCKSHIFT; - *xh = (unsigned)(std::max(viewx, std::max(vxleft, vxright)) - bmaporgx)>>MAPBLOCKSHIFT; - *yl = (unsigned)(std::min(viewy, std::min(vyleft, vyright)) - bmaporgy)>>MAPBLOCKSHIFT; - *yh = (unsigned)(std::max(viewy, std::max(vyleft, vyright)) - bmaporgy)>>MAPBLOCKSHIFT; + *xl = (unsigned)(FixedClamp((INT64)std::min(viewx, std::min(vxleft, vxright)) - bmaporgx))>>MAPBLOCKSHIFT; + *xh = (unsigned)(FixedClamp((INT64)std::max(viewx, std::max(vxleft, vxright)) - bmaporgx))>>MAPBLOCKSHIFT; + *yl = (unsigned)(FixedClamp((INT64)std::min(viewy, std::min(vyleft, vyright)) - bmaporgy))>>MAPBLOCKSHIFT; + *yh = (unsigned)(FixedClamp((INT64)std::max(viewy, std::max(vyleft, vyright)) - bmaporgy))>>MAPBLOCKSHIFT; if (*xh >= bmapwidth) *xh = bmapwidth - 1; diff --git a/src/r_main.h b/src/r_main.h index e8b7393e3..1b1096d35 100644 --- a/src/r_main.h +++ b/src/r_main.h @@ -152,8 +152,8 @@ subsector_t *R_IsPointInSubsector(fixed_t x, fixed_t y); FUNCMATH FUNCINLINE static ATTRINLINE fixed_t R_QuickDist(fixed_t x1, fixed_t y1, fixed_t x, fixed_t y) { - const fixed_t absx = abs(x - x1); - const fixed_t absy = abs(y - y1); + const fixed_t absx = FixedClamp(labs((INT64)x - (INT64)x1)); + const fixed_t absy = FixedClamp(labs((INT64)y - (INT64)y1)); #ifdef __cplusplus return std::max(absx, absy); #else diff --git a/src/r_plane.cpp b/src/r_plane.cpp index 05c4d943a..4d11c190d 100644 --- a/src/r_plane.cpp +++ b/src/r_plane.cpp @@ -228,8 +228,8 @@ static void R_MapPlane(drawspandata_t *ds, spandrawfunc_t *localspanfunc, INT32 // to step from those to the proper texture coordinate to start drawing at. // That way, the texture coordinate is always calculated by its position // on the screen and not by its position relative to the edge of the visplane. - ds->xfrac = ds->xoffs + FixedMul(planecos, distance) + (x1 - centerx) * ds->xstep; - ds->yfrac = ds->yoffs - FixedMul(planesin, distance) + (x1 - centerx) * ds->ystep; + ds->xfrac = FixedClamp((INT64)ds->xoffs + (INT64)FixedMul(planecos, distance) + ((INT64)x1 - (INT64)centerx) * (INT64)ds->xstep); + ds->yfrac = FixedClamp((INT64)ds->yoffs - (INT64)FixedMul(planesin, distance) + ((INT64)x1 - (INT64)centerx) * (INT64)ds->ystep); if (ds->planeripple.active) { @@ -1012,7 +1012,7 @@ void R_DrawSinglePlane(drawspandata_t* ds, visplane_t *pl, boolean allow_paralle levelflat_t *levelflat; void (*mapfunc)(drawspandata_t*, void(*)(drawspandata_t*), INT32, INT32, INT32, boolean) = R_MapPlane; - if (pl->minx > pl->maxx) + if (!(pl->minx <= pl->maxx)) return; // sky flat diff --git a/src/r_segs.cpp b/src/r_segs.cpp index 660b91494..0876e4a90 100644 --- a/src/r_segs.cpp +++ b/src/r_segs.cpp @@ -226,10 +226,11 @@ static void R_RenderMaskedSegLoop(drawcolumndata_t* dc, drawseg_t *drawseg, INT3 leftheight -= viewz; rightheight -= viewz; - rlight->height = (centeryfrac) - FixedMul(leftheight, drawseg->scale1); - rlight->heightstep = (centeryfrac) - FixedMul(rightheight, drawseg->scale2); - rlight->heightstep = (rlight->heightstep-rlight->height)/(range); - rlight->startheight = rlight->height; // keep starting value here to reset for each repeat + rlight->height = FixedClamp((INT64)centeryfrac - FixedMul(leftheight, drawseg->scale1)); + rlight->heightstep = FixedClamp((INT64)centeryfrac - FixedMul(rightheight, drawseg->scale2)); + rlight->heightstep = (FixedClamp((INT64)rlight->heightstep - rlight->height))/(range); + rlight->startheight = rlight->height; // keep starting value here to reset for each repeat + rlight->lightlevel = *light->lightlevel; rlight->extra_colormap = light->extra_colormap; rlight->flags = static_cast(light->flags); @@ -355,9 +356,9 @@ static void R_RenderMaskedSegLoop(drawcolumndata_t* dc, drawseg_t *drawseg, INT3 lighttable_t **xwalllights; sprbotscreen = INT32_MAX; - sprtopscreen = windowtop = (centeryfrac - FixedMul(dc->texturemid, spryscale)); + sprtopscreen = windowtop = FixedClamp((INT64)centeryfrac - FixedMul(dc->texturemid, spryscale)); - realbot = windowbottom = FixedMul(textureheight[texnum], spryscale) + sprtopscreen; + realbot = windowbottom = FixedClamp((INT64)FixedMul(textureheight[texnum], spryscale) + sprtopscreen); dc->iscale = 0xffffffffu / (unsigned)spryscale; // draw the texture @@ -407,7 +408,8 @@ static void R_RenderMaskedSegLoop(drawcolumndata_t* dc, drawseg_t *drawseg, INT3 set_light_vars(i); height = rlight->height; - rlight->height += rlight->heightstep; + + rlight->height = FixedClamp((INT64)rlight->height + rlight->heightstep); if (height <= windowtop) { @@ -455,7 +457,7 @@ static void R_RenderMaskedSegLoop(drawcolumndata_t* dc, drawseg_t *drawseg, INT3 if (frontsector->extra_colormap) dc->colormap = frontsector->extra_colormap->colormap + (dc->colormap - colormaps); - sprtopscreen = centeryfrac - FixedMul(dc->texturemid, spryscale); + sprtopscreen = FixedClamp((INT64)centeryfrac - FixedMul(dc->texturemid, spryscale)); dc->iscale = 0xffffffffu / (unsigned)spryscale; // draw the texture @@ -770,7 +772,7 @@ void R_RenderThickSideRange(drawseg_t *drawseg, INT32 x1, INT32 x2, ffloor_t *pf overflow_test = (INT64)centeryfrac - (((INT64)rightheight*drawseg->scale2)>>FRACBITS); rlight->heightstep = overflow_clamp(overflow_test); - rlight->heightstep = (rlight->heightstep-rlight->height)/(range); + rlight->heightstep = (FixedClamp((INT64)rlight->heightstep - rlight->height))/(range); rlight->flags = static_cast(light->flags); if (light->flags & FF_CUTLEVEL) @@ -787,7 +789,7 @@ void R_RenderThickSideRange(drawseg_t *drawseg, INT32 x1, INT32 x2, ffloor_t *pf overflow_test = (INT64)centeryfrac - (((INT64)rightheight*drawseg->scale2)>>FRACBITS); rlight->botheightstep = overflow_clamp(overflow_test); - rlight->botheightstep = (rlight->botheightstep-rlight->botheight)/(range); + rlight->botheightstep = (FixedClamp((INT64)rlight->botheightstep - rlight->botheight))/(range); } rlight->lightlevel = *light->lightlevel; @@ -935,9 +937,11 @@ void R_RenderThickSideRange(drawseg_t *drawseg, INT32 x1, INT32 x2, ffloor_t *pf for (i = 0; i < dc->numlights; i++) { rlight = &dc->lightlist[i]; - rlight->height += rlight->heightstep; + + rlight->height = FixedClamp((INT64)rlight->height + rlight->heightstep); + if (rlight->flags & FF_CUTLEVEL) - rlight->botheight += rlight->botheightstep; + rlight->botheight = FixedClamp((INT64)rlight->botheight + rlight->botheightstep); } } @@ -1045,12 +1049,12 @@ void R_RenderThickSideRange(drawseg_t *drawseg, INT32 x1, INT32 x2, ffloor_t *pf solid = 0; height = rlight->height; - rlight->height += rlight->heightstep; + rlight->height = FixedClamp((INT64)rlight->height + rlight->heightstep); if (solid) { - bheight = rlight->botheight - (FRACUNIT >> 1); - rlight->botheight += rlight->botheightstep; + bheight = FixedClamp((INT64)rlight->botheight - (FRACUNIT >> 1)); + rlight->botheight = FixedClamp((INT64)rlight->botheight + rlight->botheightstep); } if (height <= windowtop) @@ -1212,7 +1216,7 @@ static void R_RenderSegLoop(drawcolumndata_t* dc) for (; rw_x < rw_stopx; rw_x++) { // mark floor / ceiling areas - yl = (topfrac+HEIGHTUNIT-1)>>HEIGHTBITS; + yl = (FixedClamp((INT64)topfrac + HEIGHTUNIT-1))>>HEIGHTBITS; yh = bottomfrac>>HEIGHTBITS; // Mark ceiling @@ -1363,15 +1367,17 @@ static void R_RenderSegLoop(drawcolumndata_t* dc) //SoM: Calculate offsets for Thick fake floors. // calculate texture offset angle = (rw_centerangle + xtoviewangle[rw_x])>>ANGLETOFINESHIFT; + // Mask 4095 to guarantee this index is within bounds - texturecolumn = rw_offset-FixedMul(FINETANGENT(angle & 4095), rw_distance); + texturecolumn = FixedClamp((INT64)rw_offset - FixedMul(FINETANGENT(angle & 4095), rw_distance)); if (oldtexturecolumn != -1) { - rw_bottomtexturemid += FixedMul(rw_bottomtextureslide, oldtexturecolumn-texturecolumn); - rw_midtexturemid += FixedMul(rw_midtextureslide, oldtexturecolumn-texturecolumn); - rw_toptexturemid += FixedMul(rw_toptextureslide, oldtexturecolumn-texturecolumn); - rw_midtextureback += FixedMul(rw_midtexturebackslide, oldtexturecolumn-texturecolumn); + const fixed_t coloff = FixedClamp((INT64)oldtexturecolumn - texturecolumn); + rw_bottomtexturemid += FixedMul(rw_bottomtextureslide, coloff); + rw_midtexturemid += FixedMul(rw_midtextureslide, coloff); + rw_toptexturemid += FixedMul(rw_toptextureslide, coloff); + rw_midtextureback += FixedMul(rw_midtexturebackslide, coloff); } oldtexturecolumn = texturecolumn; @@ -1545,10 +1551,10 @@ static void R_RenderSegLoop(drawcolumndata_t* dc) { for (i = 0; i < dc->numlights; i++) { - dc->lightlist[i].height += dc->lightlist[i].heightstep; + dc->lightlist[i].height = FixedClamp((INT64)dc->lightlist[i].height + dc->lightlist[i].heightstep); if (dc->lightlist[i].flags & FF_CUTSOLIDS) - dc->lightlist[i].botheight += dc->lightlist[i].botheightstep; + dc->lightlist[i].botheight = FixedClamp((INT64)dc->lightlist[i].botheight + dc->lightlist[i].botheightstep);; } } @@ -1569,9 +1575,10 @@ static void R_RenderSegLoop(drawcolumndata_t* dc) visffloor[i].b_frac += visffloor[i].b_step; } - rw_scale += rw_scalestep; - topfrac += topstep; - bottomfrac += bottomstep; + rw_scale = FixedClamp((INT64)rw_scale + rw_scalestep); + + topfrac = FixedClamp((INT64)topfrac + topstep); + bottomfrac = FixedClamp((INT64)bottomfrac + bottomstep); } } @@ -2205,8 +2212,8 @@ void R_StoreWallRange(INT32 start, INT32 stop) } } - rw_toptexturemid += sidedef->rowoffset; - rw_bottomtexturemid += sidedef->rowoffset; + rw_toptexturemid = FixedClamp((INT64)rw_toptexturemid + sidedef->rowoffset); + rw_bottomtexturemid = FixedClamp((INT64)rw_bottomtexturemid + sidedef->rowoffset); R_AllocTextureColumnTables(rw_stopx - start); @@ -2491,8 +2498,9 @@ void R_StoreWallRange(INT32 start, INT32 stop) rw_offset = -rw_offset; /// don't use texture offset for splats - rw_offset2 = rw_offset + curline->offset; - rw_offset += sidedef->textureoffset + curline->offset; + rw_offset2 = FixedClamp((INT64)rw_offset + curline->offset); + rw_offset = FixedClamp((INT64)rw_offset + (INT64)sidedef->textureoffset + curline->offset); + rw_centerangle = ANGLE_90 + viewangle - rw_normalangle; // calculate light table @@ -2645,7 +2653,7 @@ void R_StoreWallRange(INT32 start, INT32 stop) rlight->height = (centeryfrac>>4) - FixedMul(leftheight, rw_scale); rlight->heightstep = (centeryfrac>>4) - FixedMul(rightheight, ds_p->scale2); - rlight->heightstep = (rlight->heightstep-rlight->height)/(range); + rlight->heightstep = (FixedClamp((INT64)rlight->heightstep - rlight->height))/(range); rlight->flags = static_cast(light->flags); if (light->caster && light->caster->flags & FF_CUTSOLIDS) diff --git a/src/r_things.cpp b/src/r_things.cpp index 2ced7cde2..f6e073370 100644 --- a/src/r_things.cpp +++ b/src/r_things.cpp @@ -11,6 +11,7 @@ /// \file r_things.c /// \brief Refresh of things, i.e. objects represented by sprites +#include "m_fixed.h" #include "r_main.h" // stplyr #include "r_fps.h" #include "r_things.h" @@ -616,10 +617,11 @@ void R_DrawMaskedColumn(drawcolumndata_t* dc, column_t *column) if (topdelta <= prevdelta) topdelta += prevdelta; prevdelta = topdelta; - topscreen = sprtopscreen + spryscale*topdelta; - bottomscreen = topscreen + spryscale*column->length; - dc->yl = (topscreen+FRACUNIT-1)>>FRACBITS; + topscreen = FixedClamp((INT64)sprtopscreen + (INT64)spryscale * (INT64)topdelta); + bottomscreen = FixedClamp((INT64)topscreen + (INT64)spryscale * (INT64)column->length); + + dc->yl = FixedClamp((INT64)topscreen + (FRACUNIT-1))>>FRACBITS; dc->yh = (bottomscreen-1)>>FRACBITS; if (windowtop != INT32_MAX && windowbottom != INT32_MAX) @@ -885,7 +887,7 @@ static void R_DrawVisSprite(vissprite_t *vis) for (dc.x = vis->x1; dc.x <= vis->x2; dc.x++, spryscale += scalestep) { angle_t angle = ((vis->centerangle + xtoviewangle[dc.x]) >> ANGLETOFINESHIFT) & 0xFFF; - texturecolumn = (vis->paperoffset - FixedMul(FINETANGENT(angle), vis->paperdistance)) / horzscale; + texturecolumn = FixedClamp((INT64)vis->paperoffset - FixedMul(FINETANGENT(angle), vis->paperdistance)) / horzscale; if (texturecolumn < 0 || texturecolumn >= pwidth) continue;