From e103b7a3e78ac0f0966dea51c8eddd27397ea41e Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 28 Nov 2025 16:52:58 +0100 Subject: [PATCH] HWR_Precache: lookup for flats that already have been precached yes i used c++ unordered_set here, yes i suck with linked lists, lol maybe speeds this process up a little --- src/hardware/{hw_cache.c => hw_cache.cpp} | 98 +++++++++++++---------- 1 file changed, 56 insertions(+), 42 deletions(-) rename src/hardware/{hw_cache.c => hw_cache.cpp} (91%) diff --git a/src/hardware/hw_cache.c b/src/hardware/hw_cache.cpp similarity index 91% rename from src/hardware/hw_cache.c rename to src/hardware/hw_cache.cpp index b4dbb8911..cb698ff8c 100644 --- a/src/hardware/hw_cache.c +++ b/src/hardware/hw_cache.cpp @@ -15,6 +15,8 @@ #ifdef HWRENDER +#include + #include "hw_main.h" #include "hw_glob.h" #include "hw_gl.h" @@ -36,7 +38,7 @@ INT32 patchformat = GL_TEXFMT_AP_88; // use alpha for holes INT32 textureformat = GL_TEXFMT_P_8; // use chromakey for hole -RGBA_t mapPalette[256] = {0}; // the palette for the currently loaded level or menu etc. +RGBA_t mapPalette[256] = {}; // the palette for the currently loaded level or menu etc. static void HWR_FreeTextureColormaps(patch_t *patch); @@ -98,8 +100,7 @@ static void HWR_DrawColumnInCache(const column_t *patchcol, UINT8 *block, GLMipm position = 0; } - position = ((position * scale_y) + (FRACUNIT/2)) >> FRACBITS; - position = max(position, 0); + position = std::max(((position * scale_y) + (FRACUNIT/2)) >> FRACBITS, 0); if (position + count >= pblockheight) count = pblockheight - position; @@ -163,7 +164,7 @@ static void HWR_DrawPatchInCache(GLMipmap_t *mipmap, fixed_t xfrac, xfracstep; fixed_t yfracstep, scale_y; const column_t *patchcol; - UINT8 *block = mipmap->data; + UINT8 *block = static_cast(mipmap->data); INT32 bpp; INT32 blockmodulo; RGBA_t *palette; @@ -213,7 +214,7 @@ static void HWR_DrawTexturePatchInCache(GLMipmap_t *mipmap, fixed_t xfrac, xfracstep; fixed_t yfracstep, scale_y; const column_t *patchcol; - UINT8 *block = mipmap->data; + UINT8 *block = static_cast(mipmap->data); INT32 bpp; INT32 blockmodulo; INT32 width, height; @@ -288,8 +289,8 @@ static UINT8 *MakeBlock(GLMipmap_t *glMipmap) UINT16 bu16 = ((0x00 <<8) | HWR_PATCHES_CHROMAKEY_COLORINDEX); INT32 blocksize = (glMipmap->width * glMipmap->height); - bpp = format2bpp(glMipmap->format); - block = Z_Malloc(blocksize*bpp, PU_HWRCACHE, &(glMipmap->data)); + bpp = format2bpp(glMipmap->format); + block = static_cast(Z_Malloc(blocksize*bpp, PU_HWRCACHE, &(glMipmap->data))); switch (bpp) { @@ -326,7 +327,7 @@ static void HWR_GenerateTexture(INT32 texnum, GLMapTexture_t *gltex, boolean noe texture = textures[texnum]; gltex->mipmap.flags = TF_CHROMAKEYED | TF_WRAPXY; - gltex->mipmap.format = textureformat; + gltex->mipmap.format = static_cast(textureformat); // hack the Legacy skies.. if (UNLIKELY(memcmp(texture->name, "SKY", 3) == 0 && @@ -345,7 +346,7 @@ static void HWR_GenerateTexture(INT32 texnum, GLMapTexture_t *gltex, boolean noe colormap += COLORMAP_REMAPOFFSET; #endif - gltex->mipmap.colormap = Z_Calloc(sizeof(*gltex->mipmap.colormap), PU_HWRPATCHCOLMIPMAP, NULL); + gltex->mipmap.colormap = static_cast(Z_Calloc(sizeof(*gltex->mipmap.colormap), PU_HWRPATCHCOLMIPMAP, NULL)); gltex->mipmap.colormap->source = colormap; memcpy(gltex->mipmap.colormap->data, colormap, 256 * sizeof(UINT8)); @@ -378,7 +379,7 @@ static void HWR_GenerateTexture(INT32 texnum, GLMapTexture_t *gltex, boolean noe // Composite the columns together. for (i = 0, patch = texture->patches; i < texture->patchcount; i++, patch++) { - pdata = W_CacheLumpNumPwad(patch->wad, patch->lump, PU_LEVEL); + pdata = static_cast(W_CacheLumpNumPwad(patch->wad, patch->lump, PU_LEVEL)); realpatch = (softwarepatch_t *)pdata; HWR_DrawTexturePatchInCache(&gltex->mipmap, blockwidth, blockheight, texture, patch, realpatch); @@ -414,7 +415,7 @@ void HWR_MakePatch(const patch_t *patch, GLPatch_t *glPatch, GLMipmap_t *glMipma glMipmap->flags = 0; // setup the texture info - glMipmap->format = patchformat; + glMipmap->format = static_cast(patchformat); glPatch->max_s = (float)patch->width / (float)glMipmap->width; glPatch->max_t = (float)patch->height / (float)glMipmap->height; @@ -456,7 +457,7 @@ static void HWR_FreeTextureData(patch_t *patch) if (!patch || !patch->hardware) return; - glPatch = patch->hardware; + glPatch = static_cast(patch->hardware); if (vid.glstate == VID_GL_LIBRARY_LOADED) GL_DeleteTexture(glPatch->mipmap); @@ -472,7 +473,7 @@ void HWR_FreeTexture(patch_t *patch) if (patch->hardware) { - GLPatch_t *glPatch = patch->hardware; + GLPatch_t *glPatch = static_cast(patch->hardware); HWR_FreeTextureColormaps(patch); @@ -498,7 +499,7 @@ static void HWR_FreeTextureColormaps(patch_t *patch) if (!patch) return; - pat = patch->hardware; + pat = static_cast(patch->hardware); if (!pat) return; @@ -615,10 +616,23 @@ void HWR_FreeMapTextures(void) static void HWR_PrecacheLevelFlats(void) { + // lookup for flats that may´ve been already loaded + // too lazy to make a hashtable or smth so unordered set will do :carbuncle: + std::unordered_set flatpresent; lumpnum_t lump; size_t i, j; INT32 k; + auto LoadFlat = [&](lumpnum_t lump, boolean noencoremap) + { + // flat already loaded? + if (flatpresent.count(lump) == 0) + { + HWR_GetFlat(lump, noencoremap); + flatpresent.insert(lump); + } + }; + // special case for encore #ifdef GLENCORE if (encoremap) @@ -635,16 +649,16 @@ static void HWR_PrecacheLevelFlats(void) const INT32 pic = ceiling ? sec->ceilingpic : sec->floorpic; const levelflat_t *levelflat = &levelflats[pic]; - lump = levelflat->lumpnum; - HWR_GetFlat(lump, R_NoEncore(sec, ceiling)); - if (levelflat->speed) // it is an animated flat + LoadFlat(lump, R_NoEncore(sec, ceiling)); + + if (levelflat->speed) // is it an animated flat ? { - for (k = 1; k < levelflat->numpics; k++) + // start from 0, occasionally basenumlump != flat->lumpnum + for (k = 0; k < levelflat->numpics; k++) { - lump = levelflat->baselumpnum + k; - HWR_GetFlat(lump, R_NoEncore(sec, ceiling)); + LoadFlat(levelflat->baselumpnum + k, R_NoEncore(sec, ceiling)); } } } @@ -660,15 +674,15 @@ static void HWR_PrecacheLevelFlats(void) const levelflat_t *levelflat = &levelflats[i]; lump = levelflat->lumpnum; - HWR_GetFlat(lump, false); + LoadFlat(lump, false); - if (!levelflat->speed) // is it an animated flat ? - continue; - - for (k = 1; k < levelflat->numpics; k++) + if (levelflat->speed) // is it an animated flat ? { - lump = levelflat->baselumpnum + k; - HWR_GetFlat(lump, false); + // start from 0, occasionally basenumlump != flat->lumpnum + for (k = 0; k < levelflat->numpics; k++) + { + LoadFlat(levelflat->baselumpnum + k, false); + } } } } @@ -681,7 +695,7 @@ static void HWR_PrecacheLevelTextures(void) size_t i, j, f; INT32 h; - texturepresent = calloc(gl_numtextures, sizeof(*texturepresent)); + texturepresent = static_cast(calloc(gl_numtextures, sizeof(*texturepresent))); if (texturepresent == NULL) I_Error("%s: Out of memory looking up textures", "HWR_PrecacheLevel"); @@ -856,7 +870,7 @@ void HWR_LoadMapTextures(size_t pnumtextures) HWR_FreeMapTextures(); gl_numtextures = pnumtextures; - gl_textures = calloc(gl_numtextures, sizeof(*gl_textures)); // *2 - 1 for encore-remapped texture and another for noencore texture (unused when not in encore), see: GLMapTextureSet_t + gl_textures = static_cast(calloc(gl_numtextures, sizeof(*gl_textures))); // *2 - 1 for encore-remapped texture and another for noencore texture (unused when not in encore), see: GLMapTextureSet_t if (gl_textures == NULL) I_Error("HWR_LoadMapTextures: ran out of memory for OpenGL textures. Sad!"); @@ -949,7 +963,7 @@ static void HWR_CacheFlat(GLMipmap_t *glMipmap, lumpnum_t flatlumpnum) W_ReadLump(flatlumpnum, Z_Malloc(size, PU_HWRCACHE, &glMipmap->data)); #ifdef GLENCORE - flat = glMipmap->data; + flat = static_cast(glMipmap->data); for (steppy = 0; steppy < size; steppy++) { if (flat[steppy] == HWR_PATCHES_CHROMAKEY_COLORINDEX) @@ -993,7 +1007,7 @@ void HWR_GetFlat(lumpnum_t flatlumpnum, boolean noencoremap) if (!noencoremap && encoremap) colormap += COLORMAP_REMAPOFFSET; #endif - glMipmap->colormap = Z_Calloc(sizeof(*glMipmap->colormap), PU_HWRPATCHCOLMIPMAP, NULL); + glMipmap->colormap = static_cast(Z_Calloc(sizeof(*glMipmap->colormap), PU_HWRPATCHCOLMIPMAP, NULL)); glMipmap->colormap->source = colormap; memcpy(glMipmap->colormap->data, colormap, 256 * sizeof(UINT8)); } @@ -1017,7 +1031,7 @@ void HWR_GetFlat(lumpnum_t flatlumpnum, boolean noencoremap) // --------------------+ static void HWR_LoadPatchMipmap(patch_t *patch, GLMipmap_t *glMipmap) { - GLPatch_t *glPatch = patch->hardware; + GLPatch_t *glPatch = static_cast(patch->hardware); if (!glMipmap->downloaded) { @@ -1038,7 +1052,7 @@ static void HWR_LoadPatchMipmap(patch_t *patch, GLMipmap_t *glMipmap) // ----------------------+ static void HWR_UpdatePatchMipmap(patch_t *patch, GLMipmap_t *glMipmap) { - GLPatch_t *grPatch = patch->hardware; + GLPatch_t *grPatch = static_cast(patch->hardware); HWR_MakePatch(patch, grPatch, glMipmap, true); // If hardware does not have the texture, then call GL_SetTexture to upload it @@ -1073,7 +1087,7 @@ void HWR_GetMappedPatch(patch_t *patch, const UINT8 *colormap) if (!patch->hardware) Patch_CreateGL(patch); - glPatch = patch->hardware; + glPatch = static_cast(patch->hardware); // Blatant hack for encore colormapping aside... if (colormap == colormaps || colormap == NULL || colormap == (const UINT8*)(COLORMAP_REMAPOFFSET)) @@ -1109,12 +1123,12 @@ void HWR_GetMappedPatch(patch_t *patch, const UINT8 *colormap) // (it have a liste of mipmap) // this malloc is cleared in HWR_FreeColormapCache // (...) unfortunately z_malloc fragment alot the memory :(so malloc is better - newMipmap = calloc(1, sizeof (*newMipmap)); + newMipmap = static_cast(calloc(1, sizeof (*newMipmap))); if (newMipmap == NULL) I_Error("%s: Out of memory", "HWR_GetMappedPatch"); glMipmap->nextcolormap = newMipmap; - newMipmap->colormap = Z_Calloc(sizeof(*newMipmap->colormap), PU_HWRPATCHCOLMIPMAP, NULL); + newMipmap->colormap = static_cast(Z_Calloc(sizeof(*newMipmap->colormap), PU_HWRPATCHCOLMIPMAP, NULL)); newMipmap->colormap->source = colormap; memcpy(newMipmap->colormap->data, colormap, 256 * sizeof(UINT8)); @@ -1137,7 +1151,7 @@ patch_t *HWR_GetCachedGLPatchPwad(UINT16 wadnum, UINT16 lumpnum) { void *ptr = Z_Calloc(sizeof(patch_t), PU_PATCH, &lumpcache[lumpnum]); Patch_Create(NULL, 0, ptr); - Patch_AllocateHardwarePatch(ptr); + Patch_AllocateHardwarePatch(static_cast(ptr)); } return (patch_t *)(lumpcache[lumpnum]); @@ -1154,7 +1168,7 @@ static void HWR_DrawFadeMaskInCache(GLMipmap_t *mipmap, INT32 pblockwidth, INT32 { INT32 i,j; fixed_t posx, posy, stepx, stepy; - UINT8 *block = mipmap->data; // places the data directly into here + UINT8 *block = static_cast(mipmap->data); // places the data directly into here UINT8 *flat; UINT8 *dest, *src, texel; RGBA_t col; @@ -1310,9 +1324,9 @@ void HWR_SetPalette(RGBA_t *palette) static void HWR_SetPaletteLookup(RGBA_t *palette) { int r, g, b; - UINT8 *lut = Z_Malloc( + UINT8 *lut = static_cast(Z_Malloc( HWR_PALETTE_LUT_SIZE*HWR_PALETTE_LUT_SIZE*HWR_PALETTE_LUT_SIZE*sizeof(UINT8), - PU_STATIC, NULL); + PU_STATIC, NULL)); #define STEP_SIZE (256/HWR_PALETTE_LUT_SIZE) for (b = 0; b < HWR_PALETTE_LUT_SIZE; b++) { @@ -1355,7 +1369,7 @@ void HWR_SetMapPalette(void) if (palsize < 768) // 256 * 3 I_Error("HWR_SetMapPalette: A programmer assumed palette lumps are at least 768 bytes long, but apparently this was a wrong assumption!\n"); - RGB_data = W_CacheLumpNum(lumpnum, PU_CACHE); + RGB_data = static_cast(W_CacheLumpNum(lumpnum, PU_CACHE)); // we got the RGB palette now, but we need it in RGBA format. for (i = 0; i < 256; i++) { @@ -1390,7 +1404,7 @@ static UINT32 HWR_CreateLightTable(UINT8 *lighttable) { UINT32 i, id; RGBA_t *palette = HWR_GetTexturePalette(); - RGBA_t *hw_lighttable = Z_Malloc(256 * 32 * sizeof(RGBA_t), PU_STATIC, NULL); + RGBA_t *hw_lighttable = static_cast(Z_Malloc(256 * 32 * sizeof(RGBA_t), PU_STATIC, NULL)); // To make the palette index -> RGBA mapping easier for the shader, // the hardware lighttable is composed of RGBA colors instead of palette indices.