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 "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
|
|
|
|
2019-03-01 08:51:50 +00:00
|
|
|
#include "vfs.h"
|
2019-10-10 21:29:13 +00:00
|
|
|
#include "textures.h"
|
|
|
|
#include "bitmap.h"
|
|
|
|
#include "../../glbackend/glbackend.h"
|
2019-03-01 08:51:50 +00:00
|
|
|
|
2019-10-10 21:29:13 +00:00
|
|
|
void gltexinvalidate(int32_t dapicnum, int32_t dapalnum, int32_t dameth)
|
|
|
|
{
|
|
|
|
const int32_t pic = (dapicnum & (GLTEXCACHEADSIZ - 1));
|
|
|
|
|
|
|
|
for (pthtyp* pth = texcache.list[pic]; pth; pth = pth->next)
|
|
|
|
if (pth->picnum == dapicnum && pth->palnum == dapalnum)
|
|
|
|
{
|
|
|
|
pth->flags |= PTH_INVALIDATED;
|
|
|
|
if (pth->flags & PTH_HASFULLBRIGHT)
|
|
|
|
pth->ofb->flags |= PTH_INVALIDATED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make all textures "dirty" so they reload, but not re-allocate
|
|
|
|
//This should be much faster than polymost_glreset()
|
|
|
|
//Use this for palette effects ... but not ones that change every frame!
|
|
|
|
void gltexinvalidatetype(int32_t type)
|
|
|
|
{
|
|
|
|
for (bssize_t j = 0; j <= GLTEXCACHEADSIZ - 1; j++)
|
|
|
|
{
|
|
|
|
for (pthtyp* pth = texcache.list[j]; pth; pth = pth->next)
|
|
|
|
{
|
|
|
|
if (type == INVALIDATE_ALL ||
|
|
|
|
(type == INVALIDATE_ALL_NON_INDEXED && !(pth->flags & PTH_INDEXED)) ||
|
|
|
|
(type == INVALIDATE_ART && pth->hicr == NULL) ||
|
|
|
|
(type == INVALIDATE_ART_NON_INDEXED && pth->hicr == NULL && !(pth->flags & PTH_INDEXED)))
|
|
|
|
{
|
|
|
|
pth->flags |= PTH_INVALIDATED;
|
|
|
|
if (pth->flags & PTH_HASFULLBRIGHT)
|
|
|
|
pth->ofb->flags |= PTH_INVALIDATED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearskins(type);
|
|
|
|
|
|
|
|
#ifdef DEBUGGINGAIDS
|
|
|
|
OSD_Printf("gltexinvalidateall()\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cleartexturecache()
|
|
|
|
{
|
|
|
|
for (bssize_t i = 0; i <= GLTEXCACHEADSIZ - 1; i++)
|
|
|
|
{
|
|
|
|
for (pthtyp* pth = texcache.list[i]; pth;)
|
|
|
|
{
|
|
|
|
pthtyp* const next = pth->next;
|
|
|
|
|
|
|
|
if (pth->flags & PTH_HASFULLBRIGHT)
|
|
|
|
{
|
|
|
|
delete pth->ofb->glpic;
|
|
|
|
Xfree(pth->ofb);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete pth->glpic;
|
|
|
|
Xfree(pth);
|
|
|
|
pth = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
texcache.list[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void polymost_setuptexture(FHardwareTexture* tex, const int32_t dameth, int filter)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!(dameth & DAMETH_CLAMPED))
|
|
|
|
{
|
|
|
|
tex->SetSampler(SamplerRepeat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// For sprite textures, clamping looks better than wrapping
|
|
|
|
tex->SetSampler(SamplerClampXY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void FlipNonSquareBlock(T* dst, const T* src, int x, int y, int srcpitch)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < x; ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < y; ++j)
|
|
|
|
{
|
|
|
|
dst[i * y + j] = src[i + j * srcpitch];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gloadtile_art(int32_t dapic, int32_t dameth, pthtyp* pth, int32_t doalloc)
|
|
|
|
{
|
|
|
|
vec2_16_t const& tsizart = tilesiz[dapic];
|
|
|
|
vec2_t siz = { tsizart.x, tsizart.y };
|
|
|
|
vec2_t ssiz = siz;
|
|
|
|
//POGOTODO: npoty
|
|
|
|
char npoty = 0;
|
|
|
|
|
2019-10-11 21:31:59 +00:00
|
|
|
tileCache(globalpicnum);
|
2019-10-10 21:29:13 +00:00
|
|
|
|
2019-10-11 21:31:59 +00:00
|
|
|
const uint8_t* p = tilePtr(dapic);
|
|
|
|
if (!p)
|
2019-10-10 21:29:13 +00:00
|
|
|
{
|
|
|
|
static uint8_t pix = 255;
|
|
|
|
siz.x = siz.y = 1;
|
|
|
|
|
|
|
|
p = &pix;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if (doalloc)
|
|
|
|
{
|
|
|
|
assert(pth->glpic == nullptr);
|
|
|
|
pth->glpic = GLInterface.NewTexture();
|
|
|
|
pth->glpic->CreateTexture(siz.x, siz.y, true, false);
|
|
|
|
pth->glpic->SetSampler((dameth & DAMETH_CLAMPED) ? SamplerClampXY : SamplerRepeat);
|
|
|
|
|
|
|
|
polymost_setuptexture(pth->glpic, dameth, 0);
|
|
|
|
}
|
|
|
|
TArray<uint8_t> flipped(siz.x * siz.y, true);
|
|
|
|
FlipNonSquareBlock(flipped.Data(), p, siz.y, siz.x, siz.y);
|
|
|
|
|
|
|
|
pth->glpic->LoadTexture(flipped.Data());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pth->picnum = dapic;
|
|
|
|
pth->palnum = 0;
|
|
|
|
pth->shade = 0;
|
|
|
|
pth->effects = 0;
|
|
|
|
pth->flags = PTH_HASALPHA | PTH_ONEBITALPHA | PTH_INDEXED;
|
|
|
|
pth->hicr = NULL;
|
|
|
|
pth->siz = ssiz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32_t gloadtile_hi(int32_t dapic, int32_t dapalnum, int32_t facen, hicreplctyp* hicr,
|
|
|
|
int32_t dameth, pthtyp* pth, int32_t doalloc, polytintflags_t effect)
|
|
|
|
{
|
|
|
|
if (!hicr) return -1;
|
|
|
|
|
|
|
|
char* fn;
|
|
|
|
|
|
|
|
if (facen > 0)
|
|
|
|
{
|
|
|
|
if (!hicr->skybox || facen > 6 || !hicr->skybox->face[facen - 1])
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fn = hicr->skybox->face[facen - 1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!hicr->filename)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fn = hicr->filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto texture = FTexture::GetTexture(fn);
|
|
|
|
|
|
|
|
if (texture == nullptr)
|
|
|
|
{
|
|
|
|
OSD_Printf("hightile: %s (pic %d) not found\n", fn, dapic);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((doalloc & 3) == 1)
|
|
|
|
{
|
|
|
|
pth->glpic = GLInterface.NewTexture();
|
|
|
|
pth->glpic->CreateTexture(texture->GetWidth(), texture->GetHeight(), false, true);
|
|
|
|
}
|
|
|
|
auto image = texture->GetBgraBitmap(nullptr, nullptr);
|
|
|
|
bool hasalpha = texture->GetTranslucency();
|
|
|
|
bool onebitalpha = texture->isMasked();
|
|
|
|
|
|
|
|
pth->glpic->LoadTexture(image);
|
|
|
|
|
|
|
|
#if 0 // I don't really think this is a good idea. The hightile should look indistinguishable to the game compared to the regular one.
|
|
|
|
vec2_t tsiz = { texture->GetWidth(), texture->GetHeight() };
|
|
|
|
// precalculate scaling parameters for replacement
|
|
|
|
if (facen > 0)
|
|
|
|
pth->scale = { (float)tsiz.x * (1.0f / 64.f), (float)tsiz.y * (1.0f / 64.f) };
|
|
|
|
else
|
|
|
|
pth->scale = { (float)tsiz.x / (float)tilesiz[dapic].x, (float)tsiz.y / (float)tilesiz[dapic].y };
|
|
|
|
#else
|
|
|
|
pth->scale = { 1.f,1.f };
|
|
|
|
#endif
|
|
|
|
|
|
|
|
polymost_setuptexture(pth->glpic, dameth, (hicr->flags & HICR_FORCEFILTER) ? TEXFILTER_ON : -1);
|
|
|
|
|
|
|
|
pth->picnum = dapic;
|
|
|
|
pth->effects = effect;
|
|
|
|
pth->flags = PTH_HIGHTILE | ((facen > 0) * PTH_SKYBOX) |
|
|
|
|
(onebitalpha ? PTH_ONEBITALPHA : 0) |
|
|
|
|
(hasalpha ? PTH_HASALPHA : 0) |
|
|
|
|
((hicr->flags & HICR_FORCEFILTER) ? PTH_FORCEFILTER : 0);
|
|
|
|
pth->skyface = facen;
|
|
|
|
pth->hicr = hicr;
|
|
|
|
|
|
|
|
if (facen > 0) pth->siz = { 64, 64 }; else pth->siz = { tilesiz[dapic].x, tilesiz[dapic].y };
|
|
|
|
return 0;
|
|
|
|
}
|
2019-03-01 08:51:50 +00:00
|
|
|
|
2019-06-25 11:29:08 +00:00
|
|
|
#define TEXCACHE_FREEBUFS() { Xfree(pic), Xfree(packbuf), Xfree(midbuf); }
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2013-05-17 03:42:37 +00:00
|
|
|
globaltexcache texcache;
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2018-03-23 04:02:35 +00:00
|
|
|
static pthtyp *texcache_tryart(int32_t const dapicnum, int32_t const dapalnum, int32_t const dashade, int32_t 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)
|
2019-10-10 17:16:27 +00:00
|
|
|
if (pth->picnum == dapicnum && ((pth->flags & (PTH_INDEXED|PTH_HIGHTILE)) == PTH_INDEXED))
|
2019-10-09 22:07:45 +00:00
|
|
|
{
|
|
|
|
if (pth->flags & PTH_INVALIDATED)
|
|
|
|
{
|
|
|
|
pth->flags &= ~PTH_INVALIDATED;
|
2019-10-10 17:30:05 +00:00
|
|
|
gloadtile_art(dapicnum, dameth, pth, 0);
|
2019-10-09 22:07:45 +00:00
|
|
|
pth->palnum = dapalnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pth;
|
|
|
|
}
|
2014-09-30 04:06:05 +00:00
|
|
|
|
|
|
|
pth = (pthtyp *)Xcalloc(1,sizeof(pthtyp));
|
|
|
|
|
2019-10-10 17:30:05 +00:00
|
|
|
gloadtile_art(dapicnum, 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;
|
2019-10-09 22:07:45 +00:00
|
|
|
pth->flags = 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
|
|
|
|
2019-10-09 22:07:45 +00:00
|
|
|
if (drawingskybox && usehightile)
|
|
|
|
{
|
|
|
|
auto si = hicfindskybox(dapicnum, dapalnum);
|
|
|
|
if (si == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
}
|
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;
|
2018-10-07 05:20:19 +00:00
|
|
|
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))
|
2019-10-09 22:07:45 +00:00
|
|
|
&& (pth->flags & (PTH_HIGHTILE | PTH_SKYBOX)) == (PTH_HIGHTILE | (drawingskybox > 0) * PTH_SKYBOX)
|
2017-06-27 02:24:34 +00:00
|
|
|
&& (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;
|
|
|
|
|
2019-09-18 18:44:21 +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
|
|
|
|
2019-09-13 19:43:05 +00:00
|
|
|
int32_t tilestat = gloadtile_hi(dapicnum, dapalnum, drawingskybox, si, dameth, pth, 1, (checktintpal > 0) ? 0 : tintflags);
|
2019-10-09 22:07:45 +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
|
|
|
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(pth);
|
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
|
|
|
}
|
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
|
|
|
|
|
2013-05-15 02:18:27 +00:00
|
|
|
|
2013-06-27 23:05:11 +00:00
|
|
|
#endif
|