From 782e3172ad2a93adcbdf949b6ae771835b5d5bac Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 9 Jan 2025 17:14:30 +0100 Subject: [PATCH] =?UTF-8?q?do=20some=20l=C3=B6=C3=B6p=20unrolling=20for=20?= =?UTF-8?q?software=20span=20drawers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit actually gained me like 5 fps on 1440p for some reason lel --- src/r_draw8.c | 108 +++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/src/r_draw8.c b/src/r_draw8.c index 30fe49372..8888dbc6a 100644 --- a/src/r_draw8.c +++ b/src/r_draw8.c @@ -804,17 +804,14 @@ void R_DrawSpan_8 (void) fixed_t xstep, ystep; register UINT32 bit; - UINT8 *source; - UINT8 *colormap; - register UINT8 *dest; + const UINT8 *source = ds_source; + const UINT8 *colormap = ds_colormap; + register UINT8 *dest = ylookup[ds_y] + columnofs[ds_x1]; const UINT8 *deststop = screens[0] + vid.rowbytes * vid.height; register size_t count = (ds_x2 - ds_x1 + 1); size_t i; - xposition = ds_xfrac; yposition = ds_yfrac; - xstep = ds_xstep; ystep = ds_ystep; - // SoM: we only need 6 bits for the integer part (0 thru 63) so the rest // can be used for the fraction part. This allows calculation of the memory address in the // texture with two shifts, an OR and one AND. (see below) @@ -822,12 +819,8 @@ void R_DrawSpan_8 (void) // bit per power of two (obviously) // Ok, because I was able to eliminate the variable spot below, this function is now FASTER // than the original span renderer. Whodathunkit? - xposition <<= nflatshiftup; yposition <<= nflatshiftup; - xstep <<= nflatshiftup; ystep <<= nflatshiftup; - - source = ds_source; - colormap = ds_colormap; - dest = ylookup[ds_y] + columnofs[ds_x1]; + xposition = ds_xfrac << nflatshiftup; yposition = ds_yfrac << nflatshiftup; + xstep = ds_xstep << nflatshiftup; ystep = ds_ystep << nflatshiftup; if (dest+8 > deststop) { @@ -839,13 +832,27 @@ void R_DrawSpan_8 (void) // SoM: Why didn't I see this earlier? the spot variable is a waste now because we don't // have the uber complicated math to calculate it now, so that was a memory write we didn't // need! - for (i = 0; i < 8; i++) + for (i = 0; i < 8; i += 8) { bit = (((UINT32)yposition >> nflatyshift) & nflatmask) | ((UINT32)xposition >> nflatxshift); dest[i] = colormap[source[bit]]; + bit = (((UINT32)(yposition + ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + xstep) >> nflatxshift); + dest[i+1] = colormap[source[bit]]; + bit = (((UINT32)(yposition + 2 * ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + 2 * xstep) >> nflatxshift); + dest[i+2] = colormap[source[bit]]; + bit = (((UINT32)(yposition + 3 * ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + 3 * xstep) >> nflatxshift); + dest[i+3] = colormap[source[bit]]; + bit = (((UINT32)(yposition + 4 * ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + 4 * xstep) >> nflatxshift); + dest[i+4] = colormap[source[bit]]; + bit = (((UINT32)(yposition + 5 * ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + 5 * xstep) >> nflatxshift); + dest[i+5] = colormap[source[bit]]; + bit = (((UINT32)(yposition + 6 * ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + 6 * xstep) >> nflatxshift); + dest[i+6] = colormap[source[bit]]; + bit = (((UINT32)(yposition + 7 * ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + 7 * xstep) >> nflatxshift); + dest[i+7] = colormap[source[bit]]; - xposition += xstep; - yposition += ystep; + xposition += 8 * xstep; + yposition += 8 * ystep; } dest += 8; @@ -868,14 +875,13 @@ void R_CalcTiltedLighting(fixed_t start, fixed_t end) { // ZDoom uses a different lighting setup to us, and I couldn't figure out how to adapt their version // of this function. Here's my own. - INT32 left = ds_x1, right = ds_x2; - fixed_t step = (end-start)/(ds_x2-ds_x1+1); INT32 i; + fixed_t step = (end-start)/(ds_x2-ds_x1+1); // I wanna do some optimizing by checking for out-of-range segments on either side to fill in all at once, // but I'm too bad at coding to not crash the game trying to do that. I guess this is fast enough for now... - for (i = left; i <= right; i++) + for (i = ds_x1; i <= ds_x2; i++) { tiltlighting[i] = (start += step) >> FRACBITS; @@ -897,7 +903,6 @@ void R_CalcTiltedLighting(fixed_t start, fixed_t end) */ void R_DrawTiltedSpan_8(void) { - // x1, x2 = ds_x1, ds_x2 int width = ds_x2 - ds_x1; double iz, uz, vz; UINT32 u, v; @@ -933,7 +938,6 @@ void R_DrawTiltedSpan_8(void) dest = ylookup[ds_y] + columnofs[ds_x1]; source = ds_source; - //colormap = ds_colormap; #if 0 // The "perfect" reference version of this routine. Pretty slow. // Use it only to see how things are supposed to look. @@ -961,7 +965,6 @@ void R_DrawTiltedSpan_8(void) izstep = ds_szp->x * SPANSIZE; uzstep = ds_sup->x * SPANSIZE; vzstep = ds_svp->x * SPANSIZE; - //x1 = 0; width++; while (width >= SPANSIZE) @@ -1038,7 +1041,6 @@ void R_DrawTiltedSpan_8(void) */ void R_DrawTiltedTranslucentSpan_8(void) { - // x1, x2 = ds_x1, ds_x2 int width = ds_x2 - ds_x1; double iz, uz, vz; UINT32 u, v; @@ -1074,7 +1076,6 @@ void R_DrawTiltedTranslucentSpan_8(void) dest = ylookup[ds_y] + columnofs[ds_x1]; source = ds_source; - //colormap = ds_colormap; #if 0 // The "perfect" reference version of this routine. Pretty slow. // Use it only to see how things are supposed to look. @@ -1102,7 +1103,6 @@ void R_DrawTiltedTranslucentSpan_8(void) izstep = ds_szp->x * SPANSIZE; uzstep = ds_sup->x * SPANSIZE; vzstep = ds_svp->x * SPANSIZE; - //x1 = 0; width++; while (width >= SPANSIZE) @@ -1180,7 +1180,6 @@ void R_DrawTiltedTranslucentSpan_8(void) */ void R_DrawTiltedTranslucentWaterSpan_8(void) { - // x1, x2 = ds_x1, ds_x2 int width = ds_x2 - ds_x1; double iz, uz, vz; UINT32 u, v; @@ -1218,7 +1217,6 @@ void R_DrawTiltedTranslucentWaterSpan_8(void) dsrc = screens[1] + (ds_y+ds_bgofs)*vid.width + ds_x1; source = ds_source; - //colormap = ds_colormap; #if 0 // The "perfect" reference version of this routine. Pretty slow. // Use it only to see how things are supposed to look. @@ -1246,7 +1244,6 @@ void R_DrawTiltedTranslucentWaterSpan_8(void) izstep = ds_szp->x * SPANSIZE; uzstep = ds_sup->x * SPANSIZE; vzstep = ds_svp->x * SPANSIZE; - //x1 = 0; width++; while (width >= SPANSIZE) @@ -1618,17 +1615,14 @@ void R_DrawTranslucentSpan_8 (void) fixed_t xstep, ystep; register UINT32 bit; - UINT8 *source; - UINT8 *colormap; - register UINT8 *dest; + const UINT8 *source = ds_source; + const UINT8 *colormap = ds_colormap; + register UINT8 *dest = ylookup[ds_y] + columnofs[ds_x1]; const UINT8 *deststop = screens[0] + vid.rowbytes * vid.height; register size_t count = (ds_x2 - ds_x1 + 1); size_t i; - xposition = ds_xfrac; yposition = ds_yfrac; - xstep = ds_xstep; ystep = ds_ystep; - // SoM: we only need 6 bits for the integer part (0 thru 63) so the rest // can be used for the fraction part. This allows calculation of the memory address in the // texture with two shifts, an OR and one AND. (see below) @@ -1636,24 +1630,27 @@ void R_DrawTranslucentSpan_8 (void) // bit per power of two (obviously) // Ok, because I was able to eliminate the variable spot below, this function is now FASTER // than the original span renderer. Whodathunkit? - xposition <<= nflatshiftup; yposition <<= nflatshiftup; - xstep <<= nflatshiftup; ystep <<= nflatshiftup; - - source = ds_source; - colormap = ds_colormap; - dest = ylookup[ds_y] + columnofs[ds_x1]; + xposition = ds_xfrac << nflatshiftup; yposition = ds_yfrac << nflatshiftup; + xstep = ds_xstep << nflatshiftup; ystep = ds_ystep << nflatshiftup; while (count >= 8) { // SoM: Why didn't I see this earlier? the spot variable is a waste now because we don't // have the uber complicated math to calculate it now, so that was a memory write we didn't // need! - for (i = 0; i < 8; i++) + for (i = 0; i < 8; i += 4) { bit = (((UINT32)yposition >> nflatyshift) & nflatmask) | ((UINT32)xposition >> nflatxshift); dest[i] = *(ds_transmap + (colormap[source[bit]] << 8) + dest[i]); - xposition += xstep; - yposition += ystep; + bit = (((UINT32)(yposition + ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + xstep) >> nflatxshift); + dest[i+1] = *(ds_transmap + (colormap[source[bit]] << 8) + dest[i+1]); + bit = (((UINT32)(yposition + 2 * ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + 2 * xstep) >> nflatxshift); + dest[i+2] = *(ds_transmap + (colormap[source[bit]] << 8) + dest[i+2]); + bit = (((UINT32)(yposition + 3 * ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + 3 * xstep) >> nflatxshift); + dest[i+3] = *(ds_transmap + (colormap[source[bit]] << 8) + dest[i+3]); + + xposition += 4 * xstep; + yposition += 4 * ystep; } dest += 8; @@ -1677,10 +1674,10 @@ void R_DrawTranslucentWaterSpan_8(void) UINT32 xstep, ystep; register UINT32 bit; - UINT8 *source; - UINT8 *colormap; - register UINT8 *dest; - UINT8 *dsrc; + const UINT8 *source = ds_source; + const UINT8 *colormap = ds_colormap; + register UINT8 *dest = ylookup[ds_y] + columnofs[ds_x1]; + register UINT8 *dsrc; register size_t count; size_t i; @@ -1695,9 +1692,6 @@ void R_DrawTranslucentWaterSpan_8(void) xposition = ds_xfrac << nflatshiftup; yposition = (ds_yfrac + ds_waterofs) << nflatshiftup; xstep = ds_xstep << nflatshiftup; ystep = ds_ystep << nflatshiftup; - source = ds_source; - colormap = ds_colormap; - dest = ylookup[ds_y] + columnofs[ds_x1]; dsrc = screens[1] + (ds_y+ds_bgofs)*vid.width + ds_x1; count = ds_x2 - ds_x1 + 1; @@ -1706,12 +1700,19 @@ void R_DrawTranslucentWaterSpan_8(void) // SoM: Why didn't I see this earlier? the spot variable is a waste now because we don't // have the uber complicated math to calculate it now, so that was a memory write we didn't // need! - for (i = 0; i < 8; i++) + for (i = 0; i < 8; i += 4) { - bit = ((yposition >> nflatyshift) & nflatmask) | (xposition >> nflatxshift); + bit = (((UINT32)yposition >> nflatyshift) & nflatmask) | ((UINT32)xposition >> nflatxshift); dest[i] = colormap[*(ds_transmap + (source[bit] << 8) + *dsrc++)]; - xposition += xstep; - yposition += ystep; + bit = (((UINT32)(yposition + ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + xstep) >> nflatxshift); + dest[i+1] = colormap[*(ds_transmap + (source[bit] << 8) + *dsrc++)]; + bit = (((UINT32)(yposition + 2 * ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + 2 * xstep) >> nflatxshift); + dest[i+2] = colormap[*(ds_transmap + (source[bit] << 8) + *dsrc++)]; + bit = (((UINT32)(yposition + 3 * ystep) >> nflatyshift) & nflatmask) | ((UINT32)(xposition + 3 * xstep) >> nflatxshift); + dest[i+3] = colormap[*(ds_transmap + (source[bit] << 8) + *dsrc++)]; + + xposition += 4 * xstep; + yposition += 4 * ystep; } dest += 8; @@ -1739,7 +1740,6 @@ void R_DrawFogSpan_8(void) register size_t count; colormap = ds_colormap; - //dest = ylookup[ds_y] + columnofs[ds_x1]; dest = &topleft[ds_y *vid.width + ds_x1]; count = ds_x2 - ds_x1 + 1;