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