From efbda3f0ee3b157465f1c75ee49fd7275cf2d6ce Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 27 Jul 2024 00:06:11 -0300 Subject: [PATCH] Fix edge case with reference counting. --- src/render/rt64_texture_cache.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/render/rt64_texture_cache.cpp b/src/render/rt64_texture_cache.cpp index 309a70a..1f267a7 100644 --- a/src/render/rt64_texture_cache.cpp +++ b/src/render/rt64_texture_cache.cpp @@ -210,6 +210,7 @@ namespace RT64 { for (size_t i = 0; i < textureReplacements.size(); i++) { if (textureReplacements[i] != nullptr) { textureReplacements[i] = nullptr; + textureReplacementReferenceCounted[i] = false; versions[i]++; } } @@ -258,6 +259,17 @@ namespace RT64 { return; } + // Do nothing if it's the same texture. If the operations were done correctly, it's not possible for either the texture or the + // replacement to be any different, so the operation can be considered a no-op. + if (texture == textureReplacements[it->second]) { + return; + } + + // If it's a different texture and a texture was already replaced, decrement its reference. + if ((textureReplacements[it->second] != nullptr) && textureReplacementReferenceCounted[it->second]) { + replacementMap.decrementReference(textureReplacements[it->second]); + } + Texture *replacedTexture = textures[it->second]; textureReplacements[it->second] = texture; textureReplacementReferenceCounted[it->second] = referenceCounted;