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"
|
2014-03-22 09:26:39 +00:00
|
|
|
#include "xxhash.h"
|
2014-09-30 04:18:43 +00:00
|
|
|
#include "kplib.h"
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
#define CLEAR_GL_ERRORS() while(glGetError() != GL_NO_ERROR) { }
|
2013-05-15 02:17:17 +00:00
|
|
|
#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";
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
static const char *texcache_errors[TEXCACHEERRORS] = {
|
2013-05-17 03:42:37 +00:00
|
|
|
"no error",
|
2013-05-15 02:17:17 +00:00
|
|
|
"out of memory!",
|
|
|
|
"read too few bytes from cache file",
|
|
|
|
"dedxtfilter failed",
|
2018-02-16 06:38:21 +00:00
|
|
|
"glCompressedTexImage2DARB failed",
|
|
|
|
"glGetTexLevelParameteriv failed",
|
2013-05-15 02:17:17 +00:00
|
|
|
};
|
|
|
|
|
2015-04-14 08:07:41 +00:00
|
|
|
static pthtyp *texcache_tryart(int32_t const dapicnum, int32_t const dapalnum, int32_t const dashade, int32_t const dameth)
|
2014-09-30 04:06:05 +00:00
|
|
|
{
|
|
|
|
const int32_t j = dapicnum&(GLTEXCACHEADSIZ-1);
|
2014-10-25 03:30:38 +00:00
|
|
|
pthtyp *pth;
|
2015-04-14 08:07:41 +00:00
|
|
|
int32_t tintpalnum = -1;
|
|
|
|
int32_t searchpalnum = dapalnum;
|
2017-06-21 13:46:50 +00:00
|
|
|
polytintflags_t const tintflags = hictinting[dapalnum].f;
|
2014-09-30 04:06:05 +00:00
|
|
|
|
2017-06-21 13:46:59 +00:00
|
|
|
if (tintflags & (HICTINT_USEONART|HICTINT_ALWAYSUSEART))
|
2015-04-14 08:07:41 +00:00
|
|
|
{
|
|
|
|
tintpalnum = dapalnum;
|
2017-06-21 13:46:50 +00:00
|
|
|
if (!(tintflags & HICTINT_APPLYOVERPALSWAP))
|
2015-04-14 08:07:41 +00:00
|
|
|
searchpalnum = 0;
|
|
|
|
}
|
2015-03-09 20:32:11 +00:00
|
|
|
|
2014-09-30 04:06:05 +00:00
|
|
|
// load from art
|
|
|
|
for (pth=texcache.list[j]; pth; pth=pth->next)
|
2016-06-05 04:46:28 +00:00
|
|
|
if (pth->picnum == dapicnum && pth->palnum == dapalnum && pth->shade == dashade &&
|
2015-12-04 11:52:58 +00:00
|
|
|
(pth->flags & (PTH_CLAMPED | PTH_HIGHTILE | PTH_NOTRANSFIX)) ==
|
|
|
|
(TO_PTH_CLAMPED(dameth) | TO_PTH_NOTRANSFIX(dameth)) &&
|
2014-09-30 04:14:21 +00:00
|
|
|
polymost_want_npotytex(dameth, tilesiz[dapicnum].y) == !!(pth->flags&PTH_NPOTWALL)
|
2014-09-30 04:06:05 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (pth->flags & PTH_INVALIDATED)
|
|
|
|
{
|
|
|
|
pth->flags &= ~PTH_INVALIDATED;
|
2015-04-14 08:07:41 +00:00
|
|
|
gloadtile_art(dapicnum, searchpalnum, tintpalnum, dashade, dameth, pth, 0);
|
|
|
|
pth->palnum = dapalnum;
|
2014-09-30 04:06:05 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 00:34:41 +00:00
|
|
|
return pth;
|
2014-09-30 04:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pth = (pthtyp *)Xcalloc(1,sizeof(pthtyp));
|
|
|
|
|
2015-04-14 08:07:41 +00:00
|
|
|
gloadtile_art(dapicnum, searchpalnum, tintpalnum, dashade, dameth, pth, 1);
|
2014-09-30 04:06:05 +00:00
|
|
|
|
2015-04-14 08:07:41 +00:00
|
|
|
pth->palnum = dapalnum;
|
2014-09-30 04:06:05 +00:00
|
|
|
pth->next = texcache.list[j];
|
|
|
|
texcache.list[j] = pth;
|
|
|
|
|
2016-06-21 00:34:41 +00:00
|
|
|
return pth;
|
2014-09-30 04:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pthtyp *texcache_fetchmulti(pthtyp *pth, hicreplctyp *si, int32_t dapicnum, int32_t dameth)
|
|
|
|
{
|
|
|
|
const int32_t j = dapicnum&(GLTEXCACHEADSIZ-1);
|
2014-10-25 03:30:38 +00:00
|
|
|
int32_t i;
|
2014-09-30 04:06:05 +00:00
|
|
|
|
2014-10-25 03:30:38 +00:00
|
|
|
for (i = 0; i <= (GLTEXCACHEADSIZ - 1); i++)
|
2014-09-30 04:06:05 +00:00
|
|
|
{
|
|
|
|
const pthtyp *pth2;
|
|
|
|
|
|
|
|
for (pth2=texcache.list[i]; pth2; pth2=pth2->next)
|
|
|
|
{
|
2017-06-22 01:32:05 +00:00
|
|
|
if (pth2->hicr && pth2->hicr->filename && si->filename && filnamcmp(pth2->hicr->filename, si->filename) == 0)
|
2014-09-30 04:06:05 +00:00
|
|
|
{
|
|
|
|
Bmemcpy(pth, pth2, sizeof(pthtyp));
|
|
|
|
pth->picnum = dapicnum;
|
2015-12-04 11:52:58 +00:00
|
|
|
pth->flags = TO_PTH_CLAMPED(dameth) | TO_PTH_NOTRANSFIX(dameth) |
|
|
|
|
PTH_HIGHTILE | (drawingskybox>0)*PTH_SKYBOX;
|
2014-09-30 04:06:05 +00:00
|
|
|
if (pth2->flags & PTH_HASALPHA)
|
|
|
|
pth->flags |= PTH_HASALPHA;
|
|
|
|
pth->hicr = si;
|
|
|
|
|
|
|
|
pth->next = texcache.list[j];
|
|
|
|
texcache.list[j] = pth;
|
|
|
|
|
|
|
|
return pth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-30 13:53:08 +00:00
|
|
|
// <dashade>: ignored if not in Polymost+r_usetileshades
|
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
|
|
|
{
|
2014-11-22 12:34:29 +00:00
|
|
|
const int32_t j = dapicnum & (GLTEXCACHEADSIZ - 1);
|
2017-06-21 13:46:59 +00:00
|
|
|
hicreplctyp *si = usehightile ? hicfindsubst(dapicnum, dapalnum, hictinting[dapalnum].f & HICTINT_ALWAYSUSEART) : NULL;
|
2014-09-30 04:06:05 +00:00
|
|
|
|
|
|
|
if (drawingskybox && usehightile)
|
|
|
|
if ((si = hicfindskybox(dapicnum, dapalnum)) == NULL)
|
|
|
|
return NULL;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2015-03-09 20:32:36 +00:00
|
|
|
if (!r_usetileshades || (globalflags & GLOBAL_NO_GL_TILESHADES) || getrendermode() != REND_POLYMOST)
|
2014-03-30 13:53:08 +00:00
|
|
|
dashade = 0;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
if (!si)
|
|
|
|
{
|
2014-11-22 12:34:29 +00:00
|
|
|
return (dapalnum >= (MAXPALOOKUPS - RESERVEDPALS) || hicprecaching) ?
|
|
|
|
NULL : texcache_tryart(dapicnum, dapalnum, dashade, dameth);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2017-06-21 13:46:50 +00:00
|
|
|
polytintflags_t const tintflags = hictinting[dapalnum].f;
|
|
|
|
|
|
|
|
const int32_t checktintpal = (tintflags & HICTINT_APPLYOVERALTPAL) ? 0 : si->palnum;
|
|
|
|
const int32_t checkcachepal = (tintflags & HICTINT_IN_MEMORY) || ((tintflags & HICTINT_APPLYOVERALTPAL) && si->palnum > 0) ? dapalnum : si->palnum;
|
2015-04-14 08:07:23 +00:00
|
|
|
|
2013-05-15 02:17:17 +00:00
|
|
|
// load a replacement
|
2015-03-24 00:40:33 +00:00
|
|
|
for (pthtyp *pth = texcache.list[j]; pth; pth = pth->next)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
if (pth->picnum == dapicnum && pth->palnum == checkcachepal && (checktintpal > 0 ? 1 : (pth->effects == tintflags))
|
|
|
|
&& (pth->flags & (PTH_CLAMPED | PTH_HIGHTILE | PTH_SKYBOX | PTH_NOTRANSFIX))
|
|
|
|
== (TO_PTH_CLAMPED(dameth) | TO_PTH_NOTRANSFIX(dameth) | PTH_HIGHTILE | (drawingskybox > 0) * PTH_SKYBOX)
|
|
|
|
&& (drawingskybox > 0 ? (pth->skyface == drawingskybox) : 1))
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2014-05-28 22:40:16 +00:00
|
|
|
if (pth->flags & PTH_INVALIDATED)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2014-05-28 22:40:16 +00:00
|
|
|
pth->flags &= ~PTH_INVALIDATED;
|
|
|
|
|
2015-03-24 00:40:33 +00:00
|
|
|
int32_t tilestat = gloadtile_hi(dapicnum, dapalnum, drawingskybox, si, dameth, pth, 0,
|
2017-06-21 13:46:50 +00:00
|
|
|
(checktintpal > 0) ? 0 : tintflags); // reload tile
|
2014-09-30 04:06:05 +00:00
|
|
|
|
2014-11-22 12:34:29 +00:00
|
|
|
if (!tilestat)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (tilestat == -2) // bad filename
|
|
|
|
hicclearsubst(dapicnum, dapalnum);
|
2017-06-27 02:24:34 +00:00
|
|
|
|
2014-11-22 12:34:29 +00:00
|
|
|
return (drawingskybox || hicprecaching) ? NULL : texcache_tryart(dapicnum, dapalnum, dashade, dameth);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 00:40:33 +00:00
|
|
|
return pth;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 00:40:33 +00:00
|
|
|
pthtyp *pth = (pthtyp *)Xcalloc(1, sizeof(pthtyp));
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
// possibly fetch an already loaded multitexture :_)
|
2017-06-27 11:01:12 +00:00
|
|
|
if (dapalnum == DETAILPAL && texcache_fetchmulti(pth, si, dapicnum, dameth))
|
2014-09-30 04:06:05 +00:00
|
|
|
return pth;
|
2014-05-30 00:02:14 +00:00
|
|
|
|
2015-03-24 00:40:33 +00:00
|
|
|
int32_t tilestat =
|
2017-06-21 13:46:50 +00:00
|
|
|
gloadtile_hi(dapicnum, dapalnum, drawingskybox, si, dameth, pth, 1, (checktintpal > 0) ? 0 : tintflags);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2014-11-22 12:34:29 +00:00
|
|
|
if (!tilestat)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2014-11-22 12:34:29 +00:00
|
|
|
pth->next = texcache.list[j];
|
2015-04-14 21:17:36 +00:00
|
|
|
pth->palnum = checkcachepal;
|
2014-11-22 12:34:29 +00:00
|
|
|
texcache.list[j] = pth;
|
|
|
|
return pth;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:34:29 +00:00
|
|
|
if (tilestat == -2) // bad filename
|
|
|
|
hicclearsubst(dapicnum, dapalnum);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2014-11-22 12:34:29 +00:00
|
|
|
Bfree(pth);
|
|
|
|
|
|
|
|
return (drawingskybox || hicprecaching) ? NULL : texcache_tryart(dapicnum, dapalnum, dashade, dameth);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void texcache_closefiles(void)
|
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
if (texcache.handle != -1)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
Bclose(texcache.handle);
|
|
|
|
texcache.handle = -1;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
2014-10-25 03:30:38 +00:00
|
|
|
MAYBE_FCLOSE_AND_NULL(texcache.index);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void texcache_freeptrs(void)
|
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.entrybufsiz = 0;
|
2014-11-22 12:34:29 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (!texcache.entries)
|
2014-11-22 12:34:29 +00:00
|
|
|
return;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2016-08-27 01:41:21 +00:00
|
|
|
for (bssize_t i = 0; i < texcache.numentries; i++)
|
2017-06-27 02:24:34 +00:00
|
|
|
if (texcache.entries[i])
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2016-08-27 01:41:21 +00:00
|
|
|
for (bssize_t ii = texcache.numentries - 1; ii >= 0; ii--)
|
2017-06-27 02:24:34 +00:00
|
|
|
if (i != ii && texcache.entries[ii] == texcache.entries[i])
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
/*OSD_Printf("removing duplicate cacheptr %d\n",ii);*/
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.entries[ii] = NULL;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
DO_FREE_AND_NULL(texcache.entries[i]->name);
|
|
|
|
DO_FREE_AND_NULL(texcache.entries[i]);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
2014-10-25 03:32:01 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
DO_FREE_AND_NULL(texcache.entries);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2014-10-25 03:30:38 +00:00
|
|
|
static inline void texcache_clearmemcache(void)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
DO_FREE_AND_NULL(texcache.buf);
|
|
|
|
texcache.memsize = -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
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
int32_t len = Bfilelength(texcache.handle);
|
2013-05-15 02:19:14 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (!texcache.buf || texcache.handle == -1 || len <= (int32_t)texcache.memsize)
|
2013-05-15 02:19:14 +00:00
|
|
|
return;
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.buf = (uint8_t *)Brealloc(texcache.buf, len);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (!texcache.buf)
|
2013-05-15 02:19:14 +00:00
|
|
|
{
|
|
|
|
texcache_clearmemcache();
|
2017-06-27 02:24:34 +00:00
|
|
|
initprintf("Failed syncing memcache!\n");
|
|
|
|
glusememcache = 0;
|
2013-05-15 02:19:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
initprintf("Syncing memcache\n");
|
|
|
|
Blseek(texcache.handle, texcache.memsize, BSEEK_SET);
|
|
|
|
if (Bread(texcache.handle, texcache.buf + texcache.memsize, len - texcache.memsize) != (bssize_t)(len-texcache.memsize))
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
initprintf("Failed reading texcache into memcache!\n");
|
2013-05-15 02:19:14 +00:00
|
|
|
texcache_clearmemcache();
|
2017-06-27 02:24:34 +00:00
|
|
|
glusememcache = 0;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
else
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.memsize = len;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void texcache_init(void)
|
|
|
|
{
|
2013-05-17 03:42:37 +00:00
|
|
|
if (!texcache.index)
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.handle = -1;
|
2013-05-17 03:42:37 +00:00
|
|
|
|
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();
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.current = texcache.first = (texcacheindex *)Xcalloc(1, sizeof(texcacheindex));
|
2013-05-17 03:42:37 +00:00
|
|
|
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
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
unlink(TEXCACHEFILE);
|
2013-05-15 02:17:17 +00:00
|
|
|
Bstrcpy(ptempbuf, TEXCACHEFILE);
|
|
|
|
Bstrcat(ptempbuf, ".cache");
|
|
|
|
unlink(ptempbuf);
|
2013-05-15 02:18:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 00:25:06 +00:00
|
|
|
int32_t texcache_enabled(void)
|
2013-05-15 02:18:27 +00:00
|
|
|
{
|
2016-01-08 01:33:25 +00:00
|
|
|
#if defined EDUKE32_GLES || !defined USE_GLEXT
|
|
|
|
return 0;
|
|
|
|
#else
|
2016-03-28 05:15:10 +00:00
|
|
|
if (!glinfo.texcompr || !glusetexcompr || !glusetexcache)
|
2013-05-15 02:18:27 +00:00
|
|
|
return 0;
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (!texcache.index || texcache.handle < 0)
|
2013-05-15 02:18:27 +00:00
|
|
|
{
|
|
|
|
OSD_Printf("Warning: no active cache!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2016-01-08 01:33:25 +00:00
|
|
|
#endif
|
2013-05-15 02:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void texcache_openfiles(void)
|
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
Bstrcpy(ptempbuf, TEXCACHEFILE);
|
|
|
|
Bstrcat(ptempbuf, ".cache");
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.index = Bfopen(ptempbuf, "at+");
|
|
|
|
texcache.handle = Bopen(TEXCACHEFILE, BO_BINARY | BO_CREAT | BO_APPEND | BO_RDWR, BS_IREAD | BS_IWRITE);
|
|
|
|
|
|
|
|
if (!texcache.index || texcache.handle < 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);
|
2017-06-27 02:24:34 +00:00
|
|
|
Bfprintf(texcache.index,"// automatically generated by the engine, 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)
|
|
|
|
{
|
|
|
|
if (!texcache_enabled())
|
|
|
|
return;
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.current = texcache.first;
|
|
|
|
|
|
|
|
int32_t bytes = 0;
|
|
|
|
|
|
|
|
while (texcache.current->next)
|
2013-05-15 02:18:27 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
bytes += texcache.current->len;
|
|
|
|
texcache.current = texcache.current->next;
|
2013-05-15 02:18:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
bytes = Blseek(texcache.handle, 0, BSEEK_END)-bytes;
|
2013-05-15 02:18:27 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (bytes)
|
|
|
|
initprintf("Cache contains %d bytes of garbage data\n", bytes);
|
2013-05-15 02:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
int texcache_loadoffsets(void)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
Bstrcpy(ptempbuf, TEXCACHEFILE);
|
|
|
|
Bstrcat(ptempbuf, ".cache");
|
|
|
|
scriptfile *script = scriptfile_fromfile(ptempbuf);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
if (!script) return -1;
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
int32_t foffset, fsize;
|
|
|
|
char *fname;
|
|
|
|
|
2013-05-15 02:17:17 +00:00
|
|
|
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
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
int const i = hash_find(&texcache.hashes,fname);
|
|
|
|
|
2013-05-15 02:17:17 +00:00
|
|
|
if (i > -1)
|
|
|
|
{
|
|
|
|
// update an existing entry
|
2017-06-27 02:24:34 +00:00
|
|
|
texcacheindex *t = texcache.entries[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
|
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
texcacheindex * const index = texcache.current;
|
|
|
|
|
|
|
|
index->name = Xstrdup(fname);
|
|
|
|
index->offset = foffset;
|
|
|
|
index->len = fsize;
|
|
|
|
index->next = (texcacheindex *) Xcalloc(1, sizeof(texcacheindex));
|
2013-05-17 03:42:37 +00:00
|
|
|
hash_add(&texcache.hashes, fname, texcache.numentries, 1);
|
2017-06-27 02:24:34 +00:00
|
|
|
if (++texcache.numentries > texcache.entrybufsiz)
|
2014-10-25 03:32:01 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.entrybufsiz += 512;
|
|
|
|
texcache.entries = (texcacheindex **) Xrealloc(texcache.entries, sizeof(intptr_t) * texcache.entrybufsiz);
|
2014-10-25 03:32:01 +00:00
|
|
|
}
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.entries[texcache.numentries-1] = texcache.current;
|
|
|
|
texcache.current = index->next;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptfile_close(script);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read from on-disk texcache or its in-memory cache.
|
2017-06-27 02:24:34 +00:00
|
|
|
int texcache_readdata(void *outBuf, int32_t len)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
const int32_t ofilepos = texcache.pos;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.pos += len;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (texcache.buf && texcache.memsize >= ofilepos + len)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
// initprintf("using memcache!\n");
|
2017-06-27 02:24:34 +00:00
|
|
|
Bmemcpy(outBuf, texcache.buf + ofilepos, len);
|
2013-05-17 03:42:37 +00:00
|
|
|
return 0;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (Blseek(texcache.handle, ofilepos, BSEEK_SET) != ofilepos ||
|
|
|
|
Bread(texcache.handle, outBuf, len) < len)
|
2013-05-17 03:42:37 +00:00
|
|
|
return 1;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
char const * texcache_calcid(char *outbuf, const char *filename, const int32_t len, const int32_t dameth, const char effect)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
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
|
2016-05-04 00:25:02 +00:00
|
|
|
} id = { len, dameth, effect, "" };
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
Bstrcpy(id.name, filename);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
size_t const fnlen = Bstrlen(filename);
|
2016-05-04 00:25:02 +00:00
|
|
|
while (Bstrlen(id.name) < BMAX_PATH - fnlen)
|
2017-06-27 02:24:34 +00:00
|
|
|
Bstrcat(id.name, filename);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
Bsprintf(outbuf, "%08x%08x%08x",
|
|
|
|
XXH32((uint8_t *)id.name, fnlen, TEXCACHEMAGIC[3]),
|
|
|
|
XXH32((uint8_t *)id.name, Bstrlen(id.name), TEXCACHEMAGIC[3]),
|
|
|
|
XXH32((uint8_t *)&id, sizeof(struct texcacheid_t), TEXCACHEMAGIC[3]));
|
2016-06-05 04:46:28 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
return outbuf;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
#define FAIL(x) { err = x; goto failure; }
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
// returns 1 on success
|
2017-06-27 02:24:34 +00:00
|
|
|
int texcache_readtexheader(char const * cacheid, texcacheheader *head, int32_t modelp)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2014-05-28 22:40:19 +00:00
|
|
|
if (!texcache_enabled())
|
|
|
|
return 0;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
int32_t i = hash_find(&texcache.hashes, cacheid);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (i < 0 || !texcache.entries[i])
|
2013-05-15 02:17:17 +00:00
|
|
|
return 0; // didn't find it
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.pos = texcache.entries[i]->offset;
|
2014-05-28 22:40:19 +00:00
|
|
|
// initprintf("%s %d got a match for %s offset %d\n",__FILE__, __LINE__, cachefn,offset);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
int err = 0;
|
|
|
|
|
2014-05-28 22:40:19 +00:00
|
|
|
if (texcache_readdata(head, sizeof(texcacheheader)))
|
2017-06-27 02:24:34 +00:00
|
|
|
FAIL(0);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2014-05-28 22:40:19 +00:00
|
|
|
if (Bmemcmp(head->magic, TEXCACHEMAGIC, 4))
|
2017-06-27 02:24:34 +00:00
|
|
|
FAIL(1);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
// native (little-endian) -> internal
|
2017-06-27 02:24:34 +00:00
|
|
|
head->xdim = B_LITTLE32(head->xdim);
|
|
|
|
head->ydim = B_LITTLE32(head->ydim);
|
|
|
|
head->flags = B_LITTLE32(head->flags);
|
2013-05-15 02:17:17 +00:00
|
|
|
head->quality = B_LITTLE32(head->quality);
|
|
|
|
|
2014-05-28 22:40:19 +00:00
|
|
|
if (modelp && head->quality != r_downsize)
|
2017-06-27 02:24:34 +00:00
|
|
|
FAIL(2);
|
2014-05-28 22:40:19 +00:00
|
|
|
if ((head->flags & CACHEAD_COMPRESSED) && glusetexcache != 2)
|
2017-06-27 02:24:34 +00:00
|
|
|
FAIL(3);
|
2014-05-28 22:40:19 +00:00
|
|
|
if (!(head->flags & CACHEAD_COMPRESSED) && glusetexcache == 2)
|
2017-06-27 02:24:34 +00:00
|
|
|
FAIL(4);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2016-03-07 11:21:55 +00:00
|
|
|
// handle nodownsize
|
2016-02-29 06:34:12 +00:00
|
|
|
if (!modelp && !(head->flags & CACHEAD_NODOWNSIZE) && head->quality != r_downsize)
|
2013-05-15 02:17:17 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-05-28 22:40:19 +00:00
|
|
|
if (gltexmaxsize && (head->xdim > (1<<gltexmaxsize) || head->ydim > (1<<gltexmaxsize)))
|
2017-06-27 02:24:34 +00:00
|
|
|
FAIL(5);
|
2014-05-28 22:40:19 +00:00
|
|
|
if (!glinfo.texnpot && (head->flags & CACHEAD_NONPOW2))
|
2017-06-27 02:24:34 +00:00
|
|
|
FAIL(6);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
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
|
2016-05-04 00:25:06 +00:00
|
|
|
|
|
|
|
#if defined USE_GLEXT && !defined EDUKE32_GLES
|
|
|
|
|
|
|
|
void texcache_prewritetex(texcacheheader *head)
|
|
|
|
{
|
|
|
|
Bmemcpy(head->magic, TEXCACHEMAGIC, 4); // sizes are set by caller
|
|
|
|
|
|
|
|
if (glusetexcache == 2)
|
|
|
|
head->flags |= CACHEAD_COMPRESSED;
|
|
|
|
|
|
|
|
// native -> external (little-endian)
|
2017-06-27 02:24:34 +00:00
|
|
|
head->xdim = B_LITTLE32(head->xdim);
|
|
|
|
head->ydim = B_LITTLE32(head->ydim);
|
|
|
|
head->flags = B_LITTLE32(head->flags);
|
2016-05-04 00:25:06 +00:00
|
|
|
head->quality = B_LITTLE32(head->quality);
|
|
|
|
}
|
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
#define WRITEX_FAIL_ON_ERROR() if (glGetError() != GL_NO_ERROR) goto failure
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
void texcache_writetex_fromdriver(char const * const cacheid, texcacheheader *head)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
if (!texcache_enabled()) return;
|
|
|
|
|
2016-05-04 00:25:06 +00:00
|
|
|
GLint gi = GL_FALSE;
|
2018-02-16 06:38:21 +00:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &gi);
|
2013-05-15 02:17:17 +00:00
|
|
|
if (gi != GL_TRUE)
|
|
|
|
{
|
2016-05-04 00:25:06 +00:00
|
|
|
static GLint glGetTexLevelParameterivOK = GL_TRUE;
|
2014-07-22 11:19:13 +00:00
|
|
|
if (glGetTexLevelParameterivOK == GL_TRUE)
|
|
|
|
{
|
|
|
|
OSD_Printf("Error: glGetTexLevelParameteriv returned GL_FALSE!\n");
|
|
|
|
glGetTexLevelParameterivOK = GL_FALSE;
|
|
|
|
}
|
2013-05-15 02:17:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-04 00:25:06 +00:00
|
|
|
texcache_prewritetex(head);
|
2017-06-27 02:24:34 +00:00
|
|
|
Blseek(texcache.handle, 0, BSEEK_END);
|
|
|
|
size_t const offset = Blseek(texcache.handle, 0, BSEEK_CUR);
|
|
|
|
|
|
|
|
texcachepicture pict;
|
|
|
|
|
|
|
|
char *pic = nullptr;
|
|
|
|
char *packbuf = nullptr;
|
|
|
|
void *midbuf = nullptr;
|
|
|
|
size_t alloclen = 0;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
// OSD_Printf("Caching %s, offset 0x%x\n", cachefn, offset);
|
2017-06-27 02:24:34 +00:00
|
|
|
if (Bwrite(texcache.handle, head, sizeof(texcacheheader)) != sizeof(texcacheheader)) goto failure;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
CLEAR_GL_ERRORS();
|
|
|
|
|
2016-05-04 00:25:06 +00:00
|
|
|
for (uint32_t level = 0, padx = 0, pady = 0; level == 0 || (padx > 1 || pady > 1); ++level)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
2018-02-16 06:38:21 +00:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_COMPRESSED, &gi);
|
2017-06-27 02:24:34 +00:00
|
|
|
WRITEX_FAIL_ON_ERROR();
|
|
|
|
if (gi != GL_TRUE)
|
|
|
|
goto failure; // an uncompressed mipmap
|
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_INTERNAL_FORMAT, &gi);
|
2017-06-27 02:24:34 +00:00
|
|
|
WRITEX_FAIL_ON_ERROR();
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2015-10-10 06:57:41 +00:00
|
|
|
#if defined __APPLE__ && defined POLYMER
|
2013-05-15 02:17:17 +00:00
|
|
|
if (pr_ati_textureformat_one && gi == 1) gi = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
|
|
|
|
#endif
|
|
|
|
// native -> external (little endian)
|
|
|
|
pict.format = B_LITTLE32(gi);
|
2017-06-27 02:24:34 +00:00
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &gi);
|
2017-06-27 02:24:34 +00:00
|
|
|
WRITEX_FAIL_ON_ERROR();
|
|
|
|
padx = gi;
|
|
|
|
pict.xdim = B_LITTLE32(gi);
|
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_HEIGHT, &gi);
|
2017-06-27 02:24:34 +00:00
|
|
|
WRITEX_FAIL_ON_ERROR();
|
|
|
|
pady = gi;
|
|
|
|
pict.ydim = B_LITTLE32(gi);
|
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_BORDER, &gi);
|
2017-06-27 02:24:34 +00:00
|
|
|
WRITEX_FAIL_ON_ERROR();
|
2013-05-15 02:17:17 +00:00
|
|
|
pict.border = B_LITTLE32(gi);
|
2017-06-27 02:24:34 +00:00
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_DEPTH, &gi);
|
2017-06-27 02:24:34 +00:00
|
|
|
WRITEX_FAIL_ON_ERROR();
|
2013-05-15 02:17:17 +00:00
|
|
|
pict.depth = B_LITTLE32(gi);
|
2017-06-27 02:24:34 +00:00
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &gi);
|
2017-06-27 02:24:34 +00:00
|
|
|
WRITEX_FAIL_ON_ERROR();
|
|
|
|
uint32_t miplen = gi;
|
|
|
|
pict.size = B_LITTLE32(gi);
|
2016-05-04 00:25:06 +00:00
|
|
|
|
2013-05-15 02:17:17 +00:00
|
|
|
if (alloclen < miplen)
|
|
|
|
{
|
|
|
|
alloclen = miplen;
|
2017-06-27 02:24:34 +00:00
|
|
|
pic = (char *)Xrealloc(pic, miplen);
|
|
|
|
packbuf = (char *)Xrealloc(packbuf, miplen);
|
|
|
|
midbuf = (void *)Xrealloc(midbuf, miplen);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
glGetCompressedTexImage(GL_TEXTURE_2D, level, pic);
|
2017-06-27 02:24:34 +00:00
|
|
|
WRITEX_FAIL_ON_ERROR();
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (Bwrite(texcache.handle, &pict, sizeof(texcachepicture)) != sizeof(texcachepicture)) goto failure;
|
|
|
|
if (dxtfilter(texcache.handle, &pict, pic, midbuf, packbuf, miplen)) goto failure;
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache_postwritetex(cacheid, offset);
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
failure:
|
|
|
|
initprintf("ERROR: cache failure!\n");
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.current->offset = 0;
|
|
|
|
Bfree(texcache.current->name);
|
2013-05-15 02:17:17 +00:00
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
}
|
|
|
|
|
2013-05-15 02:19:14 +00:00
|
|
|
#undef WRITEX_FAIL_ON_ERROR
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
void texcache_postwritetex(char const * const cacheid, int32_t const offset)
|
2016-05-04 00:25:06 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
int32_t const i = hash_find(&texcache.hashes, cacheid);
|
|
|
|
|
2016-05-04 00:25:06 +00:00
|
|
|
texcacheindex *t;
|
2017-06-27 02:24:34 +00:00
|
|
|
|
2016-05-04 00:25:06 +00:00
|
|
|
if (i > -1)
|
|
|
|
{
|
|
|
|
// update an existing entry
|
2017-06-27 02:24:34 +00:00
|
|
|
t = texcache.entries[i];
|
|
|
|
|
2016-05-04 00:25:06 +00:00
|
|
|
t->offset = offset;
|
2017-06-27 02:24:34 +00:00
|
|
|
t->len = Blseek(texcache.handle, 0, BSEEK_CUR) - t->offset;
|
2016-05-04 00:25:06 +00:00
|
|
|
/*initprintf("%s %d got a match for %s offset %d\n",__FILE__, __LINE__, cachefn,offset);*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
t = texcache.current;
|
|
|
|
|
|
|
|
Bfree(t->name);
|
|
|
|
t->name = Xstrdup(cacheid);
|
2016-05-04 00:25:06 +00:00
|
|
|
t->offset = offset;
|
2017-06-27 02:24:34 +00:00
|
|
|
t->len = Blseek(texcache.handle, 0, BSEEK_CUR) - t->offset;
|
|
|
|
t->next = (texcacheindex *)Xcalloc(1, sizeof(texcacheindex));
|
|
|
|
|
|
|
|
hash_add(&texcache.hashes, cacheid, texcache.numentries, 0);
|
2016-05-04 00:25:06 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (++texcache.numentries > texcache.entrybufsiz)
|
2016-05-04 00:25:06 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.entrybufsiz += 512;
|
|
|
|
texcache.entries = (texcacheindex **)Xrealloc(texcache.entries, sizeof(intptr_t) * texcache.entrybufsiz);
|
2016-05-04 00:25:06 +00:00
|
|
|
}
|
2017-06-27 02:24:34 +00:00
|
|
|
|
|
|
|
texcache.entries[texcache.numentries - 1] = t;
|
|
|
|
texcache.current = t->next;
|
2016-05-04 00:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (texcache.index)
|
|
|
|
{
|
|
|
|
fseek(texcache.index, 0, BSEEK_END);
|
|
|
|
Bfprintf(texcache.index, "%s %d %d\n", t->name, t->offset, t->len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
OSD_Printf("wtf?\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-05-15 02:17:17 +00:00
|
|
|
static void texcache_setuptexture(int32_t *doalloc, GLuint *glpic)
|
|
|
|
{
|
|
|
|
if (*doalloc&1)
|
|
|
|
{
|
2018-02-16 06:38:21 +00:00
|
|
|
glGenTextures(1, glpic); //# of textures (make OpenGL allocate structure)
|
|
|
|
*doalloc |= 2; // prevents glGenTextures being called again if we fail in here
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
2017-06-27 02:24:34 +00:00
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, *glpic);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 19:24:54 +00:00
|
|
|
static int32_t texcache_loadmips(const texcacheheader *head, GLenum *glerr)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
texcachepicture pict;
|
2017-06-27 02:24:34 +00:00
|
|
|
|
|
|
|
char *pic = nullptr;
|
|
|
|
char *packbuf = nullptr;
|
|
|
|
void *midbuf = nullptr;
|
|
|
|
|
2013-05-15 02:17:17 +00:00
|
|
|
int32_t alloclen=0;
|
|
|
|
|
2016-05-04 00:25:06 +00:00
|
|
|
#if !defined USE_GLEXT && defined EDUKE32_GLES
|
|
|
|
UNREFERENCED_PARAMETER(glerr);
|
|
|
|
UNREFERENCED_PARAMETER(head);
|
2015-03-24 00:40:33 +00:00
|
|
|
#endif
|
2016-05-04 00:25:06 +00:00
|
|
|
|
2016-08-27 01:41:21 +00:00
|
|
|
for (bssize_t level = 0; level==0 || (pict.xdim > 1 || pict.ydim > 1); level++)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
if (texcache_readdata(&pict, sizeof(texcachepicture)))
|
|
|
|
{
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return TEXCACHERR_BUFFERUNDERRUN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// external (little endian) -> native
|
2017-06-27 02:24:34 +00:00
|
|
|
pict.size = B_LITTLE32(pict.size);
|
2013-05-15 02:17:17 +00:00
|
|
|
pict.format = B_LITTLE32(pict.format);
|
2017-06-27 02:24:34 +00:00
|
|
|
pict.xdim = B_LITTLE32(pict.xdim);
|
|
|
|
pict.ydim = B_LITTLE32(pict.ydim);
|
2013-05-15 02:17:17 +00:00
|
|
|
pict.border = B_LITTLE32(pict.border);
|
2017-06-27 02:24:34 +00:00
|
|
|
pict.depth = B_LITTLE32(pict.depth);
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
if (alloclen < pict.size)
|
|
|
|
{
|
|
|
|
alloclen = pict.size;
|
2017-06-27 02:24:34 +00:00
|
|
|
pic = (char *)Xrealloc(pic, pict.size);
|
|
|
|
packbuf = (char *)Xrealloc(packbuf, pict.size + 16);
|
|
|
|
midbuf = (void *)Xrealloc(midbuf, pict.size);
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 00:25:06 +00:00
|
|
|
#if defined USE_GLEXT && !defined EDUKE32_GLES
|
2017-06-27 02:24:34 +00:00
|
|
|
if (dedxtfilter(texcache.handle, &pict, pic, midbuf, packbuf, (head->flags & CACHEAD_COMPRESSED) != 0))
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return TEXCACHERR_DEDXT;
|
|
|
|
}
|
|
|
|
|
2018-02-16 06:38:21 +00:00
|
|
|
glCompressedTexImage2D(GL_TEXTURE_2D, level, pict.format, pict.xdim, pict.ydim, pict.border, pict.size, pic);
|
|
|
|
if ((*glerr=glGetError()) != GL_NO_ERROR)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return TEXCACHERR_COMPTEX;
|
|
|
|
}
|
|
|
|
|
2016-01-08 01:33:25 +00:00
|
|
|
GLint format;
|
2018-02-16 06:38:21 +00:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_INTERNAL_FORMAT, &format);
|
|
|
|
if ((*glerr = glGetError()) != GL_NO_ERROR)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2015-03-24 00:40:33 +00:00
|
|
|
#endif
|
2013-05-15 02:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEXCACHE_FREEBUFS();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:24:54 +00:00
|
|
|
int32_t texcache_loadskin(const texcacheheader *head, int32_t *doalloc, GLuint *glpic, vec2_t *siz)
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
int32_t err=0;
|
|
|
|
GLenum glerr=GL_NO_ERROR;
|
|
|
|
|
|
|
|
texcache_setuptexture(doalloc, glpic);
|
|
|
|
|
2016-03-04 19:24:54 +00:00
|
|
|
siz->x = head->xdim;
|
|
|
|
siz->y = head->ydim;
|
|
|
|
|
2013-05-15 02:17:17 +00:00
|
|
|
CLEAR_GL_ERRORS();
|
|
|
|
|
2016-03-04 19:24:54 +00:00
|
|
|
if ((err = texcache_loadmips(head, &glerr)))
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
if (err > 0)
|
2017-06-27 02:24:34 +00:00
|
|
|
initprintf("texcache_loadskin: %s (glerr=%x)\n", texcache_errors[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)
|
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
int32_t err = 0;
|
|
|
|
GLenum glerr = GL_NO_ERROR;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
texcache_setuptexture(doalloc, &pth->glpic);
|
|
|
|
|
2014-09-30 04:18:43 +00:00
|
|
|
pth->siz.x = head->xdim;
|
|
|
|
pth->siz.y = head->ydim;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
|
|
|
CLEAR_GL_ERRORS();
|
|
|
|
|
2016-03-04 19:24:54 +00:00
|
|
|
if ((err = texcache_loadmips(head, &glerr)))
|
2013-05-15 02:17:17 +00:00
|
|
|
{
|
|
|
|
if (err > 0)
|
2017-06-27 02:24:34 +00:00
|
|
|
initprintf("texcache_loadtile: %s (glerr=%x)\n", texcache_errors[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)
|
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
if (!glusememcache || !texcache_enabled())
|
2013-05-15 02:18:27 +00:00
|
|
|
return;
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.memsize = Bfilelength(texcache.handle);
|
2013-05-15 02:18:27 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (texcache.memsize <= 0)
|
2013-05-15 02:18:27 +00:00
|
|
|
return;
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
texcache.buf = (uint8_t *)Brealloc(texcache.buf, texcache.memsize);
|
2013-05-15 02:18:27 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (!texcache.buf)
|
2013-05-15 02:18:27 +00:00
|
|
|
{
|
2017-06-27 02:24:34 +00:00
|
|
|
initprintf("Failed allocating %d bytes for memcache!\n", (int)texcache.memsize);
|
2013-05-15 02:19:14 +00:00
|
|
|
texcache_clearmemcache();
|
2017-06-27 02:24:34 +00:00
|
|
|
glusememcache = 0;
|
2013-05-15 02:18:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
if (Bread(texcache.handle, texcache.buf, texcache.memsize) != (bssize_t)texcache.memsize)
|
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();
|
2017-06-27 02:24:34 +00:00
|
|
|
glusememcache = 0;
|
2013-05-15 02:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 23:05:11 +00:00
|
|
|
#endif
|