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 **************************************************************************/
35 #include "glstate.hpp"
40 #include <Carbon/Carbon.h>
46 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
52 #endif /* __APPLE__ */
59 resetPixelPackState(void) {
60 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
61 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
62 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
63 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
64 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
65 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
66 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
67 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
68 glPixelStorei(GL_PACK_ALIGNMENT, 1);
69 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
74 restorePixelPackState(void) {
80 dumpShader(JSONWriter &json, GLuint shader)
86 GLint shader_type = 0;
87 glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
92 GLint source_length = 0;
93 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
98 GLchar *source = new GLchar[source_length];
101 glGetShaderSource(shader, source_length, &length, source);
103 json.beginMember(enumToString(shader_type));
104 json.writeString(source);
112 dumpShaderObj(JSONWriter &json, GLhandleARB shaderObj)
118 GLint shader_type = 0;
119 glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &shader_type);
124 GLint source_length = 0;
125 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
126 if (!source_length) {
130 GLcharARB *source = new GLcharARB[source_length];
133 glGetShaderSource(shaderObj, source_length, &length, source);
135 json.beginMember(enumToString(shader_type));
136 json.writeString(source);
144 dumpCurrentProgram(JSONWriter &json)
147 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
152 GLint attached_shaders = 0;
153 glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
154 if (!attached_shaders) {
158 GLuint *shaders = new GLuint[attached_shaders];
160 glGetAttachedShaders(program, attached_shaders, &count, shaders);
161 for (GLsizei i = 0; i < count; ++ i) {
162 dumpShader(json, shaders[i]);
169 dumpCurrentProgramObj(JSONWriter &json)
171 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
176 GLint attached_shaders = 0;
177 glGetProgramivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
178 if (!attached_shaders) {
182 GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
184 glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
185 for (GLsizei i = 0; i < count; ++ i) {
186 dumpShaderObj(json, shaderObjs[i]);
188 delete [] shaderObjs;
193 dumpArbProgram(JSONWriter &json, GLenum target)
195 if (!glIsEnabled(target)) {
199 GLint program_length = 0;
200 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
201 if (!program_length) {
205 GLchar *source = new GLchar[program_length + 1];
207 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
208 source[program_length] = 0;
210 json.beginMember(enumToString(target));
211 json.writeString(source);
219 dumpShaders(JSONWriter &json)
221 json.beginMember("shaders");
223 dumpCurrentProgram(json);
224 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
225 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
227 json.endMember(); //shaders
232 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
234 GLint width, height = 1, depth = 1;
237 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
239 if (target != GL_TEXTURE_1D) {
241 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
242 if (target == GL_TEXTURE_3D) {
244 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
248 if (width <= 0 || height <= 0 || depth <= 0) {
253 GLint active_texture = GL_TEXTURE0;
254 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
255 snprintf(label, sizeof label, "%s, %s, level = %i", enumToString(active_texture), enumToString(target), level);
257 json.beginMember(label);
261 // Tell the GUI this is no ordinary object, but an image
262 json.writeStringMember("__class__", "image");
264 json.writeNumberMember("__width__", width);
265 json.writeNumberMember("__height__", height);
266 json.writeNumberMember("__depth__", depth);
268 // Hardcoded for now, but we could chose types more adequate to the
269 // texture internal format
270 json.writeStringMember("__type__", "uint8");
271 json.writeBoolMember("__normalized__", true);
272 json.writeNumberMember("__channels__", 4);
274 GLubyte *pixels = new GLubyte[depth*width*height*4];
276 resetPixelPackState();
278 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
280 restorePixelPackState();
282 json.beginMember("__data__");
285 Image::writePixelsToBuffer(pixels, width, height, 4, false, &pngBuffer, &pngBufferSize);
286 json.writeBase64(pngBuffer, pngBufferSize);
288 json.endMember(); // __data__
297 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
299 GLint texture_binding = 0;
300 glGetIntegerv(binding, &texture_binding);
301 if (!glIsEnabled(target) && !texture_binding) {
307 GLint width = 0, height = 0;
308 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
309 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
310 if (!width || !height) {
314 if (target == GL_TEXTURE_CUBE_MAP) {
315 for (int face = 0; face < 6; ++face) {
316 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
319 dumpTextureImage(json, target, level);
328 dumpTextures(JSONWriter &json)
330 json.beginMember("textures");
332 GLint active_texture = GL_TEXTURE0;
333 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
334 GLint max_texture_coords = 0;
335 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
336 GLint max_combined_texture_image_units = 0;
337 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
338 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
339 for (GLint unit = 0; unit < max_units; ++unit) {
340 GLenum texture = GL_TEXTURE0 + unit;
341 glActiveTexture(texture);
342 dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
343 dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
344 dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
345 dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
346 dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
348 glActiveTexture(active_texture);
350 json.endMember(); // textures
355 getDrawableBounds(GLint *width, GLint *height) {
358 HDC hDC = wglGetCurrentDC();
363 HWND hWnd = WindowFromDC(hDC);
366 if (!GetClientRect(hWnd, &rect)) {
370 *width = rect.right - rect.left;
371 *height = rect.bottom - rect.top;
373 #elif defined(__APPLE__)
375 CGLContextObj ctx = CGLGetCurrentContext();
384 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
390 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
394 *width = rect.size.width;
395 *height = rect.size.height;
403 unsigned int w, h, bw, depth;
405 display = glXGetCurrentDisplay();
410 drawable = glXGetCurrentDrawable();
411 if (drawable == None) {
415 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
428 static const GLenum texture_bindings[][2] = {
429 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
430 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
431 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
432 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
433 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
438 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
441 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
442 target = texture_bindings[i][0];
444 GLenum binding = texture_bindings[i][1];
446 while (glGetError() != GL_NO_ERROR)
449 glGetIntegerv(binding, &bound_texture);
450 glBindTexture(target, texture);
452 if (glGetError() == GL_NO_ERROR) {
456 glBindTexture(target, bound_texture);
466 getTextureLevelSize(GLint texture, GLint level, GLint *width, GLint *height)
472 GLint bound_texture = 0;
473 if (!bindTexture(texture, target, bound_texture)) {
477 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, width);
478 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, height);
480 glBindTexture(target, bound_texture);
482 return *width > 0 && *height > 0;
487 getRenderbufferSize(GLint renderbuffer, GLint *width, GLint *height)
489 GLint bound_renderbuffer = 0;
490 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
491 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
495 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, width);
496 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, height);
498 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
500 return *width > 0 && *height > 0;
505 getFramebufferAttachmentSize(GLenum target, GLenum attachment, GLint *width, GLint *height)
507 GLint object_type = GL_NONE;
508 glGetFramebufferAttachmentParameteriv(target, attachment,
509 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
511 if (object_type == GL_NONE) {
515 GLint object_name = 0;
516 glGetFramebufferAttachmentParameteriv(target, attachment,
517 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
519 if (object_name == 0) {
523 if (object_type == GL_RENDERBUFFER) {
524 return getRenderbufferSize(object_name, width, height);
525 } else if (object_type == GL_TEXTURE) {
526 GLint texture_level = 0;
527 glGetFramebufferAttachmentParameteriv(target, attachment,
528 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
530 return getTextureLevelSize(object_name, texture_level, width, height);
532 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
539 getDrawBufferImage(GLenum format) {
540 GLint channels = __gl_format_channels(format);
545 GLint draw_framebuffer = 0;
546 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
548 GLint draw_buffer = GL_NONE;
550 if (draw_framebuffer) {
551 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
552 if (draw_buffer == GL_NONE) {
556 if (!getFramebufferAttachmentSize(GL_DRAW_FRAMEBUFFER, draw_buffer, &width, &height)) {
560 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
561 if (draw_buffer == GL_NONE) {
565 if (!getDrawableBounds(&width, &height)) {
570 Image::Image *image = new Image::Image(width, height, channels, true);
575 while (glGetError() != GL_NO_ERROR) {}
577 GLint read_framebuffer = 0;
578 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
579 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
581 GLint read_buffer = 0;
582 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
583 glReadBuffer(draw_buffer);
585 // TODO: reset imaging state too
586 resetPixelPackState();
588 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
590 restorePixelPackState();
591 glReadBuffer(read_buffer);
592 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
594 GLenum error = glGetError();
595 if (error != GL_NO_ERROR) {
597 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
598 error = glGetError();
599 } while(error != GL_NO_ERROR);
609 * Dump the image of the currently bound read buffer.
612 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format)
614 GLint channels = __gl_format_channels(format);
618 // Tell the GUI this is no ordinary object, but an image
619 json.writeStringMember("__class__", "image");
621 json.writeNumberMember("__width__", width);
622 json.writeNumberMember("__height__", height);
623 json.writeNumberMember("__depth__", 1);
625 // Hardcoded for now, but we could chose types more adequate to the
626 // texture internal format
627 json.writeStringMember("__type__", "uint8");
628 json.writeBoolMember("__normalized__", true);
629 json.writeNumberMember("__channels__", channels);
631 GLubyte *pixels = new GLubyte[width*height*channels];
633 // TODO: reset imaging state too
634 resetPixelPackState();
636 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
638 restorePixelPackState();
640 json.beginMember("__data__");
643 Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
644 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
645 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
646 json.writeBase64(pngBuffer, pngBufferSize);
648 json.endMember(); // __data__
656 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
657 GLint colorRb, GLint depthRb, GLint stencilRb,
658 GLuint *rbs, GLint *numRbs)
666 glGenFramebuffers(1, &fbo);
667 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
669 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
670 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
671 GL_RENDERBUFFER_WIDTH, &w);
672 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
673 GL_RENDERBUFFER_HEIGHT, &h);
674 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
675 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
677 glGenRenderbuffers(1, &rbs[*numRbs]);
678 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
679 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
680 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
681 GL_RENDERBUFFER, rbs[*numRbs]);
683 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
684 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
685 glDrawBuffer(drawbuffer);
686 glReadBuffer(drawbuffer);
687 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
688 GL_COLOR_BUFFER_BIT, GL_NEAREST);
689 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
692 if (stencilRb == depthRb && stencilRb) {
693 //combined depth and stencil buffer
694 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
695 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
696 GL_RENDERBUFFER_WIDTH, &w);
697 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
698 GL_RENDERBUFFER_HEIGHT, &h);
699 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
700 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
702 glGenRenderbuffers(1, &rbs[*numRbs]);
703 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
704 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
705 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
706 GL_RENDERBUFFER, rbs[*numRbs]);
707 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
708 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
709 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
710 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
711 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
715 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
716 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
717 GL_RENDERBUFFER_WIDTH, &w);
718 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
719 GL_RENDERBUFFER_HEIGHT, &h);
720 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
721 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
723 glGenRenderbuffers(1, &rbs[*numRbs]);
724 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
725 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
726 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
728 GL_RENDERBUFFER, rbs[*numRbs]);
729 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
730 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
731 glDrawBuffer(GL_DEPTH_ATTACHMENT);
732 glReadBuffer(GL_DEPTH_ATTACHMENT);
733 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
734 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
738 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
739 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
740 GL_RENDERBUFFER_WIDTH, &w);
741 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
742 GL_RENDERBUFFER_HEIGHT, &h);
743 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
744 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
746 glGenRenderbuffers(1, &rbs[*numRbs]);
747 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
748 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
749 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
750 GL_STENCIL_ATTACHMENT,
751 GL_RENDERBUFFER, rbs[*numRbs]);
752 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
753 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
754 glDrawBuffer(GL_STENCIL_ATTACHMENT);
755 glReadBuffer(GL_STENCIL_ATTACHMENT);
756 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
757 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
767 * Dump images of current draw drawable/window.
770 dumpDrawableImages(JSONWriter &json)
774 if (!getDrawableBounds(&width, &height)) {
778 GLint draw_buffer = GL_NONE;
779 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
780 glReadBuffer(draw_buffer);
782 if (draw_buffer != GL_NONE) {
783 GLint read_buffer = GL_NONE;
784 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
786 GLint alpha_bits = 0;
787 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
788 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
789 json.beginMember(enumToString(draw_buffer));
790 dumpReadBufferImage(json, width, height, format);
793 glReadBuffer(read_buffer);
796 GLint depth_bits = 0;
797 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
799 json.beginMember("GL_DEPTH_COMPONENT");
800 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
804 GLint stencil_bits = 0;
805 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
807 json.beginMember("GL_STENCIL_INDEX");
808 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
813 * Dump the specified framebuffer attachment.
815 * In the case of a color attachment, it assumes it is already bound for read.
818 dumpFramebufferAttachment(JSONWriter &json, GLenum target, GLenum attachment, GLenum format)
820 GLint width = 0, height = 0;
821 if (!getFramebufferAttachmentSize(target, attachment, &width, &height)) {
825 json.beginMember(enumToString(attachment));
826 dumpReadBufferImage(json, width, height, format);
832 dumpFramebufferAttachments(JSONWriter &json, GLenum target)
834 GLint read_framebuffer = 0;
835 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
837 GLint read_buffer = GL_NONE;
838 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
840 GLint max_draw_buffers = 1;
841 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
842 GLint max_color_attachments = 0;
843 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
845 for (GLint i = 0; i < max_draw_buffers; ++i) {
846 GLint draw_buffer = GL_NONE;
847 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
848 if (draw_buffer != GL_NONE) {
849 glReadBuffer(draw_buffer);
851 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
852 attachment = draw_buffer;
854 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
855 attachment = GL_COLOR_ATTACHMENT0;
857 GLint alpha_size = 0;
858 glGetFramebufferAttachmentParameteriv(target, attachment,
859 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
861 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
862 dumpFramebufferAttachment(json, target, attachment, format);
866 glReadBuffer(read_buffer);
868 dumpFramebufferAttachment(json, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
869 dumpFramebufferAttachment(json, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
871 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
876 dumpFramebuffer(JSONWriter &json)
878 json.beginMember("framebuffer");
881 GLint boundDrawFbo = 0, boundReadFbo = 0;
882 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
883 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
885 dumpDrawableImages(json);
887 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
888 GLint draw_buffer0 = GL_NONE;
889 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
890 bool multisample = false;
893 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
896 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
897 if (object_type == GL_RENDERBUFFER) {
898 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
899 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
901 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
907 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
908 if (object_type == GL_RENDERBUFFER) {
909 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
910 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
912 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
918 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
919 if (object_type == GL_RENDERBUFFER) {
920 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
921 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
923 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
929 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
936 // glReadPixels doesnt support multisampled buffers so we need
937 // to blit the fbo to a temporary one
938 fboCopy = downsampledFramebuffer(boundDrawFbo, draw_buffer0,
939 colorRb, depthRb, stencilRb,
943 dumpFramebufferAttachments(json, GL_DRAW_FRAMEBUFFER);
946 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
947 glDeleteRenderbuffers(numRbs, rbs);
948 glDeleteFramebuffers(1, &fboCopy);
951 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
952 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
956 json.endMember(); // framebuffer
960 static const GLenum bindings[] = {
963 GL_PIXEL_PACK_BUFFER_BINDING,
964 GL_PIXEL_UNPACK_BUFFER_BINDING,
965 GL_TEXTURE_BINDING_1D,
966 GL_TEXTURE_BINDING_2D,
967 GL_TEXTURE_BINDING_3D,
968 GL_TEXTURE_BINDING_RECTANGLE,
969 GL_TEXTURE_BINDING_CUBE_MAP,
970 GL_DRAW_FRAMEBUFFER_BINDING,
971 GL_READ_FRAMEBUFFER_BINDING,
972 GL_RENDERBUFFER_BINDING,
984 #define NUM_BINDINGS sizeof(bindings)/sizeof(bindings[0])
987 void dumpCurrentContext(std::ostream &os)
992 GLint old_bindings[NUM_BINDINGS];
993 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
995 glGetIntegerv(bindings[i], &old_bindings[i]);
999 dumpParameters(json);
1002 dumpFramebuffer(json);
1005 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1006 GLint new_binding = 0;
1007 glGetIntegerv(bindings[i], &new_binding);
1008 if (new_binding != old_bindings[i]) {
1009 std::cerr << "warning: " << enumToString(bindings[i]) << " was clobbered\n";
1017 } /* namespace glstate */