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