From: Eleni Maria Stea Date: Tue, 13 Mar 2018 07:59:55 +0000 (+0200) Subject: This is a test program that loads compressed data, renders the image X-Git-Url: http://git.mutantstargoat.com?p=test_compression;a=commitdiff_plain;h=4000d858aed75d56961a40be2dfd313b1a32a41b This is a test program that loads compressed data, renders the image and gets the pixels with glGetCompressedTexImage* functions. Just for testing. --- 4000d858aed75d56961a40be2dfd313b1a32a41b diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..84363ff --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +obj = main.o +bin = test + +CFLAGS = -pedantic -Wall -g +LDFLAGS = -lGL -lglut + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff --git a/README b/README new file mode 100644 index 0000000..6a8352c --- /dev/null +++ b/README @@ -0,0 +1,8 @@ +Build and Run: +-------------- +Build: +make + +Run: +./test if your gpu can compress textures +./test compressed_texture to load already compressed data (recommended) diff --git a/compressed_texture b/compressed_texture new file mode 100644 index 0000000..af66f3a Binary files /dev/null and b/compressed_texture differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..32da284 --- /dev/null +++ b/main.c @@ -0,0 +1,359 @@ +#include +#include +#include +#include +#include +#include + +#undef USE_SRGB + +#ifdef USE_SRGB +#define COMP_FMT GL_COMPRESSED_SRGB8_ETC2 +#else +#define COMP_FMT GL_COMPRESSED_RGB8_ETC2 +#endif + +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 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 print_compressed_formats(void); + +unsigned int tex, comp_tex; +const char *texfile; + +int main(int argc, char **argv) +{ + unsigned int glut_flags = GLUT_RGB | GLUT_DOUBLE; +#ifdef USE_SRGB + glut_flags |= GLUT_SRGB; +#endif + + texfile = argv[1]; + + glutInit(&argc, argv); + glutInitWindowSize(800, 600); + glutInitDisplayMode(glut_flags); + glutCreateWindow("test"); + + glutDisplayFunc(disp); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + + if(init() == -1) { + return 1; + } + + glutMainLoop(); + return 0; +} + +int init(void) +{ + unsigned char *pixels, *buf; + int xsz = 512; + int ysz = 512; + int is_comp = 0; + int comp_size = 0; + int tmp; + + print_compressed_formats(); + + if(texfile) { + if(!(pixels = load_compressed_image(texfile, &comp_size, &xsz, &ysz))) { + return -1; + } + printf("loaded compressed texture file: %s (%dx%d)\n", texfile, xsz, ysz); + + } else { + if(!(pixels = malloc(xsz * ysz * 3))) { + abort(); + } + gen_image(pixels, xsz, ysz); + + printf("compressing texture\n"); + if((comp_size = texcomp(pixels, COMP_FMT, pixels, xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE)) == -1) { + return -1; + } + printf("compressed texture is %d bytes (uncompressed was: %d)\n", comp_size, xsz * ysz * 3); + + dump_compressed_image("compressed_texture", pixels, comp_size, xsz, ysz); + } + + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glCompressedTexImage2D(GL_TEXTURE_2D, 0, COMP_FMT, xsz, ysz, 0, comp_size, pixels); + if(glGetError()) { + fprintf(stderr, "failed to upload compressed texture\n"); + return -1; + } + + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp); + if(!is_comp) { + fprintf(stderr, "texture is not compressed\n"); + return -1; + } + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &tmp); + if(tmp != comp_size) { + fprintf(stderr, "internal compressed size differs (expected: %d, got: %d)!\n", comp_size, tmp); + return -1; + } + + if(!(buf = malloc(comp_size))) { + fprintf(stderr, "failed to allocate comparison image buffer (%d bytes)\n", comp_size); + return -1; + } + glGetCompressedTexImage(GL_TEXTURE_2D, 0, buf); + + if(memcmp(pixels, buf, comp_size) != 0) { + fprintf(stderr, "submitted and retrieved pixel data differ!\n"); + } else { + printf("submitted and retrieved sizes match (%d bytes)\n", comp_size); + } + free(buf); + free(pixels); + +#ifdef USE_SRGB + glEnable(GL_FRAMEBUFFER_SRGB); +#endif + + glEnable(GL_TEXTURE_2D); + return 0; +} + +int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels, + int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype) +{ + unsigned int tex; + int is_comp = 0; + int comp_size = 0; + + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + glTexImage2D(GL_TEXTURE_2D, 0, tofmt, xsz, ysz, 0, fromfmt, fromtype, pixels); + if(glGetError()) { + fprintf(stderr, "failed to compress texture\n"); + return -1; + } + + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp); + if(!is_comp) { + fprintf(stderr, "texture is not compressed\n"); + return -1; + } + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &comp_size); + + glGetCompressedTexImage(GL_TEXTURE_2D, 0, compix); + return comp_size; +} + +void disp(void) +{ + glClear(GL_COLOR_BUFFER_BIT); + + glBegin(GL_QUADS); + glTexCoord2f(0, 1); glVertex2f(-1, -1); + glTexCoord2f(1, 1); glVertex2f(1, -1); + glTexCoord2f(1, 0); glVertex2f(1, 1); + glTexCoord2f(0, 0); glVertex2f(-1, 1); + glEnd(); + + glutSwapBuffers(); + assert(glGetError() == GL_NO_ERROR); +} + +void reshape(int x, int y) +{ + float aspect = (float)x / (float)y; + glViewport(0, 0, x, y); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glScalef(1.0 / aspect, 1, 1); +} + +void keyb(unsigned char key, int x, int y) +{ + if(key == 27) { + exit(0); + } +} + +void gen_image(unsigned char *pixels, int xsz, int ysz) +{ + int i, j; + + for(i=0; i