From 2eae9b197ce0150ab08a0640a930e84f72913a3d Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 13 Jun 2025 03:23:25 +0200 Subject: [PATCH] port the span and column drawer templates + tilted translucent splat drawer took the liberty to also carry over our optimizations, this is actually slightly faster than what we had before --- src/k_kart.h | 8 + src/{r_draw.c => r_draw.cpp} | 43 +- src/r_draw.h | 2 + src/r_draw_column.c | 780 --------------------- src/r_draw_column.cpp | 333 +++++++++ src/r_draw_span.c | 1234 ---------------------------------- src/r_draw_span.cpp | 446 ++++++++++++ src/r_plane.cpp | 3 + src/screen.c | 1 + 9 files changed, 814 insertions(+), 2036 deletions(-) rename src/{r_draw.c => r_draw.cpp} (91%) delete mode 100644 src/r_draw_column.c create mode 100644 src/r_draw_column.cpp delete mode 100644 src/r_draw_span.c create mode 100644 src/r_draw_span.cpp diff --git a/src/k_kart.h b/src/k_kart.h index 411bb0512..b10989618 100644 --- a/src/k_kart.h +++ b/src/k_kart.h @@ -6,6 +6,10 @@ #ifndef __K_KART__ #define __K_KART__ +#ifdef __cplusplus +extern "C" { +#endif + #include "doomdef.h" #include "d_player.h" // Need for player_t @@ -132,5 +136,9 @@ INT32 K_getMinimapTrans(void); void K_getLapsDrawinfo(drawinfo_t *out); void K_getMinimapDrawinfo(drawinfo_t *out); +#ifdef __cplusplus +} // extern "C" +#endif + // ========================================================================= #endif // __K_KART__ diff --git a/src/r_draw.c b/src/r_draw.cpp similarity index 91% rename from src/r_draw.c rename to src/r_draw.cpp index 3b3540bc6..9e0f12cb4 100644 --- a/src/r_draw.c +++ b/src/r_draw.cpp @@ -15,6 +15,8 @@ /// not the architecture of the frame buffer. /// The frame buffer is a linear one, and we need only the base address. +#include + #include "doomdef.h" #include "doomstat.h" #include "r_local.h" @@ -115,8 +117,7 @@ void R_InitTranslucencyTables(void) // Load here the transparency lookup tables 'TRANSx0' // NOTE: the TRANSx0 resources MUST BE aligned on 64k for the asm // optimised code (in other words, transtables pointer low word is 0) - transtables = Z_MallocAlign(NUMTRANSTABLES*0x10000, PU_STATIC, - NULL, 16); + transtables = static_cast(Z_MallocAlign(NUMTRANSTABLES*0x10000, PU_STATIC, NULL, 16)); W_ReadLump(W_GetNumForName("TRANS10"), transtables); W_ReadLump(W_GetNumForName("TRANS20"), transtables+0x10000); @@ -177,10 +178,10 @@ static void BlendTab_Subtractive(UINT8 *table, int style, UINT8 blendamt) RGBA_t frontrgba = V_GetColor(fg); RGBA_t result; - result.rgba = ASTBlendPixel(backrgba, frontrgba, style, 0xFF); - result.s.red = max(0, result.s.red - blendamt); - result.s.green = max(0, result.s.green - blendamt); - result.s.blue = max(0, result.s.blue - blendamt); + result.rgba = ASTBlendPixel(backrgba, frontrgba, style, 0xFF); + result.s.red = std::max(0, result.s.red - blendamt); + result.s.green = std::max(0, result.s.green - blendamt); + result.s.blue = std::max(0, result.s.blue - blendamt); table[((bg * 0x100) + fg)] = GetColorLUT(&transtab_lut, result.s.red, result.s.green, result.s.blue); } @@ -233,7 +234,7 @@ static void BlendTab_GenerateMaps(INT32 tab, INT32 style, void (*genfunc)(UINT8 for (; i < num; i++) { const size_t offs = (0x10000 * i); - const UINT16 alpha = min(amtmul * i, 0xFF); + const UINT16 alpha = std::fmin(amtmul * i, 0xFF); genfunc(blendtables[tab] + offs, style, alpha); } } @@ -243,7 +244,7 @@ void R_GenerateBlendTables(void) INT32 i; for (i = 0; i < NUMBLENDMAPS; i++) - blendtables[i] = Z_MallocAlign(BlendTab_Count[i] * 0x10000, PU_STATIC, NULL, 16); + blendtables[i] = static_cast(Z_MallocAlign(BlendTab_Count[i] * 0x10000, PU_STATIC, NULL, 16)); InitColorLUT(&transtab_lut, pLocalPalette, false); @@ -264,8 +265,8 @@ void R_GenerateBlendTables(void) BlendTab_Modulative(blendtables[blendtab_modulate]); } -#define ClipBlendLevel(style, trans) max(min((trans), BlendTab_Count[BlendTab_FromStyle[style]]-1), 0) -#define ClipTransLevel(trans) max(min((trans), NUMTRANSMAPS-2), 0) +#define ClipBlendLevel(style, trans) std::max(std::min((trans), BlendTab_Count[BlendTab_FromStyle[style]]-1), 0) +#define ClipTransLevel(trans) std::max(std::min((trans), NUMTRANSMAPS-2), 0) UINT8 *R_GetTranslucencyTable(INT32 alphalevel) { @@ -352,7 +353,7 @@ static UINT8* RGetTranslationColormap(INT32 skinnum, skincolors_t color, UINT8 f { // Allocate table for skin if necessary if (!tt[skintableindex]) - tt[skintableindex] = Z_Calloc(MAXTRANSLATIONS * sizeof(UINT8**), PU_STATIC, NULL); + tt[skintableindex] = static_cast(Z_Calloc(MAXTRANSLATIONS * sizeof(UINT8**), PU_STATIC, NULL)); // Get colormap ret = tt[skintableindex][color]; @@ -362,7 +363,7 @@ static UINT8* RGetTranslationColormap(INT32 skinnum, skincolors_t color, UINT8 f // Generate the colormap if necessary if (!ret) { - ret = Z_MallocAlign(NUM_PALETTE_ENTRIES, (flags & GTC_CACHE) ? PU_LEVEL : PU_STATIC, NULL, 8); + ret = static_cast(Z_MallocAlign(NUM_PALETTE_ENTRIES, (flags & GTC_CACHE) ? PU_LEVEL : PU_STATIC, NULL, 8)); K_GenerateKartColormap(ret, skinnum, color, local); //R_GenerateTranslationColormap(ret, skinnum, color); // SRB2kart // Cache the colormap if desired @@ -457,17 +458,15 @@ void R_FlushTranslationColormapCache(void) static void R_AllocViewMemory(void) { - negonearray = Z_Realloc(negonearray, sizeof(*negonearray) * viewwidth, PU_STATIC, NULL); - screenheightarray = Z_Realloc(screenheightarray, sizeof(*screenheightarray) * viewwidth, PU_STATIC, NULL); + negonearray = static_cast(Z_Realloc(negonearray, sizeof(*negonearray) * viewwidth, PU_STATIC, NULL)); + screenheightarray = static_cast(Z_Realloc(screenheightarray, sizeof(*screenheightarray) * viewwidth, PU_STATIC, NULL)); - floorclip = Z_Realloc(floorclip, sizeof(*floorclip) * viewwidth, PU_STATIC, NULL); - ceilingclip = Z_Realloc(ceilingclip, sizeof(*ceilingclip) * viewwidth, PU_STATIC, NULL); + floorclip = static_cast(Z_Realloc(floorclip, sizeof(*floorclip) * viewwidth, PU_STATIC, NULL)); + ceilingclip = static_cast(Z_Realloc(ceilingclip, sizeof(*ceilingclip) * viewwidth, PU_STATIC, NULL)); - frontscale = Z_Realloc(frontscale, sizeof(*frontscale) * viewwidth, PU_STATIC, NULL); + frontscale = static_cast(Z_Realloc(frontscale, sizeof(*frontscale) * viewwidth, PU_STATIC, NULL)); - xtoviewangle = Z_Realloc(xtoviewangle, sizeof(*xtoviewangle) * (viewwidth + 1), PU_STATIC, NULL); - - //tiltlighting = Z_Realloc(tiltlighting, sizeof(*tiltlighting) * viewwidth, PU_STATIC, NULL); + xtoviewangle = static_cast(Z_Realloc(xtoviewangle, sizeof(*xtoviewangle) * (viewwidth + 1), PU_STATIC, NULL)); R_AllocSegMemory(); R_AllocClipSegMemory(); @@ -662,5 +661,5 @@ void R_DrawViewBorder(void) // INCLUDE DRAWING CODE HERE // ========================================================================== -#include "r_draw_column.c" -#include "r_draw_span.c" +#include "r_draw_column.cpp" +#include "r_draw_span.cpp" diff --git a/src/r_draw.h b/src/r_draw.h index fc8b344e7..68106480b 100644 --- a/src/r_draw.h +++ b/src/r_draw.h @@ -136,6 +136,7 @@ enum SPANDRAWFUNC_SPLAT, SPANDRAWFUNC_TRANSSPLAT, SPANDRAWFUNC_TILTEDSPLAT, + SPANDRAWFUNC_TILTEDTRANSSPLAT, SPANDRAWFUNC_WATER, SPANDRAWFUNC_TILTEDWATER, @@ -264,6 +265,7 @@ void R_DrawFogSpan_Tilted(drawspandata_t* ds); void R_DrawSplat_Tilted(drawspandata_t* ds); void R_DrawSplat(drawspandata_t* ds); void R_DrawTranslucentSplat(drawspandata_t* ds); +void R_DrawTranslucentSplat_Tilted(drawspandata_t* ds); #ifdef __cplusplus } // extern "C" diff --git a/src/r_draw_column.c b/src/r_draw_column.c deleted file mode 100644 index dc825e5d6..000000000 --- a/src/r_draw_column.c +++ /dev/null @@ -1,780 +0,0 @@ -// SONIC ROBO BLAST 2 -//----------------------------------------------------------------------------- -// Copyright (C) 1998-2000 by DooM Legacy Team. -// Copyright (C) 1999-2018 by Sonic Team Junior. -// -// This program is free software distributed under the -// terms of the GNU General Public License, version 2. -// See the 'LICENSE' file for more details. -//----------------------------------------------------------------------------- -/// \file r_draw_column.c -/// \brief column drawer functions -/// \note no includes because this is included as part of r_draw.c - -// ========================================================================== -// COLUMNS -// ========================================================================== - -// A column is a vertical slice/span of a wall texture that uses -// a has a constant z depth from top to bottom. -// - -/** \brief The R_DrawColumn function - Experiment to make software go faster. Taken from the Boom source -*/ -void R_DrawColumn(drawcolumndata_t* dc) -{ - INT32 count; - register UINT8 *restrict dest; - intptr_t frac; - - count = dc->yh - dc->yl; - - if (count < 0) // Zero length, column does not exceed a pixel. - { - return; - } - - if ((unsigned)dc->x >= (unsigned)vid.width || dc->yl < 0 || dc->yh >= vid.height) - { - return; - } - - // Framebuffer destination address. - dest = R_Address(dc->x, dc->yl); - - count++; - - // Determine scaling, which is the only mapping to be done. - const intptr_t fracstep = dc->iscale; - frac = (dc->texturemid + FixedMul((dc->yl << FRACBITS) - centeryfrac, fracstep)); - - // Inner loop that does the actual texture mapping, e.g. a DDA-like scaling. - // This is as fast as it gets. - const UINT8 *restrict source = dc->source; - const lighttable_t *restrict colormap = dc->colormap; - intptr_t heightmask = dc->sourcelength-1; - - static const INT32 npow2min = -1; - const INT32 npow2max = dc->sourcelength; - - register const INT32 stride = vid.width; - - if (heightmask == -1) - { - // texture has no height, so just go - while (--count > 0) - { - *dest = colormap[source[frac>>FRACBITS]]; - dest += stride; - frac += fracstep; - } - } - else if (dc->sourcelength & heightmask) // not a power of 2 -- killough - { - heightmask = dc->texheight << FRACBITS; - - if (frac < 0) - { - while ((frac += heightmask) < 0) - { - ; - } - } - else - { - while (frac >= heightmask) - { - frac -= heightmask; - } - } - - do - { - // Re-map color indices from wall texture column - // using a lighting/special effects LUT. - // heightmask is the Tutti-Frutti fix - - // -1 is the lower clamp bound because column posts have a "safe" byte before the real data - // and a few bytes after as well - *dest = colormap[source[CLAMP(frac >> FRACBITS, npow2min, npow2max)]]; - - dest += stride; - -#if __SIZEOF_POINTER__ < 8 // 64-bit systems have large enough numbers for this to be a non-issue - // Avoid overflow. - if (fracstep > 0x7FFFFFFF - frac) - { - frac += fracstep - heightmask; - } - else -#endif - { - frac += fracstep; - } - - while (frac >= heightmask) - { - frac -= heightmask; - } - } while (--count); - } - else - { - while ((count -= 2) >= 0) // texture height is a power of 2 - { - *dest = colormap[source[(frac>>FRACBITS) & heightmask]]; - - dest += stride; - frac += fracstep; - - *dest = colormap[source[(frac>>FRACBITS) & heightmask]]; - - dest += stride; - frac += fracstep; - } - - if (count & 1) - { - *dest = colormap[source[(frac>>FRACBITS) & heightmask]]; - } - } -} - -void R_Draw2sMultiPatchColumn(drawcolumndata_t* dc) -{ - INT32 count; - register UINT8 *restrict dest; - intptr_t frac; - - count = dc->yh - dc->yl; - - if (count < 0) // Zero length, column does not exceed a pixel. - { - return; - } - - if ((unsigned)dc->x >= (unsigned)vid.width || dc->yl < 0 || dc->yh >= vid.height) - { - return; - } - - // Framebuffer destination address. - dest = R_Address(dc->x, dc->yl); - - count++; - - // Determine scaling, which is the only mapping to be done. - const intptr_t fracstep = dc->iscale; - frac = (dc->texturemid + FixedMul((dc->yl << FRACBITS) - centeryfrac, fracstep)); - - // Inner loop that does the actual texture mapping, e.g. a DDA-like scaling. - // This is as fast as it gets. - const UINT8 *restrict source = dc->source; - const lighttable_t *restrict colormap = dc->colormap; - intptr_t heightmask = dc->sourcelength-1; - UINT8 val; - - static const INT32 npow2min = -1; - const INT32 npow2max = dc->sourcelength; - - register const INT32 stride = vid.width; - - if (heightmask == -1) - { - // texture has no height, so just go - while (--count > 0) - { - *dest = colormap[source[frac>>FRACBITS]]; - dest += stride; - frac += fracstep; - } - } - else if (dc->sourcelength & heightmask) // not a power of 2 -- killough - { - heightmask = dc->texheight << FRACBITS; - - if (frac < 0) - { - while ((frac += heightmask) < 0) - { - ; - } - } - else - { - while (frac >= heightmask) - { - frac -= heightmask; - } - } - - do - { - // Re-map color indices from wall texture column - // using a lighting/special effects LUT. - // heightmask is the Tutti-Frutti fix - - // -1 is the lower clamp bound because column posts have a "safe" byte before the real data - // and a few bytes after as well - val = source[CLAMP(frac >> FRACBITS, npow2min, npow2max)]; - - if (val != TRANSPARENTPIXEL) - { - *dest = colormap[val]; - } - - dest += stride; - - // Avoid overflow. -#if __SIZEOF_POINTER__ < 8 - if (fracstep > 0x7FFFFFFF - frac) - { - frac += fracstep - heightmask; - } - else -#endif - { - frac += fracstep; - } - - while (frac >= heightmask) - { - frac -= heightmask; - } - } while (--count); - } - else - { - while ((count -= 2) >= 0) // texture height is a power of 2 - { - val = source[(frac>>FRACBITS) & heightmask]; - - if (val != TRANSPARENTPIXEL) - { - *dest = colormap[val]; - } - - dest += stride; - frac += fracstep; - - val = source[(frac>>FRACBITS) & heightmask]; - if (val != TRANSPARENTPIXEL) - { - *dest = colormap[val]; - } - - dest += stride; - frac += fracstep; - } - - if (count & 1) - { - val = source[CLAMP(frac >> FRACBITS, -1, dc->sourcelength)]; - if (val != TRANSPARENTPIXEL) - { - *dest = colormap[val]; - } - } - } -} - -void R_Draw2sMultiPatchTranslucentColumn(drawcolumndata_t* dc) -{ - INT32 count; - register UINT8 *restrict dest; - intptr_t frac; - - count = dc->yh - dc->yl; - - if (count < 0) // Zero length, column does not exceed a pixel. - { - return; - } - - if ((unsigned)dc->x >= (unsigned)vid.width || dc->yl < 0 || dc->yh >= vid.height) - { - return; - } - - // Framebuffer destination address. - dest = R_Address(dc->x, dc->yl); - - count++; - - // Determine scaling, which is the only mapping to be done. - const intptr_t fracstep = dc->iscale; - frac = (dc->texturemid + FixedMul((dc->yl << FRACBITS) - centeryfrac, fracstep)); - - // Inner loop that does the actual texture mapping, e.g. a DDA-like scaling. - // This is as fast as it gets. - const UINT8 *restrict source = dc->source; - const UINT8 *transmap = dc->transmap; - const lighttable_t *restrict colormap = dc->colormap; - intptr_t heightmask = dc->sourcelength-1; - register UINT8 val; - - static const INT32 npow2min = -1; - const INT32 npow2max = dc->sourcelength; - - register const INT32 stride = vid.width; - - if (heightmask == -1) - { - // texture has no height, so just go - while (--count > 0) - { - *dest = colormap[source[frac>>FRACBITS]]; - dest += stride; - frac += fracstep; - } - } - else if (dc->sourcelength & heightmask) // not a power of 2 -- killough - { - heightmask = dc->texheight << FRACBITS; - - if (frac < 0) - { - while ((frac += heightmask) < 0) - { - ; - } - } - else - { - while (frac >= heightmask) - { - frac -= heightmask; - } - } - - do - { - // Re-map color indices from wall texture column - // using a lighting/special effects LUT. - // heightmask is the Tutti-Frutti fix - - // -1 is the lower clamp bound because column posts have a "safe" byte before the real data - // and a few bytes after as well - val = source[CLAMP(frac >> FRACBITS, npow2min, npow2max)]; - - if (val != TRANSPARENTPIXEL) - { - *dest = *(transmap + (colormap[val]<<8) + (*dest)); - } - - dest += stride; - - // Avoid overflow. -#if __SIZEOF_POINTER__ < 8 - if (fracstep > 0x7FFFFFFF - frac) - { - frac += fracstep - heightmask; - } - else -#endif - { - frac += fracstep; - } - - while (frac >= heightmask) - { - frac -= heightmask; - } - } while (--count); - } - else - { - while ((count -= 2) >= 0) // texture height is a power of 2 - { - val = source[(frac>>FRACBITS) & heightmask]; - if (val != TRANSPARENTPIXEL) - { - *dest = *(transmap + (colormap[val]<<8) + (*dest)); - } - - dest += stride; - frac += fracstep; - - val = source[(frac>>FRACBITS) & heightmask]; - if (val != TRANSPARENTPIXEL) - { - *dest = *(transmap + (colormap[val]<<8) + (*dest)); - } - - dest += stride; - frac += fracstep; - } - - if (count & 1) - { - val = source[(frac>>FRACBITS) & heightmask]; - if (val != TRANSPARENTPIXEL) - { - *dest = *(transmap + (colormap[val]<<8) + (*dest)); - } - } - } -} - -/** \brief The R_DrawTranslucentColumn function - I've made an asm routine for the transparency, because it slows down - a lot in 640x480 with big sprites (bfg on all screen, or transparent - walls on fullscreen) -*/ -void R_DrawTranslucentColumn(drawcolumndata_t* dc) -{ - register INT32 count; - register UINT8 *restrict dest; - intptr_t frac; - - count = dc->yh - dc->yl + 1; - - if (count <= 0) // Zero length, column does not exceed a pixel. - { - return; - } - - if ((unsigned)dc->x >= (unsigned)vid.width || dc->yl < 0 || dc->yh >= vid.height) - { - return; - } - - // Framebuffer destination address. - dest = R_Address(dc->x, dc->yl); - - // Looks familiar. - const intptr_t fracstep = dc->iscale; - frac = (dc->texturemid + FixedMul((dc->yl << FRACBITS) - centeryfrac, fracstep)); - - // Inner loop that does the actual texture mapping, e.g. a DDA-like scaling. - // This is as fast as it gets. - const UINT8 *restrict source = dc->source; - const UINT8 *transmap = dc->transmap; - const lighttable_t *restrict colormap = dc->colormap; - intptr_t heightmask = dc->sourcelength-1; - - static const INT32 npow2min = -1; - const INT32 npow2max = dc->sourcelength; - - register const INT32 stride = vid.width; - - if (dc->sourcelength & heightmask) - { - heightmask = dc->texheight << FRACBITS; - - if (frac < 0) - { - while ((frac += heightmask) < 0) - { - ; - } - } - else - { - while (frac >= heightmask) - { - frac -= heightmask; - } - } - - do - { - // Re-map color indices from wall texture column - // using a lighting/special effects LUT. - // heightmask is the Tutti-Frutti fix - - // -1 is the lower clamp bound because column posts have a "safe" byte before the real data - // and a few bytes after as well - *dest = *(transmap + (colormap[source[CLAMP(frac >> FRACBITS, npow2min, npow2max)]]<<8) + (*dest)); - - dest += stride; - - if ((frac += fracstep) >= heightmask) - { - frac -= heightmask; - } - } - while (--count); - } - else - { - while ((count -= 2) >= 0) // texture height is a power of 2 - { - *dest = *(transmap + (colormap[source[(frac>>FRACBITS)&heightmask]]<<8) + (*dest)); - dest += stride; - frac += fracstep; - - *dest = *(transmap + (colormap[source[(frac>>FRACBITS)&heightmask]]<<8) + (*dest)); - dest += stride; - frac += fracstep; - } - - if (count & 1) - { - *dest = *(transmap + (colormap[source[(frac>>FRACBITS)&heightmask]]<<8) + (*dest)); - } - } -} - -/** \brief The R_DrawTranslatedTranslucentColumn function - Spiffy function. Not only does it colormap a sprite, but does translucency as well. - Uber-kudos to Cyan Helkaraxe -*/ -void R_DrawTranslatedTranslucentColumn(drawcolumndata_t* dc) -{ - register INT32 count; - register UINT8 *restrict dest; - intptr_t frac; - - count = dc->yh - dc->yl + 1; - - if (count <= 0) // Zero length, column does not exceed a pixel. - { - return; - } - - if ((unsigned)dc->x >= (unsigned)vid.width || dc->yl < 0 || dc->yh >= vid.height) - { - return; - } - - // Framebuffer destination address. - dest = R_Address(dc->x, dc->yl); - - // Looks familiar. - const intptr_t fracstep = dc->iscale; - frac = (dc->texturemid + FixedMul((dc->yl << FRACBITS) - centeryfrac, fracstep)); - - // Inner loop that does the actual texture mapping, e.g. a DDA-like scaling. - // This is as fast as it gets. - intptr_t heightmask = dc->sourcelength-1; - - static const INT32 npow2min = -1; - const INT32 npow2max = dc->sourcelength; - - register const INT32 stride = vid.width; - - if (dc->sourcelength & heightmask) - { - heightmask = dc->texheight << FRACBITS; - - if (frac < 0) - { - while ((frac += heightmask) < 0) - { - ; - } - } - else - { - while (frac >= heightmask) - { - frac -= heightmask; - } - } - - do - { - // Re-map color indices from wall texture column - // using a lighting/special effects LUT. - // heightmask is the Tutti-Frutti fix - - // -1 is the lower clamp bound because column posts have a "safe" byte before the real data - // and a few bytes after as well - *dest = *(dc->transmap + (dc->colormap[dc->translation[dc->source[CLAMP(frac >> FRACBITS, npow2min, npow2max)]]]<<8) + (*dest)); - - dest += stride; - - if ((frac += fracstep) >= heightmask) - { - frac -= heightmask; - } - } - while (--count); - } - else - { - while ((count -= 2) >= 0) // texture height is a power of 2 - { - *dest = *(dc->transmap + (dc->colormap[dc->translation[dc->source[(frac>>FRACBITS)&heightmask]]]<<8) + (*dest)); - dest += stride; - frac += fracstep; - - *dest = *(dc->transmap + (dc->colormap[dc->translation[dc->source[(frac>>FRACBITS)&heightmask]]]<<8) + (*dest)); - dest += stride; - frac += fracstep; - } - - if (count & 1) - { - *dest = *(dc->transmap + (dc->colormap[dc->translation[dc->source[(frac>>FRACBITS)&heightmask]]]<<8) + (*dest)); - } - } -} - -/** \brief The R_DrawTranslatedColumn function - Draw columns up to 128 high but remap the green ramp to other colors - - \warning STILL NOT IN ASM, TO DO.. -*/ -void R_DrawTranslatedColumn(drawcolumndata_t* dc) -{ - register INT32 count; - register UINT8 *dest; - register fixed_t frac; - - count = dc->yh - dc->yl; - - if (count < 0) - { - return; - } - - if ((unsigned)dc->x >= (unsigned)vid.width || dc->yl < 0 || dc->yh >= vid.height) - { - return; - } - - // Framebuffer destination address. - dest = R_Address(dc->x, dc->yl); - - // Looks familiar. - const fixed_t fracstep = dc->iscale; - frac = (dc->texturemid + FixedMul((dc->yl << FRACBITS) - centeryfrac, fracstep)); - - register const INT32 stride = vid.width; - - // Here we do an additional index re-mapping. - do - { - // Translation tables are used - // to map certain colorramps to other ones, - // used with PLAY sprites. - // Thus the "green" ramp of the player 0 sprite - // is mapped to gray, red, black/indigo. - *dest = dc->colormap[dc->translation[dc->source[frac>>FRACBITS]]]; - - dest += stride; - - frac += fracstep; - } while (count--); -} - -/** \brief The R_DrawFogColumn function - Fog wall. -*/ -void R_DrawFogColumn(drawcolumndata_t* dc) -{ - register INT32 count; - register UINT8 *dest; - - count = dc->yh - dc->yl; - - // Zero length, column does not exceed a pixel. - if (count < 0) - { - return; - } - - if ((unsigned)dc->x >= (unsigned)vid.width || dc->yl < 0 || dc->yh >= vid.height) - { - return; - } - - // Framebuffer destination address. - dest = R_Address(dc->x, dc->yl); - - register const INT32 stride = vid.width; - - // Determine scaling, which is the only mapping to be done. - do - { - // Simple. Apply the colormap to what's already on the screen. - *dest = dc->colormap[*dest]; - dest += stride; - } while (count--); -} - -/** \brief The R_DrawShadeColumn function - This is for 3D floors that cast shadows on walls. - - This function just cuts the column up into sections and calls R_DrawColumn -*/ -void R_DrawColumnShadowed(drawcolumndata_t* dc) -{ - register INT32 count; - INT32 realyh, i, height, bheight = 0, solid = 0; - - count = dc->yh - dc->yl; - - // Zero length, column does not exceed a pixel. - if (count < 0) - { - return; - } - - if ((unsigned)dc->x >= (unsigned)vid.width || dc->yl < 0 || dc->yh >= vid.height) - { - return; - } - - realyh = dc->yh; - - // This runs through the lightlist from top to bottom and cuts up the column accordingly. - for (i = 0; i < dc->numlights; i++) - { - // If the height of the light is above the column, get the colormap - // anyway because the lighting of the top should be affected. - solid = dc->lightlist[i].flags & FF_CUTSOLIDS; - - height = dc->lightlist[i].height >> LIGHTSCALESHIFT; - - if (solid) - { - bheight = dc->lightlist[i].botheight >> LIGHTSCALESHIFT; - if (bheight < height) - { - // confounded slopes sometimes allow partial invertedness, - // even including cases where the top and bottom heights - // should actually be the same! - // swap the height values as a workaround for this quirk - INT32 temp = height; - height = bheight; - bheight = temp; - } - } - - if (height <= dc->yl) - { - dc->colormap = dc->lightlist[i].rcolormap; - if (encoremap) - dc->colormap += COLORMAP_REMAPOFFSET; - if (solid && dc->yl < bheight) - dc->yl = bheight; - continue; - } - - // Found a break in the column! - dc->yh = height; - - if (dc->yh > realyh) - dc->yh = realyh; - - (colfuncs[BASEDRAWFUNC])(dc); // R_DrawColumn_8 for the appropriate architecture - - if (solid) - dc->yl = bheight; - else - dc->yl = dc->yh + 1; - - dc->colormap = dc->lightlist[i].rcolormap; - if (encoremap) - dc->colormap += COLORMAP_REMAPOFFSET; - } - - dc->yh = realyh; - - if (dc->yl <= realyh) - (colfuncs[BASEDRAWFUNC])(dc); // R_DrawWallColumn_8 for the appropriate architecture -} diff --git a/src/r_draw_column.cpp b/src/r_draw_column.cpp new file mode 100644 index 000000000..b546c66ea --- /dev/null +++ b/src/r_draw_column.cpp @@ -0,0 +1,333 @@ +// SONIC ROBO BLAST 2 KART +//----------------------------------------------------------------------------- +// Copyright (C) 2025 by Kart Krew. +// Copyright (C) 2020 by Sonic Team Junior. +// Copyright (C) 2000 by DooM Legacy Team. +// Copyright (C) 1996 by id Software, Inc. +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file r_draw_column.cpp +/// \brief column drawer functions +/// \note no includes because this is included as part of r_draw.cpp + +// ========================================================================== +// COLUMNS +// ========================================================================== + +// A column is a vertical slice/span of a wall texture that uses +// a has a constant z depth from top to bottom. +// + +enum DrawColumnType +{ + DC_BASIC = 0x0000, + DC_COLORMAP = 0x0001, + DC_TRANSMAP = 0x0002, + DC_HOLES = 0x0004, + DC_LIGHTLIST = 0x0008, +}; + +template +static constexpr UINT8 R_GetColumnTranslated(drawcolumndata_t* dc, UINT8 col, const UINT8 *colormap) +{ + if constexpr (Type & DrawColumnType::DC_COLORMAP) + { + col = dc->translation[col]; + } + + return colormap[col]; +} + +template +static constexpr UINT8 R_GetColumnTranslucent(drawcolumndata_t* dc, UINT8 *dest, UINT8 col, const UINT8 *colormap) +{ + col = R_GetColumnTranslated(dc, col, colormap); + + if constexpr (Type & DrawColumnType::DC_TRANSMAP) + { + return *(dc->transmap + (col << 8) + (*dest)); + } + else + { + return col; + } +} + +template +static constexpr UINT8 R_DrawColumnPixel(drawcolumndata_t* dc, UINT8 *dest, UINT32 bit, const UINT8 *source, const UINT8 *colormap) +{ + UINT8 col = source[bit]; + + if constexpr (Type & DrawColumnType::DC_HOLES) + { + if (col == TRANSPARENTPIXEL) + { + return *dest; + } + } + + return R_GetColumnTranslucent(dc, dest, col, colormap); +} + +/** \brief The R_DrawColumn function + Experiment to make software go faster. Taken from the Boom source +*/ +template +static void R_DrawColumnTemplate(drawcolumndata_t *dc) +{ + INT32 count; + + count = dc->yh - dc->yl; + + if (count < 0) // Zero length, column does not exceed a pixel. + { + return; + } + + if ((unsigned)dc->x >= (unsigned)vid.width || dc->yl < 0 || dc->yh >= vid.height) + { + return; + } + + if constexpr (Type & DrawColumnType::DC_LIGHTLIST) + { + constexpr DrawColumnType NewType = static_cast(Type & ~DC_LIGHTLIST); + INT32 i, height, bheight = 0, solid = 0; + drawcolumndata_t dc_copy = *dc; + + const INT32 realyh = dc_copy.yh; + + // This runs through the lightlist from top to bottom and cuts up the column accordingly. + for (i = 0; i < dc_copy.numlights; i++) + { + // If the height of the light is above the column, get the colormap + // anyway because the lighting of the top should be affected. + solid = dc_copy.lightlist[i].flags & FF_CUTSOLIDS; + height = dc_copy.lightlist[i].height >> LIGHTSCALESHIFT; + + if (solid) + { + bheight = dc_copy.lightlist[i].botheight >> LIGHTSCALESHIFT; + + if (bheight < height) + { + // confounded slopes sometimes allow partial invertedness, + // even including cases where the top and bottom heights + // should actually be the same! + // swap the height values as a workaround for this quirk + INT32 temp = height; + height = bheight; + bheight = temp; + } + } + + if (height <= dc_copy.yl) + { + dc_copy.colormap = dc_copy.lightlist[i].rcolormap; + + if (encoremap) + { + dc_copy.colormap += COLORMAP_REMAPOFFSET; + } + + if (solid && dc_copy.yl < bheight) + { + dc_copy.yl = bheight; + } + + continue; + } + + // Found a break in the column! + dc_copy.yh = height; + + if (dc_copy.yh > realyh) + { + dc_copy.yh = realyh; + } + + R_DrawColumnTemplate(&dc_copy); + if (solid) + { + dc_copy.yl = bheight; + } + else + { + dc_copy.yl = dc_copy.yh + 1; + } + + dc_copy.colormap = dc_copy.lightlist[i].rcolormap; + + if (encoremap) + { + dc_copy.colormap += COLORMAP_REMAPOFFSET; + } + } + + dc_copy.yh = realyh; + + if (dc_copy.yl <= realyh) + { + R_DrawColumnTemplate(&dc_copy); + } + } + else + { + // Inner loop that does the actual texture mapping, e.g. a DDA-like scaling. + // This is as fast as it gets. + fixed_t frac; + const intptr_t fracstep = dc->iscale; + intptr_t heightmask = dc->sourcelength-1; + static const INT32 npow2min = -1; + const INT32 npow2max = dc->sourcelength; + const INT32 stride = vid.width; + + const UINT8 *source = dc->source; + const lighttable_t *colormap = dc->colormap; + + // Framebuffer destination address. + UINT8 *dest = R_Address(dc->x, dc->yl); + + count++; + + // Determine scaling, which is the only mapping to be done. + frac = (dc->texturemid + FixedMul((dc->yl << FRACBITS) - centeryfrac, fracstep)); + + if (heightmask == -1) + { + if (frac < 0) + // adjust in case we underread + frac += FRACUNIT; + + // texture has no height, so just go + while (--count > 0) + { + *dest = R_DrawColumnPixel(dc, dest, frac>>FRACBITS, source, colormap); + dest += stride; + frac += fracstep; + } + } + else if (dc->sourcelength & heightmask) // not a power of 2 -- killough + { + heightmask = dc->texheight << FRACBITS; + + if (frac < 0) + { + while ((frac += heightmask) < 0) + { + ; + } + } + else + { + while (frac >= heightmask) + { + frac -= heightmask; + } + } + + do + { + // Re-map color indices from wall texture column + // using a lighting/special effects LUT. + // heightmask is the Tutti-Frutti fix + + // -1 is the lower clamp bound because column posts have a "safe" byte before the real data + // and a few bytes after as well + *dest = R_DrawColumnPixel(dc, dest, std::clamp((frac >> FRACBITS), npow2min, npow2max), source, colormap); + + dest += stride; + +#if __SIZEOF_POINTER__ < 8 // 64-bit systems have large enough numbers for this to be a non-issue + // Avoid overflow. + if (fracstep > 0x7FFFFFFF - frac) + { + frac += fracstep - heightmask; + } + else +#endif + { + frac += fracstep; + } + + while (frac >= heightmask) + { + frac -= heightmask; + } + } + while (--count); + } + else + { + while ((count -= 2) >= 0) // texture height is a power of 2 + { + *dest = R_DrawColumnPixel(dc, dest, (frac>>FRACBITS) & heightmask, source, colormap); + dest += stride; + frac += fracstep; + + *dest = R_DrawColumnPixel(dc, dest, (frac>>FRACBITS) & heightmask, source, colormap); + dest += stride; + frac += fracstep; + } + + if (count & 1) + { + *dest = R_DrawColumnPixel(dc, dest, (frac>>FRACBITS) & heightmask, source, colormap); + } + } + } +} + +#define DEFINE_COLUMN_FUNC(name, flags) \ + void name(drawcolumndata_t *dc) \ + { \ + constexpr DrawColumnType opt = static_cast(flags); \ + R_DrawColumnTemplate(dc); \ + } + +DEFINE_COLUMN_FUNC(R_DrawColumn, DC_BASIC) +DEFINE_COLUMN_FUNC(R_DrawTranslucentColumn, DC_TRANSMAP) +DEFINE_COLUMN_FUNC(R_DrawTranslatedColumn, DC_COLORMAP) +DEFINE_COLUMN_FUNC(R_DrawColumnShadowed, DC_LIGHTLIST) +DEFINE_COLUMN_FUNC(R_DrawTranslatedTranslucentColumn, DC_COLORMAP|DC_TRANSMAP) +DEFINE_COLUMN_FUNC(R_Draw2sMultiPatchColumn, DC_HOLES) +DEFINE_COLUMN_FUNC(R_Draw2sMultiPatchTranslucentColumn, DC_HOLES|DC_TRANSMAP) + +/** \brief The R_DrawFogColumn function + Fog wall. +*/ +void R_DrawFogColumn(drawcolumndata_t* dc) +{ + INT32 count; + UINT8 *dest; + + count = dc->yh - dc->yl; + + // Zero length, column does not exceed a pixel. + if (count < 0) + { + return; + } + + if ((unsigned)dc->x >= (unsigned)vid.width || dc->yl < 0 || dc->yh >= vid.height) + { + return; + } + + // Framebuffer destination address. + dest = R_Address(dc->x, dc->yl); + + const INT32 stride = vid.width; + const lighttable_t *colormap = dc->colormap; + + // Determine scaling, which is the only mapping to be done. + do + { + // Simple. Apply the colormap to what's already on the screen. + *dest = colormap[*dest]; + dest += stride; + } while (count--); +} diff --git a/src/r_draw_span.c b/src/r_draw_span.c deleted file mode 100644 index d7a9aaa68..000000000 --- a/src/r_draw_span.c +++ /dev/null @@ -1,1234 +0,0 @@ -// SONIC ROBO BLAST 2 -//----------------------------------------------------------------------------- -// Copyright (C) 1998-2000 by DooM Legacy Team. -// Copyright (C) 1999-2018 by Sonic Team Junior. -// -// This program is free software distributed under the -// terms of the GNU General Public License, version 2. -// See the 'LICENSE' file for more details. -//----------------------------------------------------------------------------- -/// \file r_draw_span.c -/// \brief span drawer functions -/// \note no includes because this is included as part of r_draw.c - -// ========================================================================== -// SPANS -// ========================================================================== - -#ifdef HAVE_THREADS -#ifdef _WIN32 -#include -#define local_for_thread static __thread -#else -#include -#define local_for_thread thread_local static -#endif -#else -#define local_for_thread static -#endif - -#define SPANSIZE 16 -#define INVSPAN 0.0625f - -// 4194303 = (2048x2048)-1 (2048x2048 is maximum flat size) -#define MAXFLATBYTES 4194303 - -/** \brief The R_DrawSpan function - Draws the actual span. -*/ -void R_DrawSpan(drawspandata_t* ds) -{ - UINT32 xposition; - UINT32 yposition; - UINT32 xstep, ystep; - register UINT32 bit; - - UINT8 *restrict source = ds->source; - UINT8 *restrict colormap = ds->colormap; - UINT8 *restrict dest = R_Address(ds->x1, ds->y); - const UINT8 *restrict deststop = vid.screens[0] + vid.rowbytes * vid.height; - - register intptr_t count = (ds->x2 - ds->x1 + 1); - - 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) - // for texture sizes > 64 the amount of precision we can allow will decrease, but only by one - // 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 <<= ds->nflatshiftup; yposition <<= ds->nflatshiftup; - xstep <<= ds->nflatshiftup; ystep <<= ds->nflatshiftup; - - if (dest+8 > deststop) - { - return; - } - - 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! - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[0] = colormap[source[bit]]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[1] = colormap[source[bit]]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[2] = colormap[source[bit]]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[3] = colormap[source[bit]]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[4] = colormap[source[bit]]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[5] = colormap[source[bit]]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[6] = colormap[source[bit]]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[7] = colormap[source[bit]]; - xposition += xstep; - yposition += ystep; - - dest += 8; - count -= 8; - } - - while (count-- && dest <= deststop) - { - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - *dest++ = colormap[source[bit]]; - xposition += xstep; - yposition += ystep; - } -} - -/** \brief The R_DrawTranslucentSpan function - Draws the actual span with translucent. -*/ -void R_DrawTranslucentSpan(drawspandata_t* ds) -{ - UINT32 xposition; - UINT32 yposition; - UINT32 xstep, ystep; - register UINT32 bit; - - UINT8 *restrict source = ds->source; - UINT8 *restrict colormap = ds->colormap; - UINT8 *restrict dest = R_Address(ds->x1, ds->y); - const UINT8 *restrict deststop = vid.screens[0] + vid.rowbytes * vid.height; - - register intptr_t count = (ds->x2 - ds->x1 + 1); - - register const UINT8 *tranmap = ds->transmap; - - 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) - // for texture sizes > 64 the amount of precision we can allow will decrease, but only by one - // 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 <<= ds->nflatshiftup; yposition <<= ds->nflatshiftup; - xstep <<= ds->nflatshiftup; ystep <<= ds->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! - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[0] = *(tranmap + (colormap[source[bit]] << 8) + dest[0]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[1] = *(tranmap + (colormap[source[bit]] << 8) + dest[1]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[2] = *(tranmap + (colormap[source[bit]] << 8) + dest[2]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[3] = *(tranmap + (colormap[source[bit]] << 8) + dest[3]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[4] = *(tranmap + (colormap[source[bit]] << 8) + dest[4]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[5] = *(tranmap + (colormap[source[bit]] << 8) + dest[5]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[6] = *(tranmap + (colormap[source[bit]] << 8) + dest[6]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[7] = *(tranmap + (colormap[source[bit]] << 8) + dest[7]); - xposition += xstep; - yposition += ystep; - - dest += 8; - count -= 8; - } - while (count-- && dest <= deststop) - { - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - *dest = *(tranmap + (colormap[source[bit]] << 8) + *dest); - dest++; - xposition += xstep; - yposition += ystep; - } -} - -void R_DrawTranslucentWaterSpan(drawspandata_t* ds) -{ - UINT32 xposition; - UINT32 yposition; - UINT32 xstep, ystep; - register UINT32 bit; - - UINT8 *restrict source = ds->source; - UINT8 *restrict colormap = ds->colormap; - UINT8 *restrict dest = R_Address(ds->x1, ds->y); - const UINT8 *restrict dsrc = vid.screens[1] + (ds->y+ds->bgofs)*vid.width + ds->x1; - - register intptr_t count = (ds->x2 - ds->x1 + 1); - - register const UINT8 *tranmap = ds->transmap; - - xposition = ds->xfrac; yposition = (ds->yfrac + ds->waterofs); - 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) - // for texture sizes > 64 the amount of precision we can allow will decrease, but only by one - // 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 <<= ds->nflatshiftup; yposition <<= ds->nflatshiftup; - xstep <<= ds->nflatshiftup; ystep <<= ds->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! - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[0] = colormap[*(tranmap + (source[bit] << 8) + *dsrc++)]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[1] = colormap[*(tranmap + (source[bit] << 8) + *dsrc++)]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[2] = colormap[*(tranmap + (source[bit] << 8) + *dsrc++)]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[3] = colormap[*(tranmap + (source[bit] << 8) + *dsrc++)]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[4] = colormap[*(tranmap + (source[bit] << 8) + *dsrc++)]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[5] = colormap[*(tranmap + (source[bit] << 8) + *dsrc++)]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[6] = colormap[*(tranmap + (source[bit] << 8) + *dsrc++)]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - dest[7] = colormap[*(tranmap + (source[bit] << 8) + *dsrc++)]; - xposition += xstep; - yposition += ystep; - - dest += 8; - count -= 8; - } - while (count--) - { - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - *dest++ = colormap[*(tranmap + (source[bit] << 8) + *dsrc++)]; - - xposition += xstep; - yposition += ystep; - } -} - -/** \brief The R_DrawSplat function - Just like R_DrawSpan, but skips transparent pixels. -*/ -void R_DrawSplat(drawspandata_t* ds) -{ - UINT32 xposition; - UINT32 yposition; - UINT32 xstep, ystep; - register UINT32 bit; - register UINT32 val; - - const UINT8 *restrict source = ds->source; - const UINT8 *restrict colormap = ds->colormap; - UINT8 *restrict dest = R_Address(ds->x1, ds->y); - - register intptr_t count = (ds->x2 - ds->x1 + 1); - - 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) - // for texture sizes > 64 the amount of precision we can allow will decrease, but only by one - // 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 <<= ds->nflatshiftup; yposition <<= ds->nflatshiftup; - xstep <<= ds->nflatshiftup; ystep <<= ds->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! - // - // 4194303 = (2048x2048)-1 (2048x2048 is maximum flat size) - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - bit &= MAXFLATBYTES; - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[0] = colormap[val]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - bit &= MAXFLATBYTES; - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[1] = colormap[val]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - bit &= MAXFLATBYTES; - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[2] = colormap[val]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - bit &= MAXFLATBYTES; - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[3] = colormap[val]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - bit &= MAXFLATBYTES; - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[4] = colormap[val]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - bit &= MAXFLATBYTES; - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[5] = colormap[val]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - bit &= MAXFLATBYTES; - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[6] = colormap[val]; - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - bit &= MAXFLATBYTES; - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[7] = colormap[val]; - xposition += xstep; - yposition += ystep; - - dest += 8; - count -= 8; - } - while (count--) - { - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - { - *dest = colormap[val]; - } - - dest++; - xposition += xstep; - yposition += ystep; - } -} - -/** \brief The R_DrawTranslucentSplat function - Just like R_DrawSplat, but is translucent! -*/ -void R_DrawTranslucentSplat(drawspandata_t* ds) -{ - UINT32 xposition; - UINT32 yposition; - UINT32 xstep, ystep; - register UINT32 bit; - register UINT32 val; - - const UINT8 *restrict source = ds->source; - const UINT8 *restrict colormap = ds->colormap; - UINT8 *restrict dest = R_Address(ds->x1, ds->y); - - register intptr_t count = (ds->x2 - ds->x1 + 1); - - 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) - // for texture sizes > 64 the amount of precision we can allow will decrease, but only by one - // 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 <<= ds->nflatshiftup; yposition <<= ds->nflatshiftup; - xstep <<= ds->nflatshiftup; ystep <<= ds->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! - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[0] = *(ds->transmap + (colormap[val] << 8) + dest[0]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[1] = *(ds->transmap + (colormap[val] << 8) + dest[1]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[2] = *(ds->transmap + (colormap[val] << 8) + dest[2]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[3] = *(ds->transmap + (colormap[val] << 8) + dest[3]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[4] = *(ds->transmap + (colormap[val] << 8) + dest[4]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[5] = *(ds->transmap + (colormap[val] << 8) + dest[5]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[6] = *(ds->transmap + (colormap[val] << 8) + dest[6]); - xposition += xstep; - yposition += ystep; - - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - dest[7] = *(ds->transmap + (colormap[val] << 8) + dest[7]); - xposition += xstep; - yposition += ystep; - - dest += 8; - count -= 8; - } - while (count--) - { - bit = ((yposition >> ds->nflatyshift) & ds->nflatmask) | (xposition >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - *dest = *(ds->transmap + (colormap[val] << 8) + *dest); - - dest++; - xposition += xstep; - yposition += ystep; - } -} - -/** \brief The R_DrawFogSpan function - Draws the actual span with fogging. -*/ -void R_DrawFogSpan(drawspandata_t* ds) -{ - const UINT8 *restrict colormap = ds->colormap; - UINT8 *restrict dest = R_Address(ds->x1, ds->y); - - register intptr_t count = ds->x2 - ds->x1 + 1; - - while (count >= 4) - { - dest[0] = colormap[dest[0]]; - dest[1] = colormap[dest[1]]; - dest[2] = colormap[dest[2]]; - dest[3] = colormap[dest[3]]; - - dest += 4; - count -= 4; - } - - while (count--) - { - *dest = colormap[*dest]; - dest++; - } -} - -// R_CalcTiltedLighting -// Exactly what it says on the tin. I wish I wasn't too lazy to explain things properly. -static void R_CalcTiltedLighting(INT32 *lightbuffer, INT32 x1, INT32 x2, 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 = x1, right = x2; - fixed_t step = (end-start)/(x2 - x1 + 1); - INT32 i; - - // 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++) - { - lightbuffer[i] = (start += step) >> FRACBITS; - - if (lightbuffer[i] < 0) - { - lightbuffer[i] = 0; - } - else if (lightbuffer[i] >= MAXLIGHTSCALE) - { - lightbuffer[i] = MAXLIGHTSCALE-1; - } - } -} - -#define PLANELIGHTFLOAT ((float)BASEVIDWIDTH * BASEVIDWIDTH / vid.width / ds->zeroheight / 21.0f * FIXED_TO_FLOAT(fovtan)) - -/** \brief The R_DrawTiltedSpan function - Draw slopes! Holy sheit! -*/ -void R_DrawSpan_Tilted(drawspandata_t* ds) -{ - int width = ds->x2 - ds->x1; - float iz, uz, vz; - UINT32 u, v; - int i; - - UINT8 *source; - UINT8 *colormap; - register UINT8 *dest; - - float startz, startu, startv; - float izstep, uzstep, vzstep; - float endz, endu, endv; - UINT32 stepu, stepv; - register UINT32 bit; - local_for_thread INT32 *tiltlighting = NULL; - local_for_thread INT32 oldviewwidth = 0; - - // dont realloc every frame pls thx - if (tiltlighting == NULL || oldviewwidth != viewwidth) - { - tiltlighting = realloc(tiltlighting, sizeof(*tiltlighting) * viewwidth); - oldviewwidth = viewwidth; - } - - iz = ds->szp.z + ds->szp.y*(centery-ds->y) + ds->szp.x*(ds->x1-centerx); - - // Lighting is simple. It's just linear interpolation from start to end - { - float planelightfloat = PLANELIGHTFLOAT; - float lightstart, lightend; - - lightend = (iz + ds->szp.x*width) * planelightfloat; - lightstart = iz * planelightfloat; - - R_CalcTiltedLighting(tiltlighting, ds->x1, ds->x2, FLOAT_TO_FIXED(lightstart), FLOAT_TO_FIXED(lightend)); - //CONS_Printf("tilted lighting %f to %f (foc %f)\n", lightstart, lightend, focallengthf); - } - - uz = ds->sup.z + ds->sup.y*(centery-ds->y) + ds->sup.x*(ds->x1-centerx); - vz = ds->svp.z + ds->svp.y*(centery-ds->y) + ds->svp.x*(ds->x1-centerx); - - dest = R_Address(ds->x1, ds->y); - - source = ds->source; - -#if 0 // The "perfect" reference version of this routine. Pretty slow. - // Use it only to see how things are supposed to look. - i = 0; - do - { - double z = 1.f/iz; - u = (INT64)(uz*z); - v = (INT64)(vz*z); - - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = colormap[source[bit]]; - - dest++; - iz += ds->szp.x; - uz += ds->sup.x; - vz += ds->svp.x; - } while (--width >= 0); -#else - startz = 1.f/iz; - startu = uz*startz; - startv = vz*startz; - - izstep = ds->szp.x * SPANSIZE; - uzstep = ds->sup.x * SPANSIZE; - vzstep = ds->svp.x * SPANSIZE; - width++; - - while (width >= SPANSIZE) - { - iz += izstep; - uz += uzstep; - vz += vzstep; - - endz = 1.f/iz; - endu = uz*endz; - endv = vz*endz; - stepu = (INT64)((endu - startu) * INVSPAN); - stepv = (INT64)((endv - startv) * INVSPAN); - u = (INT64)(startu); - v = (INT64)(startv); - - for (i = SPANSIZE-1; i >= 0; i--) - { - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = colormap[source[bit]]; - - dest++; - u += stepu; - v += stepv; - } - startu = endu; - startv = endv; - width -= SPANSIZE; - } - if (width > 0) - { - if (width == 1) - { - u = (INT64)(startu); - v = (INT64)(startv); - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = colormap[source[bit]]; - } - else - { - float left = width; - iz += ds->szp.x * left; - uz += ds->sup.x * left; - vz += ds->svp.x * left; - - endz = 1.f/iz; - endu = uz*endz; - endv = vz*endz; - left = 1.f/left; - stepu = (INT64)((endu - startu) * left); - stepv = (INT64)((endv - startv) * left); - u = (INT64)(startu); - v = (INT64)(startv); - - for (; width != 0; width--) - { - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = colormap[source[bit]]; - - dest++; - u += stepu; - v += stepv; - } - } - } -#endif -} - -/** \brief The R_DrawTiltedTranslucentSpan function - Like DrawTiltedSpan, but translucent -*/ -void R_DrawTranslucentSpan_Tilted(drawspandata_t* ds) -{ - int width = ds->x2 - ds->x1; - float iz, uz, vz; - UINT32 u, v; - int i; - - UINT8 *source; - UINT8 *colormap; - register UINT8 *dest; - - float startz, startu, startv; - float izstep, uzstep, vzstep; - float endz, endu, endv; - UINT32 stepu, stepv; - register UINT32 bit; - local_for_thread INT32 *tiltlighting = NULL; - local_for_thread INT32 oldviewwidth = 0; - - // dont realloc every frame pls thx - if (tiltlighting == NULL || oldviewwidth != viewwidth) - { - tiltlighting = realloc(tiltlighting, sizeof(*tiltlighting) * viewwidth); - oldviewwidth = viewwidth; - } - - iz = ds->szp.z + ds->szp.y*(centery-ds->y) + ds->szp.x*(ds->x1-centerx); - - // Lighting is simple. It's just linear interpolation from start to end - { - float planelightfloat = PLANELIGHTFLOAT; - float lightstart, lightend; - - lightend = (iz + ds->szp.x*width) * planelightfloat; - lightstart = iz * planelightfloat; - - R_CalcTiltedLighting(tiltlighting, ds->x1, ds->x2, FLOAT_TO_FIXED(lightstart), FLOAT_TO_FIXED(lightend)); - //CONS_Printf("tilted lighting %f to %f (foc %f)\n", lightstart, lightend, focallengthf); - } - - uz = ds->sup.z + ds->sup.y*(centery-ds->y) + ds->sup.x*(ds->x1-centerx); - vz = ds->svp.z + ds->svp.y*(centery-ds->y) + ds->svp.x*(ds->x1-centerx); - - dest = R_Address(ds->x1, ds->y); - - source = ds->source; - -#if 0 // The "perfect" reference version of this routine. Pretty slow. - // Use it only to see how things are supposed to look. - i = 0; - do - { - double z = 1.f/iz; - u = (INT64)(uz*z); - v = (INT64)(vz*z); - - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = *(ds->transmap + (colormap[source[bit]] << 8) + *dest); - - dest++; - iz += ds->szp.x; - uz += ds->sup.x; - vz += ds->svp.x; - } while (--width >= 0); -#else - startz = 1.f/iz; - startu = uz*startz; - startv = vz*startz; - - izstep = ds->szp.x * SPANSIZE; - uzstep = ds->sup.x * SPANSIZE; - vzstep = ds->svp.x * SPANSIZE; - width++; - - while (width >= SPANSIZE) - { - iz += izstep; - uz += uzstep; - vz += vzstep; - - endz = 1.f/iz; - endu = uz*endz; - endv = vz*endz; - stepu = (INT64)((endu - startu) * INVSPAN); - stepv = (INT64)((endv - startv) * INVSPAN); - u = (INT64)(startu); - v = (INT64)(startv); - - for (i = SPANSIZE-1; i >= 0; i--) - { - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = *(ds->transmap + (colormap[source[bit]] << 8) + *dest); - - dest++; - u += stepu; - v += stepv; - } - startu = endu; - startv = endv; - width -= SPANSIZE; - } - if (width > 0) - { - if (width == 1) - { - u = (INT64)(startu); - v = (INT64)(startv); - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = *(ds->transmap + (colormap[source[bit]] << 8) + *dest); - } - else - { - float left = width; - iz += ds->szp.x * left; - uz += ds->sup.x * left; - vz += ds->svp.x * left; - - endz = 1.f/iz; - endu = uz*endz; - endv = vz*endz; - left = 1.f/left; - stepu = (INT64)((endu - startu) * left); - stepv = (INT64)((endv - startv) * left); - u = (INT64)(startu); - v = (INT64)(startv); - - for (; width != 0; width--) - { - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = *(ds->transmap + (colormap[source[bit]] << 8) + *dest); - - dest++; - u += stepu; - v += stepv; - } - } - } -#endif -} - -/** \brief The R_DrawTiltedTranslucentWaterSpan function - Like DrawTiltedTranslucentSpan, but for water -*/ -void R_DrawTranslucentWaterSpan_Tilted(drawspandata_t* ds) -{ - int width = ds->x2 - ds->x1; - float iz, uz, vz; - UINT32 u, v; - int i; - - UINT8 *source; - UINT8 *colormap; - register UINT8 *dest; - UINT8 *dsrc; - - float startz, startu, startv; - float izstep, uzstep, vzstep; - float endz, endu, endv; - UINT32 stepu, stepv; - register UINT32 bit; - local_for_thread INT32 *tiltlighting = NULL; - local_for_thread INT32 oldviewwidth = 0; - - // dont realloc every frame pls thx - if (tiltlighting == NULL || oldviewwidth != viewwidth) - { - tiltlighting = realloc(tiltlighting, sizeof(*tiltlighting) * viewwidth); - oldviewwidth = viewwidth; - } - - iz = ds->szp.z + ds->szp.y*(centery-ds->y) + ds->szp.x*(ds->x1-centerx); - - // Lighting is simple. It's just linear interpolation from start to end - { - float planelightfloat = PLANELIGHTFLOAT; - float lightstart, lightend; - - lightend = (iz + ds->szp.x*width) * planelightfloat; - lightstart = iz * planelightfloat; - - R_CalcTiltedLighting(tiltlighting, ds->x1, ds->x2, FLOAT_TO_FIXED(lightstart), FLOAT_TO_FIXED(lightend)); - //CONS_Printf("tilted lighting %f to %f (foc %f)\n", lightstart, lightend, focallengthf); - } - - uz = ds->sup.z + ds->sup.y*(centery-ds->y) + ds->sup.x*(ds->x1-centerx); - vz = ds->svp.z + ds->svp.y*(centery-ds->y) + ds->svp.x*(ds->x1-centerx); - - dest = R_Address(ds->x1, ds->y); - dsrc = vid.screens[1] + (ds->y+ds->bgofs)*vid.width + ds->x1; - - source = ds->source; - -#if 0 // The "perfect" reference version of this routine. Pretty slow. - // Use it only to see how things are supposed to look. - i = 0; - do - { - double z = 1.f/iz; - u = (INT64)(uz*z); - v = (INT64)(vz*z); - - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = *(ds->transmap + (colormap[source[bit]] << 8) + *dsrc++); - - dest++; - iz += ds->szp.x; - uz += ds->sup.x; - vz += ds->svp.x; - } while (--width >= 0); -#else - startz = 1.f/iz; - startu = uz*startz; - startv = vz*startz; - - izstep = ds->szp.x * SPANSIZE; - uzstep = ds->sup.x * SPANSIZE; - vzstep = ds->svp.x * SPANSIZE; - width++; - - while (width >= SPANSIZE) - { - iz += izstep; - uz += uzstep; - vz += vzstep; - - endz = 1.f/iz; - endu = uz*endz; - endv = vz*endz; - stepu = (INT64)((endu - startu) * INVSPAN); - stepv = (INT64)((endv - startv) * INVSPAN); - u = (INT64)(startu); - v = (INT64)(startv); - - for (i = SPANSIZE-1; i >= 0; i--) - { - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = *(ds->transmap + (colormap[source[bit]] << 8) + *dsrc++); - - dest++; - u += stepu; - v += stepv; - } - startu = endu; - startv = endv; - width -= SPANSIZE; - } - if (width > 0) - { - if (width == 1) - { - u = (INT64)(startu); - v = (INT64)(startv); - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = *(ds->transmap + (colormap[source[bit]] << 8) + *dsrc++); - } - else - { - float left = width; - iz += ds->szp.x * left; - uz += ds->sup.x * left; - vz += ds->svp.x * left; - - endz = 1.f/iz; - endu = uz*endz; - endv = vz*endz; - left = 1.f/left; - stepu = (INT64)((endu - startu) * left); - stepv = (INT64)((endv - startv) * left); - u = (INT64)(startu); - v = (INT64)(startv); - - for (; width != 0; width--) - { - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = *(ds->transmap + (colormap[source[bit]] << 8) + *dsrc++); - - dest++; - u += stepu; - v += stepv; - } - } - } -#endif -} - -void R_DrawSplat_Tilted(drawspandata_t* ds) -{ - // x1, x2 = ds->x1, ds->x2 - int width = ds->x2 - ds->x1; - float iz, uz, vz; - UINT32 u, v; - int i; - - UINT8 *source; - UINT8 *colormap; - register UINT8 *dest; - - UINT8 val; - - float startz, startu, startv; - float izstep, uzstep, vzstep; - float endz, endu, endv; - UINT32 stepu, stepv; - register UINT32 bit; - local_for_thread INT32 *tiltlighting = NULL; - local_for_thread INT32 oldviewwidth = 0; - - // dont realloc every frame pls thx - if (tiltlighting == NULL || oldviewwidth != viewwidth) - { - tiltlighting = realloc(tiltlighting, sizeof(*tiltlighting) * viewwidth); - oldviewwidth = viewwidth; - } - - iz = ds->szp.z + ds->szp.y*(centery-ds->y) + ds->szp.x*(ds->x1-centerx); - - // Lighting is simple. It's just linear interpolation from start to end - { - float planelightfloat = PLANELIGHTFLOAT; - float lightstart, lightend; - - lightend = (iz + ds->szp.x*width) * planelightfloat; - lightstart = iz * planelightfloat; - - R_CalcTiltedLighting(tiltlighting, ds->x1, ds->x2, FLOAT_TO_FIXED(lightstart), FLOAT_TO_FIXED(lightend)); - //CONS_Printf("tilted lighting %f to %f (foc %f)\n", lightstart, lightend, focallengthf); - } - - uz = ds->sup.z + ds->sup.y*(centery-ds->y) + ds->sup.x*(ds->x1-centerx); - vz = ds->svp.z + ds->svp.y*(centery-ds->y) + ds->svp.x*(ds->x1-centerx); - - dest = R_Address(ds->x1, ds->y); - - 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. - i = 0; - do - { - double z = 1.f/iz; - u = (INT64)(uz*z); - v = (INT64)(vz*z); - - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - val = source[bit]; - - if (val != TRANSPARENTPIXEL) - { - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = colormap[val]; - } - dest++; - iz += ds->szp.x; - uz += ds->sup.x; - vz += ds->svp.x; - } while (--width >= 0); -#else - startz = 1.f/iz; - startu = uz*startz; - startv = vz*startz; - - izstep = ds->szp.x * SPANSIZE; - uzstep = ds->sup.x * SPANSIZE; - vzstep = ds->svp.x * SPANSIZE; - //x1 = 0; - width++; - - while (width >= SPANSIZE) - { - iz += izstep; - uz += uzstep; - vz += vzstep; - - endz = 1.f/iz; - endu = uz*endz; - endv = vz*endz; - stepu = (INT64)((endu - startu) * INVSPAN); - stepv = (INT64)((endv - startv) * INVSPAN); - u = (INT64)(startu); - v = (INT64)(startv); - - for (i = SPANSIZE-1; i >= 0; i--) - { - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - { - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = colormap[val]; - } - dest++; - u += stepu; - v += stepv; - } - startu = endu; - startv = endv; - width -= SPANSIZE; - } - if (width > 0) - { - if (width == 1) - { - u = (INT64)(startu); - v = (INT64)(startv); - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - { - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = colormap[val]; - } - } - else - { - float left = width; - iz += ds->szp.x * left; - uz += ds->sup.x * left; - vz += ds->svp.x * left; - - endz = 1.f/iz; - endu = uz*endz; - endv = vz*endz; - left = 1.f/left; - stepu = (INT64)((endu - startu) * left); - stepv = (INT64)((endv - startv) * left); - u = (INT64)(startu); - v = (INT64)(startv); - - for (; width != 0; width--) - { - bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); - val = source[bit]; - if (val != TRANSPARENTPIXEL) - { - colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = colormap[val]; - } - dest++; - u += stepu; - v += stepv; - } - } - } -#endif -} - -void R_DrawFogSpan_Tilted(drawspandata_t* ds) -{ - int width = ds->x2 - ds->x1; - float iz = ds->szp.z + ds->szp.y*(centery-ds->y) + ds->szp.x*(ds->x1-centerx); - register UINT8 *dest; - - local_for_thread INT32 *tiltlighting = NULL; - local_for_thread INT32 oldviewwidth = 0; - - // dont realloc every frame pls thx - if (tiltlighting == NULL || oldviewwidth != viewwidth) - { - tiltlighting = realloc(tiltlighting, sizeof(*tiltlighting) * viewwidth); - oldviewwidth = viewwidth; - } - - dest = R_Address(ds->x1, ds->y); - - // Lighting is simple. It's just linear interpolation from start to end - { - float planelightfloat = PLANELIGHTFLOAT; - float lightstart, lightend; - - lightend = (iz + ds->szp.x*width) * planelightfloat; - lightstart = iz * planelightfloat; - - R_CalcTiltedLighting(tiltlighting, ds->x1, ds->x2, FLOAT_TO_FIXED(lightstart), FLOAT_TO_FIXED(lightend)); - //CONS_Printf("tilted lighting %f to %f (foc %f)\n", lightstart, lightend, focallengthf); - } - - do - { - UINT8 *colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); - *dest = colormap[*dest]; - dest++; - } - while (--width >= 0); -} - diff --git a/src/r_draw_span.cpp b/src/r_draw_span.cpp new file mode 100644 index 000000000..07a76b7cc --- /dev/null +++ b/src/r_draw_span.cpp @@ -0,0 +1,446 @@ +// SONIC ROBO BLAST 2 KART +//----------------------------------------------------------------------------- +// Copyright (C) 2025 by Kart Krew. +// Copyright (C) 2020 by Sonic Team Junior. +// Copyright (C) 2000 by DooM Legacy Team. +// Copyright (C) 1996 by id Software, Inc. +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file r_draw_span.cpp +/// \brief span drawer functions +/// \note no includes because this is included as part of r_draw.cpp + +#ifdef HAVE_THREADS +#ifdef _WIN32 +#include +#define local_for_thread static __thread +#else +#include +#define local_for_thread thread_local static +#endif +#else +#define local_for_thread static +#endif + +// ========================================================================== +// SPANS +// ========================================================================== + +#define SPANSIZE 16 +#define INVSPAN 0.0625f + +// 4194303 = (2048x2048)-1 (2048x2048 is maximum flat size) +#define MAXFLATBYTES 4194303 + +#define PLANELIGHTFLOAT ((float)BASEVIDWIDTH * BASEVIDWIDTH / stride / ds->zeroheight / 21.0f * FIXED_TO_FLOAT(fovtan)) + +enum DrawSpanType +{ + DS_BASIC = 0x0000, + DS_COLORMAP = 0x0001, + DS_TRANSMAP = 0x0002, + DS_HOLES = 0x0004, + DS_RIPPLE = 0x0008, +}; + +template +static constexpr UINT8 R_GetSpanTranslucent(drawspandata_t* ds, UINT8 *dsrc, const UINT8 *colormap, UINT8 col) +{ + col = colormap[col]; + + if constexpr (Type & DrawSpanType::DS_TRANSMAP) + { + return *(ds->transmap + (col << 8) + (*dsrc)); + } + else + { + return col; + } +} + +template +static constexpr UINT8 R_DrawSpanPixel(drawspandata_t* ds, UINT8 *dsrc, const UINT8 *colormap, UINT32 bit, const UINT8 *source) +{ + UINT8 col = source[bit]; + + if constexpr (Type & DrawSpanType::DS_HOLES) + { + if (col == TRANSPARENTPIXEL) + { + return *dsrc; + } + } + + return R_GetSpanTranslucent(ds, dsrc, colormap, col); +} + +/** \brief The R_DrawSpan_8 function + Draws the actual span. +*/ +template +static void R_DrawSpanTemplate(drawspandata_t* ds) +{ + UINT32 xposition; + UINT32 yposition; + UINT32 xstep, ystep; + UINT32 bit; + + UINT8 *dest = R_Address(ds->x1, ds->y); + UINT8 *dsrc; + + const UINT8 *deststop = vid.screens[0] + vid.rowbytes * vid.height; + + if (dest+8 > deststop) + { + return; + } + + intptr_t count = (ds->x2 - ds->x1 + 1); + size_t i; + + xposition = ds->xfrac; yposition = ds->yfrac; + xstep = ds->xstep; ystep = ds->ystep; + + const UINT8 *source = ds->source; + const UINT8 *colormap = ds->colormap; + + if constexpr (Type & DS_RIPPLE) + { + yposition += ds->waterofs; + } + + // 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) + // for texture sizes > 64 the amount of precision we can allow will decrease, but only by one + // 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 <<= ds->nflatshiftup; yposition <<= ds->nflatshiftup; + xstep <<= ds->nflatshiftup; ystep <<= ds->nflatshiftup; + + if constexpr (Type & DS_RIPPLE) + { + dsrc = vid.screens[1] + (ds->y + ds->bgofs) * vid.width + ds->x1; + } + else + { + dsrc = dest; + } + + 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++) + { + bit = (((UINT32)yposition >> ds->nflatyshift) & ds->nflatmask) | ((UINT32)xposition >> ds->nflatxshift); + + dest[i] = R_DrawSpanPixel(ds, &dsrc[i], colormap, bit, source); + + xposition += xstep; + yposition += ystep; + } + + dest += 8; + dsrc += 8; + + count -= 8; + } + + while (count-- && dest <= deststop) + { + bit = (((UINT32)yposition >> ds->nflatyshift) & ds->nflatmask) | ((UINT32)xposition >> ds->nflatxshift); + + *dest = R_DrawSpanPixel(ds, dsrc, colormap, bit, source); + + dest++; + dsrc++; + + xposition += xstep; + yposition += ystep; + } +} + +// R_CalcTiltedLighting +// Exactly what it says on the tin. I wish I wasn't too lazy to explain things properly. +static void R_CalcTiltedLighting(INT32 *lightbuffer, INT32 x1, INT32 x2, 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 i, left = x1, right = x2; + const fixed_t step = (end-start)/(x2 - 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++) + { + lightbuffer[i] = (start += step) >> FRACBITS; + + if (lightbuffer[i] < 0) + { + lightbuffer[i] = 0; + } + else if (lightbuffer[i] >= MAXLIGHTSCALE) + { + lightbuffer[i] = MAXLIGHTSCALE-1; + } + } +} + +template +static void R_DrawTiltedSpanTemplate(drawspandata_t* ds) +{ + int width = ds->x2 - ds->x1; + float iz, uz, vz; + UINT32 u, v; + int i; + + UINT8 *dest = R_Address(ds->x1, ds->y); + UINT8 *dsrc; + + float startz, startu, startv; + float izstep, uzstep, vzstep; + float endz, endu, endv; + UINT32 stepu, stepv; + UINT32 bit; + local_for_thread INT32 *tiltlighting = NULL; + local_for_thread INT32 oldviewwidth = 0; + + // dont realloc every frame pls thx + if (tiltlighting == NULL || oldviewwidth != viewwidth) + { + tiltlighting = static_cast(realloc(tiltlighting, sizeof(*tiltlighting) * viewwidth)); + oldviewwidth = viewwidth; + } + + INT32 x1 = ds->x1; + const INT32 nflatxshift = ds->nflatxshift; + const INT32 nflatyshift = ds->nflatyshift; + const INT32 nflatmask = ds->nflatmask; + const INT32 stride = vid.width; + + iz = ds->szp.z + ds->szp.y*(centery-ds->y) + ds->szp.x*(ds->x1-centerx); + + // Lighting is simple. It's just linear interpolation from start to end + { + float planelightfloat = PLANELIGHTFLOAT; + float lightstart, lightend; + + lightend = (iz + ds->szp.x*width) * planelightfloat; + lightstart = iz * planelightfloat; + + R_CalcTiltedLighting(tiltlighting, ds->x1, ds->x2, FLOAT_TO_FIXED(lightstart), FLOAT_TO_FIXED(lightend)); + //CONS_Printf("tilted lighting %f to %f (foc %f)\n", lightstart, lightend, focallengthf); + } + + uz = ds->sup.z + ds->sup.y*(centery-ds->y) + ds->sup.x*(ds->x1-centerx); + vz = ds->svp.z + ds->svp.y*(centery-ds->y) + ds->svp.x*(ds->x1-centerx); + + const UINT8 *source = ds->source; + const UINT8 *colormap = ds->colormap; + + if constexpr (Type & DS_RIPPLE) + { + dsrc = vid.screens[1] + (ds->y + ds->bgofs) * stride + ds->x1; + } + else + { + dsrc = dest; + } + +#if 0 // The "perfect" reference version of this routine. Pretty slow. + // Use it only to see how things are supposed to look. + i = 0; + do + { + double z = 1.f/iz; + u = (INT64)(uz*z); + v = (INT64)(vz*z); + + bit = ((v >> nflatyshift) & nflatmask) | (u >> nflatxshift); + colormap = planezlight[tiltlighting[ds->x1]] + (ds->colormap - colormaps); + + *dest = R_DrawSpanPixel(ds, dsrc, colormap, bit); + dest++; + ds->x1++; + dsrc++; + iz += ds_szp->x; + uz += ds_sup->x; + vz += ds_svp->x; + } while (--width >= 0); +#else + startz = 1.f/iz; + startu = uz*startz; + startv = vz*startz; + + izstep = ds->szp.x * SPANSIZE; + uzstep = ds->sup.x * SPANSIZE; + vzstep = ds->svp.x * SPANSIZE; + //x1 = 0; + width++; + + while (width >= SPANSIZE) + { + iz += izstep; + uz += uzstep; + vz += vzstep; + + endz = 1.f/iz; + endu = uz*endz; + endv = vz*endz; + stepu = (INT64)((endu - startu) * INVSPAN); + stepv = (INT64)((endv - startv) * INVSPAN); + u = (INT64)(startu); + v = (INT64)(startv); + + x1 = ds->x1; + + for (i = 0; i < SPANSIZE; i++) + { + bit = (((v + stepv * i) >> nflatyshift) & nflatmask) | ((u + stepu * i) >> nflatxshift); + colormap = ds->planezlight[tiltlighting[x1 + i]] + (ds->colormap - colormaps); + dest[i] = R_DrawSpanPixel(ds, &dsrc[i], colormap, bit, source); + } + + ds->x1 += SPANSIZE; + dest += SPANSIZE; + dsrc += SPANSIZE; + startu = endu; + startv = endv; + width -= SPANSIZE; + } + + if (width > 0) + { + if (width == 1) + { + u = (INT64)(startu); + v = (INT64)(startv); + bit = ((v >> nflatyshift) & nflatmask) | (u >> nflatxshift); + colormap = ds->planezlight[tiltlighting[ds->x1]] + (ds->colormap - colormaps); + *dest = R_DrawSpanPixel(ds, dsrc, colormap, bit, source); + ds->x1++; + } + else + { + double left = width; + iz += ds->szp.x * left; + uz += ds->sup.x * left; + vz += ds->svp.x * left; + + endz = 1.f/iz; + endu = uz*endz; + endv = vz*endz; + left = 1.f/left; + stepu = (INT64)((endu - startu) * left); + stepv = (INT64)((endv - startv) * left); + u = (INT64)(startu); + v = (INT64)(startv); + + for (; width != 0; width--) + { + bit = ((v >> ds->nflatyshift) & ds->nflatmask) | (u >> ds->nflatxshift); + colormap = ds->planezlight[tiltlighting[ds->x1]] + (ds->colormap - colormaps); + *dest = R_DrawSpanPixel(ds, dsrc, colormap, bit, source); + dest++; + ds->x1++; + dsrc++; + u += stepu; + v += stepv; + } + } + } +#endif +} + +#define DEFINE_SPAN_FUNC(name, flags, template) \ +void name(drawspandata_t* ds) \ +{ \ + constexpr DrawSpanType opt = static_cast(flags); \ + template(ds); \ +} + +#define DEFINE_SPAN_COMBO(name, flags) \ +DEFINE_SPAN_FUNC(name, flags, R_DrawSpanTemplate) \ +DEFINE_SPAN_FUNC(name ## _Tilted, flags, R_DrawTiltedSpanTemplate) \ + +DEFINE_SPAN_COMBO(R_DrawSpan, DS_BASIC) +DEFINE_SPAN_COMBO(R_DrawTranslucentSpan, DS_TRANSMAP) +DEFINE_SPAN_COMBO(R_DrawSplat, DS_HOLES) +DEFINE_SPAN_COMBO(R_DrawTranslucentSplat, DS_TRANSMAP|DS_HOLES) +DEFINE_SPAN_COMBO(R_DrawTranslucentWaterSpan, DS_TRANSMAP|DS_RIPPLE) + +/** \brief The R_DrawFogSpan function + Draws the actual span with fogging. +*/ +void R_DrawFogSpan(drawspandata_t* ds) +{ + const UINT8 *colormap = ds->colormap; + UINT8 *dest = R_Address(ds->x1, ds->y); + + intptr_t count = ds->x2 - ds->x1 + 1; + + while (count >= 4) + { + dest[0] = colormap[dest[0]]; + dest[1] = colormap[dest[1]]; + dest[2] = colormap[dest[2]]; + dest[3] = colormap[dest[3]]; + + dest += 4; + count -= 4; + } + + while (count--) + { + *dest = colormap[*dest]; + dest++; + } +} + +void R_DrawFogSpan_Tilted(drawspandata_t* ds) +{ + int width = ds->x2 - ds->x1; + float iz = ds->szp.z + ds->szp.y*(centery-ds->y) + ds->szp.x*(ds->x1-centerx); + UINT8 *dest; + + local_for_thread INT32 *tiltlighting = NULL; + local_for_thread INT32 oldviewwidth = 0; + + // dont realloc every frame pls thx + if (tiltlighting == NULL || oldviewwidth != viewwidth) + { + tiltlighting = static_cast(realloc(tiltlighting, sizeof(*tiltlighting) * viewwidth)); + oldviewwidth = viewwidth; + } + + dest = R_Address(ds->x1, ds->y); + const INT32 stride = vid.width; + + // Lighting is simple. It's just linear interpolation from start to end + { + float planelightfloat = PLANELIGHTFLOAT; + float lightstart, lightend; + + lightend = (iz + ds->szp.x*width) * planelightfloat; + lightstart = iz * planelightfloat; + + R_CalcTiltedLighting(tiltlighting, ds->x1, ds->x2, FLOAT_TO_FIXED(lightstart), FLOAT_TO_FIXED(lightend)); + //CONS_Printf("tilted lighting %f to %f (foc %f)\n", lightstart, lightend, focallengthf); + } + + do + { + UINT8 *colormap = ds->planezlight[tiltlighting[ds->x1++]] + (ds->colormap - colormaps); + *dest = colormap[*dest]; + dest++; + } + while (--width >= 0); +} diff --git a/src/r_plane.cpp b/src/r_plane.cpp index e7a9d008c..9610c0c84 100644 --- a/src/r_plane.cpp +++ b/src/r_plane.cpp @@ -1272,6 +1272,9 @@ void R_DrawSinglePlane(drawspandata_t* ds, visplane_t *pl, boolean allow_paralle case SPANDRAWFUNC_SPLAT: spanfunctype = SPANDRAWFUNC_TILTEDSPLAT; break; + case SPANDRAWFUNC_TRANSSPLAT: + spanfunctype = SPANDRAWFUNC_TILTEDTRANSSPLAT; + break; case SPANDRAWFUNC_FOG: spanfunctype = SPANDRAWFUNC_TILTEDFOG; break; diff --git a/src/screen.c b/src/screen.c index cec9b4b9b..67ef0f1dd 100644 --- a/src/screen.c +++ b/src/screen.c @@ -91,6 +91,7 @@ static void SCR_SetDrawFuncs(void) spanfuncs[SPANDRAWFUNC_SPLAT] = R_DrawSplat; spanfuncs[SPANDRAWFUNC_TRANSSPLAT] = R_DrawTranslucentSplat; spanfuncs[SPANDRAWFUNC_TILTEDSPLAT] = R_DrawSplat_Tilted; + spanfuncs[SPANDRAWFUNC_TILTEDTRANSSPLAT] = R_DrawTranslucentSpan_Tilted; spanfuncs[SPANDRAWFUNC_WATER] = R_DrawTranslucentWaterSpan; spanfuncs[SPANDRAWFUNC_TILTEDWATER] = R_DrawTranslucentWaterSpan_Tilted; spanfuncs[SPANDRAWFUNC_FOG] = R_DrawFogSpan;