project64/Source/GlideHQ/TxCache.cpp

440 lines
14 KiB
C++
Raw Normal View History

2013-04-17 10:30:38 +00:00
/*
* Texture Filtering
* Version: 1.0
*
* Copyright (C) 2007 Hiroshi Morii All Rights Reserved.
* Email koolsmoky(at)users.sourceforge.net
* Web http://www.3dfxzone.it/koolsmoky
*
* this is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* this is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef WIN32
#pragma warning(disable: 4786)
#endif
#include "TxCache.h"
#include "TxDbg.h"
#include <zlib/zlib.h>
#include <Common/path.h>
#include <Common/StdString.h>
2013-04-17 10:30:38 +00:00
TxCache::~TxCache()
{
2016-02-04 17:28:21 +00:00
/* free memory, clean up, etc */
clear();
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
delete _txUtil;
2013-04-17 10:30:38 +00:00
}
2016-02-10 07:02:20 +00:00
TxCache::TxCache(int options, int cachesize, const char *path, const char *ident,
2016-02-04 17:28:21 +00:00
dispInfoFuncExt callback)
2013-04-17 10:30:38 +00:00
{
2016-02-04 17:28:21 +00:00
_txUtil = new TxUtil();
_options = options;
_cacheSize = cachesize;
_callback = callback;
_totalSize = 0;
/* save path name */
if (path)
2016-02-10 07:02:20 +00:00
{
2016-02-04 17:28:21 +00:00
_path.assign(path);
2016-02-10 07:02:20 +00:00
}
2016-02-04 17:28:21 +00:00
/* save ROM name */
if (ident)
2016-02-10 07:02:20 +00:00
{
2016-02-04 17:28:21 +00:00
_ident.assign(ident);
2016-02-10 07:02:20 +00:00
}
2016-02-04 17:28:21 +00:00
/* zlib memory buffers to (de)compress hires textures */
2016-02-10 07:02:20 +00:00
if (_options & (GZ_TEXCACHE | GZ_HIRESTEXCACHE))
{
2016-02-04 17:28:21 +00:00
_gzdest0 = TxMemBuf::getInstance()->get(0);
_gzdest1 = TxMemBuf::getInstance()->get(1);
_gzdestLen = (TxMemBuf::getInstance()->size_of(0) < TxMemBuf::getInstance()->size_of(1)) ?
TxMemBuf::getInstance()->size_of(0) : TxMemBuf::getInstance()->size_of(1);
2016-02-10 07:02:20 +00:00
if (!_gzdest0 || !_gzdest1 || !_gzdestLen)
{
2016-02-04 17:28:21 +00:00
_options &= ~(GZ_TEXCACHE | GZ_HIRESTEXCACHE);
_gzdest0 = NULL;
_gzdest1 = NULL;
_gzdestLen = 0;
}
2013-04-17 10:30:38 +00:00
}
}
boolean
TxCache::add(uint64 checksum, GHQTexInfo *info, int dataSize)
{
2016-02-04 17:28:21 +00:00
/* NOTE: dataSize must be provided if info->data is zlib compressed. */
if (!checksum || !info->data) return 0;
uint8 *dest = info->data;
uint16 format = info->format;
2016-02-10 07:02:20 +00:00
if (!dataSize)
{
2016-02-04 17:28:21 +00:00
dataSize = _txUtil->sizeofTx(info->width, info->height, info->format);
if (!dataSize) return 0;
2016-02-10 07:02:20 +00:00
if (_options & (GZ_TEXCACHE | GZ_HIRESTEXCACHE))
{
2016-02-04 17:28:21 +00:00
/* zlib compress it. compression level:1 (best speed) */
uLongf destLen = _gzdestLen;
dest = (dest == _gzdest0) ? _gzdest1 : _gzdest0;
2016-02-10 07:02:20 +00:00
if (compress2(dest, &destLen, info->data, dataSize, 1) != Z_OK)
{
2016-02-04 17:28:21 +00:00
dest = info->data;
DBG_INFO(80, "Error: zlib compression failed!\n");
2016-02-04 17:28:21 +00:00
}
2016-02-10 07:02:20 +00:00
else
{
DBG_INFO(80, "zlib compressed: %.02fkb->%.02fkb\n", (float)dataSize / 1000, (float)destLen / 1000);
2016-02-04 17:28:21 +00:00
dataSize = destLen;
format |= GR_TEXFMT_GZ;
}
2013-04-17 10:30:38 +00:00
}
2016-02-04 17:28:21 +00:00
}
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
/* if cache size exceeds limit, remove old cache */
2016-02-10 07:02:20 +00:00
if (_cacheSize > 0)
{
2016-02-04 17:28:21 +00:00
_totalSize += dataSize;
2016-02-10 07:02:20 +00:00
if ((_totalSize > _cacheSize) && !_cachelist.empty())
{
2016-02-04 17:28:21 +00:00
/* _cachelist is arranged so that frequently used textures are in the back */
std::list<uint64>::iterator itList = _cachelist.begin();
2016-02-10 07:02:20 +00:00
while (itList != _cachelist.end())
{
2016-02-04 17:28:21 +00:00
/* find it in _cache */
std::map<uint64, TXCACHE*>::iterator itMap = _cache.find(*itList);
2016-02-10 07:02:20 +00:00
if (itMap != _cache.end())
{
2016-02-04 17:28:21 +00:00
/* yep we have it. remove it. */
_totalSize -= (*itMap).second->size;
free((*itMap).second->info.data);
delete (*itMap).second;
_cache.erase(itMap);
}
itList++;
/* check if memory cache has enough space */
if (_totalSize <= _cacheSize)
break;
}
/* remove from _cachelist */
_cachelist.erase(_cachelist.begin(), itList);
2013-04-17 10:30:38 +00:00
DBG_INFO(80, "+++++++++\n");
2016-02-04 17:28:21 +00:00
}
_totalSize -= dataSize;
2013-04-17 10:30:38 +00:00
}
2016-02-04 17:28:21 +00:00
/* cache it */
uint8 *tmpdata = (uint8*)malloc(dataSize);
2016-02-10 07:02:20 +00:00
if (tmpdata)
{
2016-02-04 17:28:21 +00:00
TXCACHE *txCache = new TXCACHE;
2016-02-10 07:02:20 +00:00
if (txCache)
{
2016-02-04 17:28:21 +00:00
/* we can directly write as we filter, but for now we get away
* with doing memcpy after all the filtering is done.
*/
memcpy(tmpdata, dest, dataSize);
/* copy it */
memcpy(&txCache->info, info, sizeof(GHQTexInfo));
txCache->info.data = tmpdata;
txCache->info.format = format;
txCache->size = dataSize;
/* add to cache */
2016-02-10 07:02:20 +00:00
if (_cacheSize > 0)
{
2016-02-04 17:28:21 +00:00
_cachelist.push_back(checksum);
txCache->it = --(_cachelist.end());
}
/* _cache[checksum] = txCache; */
_cache.insert(std::map<uint64, TXCACHE*>::value_type(checksum, txCache));
2013-04-17 10:30:38 +00:00
#ifdef DEBUG
DBG_INFO(80, "[%5d] added!! crc:%08X %08X %d x %d gfmt:%x total:%.02fmb\n",
2016-02-04 17:28:21 +00:00
_cache.size(), (uint32)(checksum >> 32), (uint32)(checksum & 0xffffffff),
2016-02-10 07:02:20 +00:00
info->width, info->height, info->format, (float)_totalSize / 1000000);
2013-04-17 10:30:38 +00:00
DBG_INFO(80, "smalllodlog2:%d largelodlog2:%d aspectratiolog2:%d\n",
2016-02-04 17:28:21 +00:00
txCache->info.smallLodLog2, txCache->info.largeLodLog2, txCache->info.aspectRatioLog2);
2013-04-17 10:30:38 +00:00
2016-02-10 07:02:20 +00:00
if (info->tiles)
{
DBG_INFO(80, "tiles:%d un-tiled size:%d x %d\n", info->tiles, info->untiled_width, info->untiled_height);
2016-02-04 17:28:21 +00:00
}
2013-04-17 10:30:38 +00:00
2016-02-10 07:02:20 +00:00
if (_cacheSize > 0)
{
DBG_INFO(80, "cache max config:%.02fmb\n", (float)_cacheSize / 1000000);
2013-04-17 10:30:38 +00:00
2016-02-10 07:02:20 +00:00
if (_cache.size() != _cachelist.size())
{
DBG_INFO(80, "Error: cache/cachelist mismatch! (%d/%d)\n", _cache.size(), _cachelist.size());
2016-02-04 17:28:21 +00:00
}
2016-02-10 07:02:20 +00:00
}
2013-04-17 10:30:38 +00:00
#endif
2016-02-04 17:28:21 +00:00
/* total cache size */
_totalSize += dataSize;
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
return 1;
2016-02-10 07:02:20 +00:00
}
2016-02-04 17:28:21 +00:00
free(tmpdata);
2016-02-10 07:02:20 +00:00
}
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
return 0;
2013-04-17 10:30:38 +00:00
}
boolean
TxCache::get(uint64 checksum, GHQTexInfo *info)
{
2016-02-04 17:28:21 +00:00
if (!checksum || _cache.empty()) return 0;
/* find a match in cache */
std::map<uint64, TXCACHE*>::iterator itMap = _cache.find(checksum);
2016-02-10 07:02:20 +00:00
if (itMap != _cache.end())
{
2016-02-04 17:28:21 +00:00
/* yep, we've got it. */
memcpy(info, &(((*itMap).second)->info), sizeof(GHQTexInfo));
/* push it to the back of the list */
2016-02-10 07:02:20 +00:00
if (_cacheSize > 0)
{
2016-02-04 17:28:21 +00:00
_cachelist.erase(((*itMap).second)->it);
_cachelist.push_back(checksum);
((*itMap).second)->it = --(_cachelist.end());
}
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
/* zlib decompress it */
2016-02-10 07:02:20 +00:00
if (info->format & GR_TEXFMT_GZ)
{
2016-02-04 17:28:21 +00:00
uLongf destLen = _gzdestLen;
uint8 *dest = (_gzdest0 == info->data) ? _gzdest1 : _gzdest0;
2016-02-10 07:02:20 +00:00
if (uncompress(dest, &destLen, info->data, ((*itMap).second)->size) != Z_OK)
{
DBG_INFO(80, "Error: zlib decompression failed!\n");
2016-02-04 17:28:21 +00:00
return 0;
}
info->data = dest;
info->format &= ~GR_TEXFMT_GZ;
DBG_INFO(80, "zlib decompressed: %.02fkb->%.02fkb\n", (float)(((*itMap).second)->size) / 1000, (float)destLen / 1000);
2016-02-04 17:28:21 +00:00
}
return 1;
2013-04-17 10:30:38 +00:00
}
2016-02-04 17:28:21 +00:00
return 0;
2013-04-17 10:30:38 +00:00
}
2016-02-10 07:02:20 +00:00
boolean TxCache::save(const char *path, const char *filename, int config)
2013-04-17 10:30:38 +00:00
{
2016-02-10 07:02:20 +00:00
if (!_cache.empty())
{
CPath(path, "").DirectoryCreate();
2013-04-17 10:30:38 +00:00
2016-02-10 07:02:20 +00:00
gzFile gzfp = gzopen(CPath(path, filename), "wb1");
DBG_INFO(80, "gzfp:%x file:%ls\n", gzfp, filename);
2016-02-10 07:02:20 +00:00
if (gzfp)
{
2016-02-04 17:28:21 +00:00
/* write header to determine config match */
gzwrite(gzfp, &config, 4);
std::map<uint64, TXCACHE*>::iterator itMap = _cache.begin();
2016-02-10 07:02:20 +00:00
while (itMap != _cache.end())
{
2016-02-04 17:28:21 +00:00
uint8 *dest = (*itMap).second->info.data;
uint32 destLen = (*itMap).second->size;
uint16 format = (*itMap).second->info.format;
/* to keep things simple, we save the texture data in a zlib uncompressed state. */
/* sigh... for those who cannot wait the extra few seconds. changed to keep
* texture data in a zlib compressed state. if the GZ_TEXCACHE or GZ_HIRESTEXCACHE
* option is toggled, the cache will need to be rebuilt.
*/
/*if (format & GR_TEXFMT_GZ) {
dest = _gzdest0;
destLen = _gzdestLen;
if (dest && destLen) {
if (uncompress(dest, &destLen, (*itMap).second->info.data, (*itMap).second->size) != Z_OK) {
dest = NULL;
destLen = 0;
}
format &= ~GR_TEXFMT_GZ;
}
}*/
2016-02-10 07:02:20 +00:00
if (dest && destLen)
{
2016-02-04 17:28:21 +00:00
/* texture checksum */
gzwrite(gzfp, &((*itMap).first), 8);
/* other texture info */
gzwrite(gzfp, &((*itMap).second->info.width), 4);
gzwrite(gzfp, &((*itMap).second->info.height), 4);
gzwrite(gzfp, &format, 2);
gzwrite(gzfp, &((*itMap).second->info.smallLodLog2), 4);
gzwrite(gzfp, &((*itMap).second->info.largeLodLog2), 4);
gzwrite(gzfp, &((*itMap).second->info.aspectRatioLog2), 4);
gzwrite(gzfp, &((*itMap).second->info.tiles), 4);
gzwrite(gzfp, &((*itMap).second->info.untiled_width), 4);
gzwrite(gzfp, &((*itMap).second->info.untiled_height), 4);
gzwrite(gzfp, &((*itMap).second->info.is_hires_tex), 1);
gzwrite(gzfp, &destLen, 4);
gzwrite(gzfp, dest, destLen);
}
itMap++;
/* not ready yet */
/*if (_callback)
(*_callback)("Total textures saved to HDD: %d\n", std::distance(itMap, _cache.begin()));*/
2013-04-17 10:30:38 +00:00
}
2016-02-04 17:28:21 +00:00
gzclose(gzfp);
2013-04-17 10:30:38 +00:00
}
}
2016-02-04 17:28:21 +00:00
return _cache.empty();
2013-04-17 10:30:38 +00:00
}
2016-02-10 07:02:20 +00:00
boolean TxCache::load(const char *path, const char *filename, int config)
2013-04-17 10:30:38 +00:00
{
2016-02-04 17:28:21 +00:00
/* find it on disk */
2016-02-10 07:02:20 +00:00
CPath cbuf(path, filename);
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
gzFile gzfp = gzopen(cbuf, "rb");
DBG_INFO(80, "gzfp:%x file:%ls\n", gzfp, filename);
2016-02-10 07:02:20 +00:00
if (gzfp)
{
2016-02-04 17:28:21 +00:00
/* yep, we have it. load it into memory cache. */
int dataSize;
uint64 checksum;
GHQTexInfo tmpInfo;
int tmpconfig;
/* read header to determine config match */
gzread(gzfp, &tmpconfig, 4);
2016-02-10 07:02:20 +00:00
if (tmpconfig == config)
{
do
{
2016-02-04 17:28:21 +00:00
memset(&tmpInfo, 0, sizeof(GHQTexInfo));
gzread(gzfp, &checksum, 8);
gzread(gzfp, &tmpInfo.width, 4);
gzread(gzfp, &tmpInfo.height, 4);
gzread(gzfp, &tmpInfo.format, 2);
gzread(gzfp, &tmpInfo.smallLodLog2, 4);
gzread(gzfp, &tmpInfo.largeLodLog2, 4);
gzread(gzfp, &tmpInfo.aspectRatioLog2, 4);
gzread(gzfp, &tmpInfo.tiles, 4);
gzread(gzfp, &tmpInfo.untiled_width, 4);
gzread(gzfp, &tmpInfo.untiled_height, 4);
gzread(gzfp, &tmpInfo.is_hires_tex, 1);
gzread(gzfp, &dataSize, 4);
tmpInfo.data = (uint8*)malloc(dataSize);
2016-02-10 07:02:20 +00:00
if (tmpInfo.data)
{
2016-02-04 17:28:21 +00:00
gzread(gzfp, tmpInfo.data, dataSize);
/* add to memory cache */
add(checksum, &tmpInfo, (tmpInfo.format & GR_TEXFMT_GZ) ? dataSize : 0);
free(tmpInfo.data);
}
2016-02-10 07:02:20 +00:00
else
{
2016-02-04 17:28:21 +00:00
gzseek(gzfp, dataSize, SEEK_CUR);
}
/* skip in between to prevent the loop from being tied down to vsync */
if (_callback && (!(_cache.size() % 100) || gzeof(gzfp)))
(*_callback)("[%d] total mem:%.02fmb - %ls\n", _cache.size(), (float)_totalSize / 1000000, filename);
2016-02-04 17:28:21 +00:00
} while (!gzeof(gzfp));
gzclose(gzfp);
2013-04-17 10:30:38 +00:00
}
}
2016-02-04 17:28:21 +00:00
return !_cache.empty();
2013-04-17 10:30:38 +00:00
}
2016-02-10 07:02:20 +00:00
boolean TxCache::del(uint64 checksum)
2013-04-17 10:30:38 +00:00
{
2016-02-04 17:28:21 +00:00
if (!checksum || _cache.empty()) return 0;
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
std::map<uint64, TXCACHE*>::iterator itMap = _cache.find(checksum);
2016-02-10 07:02:20 +00:00
if (itMap != _cache.end())
{
2016-02-04 17:28:21 +00:00
/* for texture cache (not hi-res cache) */
if (!_cachelist.empty()) _cachelist.erase(((*itMap).second)->it);
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
/* remove from cache */
free((*itMap).second->info.data);
_totalSize -= (*itMap).second->size;
delete (*itMap).second;
_cache.erase(itMap);
2013-04-17 10:30:38 +00:00
DBG_INFO(80, "removed from cache: checksum = %08X %08X\n", (uint32)(checksum & 0xffffffff), (uint32)(checksum >> 32));
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
return 1;
}
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
return 0;
2013-04-17 10:30:38 +00:00
}
2016-02-10 07:02:20 +00:00
boolean TxCache::is_cached(uint64 checksum)
2013-04-17 10:30:38 +00:00
{
2016-02-04 17:28:21 +00:00
std::map<uint64, TXCACHE*>::iterator itMap = _cache.find(checksum);
if (itMap != _cache.end()) return 1;
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
return 0;
2013-04-17 10:30:38 +00:00
}
2016-02-10 07:02:20 +00:00
void TxCache::clear()
2013-04-17 10:30:38 +00:00
{
2016-02-10 07:02:20 +00:00
if (!_cache.empty())
{
2016-02-04 17:28:21 +00:00
std::map<uint64, TXCACHE*>::iterator itMap = _cache.begin();
2016-02-10 07:02:20 +00:00
while (itMap != _cache.end())
{
2016-02-04 17:28:21 +00:00
free((*itMap).second->info.data);
delete (*itMap).second;
itMap++;
}
_cache.clear();
2013-04-17 10:30:38 +00:00
}
2016-02-04 17:28:21 +00:00
if (!_cachelist.empty()) _cachelist.clear();
2013-04-17 10:30:38 +00:00
2016-02-04 17:28:21 +00:00
_totalSize = 0;
}