From 84a2695cdca40d6241de431200c2544901f9f3e0 Mon Sep 17 00:00:00 2001 From: hendricks266 Date: Wed, 4 May 2016 00:25:06 +0000 Subject: [PATCH] Polymost: Clean up and rearrange some texcache code. No functional changes. git-svn-id: https://svn.eduke32.com/eduke32@5710 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/include/dxtfilter.h | 9 ++ polymer/eduke32/build/include/texcache.h | 5 +- polymer/eduke32/build/src/dxtfilter.c | 17 +- polymer/eduke32/build/src/mdsprite.c | 4 +- polymer/eduke32/build/src/polymost.c | 4 +- polymer/eduke32/build/src/texcache.c | 185 ++++++++++------------ 6 files changed, 111 insertions(+), 113 deletions(-) diff --git a/polymer/eduke32/build/include/dxtfilter.h b/polymer/eduke32/build/include/dxtfilter.h index 36a053317..f8890ddcd 100644 --- a/polymer/eduke32/build/include/dxtfilter.h +++ b/polymer/eduke32/build/include/dxtfilter.h @@ -1,2 +1,11 @@ + +#ifndef DXTFILTER_H_ +#define DXTFILTER_H_ + +void dxt_handle_io(int32_t fil, int32_t len, void *midbuf, char *packbuf); +int32_t dedxt_handle_io(int32_t fil, int32_t, void *midbuf, int32_t mbufsiz, char *packbuf, int32_t ispacked); + int32_t dxtfilter(int32_t fil, const texcachepicture *pict, const char *pic, void *midbuf, char *packbuf, uint32_t miplen); int32_t dedxtfilter(int32_t fil, const texcachepicture *pict, char *pic, void *midbuf, char *packbuf, int32_t ispacked); + +#endif diff --git a/polymer/eduke32/build/include/texcache.h b/polymer/eduke32/build/include/texcache.h index fb2e84853..8d02d1352 100644 --- a/polymer/eduke32/build/include/texcache.h +++ b/polymer/eduke32/build/include/texcache.h @@ -52,6 +52,7 @@ extern globaltexcache texcache; extern char TEXCACHEFILE[BMAX_PATH]; +extern int32_t texcache_enabled(void); extern void texcache_freeptrs(void); extern void texcache_syncmemcache(void); extern void texcache_init(void); @@ -61,7 +62,9 @@ extern pthtyp *texcache_fetch(int32_t dapicnum, int32_t dapalnum, int32_t dashad extern int32_t texcache_loadskin(const texcacheheader *head, int32_t *doalloc, GLuint *glpic, vec2_t *siz); extern int32_t texcache_loadtile(const texcacheheader *head, int32_t *doalloc, pthtyp *pth); extern char const * texcache_calcid(char *cachefn, const char *fn, const int32_t len, const int32_t dameth, const char effect); -extern void texcache_writetex(char const * cachefn, texcacheheader *head); +extern void texcache_prewritetex(texcacheheader *head); +extern void texcache_postwritetex(char const * const cachefn, int32_t const offset); +extern void texcache_writetex_fromdriver(char const * cachefn, texcacheheader *head); extern int32_t texcache_readtexheader(char const * cachefn, texcacheheader *head, int32_t modelp); extern void texcache_openfiles(void); extern void texcache_setupmemcache(void); diff --git a/polymer/eduke32/build/src/dxtfilter.c b/polymer/eduke32/build/src/dxtfilter.c index 250856ea4..748fd636a 100644 --- a/polymer/eduke32/build/src/dxtfilter.c +++ b/polymer/eduke32/build/src/dxtfilter.c @@ -43,6 +43,7 @@ Description of Ken's filter to improve LZW compression of DXT1 format by ~15%: ( #include "texcache.h" #include "lz4.h" +#ifndef EDUKE32_GLES static uint16_t dxt_hicosub(uint16_t c) { int32_t r, g, b; @@ -60,8 +61,9 @@ static uint16_t dedxt_hicoadd(uint16_t c) b = ((c>> 0)+(g>>1))&31; return((r<<11)+(g<<5)+b); } +#endif -static void dxt_handle_io(int32_t fil, int32_t len, void *midbuf, char *packbuf) +void dxt_handle_io(int32_t fil, int32_t len, void *midbuf, char *packbuf) { void *writebuf; int32_t j, cleng; @@ -89,7 +91,7 @@ static void dxt_handle_io(int32_t fil, int32_t len, void *midbuf, char *packbuf) Bwrite(fil, writebuf, cleng); } -static int32_t dedxt_handle_io(int32_t fil, int32_t j /* TODO: better name */, +int32_t dedxt_handle_io(int32_t fil, int32_t j /* TODO: better name */, void *midbuf, int32_t mbufsiz, char *packbuf, int32_t ispacked) { void *inbuf; @@ -133,19 +135,18 @@ static int32_t dedxt_handle_io(int32_t fil, int32_t j /* TODO: better name */, return 0; } +#ifndef EDUKE32_GLES // NOTE: members are in external (little) endianness. int32_t dxtfilter(int32_t fil, const texcachepicture *pict, const char *pic, void *midbuf, char *packbuf, uint32_t miplen) { uint32_t j, k, offs, stride; char *cptr; -#if !defined EDUKE32_GLES if ((pict->format == (signed) B_LITTLE32(GL_COMPRESSED_RGB_S3TC_DXT1_EXT)) || (pict->format == (signed) B_LITTLE32(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT))) { offs = 0; stride = 8; } else if ((pict->format == (signed) B_LITTLE32(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT)) || (pict->format == (signed) B_LITTLE32(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT))) { offs = 8; stride = 16; } else -#endif { offs = 0; stride = 8; } if (stride == 16) //If DXT3... @@ -181,10 +182,6 @@ int32_t dxtfilter(int32_t fil, const texcachepicture *pict, const char *pic, voi dxt_handle_io(fil, tabledivide32(miplen, stride)<<2, midbuf, packbuf); -#if defined EDUKE32_GLES - UNREFERENCED_PARAMETER(pict); -#endif - return 0; } @@ -194,13 +191,11 @@ int32_t dedxtfilter(int32_t fil, const texcachepicture *pict, char *pic, void *m int32_t j, k, offs, stride; char *cptr; -#if !defined EDUKE32_GLES if ((pict->format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT) || (pict->format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT)) { offs = 0; stride = 8; } else if ((pict->format == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT) || (pict->format == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)) { offs = 8; stride = 16; } else -#endif { offs = 0; stride = 8; } if (stride == 16) //If DXT3... @@ -246,3 +241,5 @@ int32_t dedxtfilter(int32_t fil, const texcachepicture *pict, char *pic, void *m return 0; } #endif + +#endif diff --git a/polymer/eduke32/build/src/mdsprite.c b/polymer/eduke32/build/src/mdsprite.c index 2c1ed3144..822953b42 100644 --- a/polymer/eduke32/build/src/mdsprite.c +++ b/polymer/eduke32/build/src/mdsprite.c @@ -963,6 +963,7 @@ int32_t mdloadskin(md2model_t *m, int32_t number, int32_t pal, int32_t surf) bglTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); bglTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); +#if defined USE_GLEXT && !defined EDUKE32_GLES if (!gotcache && glinfo.texcompr && glusetexcache && !(sk->flags & HICR_NOTEXCOMPRESS) && (glusetexcompr == 2 || (glusetexcompr && !(sk->flags & HICR_ARTIMMUNITY)))) { @@ -977,7 +978,7 @@ int32_t mdloadskin(md2model_t *m, int32_t number, int32_t pal, int32_t surf) (sk->flags & (HICR_NODOWNSIZE|HICR_ARTIMMUNITY) ? CACHEAD_NODOWNSIZE : 0); /// OSD_Printf("Caching \"%s\"\n",fn); - texcache_writetex(texcacheid, &cachead); + texcache_writetex_fromdriver(texcacheid, &cachead); if (willprint) { @@ -989,6 +990,7 @@ int32_t mdloadskin(md2model_t *m, int32_t number, int32_t pal, int32_t surf) else OSD_Printf("Cached skin \"%s\"\n", fn); } +#endif if (willprint) { diff --git a/polymer/eduke32/build/src/polymost.c b/polymer/eduke32/build/src/polymost.c index c575a4a1e..5d4fea605 100644 --- a/polymer/eduke32/build/src/polymost.c +++ b/polymer/eduke32/build/src/polymost.c @@ -1518,6 +1518,7 @@ int32_t gloadtile_hi(int32_t dapic,int32_t dapalnum, int32_t facen, hicreplctyp pth->skyface = facen; pth->hicr = hicr; +#if defined USE_GLEXT && !defined EDUKE32_GLES if (!gotcache && glinfo.texcompr && glusetexcache && !(hicr->flags & HICR_NOTEXCOMPRESS) && (glusetexcompr == 2 || (glusetexcompr && !(hicr->flags & HICR_ARTIMMUNITY)))) { @@ -1533,7 +1534,7 @@ int32_t gloadtile_hi(int32_t dapic,int32_t dapalnum, int32_t facen, hicreplctyp (hicr->flags & (HICR_NODOWNSIZE|HICR_ARTIMMUNITY) ? CACHEAD_NODOWNSIZE : 0); /// OSD_Printf("Caching \"%s\"\n", fn); - texcache_writetex(texcacheid, &cachead); + texcache_writetex_fromdriver(texcacheid, &cachead); if (willprint) { @@ -1546,6 +1547,7 @@ int32_t gloadtile_hi(int32_t dapic,int32_t dapalnum, int32_t facen, hicreplctyp else OSD_Printf("Cached \"%s\"\n", fn); } +#endif if (willprint) { diff --git a/polymer/eduke32/build/src/texcache.c b/polymer/eduke32/build/src/texcache.c index 77d64ce7d..7687bcc67 100644 --- a/polymer/eduke32/build/src/texcache.c +++ b/polymer/eduke32/build/src/texcache.c @@ -282,7 +282,7 @@ static void texcache_deletefiles(void) unlink(ptempbuf); } -static int32_t texcache_enabled(void) +int32_t texcache_enabled(void) { #if defined EDUKE32_GLES || !defined USE_GLEXT return 0; @@ -525,42 +525,11 @@ failure: } #undef READTEXHEADER_FAILURE -#define WRITEX_FAIL_ON_ERROR() if (bglGetError() != GL_NO_ERROR) goto failure -void texcache_writetex(char const * const cachefn, texcacheheader *head) +#if defined USE_GLEXT && !defined EDUKE32_GLES + +void texcache_prewritetex(texcacheheader *head) { - char *pic = NULL, *packbuf = NULL; - void *midbuf = NULL; - uint32_t alloclen=0; -#ifndef EDUKE32_GLES - static GLint glGetTexLevelParameterivOK = GL_TRUE; - uint32_t level=0; - uint32_t padx=0, pady=0; - GLint gi; -#endif - int32_t offset = 0; - - if (!texcache_enabled()) return; - -#ifndef EDUKE32_GLES - gi = GL_FALSE; - bglGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_ARB, &gi); - if (gi != GL_TRUE) - { - if (glGetTexLevelParameterivOK == GL_TRUE) - { - OSD_Printf("Error: glGetTexLevelParameteriv returned GL_FALSE!\n"); - glGetTexLevelParameterivOK = GL_FALSE; - } - return; - } -#endif - - Blseek(texcache.filehandle, 0, BSEEK_END); - - offset = Blseek(texcache.filehandle, 0, BSEEK_CUR); - // OSD_Printf("Caching %s, offset 0x%x\n", cachefn, offset); - Bmemcpy(head->magic, TEXCACHEMAGIC, 4); // sizes are set by caller if (glusetexcache == 2) @@ -571,19 +540,43 @@ void texcache_writetex(char const * const cachefn, texcacheheader *head) head->ydim = B_LITTLE32(head->ydim); head->flags = B_LITTLE32(head->flags); head->quality = B_LITTLE32(head->quality); +} +#define WRITEX_FAIL_ON_ERROR() if (bglGetError() != GL_NO_ERROR) goto failure + +void texcache_writetex_fromdriver(char const * const cachefn, texcacheheader *head) +{ + texcachepicture pict; + char *pic = NULL, *packbuf = NULL; + void *midbuf = NULL; + uint32_t alloclen=0; + + if (!texcache_enabled()) return; + + GLint gi = GL_FALSE; + bglGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_ARB, &gi); + if (gi != GL_TRUE) + { + static GLint glGetTexLevelParameterivOK = GL_TRUE; + if (glGetTexLevelParameterivOK == GL_TRUE) + { + OSD_Printf("Error: glGetTexLevelParameteriv returned GL_FALSE!\n"); + glGetTexLevelParameterivOK = GL_FALSE; + } + return; + } + + texcache_prewritetex(head); + + Blseek(texcache.filehandle, 0, BSEEK_END); + int32_t offset = Blseek(texcache.filehandle, 0, BSEEK_CUR); + // OSD_Printf("Caching %s, offset 0x%x\n", cachefn, offset); if (Bwrite(texcache.filehandle, head, sizeof(texcacheheader)) != sizeof(texcacheheader)) goto failure; CLEAR_GL_ERRORS(); -#ifndef EDUKE32_GLES - for (level = 0; level==0 || (padx > 1 || pady > 1); level++) -#endif + for (uint32_t level = 0, padx = 0, pady = 0; level == 0 || (padx > 1 || pady > 1); ++level) { - uint32_t miplen; - texcachepicture pict; - -#ifndef EDUKE32_GLES 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(); @@ -602,15 +595,8 @@ void texcache_writetex(char const * const cachefn, texcacheheader *head) 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); -#else // TODO: actually code this ;) -// pict.format = GL_ETC1_RGB8_OES; - pict.xdim = head->xdim; - pict.ydim = head->ydim; - pict.border = 0; - pict.depth = 16; - miplen = 0; -#endif + uint32_t miplen = gi; pict.size = B_LITTLE32(gi); + if (alloclen < miplen) { pic = (char *)Xrealloc(pic, miplen); @@ -619,51 +605,13 @@ void texcache_writetex(char const * const cachefn, texcacheheader *head) midbuf = (void *)Xrealloc(midbuf, miplen); } -#ifdef USE_GLEXT bglGetCompressedTexImageARB(GL_TEXTURE_2D, level, pic); WRITEX_FAIL_ON_ERROR(); -#endif if (Bwrite(texcache.filehandle, &pict, sizeof(texcachepicture)) != sizeof(texcachepicture)) goto failure; if (dxtfilter(texcache.filehandle, &pict, pic, midbuf, packbuf, miplen)) goto failure; } - { - texcacheindex *t; - int32_t i = hash_find(&texcache.hashes, cachefn); - if (i > -1) - { - // update an existing entry - t = texcache.iptrs[i]; - t->offset = offset; - t->len = Blseek(texcache.filehandle, 0, BSEEK_CUR) - t->offset; - /*initprintf("%s %d got a match for %s offset %d\n",__FILE__, __LINE__, cachefn,offset);*/ - } - else - { - t = texcache.currentindex; - Bstrcpy(t->name, cachefn); - t->offset = offset; - t->len = Blseek(texcache.filehandle, 0, BSEEK_CUR) - t->offset; - t->next = (texcacheindex *)Xcalloc(1, sizeof(texcacheindex)); - - hash_add(&texcache.hashes, cachefn, texcache.numentries, 0); - if (++texcache.numentries > texcache.iptrcnt) - { - texcache.iptrcnt += 512; - texcache.iptrs = (texcacheindex **)Xrealloc(texcache.iptrs, sizeof(intptr_t) * texcache.iptrcnt); - } - texcache.iptrs[texcache.numentries - 1] = t; - texcache.currentindex = t->next; - } - - 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"); - } + texcache_postwritetex(cachefn, offset); goto success; @@ -678,6 +626,47 @@ success: #undef WRITEX_FAIL_ON_ERROR +void texcache_postwritetex(char const * const cachefn, int32_t const offset) +{ + texcacheindex *t; + int32_t i = hash_find(&texcache.hashes, cachefn); + if (i > -1) + { + // update an existing entry + t = texcache.iptrs[i]; + t->offset = offset; + t->len = Blseek(texcache.filehandle, 0, BSEEK_CUR) - t->offset; + /*initprintf("%s %d got a match for %s offset %d\n",__FILE__, __LINE__, cachefn,offset);*/ + } + else + { + t = texcache.currentindex; + Bstrcpy(t->name, cachefn); + t->offset = offset; + t->len = Blseek(texcache.filehandle, 0, BSEEK_CUR) - t->offset; + t->next = (texcacheindex *)Xcalloc(1, sizeof(texcacheindex)); + + hash_add(&texcache.hashes, cachefn, texcache.numentries, 0); + if (++texcache.numentries > texcache.iptrcnt) + { + texcache.iptrcnt += 512; + texcache.iptrs = (texcacheindex **)Xrealloc(texcache.iptrs, sizeof(intptr_t) * texcache.iptrcnt); + } + texcache.iptrs[texcache.numentries - 1] = t; + texcache.currentindex = t->next; + } + + 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 + static void texcache_setuptexture(int32_t *doalloc, GLuint *glpic) { if (*doalloc&1) @@ -695,10 +684,12 @@ static int32_t texcache_loadmips(const texcacheheader *head, GLenum *glerr) void *midbuf = NULL; int32_t alloclen=0; -#ifndef EDUKE32_GLES - int32_t level = 0; - for (level = 0; level==0 || (pict.xdim > 1 || pict.ydim > 1); level++) +#if !defined USE_GLEXT && defined EDUKE32_GLES + UNREFERENCED_PARAMETER(glerr); + UNREFERENCED_PARAMETER(head); #endif + + for (int32_t level = 0; level==0 || (pict.xdim > 1 || pict.ydim > 1); level++) { if (texcache_readdata(&pict, sizeof(texcachepicture))) { @@ -722,6 +713,7 @@ static int32_t texcache_loadmips(const texcacheheader *head, GLenum *glerr) midbuf = (void *)Xrealloc(midbuf, pict.size); } +#if defined USE_GLEXT && !defined EDUKE32_GLES if (dedxtfilter(texcache.filehandle, &pict, pic, midbuf, packbuf, (head->flags & CACHEAD_COMPRESSED)!=0)) { @@ -729,16 +721,13 @@ static int32_t texcache_loadmips(const texcacheheader *head, GLenum *glerr) return TEXCACHERR_DEDXT; } -#ifdef USE_GLEXT 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; } -#endif -#ifndef EDUKE32_GLES GLint format; bglGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_INTERNAL_FORMAT, &format); if ((*glerr = bglGetError()) != GL_NO_ERROR) @@ -756,10 +745,6 @@ static int32_t texcache_loadmips(const texcacheheader *head, GLenum *glerr) #endif } -#if !defined USE_GLEXT && defined EDUKE32_GLES - UNREFERENCED_PARAMETER(glerr); -#endif - TEXCACHE_FREEBUFS(); return 0; }