Fix edge case with reference counting.

This commit is contained in:
Dario 2024-07-27 00:06:11 -03:00
parent ab7d96f609
commit efbda3f0ee

View File

@ -210,6 +210,7 @@ namespace RT64 {
for (size_t i = 0; i < textureReplacements.size(); i++) { for (size_t i = 0; i < textureReplacements.size(); i++) {
if (textureReplacements[i] != nullptr) { if (textureReplacements[i] != nullptr) {
textureReplacements[i] = nullptr; textureReplacements[i] = nullptr;
textureReplacementReferenceCounted[i] = false;
versions[i]++; versions[i]++;
} }
} }
@ -258,6 +259,17 @@ namespace RT64 {
return; 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]; Texture *replacedTexture = textures[it->second];
textureReplacements[it->second] = texture; textureReplacements[it->second] = texture;
textureReplacementReferenceCounted[it->second] = referenceCounted; textureReplacementReferenceCounted[it->second] = referenceCounted;