From 9ab3acae2dcac621e0e58a903d31e271d3b65ceb Mon Sep 17 00:00:00 2001 From: Jaime Ita Passos Date: Sun, 22 Nov 2020 17:03:04 -0300 Subject: [PATCH] Change how texture deletion works in OpenGL --- src/hardware/hw_cache.c | 19 ++++++++++--------- src/hardware/hw_data.h | 3 +-- src/hardware/hw_drv.h | 2 ++ src/hardware/r_opengl/r_opengl.c | 23 ++++++++++++++++++++--- src/sdl/hwsym_sdl.c | 1 + src/sdl/i_video.c | 1 + src/win32/win_dll.c | 2 ++ 7 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/hardware/hw_cache.c b/src/hardware/hw_cache.c index 85dabbcec..d133840ac 100644 --- a/src/hardware/hw_cache.c +++ b/src/hardware/hw_cache.c @@ -642,6 +642,9 @@ void HWR_FreeTextureColormaps(patch_t *patch) // Set the first colormap to the one that comes after it. next = pat->mipmap->nextcolormap; + if (!next) + break; + pat->mipmap->nextcolormap = next->nextcolormap; // Free image data from memory. @@ -671,14 +674,14 @@ void HWR_ClearAllTextures(void) { HWR_FreeMapTextures(); - // free references to the textures - HWD.pfnClearMipMapCache(); - // Alam: free the Z_Blocks before freeing it's users HWR_FreePatchCache(true); + + // free references to the textures + HWD.pfnClearCacheList(); } -// free all patch colormaps after each level: must be done after ClearMipMapCache! +// free all patch colormaps after each level void HWR_FreeColormapCache(void) { HWR_FreePatchCache(false); @@ -696,6 +699,7 @@ static void FreeMapTexture(GLMapTexture_t *tex) HWD.pfnDeleteTexture(&tex->mipmap); if (tex->mipmap.data) Z_Free(tex->mipmap.data); + tex->mipmap.data = NULL; } void HWR_FreeMapTextures(void) @@ -722,18 +726,15 @@ void HWR_FreeMapTextures(void) void HWR_LoadMapTextures(size_t pnumtextures) { - // we must free it since numtextures changed + // we must free it since numtextures may have changed HWR_FreeMapTextures(); - // Why not Z_Malloc? gl_numtextures = pnumtextures; gl_textures = calloc(gl_numtextures, sizeof(*gl_textures)); gl_flats = calloc(gl_numtextures, sizeof(*gl_flats)); - // Doesn't tell you which it _is_, but hopefully - // should never ever happen (right?!) if ((gl_textures == NULL) || (gl_flats == NULL)) - I_Error("HWR_LoadMapTextures: ran out of memory for OpenGL textures. Sad!"); + I_Error("HWR_LoadMapTextures: ran out of memory for OpenGL textures"); gl_maptexturesloaded = true; } diff --git a/src/hardware/hw_data.h b/src/hardware/hw_data.h index 6a872d258..32fb96e17 100644 --- a/src/hardware/hw_data.h +++ b/src/hardware/hw_data.h @@ -55,8 +55,7 @@ struct GLMipmap_s struct GLMipmap_s *nextcolormap; const UINT8 *colormap; - // opengl - struct GLMipmap_s *nextmipmap; // opengl : liste of all texture in opengl driver + struct GLMipmap_s *prevmipmap, *nextmipmap; // Linked list of all textures }; typedef struct GLMipmap_s GLMipmap_t; diff --git a/src/hardware/hw_drv.h b/src/hardware/hw_drv.h index de17f97d2..5a2e0e44e 100644 --- a/src/hardware/hw_drv.h +++ b/src/hardware/hw_drv.h @@ -46,6 +46,7 @@ EXPORT void HWRAPI(DeleteTexture) (FTextureInfo *TexInfo); EXPORT void HWRAPI(ReadRect) (INT32 x, INT32 y, INT32 width, INT32 height, INT32 dst_stride, UINT16 *dst_data); EXPORT void HWRAPI(GClipRect) (INT32 minx, INT32 miny, INT32 maxx, INT32 maxy, float nearclip); EXPORT void HWRAPI(ClearMipMapCache) (void); +EXPORT void HWRAPI(ClearCacheList) (void); //Hurdler: added for backward compatibility EXPORT void HWRAPI(SetSpecialState) (hwdspecialstate_t IdState, INT32 Value); @@ -100,6 +101,7 @@ struct hwdriver_s ReadRect pfnReadRect; GClipRect pfnGClipRect; ClearMipMapCache pfnClearMipMapCache; + ClearCacheList pfnClearCacheList; SetSpecialState pfnSetSpecialState;//Hurdler: added for backward compatibility DrawModel pfnDrawModel; CreateModelVBOs pfnCreateModelVBOs; diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 39552dc1c..4c29dd2e4 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -1292,6 +1292,9 @@ EXPORT void HWRAPI(DeleteTexture) (FTextureInfo *pTexInfo) if (pTexInfo->downloaded) pglDeleteTextures(1, (GLuint *)&pTexInfo->downloaded); pTexInfo->downloaded = 0; + + if (pTexInfo->prevmipmap) + pTexInfo->prevmipmap->nextmipmap = pTexInfo->nextmipmap; } @@ -1308,12 +1311,21 @@ void Flush(void) DeleteTexture(gl_cachehead); gl_cachehead = gl_cachehead->nextmipmap; } - gl_cachetail = gl_cachehead = NULL; //Hurdler: well, gl_cachehead is already NULL + ClearCacheList(); //Hurdler: well, gl_cachehead is already NULL tex_downloaded = 0; } +// -----------------+ +// ClearCacheList : Clears the texture cache tail and head +// -----------------+ +EXPORT void HWRAPI(ClearCacheList) (void) +{ + gl_cachetail = gl_cachehead = NULL; +} + + // -----------------+ // isExtAvailable : Look if an OpenGL extension is available // Returns : true if extension available @@ -1929,14 +1941,19 @@ EXPORT void HWRAPI(SetTexture) (FTextureInfo *pTexInfo) else { UpdateTexture(pTexInfo); + + pTexInfo->prevmipmap = NULL; pTexInfo->nextmipmap = NULL; + + // insertion at the tail if (gl_cachetail) - { // insertion at the tail + { gl_cachetail->nextmipmap = pTexInfo; + pTexInfo->prevmipmap = gl_cachetail; gl_cachetail = pTexInfo; } else // initialization of the linked list - gl_cachetail = gl_cachehead = pTexInfo; + gl_cachetail = gl_cachehead = pTexInfo; } } diff --git a/src/sdl/hwsym_sdl.c b/src/sdl/hwsym_sdl.c index 96e3d7d69..398508662 100644 --- a/src/sdl/hwsym_sdl.c +++ b/src/sdl/hwsym_sdl.c @@ -90,6 +90,7 @@ void *hwSym(const char *funcName,void *handle) GETFUNC(ReadRect); GETFUNC(GClipRect); GETFUNC(ClearMipMapCache); + GETFUNC(ClearCacheList); GETFUNC(SetSpecialState); GETFUNC(GetTextureUsed); GETFUNC(DrawModel); diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index b9423ac21..f83f57576 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -1868,6 +1868,7 @@ void VID_StartupOpenGL(void) HWD.pfnReadRect = hwSym("ReadRect",NULL); HWD.pfnGClipRect = hwSym("GClipRect",NULL); HWD.pfnClearMipMapCache = hwSym("ClearMipMapCache",NULL); + HWD.pfnClearCacheList = hwSym("ClearCacheList",NULL); HWD.pfnSetSpecialState = hwSym("SetSpecialState",NULL); HWD.pfnSetPalette = hwSym("SetPalette",NULL); HWD.pfnGetTextureUsed = hwSym("GetTextureUsed",NULL); diff --git a/src/win32/win_dll.c b/src/win32/win_dll.c index 4743cec34..d942d8cd4 100644 --- a/src/win32/win_dll.c +++ b/src/win32/win_dll.c @@ -111,6 +111,7 @@ static loadfunc_t hwdFuncTable[] = { {"ReadRect@24", &hwdriver.pfnReadRect}, {"GClipRect@20", &hwdriver.pfnGClipRect}, {"ClearMipMapCache@0", &hwdriver.pfnClearMipMapCache}, + {"ClearCacheList@0", &hwdriver.pfnClearCacheList}, {"SetSpecialState@8", &hwdriver.pfnSetSpecialState}, {"DrawModel@16", &hwdriver.pfnDrawModel}, {"SetTransform@4", &hwdriver.pfnSetTransform}, @@ -144,6 +145,7 @@ static loadfunc_t hwdFuncTable[] = { {"ReadRect", &hwdriver.pfnReadRect}, {"GClipRect", &hwdriver.pfnGClipRect}, {"ClearMipMapCache", &hwdriver.pfnClearMipMapCache}, + {"ClearCacheList", &hwdriver.pfnClearCacheList}, {"SetSpecialState", &hwdriver.pfnSetSpecialState}, {"DrawModel", &hwdriver.pfnDrawModel}, {"SetTransform", &hwdriver.pfnSetTransform},