From 313f0174d7da0bbaf075cd7ce129b2eebf282662 Mon Sep 17 00:00:00 2001 From: kaisa847 <226997300+kaisa847@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:33:38 +0100 Subject: [PATCH] gsTexManager: stop evicting when a texture cannot fit in VRAM _blockAlloc() frees textures with a growing weight limit until a free block is large enough. The loop has no exit: when the texture is larger than all VRAM the manager owns (4 MiB minus frame and z buffers), or when gsKit_TexManager_init() was never called (__head is NULL), the loop keeps incrementing the weight forever. The program hangs in gsKit_TexManager_bind(), e.g. SDL binds every texture through it, so one oversized SDL texture freezes the app. Count the textures that are still in VRAM during each pass. When a pass finds nothing left to free and no block fits, return NULL. Make gsKit_TexManager_bind() handle NULL: print an error, clear tex->Vram and tex->VramClut and return 0 without uploading. Tested on real hardware (PSX DESR-5100 via ps2link): binding a 256x256 texture into 64 KiB of free VRAM hangs unpatched and returns with an error message with this change. Co-Authored-By: Claude Opus 4.8 --- ee/gs/src/gsTexManager.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ee/gs/src/gsTexManager.c b/ee/gs/src/gsTexManager.c index 228bcaa..502d245 100644 --- a/ee/gs/src/gsTexManager.c +++ b/ee/gs/src/gsTexManager.c @@ -168,6 +168,8 @@ _blockAlloc(unsigned int size) } while (block == NULL) { + unsigned int used = 0; + // Free blocks starting with the least used textures for (block = __head; block != NULL; block = block->pNext) { if ((block->tex != NULL) && (_blockGetWeight(block) <= weight)) { @@ -179,6 +181,15 @@ _blockAlloc(unsigned int size) break; } } + else if (block->tex != NULL) { + // Still in use, can be freed with a higher weight + used++; + } + } + + if ((block == NULL) && (used == 0)) { + // Nothing left to free, the texture does not fit in VRAM + break; } weight++; } @@ -248,6 +259,12 @@ gsKit_TexManager_bind(GSGLOBAL * gsGlobal, GSTEXTURE * tex) // Allocate new block if not already loaded if (block == NULL) { block = _blockAlloc(tsize + csize); + if (block == NULL) { + printf("gsKit: TexManager: texture does not fit in VRAM\n"); + tex->Vram = 0; + tex->VramClut = 0; + return 0; + } block->tex = tex; block->iUseCount = 0; block->iUseCountPrev = 1;