1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
36 #include "glstate.hpp"
37 #include "glstate_internal.hpp"
42 #include <Carbon/Carbon.h>
48 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
54 #endif /* __APPLE__ */
57 /* Change thi to one to force interpreting depth buffers as RGBA, which enables
58 * visualizing full dynamic range, until we can transmit HDR images to the GUI */
59 #define DEPTH_AS_RGBA 0
77 internalFormat(GL_NONE)
82 return width > 0 && height > 0 && depth > 0;
88 getActiveTextureLevelDesc(Context &context, GLenum target, GLint level, ImageDesc &desc)
91 // XXX: OpenGL ES does not support glGetTexLevelParameteriv
95 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &desc.internalFormat);
98 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &desc.width);
100 if (target == GL_TEXTURE_1D) {
105 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &desc.height);
106 if (target != GL_TEXTURE_3D) {
110 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &desc.depth);
119 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
122 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
128 GLint active_texture = GL_TEXTURE0;
129 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
130 snprintf(label, sizeof label, "%s, %s, level = %d",
131 enumToString(active_texture), enumToString(target), level);
133 json.beginMember(label);
137 // Tell the GUI this is no ordinary object, but an image
138 json.writeStringMember("__class__", "image");
140 json.writeNumberMember("__width__", desc.width);
141 json.writeNumberMember("__height__", desc.height);
142 json.writeNumberMember("__depth__", desc.depth);
144 json.writeStringMember("__format__", enumToString(desc.internalFormat));
146 // Hardcoded for now, but we could chose types more adequate to the
147 // texture internal format
148 json.writeStringMember("__type__", "uint8");
149 json.writeBoolMember("__normalized__", true);
150 json.writeNumberMember("__channels__", 4);
152 GLubyte *pixels = new GLubyte[desc.depth*desc.width*desc.height*4];
154 context.resetPixelPackState();
156 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
158 context.restorePixelPackState();
160 json.beginMember("__data__");
163 image::writePixelsToBuffer(pixels, desc.width, desc.height, 4, true, &pngBuffer, &pngBufferSize);
164 json.writeBase64(pngBuffer, pngBufferSize);
166 json.endMember(); // __data__
174 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
176 GLint texture_binding = 0;
177 glGetIntegerv(binding, &texture_binding);
178 if (!glIsEnabled(target) && !texture_binding) {
185 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
189 if (target == GL_TEXTURE_CUBE_MAP) {
190 for (int face = 0; face < 6; ++face) {
191 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
194 dumpActiveTextureLevel(json, context, target, level);
203 dumpTextures(JSONWriter &json, Context &context)
205 json.beginMember("textures");
207 GLint active_texture = GL_TEXTURE0;
208 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
209 GLint max_texture_coords = 0;
210 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
211 GLint max_combined_texture_image_units = 0;
212 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
213 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
214 for (GLint unit = 0; unit < max_units; ++unit) {
215 GLenum texture = GL_TEXTURE0 + unit;
216 glActiveTexture(texture);
217 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
218 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
219 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
220 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
221 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
223 glActiveTexture(active_texture);
225 json.endMember(); // textures
230 getDrawableBounds(GLint *width, GLint *height) {
231 #if defined(TRACE_EGL)
233 EGLContext currentContext = eglGetCurrentContext();
234 if (currentContext == EGL_NO_CONTEXT) {
238 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
239 if (currentSurface == EGL_NO_SURFACE) {
243 EGLDisplay currentDisplay = eglGetCurrentDisplay();
244 if (currentDisplay == EGL_NO_DISPLAY) {
248 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
249 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
255 #elif defined(_WIN32)
257 HDC hDC = wglGetCurrentDC();
262 HWND hWnd = WindowFromDC(hDC);
265 if (!GetClientRect(hWnd, &rect)) {
269 *width = rect.right - rect.left;
270 *height = rect.bottom - rect.top;
273 #elif defined(__APPLE__)
275 CGLContextObj ctx = CGLGetCurrentContext();
284 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
290 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
294 *width = rect.size.width;
295 *height = rect.size.height;
298 #elif defined(HAVE_X11)
304 unsigned int w, h, bw, depth;
306 display = glXGetCurrentDisplay();
311 drawable = glXGetCurrentDrawable();
312 if (drawable == None) {
316 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
332 static const GLenum texture_bindings[][2] = {
333 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
334 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
335 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
336 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
337 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
342 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
345 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
346 target = texture_bindings[i][0];
348 GLenum binding = texture_bindings[i][1];
350 while (glGetError() != GL_NO_ERROR)
353 glGetIntegerv(binding, &bound_texture);
354 glBindTexture(target, texture);
356 if (glGetError() == GL_NO_ERROR) {
360 glBindTexture(target, bound_texture);
370 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
373 GLint bound_texture = 0;
374 if (!bindTexture(texture, target, bound_texture)) {
378 getActiveTextureLevelDesc(context, target, level, desc);
380 glBindTexture(target, bound_texture);
387 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
389 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
390 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
393 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
400 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
402 GLint bound_renderbuffer = 0;
403 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
404 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
406 getBoundRenderbufferDesc(context, desc);
408 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
415 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
417 GLint object_type = GL_NONE;
418 glGetFramebufferAttachmentParameteriv(target, attachment,
419 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
421 if (object_type == GL_NONE) {
425 GLint object_name = 0;
426 glGetFramebufferAttachmentParameteriv(target, attachment,
427 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
429 if (object_name == 0) {
433 if (object_type == GL_RENDERBUFFER) {
434 return getRenderbufferDesc(context, object_name, desc);
435 } else if (object_type == GL_TEXTURE) {
436 GLint texture_level = 0;
437 glGetFramebufferAttachmentParameteriv(target, attachment,
438 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
440 return getTextureLevelDesc(context, object_name, texture_level, desc);
442 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
450 getDrawBufferImage() {
451 GLenum format = GL_RGB;
452 GLint channels = __gl_format_channels(format);
459 GLenum framebuffer_binding;
460 GLenum framebuffer_target;
462 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
463 framebuffer_target = GL_FRAMEBUFFER;
465 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
466 framebuffer_target = GL_DRAW_FRAMEBUFFER;
469 GLint draw_framebuffer = 0;
470 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
472 GLint draw_buffer = GL_NONE;
474 if (draw_framebuffer) {
475 if (context.ARB_draw_buffers) {
476 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
477 if (draw_buffer == GL_NONE) {
482 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
487 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
488 if (draw_buffer == GL_NONE) {
493 if (!getDrawableBounds(&desc.width, &desc.height)) {
500 GLenum type = GL_UNSIGNED_BYTE;
503 if (format == GL_DEPTH_COMPONENT) {
504 type = GL_UNSIGNED_INT;
509 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
514 while (glGetError() != GL_NO_ERROR) {}
516 GLint read_framebuffer = 0;
517 GLint read_buffer = GL_NONE;
519 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
520 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
522 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
523 glReadBuffer(draw_buffer);
526 // TODO: reset imaging state too
527 context.resetPixelPackState();
529 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
531 context.restorePixelPackState();
534 glReadBuffer(read_buffer);
535 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
538 GLenum error = glGetError();
539 if (error != GL_NO_ERROR) {
541 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
542 error = glGetError();
543 } while(error != GL_NO_ERROR);
553 * Dump the image of the currently bound read buffer.
556 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
557 GLint internalFormat = GL_NONE)
559 GLint channels = __gl_format_channels(format);
565 // Tell the GUI this is no ordinary object, but an image
566 json.writeStringMember("__class__", "image");
568 json.writeNumberMember("__width__", width);
569 json.writeNumberMember("__height__", height);
570 json.writeNumberMember("__depth__", 1);
572 json.writeStringMember("__format__", enumToString(internalFormat));
574 // Hardcoded for now, but we could chose types more adequate to the
575 // texture internal format
576 json.writeStringMember("__type__", "uint8");
577 json.writeBoolMember("__normalized__", true);
578 json.writeNumberMember("__channels__", channels);
580 GLenum type = GL_UNSIGNED_BYTE;
583 if (format == GL_DEPTH_COMPONENT) {
584 type = GL_UNSIGNED_INT;
589 GLubyte *pixels = new GLubyte[width*height*channels];
591 // TODO: reset imaging state too
592 context.resetPixelPackState();
594 glReadPixels(0, 0, width, height, format, type, pixels);
596 context.restorePixelPackState();
598 json.beginMember("__data__");
601 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
602 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
603 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
604 json.writeBase64(pngBuffer, pngBufferSize);
606 json.endMember(); // __data__
614 downsampledFramebuffer(Context &context,
615 GLuint oldFbo, GLint drawbuffer,
616 GLint colorRb, GLint depthRb, GLint stencilRb,
617 GLuint *rbs, GLint *numRbs)
624 glGenFramebuffers(1, &fbo);
625 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
630 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
631 getBoundRenderbufferDesc(context, desc);
633 glGenRenderbuffers(1, &rbs[*numRbs]);
634 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
635 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
636 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
637 GL_RENDERBUFFER, rbs[*numRbs]);
639 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
640 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
641 glDrawBuffer(drawbuffer);
642 glReadBuffer(drawbuffer);
643 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
644 GL_COLOR_BUFFER_BIT, GL_NEAREST);
645 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
649 if (stencilRb == depthRb && stencilRb) {
650 //combined depth and stencil buffer
652 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
653 getBoundRenderbufferDesc(context, desc);
655 glGenRenderbuffers(1, &rbs[*numRbs]);
656 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
657 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
658 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
659 GL_RENDERBUFFER, rbs[*numRbs]);
660 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
661 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
662 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
663 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
664 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
669 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
670 getBoundRenderbufferDesc(context, desc);
672 glGenRenderbuffers(1, &rbs[*numRbs]);
673 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
674 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
675 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
677 GL_RENDERBUFFER, rbs[*numRbs]);
678 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
679 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
680 glDrawBuffer(GL_DEPTH_ATTACHMENT);
681 glReadBuffer(GL_DEPTH_ATTACHMENT);
682 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
683 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
688 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
689 getBoundRenderbufferDesc(context, desc);
691 glGenRenderbuffers(1, &rbs[*numRbs]);
692 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
693 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
694 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
695 GL_STENCIL_ATTACHMENT,
696 GL_RENDERBUFFER, rbs[*numRbs]);
697 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
698 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
699 glDrawBuffer(GL_STENCIL_ATTACHMENT);
700 glReadBuffer(GL_STENCIL_ATTACHMENT);
701 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
702 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
712 * Dump images of current draw drawable/window.
715 dumpDrawableImages(JSONWriter &json, Context &context)
719 if (!getDrawableBounds(&width, &height)) {
723 GLint draw_buffer = GL_NONE;
725 draw_buffer = GL_BACK;
727 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
728 glReadBuffer(draw_buffer);
731 if (draw_buffer != GL_NONE) {
732 GLint read_buffer = GL_NONE;
734 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
737 GLint alpha_bits = 0;
739 // XXX: Ignore alpha until we are able to match the traced visual
740 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
742 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
743 json.beginMember(enumToString(draw_buffer));
744 dumpReadBufferImage(json, width, height, format);
748 glReadBuffer(read_buffer);
753 GLint depth_bits = 0;
754 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
756 json.beginMember("GL_DEPTH_COMPONENT");
757 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
761 GLint stencil_bits = 0;
762 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
764 json.beginMember("GL_STENCIL_INDEX");
765 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
773 * Dump the specified framebuffer attachment.
775 * In the case of a color attachment, it assumes it is already bound for read.
778 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
781 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
785 json.beginMember(enumToString(attachment));
786 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
792 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
794 GLint read_framebuffer = 0;
795 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
797 GLint read_buffer = GL_NONE;
798 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
800 GLint max_draw_buffers = 1;
801 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
802 GLint max_color_attachments = 0;
803 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
805 for (GLint i = 0; i < max_draw_buffers; ++i) {
806 GLint draw_buffer = GL_NONE;
807 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
808 if (draw_buffer != GL_NONE) {
809 glReadBuffer(draw_buffer);
811 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
812 attachment = draw_buffer;
814 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
815 attachment = GL_COLOR_ATTACHMENT0;
817 GLint alpha_size = 0;
818 glGetFramebufferAttachmentParameteriv(target, attachment,
819 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
821 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
822 dumpFramebufferAttachment(json, context, target, attachment, format);
826 glReadBuffer(read_buffer);
829 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
830 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
833 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
838 dumpFramebuffer(JSONWriter &json, Context &context)
840 json.beginMember("framebuffer");
843 GLint boundDrawFbo = 0, boundReadFbo = 0;
844 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
845 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
847 dumpDrawableImages(json, context);
848 } else if (context.ES) {
849 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
851 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
852 GLint draw_buffer0 = GL_NONE;
853 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
854 bool multisample = false;
857 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
860 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
861 if (object_type == GL_RENDERBUFFER) {
862 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
863 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
865 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
871 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
872 if (object_type == GL_RENDERBUFFER) {
873 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
874 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
876 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
882 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
883 if (object_type == GL_RENDERBUFFER) {
884 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
885 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
887 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
893 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
900 // glReadPixels doesnt support multisampled buffers so we need
901 // to blit the fbo to a temporary one
902 fboCopy = downsampledFramebuffer(context,
903 boundDrawFbo, draw_buffer0,
904 colorRb, depthRb, stencilRb,
908 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
911 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
912 glDeleteRenderbuffers(numRbs, rbs);
913 glDeleteFramebuffers(1, &fboCopy);
916 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
917 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
921 json.endMember(); // framebuffer
925 } /* namespace glstate */