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
65 Context::Context(void) {
66 memset(this, 0, sizeof *this);
68 const char *version = (const char *)glGetString(GL_VERSION);
70 if (version[0] == 'O' &&
84 ARB_draw_buffers = !ES;
86 // TODO: Check extensions we use below
90 Context::resetPixelPackState(void) {
92 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
93 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
94 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
95 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
96 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
97 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
98 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
99 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
100 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
103 glGetIntegerv(GL_PACK_ALIGNMENT, &packAlignment);
105 glPixelStorei(GL_PACK_ALIGNMENT, 1);
109 Context::restorePixelPackState(void) {
113 glPixelStorei(GL_PACK_ALIGNMENT, packAlignment);
119 dumpTextureImage(JSONWriter &json, Context &context, GLenum target, GLint level)
121 GLint width, height = 1, depth = 1;
124 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &format);
127 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
129 if (target != GL_TEXTURE_1D) {
131 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
132 if (target == GL_TEXTURE_3D) {
134 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
138 if (width <= 0 || height <= 0 || depth <= 0) {
143 GLint active_texture = GL_TEXTURE0;
144 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
145 snprintf(label, sizeof label, "%s, %s, level = %d",
146 enumToString(active_texture), enumToString(target), level);
148 json.beginMember(label);
152 // Tell the GUI this is no ordinary object, but an image
153 json.writeStringMember("__class__", "image");
155 json.writeNumberMember("__width__", width);
156 json.writeNumberMember("__height__", height);
157 json.writeNumberMember("__depth__", depth);
159 json.writeStringMember("__format__", enumToString(format));
161 // Hardcoded for now, but we could chose types more adequate to the
162 // texture internal format
163 json.writeStringMember("__type__", "uint8");
164 json.writeBoolMember("__normalized__", true);
165 json.writeNumberMember("__channels__", 4);
167 GLubyte *pixels = new GLubyte[depth*width*height*4];
169 context.resetPixelPackState();
171 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
173 context.restorePixelPackState();
175 json.beginMember("__data__");
178 image::writePixelsToBuffer(pixels, width, height, 4, true, &pngBuffer, &pngBufferSize);
179 json.writeBase64(pngBuffer, pngBufferSize);
181 json.endMember(); // __data__
190 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
192 GLint texture_binding = 0;
193 glGetIntegerv(binding, &texture_binding);
194 if (!glIsEnabled(target) && !texture_binding) {
200 GLint width = 0, height = 0;
201 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
202 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
203 if (!width || !height) {
207 if (target == GL_TEXTURE_CUBE_MAP) {
208 for (int face = 0; face < 6; ++face) {
209 dumpTextureImage(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
212 dumpTextureImage(json, context, target, level);
221 dumpTextures(JSONWriter &json, Context &context)
223 json.beginMember("textures");
225 GLint active_texture = GL_TEXTURE0;
226 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
227 GLint max_texture_coords = 0;
228 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
229 GLint max_combined_texture_image_units = 0;
230 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
231 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
232 for (GLint unit = 0; unit < max_units; ++unit) {
233 GLenum texture = GL_TEXTURE0 + unit;
234 glActiveTexture(texture);
235 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
236 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
237 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
238 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
239 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
241 glActiveTexture(active_texture);
243 json.endMember(); // textures
248 getDrawableBounds(GLint *width, GLint *height) {
249 #if defined(TRACE_EGL)
251 EGLContext currentContext = eglGetCurrentContext();
252 if (currentContext == EGL_NO_CONTEXT) {
256 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
257 if (currentSurface == EGL_NO_SURFACE) {
261 EGLDisplay currentDisplay = eglGetCurrentDisplay();
262 if (currentDisplay == EGL_NO_DISPLAY) {
266 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
267 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
273 #elif defined(_WIN32)
275 HDC hDC = wglGetCurrentDC();
280 HWND hWnd = WindowFromDC(hDC);
283 if (!GetClientRect(hWnd, &rect)) {
287 *width = rect.right - rect.left;
288 *height = rect.bottom - rect.top;
291 #elif defined(__APPLE__)
293 CGLContextObj ctx = CGLGetCurrentContext();
302 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
308 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
312 *width = rect.size.width;
313 *height = rect.size.height;
316 #elif defined(HAVE_X11)
322 unsigned int w, h, bw, depth;
324 display = glXGetCurrentDisplay();
329 drawable = glXGetCurrentDrawable();
330 if (drawable == None) {
334 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
350 static const GLenum texture_bindings[][2] = {
351 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
352 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
353 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
354 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
355 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
360 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
363 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
364 target = texture_bindings[i][0];
366 GLenum binding = texture_bindings[i][1];
368 while (glGetError() != GL_NO_ERROR)
371 glGetIntegerv(binding, &bound_texture);
372 glBindTexture(target, texture);
374 if (glGetError() == GL_NO_ERROR) {
378 glBindTexture(target, bound_texture);
388 getTextureLevelSize(GLint texture, GLint level, GLint *width, GLint *height)
394 GLint bound_texture = 0;
395 if (!bindTexture(texture, target, bound_texture)) {
399 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, width);
400 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, height);
402 glBindTexture(target, bound_texture);
404 return *width > 0 && *height > 0;
409 getTextureLevelFormat(GLint texture, GLint level)
412 GLint bound_texture = 0;
413 if (!bindTexture(texture, target, bound_texture)) {
417 GLint format = GL_NONE;
418 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &format);
420 glBindTexture(target, bound_texture);
428 getRenderbufferSize(GLint renderbuffer, GLint *width, GLint *height)
430 GLint bound_renderbuffer = 0;
431 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
432 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
436 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, width);
437 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, height);
439 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
441 return *width > 0 && *height > 0;
446 getRenderbufferFormat(GLint renderbuffer)
448 GLint bound_renderbuffer = 0;
449 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
450 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
452 GLint format = GL_NONE;
453 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
455 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
462 getFramebufferAttachmentSize(GLenum target, GLenum attachment, GLint *width, GLint *height)
464 GLint object_type = GL_NONE;
465 glGetFramebufferAttachmentParameteriv(target, attachment,
466 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
468 if (object_type == GL_NONE) {
472 GLint object_name = 0;
473 glGetFramebufferAttachmentParameteriv(target, attachment,
474 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
476 if (object_name == 0) {
480 if (object_type == GL_RENDERBUFFER) {
481 return getRenderbufferSize(object_name, width, height);
482 } else if (object_type == GL_TEXTURE) {
483 GLint texture_level = 0;
484 glGetFramebufferAttachmentParameteriv(target, attachment,
485 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
487 return getTextureLevelSize(object_name, texture_level, width, height);
489 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
497 getFramebufferAttachmentFormat(GLenum target, GLenum attachment)
499 GLint object_type = GL_NONE;
500 glGetFramebufferAttachmentParameteriv(target, attachment,
501 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
503 if (object_type == GL_NONE) {
507 GLint object_name = 0;
508 glGetFramebufferAttachmentParameteriv(target, attachment,
509 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
511 if (object_name == 0) {
515 if (object_type == GL_RENDERBUFFER) {
516 return getRenderbufferFormat(object_name);
517 } else if (object_type == GL_TEXTURE) {
518 GLint texture_level = 0;
519 glGetFramebufferAttachmentParameteriv(target, attachment,
520 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
522 return getTextureLevelFormat(object_name, texture_level);
524 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
532 getDrawBufferImage() {
533 GLenum format = GL_RGB;
534 GLint channels = __gl_format_channels(format);
541 GLenum framebuffer_binding;
542 GLenum framebuffer_target;
544 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
545 framebuffer_target = GL_FRAMEBUFFER;
547 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
548 framebuffer_target = GL_DRAW_FRAMEBUFFER;
551 GLint draw_framebuffer = 0;
552 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
554 GLint draw_buffer = GL_NONE;
556 if (draw_framebuffer) {
557 if (context.ARB_draw_buffers) {
558 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
559 if (draw_buffer == GL_NONE) {
564 if (!getFramebufferAttachmentSize(framebuffer_target, draw_buffer, &width, &height)) {
569 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
570 if (draw_buffer == GL_NONE) {
575 if (!getDrawableBounds(&width, &height)) {
580 GLenum type = GL_UNSIGNED_BYTE;
583 if (format == GL_DEPTH_COMPONENT) {
584 type = GL_UNSIGNED_INT;
589 image::Image *image = new image::Image(width, height, channels, true);
594 while (glGetError() != GL_NO_ERROR) {}
596 GLint read_framebuffer = 0;
597 GLint read_buffer = GL_NONE;
599 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
600 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
602 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
603 glReadBuffer(draw_buffer);
606 // TODO: reset imaging state too
607 context.resetPixelPackState();
609 glReadPixels(0, 0, width, height, format, type, image->pixels);
611 context.restorePixelPackState();
614 glReadBuffer(read_buffer);
615 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
618 GLenum error = glGetError();
619 if (error != GL_NO_ERROR) {
621 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
622 error = glGetError();
623 } while(error != GL_NO_ERROR);
633 * Dump the image of the currently bound read buffer.
636 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
637 GLint internalFormat = GL_NONE)
639 GLint channels = __gl_format_channels(format);
645 // Tell the GUI this is no ordinary object, but an image
646 json.writeStringMember("__class__", "image");
648 json.writeNumberMember("__width__", width);
649 json.writeNumberMember("__height__", height);
650 json.writeNumberMember("__depth__", 1);
652 json.writeStringMember("__format__", enumToString(internalFormat));
654 // Hardcoded for now, but we could chose types more adequate to the
655 // texture internal format
656 json.writeStringMember("__type__", "uint8");
657 json.writeBoolMember("__normalized__", true);
658 json.writeNumberMember("__channels__", channels);
660 GLenum type = GL_UNSIGNED_BYTE;
663 if (format == GL_DEPTH_COMPONENT) {
664 type = GL_UNSIGNED_INT;
669 GLubyte *pixels = new GLubyte[width*height*channels];
671 // TODO: reset imaging state too
672 context.resetPixelPackState();
674 glReadPixels(0, 0, width, height, format, type, pixels);
676 context.restorePixelPackState();
678 json.beginMember("__data__");
681 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
682 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
683 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
684 json.writeBase64(pngBuffer, pngBufferSize);
686 json.endMember(); // __data__
694 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
695 GLint colorRb, GLint depthRb, GLint stencilRb,
696 GLuint *rbs, GLint *numRbs)
704 glGenFramebuffers(1, &fbo);
705 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
707 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
708 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
709 GL_RENDERBUFFER_WIDTH, &w);
710 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
711 GL_RENDERBUFFER_HEIGHT, &h);
712 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
713 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
715 glGenRenderbuffers(1, &rbs[*numRbs]);
716 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
717 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
718 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
719 GL_RENDERBUFFER, rbs[*numRbs]);
721 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
722 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
723 glDrawBuffer(drawbuffer);
724 glReadBuffer(drawbuffer);
725 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
726 GL_COLOR_BUFFER_BIT, GL_NEAREST);
727 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
730 if (stencilRb == depthRb && stencilRb) {
731 //combined depth and stencil buffer
732 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
733 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
734 GL_RENDERBUFFER_WIDTH, &w);
735 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
736 GL_RENDERBUFFER_HEIGHT, &h);
737 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
738 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
740 glGenRenderbuffers(1, &rbs[*numRbs]);
741 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
742 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
743 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
744 GL_RENDERBUFFER, rbs[*numRbs]);
745 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
746 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
747 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
748 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
749 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
753 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
754 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
755 GL_RENDERBUFFER_WIDTH, &w);
756 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
757 GL_RENDERBUFFER_HEIGHT, &h);
758 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
759 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
761 glGenRenderbuffers(1, &rbs[*numRbs]);
762 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
763 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
764 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
766 GL_RENDERBUFFER, rbs[*numRbs]);
767 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
768 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
769 glDrawBuffer(GL_DEPTH_ATTACHMENT);
770 glReadBuffer(GL_DEPTH_ATTACHMENT);
771 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
772 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
776 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
777 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
778 GL_RENDERBUFFER_WIDTH, &w);
779 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
780 GL_RENDERBUFFER_HEIGHT, &h);
781 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
782 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
784 glGenRenderbuffers(1, &rbs[*numRbs]);
785 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
786 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
787 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
788 GL_STENCIL_ATTACHMENT,
789 GL_RENDERBUFFER, rbs[*numRbs]);
790 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
791 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
792 glDrawBuffer(GL_STENCIL_ATTACHMENT);
793 glReadBuffer(GL_STENCIL_ATTACHMENT);
794 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
795 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
805 * Dump images of current draw drawable/window.
808 dumpDrawableImages(JSONWriter &json, Context &context)
812 if (!getDrawableBounds(&width, &height)) {
816 GLint draw_buffer = GL_NONE;
818 draw_buffer = GL_BACK;
820 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
821 glReadBuffer(draw_buffer);
824 if (draw_buffer != GL_NONE) {
825 GLint read_buffer = GL_NONE;
827 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
830 GLint alpha_bits = 0;
832 // XXX: Ignore alpha until we are able to match the traced visual
833 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
835 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
836 json.beginMember(enumToString(draw_buffer));
837 dumpReadBufferImage(json, width, height, format);
841 glReadBuffer(read_buffer);
846 GLint depth_bits = 0;
847 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
849 json.beginMember("GL_DEPTH_COMPONENT");
850 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
854 GLint stencil_bits = 0;
855 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
857 json.beginMember("GL_STENCIL_INDEX");
858 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
866 * Dump the specified framebuffer attachment.
868 * In the case of a color attachment, it assumes it is already bound for read.
871 dumpFramebufferAttachment(JSONWriter &json, GLenum target, GLenum attachment, GLenum format)
873 GLint width = 0, height = 0;
874 if (!getFramebufferAttachmentSize(target, attachment, &width, &height)) {
878 GLint internalFormat = getFramebufferAttachmentFormat(target, attachment);
880 json.beginMember(enumToString(attachment));
881 dumpReadBufferImage(json, width, height, format, internalFormat);
887 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
889 GLint read_framebuffer = 0;
890 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
892 GLint read_buffer = GL_NONE;
893 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
895 GLint max_draw_buffers = 1;
896 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
897 GLint max_color_attachments = 0;
898 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
900 for (GLint i = 0; i < max_draw_buffers; ++i) {
901 GLint draw_buffer = GL_NONE;
902 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
903 if (draw_buffer != GL_NONE) {
904 glReadBuffer(draw_buffer);
906 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
907 attachment = draw_buffer;
909 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
910 attachment = GL_COLOR_ATTACHMENT0;
912 GLint alpha_size = 0;
913 glGetFramebufferAttachmentParameteriv(target, attachment,
914 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
916 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
917 dumpFramebufferAttachment(json, target, attachment, format);
921 glReadBuffer(read_buffer);
924 dumpFramebufferAttachment(json, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
925 dumpFramebufferAttachment(json, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
928 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
933 dumpFramebuffer(JSONWriter &json, Context &context)
935 json.beginMember("framebuffer");
938 GLint boundDrawFbo = 0, boundReadFbo = 0;
939 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
940 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
942 dumpDrawableImages(json, context);
943 } else if (context.ES) {
944 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
946 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
947 GLint draw_buffer0 = GL_NONE;
948 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
949 bool multisample = false;
952 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
955 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
956 if (object_type == GL_RENDERBUFFER) {
957 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
958 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
960 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
966 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
967 if (object_type == GL_RENDERBUFFER) {
968 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
969 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
971 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
977 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
978 if (object_type == GL_RENDERBUFFER) {
979 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
980 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
982 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
988 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
995 // glReadPixels doesnt support multisampled buffers so we need
996 // to blit the fbo to a temporary one
997 fboCopy = downsampledFramebuffer(boundDrawFbo, draw_buffer0,
998 colorRb, depthRb, stencilRb,
1002 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1005 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1006 glDeleteRenderbuffers(numRbs, rbs);
1007 glDeleteFramebuffers(1, &fboCopy);
1010 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1011 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1015 json.endMember(); // framebuffer
1019 static const GLenum bindings[] = {
1022 GL_PIXEL_PACK_BUFFER_BINDING,
1023 GL_PIXEL_UNPACK_BUFFER_BINDING,
1024 GL_TEXTURE_BINDING_1D,
1025 GL_TEXTURE_BINDING_2D,
1026 GL_TEXTURE_BINDING_3D,
1027 GL_TEXTURE_BINDING_RECTANGLE,
1028 GL_TEXTURE_BINDING_CUBE_MAP,
1029 GL_DRAW_FRAMEBUFFER_BINDING,
1030 GL_READ_FRAMEBUFFER_BINDING,
1031 GL_RENDERBUFFER_BINDING,
1043 #define NUM_BINDINGS sizeof(bindings)/sizeof(bindings[0])
1046 void dumpCurrentContext(std::ostream &os)
1048 JSONWriter json(os);
1051 GLint old_bindings[NUM_BINDINGS];
1052 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1053 old_bindings[i] = 0;
1054 glGetIntegerv(bindings[i], &old_bindings[i]);
1060 dumpParameters(json, context);
1061 dumpShadersUniforms(json);
1062 dumpTextures(json, context);
1063 dumpFramebuffer(json, context);
1066 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1067 GLint new_binding = 0;
1068 glGetIntegerv(bindings[i], &new_binding);
1069 if (new_binding != old_bindings[i]) {
1070 std::cerr << "warning: " << enumToString(bindings[i]) << " was clobbered\n";
1078 } /* namespace glstate */