From cc12496e9c3fce929546640bbe2c1b35056d7a86 Mon Sep 17 00:00:00 2001 From: Jaime Passos Date: Sun, 8 Dec 2019 03:23:37 -0300 Subject: [PATCH] Optimize mipmaps --- src/hardware/hw_cache.c | 22 ++++++++++++---------- src/hardware/hw_glob.h | 1 + src/hardware/hw_main.c | 1 + src/hardware/hw_main.h | 2 +- src/p_setup.c | 20 +++++++++++--------- src/r_data.c | 9 +++++++++ 6 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/hardware/hw_cache.c b/src/hardware/hw_cache.c index e19bd8331..aef97e817 100644 --- a/src/hardware/hw_cache.c +++ b/src/hardware/hw_cache.c @@ -804,15 +804,17 @@ static void FreeMipmapColormap(INT32 patchnum, void *patch) // Free image data from memory. if (next->grInfo.data) Z_Free(next->grInfo.data); + next->grInfo.data = NULL; - // Free the old colormap from memory. + // Free the old colormap mipmap from memory. free(next); } } -void HWR_FreeTextureCache(void) +void HWR_FreeMipmapCache(void) { INT32 i; + // free references to the textures HWD.pfnClearMipMapCache(); @@ -822,10 +824,15 @@ void HWR_FreeTextureCache(void) Z_FreeTag(PU_HWRCACHE_UNLOCKED); // Alam: free the Z_Blocks before freeing it's users - // free all patch colormaps after each level: must be done after ClearMipMapCache! for (i = 0; i < numwadfiles; i++) M_AATreeIterate(wadfiles[i]->hwrcache, FreeMipmapColormap); +} + +void HWR_FreeTextureCache(void) +{ + // free references to the textures + HWR_FreeMipmapCache(); // now the heap don't have any 'user' pointing to our // texturecache info, we can free it @@ -838,13 +845,8 @@ void HWR_FreeTextureCache(void) gr_numtextures = 0; } -void HWR_PrepLevelCache(size_t pnumtextures) +void HWR_LoadTextures(size_t pnumtextures) { - // problem: the mipmap cache management hold a list of mipmaps.. but they are - // reallocated on each level.. - //sub-optimal, but 1) just need re-download stuff in hardware cache VERY fast - // 2) sprite/menu stuff mixed with level textures so can't do anything else - // we must free it since numtextures changed HWR_FreeTextureCache(); @@ -856,7 +858,7 @@ void HWR_PrepLevelCache(size_t pnumtextures) // Doesn't tell you which it _is_, but hopefully // should never ever happen (right?!) if ((gr_textures == NULL) || (gr_flats == NULL)) - I_Error("HWR_PrepLevelCache: ran out of memory for OpenGL textures. Sad!"); + I_Error("HWR_LoadTextures: ran out of memory for OpenGL textures. Sad!"); } void HWR_SetPalette(RGBA_t *palette) diff --git a/src/hardware/hw_glob.h b/src/hardware/hw_glob.h index 69cf8bf46..d25f6cefd 100644 --- a/src/hardware/hw_glob.h +++ b/src/hardware/hw_glob.h @@ -100,6 +100,7 @@ void HWR_FreePolyPool(void); // -------- void HWR_InitTextureCache(void); void HWR_FreeTextureCache(void); +void HWR_FreeMipmapCache(void); void HWR_FreeExtraSubsectors(void); void HWR_GetLevelFlat(levelflat_t *levelflat); diff --git a/src/hardware/hw_main.c b/src/hardware/hw_main.c index db4990436..5baf89e6c 100644 --- a/src/hardware/hw_main.c +++ b/src/hardware/hw_main.c @@ -6687,6 +6687,7 @@ void HWR_Shutdown(void) CONS_Printf("HWR_Shutdown()\n"); HWR_FreeExtraSubsectors(); HWR_FreePolyPool(); + HWR_FreeMipmapCache(); HWR_FreeTextureCache(); HWD.pfnFlushScreenTextures(); } diff --git a/src/hardware/hw_main.h b/src/hardware/hw_main.h index e19c557d0..3ff7e98c9 100644 --- a/src/hardware/hw_main.h +++ b/src/hardware/hw_main.h @@ -47,7 +47,7 @@ void HWR_DrawCroppedPatch(GLPatch_t *gpatch, fixed_t x, fixed_t y, fixed_t scale void HWR_MakePatch(const patch_t *patch, GLPatch_t *grPatch, GLMipmap_t *grMipmap, boolean makebitmap); void HWR_CreatePlanePolygons(INT32 bspnum); void HWR_CreateStaticLightmaps(INT32 bspnum); -void HWR_PrepLevelCache(size_t pnumtextures); +void HWR_LoadTextures(size_t pnumtextures); void HWR_DrawFill(INT32 x, INT32 y, INT32 w, INT32 h, INT32 color); void HWR_DrawFadeFill(INT32 x, INT32 y, INT32 w, INT32 h, INT32 color, UINT16 actualcolor, UINT8 strength); void HWR_DrawConsoleFill(INT32 x, INT32 y, INT32 w, INT32 h, INT32 color, UINT32 actualcolor); // Lat: separate flags from color since color needs to be an uint to work right. diff --git a/src/p_setup.c b/src/p_setup.c index 673a9024e..d38a78bc8 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -3019,8 +3019,17 @@ boolean P_SetupLevel(boolean skipprecip) P_SpawnPrecipitation(); #ifdef HWRENDER // not win32 only 19990829 by Kin - if (rendermode != render_soft && rendermode != render_none) + if (rendermode == render_opengl) { + // Lactozilla (December 8, 2019) + // Level setup used to free EVERY mipmap from memory. + // Even mipmaps that aren't related to level textures. + // Presumably, the hardware render code used to store textures as level data. + // Meaning, they had memory allocated and marked with the PU_LEVEL tag. + // Now, only mipmaps that AREN'T level textures are freed between levels. + // Level textures are only reloaded after R_LoadTextures, which is + // when the texture list is loaded. + HWR_FreeMipmapCache(); #ifdef ALAM_LIGHTING // BP: reset light between levels (we draw preview frame lights on current frame) HWR_ResetLights(); @@ -3203,14 +3212,6 @@ boolean P_SetupLevel(boolean skipprecip) // Fab : 19-07-98 : start cd music for this level (note: can be remapped) I_PlayCD((UINT8)(gamemap), false); - // preload graphics -#ifdef HWRENDER // not win32 only 19990829 by Kin - if (rendermode != render_soft && rendermode != render_none) - { - HWR_PrepLevelCache(numtextures); - } -#endif - P_MapEnd(); // Remove the loading shit from the screen @@ -3540,6 +3541,7 @@ boolean P_AddWadFile(const char *wadfilename) #ifdef HWRENDER HWR_ReloadModels(); + HWR_FreeMipmapCache(); #endif // reload status bar (warning should have valid player!) diff --git a/src/r_data.c b/src/r_data.c index 5611ee6f8..c3f3bf8ff 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -32,6 +32,10 @@ #include // alloca(sizeof) #endif +#ifdef HWRENDER +#include "hardware/hw_main.h" // HWR_LoadTextures +#endif + #if defined(_MSC_VER) #pragma pack(1) #endif @@ -872,6 +876,11 @@ void R_LoadTextures(void) i++; } } + +#ifdef HWRENDER + if (rendermode == render_opengl) + HWR_LoadTextures(numtextures); +#endif } static texpatch_t *R_ParsePatch(boolean actuallyLoadPatch)