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

Optimize textures map: store values to avoid malloc/free.

This commit is contained in:
Sergey Lipskiy 2014-09-22 18:40:35 +07:00
parent 44c9dffc54
commit a6056d4c3c
2 changed files with 51 additions and 60 deletions

View File

@ -261,16 +261,15 @@ void TextureCache::init()
void TextureCache::destroy() void TextureCache::destroy()
{ {
current[0] = current[1] = NULL; current[0] = current[1] = NULL;
for (Textures::iterator cur = m_textures.begin(); cur != m_textures.end(); ++cur) {
glDeleteTextures( 1, &cur->second->glName ); for (Textures::const_iterator cur = m_textures.cbegin(); cur != m_textures.cend(); ++cur)
free(cur->second); glDeleteTextures( 1, &cur->second.glName );
}
m_textures.clear(); m_textures.clear();
for (Textures::iterator cur = m_fbTextures.begin(); cur != m_fbTextures.end(); ++cur) {
glDeleteTextures( 1, &cur->second->glName ); for (Textures::const_iterator cur = m_fbTextures.cbegin(); cur != m_fbTextures.cend(); ++cur)
free(cur->second); glDeleteTextures( 1, &cur->second.glName );
}
m_fbTextures.clear(); m_fbTextures.clear();
m_cachedBytes = 0; m_cachedBytes = 0;
} }
@ -278,49 +277,42 @@ void TextureCache::_checkCacheSize()
{ {
if (m_cachedBytes <= m_maxBytes) if (m_cachedBytes <= m_maxBytes)
return; return;
Textures::iterator iter = m_textures.begin(); Textures::const_iterator iter = m_textures.cend();
do { do {
m_cachedBytes -= iter->second->textureBytes; --iter;
glDeleteTextures( 1, &iter->second->glName ); m_cachedBytes -= iter->second.textureBytes;
free(iter->second); glDeleteTextures( 1, &iter->second.glName );
++iter; } while (m_cachedBytes > m_maxBytes && iter != m_textures.cbegin());
} while (m_cachedBytes > m_maxBytes && iter != m_textures.end()); m_textures.erase(iter, m_textures.cend());
m_textures.erase(m_textures.begin(), iter);
}
CachedTexture * TextureCache::_allocateTexture()
{
CachedTexture *pTexture = (CachedTexture*)malloc(sizeof(CachedTexture));
memset(pTexture, 0, sizeof(CachedTexture));
glGenTextures(1, &pTexture->glName);
return pTexture;
} }
CachedTexture * TextureCache::_addTexture(u32 _crc32) CachedTexture * TextureCache::_addTexture(u32 _crc32)
{ {
_checkCacheSize(); _checkCacheSize();
CachedTexture *pTexture = _allocateTexture(); GLuint glName;
pTexture->crc = _crc32; glGenTextures(1, &glName);
m_textures[_crc32] = pTexture; m_textures.emplace(_crc32, glName);
return pTexture; CachedTexture & texture = m_textures.at(_crc32);
texture.crc = _crc32;
return &texture;
} }
void TextureCache::removeFrameBufferTexture(CachedTexture * _pTexture) void TextureCache::removeFrameBufferTexture(CachedTexture * _pTexture)
{ {
Textures::iterator iter = m_fbTextures.find(_pTexture->glName); Textures::const_iterator iter = m_fbTextures.find(_pTexture->glName);
assert(iter != m_fbTextures.end()); assert(iter != m_fbTextures.end());
m_cachedBytes -= iter->second->textureBytes; m_cachedBytes -= iter->second.textureBytes;
glDeleteTextures( 1, &iter->second->glName ); glDeleteTextures( 1, &iter->second.glName );
free(iter->second);
m_fbTextures.erase(iter); m_fbTextures.erase(iter);
} }
CachedTexture * TextureCache::addFrameBufferTexture() CachedTexture * TextureCache::addFrameBufferTexture()
{ {
_checkCacheSize(); _checkCacheSize();
CachedTexture *pTexture = _allocateTexture(); GLuint glName;
m_fbTextures[pTexture->glName] = pTexture; glGenTextures(1, &glName);
return pTexture; m_fbTextures.emplace(glName, glName);
return &m_fbTextures.at(glName);
} }
void TextureCache::_loadBackground( CachedTexture *pTexture ) void TextureCache::_loadBackground( CachedTexture *pTexture )
@ -608,15 +600,15 @@ void TextureCache::_updateBackground()
u32 params[4] = {gSP.bgImage.width, gSP.bgImage.height, gSP.bgImage.format, gSP.bgImage.size}; u32 params[4] = {gSP.bgImage.width, gSP.bgImage.height, gSP.bgImage.format, gSP.bgImage.size};
crc = CRC_Calculate(crc, params, sizeof(u32)*4); crc = CRC_Calculate(crc, params, sizeof(u32)*4);
Textures::const_iterator iter = m_textures.find(crc); Textures::iterator iter = m_textures.find(crc);
if (iter != m_textures.end()) { if (iter != m_textures.end()) {
CachedTexture * pCurrent = iter->second; CachedTexture & current = iter->second;
assert((pCurrent->width == gSP.bgImage.width) && assert((current.width == gSP.bgImage.width) &&
(pCurrent->height == gSP.bgImage.height) && (current.height == gSP.bgImage.height) &&
(pCurrent->format == gSP.bgImage.format) && (current.format == gSP.bgImage.format) &&
(pCurrent->size == gSP.bgImage.size)); (current.size == gSP.bgImage.size));
activateTexture(0, pCurrent); activateTexture(0, &current);
m_hits++; m_hits++;
return; return;
} }
@ -824,24 +816,24 @@ void TextureCache::update(u32 _t)
return; return;
} }
Textures::const_iterator iter = m_textures.find(crc); Textures::iterator iter = m_textures.find(crc);
if (iter != m_textures.end()) { if (iter != m_textures.end()) {
CachedTexture * pCurrent = iter->second; CachedTexture & current = iter->second;
assert((pCurrent->width == width) && assert((current.width == width) &&
(pCurrent->height == height) && (current.height == height) &&
(pCurrent->clampWidth == clampWidth) && (current.clampWidth == clampWidth) &&
(pCurrent->clampHeight == clampHeight) && (current.clampHeight == clampHeight) &&
(pCurrent->maskS == gSP.textureTile[_t]->masks) && (current.maskS == gSP.textureTile[_t]->masks) &&
(pCurrent->maskT == gSP.textureTile[_t]->maskt) && (current.maskT == gSP.textureTile[_t]->maskt) &&
(pCurrent->mirrorS == gSP.textureTile[_t]->mirrors) && (current.mirrorS == gSP.textureTile[_t]->mirrors) &&
(pCurrent->mirrorT == gSP.textureTile[_t]->mirrort) && (current.mirrorT == gSP.textureTile[_t]->mirrort) &&
(pCurrent->clampS == gSP.textureTile[_t]->clamps) && (current.clampS == gSP.textureTile[_t]->clamps) &&
(pCurrent->clampT == gSP.textureTile[_t]->clampt) && (current.clampT == gSP.textureTile[_t]->clampt) &&
(pCurrent->format == gSP.textureTile[_t]->format) && (current.format == gSP.textureTile[_t]->format) &&
(pCurrent->size == gSP.textureTile[_t]->size) (current.size == gSP.textureTile[_t]->size)
); );
activateTexture(_t, pCurrent); activateTexture(_t, &current);
m_hits++; m_hits++;
return; return;
} }

View File

@ -13,6 +13,8 @@
struct CachedTexture struct CachedTexture
{ {
CachedTexture(GLuint _glName) : glName(_glName) {}
GLuint glName; GLuint glName;
u32 crc; u32 crc;
// float fulS, fulT; // float fulS, fulT;
@ -34,10 +36,8 @@ struct CachedTexture
u32 textureBytes; u32 textureBytes;
u32 frameBufferTexture; u32 frameBufferTexture;
CachedTexture *lower, *higher;
u32 lastDList; u32 lastDList;
u32 address; u32 address;
}; };
@ -68,13 +68,12 @@ private:
TextureCache(const TextureCache &); TextureCache(const TextureCache &);
void _checkCacheSize(); void _checkCacheSize();
CachedTexture * _allocateTexture();
CachedTexture * _addTexture(u32 _crc32); CachedTexture * _addTexture(u32 _crc32);
void _load(CachedTexture *pTexture); void _load(CachedTexture *pTexture);
void _loadBackground(CachedTexture *pTexture); void _loadBackground(CachedTexture *pTexture);
void _updateBackground(); void _updateBackground();
typedef std::map<u32, CachedTexture *> Textures; typedef std::map<u32, CachedTexture> Textures;
Textures m_textures; Textures m_textures;
Textures m_fbTextures; Textures m_fbTextures;
CachedTexture * m_pDummy; CachedTexture * m_pDummy;