From 24e7f5f864f6f6f7bd559ba24d39d71dae8bfbaf Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 12 Jul 2025 13:34:44 +0200 Subject: [PATCH] readd R_PointInSubsectorFast use it for camera, viewpoint, some precip and sound stuff and (visual) banana slope calc after profiling it a bit, this may be 2-4x faster in a bunch cases --- src/d_clisrv.c | 2 +- src/hardware/hw_main.cpp | 2 +- src/hardware/hw_portal.c | 2 +- src/k_kart.c | 2 +- src/p_map.c | 4 ++-- src/p_maputl.c | 2 +- src/p_mobj.c | 8 ++++---- src/p_setup.c | 4 ++-- src/p_spec.c | 2 +- src/p_user.c | 6 +++--- src/r_fps.c | 2 +- src/r_main.cpp | 6 ++---- src/r_main.h | 18 ++++++++++-------- src/s_sound.c | 4 ++-- 14 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 178faf8a4..892ac14a0 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -2014,7 +2014,7 @@ static void CL_ReloadReceivedSavegame(void) for (i = 0; i < MAXSPLITSCREENPLAYERS; i++) { - camera[i].subsector = R_PointInSubsector(camera[i].x, camera[i].y); + camera[i].subsector = R_PointInSubsectorFast(camera[i].x, camera[i].y); } cl_redownloadinggamestate = false; diff --git a/src/hardware/hw_main.cpp b/src/hardware/hw_main.cpp index bbb116250..1555cffda 100644 --- a/src/hardware/hw_main.cpp +++ b/src/hardware/hw_main.cpp @@ -3316,7 +3316,7 @@ static gl_vissprite_t *HWR_NewVisSprite(void) // Finds a floor through which light does not pass. static fixed_t HWR_OpaqueFloorAtPos(fixed_t x, fixed_t y, fixed_t z, fixed_t height) { - const sector_t *sec = R_PointInSubsector(x, y)->sector; + const sector_t *sec = R_PointInSubsectorFast(x, y)->sector; fixed_t floorz = sec->floorheight; if (sec->ffloors) diff --git a/src/hardware/hw_portal.c b/src/hardware/hw_portal.c index 909214171..db17d05bf 100644 --- a/src/hardware/hw_portal.c +++ b/src/hardware/hw_portal.c @@ -132,7 +132,7 @@ void HWR_PortalFrame(gl_portal_t* portal) { portalclipline = NULL; portalcullsector = NULL; - viewsector = R_PointInSubsector(viewx, viewy)->sector; + viewsector = R_PointInSubsectorFast(viewx, viewy)->sector; } } diff --git a/src/k_kart.c b/src/k_kart.c index f02da77ea..84ef84e16 100644 --- a/src/k_kart.c +++ b/src/k_kart.c @@ -4600,7 +4600,7 @@ static void K_CalculateBananaSlope(mobj_t *mobj, fixed_t x, fixed_t y, fixed_t z //sec = R_PointInSubsector(x, y)->sector; if (mobj->x != x || mobj->y != y || mobj->subsector == NULL) - sec = R_PointInSubsector(x, y)->sector; + sec = R_PointInSubsectorFast(x, y)->sector; else sec = mobj->subsector->sector; diff --git a/src/p_map.c b/src/p_map.c index 17263bfe9..f11a19fff 100644 --- a/src/p_map.c +++ b/src/p_map.c @@ -1995,7 +1995,7 @@ boolean P_CheckCameraPosition(fixed_t x, fixed_t y, camera_t *thiscam) tmbbox[BOXLEFT] = x - thiscam->radius; if (thiscam->x != x || thiscam->y != y || thiscam->subsector == NULL) - newsubsec = R_PointInSubsector(x, y); + newsubsec = R_PointInSubsectorFast(x, y); else newsubsec = thiscam->subsector; @@ -2181,7 +2181,7 @@ boolean P_TryCameraMove(fixed_t x, fixed_t y, camera_t *thiscam) return false; if (thiscam->x != x || thiscam->y != y || thiscam->subsector == NULL) - s = R_PointInSubsector(x, y); + s = R_PointInSubsectorFast(x, y); else s = thiscam->subsector; diff --git a/src/p_maputl.c b/src/p_maputl.c index 7b748c22e..11e7a1050 100644 --- a/src/p_maputl.c +++ b/src/p_maputl.c @@ -908,7 +908,7 @@ void P_SetUnderlayPosition(mobj_t *thing) void P_SetPrecipitationThingPosition(precipmobj_t *thing) { - thing->subsector = R_PointInSubsector(thing->x, thing->y); + thing->subsector = R_PointInSubsectorFast(thing->x, thing->y); // NOTE: this works because bnext/bprev are at the same // offsets in precipmobj_t and mobj_t diff --git a/src/p_mobj.c b/src/p_mobj.c index 70f4dbacf..80615c608 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -777,7 +777,7 @@ fixed_t P_CameraFloorZ(camera_t *mobj, sector_t *sector, sector_t *boundsec, fix testy += y; // If the highest point is in the sector, then we have it easy! Just get the Z at that point - if (R_PointInSubsector(testx, testy)->sector == (boundsec ? boundsec : sector)) + if (R_PointInSubsectorFast(testx, testy)->sector == (boundsec ? boundsec : sector)) return P_GetSlopeZAt(slope, testx, testy); // If boundsec is set, we're looking for specials. In that case, iterate over every line in this sector to find the TRUE highest/lowest point @@ -856,7 +856,7 @@ fixed_t P_CameraCeilingZ(camera_t *mobj, sector_t *sector, sector_t *boundsec, f testy += y; // If the highest point is in the sector, then we have it easy! Just get the Z at that point - if (R_PointInSubsector(testx, testy)->sector == (boundsec ? boundsec : sector)) + if (R_PointInSubsectorFast(testx, testy)->sector == (boundsec ? boundsec : sector)) return P_GetSlopeZAt(slope, testx, testy); // If boundsec is set, we're looking for specials. In that case, iterate over every line in this sector to find the TRUE highest/lowest point @@ -3308,7 +3308,7 @@ boolean P_CameraThinker(player_t *player, camera_t *thiscam, boolean resetcalled } } - thiscam->subsector = R_PointInSubsector(thiscam->x, thiscam->y); + thiscam->subsector = R_PointInSubsectorFast(thiscam->x, thiscam->y); thiscam->floorz = tmfloorz; thiscam->ceilingz = tmceilingz; @@ -10319,7 +10319,7 @@ void P_PrecipitationEffects(void) for (y = yl; y <= yh; y += RADIUSSTEP) for (x = xl; x <= xh; x += RADIUSSTEP) { - if (R_PointInSubsector((fixed_t)x, (fixed_t)y)->sector->ceilingpic != skyflatnum) // Found the outdoors! + if (R_PointInSubsectorFast((fixed_t)x, (fixed_t)y)->sector->ceilingpic != skyflatnum) // Found the outdoors! continue; newdist = S_CalculateSoundDistance(players[displayplayers[0]].mo->x, players[displayplayers[0]].mo->y, 0, (fixed_t)x, (fixed_t)y, 0); diff --git a/src/p_setup.c b/src/p_setup.c index 118dbfc74..4c76478dc 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -2510,7 +2510,6 @@ static void P_SetupCamera(UINT8 pnum, camera_t *cam) cam->y = players[pnum].mo->y; cam->z = players[pnum].mo->z; cam->angle = players[pnum].mo->angle; - cam->subsector = R_PointInSubsector(cam->x, cam->y); // make sure camera has a subsector set -- Monster Iestyn (12/11/18) } else { @@ -2535,9 +2534,10 @@ static void P_SetupCamera(UINT8 pnum, camera_t *cam) cam->y = thing->y; cam->z = thing->z; cam->angle = FixedAngle((fixed_t)thing->angle << FRACBITS); - cam->subsector = R_PointInSubsector(cam->x, cam->y); // make sure camera has a subsector set -- Monster Iestyn (12/11/18) } + cam->subsector = R_PointInSubsectorFast(cam->x, cam->y); // make sure camera has a subsector set -- Monster Iestyn (12/11/18) + cam->chase = false; // tell camera to reset its position next tic } diff --git a/src/p_spec.c b/src/p_spec.c index 70fb19c20..224a55171 100644 --- a/src/p_spec.c +++ b/src/p_spec.c @@ -2022,7 +2022,7 @@ static void P_ProcessLineSpecial(line_t *line, mobj_t *mo, sector_t *callsec) camera[i].y += y; camera[i].z += z; camera[i].reset = true; - camera[i].subsector = R_PointInSubsector(camera[i].x, camera[i].y); + camera[i].subsector = R_PointInSubsectorFast(camera[i].x, camera[i].y); R_RelativeTeleportViewInterpolation(i, x, y, z, 0); break; } diff --git a/src/p_user.c b/src/p_user.c index bbf3d2478..3dff40e0a 100644 --- a/src/p_user.c +++ b/src/p_user.c @@ -3402,7 +3402,7 @@ static void P_DemoCameraMovement(camera_t *cam, UINT8 num) } // update subsector to avoid crashes; - cam->subsector = R_PointInSubsector(cam->x, cam->y); + cam->subsector = R_PointInSubsectorFast(cam->x, cam->y); } void P_ResetCamera(player_t *player, camera_t *thiscam) @@ -3446,7 +3446,7 @@ void P_ResetCamera(player_t *player, camera_t *thiscam) thiscam->relativex = 0; - thiscam->subsector = R_PointInSubsector(thiscam->x,thiscam->y); + thiscam->subsector = R_PointInSubsectorFast(thiscam->x,thiscam->y); thiscam->radius = 20*FRACUNIT; thiscam->height = 16*FRACUNIT; @@ -3682,7 +3682,7 @@ static void P_MoveCameraToSpawn(UINT8 playernum) thiscam->angle = player->mo->angle; thiscam->aiming = 0; - thiscam->subsector = R_PointInSubsector(thiscam->x,thiscam->y); + thiscam->subsector = R_PointInSubsectorFast(thiscam->x,thiscam->y); thiscam->reset_aiming = true; diff --git a/src/r_fps.c b/src/r_fps.c index 1d5cc1baf..0c3a58737 100644 --- a/src/r_fps.c +++ b/src/r_fps.c @@ -232,7 +232,7 @@ void R_InterpolateView(fixed_t frac, boolean forceinvalid) viewcos = FINECOSINE(viewangle>>ANGLETOFINESHIFT); viewplayer = newview->player; - viewsector = R_PointInSubsector(viewx, viewy)->sector; + viewsector = R_PointInSubsectorFast(viewx, viewy)->sector; R_SetupFreelook(); } diff --git a/src/r_main.cpp b/src/r_main.cpp index b2b5ed73f..f6144c64e 100644 --- a/src/r_main.cpp +++ b/src/r_main.cpp @@ -579,8 +579,6 @@ static void R_InitTextureMapping(void) doubleclipangle = clipangle*2; } - - // // R_InitLightTables // Only inits the zlight table, @@ -1093,7 +1091,7 @@ static void R_SetupCommonFrame(player_t * player, sector_t * sector) if (sector != NULL) newview->sector = sector; else - newview->sector = R_PointInSubsector(newview->x, newview->y)->sector; + newview->sector = R_PointInSubsectorFast(newview->x, newview->y)->sector; R_InterpolateView(rendertimefrac_unpaused, false); } @@ -1333,7 +1331,7 @@ static void R_PortalFrame(portal_t *portal) { portalclipline = NULL; portalcullsector = NULL; - viewsector = R_PointInSubsector(viewx, viewy)->sector; + viewsector = R_PointInSubsectorFast(viewx, viewy)->sector; } } diff --git a/src/r_main.h b/src/r_main.h index 2e27d6036..efe3fc31c 100644 --- a/src/r_main.h +++ b/src/r_main.h @@ -137,16 +137,18 @@ fixed_t R_ScaleFromGlobalAngle(angle_t visangle); // // R_PointInSubsector // -FUNCINLINE static ATTRINLINE subsector_t *R_PointInSubsector(fixed_t x, fixed_t y) -{ - size_t nodenum = numnodes-1; - - while (!(nodenum & NF_SUBSECTOR)) - nodenum = nodes[nodenum].children[R_PointOnSide(x, y, nodes+nodenum)]; - - return &subsectors[nodenum & ~NF_SUBSECTOR]; +#define R_POINTINSUBSECTOR(FUNCNAME, SIDEFUNC)\ +FUNCINLINE static ATTRINLINE subsector_t *FUNCNAME(fixed_t x, fixed_t y)\ +{\ + size_t nodenum = numnodes-1;\ + while (!(nodenum & NF_SUBSECTOR))\ + nodenum = nodes[nodenum].children[SIDEFUNC(x, y, nodes+nodenum)];\ + return &subsectors[nodenum & ~NF_SUBSECTOR];\ } +R_POINTINSUBSECTOR(R_PointInSubsector, R_PointOnSide) +R_POINTINSUBSECTOR(R_PointInSubsectorFast, R_PointOnSideFast) + subsector_t *R_IsPointInSubsector(fixed_t x, fixed_t y); #define R_PointToDist(x, y) R_PointToDist2(viewx, viewy, x, y) diff --git a/src/s_sound.c b/src/s_sound.c index 03a70cf89..c93570232 100644 --- a/src/s_sound.c +++ b/src/s_sound.c @@ -957,7 +957,7 @@ boolean S_AdjustSoundParams(const mobj_t *listener, const mobj_t *source, INT32 INT64 x, y, yl, yh, xl, xh; fixed_t newdist; - if (R_PointInSubsector(listensource.x, listensource.y)->sector->ceilingpic == skyflatnum) + if (R_PointInSubsectorFast(listensource.x, listensource.y)->sector->ceilingpic == skyflatnum) approx_dist = 0; else { @@ -970,7 +970,7 @@ boolean S_AdjustSoundParams(const mobj_t *listener, const mobj_t *source, INT32 for (y = yl; y <= yh; y += FRACUNIT*64) for (x = xl; x <= xh; x += FRACUNIT*64) { - if (R_PointInSubsector(x, y)->sector->ceilingpic == skyflatnum) + if (R_PointInSubsectorFast(x, y)->sector->ceilingpic == skyflatnum) { // Found the outdoors! newdist = S_CalculateSoundDistance(listensource.x, listensource.y, 0, x, y, 0);