]> git.cworth.org Git - apitrace/blob - glstate.cpp
format.py is not executable.
[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 #include <iostream>
29 #include <algorithm>
30
31 #include "image.hpp"
32 #include "json.hpp"
33 #include "glproc.hpp"
34 #include "glsize.hpp"
35 #include "glstate.hpp"
36
37
38 #ifdef __APPLE__
39
40 #include <Carbon/Carbon.h>
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
47
48 #ifdef __cplusplus
49 }
50 #endif
51
52 #endif /* __APPLE__ */
53
54
55 namespace glstate {
56
57
58 static inline void
59 resetPixelPackState(void) {
60     glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
61     glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
62     glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
63     glPixelStorei(GL_PACK_ROW_LENGTH, 0);
64     glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
65     glPixelStorei(GL_PACK_SKIP_ROWS, 0);
66     glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
67     glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
68     glPixelStorei(GL_PACK_ALIGNMENT, 1);
69     glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
70 }
71
72
73 static inline void
74 restorePixelPackState(void) {
75     glPopClientAttrib();
76 }
77
78
79 static void
80 dumpShader(JSONWriter &json, GLuint shader)
81 {
82     if (!shader) {
83         return;
84     }
85
86     GLint shader_type = 0;
87     glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
88     if (!shader_type) {
89         return;
90     }
91
92     GLint source_length = 0;
93     glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
94     if (!source_length) {
95         return;
96     }
97
98     GLchar *source = new GLchar[source_length];
99     GLsizei length = 0;
100     source[0] = 0;
101     glGetShaderSource(shader, source_length, &length, source);
102
103     json.beginMember(enumToString(shader_type));
104     json.writeString(source);
105     json.endMember();
106
107     delete [] source;
108 }
109
110
111 static void
112 dumpShaderObj(JSONWriter &json, GLhandleARB shaderObj)
113 {
114     if (!shaderObj) {
115         return;
116     }
117
118     GLint shader_type = 0;
119     glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &shader_type);
120     if (!shader_type) {
121         return;
122     }
123
124     GLint source_length = 0;
125     glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
126     if (!source_length) {
127         return;
128     }
129
130     GLcharARB *source = new GLcharARB[source_length];
131     GLsizei length = 0;
132     source[0] = 0;
133     glGetShaderSource(shaderObj, source_length, &length, source);
134
135     json.beginMember(enumToString(shader_type));
136     json.writeString(source);
137     json.endMember();
138
139     delete [] source;
140 }
141
142
143 static inline void
144 dumpCurrentProgram(JSONWriter &json)
145 {
146     GLint program = 0;
147     glGetIntegerv(GL_CURRENT_PROGRAM, &program);
148     if (!program) {
149         return;
150     }
151
152     GLint attached_shaders = 0;
153     glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
154     if (!attached_shaders) {
155         return;
156     }
157
158     GLuint *shaders = new GLuint[attached_shaders];
159     GLsizei count = 0;
160     glGetAttachedShaders(program, attached_shaders, &count, shaders);
161     for (GLsizei i = 0; i < count; ++ i) {
162        dumpShader(json, shaders[i]);
163     }
164     delete [] shaders;
165 }
166
167
168 static inline void
169 dumpCurrentProgramObj(JSONWriter &json)
170 {
171     GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
172     if (!programObj) {
173         return;
174     }
175
176     GLint attached_shaders = 0;
177     glGetProgramivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
178     if (!attached_shaders) {
179         return;
180     }
181
182     GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
183     GLsizei count = 0;
184     glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
185     for (GLsizei i = 0; i < count; ++ i) {
186        dumpShaderObj(json, shaderObjs[i]);
187     }
188     delete [] shaderObjs;
189 }
190
191
192 static inline void
193 dumpArbProgram(JSONWriter &json, GLenum target)
194 {
195     if (!glIsEnabled(target)) {
196         return;
197     }
198
199     GLint program_length = 0;
200     glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
201     if (!program_length) {
202         return;
203     }
204
205     GLchar *source = new GLchar[program_length + 1];
206     source[0] = 0;
207     glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
208     source[program_length] = 0;
209
210     json.beginMember(enumToString(target));
211     json.writeString(source);
212     json.endMember();
213
214     delete [] source;
215 }
216
217
218 static inline void
219 dumpShaders(JSONWriter &json)
220 {
221     json.beginMember("shaders");
222     json.beginObject();
223     dumpCurrentProgram(json);
224     dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
225     dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
226     json.endObject();
227     json.endMember(); //shaders
228 }
229
230
231 static inline void
232 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
233 {
234     GLint width, height = 1, depth = 1;
235
236     width = 0;
237     glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
238
239     if (target != GL_TEXTURE_1D) {
240         height = 0;
241         glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
242         if (target == GL_TEXTURE_3D) {
243             depth = 0;
244             glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
245         }
246     }
247
248     if (width <= 0 || height <= 0 || depth <= 0) {
249         return;
250     } else {
251         char label[512];
252
253         GLint active_texture = GL_TEXTURE0;
254         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
255         snprintf(label, sizeof label, "%s, %s, level = %i", enumToString(active_texture), enumToString(target), level);
256
257         json.beginMember(label);
258
259         json.beginObject();
260
261         // Tell the GUI this is no ordinary object, but an image
262         json.writeStringMember("__class__", "image");
263
264         json.writeNumberMember("__width__", width);
265         json.writeNumberMember("__height__", height);
266         json.writeNumberMember("__depth__", depth);
267
268         // Hardcoded for now, but we could chose types more adequate to the
269         // texture internal format
270         json.writeStringMember("__type__", "uint8");
271         json.writeBoolMember("__normalized__", true);
272         json.writeNumberMember("__channels__", 4);
273
274         GLubyte *pixels = new GLubyte[depth*width*height*4];
275
276         resetPixelPackState();
277
278         glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
279
280         restorePixelPackState();
281
282         json.beginMember("__data__");
283         char *pngBuffer;
284         int pngBufferSize;
285         Image::writePixelsToBuffer(pixels, width, height, 4, false, &pngBuffer, &pngBufferSize);
286         json.writeBase64(pngBuffer, pngBufferSize);
287         free(pngBuffer);
288         json.endMember(); // __data__
289
290         delete [] pixels;
291         json.endObject();
292     }
293 }
294
295
296 static inline void
297 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
298 {
299     GLint texture_binding = 0;
300     glGetIntegerv(binding, &texture_binding);
301     if (!glIsEnabled(target) && !texture_binding) {
302         return;
303     }
304
305     GLint level = 0;
306     do {
307         GLint width = 0, height = 0;
308         glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
309         glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
310         if (!width || !height) {
311             break;
312         }
313
314         if (target == GL_TEXTURE_CUBE_MAP) {
315             for (int face = 0; face < 6; ++face) {
316                 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
317             }
318         } else {
319             dumpTextureImage(json, target, level);
320         }
321
322         ++level;
323     } while(true);
324 }
325
326
327 static inline void
328 dumpTextures(JSONWriter &json)
329 {
330     json.beginMember("textures");
331     json.beginObject();
332     GLint active_texture = GL_TEXTURE0;
333     glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
334     GLint max_texture_coords = 0;
335     glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
336     GLint max_combined_texture_image_units = 0;
337     glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
338     GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
339     for (GLint unit = 0; unit < max_units; ++unit) {
340         GLenum texture = GL_TEXTURE0 + unit;
341         glActiveTexture(texture);
342         dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
343         dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
344         dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
345         dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
346         dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
347     }
348     glActiveTexture(active_texture);
349     json.endObject();
350     json.endMember(); // textures
351 }
352
353
354 static bool
355 getDrawableBounds(GLint *width, GLint *height) {
356 #if defined(_WIN32)
357
358     HDC hDC = wglGetCurrentDC();
359     if (!hDC) {
360         return false;
361     }
362
363     HWND hWnd = WindowFromDC(hDC);
364     RECT rect;
365
366     if (!GetClientRect(hWnd, &rect)) {
367        return false;
368     }
369
370     *width  = rect.right  - rect.left;
371     *height = rect.bottom - rect.top;
372
373 #elif defined(__APPLE__)
374
375     CGLContextObj ctx = CGLGetCurrentContext();
376     if (ctx == NULL) {
377         return false;
378     }
379
380     CGSConnectionID cid;
381     CGSWindowID wid;
382     CGSSurfaceID sid;
383
384     if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
385         return false;
386     }
387
388     CGRect rect;
389
390     if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
391         return false;
392     }
393
394     *width = rect.size.width;
395     *height = rect.size.height;
396
397 #else
398
399     Display *display;
400     Drawable drawable;
401     Window root;
402     int x, y;
403     unsigned int w, h, bw, depth;
404
405     display = glXGetCurrentDisplay();
406     if (!display) {
407         return false;
408     }
409
410     drawable = glXGetCurrentDrawable();
411     if (drawable == None) {
412         return false;
413     }
414
415     if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
416         return false;
417     }
418
419     *width = w;
420     *height = h;
421
422 #endif
423
424     return true;
425 }
426
427
428 Image::Image *
429 getDrawBufferImage(GLenum format) {
430     GLint channels = __gl_format_channels(format);
431     if (channels > 4) {
432         return NULL;
433     }
434
435     GLint width, height;
436     if (!getDrawableBounds(&width, &height)) {
437         return NULL;
438     }
439
440     Image::Image *image = new Image::Image(width, height, channels, true);
441     if (!image) {
442         return NULL;
443     }
444
445     GLint draw_buffer = GL_NONE;
446     GLint read_buffer = GL_NONE;
447     glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
448     glGetIntegerv(GL_READ_BUFFER, &read_buffer);
449     glReadBuffer(draw_buffer);
450
451     resetPixelPackState();
452
453     glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
454
455     restorePixelPackState();
456     glReadBuffer(read_buffer);
457
458     return image;
459 }
460
461
462 static inline void
463 dumpDrawBufferImage(JSONWriter &json, GLenum format)
464 {
465     GLint channels = __gl_format_channels(format);
466
467     GLint width, height;
468
469     if (!getDrawableBounds(&width, &height)) {
470         json.writeNull();
471     } else {
472         json.beginObject();
473
474         // Tell the GUI this is no ordinary object, but an image
475         json.writeStringMember("__class__", "image");
476
477         json.writeNumberMember("__width__", width);
478         json.writeNumberMember("__height__", height);
479         json.writeNumberMember("__depth__", 1);
480
481         // Hardcoded for now, but we could chose types more adequate to the
482         // texture internal format
483         json.writeStringMember("__type__", "uint8");
484         json.writeBoolMember("__normalized__", true);
485         json.writeNumberMember("__channels__", channels);
486
487         GLubyte *pixels = new GLubyte[width*height*channels];
488
489         GLint draw_buffer = GL_NONE;
490         GLint read_buffer = GL_NONE;
491         glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
492         glGetIntegerv(GL_READ_BUFFER, &read_buffer);
493         glReadBuffer(draw_buffer);
494
495         resetPixelPackState();
496
497         glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
498
499         restorePixelPackState();
500         glReadBuffer(read_buffer);
501
502         json.beginMember("__data__");
503         char *pngBuffer;
504         int pngBufferSize;
505         Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
506         //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
507         //          <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
508         json.writeBase64(pngBuffer, pngBufferSize);
509         free(pngBuffer);
510         json.endMember(); // __data__
511
512         delete [] pixels;
513         json.endObject();
514     }
515 }
516
517
518 static inline GLuint
519 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
520                        GLint colorRb, GLint depthRb, GLint stencilRb,
521                        GLuint *rbs, GLint *numRbs)
522 {
523     GLuint fbo;
524     GLint format;
525     GLint w, h;
526
527     *numRbs = 0;
528
529     glGenFramebuffers(1, &fbo);
530     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
531
532     glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
533     glGetRenderbufferParameteriv(GL_RENDERBUFFER,
534                                  GL_RENDERBUFFER_WIDTH, &w);
535     glGetRenderbufferParameteriv(GL_RENDERBUFFER,
536                                  GL_RENDERBUFFER_HEIGHT, &h);
537     glGetRenderbufferParameteriv(GL_RENDERBUFFER,
538                                  GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
539
540     glGenRenderbuffers(1, &rbs[*numRbs]);
541     glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
542     glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
543     glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
544                               GL_RENDERBUFFER, rbs[*numRbs]);
545
546     glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
547     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
548     glDrawBuffer(drawbuffer);
549     glReadBuffer(drawbuffer);
550     glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
551                       GL_COLOR_BUFFER_BIT, GL_NEAREST);
552     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
553     ++*numRbs;
554
555     if (stencilRb == depthRb && stencilRb) {
556         //combined depth and stencil buffer
557         glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
558         glGetRenderbufferParameteriv(GL_RENDERBUFFER,
559                                      GL_RENDERBUFFER_WIDTH, &w);
560         glGetRenderbufferParameteriv(GL_RENDERBUFFER,
561                                      GL_RENDERBUFFER_HEIGHT, &h);
562         glGetRenderbufferParameteriv(GL_RENDERBUFFER,
563                                      GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
564
565         glGenRenderbuffers(1, &rbs[*numRbs]);
566         glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
567         glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
568         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
569                                   GL_RENDERBUFFER, rbs[*numRbs]);
570         glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
571         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
572         glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
573                           GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
574         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
575         ++*numRbs;
576     } else {
577         if (depthRb) {
578             glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
579             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
580                                          GL_RENDERBUFFER_WIDTH, &w);
581             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
582                                          GL_RENDERBUFFER_HEIGHT, &h);
583             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
584                                          GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
585
586             glGenRenderbuffers(1, &rbs[*numRbs]);
587             glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
588             glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
589             glFramebufferRenderbuffer(GL_FRAMEBUFFER,
590                                       GL_DEPTH_ATTACHMENT,
591                                       GL_RENDERBUFFER, rbs[*numRbs]);
592             glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
593             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
594             glDrawBuffer(GL_DEPTH_ATTACHMENT);
595             glReadBuffer(GL_DEPTH_ATTACHMENT);
596             glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
597                               GL_DEPTH_BUFFER_BIT, GL_NEAREST);
598             ++*numRbs;
599         }
600         if (stencilRb) {
601             glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
602             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
603                                          GL_RENDERBUFFER_WIDTH, &w);
604             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
605                                          GL_RENDERBUFFER_HEIGHT, &h);
606             glGetRenderbufferParameteriv(GL_RENDERBUFFER,
607                                      GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
608
609             glGenRenderbuffers(1, &rbs[*numRbs]);
610             glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
611             glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
612             glFramebufferRenderbuffer(GL_FRAMEBUFFER,
613                                       GL_STENCIL_ATTACHMENT,
614                                       GL_RENDERBUFFER, rbs[*numRbs]);
615             glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
616             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
617             glDrawBuffer(GL_STENCIL_ATTACHMENT);
618             glReadBuffer(GL_STENCIL_ATTACHMENT);
619             glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
620                               GL_STENCIL_BUFFER_BIT, GL_NEAREST);
621             ++*numRbs;
622         }
623     }
624
625     return fbo;
626 }
627
628
629 static void
630 dumpDrawBuffers(JSONWriter &json, GLboolean dumpDepth, GLboolean dumpStencil)
631 {
632     json.beginMember("GL_RGBA");
633     dumpDrawBufferImage(json, GL_RGBA);
634     json.endMember();
635
636     if (dumpDepth) {
637         json.beginMember("GL_DEPTH_COMPONENT");
638         dumpDrawBufferImage(json, GL_DEPTH_COMPONENT);
639         json.endMember();
640     }
641
642     if (dumpStencil) {
643         json.beginMember("GL_STENCIL_INDEX");
644         dumpDrawBufferImage(json, GL_STENCIL_INDEX);
645         json.endMember();
646     }
647 }
648
649
650 static void
651 dumpFramebuffer(JSONWriter &json)
652 {
653     json.beginMember("framebuffer");
654     json.beginObject();
655
656     GLint boundDrawFbo = 0, boundReadFbo = 0;
657     glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
658     glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
659     if (!boundDrawFbo) {
660         GLint depth_bits = 0;
661         glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
662         GLint stencil_bits = 0;
663         glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
664         dumpDrawBuffers(json, depth_bits, stencil_bits);
665     } else {
666         GLint colorRb, stencilRb, depthRb;
667         GLint boundRb;
668         glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
669         GLint drawbuffer = GL_NONE;
670         glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
671
672         glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
673                                               drawbuffer,
674                                               GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
675                                               &colorRb);
676         glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
677                                               GL_DEPTH_ATTACHMENT,
678                                               GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
679                                               &depthRb);
680         glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
681                                               GL_STENCIL_ATTACHMENT,
682                                               GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
683                                               &stencilRb);
684
685         GLint colorSamples, depthSamples, stencilSamples;
686         GLuint rbs[3];
687         GLint numRbs = 0;
688         GLuint fboCopy = 0;
689         glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
690         glGetRenderbufferParameteriv(GL_RENDERBUFFER,
691                                      GL_RENDERBUFFER_SAMPLES, &colorSamples);
692         glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
693         glGetRenderbufferParameteriv(GL_RENDERBUFFER,
694                                      GL_RENDERBUFFER_SAMPLES, &depthSamples);
695         glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
696         glGetRenderbufferParameteriv(GL_RENDERBUFFER,
697                                      GL_RENDERBUFFER_SAMPLES, &stencilSamples);
698         glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
699
700         if (colorSamples || depthSamples || stencilSamples) {
701             //glReadPixels doesnt support multisampled buffers so we need
702             // to blit the fbo to a temporary one
703             fboCopy = downsampledFramebuffer(boundDrawFbo, drawbuffer,
704                                              colorRb, depthRb, stencilRb,
705                                              rbs, &numRbs);
706         }
707         glDrawBuffer(drawbuffer);
708         glReadBuffer(drawbuffer);
709
710         dumpDrawBuffers(json, depthRb, stencilRb);
711
712         if (fboCopy) {
713             glBindFramebuffer(GL_FRAMEBUFFER, boundDrawFbo);
714             glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
715             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
716             glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
717             glDeleteRenderbuffers(numRbs, rbs);
718             glDeleteFramebuffers(1, &fboCopy);
719         }
720     }
721
722     json.endObject();
723     json.endMember(); // framebuffer
724 }
725
726
727 void dumpCurrentContext(std::ostream &os)
728 {
729     JSONWriter json(os);
730     dumpParameters(json);
731     dumpShaders(json);
732     dumpTextures(json);
733     dumpFramebuffer(json);
734 }
735
736
737 } /* namespace glstate */