// 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. // Copyright (C) 1999 by Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman // Copyright (C) 1999-2000 by Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze // Copyright (C) Copyright 2005, 2006 by Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko // Copyright (C) 2013 by James Haley, Stephen McGranahan, et al. // // 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_flush.cpp /// \brief Optimized quad column buffer code. By SoM. /// \note no includes because this is included as part of r_draw.cpp template FUNCINLINE static ATTRINLINE UINT8 R_DrawFlushColumnPixel(UINT8 restrict dest, UINT8 restrict source) { if constexpr (Type & ColumnFlushType::FLUSH_OPAQUE) { return source; } else if constexpr (Type & ColumnFlushType::FLUSH_TRANS) { // haleyjd 09/11/04: use temptranmap here return temp_dc.transmap[(source << 8) + dest]; } else if constexpr (Type & ColumnFlushType::FLUSH_COLORMAP) { return temp_dc.translation[source]; } else if constexpr (Type & ColumnFlushType::FLUSH_COLORMAP_TRANS) { // haleyjd 09/11/04: use temptranmap here return temp_dc.transmap[(temp_dc.translation[source] << 8) + dest]; } } // // R_FlushWhole // // Flushes the entire columns in the buffer, one at a time. // This is used when a quad flush isn't possible. // template static void R_FlushWhole(void) { UINT8 * restrict source; UINT8 * restrict dest; INT32 count, yl; const INT32 stride = vid.width; while (--temp_dc.x >= 0) { yl = temp_dc.yl[temp_dc.x]; source = &temp_dc.buf[temp_dc.x + (yl << 2)]; dest = R_Address(temp_dc.startx + temp_dc.x, yl); count = temp_dc.yh[temp_dc.x] - yl + 1; while (--count >= 0) { *dest = R_DrawFlushColumnPixel(*dest, *source); source += 4; dest += stride; } } } #define DEFINE_GETFLUSHWHOLE_FUNC(name, flags) \ FUNCINLINE static ATTRINLINE void name(void) \ { \ constexpr ColumnFlushType opt = static_cast(flags); \ R_FlushWhole(); \ } DEFINE_GETFLUSHWHOLE_FUNC(R_FlushWholeOpague, FLUSH_OPAQUE) DEFINE_GETFLUSHWHOLE_FUNC(R_FlushWholeTrans, FLUSH_TRANS) DEFINE_GETFLUSHWHOLE_FUNC(R_FlushWholeColormap, FLUSH_COLORMAP) DEFINE_GETFLUSHWHOLE_FUNC(R_FlushWholeColormapTrans, FLUSH_COLORMAP_TRANS) // // R_FlushHT // // Flushes the head and tail of columns in the buffer in // preparation for a quad flush. // template static void R_FlushHT(void) { UINT8 * restrict source; UINT8 * restrict dest; INT32 count, colnum = 0; INT32 yl, yh; const INT32 stride = vid.width; while (colnum < 4) { yl = temp_dc.yl[colnum]; yh = temp_dc.yh[colnum]; // flush column head if (yl < temp_dc.commontop) { source = &temp_dc.buf[colnum + (yl << 2)]; dest = R_Address(temp_dc.startx + colnum, yl); count = temp_dc.commontop - yl; while (--count >= 0) { *dest = R_DrawFlushColumnPixel(*dest, *source); source += 4; dest += stride; } } // flush column tail if (yh > temp_dc.commonbot) { source = &temp_dc.buf[colnum + ((temp_dc.commonbot + 1) << 2)]; dest = R_Address(temp_dc.startx + colnum, temp_dc.commonbot + 1); count = yh - temp_dc.commonbot; while (--count >= 0) { *dest = R_DrawFlushColumnPixel(*dest, *source); source += 4; dest += stride; } } ++colnum; } } #define DEFINE_GETFLUSHHT_FUNC(name, flags) \ FUNCINLINE static ATTRINLINE void name(void) \ { \ constexpr ColumnFlushType opt = static_cast(flags); \ R_FlushHT(); \ } DEFINE_GETFLUSHHT_FUNC(R_FlushHTOpague, FLUSH_OPAQUE) DEFINE_GETFLUSHHT_FUNC(R_FlushHTTrans, FLUSH_TRANS) DEFINE_GETFLUSHHT_FUNC(R_FlushHTColormap, FLUSH_COLORMAP) DEFINE_GETFLUSHHT_FUNC(R_FlushHTColormapTrans, FLUSH_COLORMAP_TRANS) // Begin: Quad column flushing functions. template static void R_FlushQuad(void) { const INT32 stride = vid.width; INT32 count = temp_dc.commonbot - temp_dc.commontop + 1; if constexpr (Type & ColumnFlushType::FLUSH_OPAQUE) { const INT32 *source = reinterpret_cast(temp_dc.buf + (temp_dc.commontop << 2)); INT32 *dest = reinterpret_cast(R_Address(temp_dc.startx, temp_dc.commontop)); INT32 deststep = stride / 4; while(--count >= 0) { *dest = *source++; dest += deststep; } } else if constexpr (Type & ColumnFlushType::FLUSH_TRANS) { const UINT8 * restrict source = temp_dc.buf + (temp_dc.commontop << 2); UINT8 * restrict dest = R_Address(temp_dc.startx, temp_dc.commontop); const UINT8 * restrict transmap = temp_dc.transmap; while (--count >= 0) { // haleyjd 09/11/04: use temptranmap here dest[0] = transmap[(source[0] << 8) + dest[0]]; dest[1] = transmap[(source[1] << 8) + dest[1]]; dest[2] = transmap[(source[2] << 8) + dest[2]]; dest[3] = transmap[(source[3] << 8) + dest[3]]; source += 4; dest += stride; } } else if constexpr (Type & ColumnFlushType::FLUSH_COLORMAP) { const UINT8 * restrict source = temp_dc.buf + (temp_dc.commontop << 2); UINT8 * restrict dest = R_Address(temp_dc.startx, temp_dc.commontop); const UINT8 * restrict translation = temp_dc.translation; while (--count >= 0) { dest[0] = translation[source[0]]; dest[1] = translation[source[1]]; dest[2] = translation[source[2]]; dest[3] = translation[source[3]]; source += 4; dest += stride; } } else if constexpr (Type & ColumnFlushType::FLUSH_COLORMAP_TRANS) { const UINT8 * restrict source = temp_dc.buf + (temp_dc.commontop << 2); UINT8 * restrict dest = R_Address(temp_dc.startx, temp_dc.commontop); const UINT8 * restrict transmap = temp_dc.transmap; const UINT8 * restrict translation = temp_dc.translation; while (--count >= 0) { // haleyjd 09/11/04: use temptranmap here dest[0] = transmap[(translation[source[0]] << 8) + dest[0]]; dest[1] = transmap[(translation[source[1]] << 8) + dest[1]]; dest[2] = transmap[(translation[source[2]] << 8) + dest[2]]; dest[3] = transmap[(translation[source[3]] << 8) + dest[3]]; source += 4; dest += stride; } } } #define DEFINE_GETFLUSHQUAD_FUNC(name, flags) \ FUNCINLINE static ATTRINLINE void name(void) \ { \ constexpr ColumnFlushType opt = static_cast(flags); \ R_FlushQuad(); \ } DEFINE_GETFLUSHQUAD_FUNC(R_FlushQuadOpague, FLUSH_OPAQUE) DEFINE_GETFLUSHQUAD_FUNC(R_FlushQuadTrans, FLUSH_TRANS) DEFINE_GETFLUSHQUAD_FUNC(R_FlushQuadColormap, FLUSH_COLORMAP) DEFINE_GETFLUSHQUAD_FUNC(R_FlushQuadColormapTrans, FLUSH_COLORMAP_TRANS) // haleyjd 09/12/04: split up R_GetBuffer into various different // functions to minimize the number of branches and take advantage // of as much precalculated information as possible. template static UINT8 *R_GetBuffer(drawcolumndata_t *dc) { // haleyjd: reordered predicates if (temp_dc.x == 4 || (temp_dc.x && (temp_dc.type != Type || temp_dc.x + temp_dc.startx != dc->x))) R_FlushColumns(); if (!temp_dc.x) { ++temp_dc.x; temp_dc.startx = dc->x; temp_dc.yl[0] = temp_dc.commontop = dc->yl; temp_dc.yh[0] = temp_dc.commonbot = dc->yh; temp_dc.type = Type; if constexpr (Type & ColumnFlushType::FLUSH_OPAQUE) { R_FlushWholeColumns = R_FlushWholeOpague; R_FlushHTColumns = R_FlushHTOpague; R_FlushQuadColumn = R_FlushQuadOpague; } else if constexpr (Type & ColumnFlushType::FLUSH_TRANS) { temp_dc.transmap = dc->transmap; R_FlushWholeColumns = R_FlushWholeTrans; R_FlushHTColumns = R_FlushHTTrans; R_FlushQuadColumn = R_FlushQuadTrans; } else if constexpr (Type & ColumnFlushType::FLUSH_COLORMAP) { temp_dc.translation = dc->translation; R_FlushWholeColumns = R_FlushWholeColormap; R_FlushHTColumns = R_FlushHTColormap; R_FlushQuadColumn = R_FlushQuadColormap; } else if constexpr (Type & ColumnFlushType::FLUSH_COLORMAP_TRANS) { temp_dc.transmap = dc->transmap; temp_dc.translation = dc->translation; R_FlushWholeColumns = R_FlushWholeColormapTrans; R_FlushHTColumns = R_FlushHTColormapTrans; R_FlushQuadColumn = R_FlushQuadColormapTrans; } return &temp_dc.buf[dc->yl << 2]; } temp_dc.yl[temp_dc.x] = dc->yl; temp_dc.yh[temp_dc.x] = dc->yh; if (dc->yl > temp_dc.commontop) temp_dc.commontop = dc->yl; if (dc->yh < temp_dc.commonbot) temp_dc.commonbot = dc->yh; return &temp_dc.buf[(dc->yl << 2) + temp_dc.x++]; } #define DEFINE_GETBUF_FUNC(name, flags) \ FUNCINLINE static ATTRINLINE UINT8 *name(drawcolumndata_t *dc) \ { \ constexpr ColumnFlushType opt = static_cast(flags); \ return R_GetBuffer(dc); \ } DEFINE_GETBUF_FUNC(R_GetBufferOpaque, FLUSH_OPAQUE) DEFINE_GETBUF_FUNC(R_GetBufferTrans, FLUSH_TRANS) DEFINE_GETBUF_FUNC(R_GetBufferColormap, FLUSH_COLORMAP) DEFINE_GETBUF_FUNC(R_GetBufferColormapTrans, FLUSH_COLORMAP_TRANS)