2013-06-27 23:05:11 +00:00
|
|
|
#ifdef USE_OPENGL
|
|
|
|
|
2014-02-10 10:56:14 +00:00
|
|
|
#include "baselayer.h"
|
2013-05-15 02:17:17 +00:00
|
|
|
#include "build.h"
|
2014-02-10 10:55:49 +00:00
|
|
|
#include "lz4.h"
|
2013-05-15 02:17:17 +00:00
|
|
|
#include "hightile.h"
|
|
|
|
#include "polymost.h"
|
|
|
|
#include "texcache.h"
|
|
|
|
#include "dxtfilter.h"
|
|
|
|
#include "scriptfile.h"
|
|
|
|
#include "crc32.h"
|
|
|
|
|
|
|
|
#define CLEAR_GL_ERRORS() while(bglGetError() != GL_NO_ERROR) { }
|
|
|
|
#define REALLOC_OR_FAIL(ptr, size, type) { ptr = (type *)Brealloc(ptr, size); if (!ptr) goto failure; }
|
|
|
|
#define TEXCACHE_FREEBUFS() { Bfree(pic), Bfree(packbuf), Bfree(midbuf); }
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
globaltexcache texcache;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
char TEXCACHEFILE[BMAX_PATH] = "textures";
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
static const char *texcache_errorstr[TEXCACHEERRORS] = {
|
|
|
|
"no error",
|
2013-05-15 02:17:17 +00:00
|
|
|
"out of memory!",
|
|
|
|
"read too few bytes from cache file",
|
|
|
|
"dedxtfilter failed",
|
|
|
|
"bglCompressedTexImage2DARB failed",
|
|
|
|
"bglGetTexLevelParameteriv failed",
|
|
|
|
};
|
|
|
|
|
2013-05-15 02:18:27 +00:00
|
|
|
pthtyp *texcache_fetch(int32_t dapicnum, int32_t dapalnum, int32_t dashade, int32_t dameth)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
int32_t i, j;
|
|
|
|
hicreplctyp *si;
|
|
|
|
pthtyp *pth, *pth2;
|
|
|
|
|
|
|
|
j = (dapicnum&(GLTEXCACHEADSIZ-1));
|
2013-05-15 02:18:27 +00:00
|
|
|
|
|
|
|
if (getrendermode() != REND_POLYMOST || !r_usetileshades) dashade = 0;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
si = usehightile ? hicfindsubst(dapicnum,dapalnum,drawingskybox) : NULL;
|
|
|
|
|
|
|
|
if (!si)
|
|
|
|
{
|
|
|
|
if (drawingskybox || dapalnum >= (MAXPALOOKUPS - RESERVEDPALS)) return NULL;
|
|
|
|
goto tryart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if palette > 0 && replacement found
|
|
|
|
* no effects are applied to the texture
|
|
|
|
* else if palette > 0 && no replacement found
|
|
|
|
* effects are applied to the palette 0 texture if it exists
|
|
|
|
*/
|
|
|
|
|
|
|
|
// load a replacement
|
2013-05-17 03:42:37 +00:00
|
|
|
for (pth=texcache.list[j]; pth; pth=pth->next)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
if (pth->picnum == dapicnum && pth->palnum == si->palnum &&
|
|
|
|
(si->palnum>0 ? 1 : (pth->effects == hictinting[dapalnum].f)) &&
|
|
|
|
(pth->flags & (1+2+4)) == (((dameth&4)>>2)+2+((drawingskybox>0)<<2)) &&
|
|
|
|
(drawingskybox>0 ? (pth->skyface == drawingskybox) : 1)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (pth->flags & 128)
|
|
|
|
{
|
|
|
|
pth->flags &= ~128;
|
|
|
|
if (gloadtile_hi(dapicnum,dapalnum,drawingskybox,si,dameth,pth,0,
|
|
|
|
(si->palnum>0) ? 0 : hictinting[dapalnum].f)) // reload tile
|
|
|
|
{
|
|
|
|
if (drawingskybox) return NULL;
|
|
|
|
goto tryart; // failed, so try for ART
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(pth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pth = (pthtyp *)Bcalloc(1,sizeof(pthtyp));
|
|
|
|
if (!pth) return NULL;
|
|
|
|
|
|
|
|
// possibly fetch an already loaded multitexture :_)
|
|
|
|
if (dapalnum >= (MAXPALOOKUPS - RESERVEDPALS))
|
|
|
|
for (i = (GLTEXCACHEADSIZ - 1); i >= 0; i--)
|
2013-05-17 03:42:37 +00:00
|
|
|
for (pth2=texcache.list[i]; pth2; pth2=pth2->next)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
if ((pth2->hicr) && (pth2->hicr->filename) && (Bstrcasecmp(pth2->hicr->filename, si->filename) == 0))
|
|
|
|
{
|
|
|
|
Bmemcpy(pth, pth2, sizeof(pthtyp));
|
|
|
|
pth->picnum = dapicnum;
|
|
|
|
pth->flags = ((dameth&4)>>2) + 2 + ((drawingskybox>0)<<2);
|
|
|
|
if (pth2->flags & 8) pth->flags |= 8; //hasalpha
|
|
|
|
pth->hicr = si;
|
2013-05-17 03:42:37 +00:00
|
|
|
pth->next = texcache.list[j];
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.list[j] = pth;
|
2013-05-15 02:17:17 +00:00
|
|
|
return(pth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gloadtile_hi(dapicnum,dapalnum,drawingskybox,si,dameth,pth,1, (si->palnum>0) ? 0 : hictinting[dapalnum].f))
|
|
|
|
{
|
|
|
|
Bfree(pth);
|
|
|
|
if (drawingskybox) return NULL;
|
|
|
|
goto tryart; // failed, so try for ART
|
|
|
|
}
|
|
|
|
|
|
|
|
pth->palnum = si->palnum;
|
2013-05-17 03:42:37 +00:00
|
|
|
pth->next = texcache.list[j];
|
|
|
|
texcache.list[j] = pth;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
return(pth);
|
|
|
|
|
|
|
|
tryart:
|
|
|
|
if (hicprecaching) return NULL;
|
|
|
|
|
|
|
|
// load from art
|
2013-05-17 03:42:37 +00:00
|
|
|
for (pth=texcache.list[j]; pth; pth=pth->next)
|
2013-05-15 02:18:27 +00:00
|
|
|
if (pth->picnum == dapicnum && pth->palnum == dapalnum && pth->shade == dashade &&
|
2013-05-15 02:17:17 +00:00
|
|
|
(pth->flags & (1+2)) == ((dameth&4)>>2)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (pth->flags & 128)
|
|
|
|
{
|
|
|
|
pth->flags &= ~128;
|
2013-05-15 02:18:27 +00:00
|
|
|
if (gloadtile_art(dapicnum,dapalnum,dashade,dameth,pth,0))
|
2013-05-15 02:17:17 +00:00
|
|
|
return NULL; //reload tile (for animations)
|
|
|
|
}
|
|
|
|
|
|
|
|
return(pth);
|
|
|
|
}
|
|
|
|
|
|
|
|
pth = (pthtyp *)Bcalloc(1,sizeof(pthtyp));
|
|
|
|
if (!pth) return NULL;
|
|
|
|
|
2013-05-15 02:18:27 +00:00
|
|
|
if (gloadtile_art(dapicnum,dapalnum,dashade,dameth,pth,1))
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
Bfree(pth);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
pth->next = texcache.list[j];
|
|
|
|
texcache.list[j] = pth;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
return(pth);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void texcache_closefiles(void)
|
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
if (texcache.filehandle != -1)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
Bclose(texcache.filehandle);
|
|
|
|
texcache.filehandle = -1;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (texcache.index)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
Bfclose(texcache.index);
|
|
|
|
texcache.index = NULL;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void texcache_freeptrs(void)
|
|
|
|
{
|
|
|
|
int32_t i;
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
for (i = texcache.numentries-1; i >= 0; i--)
|
|
|
|
if (texcache.ptrs[i])
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
int32_t ii;
|
2013-05-17 03:42:37 +00:00
|
|
|
for (ii = texcache.numentries-1; ii >= 0; ii--)
|
|
|
|
if (i != ii && texcache.ptrs[ii] == texcache.ptrs[i])
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
/*OSD_Printf("removing duplicate cacheptr %d\n",ii);*/
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.ptrs[ii] = NULL;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
Bfree(texcache.ptrs[i]);
|
|
|
|
texcache.ptrs[i] = NULL;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 02:19:14 +00:00
|
|
|
void texcache_clearmemcache(void)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
Bfree(texcache.memcache.ptr);
|
|
|
|
texcache.memcache.ptr = NULL;
|
|
|
|
texcache.memcache.size = -1;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2013-05-15 02:19:14 +00:00
|
|
|
void texcache_syncmemcache(void)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2013-05-15 02:19:14 +00:00
|
|
|
size_t len;
|
|
|
|
|
2013-05-17 10:41:59 +00:00
|
|
|
if (!texcache.memcache.ptr || texcache.filehandle == -1 ||
|
|
|
|
(bsize_t)Bfilelength(texcache.filehandle) <= texcache.memcache.size)
|
2013-05-15 02:19:14 +00:00
|
|
|
return;
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
len = Bfilelength(texcache.filehandle);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.memcache.ptr = (uint8_t *)Brealloc(texcache.memcache.ptr, len);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (!texcache.memcache.ptr)
|
2013-05-15 02:19:14 +00:00
|
|
|
{
|
|
|
|
texcache_clearmemcache();
|
|
|
|
initprintf("Failed syncing memcache to texcache, disabling memcache.\n");
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.memcache.noalloc = 1;
|
2013-05-15 02:19:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
initprintf("Syncing memcache to texcache\n");
|
2013-05-17 03:42:37 +00:00
|
|
|
Blseek(texcache.filehandle, texcache.memcache.size, BSEEK_SET);
|
|
|
|
if (Bread(texcache.filehandle, texcache.memcache.ptr + texcache.memcache.size, len - texcache.memcache.size) != (bssize_t)(len-texcache.memcache.size))
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2013-05-15 02:19:14 +00:00
|
|
|
initprintf("polymost_cachesync: Failed reading texcache into memcache!\n");
|
|
|
|
texcache_clearmemcache();
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.memcache.noalloc = 1;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.memcache.size = len;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void texcache_init(void)
|
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
if (!texcache.index)
|
|
|
|
texcache.filehandle = -1;
|
|
|
|
|
2013-05-15 02:17:17 +00:00
|
|
|
texcache_closefiles();
|
2013-05-15 02:19:14 +00:00
|
|
|
texcache_clearmemcache();
|
2013-05-15 02:17:17 +00:00
|
|
|
texcache_freeptrs();
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.currentindex = texcache.firstindex = (texcacheindex *)Bcalloc(1, sizeof(texcacheindex));
|
|
|
|
texcache.numentries = 0;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
// Bmemset(&firstcacheindex, 0, sizeof(texcacheindex));
|
|
|
|
// Bmemset(&cacheptrs[0], 0, sizeof(cacheptrs));
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.hashes.size = TEXCACHEHASHSIZE;
|
|
|
|
hash_init(&texcache.hashes);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
static void texcache_deletefiles(void)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
Bstrcpy(ptempbuf, TEXCACHEFILE);
|
|
|
|
unlink(ptempbuf);
|
|
|
|
Bstrcat(ptempbuf, ".cache");
|
|
|
|
unlink(ptempbuf);
|
2013-05-15 02:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t texcache_enabled(void)
|
|
|
|
{
|
|
|
|
if (!glusetexcompr || !glusetexcache) return 0;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2013-05-15 02:18:27 +00:00
|
|
|
if (!glinfo.texcompr || !bglCompressedTexImage2DARB || !bglGetCompressedTexImageARB)
|
|
|
|
{
|
|
|
|
// lacking the necessary extensions to do this
|
|
|
|
OSD_Printf("Warning: the GL driver lacks necessary functions to use caching\n");
|
|
|
|
glusetexcache = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (!texcache.index || texcache.filehandle < 0)
|
2013-05-15 02:18:27 +00:00
|
|
|
{
|
|
|
|
OSD_Printf("Warning: no active cache!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void texcache_openfiles(void)
|
|
|
|
{
|
|
|
|
Bstrcpy(ptempbuf,TEXCACHEFILE);
|
|
|
|
Bstrcat(ptempbuf,".cache");
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.index = Bfopen(ptempbuf, "at+");
|
|
|
|
texcache.filehandle = Bopen(TEXCACHEFILE, BO_BINARY|BO_CREAT|BO_APPEND|BO_RDWR, BS_IREAD|BS_IWRITE);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (!texcache.index || texcache.filehandle < 0)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
initprintf("Unable to open cache file \"%s\" or \"%s\": %s\n", TEXCACHEFILE, ptempbuf, strerror(errno));
|
|
|
|
texcache_closefiles();
|
|
|
|
glusetexcache = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
Bfseek(texcache.index, 0, BSEEK_END);
|
|
|
|
if (!Bftell(texcache.index))
|
2013-05-15 02:18:27 +00:00
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
Brewind(texcache.index);
|
|
|
|
Bfprintf(texcache.index,"// automatically generated by EDuke32, DO NOT MODIFY!\n");
|
2013-05-15 02:18:27 +00:00
|
|
|
}
|
2013-05-17 03:42:37 +00:00
|
|
|
else Brewind(texcache.index);
|
2013-05-15 02:18:27 +00:00
|
|
|
|
|
|
|
initprintf("Opened \"%s\" as cache file\n", TEXCACHEFILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void texcache_checkgarbage(void)
|
|
|
|
{
|
|
|
|
int32_t i = 0;
|
|
|
|
|
|
|
|
if (!texcache_enabled())
|
|
|
|
return;
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.currentindex = texcache.firstindex;
|
|
|
|
while (texcache.currentindex->next)
|
2013-05-15 02:18:27 +00:00
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
i += texcache.currentindex->len;
|
|
|
|
texcache.currentindex = texcache.currentindex->next;
|
2013-05-15 02:18:27 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
i = Blseek(texcache.filehandle, 0, BSEEK_END)-i;
|
2013-05-15 02:18:27 +00:00
|
|
|
|
|
|
|
if (i)
|
|
|
|
initprintf("Cache contains %d bytes of garbage data\n",i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void texcache_invalidate(void)
|
|
|
|
{
|
|
|
|
#ifdef DEBUGGINGAIDS
|
|
|
|
OSD_Printf("texcache_invalidate()\n");
|
|
|
|
#endif
|
|
|
|
r_downsizevar = r_downsize; // update the cvar representation when the menu changes r_downsize
|
|
|
|
|
|
|
|
polymost_glreset();
|
|
|
|
|
|
|
|
texcache_init();
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache_deletefiles();
|
2013-05-15 02:18:27 +00:00
|
|
|
texcache_openfiles();
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t texcache_loadoffsets(void)
|
|
|
|
{
|
|
|
|
int32_t foffset, fsize, i;
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
scriptfile *script;
|
|
|
|
|
|
|
|
Bstrcpy(ptempbuf,TEXCACHEFILE);
|
|
|
|
Bstrcat(ptempbuf,".cache");
|
|
|
|
script = scriptfile_fromfile(ptempbuf);
|
|
|
|
|
|
|
|
if (!script) return -1;
|
|
|
|
|
|
|
|
while (!scriptfile_eof(script))
|
|
|
|
{
|
|
|
|
if (scriptfile_getstring(script, &fname)) break; // hashed filename
|
|
|
|
if (scriptfile_getnumber(script, &foffset)) break; // offset in cache
|
|
|
|
if (scriptfile_getnumber(script, &fsize)) break; // size
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
i = hash_find(&texcache.hashes,fname);
|
2013-05-15 02:17:17 +00:00
|
|
|
if (i > -1)
|
|
|
|
{
|
|
|
|
// update an existing entry
|
2013-05-17 03:42:37 +00:00
|
|
|
texcacheindex *t = texcache.ptrs[i];
|
2013-05-15 02:17:17 +00:00
|
|
|
t->offset = foffset;
|
|
|
|
t->len = fsize;
|
|
|
|
/*initprintf("%s %d got a match for %s offset %d\n",__FILE__, __LINE__, fname,foffset);*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
Bstrncpyz(texcache.currentindex->name, fname, BMAX_PATH);
|
|
|
|
texcache.currentindex->offset = foffset;
|
|
|
|
texcache.currentindex->len = fsize;
|
|
|
|
texcache.currentindex->next = (texcacheindex *)Bcalloc(1, sizeof(texcacheindex));
|
|
|
|
hash_add(&texcache.hashes, fname, texcache.numentries, 1);
|
|
|
|
texcache.ptrs[texcache.numentries++] = texcache.currentindex;
|
|
|
|
texcache.currentindex = texcache.currentindex->next;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptfile_close(script);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read from on-disk texcache or its in-memory cache.
|
|
|
|
int32_t texcache_readdata(void *dest, int32_t len)
|
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
const int32_t ocachepos = texcache.filepos;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.filepos += len;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2013-05-17 10:41:59 +00:00
|
|
|
if (texcache.memcache.ptr && texcache.memcache.size >= (bsize_t)ocachepos+len)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
// initprintf("using memcache!\n");
|
2013-05-17 03:42:37 +00:00
|
|
|
Bmemcpy(dest, texcache.memcache.ptr+ocachepos, len);
|
|
|
|
return 0;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (Blseek(texcache.filehandle, ocachepos, BSEEK_SET) != ocachepos)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (Bread(texcache.filehandle, dest, len) < len)
|
|
|
|
return 1;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * texcache_calcid(char *cachefn, const char *fn, const int32_t len, const int32_t dameth, const char effect)
|
|
|
|
{
|
2013-10-11 22:20:46 +00:00
|
|
|
// Assert that BMAX_PATH is a multiple of 4 so that struct texcacheid_t
|
|
|
|
// gets no padding inserted by the compiler.
|
|
|
|
EDUKE32_STATIC_ASSERT((BMAX_PATH & 3) == 0);
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
struct texcacheid_t {
|
|
|
|
int32_t len, method;
|
2013-10-11 22:20:46 +00:00
|
|
|
char effect, name[BMAX_PATH+3]; // +3: pad to a multiple of 4
|
2013-05-17 03:42:37 +00:00
|
|
|
} id = { len, dameth, effect, "" };
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
Bstrcpy(id.name, fn);
|
|
|
|
|
|
|
|
while (Bstrlen(id.name) < BMAX_PATH - Bstrlen(fn))
|
|
|
|
Bstrcat(id.name, fn);
|
|
|
|
|
2013-10-11 22:20:46 +00:00
|
|
|
Bsprintf(cachefn, "%08x%08x%08x",
|
2013-05-15 02:17:17 +00:00
|
|
|
crc32once((uint8_t *)fn, Bstrlen(fn)),
|
|
|
|
crc32once((uint8_t *)id.name, Bstrlen(id.name)),
|
2013-05-17 03:42:37 +00:00
|
|
|
crc32once((uint8_t *)&id, sizeof(struct texcacheid_t)));
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
return cachefn;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define READTEXHEADER_FAILURE(x) { err = x; goto failure; }
|
|
|
|
|
|
|
|
// returns 1 on success
|
|
|
|
int32_t texcache_readtexheader(const char *fn, int32_t len, int32_t dameth, char effect,
|
|
|
|
texcacheheader *head, int32_t modelp)
|
|
|
|
{
|
|
|
|
int32_t i, err = 0;
|
|
|
|
char cachefn[BMAX_PATH];
|
|
|
|
|
|
|
|
if (!texcache_enabled()) return 0;
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
i = hash_find(&texcache.hashes, texcache_calcid(cachefn, fn, len, dameth, effect));
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (i < 0 || !texcache.ptrs[i])
|
2013-05-15 02:17:17 +00:00
|
|
|
return 0; // didn't find it
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.filepos = texcache.ptrs[i]->offset;
|
2013-05-15 02:17:17 +00:00
|
|
|
// initprintf("%s %d got a match for %s offset %d\n",__FILE__, __LINE__, cachefn,offset);
|
|
|
|
|
|
|
|
|
|
|
|
if (texcache_readdata(head, sizeof(texcacheheader))) READTEXHEADER_FAILURE(0);
|
|
|
|
|
|
|
|
if (Bmemcmp(head->magic, TEXCACHEMAGIC, 4)) READTEXHEADER_FAILURE(1);
|
|
|
|
|
|
|
|
// native (little-endian) -> internal
|
|
|
|
head->xdim = B_LITTLE32(head->xdim);
|
|
|
|
head->ydim = B_LITTLE32(head->ydim);
|
|
|
|
head->flags = B_LITTLE32(head->flags);
|
|
|
|
head->quality = B_LITTLE32(head->quality);
|
|
|
|
|
|
|
|
if (modelp && head->quality != r_downsize) READTEXHEADER_FAILURE(2);
|
|
|
|
if ((head->flags & 4) && glusetexcache != 2) READTEXHEADER_FAILURE(3);
|
|
|
|
if (!(head->flags & 4) && glusetexcache == 2) READTEXHEADER_FAILURE(4);
|
|
|
|
|
|
|
|
// handle nocompress
|
|
|
|
if (!modelp && !(head->flags & 8) && head->quality != r_downsize)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (gltexmaxsize && (head->xdim > (1<<gltexmaxsize) || head->ydim > (1<<gltexmaxsize))) READTEXHEADER_FAILURE(5);
|
|
|
|
if (!glinfo.texnpot && (head->flags & 1)) READTEXHEADER_FAILURE(6);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
failure:
|
|
|
|
{
|
|
|
|
static const char *error_msgs[] = {
|
|
|
|
"failed reading texture cache header", // 0
|
|
|
|
"header magic string doesn't match", // 1
|
|
|
|
"r_downsize doesn't match", // 2 (skins only)
|
|
|
|
"compression doesn't match: cache contains compressed tex", // 3
|
|
|
|
"compression doesn't match: cache contains uncompressed tex", // 4
|
|
|
|
"texture in cache exceeds maximum supported size", // 5
|
|
|
|
"texture in cache has non-power-of-two size, unsupported", // 6
|
|
|
|
};
|
|
|
|
|
|
|
|
initprintf("%s cache miss: %s\n", modelp?"Skin":"Texture", error_msgs[err]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-15 02:19:14 +00:00
|
|
|
#undef READTEXHEADER_FAILURE
|
2013-05-15 02:17:17 +00:00
|
|
|
#define WRITEX_FAIL_ON_ERROR() if (bglGetError() != GL_NO_ERROR) goto failure
|
|
|
|
|
|
|
|
void texcache_writetex(const char *fn, int32_t len, int32_t dameth, char effect, texcacheheader *head)
|
|
|
|
{
|
|
|
|
char cachefn[BMAX_PATH];
|
|
|
|
char *pic = NULL, *packbuf = NULL;
|
|
|
|
void *midbuf = NULL;
|
|
|
|
uint32_t alloclen=0, level;
|
|
|
|
uint32_t padx=0, pady=0;
|
|
|
|
GLint gi;
|
|
|
|
int32_t offset = 0;
|
|
|
|
|
|
|
|
if (!texcache_enabled()) return;
|
|
|
|
|
|
|
|
gi = GL_FALSE;
|
|
|
|
bglGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_ARB, &gi);
|
|
|
|
if (gi != GL_TRUE)
|
|
|
|
{
|
|
|
|
OSD_Printf("Error: glGetTexLevelParameteriv returned GL_FALSE!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
Blseek(texcache.filehandle, 0, BSEEK_END);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
offset = Blseek(texcache.filehandle, 0, BSEEK_CUR);
|
2013-05-15 02:17:17 +00:00
|
|
|
// OSD_Printf("Caching %s, offset 0x%x\n", cachefn, offset);
|
|
|
|
|
|
|
|
Bmemcpy(head->magic, TEXCACHEMAGIC, 4); // sizes are set by caller
|
|
|
|
|
|
|
|
if (glusetexcache == 2) head->flags |= 4;
|
|
|
|
|
|
|
|
// native -> external (little-endian)
|
|
|
|
head->xdim = B_LITTLE32(head->xdim);
|
|
|
|
head->ydim = B_LITTLE32(head->ydim);
|
|
|
|
head->flags = B_LITTLE32(head->flags);
|
|
|
|
head->quality = B_LITTLE32(head->quality);
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (Bwrite(texcache.filehandle, head, sizeof(texcacheheader)) != sizeof(texcacheheader)) goto failure;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
CLEAR_GL_ERRORS();
|
|
|
|
|
|
|
|
for (level = 0; level==0 || (padx > 1 || pady > 1); level++)
|
|
|
|
{
|
|
|
|
uint32_t miplen;
|
|
|
|
texcachepicture pict;
|
|
|
|
|
|
|
|
bglGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_COMPRESSED_ARB, &gi); WRITEX_FAIL_ON_ERROR();
|
|
|
|
if (gi != GL_TRUE) goto failure; // an uncompressed mipmap
|
|
|
|
bglGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_INTERNAL_FORMAT, &gi); WRITEX_FAIL_ON_ERROR();
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if (pr_ati_textureformat_one && gi == 1) gi = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
|
|
|
|
#endif
|
|
|
|
// native -> external (little endian)
|
|
|
|
pict.format = B_LITTLE32(gi);
|
|
|
|
bglGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &gi); WRITEX_FAIL_ON_ERROR();
|
|
|
|
padx = gi; pict.xdim = B_LITTLE32(gi);
|
|
|
|
bglGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_HEIGHT, &gi); WRITEX_FAIL_ON_ERROR();
|
|
|
|
pady = gi; pict.ydim = B_LITTLE32(gi);
|
|
|
|
bglGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_BORDER, &gi); WRITEX_FAIL_ON_ERROR();
|
|
|
|
pict.border = B_LITTLE32(gi);
|
|
|
|
bglGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_DEPTH, &gi); WRITEX_FAIL_ON_ERROR();
|
|
|
|
pict.depth = B_LITTLE32(gi);
|
|
|
|
bglGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &gi); WRITEX_FAIL_ON_ERROR();
|
|
|
|
miplen = gi; pict.size = B_LITTLE32(gi);
|
|
|
|
|
|
|
|
if (alloclen < miplen)
|
|
|
|
{
|
|
|
|
REALLOC_OR_FAIL(pic, miplen, char);
|
|
|
|
alloclen = miplen;
|
|
|
|
REALLOC_OR_FAIL(packbuf, alloclen+400, char);
|
|
|
|
REALLOC_OR_FAIL(midbuf, miplen, void);
|
|
|
|
}
|
|
|
|
|
|
|
|
bglGetCompressedTexImageARB(GL_TEXTURE_2D, level, pic); WRITEX_FAIL_ON_ERROR();
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (Bwrite(texcache.filehandle, &pict, sizeof(texcachepicture)) != sizeof(texcachepicture)) goto failure;
|
|
|
|
if (dxtfilter(texcache.filehandle, &pict, pic, midbuf, packbuf, miplen)) goto failure;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
texcacheindex *t;
|
2013-05-17 03:42:37 +00:00
|
|
|
int32_t i = hash_find(&texcache.hashes, texcache_calcid(cachefn, fn, len, dameth, effect));
|
2013-05-15 02:17:17 +00:00
|
|
|
if (i > -1)
|
|
|
|
{
|
|
|
|
// update an existing entry
|
2013-05-17 03:42:37 +00:00
|
|
|
t = texcache.ptrs[i];
|
2013-05-15 02:17:17 +00:00
|
|
|
t->offset = offset;
|
2013-05-17 03:42:37 +00:00
|
|
|
t->len = Blseek(texcache.filehandle, 0, BSEEK_CUR) - t->offset;
|
2013-05-15 02:17:17 +00:00
|
|
|
/*initprintf("%s %d got a match for %s offset %d\n",__FILE__, __LINE__, cachefn,offset);*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
t = texcache.currentindex;
|
2013-05-15 02:17:17 +00:00
|
|
|
Bstrcpy(t->name, cachefn);
|
|
|
|
t->offset = offset;
|
2013-05-17 03:42:37 +00:00
|
|
|
t->len = Blseek(texcache.filehandle, 0, BSEEK_CUR) - t->offset;
|
2013-05-15 02:17:17 +00:00
|
|
|
t->next = (texcacheindex *)Bcalloc(1, sizeof(texcacheindex));
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
hash_add(&texcache.hashes, cachefn, texcache.numentries, 0);
|
|
|
|
texcache.ptrs[texcache.numentries++] = t;
|
|
|
|
texcache.currentindex = t->next;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (texcache.index)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
fseek(texcache.index, 0, BSEEK_END);
|
|
|
|
Bfprintf(texcache.index, "%s %d %d\n", t->name, t->offset, t->len);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
else OSD_Printf("wtf?\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
goto success;
|
|
|
|
|
|
|
|
failure:
|
|
|
|
initprintf("ERROR: cache failure!\n");
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.currentindex->offset = 0;
|
|
|
|
Bmemset(texcache.currentindex->name,0,sizeof(texcache.currentindex->name));
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
success:
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
}
|
|
|
|
|
2013-05-15 02:19:14 +00:00
|
|
|
#undef WRITEX_FAIL_ON_ERROR
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
static void texcache_setuptexture(int32_t *doalloc, GLuint *glpic)
|
|
|
|
{
|
|
|
|
if (*doalloc&1)
|
|
|
|
{
|
|
|
|
bglGenTextures(1,glpic); //# of textures (make OpenGL allocate structure)
|
|
|
|
*doalloc |= 2; // prevents bglGenTextures being called again if we fail in here
|
|
|
|
}
|
|
|
|
bglBindTexture(GL_TEXTURE_2D,*glpic);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t texcache_loadmips(const texcacheheader *head, GLenum *glerr, int32_t *xsiz, int32_t *ysiz)
|
|
|
|
{
|
|
|
|
int32_t level;
|
|
|
|
texcachepicture pict;
|
|
|
|
char *pic = NULL, *packbuf = NULL;
|
|
|
|
void *midbuf = NULL;
|
|
|
|
int32_t alloclen=0;
|
|
|
|
|
|
|
|
for (level = 0; level==0 || (pict.xdim > 1 || pict.ydim > 1); level++)
|
|
|
|
{
|
|
|
|
GLint format;
|
|
|
|
|
|
|
|
if (texcache_readdata(&pict, sizeof(texcachepicture)))
|
|
|
|
{
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return TEXCACHERR_BUFFERUNDERRUN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// external (little endian) -> native
|
|
|
|
pict.size = B_LITTLE32(pict.size);
|
|
|
|
pict.format = B_LITTLE32(pict.format);
|
|
|
|
pict.xdim = B_LITTLE32(pict.xdim);
|
|
|
|
pict.ydim = B_LITTLE32(pict.ydim);
|
|
|
|
pict.border = B_LITTLE32(pict.border);
|
|
|
|
pict.depth = B_LITTLE32(pict.depth);
|
|
|
|
|
|
|
|
if (level == 0)
|
|
|
|
{
|
|
|
|
if (xsiz) *xsiz = pict.xdim;
|
|
|
|
if (ysiz) *ysiz = pict.ydim;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alloclen < pict.size)
|
|
|
|
{
|
|
|
|
REALLOC_OR_FAIL(pic, pict.size, char);
|
|
|
|
alloclen = pict.size;
|
|
|
|
REALLOC_OR_FAIL(packbuf, alloclen+16, char);
|
|
|
|
REALLOC_OR_FAIL(midbuf, pict.size, void);
|
|
|
|
}
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (dedxtfilter(texcache.filehandle, &pict, pic, midbuf, packbuf, (head->flags&4)==4))
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return TEXCACHERR_DEDXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
bglCompressedTexImage2DARB(GL_TEXTURE_2D,level,pict.format,pict.xdim,pict.ydim,pict.border,pict.size,pic);
|
|
|
|
if ((*glerr=bglGetError()) != GL_NO_ERROR)
|
|
|
|
{
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return TEXCACHERR_COMPTEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
bglGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_INTERNAL_FORMAT, &format);
|
|
|
|
if ((*glerr = bglGetError()) != GL_NO_ERROR)
|
|
|
|
{
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return TEXCACHERR_GETTEXLEVEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pict.format != format)
|
|
|
|
{
|
|
|
|
OSD_Printf("gloadtile_cached: invalid texture cache file format %d %d\n", pict.format, format);
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
failure:
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return TEXCACHERR_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t texcache_loadskin(const texcacheheader *head, int32_t *doalloc, GLuint *glpic, int32_t *xsiz, int32_t *ysiz)
|
|
|
|
{
|
|
|
|
int32_t err=0;
|
|
|
|
GLenum glerr=GL_NO_ERROR;
|
|
|
|
|
|
|
|
texcache_setuptexture(doalloc, glpic);
|
|
|
|
|
|
|
|
CLEAR_GL_ERRORS();
|
|
|
|
|
|
|
|
if ((err = texcache_loadmips(head, &glerr, xsiz, ysiz)))
|
|
|
|
{
|
|
|
|
if (err > 0)
|
2013-05-17 03:42:37 +00:00
|
|
|
initprintf("texcache_loadskin: %s (glerr=%x)\n", texcache_errorstr[err], glerr);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t texcache_loadtile(const texcacheheader *head, int32_t *doalloc, pthtyp *pth)
|
|
|
|
{
|
|
|
|
int32_t err=0;
|
|
|
|
GLenum glerr=GL_NO_ERROR;
|
|
|
|
|
|
|
|
texcache_setuptexture(doalloc, &pth->glpic);
|
|
|
|
|
|
|
|
pth->sizx = head->xdim;
|
|
|
|
pth->sizy = head->ydim;
|
|
|
|
|
|
|
|
CLEAR_GL_ERRORS();
|
|
|
|
|
|
|
|
if ((err = texcache_loadmips(head, &glerr, NULL, NULL)))
|
|
|
|
{
|
|
|
|
if (err > 0)
|
2013-05-17 03:42:37 +00:00
|
|
|
initprintf("texcache_loadtile: %s (glerr=%x)\n", texcache_errorstr[err], glerr);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-15 02:18:27 +00:00
|
|
|
|
|
|
|
void texcache_setupmemcache(void)
|
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
if (!glusememcache || texcache.memcache.noalloc || !texcache_enabled())
|
2013-05-15 02:18:27 +00:00
|
|
|
return;
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.memcache.size = Bfilelength(texcache.filehandle);
|
2013-05-15 02:18:27 +00:00
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (texcache.memcache.size <= 0)
|
2013-05-15 02:18:27 +00:00
|
|
|
return;
|
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.memcache.ptr = (uint8_t *)Brealloc(texcache.memcache.ptr, texcache.memcache.size);
|
2013-05-15 02:18:27 +00:00
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
if (!texcache.memcache.ptr)
|
2013-05-15 02:18:27 +00:00
|
|
|
{
|
2013-05-17 10:41:59 +00:00
|
|
|
initprintf("Failed allocating %d bytes for memcache, disabling memcache.\n", (int)texcache.memcache.size);
|
2013-05-15 02:19:14 +00:00
|
|
|
texcache_clearmemcache();
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.memcache.noalloc = 1;
|
2013-05-15 02:18:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-17 10:41:59 +00:00
|
|
|
if (Bread(texcache.filehandle, texcache.memcache.ptr, texcache.memcache.size) != (bssize_t)texcache.memcache.size)
|
2013-05-15 02:18:27 +00:00
|
|
|
{
|
|
|
|
initprintf("Failed reading texcache into memcache!\n");
|
2013-05-15 02:19:14 +00:00
|
|
|
texcache_clearmemcache();
|
2013-05-17 03:42:37 +00:00
|
|
|
texcache.memcache.noalloc = 1;
|
2013-05-15 02:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 23:05:11 +00:00
|
|
|
#endif
|