7 #include <GL/freeglut.h>
12 #define COMP_FMT GL_COMPRESSED_SRGB8_ETC2
14 #define COMP_FMT GL_COMPRESSED_RGB8_ETC2
18 int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels,
19 int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype);
21 void reshape(int x, int y);
22 void keyb(unsigned char key, int x, int y);
23 void gen_image(unsigned char *pixels, int xsz, int ysz);
24 unsigned char *load_compressed_image(const char *fname, int *cszptr, int *xszptr, int *yszptr);
25 int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h);
26 void print_compressed_formats(void);
28 unsigned int tex, tex2, comp_tex;
30 int subtest, copytest;
32 int main(int argc, char **argv)
35 unsigned int glut_flags = GLUT_RGB | GLUT_DOUBLE;
37 glut_flags |= GLUT_SRGB;
40 for(i=1; i<argc; i++) {
41 if(argv[i][0] == '-') {
42 if(strcmp(argv[i], "-subtest") == 0) {
44 } else if(strcmp(argv[i], "-copytest") == 0) {
47 fprintf(stderr, "invalid option: %s\n", argv[i]);
52 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
59 glutInit(&argc, argv);
60 glutInitWindowSize(800, 600);
61 glutInitDisplayMode(glut_flags);
62 glutCreateWindow("test");
64 glutDisplayFunc(disp);
65 glutReshapeFunc(reshape);
66 glutKeyboardFunc(keyb);
80 unsigned char *pixels, *buf;
87 print_compressed_formats();
90 if(!(pixels = load_compressed_image(texfile, &comp_size, &xsz, &ysz))) {
93 printf("loaded compressed texture file: %s (%dx%d)\n", texfile, xsz, ysz);
96 if(!(pixels = malloc(xsz * ysz * 3))) {
99 gen_image(pixels, xsz, ysz);
101 printf("compressing texture\n");
102 if((comp_size = texcomp(pixels, COMP_FMT, pixels, xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE)) == -1) {
105 printf("compressed texture is %d bytes (uncompressed was: %d)\n", comp_size, xsz * ysz * 3);
107 dump_compressed_image("compressed_texture", pixels, comp_size, xsz, ysz);
110 glGenTextures(1, &tex);
111 glBindTexture(GL_TEXTURE_2D, tex);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
114 glCompressedTexImage2D(GL_TEXTURE_2D, 0, COMP_FMT, xsz, ysz, 0, comp_size, pixels);
116 fprintf(stderr, "failed to upload compressed texture\n");
120 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
122 fprintf(stderr, "texture is not compressed\n");
125 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &tmp);
126 if(tmp != comp_size) {
127 fprintf(stderr, "internal compressed size differs (expected: %d, got: %d)!\n", comp_size, tmp);
131 if(!(buf = malloc(comp_size))) {
132 fprintf(stderr, "failed to allocate comparison image buffer (%d bytes)\n", comp_size);
135 glGetCompressedTexImage(GL_TEXTURE_2D, 0, buf);
137 if(memcmp(pixels, buf, comp_size) != 0) {
138 fprintf(stderr, "submitted and retrieved pixel data differ!\n");
140 printf("submitted and retrieved sizes match (%d bytes)\n", comp_size);
144 printf("testing glGetCompressedTextureSubImage and glCompressedTexSubImage2D\n");
145 memset(buf, 0, comp_size);
146 glGetCompressedTextureSubImage(tex, 0, 192, 64, 0, 64, 64, 1, comp_size, buf);
147 glCompressedTexSubImage2D(GL_TEXTURE_2D, 0, 32, 32, 64, 64, COMP_FMT, 2048, buf);
151 printf("testing glCopyImageSubData\n");
153 glGenTextures(1, &tex2);
154 glBindTexture(GL_TEXTURE_2D, tex2);
155 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
156 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
157 glCompressedTexImage2D(GL_TEXTURE_2D, 0, COMP_FMT, xsz, ysz, 0, comp_size, pixels);
158 glBindTexture(GL_TEXTURE_2D, 0);
160 glCopyImageSubData(tex2, GL_TEXTURE_2D, 0, 128, 64, 0,
161 tex, GL_TEXTURE_2D, 0, 32, 32, 0, 64, 64, 1);
163 glBindTexture(GL_TEXTURE_2D, tex);
170 glEnable(GL_FRAMEBUFFER_SRGB);
173 glEnable(GL_TEXTURE_2D);
177 int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels,
178 int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype)
184 glGenTextures(1, &tex);
185 glBindTexture(GL_TEXTURE_2D, tex);
186 glTexImage2D(GL_TEXTURE_2D, 0, tofmt, xsz, ysz, 0, fromfmt, fromtype, pixels);
188 fprintf(stderr, "failed to compress texture\n");
192 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
194 fprintf(stderr, "texture is not compressed\n");
197 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &comp_size);
199 glGetCompressedTexImage(GL_TEXTURE_2D, 0, compix);
205 glClear(GL_COLOR_BUFFER_BIT);
208 glTexCoord2f(0, 1); glVertex2f(-1, -1);
209 glTexCoord2f(1, 1); glVertex2f(1, -1);
210 glTexCoord2f(1, 0); glVertex2f(1, 1);
211 glTexCoord2f(0, 0); glVertex2f(-1, 1);
215 assert(glGetError() == GL_NO_ERROR);
218 void reshape(int x, int y)
220 float aspect = (float)x / (float)y;
221 glViewport(0, 0, x, y);
223 glMatrixMode(GL_PROJECTION);
225 glScalef(1.0 / aspect, 1, 1);
228 void keyb(unsigned char key, int x, int y)
235 void gen_image(unsigned char *pixels, int xsz, int ysz)
239 for(i=0; i<ysz; i++) {
240 for(j=0; j<xsz; j++) {
243 *pixels++ = xor & 0xff;
244 *pixels++ = (xor << 1) & 0xff;
245 *pixels++ = (xor << 2) & 0xff;
250 unsigned char *load_compressed_image(const char *fname, int *cszptr, int *xszptr, int *yszptr)
252 unsigned char *pixels;
253 long start, comp_size;
257 if(!(fp = fopen(texfile, "rb"))) {
258 fprintf(stderr, "failed to open compressed texture file: %s: %s\n", texfile, strerror(errno));
262 if(fread(&xsz, sizeof xsz, 1, fp) < 1 || fread(&ysz, sizeof ysz, 1, fp) < 1) {
263 fprintf(stderr, "failed to read compressed texture file header: %s: %s\n", texfile, strerror(errno));
268 fseek(fp, 0, SEEK_END);
269 comp_size = ftell(fp) - start;
270 fseek(fp, start, SEEK_SET);
272 if(!(pixels = malloc(comp_size))) {
273 perror("failed to allocate pixel buffer");
276 if(fread(pixels, 1, comp_size, fp) < comp_size) {
277 fprintf(stderr, "failed to read compressed texture file: %s: %s\n", texfile, strerror(errno));
290 int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h)
294 if(!(fp = fopen(fname, "wb"))) {
295 fprintf(stderr, "failed to open compressed texture dump file: %s: %s\n", fname, strerror(errno));
299 if(fwrite(&w, sizeof w, 1, fp) < 1 ||
300 fwrite(&h, sizeof h, 1, fp) < 1 ||
301 fwrite(data, 1, size, fp) < size) {
302 fprintf(stderr, "failed to dump compressed texture: %s\n", strerror(errno));
308 const char *fmtstr(int fmt)
311 case 0x86b0: return "GL_COMPRESSED_RGB_FXT1_3DFX";
312 case 0x86b1: return "GL_COMPRESSED_RGBA_FXT1_3DFX";
313 case 0x8dbb: return "GL_COMPRESSED_RED_RGTC1";
314 case 0x8dbc: return "GL_COMPRESSED_SIGNED_RED_RGTC1";
315 case 0x8dbd: return "GL_COMPRESSED_RG_RGTC2";
316 case 0x8dbe: return "GL_COMPRESSED_SIGNED_RG_RGTC2";
317 case 0x8e8c: return "GL_COMPRESSED_RGBA_BPTC_UNORM";
318 case 0x8e8d: return "GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM";
319 case 0x8e8e: return "GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT";
320 case 0x8e8f: return "GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT";
321 case 0x9274: return "GL_COMPRESSED_RGB8_ETC2";
322 case 0x9275: return "GL_COMPRESSED_SRGB8_ETC2";
323 case 0x9276: return "GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2";
324 case 0x9277: return "GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2";
325 case 0x9278: return "GL_COMPRESSED_RGBA8_ETC2_EAC";
326 case 0x9279: return "GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC";
327 case 0x9270: return "GL_COMPRESSED_R11_EAC";
328 case 0x9271: return "GL_COMPRESSED_SIGNED_R11_EAC";
329 case 0x9272: return "GL_COMPRESSED_RG11_EAC";
330 case 0x9273: return "GL_COMPRESSED_SIGNED_RG11_EAC";
331 case 0x83F0: return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT";
332 case 0x83F1: return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT";
333 case 0x83F2: return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT";
334 case 0x83F3: return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT";
335 case 0x8C48: return "GL_COMPRESSED_SRGB_EXT";
336 case 0x8C49: return "GL_COMPRESSED_SRGB_ALPHA_EXT";
337 case 0x8C4A: return "GL_COMPRESSED_SLUMINANCE_EXT";
338 case 0x8C4B: return "GL_COMPRESSED_SLUMINANCE_ALPHA_EXT";
339 case 0x8C4C: return "GL_COMPRESSED_SRGB_S3TC_DXT1_EXT";
340 case 0x8C4D: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT";
341 case 0x8C4E: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT";
342 case 0x8C4F: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT";
343 case 0x8B90: return "GL_PALETTE4_RGB8_OES";
344 case 0x8B91: return "GL_PALETTE4_RGBA8_OES";
345 case 0x8B92: return "GL_PALETTE4_R5_G6_B5_OES";
346 case 0x8B93: return "GL_PALETTE4_RGBA4_OES";
347 case 0x8B94: return "GL_PALETTE4_RGB5_A1_OES";
348 case 0x8B95: return "GL_PALETTE8_RGB8_OES";
349 case 0x8B96: return "GL_PALETTE8_RGBA8_OES";
350 case 0x8B97: return "GL_PALETTE8_R5_G6_B5_OES";
351 case 0x8B98: return "GL_PALETTE8_RGBA4_OES";
352 case 0x8B99: return "GL_PALETTE8_RGB5_A1_OES";
353 case 0x93B0: return "GL_COMPRESSED_RGBA_ASTC_4";
354 case 0x93B1: return "GL_COMPRESSED_RGBA_ASTC_5";
355 case 0x93B2: return "GL_COMPRESSED_RGBA_ASTC_5";
356 case 0x93B3: return "GL_COMPRESSED_RGBA_ASTC_6";
357 case 0x93B4: return "GL_COMPRESSED_RGBA_ASTC_6";
358 case 0x93B5: return "GL_COMPRESSED_RGBA_ASTC_8";
359 case 0x93B6: return "GL_COMPRESSED_RGBA_ASTC_8";
360 case 0x93B7: return "GL_COMPRESSED_RGBA_ASTC_8";
361 case 0x93B8: return "GL_COMPRESSED_RGBA_ASTC_10";
362 case 0x93B9: return "GL_COMPRESSED_RGBA_ASTC_10";
363 case 0x93BA: return "GL_COMPRESSED_RGBA_ASTC_10";
364 case 0x93BB: return "GL_COMPRESSED_RGBA_ASTC_10";
365 case 0x93BC: return "GL_COMPRESSED_RGBA_ASTC_12";
366 case 0x93BD: return "GL_COMPRESSED_RGBA_ASTC_12";
367 case 0x93D0: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4";
368 case 0x93D1: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5";
369 case 0x93D2: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5";
370 case 0x93D3: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6";
371 case 0x93D4: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6";
372 case 0x93D5: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
373 case 0x93D6: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
374 case 0x93D7: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
375 case 0x93D8: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
376 case 0x93D9: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
377 case 0x93DA: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
378 case 0x93DB: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
379 case 0x93DC: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12";
380 case 0x93DD: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12";
387 void print_compressed_formats(void)
392 glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num_fmt);
393 printf("%d generic compressed texture formats available:\n", num_fmt);
395 if(!(fmtlist = malloc(num_fmt * sizeof *fmtlist))) {
396 fprintf(stderr, "failed to allocate texture formats enumeration buffer\n");
399 glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, fmtlist);
401 for(i=0; i<num_fmt; i++) {
402 printf(" %05x: %s ", fmtlist[i], fmtstr(fmtlist[i]));
404 glGetInternalformativ(GL_TEXTURE_2D, fmtlist[i], GL_TEXTURE_COMPRESSED, 1, ¶ms);
405 printf("(%s format)\n", params == GL_TRUE ? "compressed" : "not compressed");