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 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
389 GLint bound_renderbuffer = 0;
390 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
391 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
393 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
394 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
397 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
399 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
406 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
408 GLint object_type = GL_NONE;
409 glGetFramebufferAttachmentParameteriv(target, attachment,
410 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
412 if (object_type == GL_NONE) {
416 GLint object_name = 0;
417 glGetFramebufferAttachmentParameteriv(target, attachment,
418 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
420 if (object_name == 0) {
424 if (object_type == GL_RENDERBUFFER) {
425 return getRenderbufferDesc(context, object_name, desc);
426 } else if (object_type == GL_TEXTURE) {
427 GLint texture_level = 0;
428 glGetFramebufferAttachmentParameteriv(target, attachment,
429 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
431 return getTextureLevelDesc(context, object_name, texture_level, desc);
433 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
441 getDrawBufferImage() {
442 GLenum format = GL_RGB;
443 GLint channels = __gl_format_channels(format);
450 GLenum framebuffer_binding;
451 GLenum framebuffer_target;
453 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
454 framebuffer_target = GL_FRAMEBUFFER;
456 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
457 framebuffer_target = GL_DRAW_FRAMEBUFFER;
460 GLint draw_framebuffer = 0;
461 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
463 GLint draw_buffer = GL_NONE;
465 if (draw_framebuffer) {
466 if (context.ARB_draw_buffers) {
467 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
468 if (draw_buffer == GL_NONE) {
473 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
478 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
479 if (draw_buffer == GL_NONE) {
484 if (!getDrawableBounds(&desc.width, &desc.height)) {
491 GLenum type = GL_UNSIGNED_BYTE;
494 if (format == GL_DEPTH_COMPONENT) {
495 type = GL_UNSIGNED_INT;
500 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
505 while (glGetError() != GL_NO_ERROR) {}
507 GLint read_framebuffer = 0;
508 GLint read_buffer = GL_NONE;
510 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
511 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
513 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
514 glReadBuffer(draw_buffer);
517 // TODO: reset imaging state too
518 context.resetPixelPackState();
520 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
522 context.restorePixelPackState();
525 glReadBuffer(read_buffer);
526 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
529 GLenum error = glGetError();
530 if (error != GL_NO_ERROR) {
532 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
533 error = glGetError();
534 } while(error != GL_NO_ERROR);
544 * Dump the image of the currently bound read buffer.
547 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
548 GLint internalFormat = GL_NONE)
550 GLint channels = __gl_format_channels(format);
556 // Tell the GUI this is no ordinary object, but an image
557 json.writeStringMember("__class__", "image");
559 json.writeNumberMember("__width__", width);
560 json.writeNumberMember("__height__", height);
561 json.writeNumberMember("__depth__", 1);
563 json.writeStringMember("__format__", enumToString(internalFormat));
565 // Hardcoded for now, but we could chose types more adequate to the
566 // texture internal format
567 json.writeStringMember("__type__", "uint8");
568 json.writeBoolMember("__normalized__", true);
569 json.writeNumberMember("__channels__", channels);
571 GLenum type = GL_UNSIGNED_BYTE;
574 if (format == GL_DEPTH_COMPONENT) {
575 type = GL_UNSIGNED_INT;
580 GLubyte *pixels = new GLubyte[width*height*channels];
582 // TODO: reset imaging state too
583 context.resetPixelPackState();
585 glReadPixels(0, 0, width, height, format, type, pixels);
587 context.restorePixelPackState();
589 json.beginMember("__data__");
592 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
593 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
594 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
595 json.writeBase64(pngBuffer, pngBufferSize);
597 json.endMember(); // __data__
605 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
606 GLint colorRb, GLint depthRb, GLint stencilRb,
607 GLuint *rbs, GLint *numRbs)
615 glGenFramebuffers(1, &fbo);
616 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
618 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
619 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
620 GL_RENDERBUFFER_WIDTH, &w);
621 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
622 GL_RENDERBUFFER_HEIGHT, &h);
623 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
624 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
626 glGenRenderbuffers(1, &rbs[*numRbs]);
627 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
628 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
629 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
630 GL_RENDERBUFFER, rbs[*numRbs]);
632 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
633 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
634 glDrawBuffer(drawbuffer);
635 glReadBuffer(drawbuffer);
636 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
637 GL_COLOR_BUFFER_BIT, GL_NEAREST);
638 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
641 if (stencilRb == depthRb && stencilRb) {
642 //combined depth and stencil buffer
643 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
644 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
645 GL_RENDERBUFFER_WIDTH, &w);
646 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
647 GL_RENDERBUFFER_HEIGHT, &h);
648 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
649 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
651 glGenRenderbuffers(1, &rbs[*numRbs]);
652 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
653 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
654 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
655 GL_RENDERBUFFER, rbs[*numRbs]);
656 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
657 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
658 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
659 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
660 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
664 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
665 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
666 GL_RENDERBUFFER_WIDTH, &w);
667 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
668 GL_RENDERBUFFER_HEIGHT, &h);
669 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
670 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
672 glGenRenderbuffers(1, &rbs[*numRbs]);
673 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
674 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
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, w, h, 0, 0, w, h,
683 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
687 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
688 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
689 GL_RENDERBUFFER_WIDTH, &w);
690 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
691 GL_RENDERBUFFER_HEIGHT, &h);
692 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
693 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
695 glGenRenderbuffers(1, &rbs[*numRbs]);
696 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
697 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
698 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
699 GL_STENCIL_ATTACHMENT,
700 GL_RENDERBUFFER, rbs[*numRbs]);
701 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
702 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
703 glDrawBuffer(GL_STENCIL_ATTACHMENT);
704 glReadBuffer(GL_STENCIL_ATTACHMENT);
705 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
706 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
716 * Dump images of current draw drawable/window.
719 dumpDrawableImages(JSONWriter &json, Context &context)
723 if (!getDrawableBounds(&width, &height)) {
727 GLint draw_buffer = GL_NONE;
729 draw_buffer = GL_BACK;
731 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
732 glReadBuffer(draw_buffer);
735 if (draw_buffer != GL_NONE) {
736 GLint read_buffer = GL_NONE;
738 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
741 GLint alpha_bits = 0;
743 // XXX: Ignore alpha until we are able to match the traced visual
744 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
746 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
747 json.beginMember(enumToString(draw_buffer));
748 dumpReadBufferImage(json, width, height, format);
752 glReadBuffer(read_buffer);
757 GLint depth_bits = 0;
758 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
760 json.beginMember("GL_DEPTH_COMPONENT");
761 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
765 GLint stencil_bits = 0;
766 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
768 json.beginMember("GL_STENCIL_INDEX");
769 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
777 * Dump the specified framebuffer attachment.
779 * In the case of a color attachment, it assumes it is already bound for read.
782 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
785 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
789 json.beginMember(enumToString(attachment));
790 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
796 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
798 GLint read_framebuffer = 0;
799 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
801 GLint read_buffer = GL_NONE;
802 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
804 GLint max_draw_buffers = 1;
805 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
806 GLint max_color_attachments = 0;
807 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
809 for (GLint i = 0; i < max_draw_buffers; ++i) {
810 GLint draw_buffer = GL_NONE;
811 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
812 if (draw_buffer != GL_NONE) {
813 glReadBuffer(draw_buffer);
815 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
816 attachment = draw_buffer;
818 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
819 attachment = GL_COLOR_ATTACHMENT0;
821 GLint alpha_size = 0;
822 glGetFramebufferAttachmentParameteriv(target, attachment,
823 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
825 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
826 dumpFramebufferAttachment(json, context, target, attachment, format);
830 glReadBuffer(read_buffer);
833 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
834 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
837 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
842 dumpFramebuffer(JSONWriter &json, Context &context)
844 json.beginMember("framebuffer");
847 GLint boundDrawFbo = 0, boundReadFbo = 0;
848 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
849 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
851 dumpDrawableImages(json, context);
852 } else if (context.ES) {
853 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
855 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
856 GLint draw_buffer0 = GL_NONE;
857 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
858 bool multisample = false;
861 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
864 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
865 if (object_type == GL_RENDERBUFFER) {
866 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
867 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
869 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
875 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
876 if (object_type == GL_RENDERBUFFER) {
877 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
878 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
880 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
886 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
887 if (object_type == GL_RENDERBUFFER) {
888 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
889 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
891 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
897 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
904 // glReadPixels doesnt support multisampled buffers so we need
905 // to blit the fbo to a temporary one
906 fboCopy = downsampledFramebuffer(boundDrawFbo, draw_buffer0,
907 colorRb, depthRb, stencilRb,
911 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
914 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
915 glDeleteRenderbuffers(numRbs, rbs);
916 glDeleteFramebuffers(1, &fboCopy);
919 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
920 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
924 json.endMember(); // framebuffer
928 } /* namespace glstate */