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 * OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
260 * texture to a framebuffer.
263 getTexImageOES(GLenum target, GLint level, ImageDesc &desc, GLubyte *pixels)
265 memset(pixels, 0x80, desc.height * desc.width * 4);
267 GLenum texture_binding = GL_NONE;
270 texture_binding = GL_TEXTURE_BINDING_2D;
272 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
273 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
274 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
275 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
276 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
277 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
278 texture_binding = GL_TEXTURE_BINDING_CUBE_MAP;
280 case GL_TEXTURE_3D_OES:
281 texture_binding = GL_TEXTURE_BINDING_3D_OES;
287 glGetIntegerv(texture_binding, &texture);
294 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
295 glGenFramebuffers(1, &fbo);
296 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
302 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
303 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
304 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
305 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
306 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
307 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
308 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
309 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
310 if (status != GL_FRAMEBUFFER_COMPLETE) {
311 std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
313 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
315 case GL_TEXTURE_3D_OES:
316 for (int i = 0; i < desc.depth; i++) {
317 glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
318 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * desc.width * desc.height);
323 glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);
325 glDeleteFramebuffers(1, &fbo);
330 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
333 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
339 GLint active_texture = GL_TEXTURE0;
340 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
341 snprintf(label, sizeof label, "%s, %s, level = %d",
342 enumToString(active_texture), enumToString(target), level);
344 json.beginMember(label);
348 // Tell the GUI this is no ordinary object, but an image
349 json.writeStringMember("__class__", "image");
351 json.writeNumberMember("__width__", desc.width);
352 json.writeNumberMember("__height__", desc.height);
353 json.writeNumberMember("__depth__", desc.depth);
355 json.writeStringMember("__format__", enumToString(desc.internalFormat));
357 // Hardcoded for now, but we could chose types more adequate to the
358 // texture internal format
359 json.writeStringMember("__type__", "uint8");
360 json.writeBoolMember("__normalized__", true);
361 json.writeNumberMember("__channels__", 4);
363 GLubyte *pixels = new GLubyte[desc.depth*desc.width*desc.height*4];
365 context.resetPixelPackState();
368 getTexImageOES(target, level, desc, pixels);
370 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
373 context.restorePixelPackState();
375 json.beginMember("__data__");
378 image::writePixelsToBuffer(pixels, desc.width, desc.height, 4, true, &pngBuffer, &pngBufferSize);
379 json.writeBase64(pngBuffer, pngBufferSize);
381 json.endMember(); // __data__
389 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
391 GLint texture_binding = 0;
392 glGetIntegerv(binding, &texture_binding);
393 if (!glIsEnabled(target) && !texture_binding) {
400 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
404 if (target == GL_TEXTURE_CUBE_MAP) {
405 for (int face = 0; face < 6; ++face) {
406 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
409 dumpActiveTextureLevel(json, context, target, level);
418 dumpTextures(JSONWriter &json, Context &context)
420 json.beginMember("textures");
422 GLint active_texture = GL_TEXTURE0;
423 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
424 GLint max_texture_coords = 0;
425 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
426 GLint max_combined_texture_image_units = 0;
427 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
428 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
429 for (GLint unit = 0; unit < max_units; ++unit) {
430 GLenum texture = GL_TEXTURE0 + unit;
431 glActiveTexture(texture);
432 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
433 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
434 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
435 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
436 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
438 glActiveTexture(active_texture);
440 json.endMember(); // textures
445 getDrawableBounds(GLint *width, GLint *height) {
446 #if defined(TRACE_EGL)
448 EGLContext currentContext = eglGetCurrentContext();
449 if (currentContext == EGL_NO_CONTEXT) {
453 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
454 if (currentSurface == EGL_NO_SURFACE) {
458 EGLDisplay currentDisplay = eglGetCurrentDisplay();
459 if (currentDisplay == EGL_NO_DISPLAY) {
463 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
464 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
470 #elif defined(_WIN32)
472 HDC hDC = wglGetCurrentDC();
477 HWND hWnd = WindowFromDC(hDC);
480 if (!GetClientRect(hWnd, &rect)) {
484 *width = rect.right - rect.left;
485 *height = rect.bottom - rect.top;
488 #elif defined(__APPLE__)
490 CGLContextObj ctx = CGLGetCurrentContext();
499 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
505 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
509 *width = rect.size.width;
510 *height = rect.size.height;
513 #elif defined(HAVE_X11)
519 unsigned int w, h, bw, depth;
521 display = glXGetCurrentDisplay();
526 drawable = glXGetCurrentDrawable();
527 if (drawable == None) {
531 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
547 static const GLenum texture_bindings[][2] = {
548 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
549 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
550 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
551 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
552 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
557 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
560 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
561 target = texture_bindings[i][0];
563 GLenum binding = texture_bindings[i][1];
565 while (glGetError() != GL_NO_ERROR)
568 glGetIntegerv(binding, &bound_texture);
569 glBindTexture(target, texture);
571 if (glGetError() == GL_NO_ERROR) {
575 glBindTexture(target, bound_texture);
585 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
588 GLint bound_texture = 0;
589 if (!bindTexture(texture, target, bound_texture)) {
593 getActiveTextureLevelDesc(context, target, level, desc);
595 glBindTexture(target, bound_texture);
602 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
604 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
605 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
608 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
615 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
617 GLint bound_renderbuffer = 0;
618 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
619 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
621 getBoundRenderbufferDesc(context, desc);
623 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
630 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
632 GLint object_type = GL_NONE;
633 glGetFramebufferAttachmentParameteriv(target, attachment,
634 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
636 if (object_type == GL_NONE) {
640 GLint object_name = 0;
641 glGetFramebufferAttachmentParameteriv(target, attachment,
642 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
644 if (object_name == 0) {
648 if (object_type == GL_RENDERBUFFER) {
649 return getRenderbufferDesc(context, object_name, desc);
650 } else if (object_type == GL_TEXTURE) {
651 GLint texture_level = 0;
652 glGetFramebufferAttachmentParameteriv(target, attachment,
653 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
655 return getTextureLevelDesc(context, object_name, texture_level, desc);
657 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
665 getDrawBufferImage() {
666 GLenum format = GL_RGB;
667 GLint channels = __gl_format_channels(format);
674 GLenum framebuffer_binding;
675 GLenum framebuffer_target;
677 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
678 framebuffer_target = GL_FRAMEBUFFER;
680 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
681 framebuffer_target = GL_DRAW_FRAMEBUFFER;
684 GLint draw_framebuffer = 0;
685 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
687 GLint draw_buffer = GL_NONE;
689 if (draw_framebuffer) {
690 if (context.ARB_draw_buffers) {
691 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
692 if (draw_buffer == GL_NONE) {
697 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
702 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
703 if (draw_buffer == GL_NONE) {
708 if (!getDrawableBounds(&desc.width, &desc.height)) {
715 GLenum type = GL_UNSIGNED_BYTE;
718 if (format == GL_DEPTH_COMPONENT) {
719 type = GL_UNSIGNED_INT;
724 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
729 while (glGetError() != GL_NO_ERROR) {}
731 GLint read_framebuffer = 0;
732 GLint read_buffer = GL_NONE;
734 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
735 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
737 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
738 glReadBuffer(draw_buffer);
741 // TODO: reset imaging state too
742 context.resetPixelPackState();
744 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
746 context.restorePixelPackState();
749 glReadBuffer(read_buffer);
750 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
753 GLenum error = glGetError();
754 if (error != GL_NO_ERROR) {
756 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
757 error = glGetError();
758 } while(error != GL_NO_ERROR);
768 * Dump the image of the currently bound read buffer.
771 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
772 GLint internalFormat = GL_NONE)
774 GLint channels = __gl_format_channels(format);
780 // Tell the GUI this is no ordinary object, but an image
781 json.writeStringMember("__class__", "image");
783 json.writeNumberMember("__width__", width);
784 json.writeNumberMember("__height__", height);
785 json.writeNumberMember("__depth__", 1);
787 json.writeStringMember("__format__", enumToString(internalFormat));
789 // Hardcoded for now, but we could chose types more adequate to the
790 // texture internal format
791 json.writeStringMember("__type__", "uint8");
792 json.writeBoolMember("__normalized__", true);
793 json.writeNumberMember("__channels__", channels);
795 GLenum type = GL_UNSIGNED_BYTE;
798 if (format == GL_DEPTH_COMPONENT) {
799 type = GL_UNSIGNED_INT;
804 GLubyte *pixels = new GLubyte[width*height*channels];
806 // TODO: reset imaging state too
807 context.resetPixelPackState();
809 glReadPixels(0, 0, width, height, format, type, pixels);
811 context.restorePixelPackState();
813 json.beginMember("__data__");
816 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
817 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
818 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
819 json.writeBase64(pngBuffer, pngBufferSize);
821 json.endMember(); // __data__
829 downsampledFramebuffer(Context &context,
830 GLuint oldFbo, GLint drawbuffer,
831 GLint colorRb, GLint depthRb, GLint stencilRb,
832 GLuint *rbs, GLint *numRbs)
839 glGenFramebuffers(1, &fbo);
840 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
845 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
846 getBoundRenderbufferDesc(context, desc);
848 glGenRenderbuffers(1, &rbs[*numRbs]);
849 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
850 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
851 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
852 GL_RENDERBUFFER, rbs[*numRbs]);
854 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
855 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
856 glDrawBuffer(drawbuffer);
857 glReadBuffer(drawbuffer);
858 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
859 GL_COLOR_BUFFER_BIT, GL_NEAREST);
860 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
864 if (stencilRb == depthRb && stencilRb) {
865 //combined depth and stencil buffer
867 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
868 getBoundRenderbufferDesc(context, desc);
870 glGenRenderbuffers(1, &rbs[*numRbs]);
871 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
872 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
873 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
874 GL_RENDERBUFFER, rbs[*numRbs]);
875 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
876 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
877 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
878 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
879 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
884 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
885 getBoundRenderbufferDesc(context, desc);
887 glGenRenderbuffers(1, &rbs[*numRbs]);
888 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
889 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
890 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
892 GL_RENDERBUFFER, rbs[*numRbs]);
893 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
894 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
895 glDrawBuffer(GL_DEPTH_ATTACHMENT);
896 glReadBuffer(GL_DEPTH_ATTACHMENT);
897 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
898 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
903 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
904 getBoundRenderbufferDesc(context, desc);
906 glGenRenderbuffers(1, &rbs[*numRbs]);
907 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
908 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
909 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
910 GL_STENCIL_ATTACHMENT,
911 GL_RENDERBUFFER, rbs[*numRbs]);
912 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
913 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
914 glDrawBuffer(GL_STENCIL_ATTACHMENT);
915 glReadBuffer(GL_STENCIL_ATTACHMENT);
916 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
917 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
927 * Dump images of current draw drawable/window.
930 dumpDrawableImages(JSONWriter &json, Context &context)
934 if (!getDrawableBounds(&width, &height)) {
938 GLint draw_buffer = GL_NONE;
940 draw_buffer = GL_BACK;
942 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
943 glReadBuffer(draw_buffer);
946 if (draw_buffer != GL_NONE) {
947 GLint read_buffer = GL_NONE;
949 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
952 GLint alpha_bits = 0;
954 // XXX: Ignore alpha until we are able to match the traced visual
955 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
957 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
958 json.beginMember(enumToString(draw_buffer));
959 dumpReadBufferImage(json, width, height, format);
963 glReadBuffer(read_buffer);
968 GLint depth_bits = 0;
969 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
971 json.beginMember("GL_DEPTH_COMPONENT");
972 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
976 GLint stencil_bits = 0;
977 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
979 json.beginMember("GL_STENCIL_INDEX");
980 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
988 * Dump the specified framebuffer attachment.
990 * In the case of a color attachment, it assumes it is already bound for read.
993 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
996 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1000 json.beginMember(enumToString(attachment));
1001 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1007 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1009 GLint read_framebuffer = 0;
1010 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1012 GLint read_buffer = GL_NONE;
1013 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1015 GLint max_draw_buffers = 1;
1016 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1017 GLint max_color_attachments = 0;
1018 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1020 for (GLint i = 0; i < max_draw_buffers; ++i) {
1021 GLint draw_buffer = GL_NONE;
1022 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1023 if (draw_buffer != GL_NONE) {
1024 glReadBuffer(draw_buffer);
1026 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1027 attachment = draw_buffer;
1029 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1030 attachment = GL_COLOR_ATTACHMENT0;
1032 GLint alpha_size = 0;
1033 glGetFramebufferAttachmentParameteriv(target, attachment,
1034 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1036 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1037 dumpFramebufferAttachment(json, context, target, attachment, format);
1041 glReadBuffer(read_buffer);
1044 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1045 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1048 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1053 dumpFramebuffer(JSONWriter &json, Context &context)
1055 json.beginMember("framebuffer");
1058 GLint boundDrawFbo = 0, boundReadFbo = 0;
1059 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1060 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1061 if (!boundDrawFbo) {
1062 dumpDrawableImages(json, context);
1063 } else if (context.ES) {
1064 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1066 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1067 GLint draw_buffer0 = GL_NONE;
1068 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1069 bool multisample = false;
1072 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1075 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1076 if (object_type == GL_RENDERBUFFER) {
1077 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1078 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1080 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1086 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1087 if (object_type == GL_RENDERBUFFER) {
1088 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1089 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1091 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1097 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1098 if (object_type == GL_RENDERBUFFER) {
1099 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1100 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1102 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1108 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1115 // glReadPixels doesnt support multisampled buffers so we need
1116 // to blit the fbo to a temporary one
1117 fboCopy = downsampledFramebuffer(context,
1118 boundDrawFbo, draw_buffer0,
1119 colorRb, depthRb, stencilRb,
1123 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1126 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1127 glDeleteRenderbuffers(numRbs, rbs);
1128 glDeleteFramebuffers(1, &fboCopy);
1131 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1132 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1136 json.endMember(); // framebuffer
1140 } /* namespace glstate */