[Glide64] Clean up GlideHQ/TxHiResCache.cpp

This commit is contained in:
zilmar 2016-02-05 04:35:02 +11:00
parent d8a35cbfba
commit d6722faee6
1 changed files with 888 additions and 883 deletions

View File

@ -71,9 +71,9 @@ TxHiResCache::~TxHiResCache()
/* dump cache to disk */
std::wstring filename = _ident + L"_HIRESTEXTURES.dat";
CPath cachepath(stdstr().FromUTF16(_path.c_str()).c_str(),"");
CPath cachepath(stdstr().FromUTF16(_path.c_str()).c_str(), "");
cachepath.AppendDirectory("cache");
int config = _options & (HIRESTEXTURES_MASK|COMPRESS_HIRESTEX|COMPRESSION_MASK|TILE_HIRESTEX|FORCE16BPP_HIRESTEX|GZ_HIRESTEXCACHE|LET_TEXARTISTS_FLY);
int config = _options & (HIRESTEXTURES_MASK | COMPRESS_HIRESTEX | COMPRESSION_MASK | TILE_HIRESTEX | FORCE16BPP_HIRESTEX | GZ_HIRESTEXCACHE | LET_TEXARTISTS_FLY);
TxCache::save(stdstr((std::string &)cachepath).ToUTF16().c_str(), filename.c_str(), config);
}
@ -113,9 +113,9 @@ TxHiResCache::TxHiResCache(int maxwidth, int maxheight, int maxbpp, int options,
if (_options & DUMP_HIRESTEXCACHE) {
/* find it on disk */
std::wstring filename = _ident + L"_HIRESTEXTURES.dat";
CPath cachepath(stdstr().FromUTF16(_path.c_str()).c_str(),"");
CPath cachepath(stdstr().FromUTF16(_path.c_str()).c_str(), "");
cachepath.AppendDirectory("cache");
int config = _options & (HIRESTEXTURES_MASK|COMPRESS_HIRESTEX|COMPRESSION_MASK|TILE_HIRESTEX|FORCE16BPP_HIRESTEX|GZ_HIRESTEXCACHE|LET_TEXARTISTS_FLY);
int config = _options & (HIRESTEXTURES_MASK | COMPRESS_HIRESTEX | COMPRESSION_MASK | TILE_HIRESTEX | FORCE16BPP_HIRESTEX | GZ_HIRESTEXCACHE | LET_TEXARTISTS_FLY);
_haveCache = TxCache::load(stdstr((std::string &)cachepath).ToUTF16().c_str(), filename.c_str(), config);
}
@ -135,10 +135,9 @@ boolean
TxHiResCache::load(boolean replace) /* 0 : reload, 1 : replace partial */
{
if (!_path.empty() && !_ident.empty()) {
if (!replace) TxCache::clear();
CPath dir_path(stdstr().FromUTF16(_path.c_str()).c_str(),"");
CPath dir_path(stdstr().FromUTF16(_path.c_str()).c_str(), "");
switch (_options & HIRESTEXTURES_MASK) {
case GHQ_HIRESTEXTURES:
@ -173,7 +172,7 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
DBG_INFO(80, L"-----\n");
DBG_INFO(80, L"path: %s\n", stdstr(dir_path).ToUTF16().c_str());
CPath TextureDir(dir_path,"");
CPath TextureDir(dir_path, "");
/* find it on disk */
if (!TextureDir.DirectoryExists())
@ -212,7 +211,7 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
uint8 *tex = NULL;
int tmpwidth = 0, tmpheight = 0;
uint16 tmpformat = 0;
uint8 *tmptex= NULL;
uint8 *tmptex = NULL;
int untiled_width = 0, untiled_height = 0;
uint16 destformat = 0;
@ -236,8 +235,8 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
ident.assign(fname);
/* read in Rice's file naming convention */
#define CRCFMTSIZ_LEN 13
#define PALCRC_LEN 9
#define CRCFMTSIZ_LEN 13
#define PALCRC_LEN 9
wcstombs(fname, stdstr(TextureDir.GetNameExtension()).ToUTF16().c_str(), MAX_PATH);
/* XXX case sensitivity fiasco!
* files must use _a, _rgb, _all, _allciByRGBA, _ciByRGBA, _ci
@ -253,11 +252,11 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
pfname == strstr(fname, ".bmp") ||
pfname == strstr(fname, ".dds")))
{
#if !DEBUG
#if !DEBUG
INFO(80, L"-----\n");
INFO(80, L"path: %ls\n", stdstr(dir_path).ToUTF16().c_str());
INFO(80, L"file: %ls\n", TextureDir.GetNameExtension().ToUTF16().c_str());
#endif
#endif
INFO(80, L"Error: not png or bmp or dds!\n");
continue;
}
@ -272,20 +271,20 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
pfname = 0;
}
if (!pfname) {
#if !DEBUG
#if !DEBUG
INFO(80, L"-----\n");
INFO(80, L"path: %ls\n", stdstr(dir_path).ToUTF16().c_str());
INFO(80, L"file: %ls\n", TextureDir.GetNameExtension().ToUTF16().c_str());
#endif
#endif
INFO(80, L"Error: not Rice texture naming convention!\n");
continue;
}
if (!chksum) {
#if !DEBUG
#if !DEBUG
INFO(80, L"-----\n");
INFO(80, L"path: %ls\n", stdstr(dir_path).ToUTF16().c_str());
INFO(80, L"file: %ls\n", TextureDir.GetNameExtension().ToUTF16().c_str());
#endif
#endif
INFO(80, L"Error: crc32 = 0!\n");
continue;
}
@ -296,11 +295,11 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
chksum64 <<= 32;
chksum64 |= (uint64)chksum;
if (TxCache::is_cached(chksum64)) {
#if !DEBUG
#if !DEBUG
INFO(80, L"-----\n");
INFO(80, L"path: %ls\n", stdstr(dir_path).ToUTF16().c_str());
INFO(80, L"file: %ls\n", TextureDir.GetNameExtension().ToUTF16().c_str());
#endif
#endif
INFO(80, L"Error: already cached! duplicate texture!\n");
continue;
}
@ -329,25 +328,25 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
*/
if (pfname == strstr(fname, "_rgb.") || pfname == strstr(fname, "_a.")) {
strcpy(pfname, "_rgb.png");
CPath TargetFile(dir_path,fname);
CPath TargetFile(dir_path, fname);
if (!TargetFile.Exists())
{
strcpy(pfname, "_rgb.bmp");
TargetFile = CPath(dir_path,fname);
TargetFile = CPath(dir_path, fname);
if (!TargetFile.Exists())
{
#if !DEBUG
#if !DEBUG
INFO(80, L"-----\n");
INFO(80, L"path: %ls\n", stdstr(dir_path).ToUTF16().c_str());
INFO(80, L"file: %ls\n", TextureDir.GetNameExtension().ToUTF16().c_str());
#endif
#endif
INFO(80, L"Error: missing _rgb.*! _a.* must be paired with _rgb.*!\n");
continue;
}
}
/* _a.png */
strcpy(pfname, "_a.png");
TargetFile = CPath(dir_path,fname);
TargetFile = CPath(dir_path, fname);
if ((fp = fopen(TargetFile, "rb")) != NULL) {
tmptex = _txImage->readPNG(fp, &tmpwidth, &tmpheight, &tmpformat);
fclose(fp);
@ -355,7 +354,7 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
if (!tmptex) {
/* _a.bmp */
strcpy(pfname, "_a.bmp");
TargetFile = CPath(dir_path,fname);
TargetFile = CPath(dir_path, fname);
if ((fp = fopen(TargetFile, "rb")) != NULL) {
tmptex = _txImage->readBMP(fp, &tmpwidth, &tmpheight, &tmpformat);
fclose(fp);
@ -363,7 +362,7 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
}
/* _rgb.png */
strcpy(pfname, "_rgb.png");
TargetFile = CPath(dir_path,fname);
TargetFile = CPath(dir_path, fname);
if ((fp = fopen(TargetFile, "rb")) != NULL) {
tex = _txImage->readPNG(fp, &width, &height, &format);
fclose(fp);
@ -371,7 +370,7 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
if (!tex) {
/* _rgb.bmp */
strcpy(pfname, "_rgb.bmp");
TargetFile = CPath(dir_path,fname);
TargetFile = CPath(dir_path, fname);
if ((fp = fopen(TargetFile, "rb")) != NULL) {
tex = _txImage->readBMP(fp, &width, &height, &format);
fclose(fp);
@ -388,9 +387,11 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
#endif
if (!tex) {
INFO(80, L"Error: missing _rgb.*!\n");
} else if (width != tmpwidth || height != tmpheight) {
}
else if (width != tmpwidth || height != tmpheight) {
INFO(80, L"Error: _rgb.* and _a.* have mismatched width or height!\n");
} else if (format != GR_TEXFMT_ARGB_8888 || tmpformat != GR_TEXFMT_ARGB_8888) {
}
else if (format != GR_TEXFMT_ARGB_8888 || tmpformat != GR_TEXFMT_ARGB_8888) {
INFO(80, L"Error: _rgb.* or _a.* not in 32bit color!\n");
}
if (tex) free(tex);
@ -431,7 +432,8 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
}
free(tmptex);
tmptex = NULL;
} else {
}
else {
/* clobber A comp. never a question of alpha. only RGB used. */
#if !DEBUG
INFO(80, L"-----\n");
@ -445,7 +447,8 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
}
}
}
} else
}
else
/*
* read in _all.png, _all.dds, _allciByRGBA.png, _allciByRGBA.dds
@ -465,7 +468,7 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
pfname == strstr(fname, "_ciByRGBA.dds") ||
#endif
pfname == strstr(fname, "_ci.bmp")) {
CPath TargetFile(dir_path,fname);
CPath TargetFile(dir_path, fname);
if ((fp = fopen(TargetFile, "rb")) != NULL) {
if (strstr(fname, ".png")) tex = _txImage->readPNG(fp, &width, &height, &format);
else if (strstr(fname, ".dds")) tex = _txImage->readDDS(fp, &width, &height, &format);
@ -474,7 +477,7 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
}
/* XXX: auto-adjustment of dxt dds textures unsupported for now */
if (tex && strstr(fname, ".dds")) {
const float aspectratio = (width > height) ? (float)width/(float)height : (float)height/(float)width;
const float aspectratio = (width > height) ? (float)width / (float)height : (float)height / (float)width;
if (!(aspectratio == 1.0 ||
aspectratio == 2.0 ||
aspectratio == 4.0 ||
@ -585,15 +588,16 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
if ((tmptexel[k] & 0xff000000) == 0xff000000) {
r += ((tmptexel[k] & 0x00ff0000) >> 16);
g += ((tmptexel[k] & 0x0000ff00) >> 8);
b += ((tmptexel[k] & 0x000000ff) );
b += ((tmptexel[k] & 0x000000ff));
numtexel++;
}
}
if (numtexel) {
((uint32*)tex)[i * width + j] = ((r / numtexel) << 16) |
((g / numtexel) << 8) |
((b / numtexel) );
} else {
((b / numtexel));
}
else {
((uint32*)tex)[i * width + j] = texel & 0x00ffffff;
}
}
@ -610,14 +614,16 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
if ((texel & 0xff000000) < 0x00000003) {
alphabits = 1;
fullalpha++;
} else if ((texel & 0xff000000) < 0xfe000000) {
}
else if ((texel & 0xff000000) < 0xfe000000) {
alphabits = 8;
}
#else
if ((texel & 0xff000000) == 0x00000000) {
alphabits = 1;
fullalpha++;
} else if ((texel & 0xff000000) != 0xff000000) {
}
else if ((texel & 0xff000000) != 0xff000000) {
alphabits = 8;
}
#endif
@ -625,7 +631,7 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
if (intensity) {
int rcomp = (texel >> 16) & 0xff;
int gcomp = (texel >> 8) & 0xff;
int bcomp = (texel ) & 0xff;
int bcomp = (texel)& 0xff;
#if AGGRESSIVE_QUANTIZATION
if (abs(rcomp - gcomp) > 8 || abs(rcomp - bcomp) > 8 || abs(gcomp - bcomp) > 8) intensity = 0;
#else
@ -638,13 +644,14 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
/* preparations based on above analysis */
#if !REDUCE_TEXTURE_FOOTPRINT
if (_maxbpp < 32 || _options & (FORCE16BPP_HIRESTEX|COMPRESSION_MASK)) {
if (_maxbpp < 32 || _options & (FORCE16BPP_HIRESTEX | COMPRESSION_MASK)) {
#endif
if (alphabits == 0) destformat = GR_TEXFMT_RGB_565;
else if (alphabits == 1) destformat = GR_TEXFMT_ARGB_1555;
else destformat = GR_TEXFMT_ARGB_8888;
#if !REDUCE_TEXTURE_FOOTPRINT
} else {
}
else {
destformat = GR_TEXFMT_ARGB_8888;
}
#endif
@ -655,7 +662,7 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
uint32 texel = ((uint32*)tex)[i];
uint32 icomp = (((texel >> 16) & 0xff) +
((texel >> 8) & 0xff) +
((texel ) & 0xff)) / 3;
((texel)& 0xff)) / 3;
((uint32*)tex)[i] = (icomp << 24) | (texel & 0x00ffffff);
}
}
@ -663,7 +670,8 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
if (alphabits == 0) {
if (fmt == 4) destformat = GR_TEXFMT_ALPHA_8;
else destformat = GR_TEXFMT_INTENSITY_8;
} else {
}
else {
destformat = GR_TEXFMT_ALPHA_INTENSITY_88;
}
}
@ -673,10 +681,8 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
/*
* Rice hi-res textures: end */
/* XXX: only ARGB8888 for now. comeback to this later... */
if (format == GR_TEXFMT_ARGB_8888) {
#if TEXTURE_TILING
/* Glide64 style texture tiling */
@ -708,7 +714,8 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
DBG_INFO(80, L"width > 256, minification ratio:%d %d x %d -> %d x %d\n",
ratio, width, height, width / ratio, height / ratio);
}
} else {
}
else {
/* normal minification to fit max texture size */
if (width > _maxwidth || height > _maxheight) {
DBG_INFO(80, L"determine minification ratio to fit max texture size\n");
@ -745,7 +752,6 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
/* to tile or not to tile, that is the question */
if (width > 256 && height <= 128 && (((width - 1) >> 8) + 1) * height <= 256) {
if (width > _maxwidth) usetile = 1;
else {
/* tile if the tiled texture memory footprint is smaller */
@ -772,13 +778,13 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
/* 3dfx Glide3 tmpheight, W:H aspect ratio range (8:1 - 1:8) */
if (tmpwidth > tmpheight) {
if (tmpwidth > (tmpheight << 3)) tmpheight = tmpwidth >> 3;
} else {
}
else {
if (tmpheight > (tmpwidth << 3)) tmpwidth = tmpheight >> 3;
}
usetile = (tilewidth * tileheight < tmpwidth * tmpheight);
}
}
/* tile it! do the actual tiling into 256x256 size */
@ -794,7 +800,8 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
for (y = 0; y < height; y++) {
if (x < ratio - 1) {
memcpy(&tmptex[offset << 2], &tex[(x * 256 + y * width) << 2], 256 << 2);
} else {
}
else {
for (z = 0; z < width - 256 * (ratio - 1); z++) {
((uint32*)tmptex)[offset + z] = ((uint32*)tex)[x * 256 + y * width + z];
}
@ -855,10 +862,10 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
#if POW2_TEXTURES
#if (POW2_TEXTURES == 2)
/* 3dfx Glide3x aspect ratio (8:1 - 1:8) */
if (!_txReSample->nextPow2(&tex, &width , &height, 32, 1)) {
if (!_txReSample->nextPow2(&tex, &width, &height, 32, 1)) {
#else
/* normal pow2 expansion */
if (!_txReSample->nextPow2(&tex, &width , &height, 32, 0)) {
if (!_txReSample->nextPow2(&tex, &width, &height, 32, 0)) {
#endif
free(tex);
tex = NULL;
@ -928,21 +935,21 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
width = tmpwidth;
height = tmpheight;
format = destformat = tmpformat;
} else {
}
else {
free(tmptex);
}
}
}
} else {
}
else {
#if POW2_TEXTURES
#if (POW2_TEXTURES == 2)
/* 3dfx Glide3x aspect ratio (8:1 - 1:8) */
if (!_txReSample->nextPow2(&tex, &width , &height, 32, 1)) {
if (!_txReSample->nextPow2(&tex, &width, &height, 32, 1)) {
#else
/* normal pow2 expansion */
if (!_txReSample->nextPow2(&tex, &width , &height, 32, 0)) {
if (!_txReSample->nextPow2(&tex, &width, &height, 32, 0)) {
#endif
free(tex);
tex = NULL;
@ -997,10 +1004,8 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
}
}
}
}
/* last minute validations */
if (!tex || !chksum || !width || !height || !format || width > _maxwidth || height > _maxheight) {
#if !DEBUG
@ -1012,7 +1017,8 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
free(tex);
tex = NULL;
INFO(80, L"Error: bad format or size! %d x %d gfmt:%x\n", width, height, format);
} else {
}
else {
INFO(80, L"Error: load failed!!\n");
}
continue;
@ -1057,14 +1063,13 @@ boolean TxHiResCache::loadHiResTextures(LPCSTR dir_path, boolean replace)
if (_callback) {
wchar_t tmpbuf[MAX_PATH];
mbstowcs(tmpbuf, fname, MAX_PATH);
(*_callback)(L"[%d] total mem:%.2fmb - %ls\n", _cache.size(), (float)_totalSize/1000000, tmpbuf);
(*_callback)(L"[%d] total mem:%.2fmb - %ls\n", _cache.size(), (float)_totalSize / 1000000, tmpbuf);
}
DBG_INFO(80, L"texture loaded!\n");
}
free(tex);
}
} while (TextureDir.FindNext());
}
return 1;
}
}