From 169e7cd462d6d96bf3d10db96b9af3d9178cca82 Mon Sep 17 00:00:00 2001 From: Alug Date: Sun, 5 May 2024 21:52:48 +0200 Subject: [PATCH] Render precip through blockmap instead of during BSP from RR --- src/hardware/hw_main.c | 60 ++++++++++++++++++++++++------------ src/p_local.h | 1 + src/p_maputl.c | 70 ++++++++++++++++++++++++------------------ src/p_mobj.h | 12 ++++---- src/p_setup.c | 45 ++++++++------------------- src/r_defs.h | 1 - src/r_main.c | 31 ++++++++++++++++++- src/r_main.h | 1 + src/r_things.c | 40 ++++++++++++++++++------ src/r_things.h | 1 + src/tables.h | 5 +++ 11 files changed, 168 insertions(+), 99 deletions(-) diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index 5bb5ff370..4f5475a67 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -265,6 +265,8 @@ ps_metric_t ps_hw_numcolors = {0}; ps_metric_t ps_hw_batchsorttime = {0}; ps_metric_t ps_hw_batchdrawtime = {0}; +static void HWR_AddPrecipitationSprites(void); + // ========================================================================== // View position // ========================================================================== @@ -4749,8 +4751,7 @@ void HWR_DrawSprites(void) void HWR_AddSprites(sector_t *sec) { mobj_t *thing; - precipmobj_t *precipthing; - fixed_t approx_dist, limit_dist, precip_limit_dist; + fixed_t approx_dist, limit_dist; INT32 splitflags; boolean split_drawsprite; // drawing with splitscreen flags @@ -4772,13 +4773,9 @@ void HWR_AddSprites(sector_t *sec) limit_dist = min(current_bsp_culling_distance, cv_drawdist.value << FRACBITS); else limit_dist = current_bsp_culling_distance; - precip_limit_dist = min(current_bsp_culling_distance, cv_drawdist_precip.value << FRACBITS); } else - { limit_dist = cv_drawdist.value << FRACBITS; - precip_limit_dist = cv_drawdist_precip.value << FRACBITS; - } // Handle all things in sector. // If a limit exists, handle things a tiny bit different. @@ -4786,7 +4783,6 @@ void HWR_AddSprites(sector_t *sec) { for (thing = sec->thinglist; thing; thing = thing->snext) { - split_drawsprite = false; if (thing->sprite == SPR_NULL || thing->flags2 & MF2_DONTDRAW) @@ -4831,7 +4827,6 @@ void HWR_AddSprites(sector_t *sec) // Draw everything in sector, no checks for (thing = sec->thinglist; thing; thing = thing->snext) { - split_drawsprite = false; if (thing->sprite == SPR_NULL || thing->flags2 & MF2_DONTDRAW) @@ -4866,21 +4861,46 @@ void HWR_AddSprites(sector_t *sec) HWR_ProjectSprite(thing); } } +} + +// -------------------------------------------------------------------------- +// HWR_AddPrecipitationSprites +// This renders through the blockmap instead of BSP to avoid +// iterating a huge amount of precipitation sprites in sectors +// that are beyond drawdist. +// -------------------------------------------------------------------------- +static void HWR_AddPrecipitationSprites(void) +{ + //const fixed_t drawdist = cv_drawdist_precip.value * mapobjectscale; + fixed_t drawdist; + + INT32 xl, xh, yl, yh, bx, by; + precipmobj_t *th; + + if (current_bsp_culling_distance) + drawdist = min(current_bsp_culling_distance, cv_drawdist_precip.value * mapobjectscale); + else + drawdist = cv_drawdist_precip.value * mapobjectscale; // No to infinite precipitation draw distance. - if (precip_limit_dist) + if (drawdist == 0) { - for (precipthing = sec->preciplist; precipthing; precipthing = precipthing->snext) + return; + } + + R_GetRenderBlockMapDimensions(drawdist, &xl, &xh, &yl, &yh); + + for (bx = xl; bx <= xh; bx++) + { + for (by = yl; by <= yh; by++) { - if (precipthing->precipflags & PCF_INVISIBLE) - continue; + for (th = precipblocklinks[(by * bmapwidth) + bx]; th; th = th->bnext) + { + if (th->precipflags & PCF_INVISIBLE) + continue; - approx_dist = P_AproxDistance(viewx-precipthing->x, viewy-precipthing->y); - - if (approx_dist > precip_limit_dist) - continue; - - HWR_ProjectPrecipitationSprite(precipthing); + HWR_ProjectPrecipitationSprite(th); + } } } } @@ -5708,6 +5728,8 @@ static void HWR_RenderViewpoint(gl_portal_t *rootportal, const float fpov, playe if (cv_grbatching.value) HWR_StartBatching(); + HWR_AddPrecipitationSprites(); + ps_numbspcalls.value.i = 0; ps_numpolyobjects.value.i = 0; PS_START_TIMING(ps_bsptime); @@ -5727,7 +5749,7 @@ static void HWR_RenderViewpoint(gl_portal_t *rootportal, const float fpov, playe gr_portal = oldgl_portal_state; PS_STOP_TIMING(ps_bsptime); - + if (cv_grbatching.value) HWR_RenderBatches(); diff --git a/src/p_local.h b/src/p_local.h index dc473b648..e1a07526a 100644 --- a/src/p_local.h +++ b/src/p_local.h @@ -387,6 +387,7 @@ extern INT32 bmapheight; // in mapblocks extern fixed_t bmaporgx; extern fixed_t bmaporgy; // origin of block map extern mobj_t **blocklinks; // for thing chains +extern precipmobj_t **precipblocklinks; // special blockmap for precip rendering // // P_INTER diff --git a/src/p_maputl.c b/src/p_maputl.c index c680931d0..3c91dc922 100644 --- a/src/p_maputl.c +++ b/src/p_maputl.c @@ -754,15 +754,45 @@ void P_UnsetThingPosition(mobj_t *thing) void P_UnsetPrecipThingPosition(precipmobj_t *thing) { - precipmobj_t **sprev = thing->sprev; - precipmobj_t *snext = thing->snext; - if ((*sprev = snext) != NULL) // unlink from sector list - snext->sprev = sprev; + precipmobj_t **bprev = thing->bprev; + precipmobj_t *bnext = thing->bnext; + + if (bprev && (*bprev = bnext) != NULL) // unlink from block map + bnext->bprev = bprev; precipsector_list = thing->touching_sectorlist; thing->touching_sectorlist = NULL; //to be restored by P_SetPrecipThingPosition } +static void P_LinkToBlockMap(mobj_t *thing, mobj_t **bmap) +{ + const INT32 blockx = (unsigned)(thing->x - bmaporgx) >> MAPBLOCKSHIFT; + const INT32 blocky = (unsigned)(thing->y - bmaporgy) >> MAPBLOCKSHIFT; + + if (blockx >= 0 && blockx < bmapwidth + && blocky >= 0 && blocky < bmapheight) + { + // killough 8/11/98: simpler scheme using + // pointer-to-pointer prev pointers -- + // allows head nodes to be treated like everything else + + mobj_t **link = &bmap[(blocky * bmapwidth) + blockx]; + mobj_t *bnext = *link; + + thing->bnext = bnext; + + if (bnext != NULL) + bnext->bprev = &thing->bnext; + + thing->bprev = link; + *link = thing; + } + else // thing is off the map + { + thing->bnext = NULL, thing->bprev = NULL; + } +} + // // P_SetThingPosition // Links a thing into both a block and a subsector @@ -819,24 +849,7 @@ void P_SetThingPosition(mobj_t *thing) if (!(thing->flags & MF_NOBLOCKMAP)) { // inert things don't need to be in blockmap - const INT32 blockx = (unsigned)(thing->x - bmaporgx)>>MAPBLOCKSHIFT; - const INT32 blocky = (unsigned)(thing->y - bmaporgy)>>MAPBLOCKSHIFT; - if (blockx >= 0 && blockx < bmapwidth - && blocky >= 0 && blocky < bmapheight) - { - // killough 8/11/98: simpler scheme using - // pointer-to-pointer prev pointers -- - // allows head nodes to be treated like everything else - - mobj_t **link = &blocklinks[blocky*bmapwidth + blockx]; - mobj_t *bnext = *link; - if ((thing->bnext = bnext) != NULL) - bnext->bprev = &thing->bnext; - thing->bprev = link; - *link = thing; - } - else // thing is off the map - thing->bnext = NULL, thing->bprev = NULL; + P_LinkToBlockMap(thing, blocklinks); } // Allows you to 'step' on a new linedef exec when the previous @@ -891,18 +904,15 @@ void P_SetUnderlayPosition(mobj_t *thing) void P_SetPrecipitationThingPosition(precipmobj_t *thing) { - subsector_t *ss = thing->subsector = R_PointInSubsector(thing->x, thing->y); - - precipmobj_t **link = &ss->sector->preciplist; - precipmobj_t *snext = *link; - if ((thing->snext = snext) != NULL) - snext->sprev = &thing->snext; - thing->sprev = link; - *link = thing; + thing->subsector = R_PointInSubsector(thing->x, thing->y); P_CreatePrecipSecNodeList(thing, thing->x, thing->y); thing->touching_sectorlist = precipsector_list; // Attach to Thing's precipmobj_t precipsector_list = NULL; // clear for next time + + // NOTE: this works because bnext/bprev are at the same + // offsets in precipmobj_t and mobj_t + P_LinkToBlockMap((mobj_t*)thing, (mobj_t**)precipblocklinks); } // diff --git a/src/p_mobj.h b/src/p_mobj.h index 5d26f9b44..a824edc6a 100644 --- a/src/p_mobj.h +++ b/src/p_mobj.h @@ -279,8 +279,8 @@ typedef struct mobj_s fixed_t old_x2, old_y2, old_z2; // More list: links in sector (if needed) - struct mobj_s *snext; - struct mobj_s **sprev; // killough 8/11/98: change to ptr-to-ptr + struct mobj_s *bnext; + struct mobj_s **bprev; // killough 8/11/98: change to ptr-to-ptr // More drawing info: to determine current sprite. angle_t angle, pitch, roll; // orientation @@ -338,8 +338,8 @@ typedef struct mobj_s // Interaction info, by BLOCKMAP. // Links in blocks (if needed). - struct mobj_s *bnext; - struct mobj_s **bprev; // killough 8/11/98: change to ptr-to-ptr + struct mobj_s *snext; + struct mobj_s **sprev; // killough 8/11/98: change to ptr-to-ptr // Additional pointers for NiGHTS hoops struct mobj_s *hnext; @@ -432,8 +432,8 @@ typedef struct precipmobj_s fixed_t old_x2, old_y2, old_z2; // More list: links in sector (if needed) - struct precipmobj_s *snext; - struct precipmobj_s **sprev; // killough 8/11/98: change to ptr-to-ptr + struct precipmobj_s *bnext; + struct precipmobj_s **bprev; // killough 8/11/98: change to ptr-to-ptr // More drawing info: to determine current sprite. angle_t angle, pitch, roll; // orientation diff --git a/src/p_setup.c b/src/p_setup.c index 9ae36afc9..c99326116 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -127,6 +127,7 @@ INT32 *blockmaplump; // Big blockmap fixed_t bmaporgx, bmaporgy; // for thing chains mobj_t **blocklinks; +precipmobj_t **precipblocklinks; // REJECT // For fast sight rejection. @@ -714,7 +715,7 @@ static void P_LoadRawSectors(UINT8 *data, size_t i) ss->thinglist = NULL; ss->touching_thinglist = NULL; - ss->preciplist = NULL; + ss->touching_preciplist = NULL; ss->floordata = NULL; @@ -1948,6 +1949,9 @@ static void P_CreateBlockMap(void) // haleyjd 2/22/06: setup polyobject blockmap count = sizeof(*polyblocklinks) * bmapwidth * bmapheight; polyblocklinks = Z_Calloc(count, PU_LEVEL, NULL); + + count = sizeof (*precipblocklinks)* bmapwidth*bmapheight; + precipblocklinks = Z_Calloc(count, PU_LEVEL, NULL); } } @@ -1982,10 +1986,6 @@ static void P_ReadBlockMapLump(INT16 *wadblockmaplump, size_t count) // this should return false. static boolean P_LoadBlockMap(lumpnum_t lumpnum) { -#if 0 - (void)lumpnum; - return false; -#else size_t count; const char *lumpname = W_CheckNameForNum(lumpnum); @@ -2023,27 +2023,11 @@ static boolean P_LoadBlockMap(lumpnum_t lumpnum) // haleyjd 2/22/06: setup polyobject blockmap count = sizeof(*polyblocklinks) * bmapwidth * bmapheight; polyblocklinks = Z_Calloc(count, PU_LEVEL, NULL); + + count = sizeof (*precipblocklinks)* bmapwidth*bmapheight; + precipblocklinks = Z_Calloc(count, PU_LEVEL, NULL); + return true; -/* Original - blockmaplump = W_CacheLumpNum(lump, PU_LEVEL); - blockmap = blockmaplump+4; - count = W_LumpLength (lump)/2; - - for (i = 0; i < count; i++) - blockmaplump[i] = SHORT(blockmaplump[i]); - - bmaporgx = blockmaplump[0]<>MAPBLOCKSHIFT; + *xh = (unsigned)(max(viewx, max(vxleft, vxright)) - bmaporgx)>>MAPBLOCKSHIFT; + *yl = (unsigned)(min(viewy, min(vyleft, vyright)) - bmaporgy)>>MAPBLOCKSHIFT; + *yh = (unsigned)(max(viewy, max(vyleft, vyright)) - bmaporgy)>>MAPBLOCKSHIFT; + + if (*xh >= bmapwidth) + *xh = bmapwidth - 1; + + if (*yh >= bmapheight) + *yh = bmapheight - 1; + + BMBOUNDFIX(*xl, *xh, *yl, *yh); +} + // // R_InitTextureMapping // @@ -1728,11 +1756,12 @@ void R_RenderPlayerView(player_t *player) PS_START_TIMING(ps_bsptime); R_RenderBSPNode((INT32)numnodes - 1); PS_STOP_TIMING(ps_bsptime); + R_AddPrecipitationSprites(); PS_START_TIMING(ps_sw_spritecliptime); R_ClipSprites(); PS_STOP_TIMING(ps_sw_spritecliptime); - ps_numsprites.value.i = numvisiblesprites; + //ps_numsprites.value.i = numvisiblesprites; // PORTAL RENDERING PS_START_TIMING(ps_sw_portaltime); diff --git a/src/r_main.h b/src/r_main.h index b3f38e156..56e351138 100644 --- a/src/r_main.h +++ b/src/r_main.h @@ -80,6 +80,7 @@ subsector_t *R_PointInSubsector(fixed_t x, fixed_t y); subsector_t *R_IsPointInSubsector(fixed_t x, fixed_t y); boolean R_DoCulling(line_t *cullheight, line_t *viewcullheight, fixed_t vz, fixed_t bottomh, fixed_t toph); +void R_GetRenderBlockMapDimensions(fixed_t drawdist, INT32 *xl, INT32 *xh, INT32 *yl, INT32 *yh); // Performance stats extern precise_t ps_prevframetime;// time when previous frame was rendered diff --git a/src/r_things.c b/src/r_things.c index c66ca9f58..dfe05dbdf 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -1897,7 +1897,6 @@ weatherthink: void R_AddSprites(sector_t *sec, INT32 lightlevel) { mobj_t *thing; - precipmobj_t *precipthing; // Tails 08-25-2002 INT32 lightnum; fixed_t approx_dist, limit_dist; @@ -2016,21 +2015,42 @@ void R_AddSprites(sector_t *sec, INT32 lightlevel) R_ProjectSprite(thing); } } +} + +// R_AddPrecipitationSprites +// This renders through the blockmap instead of BSP to avoid +// iterating a huge amount of precipitation sprites in sectors +// that are beyond drawdist. +// +void R_AddPrecipitationSprites(void) +{ + const fixed_t drawdist = cv_drawdist_precip.value * mapobjectscale; + + INT32 xl, xh, yl, yh, bx, by; + precipmobj_t *th, *next; + + if (drawdist == 0) + { + return; + } + + R_GetRenderBlockMapDimensions(drawdist, &xl, &xh, &yl, &yh); // no, no infinite draw distance for precipitation. this option at zero is supposed to turn it off - if ((limit_dist = (fixed_t)cv_drawdist_precip.value << FRACBITS)) + for (bx = xl; bx <= xh; bx++) { - for (precipthing = sec->preciplist; precipthing; precipthing = precipthing->snext) + for (by = yl; by <= yh; by++) { - if (precipthing->precipflags & PCF_INVISIBLE) - continue; + for (th = precipblocklinks[(by * bmapwidth) + bx]; th; th = next) + { + // Store this beforehand because R_ProjectPrecipitionSprite may free th (see P_PrecipThinker) + next = th->bnext; - approx_dist = P_AproxDistance(viewx-precipthing->x, viewy-precipthing->y); + if (th->precipflags & PCF_INVISIBLE) + continue; - if (approx_dist > limit_dist) - continue; - - R_ProjectPrecipitationSprite(precipthing); + R_ProjectPrecipitationSprite(th); + } } } } diff --git a/src/r_things.h b/src/r_things.h index b9e05022e..88e1abdab 100644 --- a/src/r_things.h +++ b/src/r_things.h @@ -67,6 +67,7 @@ void R_AddSpriteDefs(UINT16 wadnum); //SoM: 6/5/2000: Light sprites correctly! void R_AddSprites(sector_t *sec, INT32 lightlevel); +void R_AddPrecipitationSprites(void); void R_InitSprites(void); void R_ClearSprites(void); void R_DrawMasked(void); diff --git a/src/tables.h b/src/tables.h index 353bf591f..761a6499e 100644 --- a/src/tables.h +++ b/src/tables.h @@ -24,6 +24,7 @@ #define FINEMASK (FINEANGLES - 1) #define ANGLETOFINESHIFT 19 // 0x100000000 to 0x2000 #define FINEANGLE_C(x) ((FixedAngle((x)*FRACUNIT)>>ANGLETOFINESHIFT) & FINEMASK) // ((x*(ANGLE_45/45))>>ANGLETOFINESHIFT) & FINEMASK +#define ANGLETOFINE(x) (((x)>>ANGLETOFINESHIFT) & FINEMASK) // Effective size is 10240. extern fixed_t finesine[5*FINEANGLES/4]; @@ -121,4 +122,8 @@ void FM_Rotate(matrix_t *dest, angle_t angle, fixed_t x, fixed_t y, fixed_t z); #define FINECOSINE(n) (finecosine[n]>>(FINE_FRACBITS-FRACBITS)) #define FINETANGENT(n) (finetangent[n]>>(FINE_FRACBITS-FRACBITS)) +// FSIN(ANGLE_90) = FRACUNIT +#define FSIN(n) FINESINE(ANGLETOFINE(n)) +#define FCOS(n) FINECOSINE(ANGLETOFINE(n)) + #endif