From: Eleni Maria Stea Date: Sun, 21 Oct 2018 11:43:46 +0000 (+0300) Subject: Added mipmaps. Buggy! X-Git-Url: https://eleni.mutantstargoat.com/git/?p=test_compression;a=commitdiff_plain;h=03daf3cf2183d0c1aabe1b10645dba9c8597d53e Added mipmaps. Buggy! This code has a bug because when I load the mipmap on nvidia, it looks correct, but I see the message "loaded and compressed data differ". Pushing for backup --- diff --git a/compressed/full.tex b/compressed/full.tex new file mode 100644 index 0000000..1aa7581 Binary files /dev/null and b/compressed/full.tex differ diff --git a/compressed/main.c b/compressed/main.c new file mode 100644 index 0000000..1993b32 --- /dev/null +++ b/compressed/main.c @@ -0,0 +1,432 @@ +#include +#include +#include +#include +#include +#include +#include + +struct texture { + unsigned int id; + int width, height; + unsigned int fmt; + unsigned int compsize; + void *data; +}; + +int init(void); +void disp(void); +void reshape(int x, int y); +void keyb(unsigned char key, int x, int y); +void idle(void); +int load_texture(const char *fname, struct texture *tex); +const char *fmtstr(int fmt); +void print_compressed_formats(void); + +struct texture tex; +unsigned int tex2; +const char *texfile; +int subtest, copytest; + +int main(int argc, char **argv) +{ + int i, loop = 0; + unsigned int glut_flags = GLUT_RGB | GLUT_DOUBLE; + + for(i=1; i 20 || !hdr.datadesc[0].size) { + fprintf(stderr, "%s is not a compressed texture file, or is corrupted\n", fname); + fclose(fp); + return -1; + } + + if(!(buf = malloc(hdr.datadesc[0].size))) { + fprintf(stderr, "failed to allocate compressed texture buffer (%d bytes): %s\n", + hdr.datadesc[0].size, strerror(errno)); + fclose(fp); + return -1; + } + if(!(tex->data = malloc(hdr.datadesc[0].size))) { + fprintf(stderr, "failed to allocate data buffer\n"); + fclose(fp); + free(buf); + return -1; + } + + + glGenTextures(1, &tex->id); + glBindTexture(GL_TEXTURE_2D, tex->id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + hdr.levels > 1 ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, 1); + + tex->fmt = hdr.glfmt; + tex->width = hdr.width; + tex->height = hdr.height; + tex->compsize = hdr.datadesc[0].size; + + printf("%s: %dx%d format: %s\n", fname, tex->width, tex->height, fmtstr(tex->fmt)); + glutReshapeWindow(tex->width + tex->width / 2, tex->height); + + for(i=0; iid); + return -1; + } + if(i == 0) { + memcpy(tex->data, buf, hdr.datadesc[0].size); + } + glCompressedTexImage2D(GL_TEXTURE_2D, i, hdr.glfmt, hdr.width, hdr.height, 0, + hdr.datadesc[i].size, buf); + + hdr.width /= 2; + hdr.height /= 2; + } + + free(buf); + fclose(fp); + return 0; +} + +const char *fmtstr(int fmt) +{ + switch(fmt) { + case 0x86b0: return "GL_COMPRESSED_RGB_FXT1_3DFX"; + case 0x86b1: return "GL_COMPRESSED_RGBA_FXT1_3DFX"; + case 0x8dbb: return "GL_COMPRESSED_RED_RGTC1"; + case 0x8dbc: return "GL_COMPRESSED_SIGNED_RED_RGTC1"; + case 0x8dbd: return "GL_COMPRESSED_RG_RGTC2"; + case 0x8dbe: return "GL_COMPRESSED_SIGNED_RG_RGTC2"; + case 0x8e8c: return "GL_COMPRESSED_RGBA_BPTC_UNORM"; + case 0x8e8d: return "GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM"; + case 0x8e8e: return "GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT"; + case 0x8e8f: return "GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT"; + case 0x9274: return "GL_COMPRESSED_RGB8_ETC2"; + case 0x9275: return "GL_COMPRESSED_SRGB8_ETC2"; + case 0x9276: return "GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2"; + case 0x9277: return "GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2"; + case 0x9278: return "GL_COMPRESSED_RGBA8_ETC2_EAC"; + case 0x9279: return "GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC"; + case 0x9270: return "GL_COMPRESSED_R11_EAC"; + case 0x9271: return "GL_COMPRESSED_SIGNED_R11_EAC"; + case 0x9272: return "GL_COMPRESSED_RG11_EAC"; + case 0x9273: return "GL_COMPRESSED_SIGNED_RG11_EAC"; + case 0x83F0: return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT"; + case 0x83F1: return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT"; + case 0x83F2: return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT"; + case 0x83F3: return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT"; + case 0x8C48: return "GL_COMPRESSED_SRGB_EXT"; + case 0x8C49: return "GL_COMPRESSED_SRGB_ALPHA_EXT"; + case 0x8C4A: return "GL_COMPRESSED_SLUMINANCE_EXT"; + case 0x8C4B: return "GL_COMPRESSED_SLUMINANCE_ALPHA_EXT"; + case 0x8C4C: return "GL_COMPRESSED_SRGB_S3TC_DXT1_EXT"; + case 0x8C4D: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT"; + case 0x8C4E: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT"; + case 0x8C4F: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT"; + case 0x8B90: return "GL_PALETTE4_RGB8_OES"; + case 0x8B91: return "GL_PALETTE4_RGBA8_OES"; + case 0x8B92: return "GL_PALETTE4_R5_G6_B5_OES"; + case 0x8B93: return "GL_PALETTE4_RGBA4_OES"; + case 0x8B94: return "GL_PALETTE4_RGB5_A1_OES"; + case 0x8B95: return "GL_PALETTE8_RGB8_OES"; + case 0x8B96: return "GL_PALETTE8_RGBA8_OES"; + case 0x8B97: return "GL_PALETTE8_R5_G6_B5_OES"; + case 0x8B98: return "GL_PALETTE8_RGBA4_OES"; + case 0x8B99: return "GL_PALETTE8_RGB5_A1_OES"; + case 0x93B0: return "GL_COMPRESSED_RGBA_ASTC_4"; + case 0x93B1: return "GL_COMPRESSED_RGBA_ASTC_5"; + case 0x93B2: return "GL_COMPRESSED_RGBA_ASTC_5"; + case 0x93B3: return "GL_COMPRESSED_RGBA_ASTC_6"; + case 0x93B4: return "GL_COMPRESSED_RGBA_ASTC_6"; + case 0x93B5: return "GL_COMPRESSED_RGBA_ASTC_8"; + case 0x93B6: return "GL_COMPRESSED_RGBA_ASTC_8"; + case 0x93B7: return "GL_COMPRESSED_RGBA_ASTC_8"; + case 0x93B8: return "GL_COMPRESSED_RGBA_ASTC_10"; + case 0x93B9: return "GL_COMPRESSED_RGBA_ASTC_10"; + case 0x93BA: return "GL_COMPRESSED_RGBA_ASTC_10"; + case 0x93BB: return "GL_COMPRESSED_RGBA_ASTC_10"; + case 0x93BC: return "GL_COMPRESSED_RGBA_ASTC_12"; + case 0x93BD: return "GL_COMPRESSED_RGBA_ASTC_12"; + case 0x93D0: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4"; + case 0x93D1: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5"; + case 0x93D2: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5"; + case 0x93D3: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6"; + case 0x93D4: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6"; + case 0x93D5: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8"; + case 0x93D6: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8"; + case 0x93D7: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8"; + case 0x93D8: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10"; + case 0x93D9: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10"; + case 0x93DA: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10"; + case 0x93DB: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10"; + case 0x93DC: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12"; + case 0x93DD: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12"; + case GL_LUMINANCE: + case 1: + return "GL_LUMINANCE"; + case GL_RGB: + case 3: + return "GL_RGB"; + case GL_RGBA: + case 4: + return "GL_RGBA"; + case GL_BGR: return "GL_BGR"; + case GL_BGRA: return "GL_BGRA"; + case GL_SLUMINANCE: return "GL_SLUMINANCE"; + case GL_SLUMINANCE8: return "GL_SLUMINANCE8"; + case GL_SLUMINANCE_ALPHA: return "GL_SLUMINANCE_ALPHA"; + case GL_SLUMINANCE8_ALPHA8: return "GL_SLUMINANCE8_ALPHA8"; + case GL_SRGB: return "GL_SRGB"; + case GL_SRGB8: return "GL_SRGB8"; + case GL_SRGB_ALPHA: return "GL_SRGB_ALPHA"; + case GL_SRGB8_ALPHA8: return "GL_SRGB8_ALPHA8"; + default: + break; + } + return "unknown"; +} + +void print_compressed_formats(void) +{ + int i, num_fmt; + int *fmtlist; + + glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num_fmt); + printf("%d generic compressed texture formats available:\n", num_fmt); + + if(!(fmtlist = malloc(num_fmt * sizeof *fmtlist))) { + fprintf(stderr, "failed to allocate texture formats enumeration buffer\n"); + return; + } + glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, fmtlist); + + for(i=0; i #include -#undef USE_SRGB - -#ifdef USE_SRGB -#define COMP_FMT GL_COMPRESSED_SRGB8_ETC2 -#else -#define COMP_FMT GL_COMPRESSED_RGB8_ETC2 -#endif +struct texture { + unsigned int id; + int width, height; + unsigned int fmt; + unsigned int compsize; + void *data; +}; int init(void); -int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels, - int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype); void disp(void); void reshape(int x, int y); void keyb(unsigned char key, int x, int y); -void idle(); -void gen_image(unsigned char *pixels, int xsz, int ysz); -unsigned char *load_compressed_image(const char *fname, int *cszptr, int *xszptr, int *yszptr); -int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h); +void idle(void); +int load_texture(const char *fname, struct texture *tex); +const char *fmtstr(int fmt); void print_compressed_formats(void); -unsigned int tex, tex2, comp_tex; +struct texture tex; +unsigned int tex2; const char *texfile; -int subtest, copytest, loop; +int subtest, copytest; int main(int argc, char **argv) { - int i; + int i, loop = 0; unsigned int glut_flags = GLUT_RGB | GLUT_DOUBLE; -#ifdef USE_SRGB - glut_flags |= GLUT_SRGB; -#endif for(i=1; i 20 || !hdr.datadesc[0].size) { + fprintf(stderr, "%s is not a compressed texture file, or is corrupted\n", fname); + fclose(fp); + return -1; } - if(fread(pixels, 1, comp_size, fp) < comp_size) { - fprintf(stderr, "failed to read compressed texture file: %s: %s\n", texfile, strerror(errno)); + + if(!(buf = malloc(hdr.datadesc[0].size))) { + fprintf(stderr, "failed to allocate compressed texture buffer (%d bytes): %s\n", + hdr.datadesc[0].size, strerror(errno)); fclose(fp); - free(pixels); - return 0; + return -1; + } + if(!(tex->data = malloc(hdr.datadesc[0].size))) { + fprintf(stderr, "failed to allocate data buffer\n"); + fclose(fp); + free(buf); + return -1; } - fclose(fp); - *cszptr = comp_size; - *xszptr = xsz; - *yszptr = ysz; - return pixels; -} -int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h) -{ - FILE *fp; + glGenTextures(1, &tex->id); + glBindTexture(GL_TEXTURE_2D, tex->id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + hdr.levels > 1 ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, 1); - if(!(fp = fopen(fname, "wb"))) { - fprintf(stderr, "failed to open compressed texture dump file: %s: %s\n", fname, strerror(errno)); - return -1; - } + tex->fmt = hdr.glfmt; + tex->width = hdr.width; + tex->height = hdr.height; + tex->compsize = hdr.datadesc[0].size; + + printf("%s: %dx%d format: %s\n", fname, tex->width, tex->height, fmtstr(tex->fmt)); + glutReshapeWindow(tex->width + tex->width / 2, tex->height); - if(fwrite(&w, sizeof w, 1, fp) < 1 || - fwrite(&h, sizeof h, 1, fp) < 1 || - fwrite(data, 1, size, fp) < size) { - fprintf(stderr, "failed to dump compressed texture: %s\n", strerror(errno)); + for(i=0; iid); + return -1; + } + if(i == 0) { + memcpy(tex->data, buf, hdr.datadesc[0].size); + } + glCompressedTexImage2D(GL_TEXTURE_2D, i, hdr.glfmt, hdr.width, hdr.height, 0, + hdr.datadesc[i].size, buf); + + hdr.width /= 2; + hdr.height /= 2; } + + free(buf); fclose(fp); return 0; } @@ -390,6 +398,25 @@ const char *fmtstr(int fmt) case 0x93DB: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10"; case 0x93DC: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12"; case 0x93DD: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12"; + case GL_LUMINANCE: + case 1: + return "GL_LUMINANCE"; + case GL_RGB: + case 3: + return "GL_RGB"; + case GL_RGBA: + case 4: + return "GL_RGBA"; + case GL_BGR: return "GL_BGR"; + case GL_BGRA: return "GL_BGRA"; + case GL_SLUMINANCE: return "GL_SLUMINANCE"; + case GL_SLUMINANCE8: return "GL_SLUMINANCE8"; + case GL_SLUMINANCE_ALPHA: return "GL_SLUMINANCE_ALPHA"; + case GL_SLUMINANCE8_ALPHA8: return "GL_SLUMINANCE8_ALPHA8"; + case GL_SRGB: return "GL_SRGB"; + case GL_SRGB8: return "GL_SRGB8"; + case GL_SRGB_ALPHA: return "GL_SRGB_ALPHA"; + case GL_SRGB8_ALPHA8: return "GL_SRGB8_ALPHA8"; default: break; }