]> git.cworth.org Git - apitrace/blob - glstate.cpp
Fix fetching of the data for uniform arrays.
[apitrace] / glstate.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <string.h>
28
29 #include <algorithm>
30 #include <iostream>
31 #include <map>
32 #include <sstream>
33
34 #include "image.hpp"
35 #include "json.hpp"
36 #include "glproc.hpp"
37 #include "glsize.hpp"
38 #include "glstate.hpp"
39
40
41 #ifdef __APPLE__
42
43 #include <Carbon/Carbon.h>
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
50
51 #ifdef __cplusplus
52 }
53 #endif
54
55 #endif /* __APPLE__ */
56
57
58 namespace glstate {
59
60
61 static inline void
62 resetPixelPackState(void) {
63     glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
64     glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
65     glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
66     glPixelStorei(GL_PACK_ROW_LENGTH, 0);
67     glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
68     glPixelStorei(GL_PACK_SKIP_ROWS, 0);
69     glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
70     glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
71     glPixelStorei(GL_PACK_ALIGNMENT, 1);
72     glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
73 }
74
75
76 static inline void
77 restorePixelPackState(void) {
78     glPopClientAttrib();
79 }
80
81
82 // Mapping from shader type to shader source, used to accumulated the sources
83 // of different shaders with same type.
84 typedef std::map<std::string, std::string> ShaderMap;
85
86
87 static void
88 getShaderSource(ShaderMap &shaderMap, GLuint shader)
89 {
90     if (!shader) {
91         return;
92     }
93
94     GLint shader_type = 0;
95     glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
96     if (!shader_type) {
97         return;
98     }
99
100     GLint source_length = 0;
101     glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
102     if (!source_length) {
103         return;
104     }
105
106     GLchar *source = new GLchar[source_length];
107     GLsizei length = 0;
108     source[0] = 0;
109     glGetShaderSource(shader, source_length, &length, source);
110
111     shaderMap[enumToString(shader_type)] += source;
112
113     delete [] source;
114 }
115
116
117 static void
118 getShaderObjSource(ShaderMap &shaderMap, GLhandleARB shaderObj)
119 {
120     if (!shaderObj) {
121         return;
122     }
123
124     GLint object_type = 0;
125     glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &object_type);
126     if (object_type != GL_SHADER_OBJECT_ARB) {
127         return;
128     }
129
130     GLint shader_type = 0;
131     glGetObjectParameterivARB(shaderObj, GL_OBJECT_SUBTYPE_ARB, &shader_type);
132     if (!shader_type) {
133         return;
134     }
135
136     GLint source_length = 0;
137     glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
138     if (!source_length) {
139         return;
140     }
141
142     GLcharARB *source = new GLcharARB[source_length];
143     GLsizei length = 0;
144     source[0] = 0;
145     glGetShaderSource(shaderObj, source_length, &length, source);
146
147     shaderMap[enumToString(shader_type)] += source;
148
149     delete [] source;
150 }
151
152
153 static inline void
154 dumpProgram(JSONWriter &json, GLint program)
155 {
156     GLint attached_shaders = 0;
157     glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
158     if (!attached_shaders) {
159         return;
160     }
161
162     ShaderMap shaderMap;
163
164     GLuint *shaders = new GLuint[attached_shaders];
165     GLsizei count = 0;
166     glGetAttachedShaders(program, attached_shaders, &count, shaders);
167     std::sort(shaders, shaders + count);
168     for (GLsizei i = 0; i < count; ++ i) {
169        getShaderSource(shaderMap, shaders[i]);
170     }
171     delete [] shaders;
172
173     for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
174         json.beginMember(it->first);
175         json.writeString(it->second);
176         json.endMember();
177     }
178 }
179
180
181 static inline void
182 dumpProgramObj(JSONWriter &json, GLhandleARB programObj)
183 {
184     GLint attached_shaders = 0;
185     glGetObjectParameterivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
186     if (!attached_shaders) {
187         return;
188     }
189
190     ShaderMap shaderMap;
191
192     GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
193     GLsizei count = 0;
194     glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
195     std::sort(shaderObjs, shaderObjs + count);
196     for (GLsizei i = 0; i < count; ++ i) {
197        getShaderObjSource(shaderMap, shaderObjs[i]);
198     }
199     delete [] shaderObjs;
200
201     for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
202         json.beginMember(it->first);
203         json.writeString(it->second);
204         json.endMember();
205     }
206 }
207
208 /*
209  * When fetching the uniform name of an array we usually get name[0]
210  * so we need to cut the trailing "[0]" in order to properly construct
211  * array names later. Otherwise we endup with stuff like
212  * uniformArray[0][0],
213  * uniformArray[0][1],
214  * instead of
215  * uniformArray[0],
216  * uniformArray[1].
217  */
218 static std::string
219 resolveUniformName(const GLchar *name,  GLint size)
220 {
221     std::string qualifiedName(name);
222     if (size > 1) {
223         std::string::size_type nameLength = qualifiedName.length();
224         static const char * const arrayStart = "[0]";
225         static const int arrayStartLen = 3;
226         if (qualifiedName.rfind(arrayStart) == (nameLength - arrayStartLen)) {
227             qualifiedName = qualifiedName.substr(0, nameLength - 3);
228         }
229     }
230     return qualifiedName;
231 }
232
233 static void
234 dumpUniform(JSONWriter &json, GLint program, GLint size, GLenum type, const GLchar *name) {
235     GLenum elemType;
236     GLint numElems;
237     __gl_uniform_size(type, elemType, numElems);
238     if (elemType == GL_NONE) {
239         return;
240     }
241
242     GLfloat fvalues[4*4];
243     GLdouble dvalues[4*4];
244     GLint ivalues[4*4];
245     GLuint uivalues[4*4];
246
247     GLint i, j;
248
249     std::string qualifiedName = resolveUniformName(name, size);
250
251     for (i = 0; i < size; ++i) {
252         std::stringstream ss;
253         ss << qualifiedName;
254
255         if (size > 1) {
256             ss << '[' << i << ']';
257         }
258
259         std::string elemName = ss.str();
260
261         json.beginMember(elemName);
262
263         GLint location = glGetUniformLocation(program, elemName.c_str());
264
265         if (numElems > 1) {
266             json.beginArray();
267         }
268
269         switch (elemType) {
270         case GL_FLOAT:
271             glGetUniformfv(program, location, fvalues);
272             for (j = 0; j < numElems; ++j) {
273                 json.writeNumber(fvalues[j]);
274             }
275             break;
276         case GL_DOUBLE:
277             glGetUniformdv(program, location, dvalues);
278             for (j = 0; j < numElems; ++j) {
279                 json.writeNumber(dvalues[j]);
280             }
281             break;
282         case GL_INT:
283             glGetUniformiv(program, location, ivalues);
284             for (j = 0; j < numElems; ++j) {
285                 json.writeNumber(ivalues[j]);
286             }
287             break;
288         case GL_UNSIGNED_INT:
289             glGetUniformuiv(program, location, uivalues);
290             for (j = 0; j < numElems; ++j) {
291                 json.writeNumber(uivalues[j]);
292             }
293             break;
294         case GL_BOOL:
295             glGetUniformiv(program, location, ivalues);
296             for (j = 0; j < numElems; ++j) {
297                 json.writeBool(ivalues[j]);
298             }
299             break;
300         default:
301             assert(0);
302             break;
303         }
304
305         if (numElems > 1) {
306             json.endArray();
307         }
308
309         json.endMember();
310     }
311 }
312
313
314 static void
315 dumpUniformARB(JSONWriter &json, GLhandleARB programObj, GLint size, GLenum type, const GLchar *name) {
316
317     GLenum elemType;
318     GLint numElems;
319     __gl_uniform_size(type, elemType, numElems);
320     if (elemType == GL_NONE) {
321         return;
322     }
323
324     GLfloat fvalues[4*4];
325     GLint ivalues[4*4];
326
327     GLint i, j;
328
329     std::string qualifiedName = resolveUniformName(name, size);
330
331     for (i = 0; i < size; ++i) {
332         std::stringstream ss;
333         ss << qualifiedName;
334
335         if (size > 1) {
336             ss << '[' << i << ']';
337         }
338
339         std::string elemName = ss.str();
340
341         json.beginMember(elemName);
342
343         GLint location = glGetUniformLocationARB(programObj, elemName.c_str());
344
345         if (numElems > 1) {
346             json.beginArray();
347         }
348
349         switch (elemType) {
350         case GL_DOUBLE:
351             // glGetUniformdvARB does not exists
352         case GL_FLOAT:
353             glGetUniformfvARB(programObj, location, fvalues);
354             for (j = 0; j < numElems; ++j) {
355                 json.writeNumber(fvalues[j]);
356             }
357             break;
358         case GL_UNSIGNED_INT:
359             // glGetUniformuivARB does not exists
360         case GL_INT:
361             glGetUniformivARB(programObj, location, ivalues);
362             for (j = 0; j < numElems; ++j) {
363                 json.writeNumber(ivalues[j]);
364             }
365             break;
366         case GL_BOOL:
367             glGetUniformivARB(programObj, location, ivalues);
368             for (j = 0; j < numElems; ++j) {
369                 json.writeBool(ivalues[j]);
370             }
371             break;
372         default:
373             assert(0);
374             break;
375         }
376
377         if (numElems > 1) {
378             json.endArray();
379         }
380
381         json.endMember();
382     }
383 }
384
385
386 static inline void
387 dumpProgramUniforms(JSONWriter &json, GLint program)
388 {
389     GLint active_uniforms = 0;
390     glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &active_uniforms);
391     if (!active_uniforms) {
392         return;
393     }
394
395     GLint active_uniform_max_length = 0;
396     glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &active_uniform_max_length);
397     GLchar *name = new GLchar[active_uniform_max_length];
398     if (!name) {
399         return;
400     }
401
402     for (GLint index = 0; index < active_uniforms; ++index) {
403         GLsizei length = 0;
404         GLint size = 0;
405         GLenum type = GL_NONE;
406         glGetActiveUniform(program, index, active_uniform_max_length, &length, &size, &type, name);
407
408         dumpUniform(json, program, size, type, name);
409     }
410
411     delete [] name;
412 }
413
414
415 static inline void
416 dumpProgramObjUniforms(JSONWriter &json, GLhandleARB programObj)
417 {
418     GLint active_uniforms = 0;
419     glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &active_uniforms);
420     if (!active_uniforms) {
421         return;
422     }
423
424     GLint active_uniform_max_length = 0;
425     glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB, &active_uniform_max_length);
426     GLchar *name = new GLchar[active_uniform_max_length];
427     if (!name) {
428         return;
429     }
430
431     for (GLint index = 0; index < active_uniforms; ++index) {
432         GLsizei length = 0;
433         GLint size = 0;
434         GLenum type = GL_NONE;
435         glGetActiveUniformARB(programObj, index, active_uniform_max_length, &length, &size, &type, name);
436
437         dumpUniformARB(json, programObj, size, type, name);
438     }
439
440     delete [] name;
441 }
442
443
444 static inline void
445 dumpArbProgram(JSONWriter &json, GLenum target)
446 {
447     if (!glIsEnabled(target)) {
448         return;
449     }
450
451     GLint program_length = 0;
452     glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
453     if (!program_length) {
454         return;
455     }
456
457     GLchar *source = new GLchar[program_length + 1];
458     source[0] = 0;
459     glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
460     source[program_length] = 0;
461
462     json.beginMember(enumToString(target));
463     json.writeString(source);
464     json.endMember();
465
466     delete [] source;
467 }
468
469
470 static inline void
471 dumpArbProgramUniforms(JSONWriter &json, GLenum target, const char *prefix)
472 {
473     if (!glIsEnabled(target)) {
474         return;
475     }
476
477     GLint program_parameters = 0;
478     glGetProgramivARB(target, GL_PROGRAM_PARAMETERS_ARB, &program_parameters);
479     if (!program_parameters) {
480         return;
481     }
482
483     GLint max_program_local_parameters = 0;
484     glGetProgramivARB(target, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &max_program_local_parameters);
485     for (GLint index = 0; index < max_program_local_parameters; ++index) {
486         GLdouble params[4] = {0, 0, 0, 0};
487         glGetProgramLocalParameterdvARB(target, index, params);
488
489         if (!params[0] && !params[1] && !params[2] && !params[3]) {
490             continue;
491         }
492
493         char name[256];
494         snprintf(name, sizeof name, "%sprogram.local[%i]", prefix, index);
495
496         json.beginMember(name);
497         json.beginArray();
498         json.writeNumber(params[0]);
499         json.writeNumber(params[1]);
500         json.writeNumber(params[2]);
501         json.writeNumber(params[3]);
502         json.endArray();
503         json.endMember();
504     }
505
506     GLint max_program_env_parameters = 0;
507     glGetProgramivARB(target, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, &max_program_env_parameters);
508     for (GLint index = 0; index < max_program_env_parameters; ++index) {
509         GLdouble params[4] = {0, 0, 0, 0};
510         glGetProgramEnvParameterdvARB(target, index, params);
511
512         if (!params[0] && !params[1] && !params[2] && !params[3]) {
513             continue;
514         }
515
516         char name[256];
517         snprintf(name, sizeof name, "%sprogram.env[%i]", prefix, index);
518
519         json.beginMember(name);
520         json.beginArray();
521         json.writeNumber(params[0]);
522         json.writeNumber(params[1]);
523         json.writeNumber(params[2]);
524         json.writeNumber(params[3]);
525         json.endArray();
526         json.endMember();
527     }
528 }
529
530
531 static inline void
532 dumpShadersUniforms(JSONWriter &json)
533 {
534     GLint program = 0;
535     glGetIntegerv(GL_CURRENT_PROGRAM, &program);
536
537     GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
538
539     json.beginMember("shaders");
540     json.beginObject();
541     if (program) {
542         dumpProgram(json, program);
543     } else if (programObj) {
544         dumpProgramObj(json, programObj);
545     } else {
546         dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
547         dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
548     }
549     json.endObject();
550     json.endMember(); // shaders
551
552     json.beginMember("uniforms");
553     json.beginObject();
554     if (program) {
555         dumpProgramUniforms(json, program);
556     } else if (programObj) {
557         dumpProgramObjUniforms(json, programObj);
558     } else {
559         dumpArbProgramUniforms(json, GL_FRAGMENT_PROGRAM_ARB, "fp.");
560         dumpArbProgramUniforms(json, GL_VERTEX_PROGRAM_ARB, "vp.");
561     }
562     json.endObject();
563     json.endMember(); // uniforms
564 }
565
566
567 static inline void
568 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
569 {
570     GLint width, height = 1, depth = 1;
571
572     width = 0;
573     glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
574
575     if (target != GL_TEXTURE_1D) {
576         height = 0;
577         glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
578         if (target == GL_TEXTURE_3D) {
579             depth = 0;
580             glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
581         }
582     }
583
584     if (width <= 0 || height <= 0 || depth <= 0) {
585         return;
586     } else {
587         char label[512];
588
589         GLint active_texture = GL_TEXTURE0;
590         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
591         snprintf(label, sizeof label, "%s, %s, level = %i", enumToString(active_texture), enumToString(target), level);
592
593         json.beginMember(label);
594
595         json.beginObject();
596
597         // Tell the GUI this is no ordinary object, but an image
598         json.writeStringMember("__class__", "image");
599
600         json.writeNumberMember("__width__", width);
601         json.writeNumberMember("__height__", height);
602         json.writeNumberMember("__depth__", depth);
603
604         // Hardcoded for now, but we could chose types more adequate to the
605         // texture internal format
606         json.writeStringMember("__type__", "uint8");
607         json.writeBoolMember("__normalized__", true);
608         json.writeNumberMember("__channels__", 4);
609
610         GLubyte *pixels = new GLubyte[depth*width*height*4];
611
612         resetPixelPackState();
613
614         glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
615
616         restorePixelPackState();
617
618         json.beginMember("__data__");
619         char *pngBuffer;
620         int pngBufferSize;
621         image::writePixelsToBuffer(pixels, width, height, 4, true, &pngBuffer, &pngBufferSize);
622         json.writeBase64(pngBuffer, pngBufferSize);
623         free(pngBuffer);
624         json.endMember(); // __data__
625
626         delete [] pixels;
627         json.endObject();
628     }
629 }
630
631
632 static inline void
633 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
634 {
635     GLint texture_binding = 0;
636     glGetIntegerv(binding, &texture_binding);
637     if (!glIsEnabled(target) && !texture_binding) {
638         return;
639     }
640
641     GLint level = 0;
642     do {
643         GLint width = 0, height = 0;
644         glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
645         glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
646         if (!width || !height) {
647             break;
648         }
649
650         if (target == GL_TEXTURE_CUBE_MAP) {
651             for (int face = 0; face < 6; ++face) {
652                 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
653             }
654         } else {
655             dumpTextureImage(json, target, level);
656         }
657
658         ++level;
659     } while(true);
660 }
661
662
663 static inline void
664 dumpTextures(JSONWriter &json)
665 {
666     json.beginMember("textures");
667     json.beginObject();
668     GLint active_texture = GL_TEXTURE0;
669     glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
670     GLint max_texture_coords = 0;
671     glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
672     GLint max_combined_texture_image_units = 0;
673     glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
674     GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
675     for (GLint unit = 0; unit < max_units; ++unit) {
676         GLenum texture = GL_TEXTURE0 + unit;
677         glActiveTexture(texture);
678         dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
679         dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
680         dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
681         dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
682         dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
683     }
684     glActiveTexture(active_texture);
685     json.endObject();
686     json.endMember(); // textures
687 }
688
689
690 static bool
691 getDrawableBounds(GLint *width, GLint *height) {
692 #if defined(_WIN32)
693
694     HDC hDC = wglGetCurrentDC();
695     if (!hDC) {
696         return false;
697     }
698
699     HWND hWnd = WindowFromDC(hDC);
700     RECT rect;
701
702     if (!GetClientRect(hWnd, &rect)) {
703        return false;
704     }
705
706     *width  = rect.right  - rect.left;
707     *height = rect.bottom - rect.top;
708
709 #elif defined(__APPLE__)
710
711     CGLContextObj ctx = CGLGetCurrentContext();
712     if (ctx == NULL) {
713         return false;
714     }
715
716     CGSConnectionID cid;
717     CGSWindowID wid;
718     CGSSurfaceID sid;
719
720     if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
721         return false;
722     }
723
724     CGRect rect;
725
726     if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
727         return false;
728     }
729
730     *width = rect.size.width;
731     *height = rect.size.height;
732
733 #else
734
735     Display *display;
736     Drawable drawable;
737     Window root;
738     int x, y;
739     unsigned int w, h, bw, depth;
740
741     display = glXGetCurrentDisplay();
742     if (!display) {
743         return false;
744     }
745
746     drawable = glXGetCurrentDrawable();
747     if (drawable == None) {
748         return false;
749     }
750
751     if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
752         return false;
753     }
754
755     *width = w;
756     *height = h;
757
758 #endif
759
760     return true;
761 }
762
763
764 static const GLenum texture_bindings[][2] = {
765     {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
766     {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
767     {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
768     {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
769     {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
770 };
771
772
773 static bool
774 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
775 {
776
777     for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
778         target  = texture_bindings[i][0];
779
780         GLenum binding = texture_bindings[i][1];
781
782         while (glGetError() != GL_NO_ERROR)
783             ;
784
785         glGetIntegerv(binding, &bound_texture);
786         glBindTexture(target, texture);
787
788         if (glGetError() == GL_NO_ERROR) {
789             return true;
790         }
791
792         glBindTexture(target, bound_texture);
793     }
794
795     target = GL_NONE;
796
797     return false;
798 }
799
800
801 static bool
802 getTextureLevelSize(GLint texture, GLint level, GLint *width, GLint *height)
803 {
804     *width = 0;
805     *height = 0;
806
807     GLenum target;
808     GLint bound_texture = 0;
809     if (!bindTexture(texture, target, bound_texture)) {
810         return false;
811     }
812
813     glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, width);
814     glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, height);
815
816     glBindTexture(target, bound_texture);
817
818     return *width > 0 && *height > 0;
819 }
820
821
822 static bool
823 getRenderbufferSize(GLint renderbuffer, GLint *width, GLint *height)
824 {
825     GLint bound_renderbuffer = 0;
826     glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
827     glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
828
829     *width = 0;
830     *height = 0;
831     glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, width);
832     glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, height);
833
834     glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
835     
836     return *width > 0 && *height > 0;
837 }
838
839
840 static bool
841 getFramebufferAttachmentSize(GLenum target, GLenum attachment, GLint *width, GLint *height)
842 {
843     GLint object_type = GL_NONE;
844     glGetFramebufferAttachmentParameteriv(target, attachment,
845                                           GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
846                                           &object_type);
847     if (object_type == GL_NONE) {
848         return false;
849     }
850
851     GLint object_name = 0;
852     glGetFramebufferAttachmentParameteriv(target, attachment,
853                                           GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
854                                           &object_name);
855     if (object_name == 0) {
856         return false;
857     }
858
859     if (object_type == GL_RENDERBUFFER) {
860         return getRenderbufferSize(object_name, width, height);
861     } else if (object_type == GL_TEXTURE) {
862         GLint texture_level = 0;
863         glGetFramebufferAttachmentParameteriv(target, attachment,
864                                               GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
865                                               &texture_level);
866         return getTextureLevelSize(object_name, texture_level, width, height);
867     } else {
868         std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
869         return false;
870     }
871 }
872
873
874 image::Image *
875 getDrawBufferImage(GLenum format) {
876     GLint channels = __gl_format_channels(format);
877     if (channels > 4) {
878         return NULL;
879     }
880
881     GLint draw_framebuffer = 0;
882     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
883
884     GLint draw_buffer = GL_NONE;
885     GLint width, height;
886     if (draw_framebuffer) {
887         glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
888         if (draw_buffer == GL_NONE) {
889             return NULL;
890         }
891
892         if (!getFramebufferAttachmentSize(GL_DRAW_FRAMEBUFFER, draw_buffer, &width, &height)) {
893             return NULL;
894         }
895     } else {
896         glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
897         if (draw_buffer == GL_NONE) {
898             return NULL;
899         }
900
901         if (!getDrawableBounds(&width, &height)) {
902             return NULL;
903         }
904     }
905
906     image::Image *image = new image::Image(width, height, channels, true);
907     if (!image) {
908         return NULL;
909     }
910
911     while (glGetError() != GL_NO_ERROR) {}
912
913     GLint read_framebuffer = 0;
914     glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
915     glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
916
917     GLint read_buffer = 0;
918     glGetIntegerv(GL_READ_BUFFER, &read_buffer);
919     glReadBuffer(draw_buffer);
920
921     // TODO: reset imaging state too
922     resetPixelPackState();
923
924     glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
925
926     restorePixelPackState();
927     glReadBuffer(read_buffer);
928     glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
929
930     GLenum error = glGetError();
931     if (error != GL_NO_ERROR) {
932         do {
933             std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
934             error = glGetError();
935         } while(error != GL_NO_ERROR);
936         delete image;
937         return NULL;
938     }
939      
940     return image;
941 }
942
943
944 /**
945  * Dump the image of the currently bound read buffer.
946  */
947 static inline void
948 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format)
949 {
950     GLint channels = __gl_format_channels(format);
951
952     json.beginObject();
953
954     // Tell the GUI this is no ordinary object, but an image
955     json.writeStringMember("__class__", "image");
956
957     json.writeNumberMember("__width__", width);
958     json.writeNumberMember("__height__", height);
959     json.writeNumberMember("__depth__", 1);
960
961     // Hardcoded for now, but we could chose types more adequate to the
962     // texture internal format
963     json.writeStringMember("__type__", "uint8");
964     json.writeBoolMember("__normalized__", true);
965     json.writeNumberMember("__channels__", channels);
966
967     GLubyte *pixels = new GLubyte[width*height*channels];
968
969     // TODO: reset imaging state too
970     resetPixelPackState();
971
972     glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
973
974     restorePixelPackState();
975
976     json.beginMember("__data__");
977     char *pngBuffer;
978     int pngBufferSize;
979     image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
980     //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
981     //          <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
982     json.writeBase64(pngBuffer, pngBufferSize);
983     free(pngBuffer);
984     json.endMember(); // __data__
985
986     delete [] pixels;
987     json.endObject();
988 }
989
990
991 static inline GLuint
992 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
993                        GLint colorRb, GLint depthRb, GLint stencilRb,
994                        GLuint *rbs, GLint *numRbs)
995 {
996     GLuint fbo;
997     GLint format;
998     GLint w, h;
999
1000     *numRbs = 0;
1001
1002     glGenFramebuffers(1, &fbo);
1003     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1004
1005     glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1006     glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1007                                  GL_RENDERBUFFER_WIDTH, &w);
1008     glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1009                                  GL_RENDERBUFFER_HEIGHT, &h);
1010     glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1011                                  GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1012
1013     glGenRenderbuffers(1, &rbs[*numRbs]);
1014     glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1015     glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1016     glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
1017                               GL_RENDERBUFFER, rbs[*numRbs]);
1018
1019     glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1020     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1021     glDrawBuffer(drawbuffer);
1022     glReadBuffer(drawbuffer);
1023     glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1024                       GL_COLOR_BUFFER_BIT, GL_NEAREST);
1025     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1026     ++*numRbs;
1027
1028     if (stencilRb == depthRb && stencilRb) {
1029         //combined depth and stencil buffer
1030         glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1031         glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1032                                      GL_RENDERBUFFER_WIDTH, &w);
1033         glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1034                                      GL_RENDERBUFFER_HEIGHT, &h);
1035         glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1036                                      GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1037
1038         glGenRenderbuffers(1, &rbs[*numRbs]);
1039         glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1040         glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1041         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
1042                                   GL_RENDERBUFFER, rbs[*numRbs]);
1043         glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1044         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1045         glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1046                           GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1047         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1048         ++*numRbs;
1049     } else {
1050         if (depthRb) {
1051             glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1052             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1053                                          GL_RENDERBUFFER_WIDTH, &w);
1054             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1055                                          GL_RENDERBUFFER_HEIGHT, &h);
1056             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1057                                          GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1058
1059             glGenRenderbuffers(1, &rbs[*numRbs]);
1060             glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1061             glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1062             glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1063                                       GL_DEPTH_ATTACHMENT,
1064                                       GL_RENDERBUFFER, rbs[*numRbs]);
1065             glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1066             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1067             glDrawBuffer(GL_DEPTH_ATTACHMENT);
1068             glReadBuffer(GL_DEPTH_ATTACHMENT);
1069             glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1070                               GL_DEPTH_BUFFER_BIT, GL_NEAREST);
1071             ++*numRbs;
1072         }
1073         if (stencilRb) {
1074             glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1075             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1076                                          GL_RENDERBUFFER_WIDTH, &w);
1077             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1078                                          GL_RENDERBUFFER_HEIGHT, &h);
1079             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1080                                      GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1081
1082             glGenRenderbuffers(1, &rbs[*numRbs]);
1083             glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1084             glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1085             glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1086                                       GL_STENCIL_ATTACHMENT,
1087                                       GL_RENDERBUFFER, rbs[*numRbs]);
1088             glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1089             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1090             glDrawBuffer(GL_STENCIL_ATTACHMENT);
1091             glReadBuffer(GL_STENCIL_ATTACHMENT);
1092             glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1093                               GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1094             ++*numRbs;
1095         }
1096     }
1097
1098     return fbo;
1099 }
1100
1101
1102 /**
1103  * Dump images of current draw drawable/window.
1104  */
1105 static void
1106 dumpDrawableImages(JSONWriter &json)
1107 {
1108     GLint width, height;
1109
1110     if (!getDrawableBounds(&width, &height)) {
1111         return;
1112     }
1113
1114     GLint draw_buffer = GL_NONE;
1115     glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1116     glReadBuffer(draw_buffer);
1117
1118     if (draw_buffer != GL_NONE) {
1119         GLint read_buffer = GL_NONE;
1120         glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1121
1122         GLint alpha_bits = 0;
1123         glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1124         GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1125         json.beginMember(enumToString(draw_buffer));
1126         dumpReadBufferImage(json, width, height, format);
1127         json.endMember();
1128
1129         glReadBuffer(read_buffer);
1130     }
1131
1132     GLint depth_bits = 0;
1133     glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1134     if (depth_bits) {
1135         json.beginMember("GL_DEPTH_COMPONENT");
1136         dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1137         json.endMember();
1138     }
1139
1140     GLint stencil_bits = 0;
1141     glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1142     if (stencil_bits) {
1143         json.beginMember("GL_STENCIL_INDEX");
1144         dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1145         json.endMember();
1146     }
1147 }
1148 /**
1149  * Dump the specified framebuffer attachment.
1150  *
1151  * In the case of a color attachment, it assumes it is already bound for read.
1152  */
1153 static void
1154 dumpFramebufferAttachment(JSONWriter &json, GLenum target, GLenum attachment, GLenum format)
1155 {
1156     GLint width = 0, height = 0;
1157     if (!getFramebufferAttachmentSize(target, attachment, &width, &height)) {
1158         return;
1159     }
1160
1161     json.beginMember(enumToString(attachment));
1162     dumpReadBufferImage(json, width, height, format);
1163     json.endMember();
1164 }
1165
1166
1167 static void
1168 dumpFramebufferAttachments(JSONWriter &json, GLenum target)
1169 {
1170     GLint read_framebuffer = 0;
1171     glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1172
1173     GLint read_buffer = GL_NONE;
1174     glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1175
1176     GLint max_draw_buffers = 1;
1177     glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1178     GLint max_color_attachments = 0;
1179     glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1180
1181     for (GLint i = 0; i < max_draw_buffers; ++i) {
1182         GLint draw_buffer = GL_NONE;
1183         glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1184         if (draw_buffer != GL_NONE) {
1185             glReadBuffer(draw_buffer);
1186             GLint attachment;
1187             if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1188                 attachment = draw_buffer;
1189             } else {
1190                 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1191                 attachment = GL_COLOR_ATTACHMENT0;
1192             }
1193             GLint alpha_size = 0;
1194             glGetFramebufferAttachmentParameteriv(target, attachment,
1195                                                   GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1196                                                   &alpha_size);
1197             GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1198             dumpFramebufferAttachment(json, target, attachment, format);
1199         }
1200     }
1201
1202     glReadBuffer(read_buffer);
1203
1204     dumpFramebufferAttachment(json, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1205     dumpFramebufferAttachment(json, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1206
1207     glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1208 }
1209
1210
1211 static void
1212 dumpFramebuffer(JSONWriter &json)
1213 {
1214     json.beginMember("framebuffer");
1215     json.beginObject();
1216
1217     GLint boundDrawFbo = 0, boundReadFbo = 0;
1218     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1219     glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1220     if (!boundDrawFbo) {
1221         dumpDrawableImages(json);
1222     } else {
1223         GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1224         GLint draw_buffer0 = GL_NONE;
1225         glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1226         bool multisample = false;
1227
1228         GLint boundRb = 0;
1229         glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1230
1231         GLint object_type;
1232         glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1233         if (object_type == GL_RENDERBUFFER) {
1234             glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1235             glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1236             GLint samples = 0;
1237             glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1238             if (samples) {
1239                 multisample = true;
1240             }
1241         }
1242
1243         glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1244         if (object_type == GL_RENDERBUFFER) {
1245             glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1246             glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1247             GLint samples = 0;
1248             glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1249             if (samples) {
1250                 multisample = true;
1251             }
1252         }
1253
1254         glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1255         if (object_type == GL_RENDERBUFFER) {
1256             glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1257             glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1258             GLint samples = 0;
1259             glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1260             if (samples) {
1261                 multisample = true;
1262             }
1263         }
1264
1265         glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1266
1267         GLuint rbs[3];
1268         GLint numRbs = 0;
1269         GLuint fboCopy = 0;
1270
1271         if (multisample) {
1272             // glReadPixels doesnt support multisampled buffers so we need
1273             // to blit the fbo to a temporary one
1274             fboCopy = downsampledFramebuffer(boundDrawFbo, draw_buffer0,
1275                                              colorRb, depthRb, stencilRb,
1276                                              rbs, &numRbs);
1277         }
1278
1279         dumpFramebufferAttachments(json, GL_DRAW_FRAMEBUFFER);
1280
1281         if (multisample) {
1282             glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1283             glDeleteRenderbuffers(numRbs, rbs);
1284             glDeleteFramebuffers(1, &fboCopy);
1285         }
1286
1287         glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1288         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1289     }
1290
1291     json.endObject();
1292     json.endMember(); // framebuffer
1293 }
1294
1295
1296 static const GLenum bindings[] = {
1297     GL_DRAW_BUFFER,
1298     GL_READ_BUFFER,
1299     GL_PIXEL_PACK_BUFFER_BINDING,
1300     GL_PIXEL_UNPACK_BUFFER_BINDING,
1301     GL_TEXTURE_BINDING_1D,
1302     GL_TEXTURE_BINDING_2D,
1303     GL_TEXTURE_BINDING_3D,
1304     GL_TEXTURE_BINDING_RECTANGLE,
1305     GL_TEXTURE_BINDING_CUBE_MAP,
1306     GL_DRAW_FRAMEBUFFER_BINDING,
1307     GL_READ_FRAMEBUFFER_BINDING,
1308     GL_RENDERBUFFER_BINDING,
1309     GL_DRAW_BUFFER0,
1310     GL_DRAW_BUFFER1,
1311     GL_DRAW_BUFFER2,
1312     GL_DRAW_BUFFER3,
1313     GL_DRAW_BUFFER4,
1314     GL_DRAW_BUFFER5,
1315     GL_DRAW_BUFFER6,
1316     GL_DRAW_BUFFER7,
1317 };
1318
1319
1320 #define NUM_BINDINGS sizeof(bindings)/sizeof(bindings[0])
1321
1322
1323 void dumpCurrentContext(std::ostream &os)
1324 {
1325     JSONWriter json(os);
1326
1327 #ifndef NDEBUG
1328     GLint old_bindings[NUM_BINDINGS];
1329     for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1330         old_bindings[i] = 0;
1331         glGetIntegerv(bindings[i], &old_bindings[i]);
1332     }
1333 #endif
1334
1335     dumpParameters(json);
1336     dumpShadersUniforms(json);
1337     dumpTextures(json);
1338     dumpFramebuffer(json);
1339
1340 #ifndef NDEBUG
1341     for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1342         GLint new_binding = 0;
1343         glGetIntegerv(bindings[i], &new_binding);
1344         if (new_binding != old_bindings[i]) {
1345             std::cerr << "warning: " << enumToString(bindings[i]) << " was clobbered\n";
1346         }
1347     }
1348 #endif
1349
1350 }
1351
1352
1353 } /* namespace glstate */