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 * OpenGL ES does not support glGetTexLevelParameteriv, but it is possible to
89 * probe whether a texture has a given size by crafting a dummy glTexSubImage()
93 probeTextureLevelSizeOES(GLenum target, GLint level, const GLint size[3]) {
94 while (glGetError() != GL_NO_ERROR)
97 GLenum internalFormat = GL_RGBA;
98 GLenum type = GL_UNSIGNED_BYTE;
103 case GL_TEXTURE_CUBE_MAP:
104 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
105 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
106 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
107 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
108 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
109 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
110 glTexSubImage2D(target, level, size[0], size[1], 0, 0, internalFormat, type, &dummy);
112 case GL_TEXTURE_3D_OES:
113 glTexSubImage3DOES(target, level, size[0], size[1], size[2], 0, 0, 0, internalFormat, type, &dummy);
119 GLenum error = glGetError();
122 std::cerr << "(" << size[0] << ", " << size[1] << ", " << size[2] << ") = " << enumToString(error) << "\n";
125 if (error == GL_NO_ERROR) {
129 while (glGetError() != GL_NO_ERROR)
137 * Bisect the texture size along an axis.
139 * It is assumed that the texture exists.
142 bisectTextureLevelSizeOES(GLenum target, GLint level, GLint axis, GLint max) {
143 GLint size[3] = {0, 0, 0};
150 GLint test = (min + max) / 2;
157 if (probeTextureLevelSizeOES(target, level, size)) {
167 * Special path to obtain texture size on OpenGL ES, that does not rely on
168 * glGetTexLevelParameteriv
171 getActiveTextureLevelDescOES(Context &context, GLenum target, GLint level, ImageDesc &desc)
173 if (target == GL_TEXTURE_1D) {
174 // OpenGL ES does not support 1D textures
178 const GLint size[3] = {1, 1, 1};
179 if (!probeTextureLevelSizeOES(target, level, size)) {
184 desc.internalFormat = GL_RGBA;
189 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
190 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
191 desc.height = bisectTextureLevelSizeOES(target, level, 1, maxSize);
194 case GL_TEXTURE_CUBE_MAP:
195 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
196 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
197 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
198 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
199 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
200 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
201 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxSize);
202 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
203 desc.height = desc.width;
206 case GL_TEXTURE_3D_OES:
207 glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_OES, &maxSize);
208 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
209 desc.width = bisectTextureLevelSizeOES(target, level, 1, maxSize);
210 desc.depth = bisectTextureLevelSizeOES(target, level, 2, maxSize);
218 << enumToString(target) << " "
220 << desc.width << "x" << desc.height << "x" << desc.depth
229 getActiveTextureLevelDesc(Context &context, GLenum target, GLint level, ImageDesc &desc)
232 return getActiveTextureLevelDescOES(context, target, level, desc);
235 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &desc.internalFormat);
238 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &desc.width);
240 if (target == GL_TEXTURE_1D) {
245 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &desc.height);
246 if (target != GL_TEXTURE_3D) {
250 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &desc.depth);
259 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
262 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
268 GLint active_texture = GL_TEXTURE0;
269 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
270 snprintf(label, sizeof label, "%s, %s, level = %d",
271 enumToString(active_texture), enumToString(target), level);
273 json.beginMember(label);
277 // Tell the GUI this is no ordinary object, but an image
278 json.writeStringMember("__class__", "image");
280 json.writeNumberMember("__width__", desc.width);
281 json.writeNumberMember("__height__", desc.height);
282 json.writeNumberMember("__depth__", desc.depth);
284 json.writeStringMember("__format__", enumToString(desc.internalFormat));
286 // Hardcoded for now, but we could chose types more adequate to the
287 // texture internal format
288 json.writeStringMember("__type__", "uint8");
289 json.writeBoolMember("__normalized__", true);
290 json.writeNumberMember("__channels__", 4);
292 GLubyte *pixels = new GLubyte[desc.depth*desc.width*desc.height*4];
294 context.resetPixelPackState();
296 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
298 context.restorePixelPackState();
300 json.beginMember("__data__");
303 image::writePixelsToBuffer(pixels, desc.width, desc.height, 4, true, &pngBuffer, &pngBufferSize);
304 json.writeBase64(pngBuffer, pngBufferSize);
306 json.endMember(); // __data__
314 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
316 GLint texture_binding = 0;
317 glGetIntegerv(binding, &texture_binding);
318 if (!glIsEnabled(target) && !texture_binding) {
325 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
329 if (target == GL_TEXTURE_CUBE_MAP) {
330 for (int face = 0; face < 6; ++face) {
331 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
334 dumpActiveTextureLevel(json, context, target, level);
343 dumpTextures(JSONWriter &json, Context &context)
345 json.beginMember("textures");
347 GLint active_texture = GL_TEXTURE0;
348 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
349 GLint max_texture_coords = 0;
350 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
351 GLint max_combined_texture_image_units = 0;
352 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
353 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
354 for (GLint unit = 0; unit < max_units; ++unit) {
355 GLenum texture = GL_TEXTURE0 + unit;
356 glActiveTexture(texture);
357 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
358 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
359 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
360 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
361 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
363 glActiveTexture(active_texture);
365 json.endMember(); // textures
370 getDrawableBounds(GLint *width, GLint *height) {
371 #if defined(TRACE_EGL)
373 EGLContext currentContext = eglGetCurrentContext();
374 if (currentContext == EGL_NO_CONTEXT) {
378 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
379 if (currentSurface == EGL_NO_SURFACE) {
383 EGLDisplay currentDisplay = eglGetCurrentDisplay();
384 if (currentDisplay == EGL_NO_DISPLAY) {
388 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
389 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
395 #elif defined(_WIN32)
397 HDC hDC = wglGetCurrentDC();
402 HWND hWnd = WindowFromDC(hDC);
405 if (!GetClientRect(hWnd, &rect)) {
409 *width = rect.right - rect.left;
410 *height = rect.bottom - rect.top;
413 #elif defined(__APPLE__)
415 CGLContextObj ctx = CGLGetCurrentContext();
424 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
430 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
434 *width = rect.size.width;
435 *height = rect.size.height;
438 #elif defined(HAVE_X11)
444 unsigned int w, h, bw, depth;
446 display = glXGetCurrentDisplay();
451 drawable = glXGetCurrentDrawable();
452 if (drawable == None) {
456 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
472 static const GLenum texture_bindings[][2] = {
473 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
474 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
475 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
476 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
477 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
482 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
485 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
486 target = texture_bindings[i][0];
488 GLenum binding = texture_bindings[i][1];
490 while (glGetError() != GL_NO_ERROR)
493 glGetIntegerv(binding, &bound_texture);
494 glBindTexture(target, texture);
496 if (glGetError() == GL_NO_ERROR) {
500 glBindTexture(target, bound_texture);
510 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
513 GLint bound_texture = 0;
514 if (!bindTexture(texture, target, bound_texture)) {
518 getActiveTextureLevelDesc(context, target, level, desc);
520 glBindTexture(target, bound_texture);
527 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
529 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
530 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
533 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
540 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
542 GLint bound_renderbuffer = 0;
543 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
544 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
546 getBoundRenderbufferDesc(context, desc);
548 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
555 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
557 GLint object_type = GL_NONE;
558 glGetFramebufferAttachmentParameteriv(target, attachment,
559 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
561 if (object_type == GL_NONE) {
565 GLint object_name = 0;
566 glGetFramebufferAttachmentParameteriv(target, attachment,
567 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
569 if (object_name == 0) {
573 if (object_type == GL_RENDERBUFFER) {
574 return getRenderbufferDesc(context, object_name, desc);
575 } else if (object_type == GL_TEXTURE) {
576 GLint texture_level = 0;
577 glGetFramebufferAttachmentParameteriv(target, attachment,
578 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
580 return getTextureLevelDesc(context, object_name, texture_level, desc);
582 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
590 getDrawBufferImage() {
591 GLenum format = GL_RGB;
592 GLint channels = __gl_format_channels(format);
599 GLenum framebuffer_binding;
600 GLenum framebuffer_target;
602 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
603 framebuffer_target = GL_FRAMEBUFFER;
605 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
606 framebuffer_target = GL_DRAW_FRAMEBUFFER;
609 GLint draw_framebuffer = 0;
610 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
612 GLint draw_buffer = GL_NONE;
614 if (draw_framebuffer) {
615 if (context.ARB_draw_buffers) {
616 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
617 if (draw_buffer == GL_NONE) {
622 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
627 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
628 if (draw_buffer == GL_NONE) {
633 if (!getDrawableBounds(&desc.width, &desc.height)) {
640 GLenum type = GL_UNSIGNED_BYTE;
643 if (format == GL_DEPTH_COMPONENT) {
644 type = GL_UNSIGNED_INT;
649 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
654 while (glGetError() != GL_NO_ERROR) {}
656 GLint read_framebuffer = 0;
657 GLint read_buffer = GL_NONE;
659 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
660 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
662 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
663 glReadBuffer(draw_buffer);
666 // TODO: reset imaging state too
667 context.resetPixelPackState();
669 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
671 context.restorePixelPackState();
674 glReadBuffer(read_buffer);
675 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
678 GLenum error = glGetError();
679 if (error != GL_NO_ERROR) {
681 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
682 error = glGetError();
683 } while(error != GL_NO_ERROR);
693 * Dump the image of the currently bound read buffer.
696 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
697 GLint internalFormat = GL_NONE)
699 GLint channels = __gl_format_channels(format);
705 // Tell the GUI this is no ordinary object, but an image
706 json.writeStringMember("__class__", "image");
708 json.writeNumberMember("__width__", width);
709 json.writeNumberMember("__height__", height);
710 json.writeNumberMember("__depth__", 1);
712 json.writeStringMember("__format__", enumToString(internalFormat));
714 // Hardcoded for now, but we could chose types more adequate to the
715 // texture internal format
716 json.writeStringMember("__type__", "uint8");
717 json.writeBoolMember("__normalized__", true);
718 json.writeNumberMember("__channels__", channels);
720 GLenum type = GL_UNSIGNED_BYTE;
723 if (format == GL_DEPTH_COMPONENT) {
724 type = GL_UNSIGNED_INT;
729 GLubyte *pixels = new GLubyte[width*height*channels];
731 // TODO: reset imaging state too
732 context.resetPixelPackState();
734 glReadPixels(0, 0, width, height, format, type, pixels);
736 context.restorePixelPackState();
738 json.beginMember("__data__");
741 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
742 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
743 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
744 json.writeBase64(pngBuffer, pngBufferSize);
746 json.endMember(); // __data__
754 downsampledFramebuffer(Context &context,
755 GLuint oldFbo, GLint drawbuffer,
756 GLint colorRb, GLint depthRb, GLint stencilRb,
757 GLuint *rbs, GLint *numRbs)
764 glGenFramebuffers(1, &fbo);
765 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
770 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
771 getBoundRenderbufferDesc(context, desc);
773 glGenRenderbuffers(1, &rbs[*numRbs]);
774 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
775 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
776 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
777 GL_RENDERBUFFER, rbs[*numRbs]);
779 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
780 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
781 glDrawBuffer(drawbuffer);
782 glReadBuffer(drawbuffer);
783 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
784 GL_COLOR_BUFFER_BIT, GL_NEAREST);
785 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
789 if (stencilRb == depthRb && stencilRb) {
790 //combined depth and stencil buffer
792 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
793 getBoundRenderbufferDesc(context, desc);
795 glGenRenderbuffers(1, &rbs[*numRbs]);
796 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
797 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
798 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
799 GL_RENDERBUFFER, rbs[*numRbs]);
800 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
801 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
802 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
803 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
804 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
809 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
810 getBoundRenderbufferDesc(context, desc);
812 glGenRenderbuffers(1, &rbs[*numRbs]);
813 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
814 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
815 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
817 GL_RENDERBUFFER, rbs[*numRbs]);
818 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
819 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
820 glDrawBuffer(GL_DEPTH_ATTACHMENT);
821 glReadBuffer(GL_DEPTH_ATTACHMENT);
822 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
823 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
828 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
829 getBoundRenderbufferDesc(context, desc);
831 glGenRenderbuffers(1, &rbs[*numRbs]);
832 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
833 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
834 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
835 GL_STENCIL_ATTACHMENT,
836 GL_RENDERBUFFER, rbs[*numRbs]);
837 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
838 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
839 glDrawBuffer(GL_STENCIL_ATTACHMENT);
840 glReadBuffer(GL_STENCIL_ATTACHMENT);
841 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
842 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
852 * Dump images of current draw drawable/window.
855 dumpDrawableImages(JSONWriter &json, Context &context)
859 if (!getDrawableBounds(&width, &height)) {
863 GLint draw_buffer = GL_NONE;
865 draw_buffer = GL_BACK;
867 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
868 glReadBuffer(draw_buffer);
871 if (draw_buffer != GL_NONE) {
872 GLint read_buffer = GL_NONE;
874 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
877 GLint alpha_bits = 0;
879 // XXX: Ignore alpha until we are able to match the traced visual
880 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
882 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
883 json.beginMember(enumToString(draw_buffer));
884 dumpReadBufferImage(json, width, height, format);
888 glReadBuffer(read_buffer);
893 GLint depth_bits = 0;
894 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
896 json.beginMember("GL_DEPTH_COMPONENT");
897 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
901 GLint stencil_bits = 0;
902 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
904 json.beginMember("GL_STENCIL_INDEX");
905 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
913 * Dump the specified framebuffer attachment.
915 * In the case of a color attachment, it assumes it is already bound for read.
918 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
921 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
925 json.beginMember(enumToString(attachment));
926 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
932 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
934 GLint read_framebuffer = 0;
935 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
937 GLint read_buffer = GL_NONE;
938 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
940 GLint max_draw_buffers = 1;
941 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
942 GLint max_color_attachments = 0;
943 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
945 for (GLint i = 0; i < max_draw_buffers; ++i) {
946 GLint draw_buffer = GL_NONE;
947 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
948 if (draw_buffer != GL_NONE) {
949 glReadBuffer(draw_buffer);
951 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
952 attachment = draw_buffer;
954 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
955 attachment = GL_COLOR_ATTACHMENT0;
957 GLint alpha_size = 0;
958 glGetFramebufferAttachmentParameteriv(target, attachment,
959 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
961 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
962 dumpFramebufferAttachment(json, context, target, attachment, format);
966 glReadBuffer(read_buffer);
969 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
970 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
973 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
978 dumpFramebuffer(JSONWriter &json, Context &context)
980 json.beginMember("framebuffer");
983 GLint boundDrawFbo = 0, boundReadFbo = 0;
984 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
985 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
987 dumpDrawableImages(json, context);
988 } else if (context.ES) {
989 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
991 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
992 GLint draw_buffer0 = GL_NONE;
993 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
994 bool multisample = false;
997 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1000 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1001 if (object_type == GL_RENDERBUFFER) {
1002 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1003 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1005 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1011 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1012 if (object_type == GL_RENDERBUFFER) {
1013 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1014 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1016 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1022 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1023 if (object_type == GL_RENDERBUFFER) {
1024 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1025 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1027 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1033 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1040 // glReadPixels doesnt support multisampled buffers so we need
1041 // to blit the fbo to a temporary one
1042 fboCopy = downsampledFramebuffer(context,
1043 boundDrawFbo, draw_buffer0,
1044 colorRb, depthRb, stencilRb,
1048 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1051 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1052 glDeleteRenderbuffers(numRbs, rbs);
1053 glDeleteFramebuffers(1, &fboCopy);
1056 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1057 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1061 json.endMember(); // framebuffer
1065 } /* namespace glstate */