From c31e7ff2b37abbd561a4e20b05fdaf726f50203a Mon Sep 17 00:00:00 2001 From: alufolie91 Date: Fri, 8 Sep 2023 12:44:13 +0200 Subject: [PATCH] Optimize sprite rendering by ignoring completely occluded sprites backport of the srb2 thing judging by perfstats it often cuts the spritecount in half take with a grain of salt since i barely tested it software mode only btw https://git.do.srb2.org/STJr/SRB2/-/merge_requests/2131?commit_id=b8313ceda283ac0c8dd9ab6141e83a31f0bb9639 --- src/r_main.c | 29 +++-------------- src/r_main.h | 2 +- src/r_things.c | 86 ++++++++++++++++++++++++++++++++++++++++++++------ src/r_things.h | 9 +++--- 4 files changed, 87 insertions(+), 39 deletions(-) diff --git a/src/r_main.c b/src/r_main.c index dc914e79a..484cda6d7 100644 --- a/src/r_main.c +++ b/src/r_main.c @@ -37,16 +37,6 @@ #include "hardware/hw_main.h" #endif -//profile stuff --------------------------------------------------------- -//#define TIMING -#ifdef TIMING -#include "p5prof.h" -INT64 mycount; -INT64 mytotal = 0; -//unsigned long nombre = 100000; -#endif -//profile stuff --------------------------------------------------------- - // Fineangles in the SCREENWIDTH wide window. #define FIELDOFVIEW 2048 @@ -197,7 +187,8 @@ consvar_t cv_flipcam4 = {"flipcam4", "No", CV_SAVE|CV_CALL|CV_NOINIT, CV_YesNo, consvar_t cv_shadow = {"shadow", "Off", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_shadowoffs = {"offsetshadows", "Off", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_skybox = {"skybox", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; -consvar_t cv_ffloorclip = {"ffloorclip", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_ffloorclip = {"r_ffloorclip", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_spriteclip = {"r_spriteclip", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_soniccd = {"soniccd", "Off", CV_NETVAR|CV_NOSHOWHELP, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_allowmlook = {"allowmlook", "Yes", CV_NETVAR, CV_YesNo, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_showhud = {"showhud", "Yes", CV_CALL, CV_YesNo, R_SetViewSize, 0, NULL, NULL, 0, 0, NULL}; @@ -1765,26 +1756,15 @@ void R_RenderPlayerView(player_t *player) // The head node is the last node output. -//profile stuff --------------------------------------------------------- -#ifdef TIMING - mytotal = 0; - ProfZeroTimer(); -#endif ps_numbspcalls.value.i = ps_numpolyobjects.value.i = ps_numdrawnodes.value.i = 0; PS_START_TIMING(ps_bsptime); R_RenderBSPNode((INT32)numnodes - 1); PS_STOP_TIMING(ps_bsptime); - ps_numsprites.value.i = visspritecount; PS_START_TIMING(ps_sw_spritecliptime); R_ClipSprites(); PS_STOP_TIMING(ps_sw_spritecliptime); -#ifdef TIMING - RDMSR(0x10, &mycount); - mytotal += mycount; // 64bit add - - CONS_Debug(DBG_RENDER, "RenderBSPNode: 0x%d %d\n", *((INT32 *)&mytotal + 1), (INT32)mytotal); -#endif -//profile stuff --------------------------------------------------------- + + ps_numsprites.value.i = numvisiblesprites; // PORTAL RENDERING PS_START_TIMING(ps_sw_portaltime); @@ -1880,6 +1860,7 @@ void R_RegisterEngineStuff(void) CV_RegisterVar(&cv_shadowoffs); CV_RegisterVar(&cv_skybox); CV_RegisterVar(&cv_ffloorclip); + CV_RegisterVar(&cv_spriteclip); CV_RegisterVar(&cv_cam_dist); CV_RegisterVar(&cv_cam_still); diff --git a/src/r_main.h b/src/r_main.h index 70adf1bb5..bd794abeb 100644 --- a/src/r_main.h +++ b/src/r_main.h @@ -110,7 +110,7 @@ extern consvar_t cv_homremoval; extern consvar_t cv_chasecam, cv_chasecam2, cv_chasecam3, cv_chasecam4; extern consvar_t cv_flipcam, cv_flipcam2, cv_flipcam3, cv_flipcam4; extern consvar_t cv_shadow, cv_shadowoffs; -extern consvar_t cv_ffloorclip; +extern consvar_t cv_ffloorclip, cv_spriteclip; extern consvar_t cv_translucency; extern consvar_t /*cv_precipdensity,*/ cv_drawdist, /*cv_drawdist_nights,*/ cv_drawdist_precip; extern consvar_t cv_fov; diff --git a/src/r_things.c b/src/r_things.c index 26dad2f0b..80a7db511 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -589,7 +589,8 @@ void R_DelSpriteDefs(UINT16 wadnum) // // GAME FUNCTIONS // -UINT32 visspritecount; +UINT32 visspritecount, numvisiblesprites; + static UINT32 clippedvissprites; static vissprite_t *visspritechunks[MAXVISSPRITES >> VISSPRITECHUNKBITS] = {NULL}; @@ -661,7 +662,7 @@ void R_InitSprites(void) // void R_ClearSprites(void) { - visspritecount = clippedvissprites = 0; + visspritecount = numvisiblesprites = clippedvissprites = 0; } // @@ -935,10 +936,10 @@ static void R_DrawVisSprite(vissprite_t *vis) { vis->scale = FixedMul(vis->scale, this_scale); vis->scalestep = FixedMul(vis->scalestep, this_scale); - vis->xiscale = FixedDiv(vis->xiscale,this_scale); + vis->xiscale = FixedDiv(vis->xiscale, this_scale); vis->isScaled = true; } - dc_texturemid = FixedDiv(dc_texturemid,this_scale); + dc_texturemid = FixedDiv(dc_texturemid, this_scale); } spryscale = vis->scale; @@ -1195,6 +1196,8 @@ static void R_ProjectSprite(mobj_t *thing) INT32 lindex; vissprite_t *vis; + + spritecut_e cut = SC_NONE; angle_t ang = 0; // gcc 4.6 and lower fix fixed_t iscale; @@ -1348,9 +1351,6 @@ static void R_ProjectSprite(mobj_t *thing) I_Assert(lump < max_spritelumps); - if (thing->skin && ((skin_t *)thing->skin)->flags & SF_HIRES) - this_scale = FixedMul(this_scale, ((skin_t *)thing->skin)->highresscale); - spr_width = spritecachedinfo[lump].width; spr_height = spritecachedinfo[lump].height; spr_offset = spritecachedinfo[lump].offset; @@ -1392,6 +1392,14 @@ static void R_ProjectSprite(mobj_t *thing) // calculate edges of the shape spritexscale = interp.spritexscale; spriteyscale = interp.spriteyscale; + + if (thing->skin && ((skin_t *)thing->skin)->flags & SF_HIRES) + { + fixed_t highresscale = ((skin_t *)thing->skin)->highresscale; + spritexscale = FixedMul(spritexscale, highresscale); + spriteyscale = FixedMul(spriteyscale, highresscale); + } + if (spritexscale < 1 || spriteyscale < 1) return; @@ -2085,12 +2093,15 @@ void R_SortVisSprites(void) best = ds; } } + if (best) + { best->next->prev = best->prev; best->prev->next = best->next; best->next = &vsprsortedhead; best->prev = vsprsortedhead.prev; vsprsortedhead.prev->next = best; vsprsortedhead.prev = best; + } } } @@ -2452,7 +2463,45 @@ static void R_DrawPrecipitationSprite(vissprite_t *spr) R_DrawPrecipitationVisSprite(spr); } -// R_ClipSprites +static boolean R_CheckSpriteVisible(vissprite_t *spr, INT32 x1, INT32 x2) +{ + INT16 sz = spr->sz; + INT16 szt = spr->szt; + + fixed_t texturemid, yscale, scalestep = spr->scalestep; + INT32 height; + + if (scalestep) + { + height = spr->patch->height; + yscale = spr->scale; + scalestep = FixedMul(scalestep, spr->spriteyscale); + + if (spr->thingscale != FRACUNIT) + texturemid = FixedDiv(spr->texturemid, max(spr->thingscale, 1)); + else + texturemid = spr->texturemid; + } + + for (INT32 x = x1; x <= x2; x++) + { + if (scalestep) + { + fixed_t top = centeryfrac - FixedMul(texturemid, yscale); + fixed_t bottom = top + (height * yscale); + szt = (INT16)(top >> FRACBITS); + sz = (INT16)(bottom >> FRACBITS); + yscale += scalestep; + } + + if (spr->cliptop[x] < spr->clipbot[x] && sz > spr->cliptop[x] && szt < spr->clipbot[x]) + return true; + } + + return false; +} + +// R_ClipVisSprite // Clips vissprites without drawing, so that portals can work. -Red void R_ClipVisSprite(vissprite_t *spr, INT32 x1, INT32 x2) { @@ -2635,8 +2684,15 @@ void R_ClipVisSprite(vissprite_t *spr, INT32 x1, INT32 x2) spr->clipbot[x] = (INT16)viewheight; if (spr->cliptop[x] == -2) - //Fab : 26-04-98: was -1, now clips against console bottom - spr->cliptop[x] = (INT16)con_clipviewtop; + spr->cliptop[x] = -1; + } + + // Check if it'll be visible + // Not done for floorsprites. + if (cv_spriteclip.value) + { + if (!R_CheckSpriteVisible(spr, x1, x2)) + spr->cut |= SC_NOTVISIBLE; } } @@ -2705,6 +2761,13 @@ void R_ClipSprites(void) for (; clippedvissprites < visspritecount; clippedvissprites++) { vissprite_t *spr = R_GetVisSprite(clippedvissprites); + + if (cv_spriteclip.value + && (spr->szt > vid.height || spr->sz < 0)) + { + spr->cut |= SC_NOTVISIBLE; + continue; + } if (spr->x2 < cx) { @@ -2723,6 +2786,9 @@ void R_ClipSprites(void) } R_ClipVisSprite(spr, spr->x1, spr->x2); + + if ((spr->cut & SC_NOTVISIBLE) == 0) + numvisiblesprites++; } } diff --git a/src/r_things.h b/src/r_things.h index ab4688f91..cb96ffbae 100644 --- a/src/r_things.h +++ b/src/r_things.h @@ -109,7 +109,10 @@ typedef enum SC_NONE = 0, SC_TOP = 1, SC_BOTTOM = 2, - SC_VFLIP = 3 + SC_VFLIP = 3, + SC_NOTVISIBLE = 4, + SC_CUTMASK = SC_TOP|SC_BOTTOM|SC_NOTVISIBLE, + SC_FLAGMASK = ~SC_CUTMASK } spritecut_e; // A vissprite_t is a thing that will be drawn during a refresh, @@ -152,8 +155,6 @@ typedef struct vissprite_s extracolormap_t *extra_colormap; // global colormaps - //fixed_t xscale; - // Precalculated top and bottom screen coords for the sprite. fixed_t thingheight; // The actual height of the thing (for 3D floors) sector_t *sector; // The sector containing the thing. @@ -175,7 +176,7 @@ typedef struct vissprite_s INT32 dispoffset; // copy of info->dispoffset, affects ordering but not drawing } vissprite_t; -extern UINT32 visspritecount; +extern UINT32 visspritecount, numvisiblesprites; void R_ClipSprites(void); void R_ClipVisSprite(vissprite_t *spr, INT32 x1, INT32 x2);