diff --git a/src/hardware/hw_cache.c b/src/hardware/hw_cache.c index 5534e753a..8f272a297 100644 --- a/src/hardware/hw_cache.c +++ b/src/hardware/hw_cache.c @@ -513,6 +513,7 @@ static void FreeMipmapColormap(INT32 patchnum, void *patch) if (next->grInfo.data) Z_Free(next->grInfo.data); next->grInfo.data = NULL; + HWD.pfnDeleteTexture(next); // Free the old colormap from memory. free(next); diff --git a/src/hardware/hw_defs.h b/src/hardware/hw_defs.h index 4c682b2eb..7c21dc07e 100644 --- a/src/hardware/hw_defs.h +++ b/src/hardware/hw_defs.h @@ -207,7 +207,16 @@ enum ETextureFlags TF_TRANSPARENT = 0x00000040, // texture with some alpha == 0 }; -typedef struct GLMipmap_s FTextureInfo; +struct FTextureInfo +{ + UINT32 width, height; + UINT32 downloaded; + UINT32 format; + + struct GLMipmap_s *texture; + struct FTextureInfo *prev, *next; +}; +typedef struct FTextureInfo FTextureInfo; struct FLightInfo { diff --git a/src/hardware/hw_drv.h b/src/hardware/hw_drv.h index bcf627236..7a725b09a 100644 --- a/src/hardware/hw_drv.h +++ b/src/hardware/hw_drv.h @@ -38,8 +38,9 @@ EXPORT void HWRAPI(Draw2DLine) (F2DCoord *v1, F2DCoord *v2, RGBA_t Color); EXPORT void HWRAPI(DrawPolygon) (FSurfaceInfo *pSurf, FOutVector *pOutVerts, FUINT iNumPts, FBITFIELD PolyFlags, boolean horizonSpecial); EXPORT void HWRAPI(SetBlend) (FBITFIELD PolyFlags); EXPORT void HWRAPI(ClearBuffer) (FBOOLEAN ColorMask, FBOOLEAN DepthMask, FBOOLEAN StencilMask, FRGBAFloat *ClearColor); -EXPORT void HWRAPI(SetTexture) (FTextureInfo *TexInfo); -EXPORT void HWRAPI(UpdateTexture) (FTextureInfo *TexInfo); +EXPORT void HWRAPI(SetTexture) (GLMipmap_t *TexInfo); +EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *TexInfo); +EXPORT void HWRAPI(DeleteTexture) (GLMipmap_t *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, float farclip); EXPORT void HWRAPI(ClearMipMapCache) (void); @@ -102,6 +103,7 @@ struct hwdriver_s ClearBuffer pfnClearBuffer; SetTexture pfnSetTexture; UpdateTexture pfnUpdateTexture; + DeleteTexture pfnDeleteTexture; ReadRect pfnReadRect; GClipRect pfnGClipRect; ClearMipMapCache pfnClearMipMapCache; diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 18de50104..8411d9b80 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -79,8 +79,9 @@ static GLuint tex_downloaded = 0; static GLfloat fov = 90.0f; static FBITFIELD CurrentPolyFlags; -static FTextureInfo *gr_cachetail = NULL; -static FTextureInfo *gr_cachehead = NULL; +// Linked list of all textures. +static FTextureInfo *TexCacheTail = NULL; +static FTextureInfo *TexCacheHead = NULL; typedef struct LightTableCacheEntry_s { @@ -1541,6 +1542,39 @@ void SetStates(void) pglEnable(GL_STENCIL_TEST); } + +// -----------------+ +// DeleteTexture : Deletes a texture from the GPU and frees its data +// -----------------+ +EXPORT void HWRAPI(DeleteTexture) (GLMipmap_t *pTexInfo) +{ + FTextureInfo *head = TexCacheHead; + + if (!pTexInfo) + return; + else if (pTexInfo->downloaded) + pglDeleteTextures(1, (GLuint *)&pTexInfo->downloaded); + + while (head) + { + if (head->downloaded == pTexInfo->downloaded) + { + if (head->next) + head->next->prev = head->prev; + if (head->prev) + head->prev->next = head->next; + free(head); + break; + } + + head = head->next; + } + + pTexInfo->downloaded = 0; +} + + + // -----------------+ // Flush : flush OpenGL textures // : Clear list of downloaded mipmaps @@ -1549,21 +1583,30 @@ void Flush(void) { //GL_DBG_Printf("HWR_Flush()\n"); - while (gr_cachehead) + while (TexCacheHead) { - // this is not necessary at all, because you have loaded them normally, - // and so they already are in your list! - if (gr_cachehead->downloaded) - pglDeleteTextures(1, (GLuint *)&gr_cachehead->downloaded); - gr_cachehead->downloaded = 0; - gr_cachehead = gr_cachehead->nextmipmap; - } - gr_cachetail = gr_cachehead = NULL; //Hurdler: well, gr_cachehead is already NULL + FTextureInfo *pTexInfo = TexCacheHead; + GLMipmap_t *texture = pTexInfo->texture; + if (pTexInfo->downloaded) + { + pglDeleteTextures(1, (GLuint *)&pTexInfo->downloaded); + pTexInfo->downloaded = 0; + } + + if (texture) + texture->downloaded = 0; + + TexCacheHead = pTexInfo->next; + free(pTexInfo); + } + + TexCacheTail = TexCacheHead = NULL; //Hurdler: well, TexCacheHead is already NULL tex_downloaded = 0; } + // -----------------+ // isExtAvailable : Look if an OpenGL extension is available // Returns : true if extension available @@ -1979,7 +2022,7 @@ static void FreeTextureBuffer(RGBA_t *tex) // -----------------+ // UpdateTexture : Updates texture data. // -----------------+ -EXPORT void HWRAPI(UpdateTexture) (FTextureInfo *pTexInfo) +EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *pTexInfo) { // Upload a texture GLuint num = pTexInfo->downloaded; @@ -2180,7 +2223,7 @@ EXPORT void HWRAPI(UpdateTexture) (FTextureInfo *pTexInfo) // -----------------+ // SetTexture : The mipmap becomes the current texture source // -----------------+ -EXPORT void HWRAPI(SetTexture) (FTextureInfo *pTexInfo) +EXPORT void HWRAPI(SetTexture) (GLMipmap_t *pTexInfo) { if (!pTexInfo) { @@ -2197,15 +2240,25 @@ EXPORT void HWRAPI(SetTexture) (FTextureInfo *pTexInfo) } else { + FTextureInfo *newTex = calloc(1, sizeof (*newTex)); + UpdateTexture(pTexInfo); - pTexInfo->nextmipmap = NULL; - if (gr_cachetail) - { // insertion at the tail - gr_cachetail->nextmipmap = pTexInfo; - gr_cachetail = pTexInfo; + newTex->texture = pTexInfo; + newTex->downloaded = (UINT32)pTexInfo->downloaded; + newTex->width = (UINT32)pTexInfo->width; + newTex->height = (UINT32)pTexInfo->height; + newTex->format = (UINT32)pTexInfo->grInfo.format;; + + // insertion at the tail + if (TexCacheTail) + { + newTex->prev = TexCacheTail; + TexCacheTail->next = newTex; + TexCacheTail = newTex; + } else // initialization of the linked list - gr_cachetail = gr_cachehead = pTexInfo; + TexCacheTail = TexCacheHead = newTex; } } @@ -3866,7 +3919,7 @@ EXPORT void HWRAPI(SetTransform) (FTransform *stransform) EXPORT INT32 HWRAPI(GetTextureUsed) (void) { - FTextureInfo *tmp = gr_cachehead; + FTextureInfo *tmp = TexCacheHead; INT32 res = 0; while (tmp) @@ -3875,7 +3928,7 @@ EXPORT INT32 HWRAPI(GetTextureUsed) (void) // I don't know which one the game actually _uses_ but this // follows format2bpp in hw_cache.c int bpp = 1; - int format = tmp->grInfo.format; + int format = tmp->format; if (format == GR_RGBA) bpp = 4; else if (format == GR_TEXFMT_RGB_565 @@ -3887,7 +3940,7 @@ EXPORT INT32 HWRAPI(GetTextureUsed) (void) // Add it up! res += tmp->height*tmp->width*bpp; - tmp = tmp->nextmipmap; + tmp = tmp->next; } return res; diff --git a/src/sdl/hwsym_sdl.c b/src/sdl/hwsym_sdl.c index 4af37ee42..5755a4833 100644 --- a/src/sdl/hwsym_sdl.c +++ b/src/sdl/hwsym_sdl.c @@ -84,6 +84,7 @@ void *hwSym(const char *funcName,void *handle) GETFUNC(ClearBuffer); GETFUNC(SetTexture); GETFUNC(UpdateTexture); + GETFUNC(DeleteTexture); GETFUNC(ReadRect); GETFUNC(GClipRect); GETFUNC(ClearMipMapCache); diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 82e279093..39bcb3547 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -2119,6 +2119,7 @@ void I_StartupGraphics(void) HWD.pfnClearBuffer = hwSym("ClearBuffer",NULL); HWD.pfnSetTexture = hwSym("SetTexture",NULL); HWD.pfnUpdateTexture = hwSym("UpdateTexture",NULL); + HWD.pfnDeleteTexture = hwSym("DeleteTexture",NULL); HWD.pfnReadRect = hwSym("ReadRect",NULL); HWD.pfnGClipRect = hwSym("GClipRect",NULL); HWD.pfnClearMipMapCache = hwSym("ClearMipMapCache",NULL);