From e38f7f48d84c95dcbaf082756a7cded5b6b6883a Mon Sep 17 00:00:00 2001 From: RH Date: Fri, 13 Sep 2024 22:53:31 +1000 Subject: [PATCH] Fix for incorrect bits per pixel set on texture reload (#2138) * Set pixel format of cached FPS texture to correct value * Do not change _bitsPerPixel if it is a valid value * Only set _bitsPerPixel if it is the default value of 0 * Assert that size is not equal to 0 Move width, height and bitsperpixel clamping * Deprecate findVolotileTexture and replace with getOrAddVolatileTexture. * Add missing static specifier * Replace calls to findVolotileTexture with getOrAddVolatileTexture --- core/renderer/Texture2D.cpp | 2 +- core/renderer/TextureCache.cpp | 10 +++++----- core/renderer/TextureCache.h | 3 ++- core/renderer/backend/Texture.cpp | 9 +++++++-- core/renderer/backend/opengl/TextureGL.cpp | 7 ++----- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/core/renderer/Texture2D.cpp b/core/renderer/Texture2D.cpp index 245acc22d6e7..fdbb1c331f80 100644 --- a/core/renderer/Texture2D.cpp +++ b/core/renderer/Texture2D.cpp @@ -333,7 +333,7 @@ bool Texture2D::updateWithMipmaps(MipmapInfo* mipmaps, } #if AX_ENABLE_CACHE_TEXTURE_DATA - VolatileTextureMgr::findVolotileTexture(this); + VolatileTextureMgr::getOrAddVolatileTexture(this); #endif backend::TextureDescriptor textureDescriptor; diff --git a/core/renderer/TextureCache.cpp b/core/renderer/TextureCache.cpp index 95af7b87dcf9..5a832dbfbb47 100644 --- a/core/renderer/TextureCache.cpp +++ b/core/renderer/TextureCache.cpp @@ -837,7 +837,7 @@ void VolatileTextureMgr::addImageTexture(Texture2D* tt, std::string_view imageFi return; } - VolatileTexture* vt = findVolotileTexture(tt); + VolatileTexture* vt = getOrAddVolatileTexture(tt); vt->_cashedImageType = VolatileTexture::kImageFile; vt->_fileName = imageFileName; @@ -849,7 +849,7 @@ void VolatileTextureMgr::addImage(Texture2D* tt, Image* image) if (tt == nullptr || image == nullptr) return; - VolatileTexture* vt = findVolotileTexture(tt); + VolatileTexture* vt = getOrAddVolatileTexture(tt); if(vt->_uiImage != image) { AX_SAFE_RELEASE(vt->_uiImage); @@ -860,7 +860,7 @@ void VolatileTextureMgr::addImage(Texture2D* tt, Image* image) } } -VolatileTexture* VolatileTextureMgr::findVolotileTexture(Texture2D* tt) +VolatileTexture* VolatileTextureMgr::getOrAddVolatileTexture(Texture2D* tt) { VolatileTexture* vt = nullptr; for (const auto& texture : _textures) @@ -893,7 +893,7 @@ void VolatileTextureMgr::addDataTexture(Texture2D* tt, return; } - VolatileTexture* vt = findVolotileTexture(tt); + VolatileTexture* vt = getOrAddVolatileTexture(tt); vt->_cashedImageType = VolatileTexture::kImageData; vt->_textureData = data; @@ -909,7 +909,7 @@ void VolatileTextureMgr::addStringTexture(Texture2D* tt, std::string_view text, return; } - VolatileTexture* vt = findVolotileTexture(tt); + VolatileTexture* vt = getOrAddVolatileTexture(tt); vt->_cashedImageType = VolatileTexture::kString; vt->_text = text; diff --git a/core/renderer/TextureCache.h b/core/renderer/TextureCache.h index 842328c884de..7c494c64e95f 100644 --- a/core/renderer/TextureCache.h +++ b/core/renderer/TextureCache.h @@ -312,7 +312,8 @@ class AX_DLL VolatileTextureMgr // find VolatileTexture by Texture2D* // if not found, create a new one - static VolatileTexture* findVolotileTexture(Texture2D* tt); + AX_DEPRECATED(2.2) static VolatileTexture* findVolotileTexture(Texture2D* tt) { return getOrAddVolatileTexture(tt); } + static VolatileTexture* getOrAddVolatileTexture(Texture2D* tt); private: static void reloadTexture(Texture2D* texture, std::string_view filename, backend::PixelFormat pixelFormat); diff --git a/core/renderer/backend/Texture.cpp b/core/renderer/backend/Texture.cpp index f179e31b1cea..6fd726e609df 100644 --- a/core/renderer/backend/Texture.cpp +++ b/core/renderer/backend/Texture.cpp @@ -36,8 +36,13 @@ void TextureBackend::updateTextureDescriptor(const ax::backend::TextureDescripto _textureType = descriptor.textureType; _textureFormat = descriptor.textureFormat; _textureUsage = descriptor.textureUsage; - _width = descriptor.width; - _height = descriptor.height; + _width = (std::max)(descriptor.width, (uint32_t)1); + _height = (std::max)(descriptor.height, (uint32_t)1); + + if (_bitsPerPixel == 0) + { + _bitsPerPixel = (uint8_t)(8 * 4); + } } NS_AX_BACKEND_END diff --git a/core/renderer/backend/opengl/TextureGL.cpp b/core/renderer/backend/opengl/TextureGL.cpp index 781dcd1480f9..47018964d3ec 100644 --- a/core/renderer/backend/opengl/TextureGL.cpp +++ b/core/renderer/backend/opengl/TextureGL.cpp @@ -168,12 +168,9 @@ void Texture2DGL::initWithZeros() // FIXME: Don't call at Texture2DGL::updateTextureDescriptor, when the texture is compressed, initWithZeros will // cause GL Error: 0x501 We call at here once to ensure depth buffer works well. Ensure the final data size at least // 4 byte - - _width = (std::max)(_width, (uint32_t)1); - _height = (std::max)(_height, (uint32_t)1); - _bitsPerPixel = (std::max)(_bitsPerPixel, (uint8_t)(8 * 4)); - auto size = _width * _height * _bitsPerPixel / 8; + assert(size > 0); + uint8_t* data = (uint8_t*)malloc(size); memset(data, 0, size); updateData(data, _width, _height, 0);