added check for compressed format support (ARB_internalformat_query2.txt)
[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, comp_tex;
29 const char *texfile;
30
31 int main(int argc, char **argv)
32 {
33         unsigned int glut_flags = GLUT_RGB | GLUT_DOUBLE;
34 #ifdef USE_SRGB
35         glut_flags |= GLUT_SRGB;
36 #endif
37
38         texfile = argv[1];
39
40         glutInit(&argc, argv);
41         glutInitWindowSize(800, 600);
42         glutInitDisplayMode(glut_flags);
43         glutCreateWindow("test");
44
45         glutDisplayFunc(disp);
46         glutReshapeFunc(reshape);
47         glutKeyboardFunc(keyb);
48
49         glewInit();
50
51         if(init() == -1) {
52                 return 1;
53         }
54
55         glutMainLoop();
56         return 0;
57 }
58
59 int init(void)
60 {
61         unsigned char *pixels, *buf;
62         int xsz = 512;
63         int ysz = 512;
64         int is_comp = 0;
65         int comp_size = 0;
66         int tmp;
67
68         print_compressed_formats();
69
70         if(texfile) {
71                 if(!(pixels = load_compressed_image(texfile, &comp_size, &xsz, &ysz))) {
72                         return -1;
73                 }
74                 printf("loaded compressed texture file: %s (%dx%d)\n", texfile, xsz, ysz);
75
76         } else {
77                 if(!(pixels = malloc(xsz * ysz * 3))) {
78                         abort();
79                 }
80                 gen_image(pixels, xsz, ysz);
81
82                 printf("compressing texture\n");
83                 if((comp_size = texcomp(pixels, COMP_FMT, pixels, xsz, ysz, GL_RGB, GL_UNSIGNED_BYTE)) == -1) {
84                         return -1;
85                 }
86                 printf("compressed texture is %d bytes (uncompressed was: %d)\n", comp_size, xsz * ysz * 3);
87
88                 dump_compressed_image("compressed_texture", pixels, comp_size, xsz, ysz);
89         }
90
91         glGenTextures(1, &tex);
92         glBindTexture(GL_TEXTURE_2D, tex);
93         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
94         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
95         glCompressedTexImage2D(GL_TEXTURE_2D, 0, COMP_FMT, xsz, ysz, 0, comp_size, pixels);
96         if(glGetError()) {
97                 fprintf(stderr, "failed to upload compressed texture\n");
98                 return -1;
99         }
100
101         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
102         if(!is_comp) {
103                 fprintf(stderr, "texture is not compressed\n");
104                 return -1;
105         }
106         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &tmp);
107         if(tmp != comp_size) {
108                 fprintf(stderr, "internal compressed size differs (expected: %d, got: %d)!\n", comp_size, tmp);
109                 return -1;
110         }
111
112         if(!(buf = malloc(comp_size))) {
113                 fprintf(stderr, "failed to allocate comparison image buffer (%d bytes)\n", comp_size);
114                 return -1;
115         }
116         glGetCompressedTexImage(GL_TEXTURE_2D, 0, buf);
117
118         if(memcmp(pixels, buf, comp_size) != 0) {
119                 fprintf(stderr, "submitted and retrieved pixel data differ!\n");
120         } else {
121                 printf("submitted and retrieved sizes match (%d bytes)\n", comp_size);
122         }
123         free(buf);
124         free(pixels);
125
126 #ifdef USE_SRGB
127         glEnable(GL_FRAMEBUFFER_SRGB);
128 #endif
129
130         glEnable(GL_TEXTURE_2D);
131         return 0;
132 }
133
134 int texcomp(unsigned char *compix, unsigned int tofmt, unsigned char *pixels,
135                 int xsz, int ysz, unsigned int fromfmt, unsigned int fromtype)
136 {
137         unsigned int tex;
138         int is_comp = 0;
139         int comp_size = 0;
140
141         glGenTextures(1, &tex);
142         glBindTexture(GL_TEXTURE_2D, tex);
143         glTexImage2D(GL_TEXTURE_2D, 0, tofmt, xsz, ysz, 0, fromfmt, fromtype, pixels);
144         if(glGetError()) {
145                 fprintf(stderr, "failed to compress texture\n");
146                 return -1;
147         }
148
149         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED, &is_comp);
150         if(!is_comp) {
151                 fprintf(stderr, "texture is not compressed\n");
152                 return -1;
153         }
154         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &comp_size);
155
156         glGetCompressedTexImage(GL_TEXTURE_2D, 0, compix);
157         return comp_size;
158 }
159
160 void disp(void)
161 {
162         glClear(GL_COLOR_BUFFER_BIT);
163
164         glBegin(GL_QUADS);
165         glTexCoord2f(0, 1); glVertex2f(-1, -1);
166         glTexCoord2f(1, 1); glVertex2f(1, -1);
167         glTexCoord2f(1, 0); glVertex2f(1, 1);
168         glTexCoord2f(0, 0);     glVertex2f(-1, 1);
169         glEnd();
170
171         glutSwapBuffers();
172         assert(glGetError() == GL_NO_ERROR);
173 }
174
175 void reshape(int x, int y)
176 {
177         float aspect = (float)x / (float)y;
178         glViewport(0, 0, x, y);
179
180         glMatrixMode(GL_PROJECTION);
181         glLoadIdentity();
182         glScalef(1.0 / aspect, 1, 1);
183 }
184
185 void keyb(unsigned char key, int x, int y)
186 {
187         if(key == 27) {
188                 exit(0);
189         }
190 }
191
192 void gen_image(unsigned char *pixels, int xsz, int ysz)
193 {
194         int i, j;
195
196         for(i=0; i<ysz; i++) {
197                 for(j=0; j<xsz; j++) {
198                         int xor = i ^ j;
199
200                         *pixels++ = xor & 0xff;
201                         *pixels++ = (xor << 1) & 0xff;
202                         *pixels++ = (xor << 2) & 0xff;
203                 }
204         }
205 }
206
207 unsigned char *load_compressed_image(const char *fname, int *cszptr, int *xszptr, int *yszptr)
208 {
209         unsigned char *pixels;
210         long start, comp_size;
211         int xsz, ysz;
212         FILE *fp;
213
214         if(!(fp = fopen(texfile, "rb"))) {
215                 fprintf(stderr, "failed to open compressed texture file: %s: %s\n", texfile, strerror(errno));
216                 return 0;
217         }
218
219         if(fread(&xsz, sizeof xsz, 1, fp) < 1 || fread(&ysz, sizeof ysz, 1, fp) < 1) {
220                 fprintf(stderr, "failed to read compressed texture file header: %s: %s\n", texfile, strerror(errno));
221                 fclose(fp);
222                 return 0;
223         }
224         start = ftell(fp);
225         fseek(fp, 0, SEEK_END);
226         comp_size = ftell(fp) - start;
227         fseek(fp, start, SEEK_SET);
228
229         if(!(pixels = malloc(comp_size))) {
230                 perror("failed to allocate pixel buffer");
231                 return 0;
232         }
233         if(fread(pixels, 1, comp_size, fp) < comp_size) {
234                 fprintf(stderr, "failed to read compressed texture file: %s: %s\n", texfile, strerror(errno));
235                 fclose(fp);
236                 free(pixels);
237                 return 0;
238         }
239         fclose(fp);
240
241         *cszptr = comp_size;
242         *xszptr = xsz;
243         *yszptr = ysz;
244         return pixels;
245 }
246
247 int dump_compressed_image(const char *fname, unsigned char *data, int size, int w, int h)
248 {
249         FILE *fp;
250
251         if(!(fp = fopen(fname, "wb"))) {
252                 fprintf(stderr, "failed to open compressed texture dump file: %s: %s\n", fname, strerror(errno));
253                 return -1;
254         }
255
256         if(fwrite(&w, sizeof w, 1, fp) < 1 ||
257                         fwrite(&h, sizeof h, 1, fp) < 1 ||
258                         fwrite(data, 1, size, fp) < size) {
259                 fprintf(stderr, "failed to dump compressed texture: %s\n", strerror(errno));
260         }
261         fclose(fp);
262         return 0;
263 }
264
265 const char *fmtstr(int fmt)
266 {
267         switch(fmt) {
268         case 0x86b0: return "GL_COMPRESSED_RGB_FXT1_3DFX";
269         case 0x86b1: return "GL_COMPRESSED_RGBA_FXT1_3DFX";
270         case 0x8dbb: return "GL_COMPRESSED_RED_RGTC1";
271         case 0x8dbc: return "GL_COMPRESSED_SIGNED_RED_RGTC1";
272         case 0x8dbd: return "GL_COMPRESSED_RG_RGTC2";
273         case 0x8dbe: return "GL_COMPRESSED_SIGNED_RG_RGTC2";
274         case 0x8e8c: return "GL_COMPRESSED_RGBA_BPTC_UNORM";
275         case 0x8e8d: return "GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM";
276         case 0x8e8e: return "GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT";
277         case 0x8e8f: return "GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT";
278         case 0x9274: return "GL_COMPRESSED_RGB8_ETC2";
279         case 0x9275: return "GL_COMPRESSED_SRGB8_ETC2";
280         case 0x9276: return "GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2";
281         case 0x9277: return "GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2";
282         case 0x9278: return "GL_COMPRESSED_RGBA8_ETC2_EAC";
283         case 0x9279: return "GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC";
284         case 0x9270: return "GL_COMPRESSED_R11_EAC";
285         case 0x9271: return "GL_COMPRESSED_SIGNED_R11_EAC";
286         case 0x9272: return "GL_COMPRESSED_RG11_EAC";
287         case 0x9273: return "GL_COMPRESSED_SIGNED_RG11_EAC";
288         case 0x83F0: return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT";
289         case 0x83F1: return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT";
290         case 0x83F2: return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT";
291         case 0x83F3: return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT";
292         case 0x8C48: return "GL_COMPRESSED_SRGB_EXT";
293         case 0x8C49: return "GL_COMPRESSED_SRGB_ALPHA_EXT";
294         case 0x8C4A: return "GL_COMPRESSED_SLUMINANCE_EXT";
295         case 0x8C4B: return "GL_COMPRESSED_SLUMINANCE_ALPHA_EXT";
296         case 0x8C4C: return "GL_COMPRESSED_SRGB_S3TC_DXT1_EXT";
297         case 0x8C4D: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT";
298         case 0x8C4E: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT";
299         case 0x8C4F: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT";
300         case 0x8B90: return "GL_PALETTE4_RGB8_OES";
301         case 0x8B91: return "GL_PALETTE4_RGBA8_OES";
302         case 0x8B92: return "GL_PALETTE4_R5_G6_B5_OES";
303         case 0x8B93: return "GL_PALETTE4_RGBA4_OES";
304         case 0x8B94: return "GL_PALETTE4_RGB5_A1_OES";
305         case 0x8B95: return "GL_PALETTE8_RGB8_OES";
306         case 0x8B96: return "GL_PALETTE8_RGBA8_OES";
307         case 0x8B97: return "GL_PALETTE8_R5_G6_B5_OES";
308         case 0x8B98: return "GL_PALETTE8_RGBA4_OES";
309         case 0x8B99: return "GL_PALETTE8_RGB5_A1_OES";
310         case 0x93B0: return "GL_COMPRESSED_RGBA_ASTC_4";
311         case 0x93B1: return "GL_COMPRESSED_RGBA_ASTC_5";
312         case 0x93B2: return "GL_COMPRESSED_RGBA_ASTC_5";
313         case 0x93B3: return "GL_COMPRESSED_RGBA_ASTC_6";
314         case 0x93B4: return "GL_COMPRESSED_RGBA_ASTC_6";
315         case 0x93B5: return "GL_COMPRESSED_RGBA_ASTC_8";
316         case 0x93B6: return "GL_COMPRESSED_RGBA_ASTC_8";
317         case 0x93B7: return "GL_COMPRESSED_RGBA_ASTC_8";
318         case 0x93B8: return "GL_COMPRESSED_RGBA_ASTC_10";
319         case 0x93B9: return "GL_COMPRESSED_RGBA_ASTC_10";
320         case 0x93BA: return "GL_COMPRESSED_RGBA_ASTC_10";
321         case 0x93BB: return "GL_COMPRESSED_RGBA_ASTC_10";
322         case 0x93BC: return "GL_COMPRESSED_RGBA_ASTC_12";
323         case 0x93BD: return "GL_COMPRESSED_RGBA_ASTC_12";
324         case 0x93D0: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4";
325         case 0x93D1: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5";
326         case 0x93D2: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5";
327         case 0x93D3: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6";
328         case 0x93D4: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6";
329         case 0x93D5: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
330         case 0x93D6: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
331         case 0x93D7: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8";
332         case 0x93D8: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
333         case 0x93D9: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
334         case 0x93DA: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
335         case 0x93DB: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10";
336         case 0x93DC: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12";
337         case 0x93DD: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12";
338         default:
339                 break;
340         }
341         return "unknown";
342 }
343
344 void print_compressed_formats(void)
345 {
346         int i, num_fmt;
347         int *fmtlist;
348
349         glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num_fmt);
350         printf("%d generic compressed texture formats available:\n", num_fmt);
351
352         if(!(fmtlist = malloc(num_fmt * sizeof *fmtlist))) {
353                 fprintf(stderr, "failed to allocate texture formats enumeration buffer\n");
354                 return;
355         }
356         glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, fmtlist);
357
358         for(i=0; i<num_fmt; i++) {
359                 printf("\n");
360                 printf(" %05x: %s\n", fmtlist[i], fmtstr(fmtlist[i]));
361                 GLint params;
362                 glGetInternalformativ(GL_TEXTURE_2D, fmtlist[i], GL_TEXTURE_COMPRESSED, 1, &params);
363                 printf("the format is %s\n", params == GL_TRUE ? "compressed" : "not compressed");
364         }
365         free(fmtlist);
366 }