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 resetPixelPackState();
587 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
589 restorePixelPackState();
590 glReadBuffer(read_buffer);
591 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
593 GLenum error = glGetError();
594 if (error != GL_NO_ERROR) {
596 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
597 error = glGetError();
598 } while(error != GL_NO_ERROR);
608 * Dump the image of the currently bound read buffer.
611 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format)
613 GLint channels = __gl_format_channels(format);
617 // Tell the GUI this is no ordinary object, but an image
618 json.writeStringMember("__class__", "image");
620 json.writeNumberMember("__width__", width);
621 json.writeNumberMember("__height__", height);
622 json.writeNumberMember("__depth__", 1);
624 // Hardcoded for now, but we could chose types more adequate to the
625 // texture internal format
626 json.writeStringMember("__type__", "uint8");
627 json.writeBoolMember("__normalized__", true);
628 json.writeNumberMember("__channels__", channels);
630 GLubyte *pixels = new GLubyte[width*height*channels];
632 resetPixelPackState();
634 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
636 restorePixelPackState();
638 json.beginMember("__data__");
641 Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
642 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
643 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
644 json.writeBase64(pngBuffer, pngBufferSize);
646 json.endMember(); // __data__
654 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
655 GLint colorRb, GLint depthRb, GLint stencilRb,
656 GLuint *rbs, GLint *numRbs)
664 glGenFramebuffers(1, &fbo);
665 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
667 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
668 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
669 GL_RENDERBUFFER_WIDTH, &w);
670 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
671 GL_RENDERBUFFER_HEIGHT, &h);
672 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
673 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
675 glGenRenderbuffers(1, &rbs[*numRbs]);
676 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
677 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
678 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
679 GL_RENDERBUFFER, rbs[*numRbs]);
681 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
682 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
683 glDrawBuffer(drawbuffer);
684 glReadBuffer(drawbuffer);
685 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
686 GL_COLOR_BUFFER_BIT, GL_NEAREST);
687 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
690 if (stencilRb == depthRb && stencilRb) {
691 //combined depth and stencil buffer
692 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
693 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
694 GL_RENDERBUFFER_WIDTH, &w);
695 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
696 GL_RENDERBUFFER_HEIGHT, &h);
697 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
698 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
700 glGenRenderbuffers(1, &rbs[*numRbs]);
701 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
702 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
703 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
704 GL_RENDERBUFFER, rbs[*numRbs]);
705 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
706 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
707 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
708 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
709 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
713 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
714 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
715 GL_RENDERBUFFER_WIDTH, &w);
716 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
717 GL_RENDERBUFFER_HEIGHT, &h);
718 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
719 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
721 glGenRenderbuffers(1, &rbs[*numRbs]);
722 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
723 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
724 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
726 GL_RENDERBUFFER, rbs[*numRbs]);
727 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
728 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
729 glDrawBuffer(GL_DEPTH_ATTACHMENT);
730 glReadBuffer(GL_DEPTH_ATTACHMENT);
731 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
732 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
736 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
737 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
738 GL_RENDERBUFFER_WIDTH, &w);
739 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
740 GL_RENDERBUFFER_HEIGHT, &h);
741 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
742 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
744 glGenRenderbuffers(1, &rbs[*numRbs]);
745 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
746 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
747 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
748 GL_STENCIL_ATTACHMENT,
749 GL_RENDERBUFFER, rbs[*numRbs]);
750 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
751 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
752 glDrawBuffer(GL_STENCIL_ATTACHMENT);
753 glReadBuffer(GL_STENCIL_ATTACHMENT);
754 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
755 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
765 * Dump images of current draw drawable/window.
768 dumpDrawableImages(JSONWriter &json)
772 if (!getDrawableBounds(&width, &height)) {
776 GLint draw_buffer = GL_NONE;
777 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
778 glReadBuffer(draw_buffer);
780 if (draw_buffer != GL_NONE) {
781 GLint read_buffer = GL_NONE;
782 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
784 GLint alpha_bits = 0;
785 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
786 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
787 json.beginMember(enumToString(draw_buffer));
788 dumpReadBufferImage(json, width, height, format);
791 glReadBuffer(read_buffer);
794 GLint depth_bits = 0;
795 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
797 json.beginMember("GL_DEPTH_COMPONENT");
798 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
802 GLint stencil_bits = 0;
803 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
805 json.beginMember("GL_STENCIL_INDEX");
806 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
811 * Dump the specified framebuffer attachment.
813 * In the case of a color attachment, it assumes it is already bound for read.
816 dumpFramebufferAttachment(JSONWriter &json, GLenum target, GLenum attachment, GLenum format)
818 GLint width = 0, height = 0;
819 if (!getFramebufferAttachmentSize(target, attachment, &width, &height)) {
823 json.beginMember(enumToString(attachment));
824 dumpReadBufferImage(json, width, height, format);
830 dumpFramebufferAttachments(JSONWriter &json, GLenum target)
832 GLint read_framebuffer = 0;
833 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
835 GLint read_buffer = GL_NONE;
836 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
838 GLint max_draw_buffers = 1;
839 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
840 GLint max_color_attachments = 0;
841 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
843 for (GLint i = 0; i < max_draw_buffers; ++i) {
844 GLint draw_buffer = GL_NONE;
845 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
846 if (draw_buffer != GL_NONE) {
847 glReadBuffer(draw_buffer);
849 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
850 attachment = draw_buffer;
852 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
853 attachment = GL_COLOR_ATTACHMENT0;
855 GLint alpha_size = 0;
856 glGetFramebufferAttachmentParameteriv(target, attachment,
857 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
859 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
860 dumpFramebufferAttachment(json, target, attachment, format);
864 glReadBuffer(read_buffer);
866 dumpFramebufferAttachment(json, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
867 dumpFramebufferAttachment(json, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
869 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
874 dumpFramebuffer(JSONWriter &json)
876 json.beginMember("framebuffer");
879 GLint boundDrawFbo = 0, boundReadFbo = 0;
880 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
881 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
883 dumpDrawableImages(json);
885 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
886 GLint draw_buffer0 = GL_NONE;
887 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
888 bool multisample = false;
891 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
894 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
895 if (object_type == GL_RENDERBUFFER) {
896 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
897 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
899 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
905 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
906 if (object_type == GL_RENDERBUFFER) {
907 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
908 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
910 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
916 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
917 if (object_type == GL_RENDERBUFFER) {
918 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
919 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
921 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
927 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
934 // glReadPixels doesnt support multisampled buffers so we need
935 // to blit the fbo to a temporary one
936 fboCopy = downsampledFramebuffer(boundDrawFbo, draw_buffer0,
937 colorRb, depthRb, stencilRb,
941 dumpFramebufferAttachments(json, GL_DRAW_FRAMEBUFFER);
944 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
945 glDeleteRenderbuffers(numRbs, rbs);
946 glDeleteFramebuffers(1, &fboCopy);
949 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
950 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
954 json.endMember(); // framebuffer
958 void dumpCurrentContext(std::ostream &os)
961 dumpParameters(json);
964 dumpFramebuffer(json);
968 } /* namespace glstate */