1
0
mirror of https://github.com/blawar/GLideN64.git synced 2024-07-04 10:03:36 +00:00

[Code refactor] Correct TextureCache::_load:

add RAII holder for texture data.
This commit is contained in:
Sergey Lipskiy 2020-08-28 11:36:59 +07:00
parent 1111a7636c
commit 76d72f77b7

View File

@ -1083,8 +1083,6 @@ void TextureCache::_load(u32 _tile, CachedTexture *_pTexture)
if (_loadHiresTexture(_tile, _pTexture, ricecrc)) if (_loadHiresTexture(_tile, _pTexture, ricecrc))
return; return;
u32 *pDest;
u16 line; u16 line;
GetTexelFunc GetTexel; GetTexelFunc GetTexel;
InternalColorFormatParam glInternalFormat; InternalColorFormatParam glInternalFormat;
@ -1107,8 +1105,28 @@ void TextureCache::_load(u32 _tile, CachedTexture *_pTexture)
glType = loadParams.glType16; glType = loadParams.glType16;
} }
pDest = (u32*)malloc(_pTexture->textureBytes); // RAII holder for texture data
assert(pDest != nullptr); class TexData
{
public:
TexData(u32 bytes)
{
pData = (u32*)malloc(bytes);
assert(pData != NULL);
}
~TexData()
{
free(pData);
pData = NULL;
}
u32 * get() const
{
return pData;
}
private:
u32 *pData = NULL;
} texData(_pTexture->textureBytes);
s32 mipLevel = 0; s32 mipLevel = 0;
_pTexture->max_level = 0; _pTexture->max_level = 0;
@ -1131,18 +1149,17 @@ void TextureCache::_load(u32 _tile, CachedTexture *_pTexture)
line = tmptex.line; line = tmptex.line;
while (true) { while (true) {
_getTextureDestData(tmptex, pDest, glInternalFormat, GetTexel, &line); _getTextureDestData(tmptex, texData.get(), glInternalFormat, GetTexel, &line);
if ((config.generalEmulation.hacks&hack_LoadDepthTextures) != 0 && gDP.colorImage.address == gDP.depthImageAddress) { if ((config.generalEmulation.hacks&hack_LoadDepthTextures) != 0 && gDP.colorImage.address == gDP.depthImageAddress) {
_loadDepthTexture(_pTexture, (u16*)pDest); _loadDepthTexture(_pTexture, (u16*)texData.get());
free(pDest);
return; return;
} }
if (m_toggleDumpTex && if (m_toggleDumpTex &&
config.textureFilter.txHiresEnable != 0 && config.textureFilter.txHiresEnable != 0 &&
config.textureFilter.txDump != 0) { config.textureFilter.txDump != 0) {
txfilter_dmptx((u8*)pDest, tmptex.width, tmptex.height, txfilter_dmptx((u8*)texData.get(), tmptex.width, tmptex.height,
tmptex.width, (u16)u32(glInternalFormat), tmptex.width, (u16)u32(glInternalFormat),
(unsigned short)(_pTexture->format << 8 | _pTexture->size), (unsigned short)(_pTexture->format << 8 | _pTexture->size),
ricecrc); ricecrc);
@ -1169,7 +1186,7 @@ void TextureCache::_load(u32 _tile, CachedTexture *_pTexture)
if (needEnhance) { if (needEnhance) {
GHQTexInfo ghqTexInfo; GHQTexInfo ghqTexInfo;
if (txfilter_filter((u8*)pDest, tmptex.width, tmptex.height, if (txfilter_filter((u8*)texData.get(), tmptex.width, tmptex.height,
(u16)u32(glInternalFormat), (uint64)_pTexture->crc, (u16)u32(glInternalFormat), (uint64)_pTexture->crc,
&ghqTexInfo) != 0 && ghqTexInfo.data != nullptr) { &ghqTexInfo) != 0 && ghqTexInfo.data != nullptr) {
if (ghqTexInfo.width % 2 != 0 && if (ghqTexInfo.width % 2 != 0 &&
@ -1209,7 +1226,7 @@ void TextureCache::_load(u32 _tile, CachedTexture *_pTexture)
params.internalFormat = gfxContext.convertInternalTextureFormat(u32(glInternalFormat)); params.internalFormat = gfxContext.convertInternalTextureFormat(u32(glInternalFormat));
params.format = colorFormat::RGBA; params.format = colorFormat::RGBA;
params.dataType = glType; params.dataType = glType;
params.data = pDest; params.data = texData.get();
gfxContext.init2DTexture(params); gfxContext.init2DTexture(params);
} }
if (mipLevel == _pTexture->max_level) if (mipLevel == _pTexture->max_level)
@ -1235,7 +1252,6 @@ void TextureCache::_load(u32 _tile, CachedTexture *_pTexture)
} }
if (m_curUnpackAlignment > 1) if (m_curUnpackAlignment > 1)
gfxContext.setTextureUnpackAlignment(m_curUnpackAlignment); gfxContext.setTextureUnpackAlignment(m_curUnpackAlignment);
free(pDest);
} }
struct TextureParams struct TextureParams