Skip to content

Commit

Permalink
Fix for incorrect bits per pixel set on texture reload (axmolengine#2138
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
rh101 authored and xfbird committed Sep 18, 2024
1 parent 4e37196 commit e38f7f4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/renderer/Texture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions core/renderer/TextureCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion core/renderer/TextureCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions core/renderer/backend/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions core/renderer/backend/opengl/TextureGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e38f7f4

Please sign in to comment.