From c7931ca69cbb4f32bc96cb9174183add0b633a79 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 26 Jan 2024 02:28:07 +0100 Subject: [PATCH] DSDADoom shenanigans R_PointToPseudoAngle seems rather promising --- src/hardware/hw_clip.c | 5 +- src/hardware/hw_main.c | 8 +-- src/p_maputl.c | 152 ++++++++++------------------------------- src/r_bsp.c | 8 +-- src/r_main.c | 58 +++++++++------- src/r_main.h | 2 +- src/xs_Float.h | 81 ++++++++++++++++++++++ 7 files changed, 163 insertions(+), 151 deletions(-) create mode 100644 src/xs_Float.h diff --git a/src/hardware/hw_clip.c b/src/hardware/hw_clip.c index 8ef5636aa..a041db7a9 100644 --- a/src/hardware/hw_clip.c +++ b/src/hardware/hw_clip.c @@ -77,6 +77,7 @@ #include "../tables.h" #include "r_opengl/r_opengl.h" #include "../r_main.h" // for cv_fov +#include "../xs_Float.h" #ifdef HAVE_SPHEREFRUSTRUM static GLdouble viewMatrix[16]; @@ -344,9 +345,9 @@ angle_t gld_FrustumAngle(angle_t tiltangle) // ok, this is a gross hack that barely works... // but at least it doesn't overestimate too much... floatangle = 2.0f + (45.0f + (tilt / 1.9f)) * (float)render_fov * 48.0f / render_multiplier / 90.0f; - a1 = ANG1 * (int)floatangle; - if (a1 >= ANGLE_180) + if (floatangle >= ANGLE_180) return 0xffffffff; + a1 = (angle_t)xs_CRoundToInt(ANG1 * floatangle); return a1; } diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index de6b5f232..a98c1592f 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -2881,8 +2881,8 @@ void HWR_AddLine(seg_t *line) } // OPTIMIZE: quickly reject orthogonal back sides. - angle1 = R_PointToAngle64(v1x, v1y); - angle2 = R_PointToAngle64(v2x, v2y); + angle1 = R_PointToPseudoAngle(v1x, v1y); + angle2 = R_PointToPseudoAngle(v2x, v2y); // do an extra culling check when rendering portals // check if any line vertex is on the viewable side of the portal target line @@ -3075,8 +3075,8 @@ boolean HWR_CheckBBox(fixed_t *bspcoord) if (mindist > current_bsp_culling_distance) return false; } - angle1 = R_PointToAngle64(px1, py1); - angle2 = R_PointToAngle64(px2, py2); + angle1 = R_PointToPseudoAngle(px1, py1); + angle2 = R_PointToPseudoAngle(px2, py2); return gld_clipper_SafeCheckRange(angle2, angle1); } diff --git a/src/p_maputl.c b/src/p_maputl.c index 17df33a91..0e9c2d12c 100644 --- a/src/p_maputl.c +++ b/src/p_maputl.c @@ -136,33 +136,10 @@ void P_ClosestPointOnLine3D(fixed_t x, fixed_t y, fixed_t z, line_t *line, verte // INT32 P_PointOnLineSide(fixed_t x, fixed_t y, line_t *line) { - const vertex_t *v1 = line->v1; - fixed_t dx, dy, left, right; - - if (!line->dx) - { - if (x <= v1->x) - return (line->dy > 0); - - return (line->dy < 0); - } - if (!line->dy) - { - if (y <= v1->y) - return (line->dx < 0); - - return (line->dx > 0); - } - - dx = (x - v1->x); - dy = (y - v1->y); - - left = FixedMul(line->dy>>FRACBITS, dx); - right = FixedMul(dy, line->dx>>FRACBITS); - - if (right < left) - return 0; // front side - return 1; // back side + return + !line->dx ? x <= line->v1->x ? line->dy > 0 : line->dy < 0 : + !line->dy ? y <= line->v1->y ? line->dx < 0 : line->dx > 0 : + ((long long) y - line->v1->y) * line->dx >= ((long long) x - line->v1->x) * line->dy; } // @@ -172,48 +149,27 @@ INT32 P_PointOnLineSide(fixed_t x, fixed_t y, line_t *line) // INT32 P_BoxOnLineSide(fixed_t *tmbox, line_t *ld) { - INT32 p1, p2; - - switch (ld->slopetype) - { - case ST_HORIZONTAL: - p1 = tmbox[BOXTOP] > ld->v1->y; - p2 = tmbox[BOXBOTTOM] > ld->v1->y; - if (ld->dx < 0) - { - p1 ^= 1; - p2 ^= 1; - } - break; - - case ST_VERTICAL: - p1 = tmbox[BOXRIGHT] < ld->v1->x; - p2 = tmbox[BOXLEFT] < ld->v1->x; - if (ld->dy < 0) - { - p1 ^= 1; - p2 ^= 1; - } - break; - - case ST_POSITIVE: - p1 = P_PointOnLineSide(tmbox[BOXLEFT], tmbox[BOXTOP], ld); - p2 = P_PointOnLineSide(tmbox[BOXRIGHT], tmbox[BOXBOTTOM], ld); - break; - - case ST_NEGATIVE: - p1 = P_PointOnLineSide(tmbox[BOXRIGHT], tmbox[BOXTOP], ld); - p2 = P_PointOnLineSide(tmbox[BOXLEFT], tmbox[BOXBOTTOM], ld); - break; - - default: - I_Error("P_BoxOnLineSide: unknown slopetype %d\n", ld->slopetype); - return -1; - } - - if (p1 == p2) - return p1; - return -1; + switch (ld->slopetype) + { + int p; + default: // shut up compiler warnings -- killough + case ST_HORIZONTAL: + return + (tmbox[BOXBOTTOM] > ld->v1->y) == (p = tmbox[BOXTOP] > ld->v1->y) ? + p ^ (ld->dx < 0) : -1; + case ST_VERTICAL: + return + (tmbox[BOXLEFT] < ld->v1->x) == (p = tmbox[BOXRIGHT] < ld->v1->x) ? + p ^ (ld->dy < 0) : -1; + case ST_POSITIVE: + return + P_PointOnLineSide(tmbox[BOXRIGHT], tmbox[BOXBOTTOM], ld) == + (p = P_PointOnLineSide(tmbox[BOXLEFT], tmbox[BOXTOP], ld)) ? p : -1; + case ST_NEGATIVE: + return + (P_PointOnLineSide(tmbox[BOXLEFT], tmbox[BOXBOTTOM], ld)) == + (p = P_PointOnLineSide(tmbox[BOXRIGHT], tmbox[BOXTOP], ld)) ? p : -1; + } } // @@ -222,40 +178,11 @@ INT32 P_BoxOnLineSide(fixed_t *tmbox, line_t *ld) // static INT32 P_PointOnDivlineSide(fixed_t x, fixed_t y, divline_t *line) { - fixed_t dx, dy, left, right; - - if (!line->dx) - { - if (x <= line->x) - return line->dy > 0; - - return line->dy < 0; - } - if (!line->dy) - { - if (y <= line->y) - return line->dx < 0; - - return line->dx > 0; - } - - dx = (x - line->x); - dy = (y - line->y); - - // try to quickly decide by looking at sign bits - if ((line->dy ^ line->dx ^ dx ^ dy) & 0x80000000) - { - if ((line->dy ^ dx) & 0x80000000) - return 1; // left is negative - return 0; - } - - left = FixedMul(line->dy>>8, dx>>8); - right = FixedMul(dy>>8, line->dx>>8); - - if (right < left) - return 0; // front side - return 1; // back side + return + !line->dx ? x <= line->x ? line->dy > 0 : line->dy < 0 : + !line->dy ? y <= line->y ? line->dx < 0 : line->dx > 0 : + (line->dy^line->dx^(x -= line->x)^(y -= line->y)) < 0 ? (line->dy^x) < 0 : + (long long) y * line->dx >= (long long) x * line->dy; } // @@ -276,18 +203,13 @@ void P_MakeDivline(line_t *li, divline_t *dl) // fixed_t P_InterceptVector(divline_t *v2, divline_t *v1) { - fixed_t frac, num, den; - - den = FixedMul(v1->dy>>8, v2->dx) - FixedMul(v1->dx>>8, v2->dy); - - if (!den) - return 0; - - num = FixedMul((v1->x - v2->x)>>8, v1->dy) + FixedMul((v2->y - v1->y)>>8, v1->dx); - frac = FixedDiv(num, den); - - return frac; -} + /* cph - This was introduced at prboom_4_compatibility - no precision/overflow problems */ + int64_t den = (int64_t)v1->dy * v2->dx - (int64_t)v1->dx * v2->dy; + den >>= 16; + if (!den) + return 0; + return (fixed_t)(((int64_t)(v1->x - v2->x) * v1->dy - (int64_t)(v1->y - v2->y) * v1->dx) / den); + } // // P_LineOpening diff --git a/src/r_bsp.c b/src/r_bsp.c index 2de30d2ab..b2f0d42ee 100644 --- a/src/r_bsp.c +++ b/src/r_bsp.c @@ -410,8 +410,8 @@ static void R_AddLine(seg_t *line) return; // big room fix - angle1 = R_PointToAngle64(line->v1->x, line->v1->y); - angle2 = R_PointToAngle64(line->v2->x, line->v2->y); + angle1 = R_PointToAngleEx(viewx, viewy, line->v1->x, line->v1->y); + angle2 = R_PointToAngleEx(viewx, viewy, line->v2->x, line->v2->y); curline = line; // Clip to view edges. @@ -613,8 +613,8 @@ static boolean R_CheckBBox(const fixed_t *bspcoord) check = checkcoord[boxpos]; // big room fix - angle1 = R_PointToAngle64(bspcoord[check[0]], bspcoord[check[1]]) - viewangle; - angle2 = R_PointToAngle64(bspcoord[check[2]], bspcoord[check[3]]) - viewangle; + angle1 = R_PointToAngleEx(viewx, viewy, bspcoord[check[0]], bspcoord[check[1]]) - viewangle; + angle2 = R_PointToAngleEx(viewx, viewy, bspcoord[check[2]], bspcoord[check[3]]) - viewangle; if ((signed)angle1 < (signed)angle2) { diff --git a/src/r_main.c b/src/r_main.c index 9ca72dd15..79387486f 100644 --- a/src/r_main.c +++ b/src/r_main.c @@ -32,6 +32,7 @@ #include "m_random.h" // quake camera shake #include "doomstat.h" // MAXSPLITSCREENPLAYERS #include "r_fps.h" // Frame interpolation/uncapped +#include "xs_Float.h" #ifdef HWRENDER #include "hardware/hw_main.h" @@ -329,19 +330,18 @@ static void FlipCam4_OnChange(void) FUNCINLINE ATTRINLINE INT32 R_PointOnSide(fixed_t x, fixed_t y, node_t *restrict node) { if (!node->dx) - return x <= node->x ? node->dy > 0 : node->dy < 0; + return x <= node->x ? node->dy > 0 : node->dy < 0; if (!node->dy) - return y <= node->y ? node->dx < 0 : node->dx > 0; + return y <= node->y ? node->dx < 0 : node->dx > 0; x -= node->x; y -= node->y; - // Try to quickly decide by looking at sign bits. - // also use a mask to avoid branch prediction - INT32 mask = (node->dy ^ node->dx ^ x ^ y) >> 31; - return (mask & ((node->dy ^ x) < 0)) | // (left is negative) - (~mask & (FixedMul(y, node->dx>>FRACBITS) >= FixedMul(node->dy>>FRACBITS, x))); + // Try to quickly decide by looking at sign bits. + if ((node->dy ^ node->dx ^ x ^ y) < 0) + return (node->dy ^ x) < 0; // (left is negative) + return (long long) y * node->dx >= (long long) x * node->dy; } // killough 5/2/98: reformatted @@ -364,7 +364,7 @@ FUNCINLINE ATTRINLINE INT32 R_PointOnSegSide(fixed_t x, fixed_t y, seg_t *line) // Try to quickly decide by looking at sign bits. if ((ldy ^ ldx ^ x ^ y) < 0) return (ldy ^ x) < 0; // (left is negative) - return FixedMul(y, ldx>>FRACBITS) >= FixedMul(ldy>>FRACBITS, x); + return (long long) y * ldx >= (long long) x * ldy; } // @@ -396,23 +396,6 @@ FUNCINLINE ATTRINLINE angle_t R_PointToAngle(fixed_t x, fixed_t y) 0; } -// This version uses 64-bit variables to avoid overflows with large values. -angle_t R_PointToAngle64(INT64 x, INT64 y) -{ - return (y -= viewy, (x -= viewx) || y) ? - x >= 0 ? - y >= 0 ? - (x > y) ? tantoangle[SlopeDivEx(y,x)] : // octant 0 - ANGLE_90-tantoangle[SlopeDivEx(x,y)] : // octant 1 - x > (y = -y) ? 0-tantoangle[SlopeDivEx(y,x)] : // octant 8 - ANGLE_270+tantoangle[SlopeDivEx(x,y)] : // octant 7 - y >= 0 ? (x = -x) > y ? ANGLE_180-tantoangle[SlopeDivEx(y,x)] : // octant 3 - ANGLE_90 + tantoangle[SlopeDivEx(x,y)] : // octant 2 - (x = -x) > (y = -y) ? ANGLE_180+tantoangle[SlopeDivEx(y,x)] : // octant 4 - ANGLE_270-tantoangle[SlopeDivEx(x,y)] : // octant 5 - 0; -} - angle_t R_PointToAngle2(fixed_t pviewx, fixed_t pviewy, fixed_t x, fixed_t y) { return (y -= pviewy, (x -= pviewx) || y) ? @@ -429,6 +412,27 @@ angle_t R_PointToAngle2(fixed_t pviewx, fixed_t pviewy, fixed_t x, fixed_t y) 0; } +angle_t R_PointToPseudoAngle (fixed_t x, fixed_t y) +{ + // Note: float won't work here as it's less precise than the BAM values being passed as parameters + double vecx = (double)x - viewx; + double vecy = (double)y - viewy; + + if (vecx == 0 && vecy == 0) + { + return 0; + } + else + { + double result = vecy / (fabs(vecx) + fabs(vecy)); + if (vecx < 0) + { + result = 2.0 - result; + } + return (angle_t)xs_CRoundToInt(result * (1 << 30)); + } +} + fixed_t R_PointToDist2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1) { return FixedHypot(px1 - px2, py1 - py2); @@ -1183,6 +1187,10 @@ boolean R_IsPointInSector(sector_t *sector, fixed_t x, fixed_t y) FUNCINLINE ATTRINLINE subsector_t *R_PointInSubsector(fixed_t x, fixed_t y) { size_t nodenum = numnodes-1; + + // special case for trivial maps (single subsector, no nodes) + if (numnodes == 0) + return subsectors; while (!(nodenum & NF_SUBSECTOR)) nodenum = nodes[nodenum].children[R_PointOnSide(x, y, nodes+nodenum)]; diff --git a/src/r_main.h b/src/r_main.h index 3df5bd088..826ece458 100644 --- a/src/r_main.h +++ b/src/r_main.h @@ -68,9 +68,9 @@ extern lighttable_t *zlight[LIGHTLEVELS][MAXLIGHTZ]; INT32 R_PointOnSide(fixed_t x, fixed_t y, node_t *node); INT32 R_PointOnSegSide(fixed_t x, fixed_t y, seg_t *line); angle_t R_PointToAngle(fixed_t x, fixed_t y); -angle_t R_PointToAngle64(INT64 x, INT64 y); angle_t R_PointToAngle2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1); angle_t R_PointToAngleEx(INT32 x2, INT32 y2, INT32 x1, INT32 y1); +angle_t R_PointToPseudoAngle (fixed_t x, fixed_t y); fixed_t R_PointToDist(fixed_t x, fixed_t y); fixed_t R_PointToDist2(fixed_t px2, fixed_t py2, fixed_t px1, fixed_t py1); angle_t R_PlayerSliptideAngle(player_t *player); diff --git a/src/xs_Float.h b/src/xs_Float.h new file mode 100644 index 000000000..68901d5fe --- /dev/null +++ b/src/xs_Float.h @@ -0,0 +1,81 @@ +// ==================================================================================================================== +// ==================================================================================================================== +// xs_Float.h +// +// Source: "Know Your FPU: Fixing Floating Fast" +// http://www.stereopsis.com/sree/fpu2006.html +// +// xs_CRoundToInt: Round toward nearest, but ties round toward even (just like FISTP) +// xs_ToInt: Round toward zero, just like the C (int) cast +// xs_FloorToInt: Round down +// xs_CeilToInt: Round up +// xs_RoundToInt: Round toward nearest, but ties round up +// ==================================================================================================================== +// ==================================================================================================================== +#ifndef _xs_FLOAT_H_ +#define _xs_FLOAT_H_ + +//#include "xs_Config.h" + +// ==================================================================================================================== +// Defines +// ==================================================================================================================== +#ifndef _xs_DEFAULT_CONVERSION +#define _xs_DEFAULT_CONVERSION 0 +#endif //_xs_DEFAULT_CONVERSION + + +#ifdef WORDS_BIGENDIAN + #define _xs_iexp_ 0 + #define _xs_iman_ 1 +#else + #define _xs_iexp_ 1 //intel is little endian + #define _xs_iman_ 0 +#endif //BigEndian_ + +#ifdef __GNUC__ +#define finline inline __attribute__ ((__always_inline__)) +#else +#define finline __forceinline +#endif + +typedef double real64; + + +typedef union _xs_doubleints +{ + real64 val; + unsigned ival[2]; +} _xs_doubleints; + + +// ==================================================================================================================== +// ==================================================================================================================== +// Inline implementation +// ==================================================================================================================== +// ==================================================================================================================== +finline int xs_CRoundToInt(real64 val) +{ +#if _xs_DEFAULT_CONVERSION==0 + _xs_doubleints uval; + uval.val = val + 6755399441055744.0; + return uval.ival[_xs_iman_]; +#else + return int(floor(val+.5)); +#endif +} + + +// ==================================================================================================================== +// ==================================================================================================================== +// Unsigned variants +// ==================================================================================================================== +// ==================================================================================================================== +finline unsigned xs_CRoundToUInt(real64 val) +{ + return (unsigned)xs_CRoundToInt(val); +} + +// ==================================================================================================================== +// ==================================================================================================================== +#endif // _xs_FLOAT_H_