From 87923973e3d1adc4cfe8248cf45878d22bfb90db Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 10 Dec 2025 19:18:36 +0100 Subject: [PATCH] test: replace gl colormap mipmap linked list with unordered map for faster lookup so far this seems to cut the time searching for an existing colormipmap in half, needs some testing as this is finicky code that may leak badly if not handled correctly models currently broken, for reasons above --- src/hardware/hw_cache.cpp | 74 +++++++++++++++++++++++---------------- src/hardware/hw_data.h | 2 +- src/hardware/hw_md2.c | 7 ++++ 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/hardware/hw_cache.cpp b/src/hardware/hw_cache.cpp index 3af38bfbe..ab70305da 100644 --- a/src/hardware/hw_cache.cpp +++ b/src/hardware/hw_cache.cpp @@ -16,6 +16,7 @@ #ifdef HWRENDER #include +#include #include "hw_main.h" #include "hw_glob.h" @@ -35,6 +36,10 @@ #include "../p_spec.h" // anim_t #include "../r_sky.h" +struct ColorMipMap { + std::unordered_map map; +}; + INT32 patchformat = GL_TEXFMT_AP_88; // use alpha for holes INT32 textureformat = GL_TEXFMT_P_8; // use chromakey for hole @@ -504,34 +509,31 @@ static void HWR_FreeTextureColormaps(patch_t *patch) return; // The mipmap must be valid, obviously - while (pat->mipmap) + if (!pat->mipmap) + return; + + if (pat->mipmap->nextcolormap) { - // Confusing at first, but pat->mipmap->nextcolormap - // at the beginning of the loop is the first colormap - // from the linked list of colormaps. - GLMipmap_t *next = NULL; + for (auto& pair : pat->mipmap->nextcolormap->map) + { + GLMipmap_t *glMipmap = pair.second; - // No mipmap in this patch, break out of the loop. - if (!pat->mipmap) - break; + if (glMipmap) + { + // Free image data from memory. + Z_Free(glMipmap->data); + Z_Free(glMipmap->colormap); + glMipmap->data = NULL; + glMipmap->colormap = NULL; + GL_DeleteTexture(glMipmap); - // No colormap mipmaps either. - if (!pat->mipmap->nextcolormap) - break; + // Free the colormap mipmap from memory. + free(glMipmap); + } + } - // Set the first colormap to the one that comes after it. - next = pat->mipmap->nextcolormap; - pat->mipmap->nextcolormap = next->nextcolormap; - - // Free image data from memory. - Z_Free(next->data); - Z_Free(next->colormap); - next->data = NULL; - next->colormap = NULL; - GL_DeleteTexture(next); - - // Free the old colormap mipmap from memory. - free(next); + delete pat->mipmap->nextcolormap; + pat->mipmap->nextcolormap = NULL; } } @@ -1088,7 +1090,7 @@ void HWR_GetPatch(patch_t *patch) void HWR_GetMappedPatch(patch_t *patch, const UINT8 *colormap) { GLPatch_t *glPatch; - GLMipmap_t *glMipmap, *newMipmap; + GLMipmap_t *newMipmap; if (!patch->hardware) Patch_CreateGL(patch); @@ -1102,11 +1104,19 @@ void HWR_GetMappedPatch(patch_t *patch, const UINT8 *colormap) return; } - // search for the mipmap - // skip the first (no colormap translated) - for (glMipmap = glPatch->mipmap; glMipmap->nextcolormap;) + // gotta make our map if theres none + if (!glPatch->mipmap->nextcolormap) { - glMipmap = glMipmap->nextcolormap; + glPatch->mipmap->nextcolormap = new ColorMipMap(); + } + + // search for the mipmap + auto& map = glPatch->mipmap->nextcolormap->map; + auto it = map.find(colormap); + + if (it != map.end()) + { + GLMipmap_t *glMipmap = it->second; if (glMipmap->colormap && glMipmap->colormap->source == colormap) { @@ -1116,7 +1126,9 @@ void HWR_GetMappedPatch(patch_t *patch, const UINT8 *colormap) HWR_UpdatePatchMipmap(patch, glMipmap); } else + { HWR_LoadPatchMipmap(patch, glMipmap); + } return; } @@ -1131,7 +1143,9 @@ void HWR_GetMappedPatch(patch_t *patch, const UINT8 *colormap) newMipmap = static_cast(calloc(1, sizeof (*newMipmap))); if (newMipmap == NULL) I_Error("%s: Out of memory", "HWR_GetMappedPatch"); - glMipmap->nextcolormap = newMipmap; + + // add to nextcolormap map + map[colormap] = newMipmap; newMipmap->colormap = static_cast(Z_Calloc(sizeof(*newMipmap->colormap), PU_HWRPATCHCOLMIPMAP, NULL)); newMipmap->colormap->source = colormap; diff --git a/src/hardware/hw_data.h b/src/hardware/hw_data.h index bb174b205..d66fe0662 100644 --- a/src/hardware/hw_data.h +++ b/src/hardware/hw_data.h @@ -54,7 +54,7 @@ struct GLMipmap_s UINT16 width, height; UINT32 downloaded; // The GPU has this texture. - struct GLMipmap_s *nextcolormap; + struct ColorMipMap *nextcolormap; struct GLColormap_s *colormap; }; typedef struct GLMipmap_s GLMipmap_t; diff --git a/src/hardware/hw_md2.c b/src/hardware/hw_md2.c index 763fb873c..50ba31951 100644 --- a/src/hardware/hw_md2.c +++ b/src/hardware/hw_md2.c @@ -1033,6 +1033,12 @@ skippixel: static void HWR_GetBlendedTexture(patch_t *patch, patch_t *blendgpatch, INT32 skinnum, const UINT8 *colormap, skincolors_t color) { + (void)patch; + (void)blendgpatch; + (void)skinnum; + (void)colormap; + (void)color; + /* // mostly copied from HWR_GetMappedPatch, hence the similarities and comment GLPatch_t *glPatch = patch->hardware; GLMipmap_t *glMipmap, *newMipmap; @@ -1090,6 +1096,7 @@ static void HWR_GetBlendedTexture(patch_t *patch, patch_t *blendgpatch, INT32 sk GL_SetTexture(newMipmap); Z_ChangeTag(newMipmap->data, PU_HWRMODELTEXTURE_UNLOCKED); + */ } // -----------------+