diff options
| author | xolatile | 2025-08-10 00:19:46 +0200 |
|---|---|---|
| committer | xolatile | 2025-08-10 00:19:46 +0200 |
| commit | 757096e7df15c14b9b10352fa91663483f9e34f8 (patch) | |
| tree | dcad789d0abd8be76463277dc01bf32c4e6a527b /src/engine/texture.cpp | |
| parent | b2c89d7060e99a36c8c7ac897b7386686c74deac (diff) | |
| download | xolatile-badassbug-757096e7df15c14b9b10352fa91663483f9e34f8.tar.xz xolatile-badassbug-757096e7df15c14b9b10352fa91663483f9e34f8.tar.zst | |
all
Diffstat (limited to 'src/engine/texture.cpp')
| -rw-r--r-- | src/engine/texture.cpp | 90 |
1 files changed, 26 insertions, 64 deletions
diff --git a/src/engine/texture.cpp b/src/engine/texture.cpp index 0f7a5df..744996e 100644 --- a/src/engine/texture.cpp +++ b/src/engine/texture.cpp @@ -223,7 +223,7 @@ static void reorients3tc(GLenum format, int blocksize, int w, int h, uchar *src, } } -static void reorientrgtc(GLenum format, int blocksize, int w, int h, uchar *src, uchar *dst, bool flipx, bool flipy, bool swapxy) { +static void reorientrgtc(int blocksize, int w, int h, uchar *src, uchar *dst, bool flipx, bool flipy, bool swapxy) { int bx1 = 0, by1 = 0, bx2 = min(w, 4), by2 = min(h, 4), bw = (w+3)/4, bh = (h+3)/4, stridex = blocksize, stridey = blocksize; if(swapxy) stridex *= bw; else stridey *= bh; if(flipx) { dst += (bw-1)*stridex; stridex = -stridex; bx1 += 4-bx2; bx2 = 4; } @@ -333,19 +333,6 @@ void forcergbaimage(ImageData &s) { s.replace(d); } -void swizzleimage(ImageData &s) { - if(s.bpp==2) { - ImageData d(s.w, s.h, 4); - readwritetex(d, s, { dst[0] = dst[1] = dst[2] = src[0]; dst[3] = src[1]; }); - s.replace(d); - } - else if(s.bpp==1) { - ImageData d(s.w, s.h, 3); - readwritetex(d, s, { dst[0] = dst[1] = dst[2] = src[0]; }); - s.replace(d); - } -} - void texreorient(ImageData &s, bool flipx, bool flipy, bool swapxy, int type = TEX_DIFFUSE) { ImageData d(swapxy ? s.h : s.w, swapxy ? s.w : s.h, s.bpp, s.levels, s.align, s.compressed); switch(s.compressed) { @@ -373,7 +360,7 @@ void texreorient(ImageData &s, bool flipx, bool flipy, bool swapxy, int type = T case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT: { uchar *dst = d.data, *src = s.data; loopi(s.levels) { - reorientrgtc(s.compressed, s.bpp, max(s.w>>i, 1), max(s.h>>i, 1), src, dst, flipx, flipy, swapxy); + reorientrgtc(s.bpp, max(s.w>>i, 1), max(s.h>>i, 1), src, dst, flipx, flipy, swapxy); src += s.calclevelsize(i); dst += d.calclevelsize(i); } @@ -423,8 +410,6 @@ void texoffset(ImageData &s, int xoffset, int yoffset) { } void texmad(ImageData &s, const vec &mul, const vec &add) { - if(s.bpp < 3 && (mul.x != mul.y || mul.y != mul.z || add.x != add.y || add.y != add.z)) - swizzleimage(s); int maxk = min(int(s.bpp), 3); writetex(s, loopk(maxk) dst[k] = uchar(clamp(dst[k]*mul[k] + 255*add[k], 0.0f, 255.0f)); @@ -646,7 +631,7 @@ void resizetexture(int w, int h, bool mipmap, bool canreduce, GLenum target, int } } -void uploadtexture(int tnum, GLenum target, GLenum internal, int tw, int th, GLenum format, GLenum type, void *pixels, int pw, int ph, int pitch, bool mipmap) { +void uploadtexture(GLenum target, GLenum internal, int tw, int th, GLenum format, GLenum type, void *pixels, int pw, int ph, int pitch, bool mipmap) { int bpp = formatsize(format), row = 0, rowalign = 0; if(!pitch) pitch = pw*bpp; uchar *buf = NULL; @@ -656,7 +641,7 @@ void uploadtexture(int tnum, GLenum target, GLenum internal, int tw, int th, GLe } else if(tw*bpp != pitch) { row = pitch/bpp; - rowalign = texalign(pixels, pitch, 1); + rowalign = texalign(pitch, 1); while(rowalign > 0 && ((row*bpp + rowalign - 1)/rowalign)*rowalign != pitch) rowalign >>= 1; if(!rowalign) { row = 0; @@ -667,7 +652,7 @@ void uploadtexture(int tnum, GLenum target, GLenum internal, int tw, int th, GLe for(int level = 0, align = 0, mw = tw, mh = th;; level++) { uchar *src = buf ? buf : (uchar *)pixels; if(buf) pitch = mw*bpp; - int srcalign = row > 0 ? rowalign : texalign(src, pitch, 1); + int srcalign = row > 0 ? rowalign : texalign(pitch, 1); if(align != srcalign) glPixelStorei(GL_UNPACK_ALIGNMENT, align = srcalign); if(row > 0) glPixelStorei(GL_UNPACK_ROW_LENGTH, row); glTexImage2D(target, level, internal, mw, mh, 0, format, type, src); @@ -715,17 +700,7 @@ GLenum textarget(GLenum subtarget) { return subtarget; } -const GLint *swizzlemask(GLenum format) { - static const GLint luminance[4] = { GL_RED, GL_RED, GL_RED, GL_ONE }; - static const GLint luminancealpha[4] = { GL_RED, GL_RED, GL_RED, GL_GREEN }; - switch(format) { - case GL_RED: return luminance; - case GL_RG: return luminancealpha; - } - return NULL; -} - -void setuptexparameters(int tnum, void *pixels, int clamp, int filter, GLenum format, GLenum target, bool swizzle) { +void setuptexparameters(int tnum, int clamp, int filter, GLenum format, GLenum target) { glBindTexture(target, tnum); glTexParameteri(target, GL_TEXTURE_WRAP_S, clamp&1 ? GL_CLAMP_TO_EDGE : (clamp&0x100 ? GL_MIRRORED_REPEAT : GL_REPEAT)); glTexParameteri(target, GL_TEXTURE_WRAP_T, clamp&2 ? GL_CLAMP_TO_EDGE : (clamp&0x200 ? GL_MIRRORED_REPEAT : GL_REPEAT)); @@ -737,10 +712,6 @@ void setuptexparameters(int tnum, void *pixels, int clamp, int filter, GLenum fo (bilinear ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_LINEAR) : (bilinear ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_NEAREST)) : (filter && bilinear ? GL_LINEAR : GL_NEAREST)); - if(swizzle && hasTRG && hasTSW) { - const GLint *mask = swizzlemask(format); - if(mask) glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, mask); - } } static GLenum textype(GLenum &component, GLenum &format) { @@ -828,9 +799,9 @@ static GLenum textype(GLenum &component, GLenum &format) { return type; } -void createtexture(int tnum, int w, int h, void *pixels, int clamp, int filter, GLenum component, GLenum subtarget, int pw, int ph, int pitch, bool resize, GLenum format, bool swizzle) { +void createtexture(int tnum, int w, int h, void *pixels, int clamp, int filter, GLenum component, GLenum subtarget, int pw, int ph, int pitch, bool resize, GLenum format) { GLenum target = textarget(subtarget), type = textype(component, format); - if(filter >= 0 && clamp >= 0) setuptexparameters(tnum, pixels, clamp, filter, format, target, swizzle); + if(filter >= 0 && clamp >= 0) setuptexparameters(tnum, clamp, filter, format, target); if(!pw) pw = w; if(!ph) ph = h; int tw = w, th = h; @@ -839,12 +810,12 @@ void createtexture(int tnum, int w, int h, void *pixels, int clamp, int filter, resizetexture(w, h, mipmap, false, target, 0, tw, th); if(mipmap) component = compressedformat(component, tw, th); } - uploadtexture(tnum, subtarget, component, tw, th, format, type, pixels, pw, ph, pitch, mipmap); + uploadtexture(subtarget, component, tw, th, format, type, pixels, pw, ph, pitch, mipmap); } -void createcompressedtexture(int tnum, int w, int h, uchar *data, int align, int blocksize, int levels, int clamp, int filter, GLenum format, GLenum subtarget, bool swizzle = false) { +void createcompressedtexture(int tnum, int w, int h, uchar *data, int align, int blocksize, int levels, int clamp, int filter, GLenum format, GLenum subtarget) { GLenum target = textarget(subtarget); - if(filter >= 0 && clamp >= 0) setuptexparameters(tnum, data, clamp, filter, format, target); + if(filter >= 0 && clamp >= 0) setuptexparameters(tnum, clamp, filter, format, target); uploadcompressedtexture(target, subtarget, format, w, h, data, align, blocksize, levels, filter > 1); } @@ -852,10 +823,10 @@ hashnameset<Texture> textures; Texture *notexture = NULL; // used as default, ensured to be loaded -static GLenum texformat(int bpp, bool swizzle = false) { +static GLenum texformat(int bpp) { switch(bpp) { - case 1: return hasTRG && (hasTSW || !glcompat || !swizzle) ? GL_RED : GL_LUMINANCE; - case 2: return hasTRG && (hasTSW || !glcompat || !swizzle) ? GL_RG : GL_LUMINANCE_ALPHA; + case 1: return hasTRG && (hasTSW || !glcompat) ? GL_RED : GL_LUMINANCE; + case 2: return hasTRG && (hasTSW || !glcompat) ? GL_RG : GL_LUMINANCE_ALPHA; case 3: return GL_RGB; case 4: return GL_RGBA; default: return 0; @@ -874,7 +845,7 @@ static bool alphaformat(GLenum format) { } } -int texalign(const void *data, int w, int bpp) { +int texalign(int w, int bpp) { int stride = w*bpp; if(stride&1) return 1; if(stride&2) return 2; @@ -897,7 +868,6 @@ static Texture *newtexture(Texture *t, const char *rname, ImageData &s, int clam t->w = t->h = t->xs = t->ys = t->bpp = 0; return t; } - bool swizzle = !(clamp&0x10000); GLenum format; if(s.compressed) { format = uncompressedformat(s.compressed); @@ -905,13 +875,8 @@ static Texture *newtexture(Texture *t, const char *rname, ImageData &s, int clam t->type |= Texture::COMPRESSED; } else { - format = texformat(s.bpp, swizzle); + format = texformat(s.bpp); t->bpp = s.bpp; - if(swizzle && hasTRG && !hasTSW && swizzlemask(format)) { - swizzleimage(s); - format = texformat(s.bpp, swizzle); - t->bpp = s.bpp; - } } if(alphaformat(format)) t->type |= Texture::ALPHA; t->w = t->xs = s.w; @@ -934,12 +899,12 @@ static Texture *newtexture(Texture *t, const char *rname, ImageData &s, int clam if(t->w > 1) t->w /= 2; if(t->h > 1) t->h /= 2; } - createcompressedtexture(t->id, t->w, t->h, data, s.align, s.bpp, levels, clamp, filter, s.compressed, GL_TEXTURE_2D, swizzle); + createcompressedtexture(t->id, t->w, t->h, data, s.align, s.bpp, levels, clamp, filter, s.compressed, GL_TEXTURE_2D); } else { resizetexture(t->w, t->h, mipit, canreduce, GL_TEXTURE_2D, compress, t->w, t->h); GLenum component = compressedformat(format, t->w, t->h, compress); - createtexture(t->id, t->w, t->h, s.data, clamp, filter, component, GL_TEXTURE_2D, t->xs, t->ys, s.pitch, false, format, swizzle); + createtexture(t->id, t->w, t->h, s.data, clamp, filter, component, GL_TEXTURE_2D, t->xs, t->ys, s.pitch, false, format); } return t; } @@ -1275,9 +1240,6 @@ static bool texturedata(ImageData &d, const char *tname, Slot::Tex *tex = NULL, if(matchstring(cmd, len, "mirror")) { if(wrap) *wrap |= 0x300; } - else if(matchstring(cmd, len, "noswizzle")) { - if(wrap) *wrap |= 0x10000; - } } return true; } @@ -1860,7 +1822,7 @@ static void mergealpha(ImageData &c, ImageData &s) { else { readwritergbatex(c, s, dst[3] = src[3]; ); } } -static void addname(vector<char> &key, Slot &slot, Slot::Tex &t, bool combined = false, const char *prefix = NULL) { +static void addname(vector<char> &key, Slot::Tex &t, bool combined = false, const char *prefix = NULL) { if(combined) key.add('&'); if(prefix) { while(*prefix) key.add(*prefix++); } defformatstring(tname, "packages/%s", t.name); @@ -1869,7 +1831,7 @@ static void addname(vector<char> &key, Slot &slot, Slot::Tex &t, bool combined = static void texcombine(Slot &s, int index, Slot::Tex &t, bool forceload = false) { vector<char> key; - addname(key, s, t); + addname(key, t); int texmask = 0; if(!forceload) switch(t.type) { case TEX_DIFFUSE: @@ -1878,7 +1840,7 @@ static void texcombine(Slot &s, int index, Slot::Tex &t, bool forceload = false) if(i<0) break; texmask |= 1<<s.sts[i].type; s.sts[i].combined = index; - addname(key, s, s.sts[i], true); + addname(key, s.sts[i], true); break; } } @@ -1955,17 +1917,17 @@ Texture *loadthumbnail(Slot &slot) { linkslotshader(slot, false); linkvslotshader(vslot, false); vector<char> name; - if(vslot.colorscale == vec(1, 1, 1)) addname(name, slot, slot.sts[0], false, "<thumbnail>"); + if(vslot.colorscale == vec(1, 1, 1)) addname(name, slot.sts[0], false, "<thumbnail>"); else { defformatstring(prefix, "<thumbnail:%.2f/%.2f/%.2f>", vslot.colorscale.x, vslot.colorscale.y, vslot.colorscale.z); - addname(name, slot, slot.sts[0], false, prefix); + addname(name, slot.sts[0], false, prefix); } VSlot *layer = vslot.layer ? &lookupvslot(vslot.layer, false) : NULL; if(layer) { - if(layer->colorscale == vec(1, 1, 1)) addname(name, *layer->slot, layer->slot->sts[0], true, "<layer>"); + if(layer->colorscale == vec(1, 1, 1)) addname(name, layer->slot->sts[0], true, "<layer>"); else { defformatstring(prefix, "<layer:%.2f/%.2f/%.2f>", vslot.colorscale.x, vslot.colorscale.y, vslot.colorscale.z); - addname(name, *layer->slot, layer->slot->sts[0], true, prefix); + addname(name, layer->slot->sts[0], true, prefix); } } name.add('\0'); @@ -2248,7 +2210,7 @@ void screenshot(char *filename) { concatstring(buf, imageexts[format]); } ImageData image(screenw, screenh, 3); - glPixelStorei(GL_PACK_ALIGNMENT, texalign(image.data, screenw, 3)); + glPixelStorei(GL_PACK_ALIGNMENT, texalign(screenw, 3)); glReadPixels(0, 0, screenw, screenh, GL_RGB, GL_UNSIGNED_BYTE, image.data); saveimage(path(buf), format, image, true); } |
