added test for glCopyImageSubData, run with -copytest
[test_compression] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <assert.h>
6 #include <GL/glew.h>
7 #include <GL/freeglut.h>
8
9 #undef USE_SRGB
10
11 #ifdef USE_SRGB
12 #define COMP_FMT        GL_COMPRESSED_SRGB8_ETC2
13 #else
14 #define COMP_FMT        GL_COMPRESSED_RGB8_ETC2
15 #endif
16
17 int init(void);
18 int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels,
19                 int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype);
20 void disp(void);
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);
27
28 unsigned int tex, tex2, comp_tex;
29 const char *texfile;
30 int subtest, copytest;
31
32 int main(int argc, char **argv)
33 {
34         int i;
35         unsigned int glut_flags = GLUT_RGB | GLUT_DOUBLE;
36 #ifdef USE_SRGB
37         glut_flags |= GLUT_SRGB;
38 #endif
39
40         for(i=1; i<argc; i++) {
41                 if(argv[i][0] == '-') {
42                         if(strcmp(argv[i], "-subtest") == 0) {
43                                 subtest = 1;
44                         } else if(strcmp(argv[i], "-copytest") == 0) {
45                                 copytest = 1;
46                         } else {
47                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
48                                 return 1;
49                         }
50                 } else {
51                         if(texfile) {
52                                 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
53                                 return 1;
54                         }
55                         texfile = argv[i];
56                 }
57         }
58
59         glutInit(&argc, argv);
60         glutInitWindowSize(800, 600);
61         glutInitDisplayMode(glut_flags);
62         glutCreateWindow("test");
63
64         glutDisplayFunc(disp);
65         glutReshapeFunc(reshape);
66         glutKeyboardFunc(keyb);
67
68         glewInit();
69
70         if(init() == -1) {
71                 return 1;
72         }
73
74         glutMainLoop();
75         return 0;
76 }
77
78 int init(void)
79 {
80         unsigned char *pixels, *buf;
81         int xsz = 512;
82         int ysz = 512;
83         int is_comp = 0;
84         int comp_size = 0;
85         int tmp;
86
87         print_compressed_formats();
88
89         if(texfile) {
90                 if(!(pixels = load_compressed_image(texfile, &comp_size, &xsz, &ysz))) {
91                         return -1;
92                 }
93                 printf("loaded compressed texture file: %s (%dx%d)\n", texfile, xsz, ysz);
94
95         } else {
96                 if(!(pixels = malloc(xsz * ysz * 3))) {
97                         abort();
98                 }
99                 gen_image(pixels, xsz, ysz);
100
101                 printf("compressing texture\n");
102                 if((comp_size = texcomp(pixels, COMP_FMT, pixels, xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE)) == -1) {
103                         return -1;
104                 }
105                 printf("compressed texture is %d bytes (uncompressed was: %d)\n", comp_size, xsz * ysz * 3);
106
107                 dump_compressed_image("compressed_texture", pixels, comp_size, xsz, ysz);
108         }
109
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);
115         if(glGetError()) {
116                 fprintf(stderr, "failed to upload compressed texture\n");
117                 return -1;
118         }
119
120         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
121         if(!is_comp) {
122                 fprintf(stderr, "texture is not compressed\n");
123                 return -1;
124         }
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);
128                 return -1;
129         }
130
131         if(!(buf = malloc(comp_size))) {
132                 fprintf(stderr, "failed to allocate comparison image buffer (%d bytes)\n", comp_size);
133                 return -1;
134         }
135         glGetCompressedTexImage(GL_TEXTURE_2D, 0, buf);
136
137         if(memcmp(pixels, buf, comp_size) != 0) {
138                 fprintf(stderr, "submitted and retrieved pixel data differ!\n");
139         } else {
140                 printf("submitted and retrieved sizes match (%d bytes)\n", comp_size);
141         }
142
143         if(subtest) {
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);
148         }
149
150         if(copytest) {
151                 printf("testing glCopyImageSubData\n");
152
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);
159
160                 glCopyImageSubData(tex2, GL_TEXTURE_2D, 0, 128, 64, 0,
161                                 tex, GL_TEXTURE_2D, 0, 32, 32, 0, 64, 64, 1);
162
163                 glBindTexture(GL_TEXTURE_2D, tex);
164         }
165
166         free(buf);
167         free(pixels);
168
169 #ifdef USE_SRGB
170         glEnable(GL_FRAMEBUFFER_SRGB);
171 #endif
172
173         glEnable(GL_TEXTURE_2D);
174         return 0;
175 }
176
177 int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels,
178                 int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype)
179 {
180         unsigned int tex;
181         int is_comp = 0;
182         int comp_size = 0;
183
184         glGenTextures(1, &tex);
185         glBindTexture(GL_TEXTURE_2D, tex);
186         glTexImage2D(GL_TEXTURE_2D, 0, tofmt, xsz, ysz, 0, fromfmt, fromtype, pixels);
187         if(glGetError()) {
188                 fprintf(stderr, "failed to compress texture\n");
189                 return -1;
190         }
191
192         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
193         if(!is_comp) {
194                 fprintf(stderr, "texture is not compressed\n");
195                 return -1;
196         }
197         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &comp_size);
198
199         glGetCompressedTexImage(GL_TEXTURE_2D, 0, compix);
200         return comp_size;
201 }
202
203 void disp(void)
204 {
205         glClear(GL_COLOR_BUFFER_BIT);
206
207         glBegin(GL_QUADS);
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);
212         glEnd();
213
214         glutSwapBuffers();
215         assert(glGetError() == GL_NO_ERROR);
216 }
217
218 void reshape(int x, int y)
219 {
220         float aspect = (float)x / (float)y;
221         glViewport(0, 0, x, y);
222
223         glMatrixMode(GL_PROJECTION);
224         glLoadIdentity();
225         glScalef(1.0 / aspect, 1, 1);
226 }
227
228 void keyb(unsigned char key, int x, int y)
229 {
230         if(key == 27) {
231                 exit(0);
232         }
233 }
234
235 void gen_image(unsigned char *pixels, int xsz, int ysz)
236 {
237         int i, j;
238
239         for(i=0; i<ysz; i++) {
240                 for(j=0; j<xsz; j++) {
241                         int xor = i ^ j;
242
243                         *pixels++ = xor & 0xff;
244                         *pixels++ = (xor << 1) & 0xff;
245                         *pixels++ = (xor << 2) & 0xff;
246                 }
247         }
248 }
249
250 unsigned char *load_compressed_image(const char *fname, int *cszptr, int *xszptr, int *yszptr)
251 {
252         unsigned char *pixels;
253         long start, comp_size;
254         int xsz, ysz;
255         FILE *fp;
256
257         if(!(fp = fopen(texfile, "rb"))) {
258                 fprintf(stderr, "failed to open compressed texture file: %s: %s\n", texfile, strerror(errno));
259                 return 0;
260         }
261
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));
264                 fclose(fp);
265                 return 0;
266         }
267         start = ftell(fp);
268         fseek(fp, 0, SEEK_END);
269         comp_size = ftell(fp) - start;
270         fseek(fp, start, SEEK_SET);
271
272         if(!(pixels = malloc(comp_size))) {
273                 perror("failed to allocate pixel buffer");
274                 return 0;
275         }
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));
278                 fclose(fp);
279                 free(pixels);
280                 return 0;
281         }
282         fclose(fp);
283
284         *cszptr = comp_size;
285         *xszptr = xsz;
286         *yszptr = ysz;
287         return pixels;
288 }
289
290 int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h)
291 {
292         FILE *fp;
293
294         if(!(fp = fopen(fname, "wb"))) {
295                 fprintf(stderr, "failed to open compressed texture dump file: %s: %s\n", fname, strerror(errno));
296                 return -1;
297         }
298
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));
303         }
304         fclose(fp);
305         return 0;
306 }
307
308 const char *fmtstr(int fmt)
309 {
310         switch(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";
381         default:
382                 break;
383         }
384         return "unknown";
385 }
386
387 void print_compressed_formats(void)
388 {
389         int i, num_fmt;
390         int *fmtlist;
391
392         glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num_fmt);
393         printf("%d generic compressed texture formats available:\n", num_fmt);
394
395         if(!(fmtlist = malloc(num_fmt * sizeof *fmtlist))) {
396                 fprintf(stderr, "failed to allocate texture formats enumeration buffer\n");
397                 return;
398         }
399         glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, fmtlist);
400
401         for(i=0; i<num_fmt; i++) {
402                 printf(" %05x: %s ", fmtlist[i], fmtstr(fmtlist[i]));
403                 GLint params;
404                 glGetInternalformativ(GL_TEXTURE_2D, fmtlist[i], GL_TEXTURE_COMPRESSED, 1, &params);
405                 printf("(%s format)\n", params == GL_TRUE ? "compressed" : "not compressed");
406         }
407         free(fmtlist);
408 }