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"
46 #include <Carbon/Carbon.h>
52 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
58 #endif /* __APPLE__ */
61 /* Change thi to one to force interpreting depth buffers as RGBA, which enables
62 * visualizing full dynamic range, until we can transmit HDR images to the GUI */
63 #define DEPTH_AS_RGBA 0
81 internalFormat(GL_NONE)
86 return width > 0 && height > 0 && depth > 0;
92 * OpenGL ES does not support glGetTexLevelParameteriv, but it is possible to
93 * probe whether a texture has a given size by crafting a dummy glTexSubImage()
97 probeTextureLevelSizeOES(GLenum target, GLint level, const GLint size[3]) {
98 while (glGetError() != GL_NO_ERROR)
101 GLenum internalFormat = GL_RGBA;
102 GLenum type = GL_UNSIGNED_BYTE;
107 case GL_TEXTURE_CUBE_MAP:
108 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
109 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
110 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
111 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
112 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
113 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
114 glTexSubImage2D(target, level, size[0], size[1], 0, 0, internalFormat, type, &dummy);
116 case GL_TEXTURE_3D_OES:
117 glTexSubImage3DOES(target, level, size[0], size[1], size[2], 0, 0, 0, internalFormat, type, &dummy);
123 GLenum error = glGetError();
126 std::cerr << "(" << size[0] << ", " << size[1] << ", " << size[2] << ") = " << enumToString(error) << "\n";
129 if (error == GL_NO_ERROR) {
133 while (glGetError() != GL_NO_ERROR)
141 * Bisect the texture size along an axis.
143 * It is assumed that the texture exists.
146 bisectTextureLevelSizeOES(GLenum target, GLint level, GLint axis, GLint max) {
147 GLint size[3] = {0, 0, 0};
154 GLint test = (min + max) / 2;
161 if (probeTextureLevelSizeOES(target, level, size)) {
171 * Special path to obtain texture size on OpenGL ES, that does not rely on
172 * glGetTexLevelParameteriv
175 getActiveTextureLevelDescOES(Context &context, GLenum target, GLint level, ImageDesc &desc)
177 if (target == GL_TEXTURE_1D) {
178 // OpenGL ES does not support 1D textures
182 const GLint size[3] = {1, 1, 1};
183 if (!probeTextureLevelSizeOES(target, level, size)) {
188 desc.internalFormat = GL_RGBA;
193 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
194 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
195 desc.height = bisectTextureLevelSizeOES(target, level, 1, maxSize);
198 case GL_TEXTURE_CUBE_MAP:
199 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
200 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
201 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
202 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
203 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
204 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
205 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxSize);
206 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
207 desc.height = desc.width;
210 case GL_TEXTURE_3D_OES:
211 glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_OES, &maxSize);
212 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
213 desc.width = bisectTextureLevelSizeOES(target, level, 1, maxSize);
214 desc.depth = bisectTextureLevelSizeOES(target, level, 2, maxSize);
222 << enumToString(target) << " "
224 << desc.width << "x" << desc.height << "x" << desc.depth
233 getActiveTextureLevelDesc(Context &context, GLenum target, GLint level, ImageDesc &desc)
236 return getActiveTextureLevelDescOES(context, target, level, desc);
239 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &desc.internalFormat);
242 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &desc.width);
244 if (target == GL_TEXTURE_1D) {
249 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &desc.height);
250 if (target != GL_TEXTURE_3D) {
254 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &desc.depth);
263 * OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
264 * texture to a framebuffer.
267 getTexImageOES(GLenum target, GLint level, ImageDesc &desc, GLubyte *pixels)
269 memset(pixels, 0x80, desc.height * desc.width * 4);
271 GLenum texture_binding = GL_NONE;
274 texture_binding = GL_TEXTURE_BINDING_2D;
276 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
277 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
278 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
279 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
280 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
281 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
282 texture_binding = GL_TEXTURE_BINDING_CUBE_MAP;
284 case GL_TEXTURE_3D_OES:
285 texture_binding = GL_TEXTURE_BINDING_3D_OES;
291 glGetIntegerv(texture_binding, &texture);
298 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
299 glGenFramebuffers(1, &fbo);
300 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
306 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
307 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
308 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
309 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
310 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
311 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
312 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
313 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
314 if (status != GL_FRAMEBUFFER_COMPLETE) {
315 std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
317 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
319 case GL_TEXTURE_3D_OES:
320 for (int i = 0; i < desc.depth; i++) {
321 glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
322 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * desc.width * desc.height);
327 glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);
329 glDeleteFramebuffers(1, &fbo);
334 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
337 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
343 GLint active_texture = GL_TEXTURE0;
344 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
345 snprintf(label, sizeof label, "%s, %s, level = %d",
346 enumToString(active_texture), enumToString(target), level);
348 json.beginMember(label);
352 // Tell the GUI this is no ordinary object, but an image
353 json.writeStringMember("__class__", "image");
355 json.writeNumberMember("__width__", desc.width);
356 json.writeNumberMember("__height__", desc.height);
357 json.writeNumberMember("__depth__", desc.depth);
359 json.writeStringMember("__format__", enumToString(desc.internalFormat));
361 // Hardcoded for now, but we could chose types more adequate to the
362 // texture internal format
363 json.writeStringMember("__type__", "uint8");
364 json.writeBoolMember("__normalized__", true);
365 json.writeNumberMember("__channels__", 4);
367 GLubyte *pixels = new GLubyte[desc.depth*desc.width*desc.height*4];
369 context.resetPixelPackState();
372 getTexImageOES(target, level, desc, pixels);
374 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
377 context.restorePixelPackState();
379 json.beginMember("__data__");
382 image::writePixelsToBuffer(pixels, desc.width, desc.height, 4, true, &pngBuffer, &pngBufferSize);
383 json.writeBase64(pngBuffer, pngBufferSize);
385 json.endMember(); // __data__
393 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
395 GLint texture_binding = 0;
396 glGetIntegerv(binding, &texture_binding);
397 if (!glIsEnabled(target) && !texture_binding) {
404 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
408 if (target == GL_TEXTURE_CUBE_MAP) {
409 for (int face = 0; face < 6; ++face) {
410 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
413 dumpActiveTextureLevel(json, context, target, level);
422 dumpTextures(JSONWriter &json, Context &context)
424 json.beginMember("textures");
426 GLint active_texture = GL_TEXTURE0;
427 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
428 GLint max_texture_coords = 0;
429 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
430 GLint max_combined_texture_image_units = 0;
431 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
432 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
433 for (GLint unit = 0; unit < max_units; ++unit) {
434 GLenum texture = GL_TEXTURE0 + unit;
435 glActiveTexture(texture);
436 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
437 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
438 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
439 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
440 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
442 glActiveTexture(active_texture);
444 json.endMember(); // textures
449 getDrawableBounds(GLint *width, GLint *height) {
450 #if defined(__linux__)
451 if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) {
452 EGLContext currentContext = eglGetCurrentContext();
453 if (currentContext == EGL_NO_CONTEXT) {
457 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
458 if (currentSurface == EGL_NO_SURFACE) {
462 EGLDisplay currentDisplay = eglGetCurrentDisplay();
463 if (currentDisplay == EGL_NO_DISPLAY) {
467 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
468 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
478 HDC hDC = wglGetCurrentDC();
483 HWND hWnd = WindowFromDC(hDC);
486 if (!GetClientRect(hWnd, &rect)) {
490 *width = rect.right - rect.left;
491 *height = rect.bottom - rect.top;
494 #elif defined(__APPLE__)
496 CGLContextObj ctx = CGLGetCurrentContext();
505 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
511 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
515 *width = rect.size.width;
516 *height = rect.size.height;
519 #elif defined(HAVE_X11)
525 unsigned int w, h, bw, depth;
527 display = glXGetCurrentDisplay();
532 drawable = glXGetCurrentDrawable();
533 if (drawable == None) {
537 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
553 static const GLenum texture_bindings[][2] = {
554 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
555 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
556 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
557 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
558 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
563 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
566 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
567 target = texture_bindings[i][0];
569 GLenum binding = texture_bindings[i][1];
571 while (glGetError() != GL_NO_ERROR)
574 glGetIntegerv(binding, &bound_texture);
575 glBindTexture(target, texture);
577 if (glGetError() == GL_NO_ERROR) {
581 glBindTexture(target, bound_texture);
591 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
594 GLint bound_texture = 0;
595 if (!bindTexture(texture, target, bound_texture)) {
599 getActiveTextureLevelDesc(context, target, level, desc);
601 glBindTexture(target, bound_texture);
608 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
610 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
611 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
614 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
621 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
623 GLint bound_renderbuffer = 0;
624 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
625 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
627 getBoundRenderbufferDesc(context, desc);
629 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
636 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
638 GLint object_type = GL_NONE;
639 glGetFramebufferAttachmentParameteriv(target, attachment,
640 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
642 if (object_type == GL_NONE) {
646 GLint object_name = 0;
647 glGetFramebufferAttachmentParameteriv(target, attachment,
648 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
650 if (object_name == 0) {
654 if (object_type == GL_RENDERBUFFER) {
655 return getRenderbufferDesc(context, object_name, desc);
656 } else if (object_type == GL_TEXTURE) {
657 GLint texture_level = 0;
658 glGetFramebufferAttachmentParameteriv(target, attachment,
659 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
661 return getTextureLevelDesc(context, object_name, texture_level, desc);
663 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
671 getDrawBufferImage() {
672 GLenum format = GL_RGB;
673 GLint channels = __gl_format_channels(format);
680 GLenum framebuffer_binding;
681 GLenum framebuffer_target;
683 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
684 framebuffer_target = GL_FRAMEBUFFER;
686 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
687 framebuffer_target = GL_DRAW_FRAMEBUFFER;
690 GLint draw_framebuffer = 0;
691 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
693 GLint draw_buffer = GL_NONE;
695 if (draw_framebuffer) {
696 if (context.ARB_draw_buffers) {
697 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
698 if (draw_buffer == GL_NONE) {
703 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
708 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
709 if (draw_buffer == GL_NONE) {
714 if (!getDrawableBounds(&desc.width, &desc.height)) {
721 GLenum type = GL_UNSIGNED_BYTE;
724 if (format == GL_DEPTH_COMPONENT) {
725 type = GL_UNSIGNED_INT;
730 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
735 while (glGetError() != GL_NO_ERROR) {}
737 GLint read_framebuffer = 0;
738 GLint read_buffer = GL_NONE;
740 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
741 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
743 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
744 glReadBuffer(draw_buffer);
747 // TODO: reset imaging state too
748 context.resetPixelPackState();
750 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
752 context.restorePixelPackState();
755 glReadBuffer(read_buffer);
756 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
759 GLenum error = glGetError();
760 if (error != GL_NO_ERROR) {
762 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
763 error = glGetError();
764 } while(error != GL_NO_ERROR);
774 * Dump the image of the currently bound read buffer.
777 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
778 GLint internalFormat = GL_NONE)
780 GLint channels = __gl_format_channels(format);
786 // Tell the GUI this is no ordinary object, but an image
787 json.writeStringMember("__class__", "image");
789 json.writeNumberMember("__width__", width);
790 json.writeNumberMember("__height__", height);
791 json.writeNumberMember("__depth__", 1);
793 json.writeStringMember("__format__", enumToString(internalFormat));
795 // Hardcoded for now, but we could chose types more adequate to the
796 // texture internal format
797 json.writeStringMember("__type__", "uint8");
798 json.writeBoolMember("__normalized__", true);
799 json.writeNumberMember("__channels__", channels);
801 GLenum type = GL_UNSIGNED_BYTE;
804 if (format == GL_DEPTH_COMPONENT) {
805 type = GL_UNSIGNED_INT;
810 GLubyte *pixels = new GLubyte[width*height*channels];
812 // TODO: reset imaging state too
813 context.resetPixelPackState();
815 glReadPixels(0, 0, width, height, format, type, pixels);
817 context.restorePixelPackState();
819 json.beginMember("__data__");
822 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
823 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
824 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
825 json.writeBase64(pngBuffer, pngBufferSize);
827 json.endMember(); // __data__
835 downsampledFramebuffer(Context &context,
836 GLuint oldFbo, GLint drawbuffer,
837 GLint colorRb, GLint depthRb, GLint stencilRb,
838 GLuint *rbs, GLint *numRbs)
845 glGenFramebuffers(1, &fbo);
846 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
851 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
852 getBoundRenderbufferDesc(context, desc);
854 glGenRenderbuffers(1, &rbs[*numRbs]);
855 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
856 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
857 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
858 GL_RENDERBUFFER, rbs[*numRbs]);
860 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
861 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
862 glDrawBuffer(drawbuffer);
863 glReadBuffer(drawbuffer);
864 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
865 GL_COLOR_BUFFER_BIT, GL_NEAREST);
866 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
870 if (stencilRb == depthRb && stencilRb) {
871 //combined depth and stencil buffer
873 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
874 getBoundRenderbufferDesc(context, desc);
876 glGenRenderbuffers(1, &rbs[*numRbs]);
877 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
878 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
879 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
880 GL_RENDERBUFFER, rbs[*numRbs]);
881 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
882 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
883 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
884 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
885 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
890 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
891 getBoundRenderbufferDesc(context, desc);
893 glGenRenderbuffers(1, &rbs[*numRbs]);
894 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
895 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
896 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
898 GL_RENDERBUFFER, rbs[*numRbs]);
899 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
900 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
901 glDrawBuffer(GL_DEPTH_ATTACHMENT);
902 glReadBuffer(GL_DEPTH_ATTACHMENT);
903 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
904 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
909 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
910 getBoundRenderbufferDesc(context, desc);
912 glGenRenderbuffers(1, &rbs[*numRbs]);
913 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
914 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
915 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
916 GL_STENCIL_ATTACHMENT,
917 GL_RENDERBUFFER, rbs[*numRbs]);
918 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
919 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
920 glDrawBuffer(GL_STENCIL_ATTACHMENT);
921 glReadBuffer(GL_STENCIL_ATTACHMENT);
922 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
923 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
933 * Dump images of current draw drawable/window.
936 dumpDrawableImages(JSONWriter &json, Context &context)
940 if (!getDrawableBounds(&width, &height)) {
944 GLint draw_buffer = GL_NONE;
946 draw_buffer = GL_BACK;
948 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
949 glReadBuffer(draw_buffer);
952 if (draw_buffer != GL_NONE) {
953 GLint read_buffer = GL_NONE;
955 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
958 GLint alpha_bits = 0;
960 // XXX: Ignore alpha until we are able to match the traced visual
961 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
963 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
964 json.beginMember(enumToString(draw_buffer));
965 dumpReadBufferImage(json, width, height, format);
969 glReadBuffer(read_buffer);
974 GLint depth_bits = 0;
975 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
977 json.beginMember("GL_DEPTH_COMPONENT");
978 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
982 GLint stencil_bits = 0;
983 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
985 json.beginMember("GL_STENCIL_INDEX");
986 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
994 * Dump the specified framebuffer attachment.
996 * In the case of a color attachment, it assumes it is already bound for read.
999 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
1002 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1006 json.beginMember(enumToString(attachment));
1007 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1013 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1015 GLint read_framebuffer = 0;
1016 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1018 GLint read_buffer = GL_NONE;
1019 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1021 GLint max_draw_buffers = 1;
1022 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1023 GLint max_color_attachments = 0;
1024 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1026 for (GLint i = 0; i < max_draw_buffers; ++i) {
1027 GLint draw_buffer = GL_NONE;
1028 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1029 if (draw_buffer != GL_NONE) {
1030 glReadBuffer(draw_buffer);
1032 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1033 attachment = draw_buffer;
1035 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1036 attachment = GL_COLOR_ATTACHMENT0;
1038 GLint alpha_size = 0;
1039 glGetFramebufferAttachmentParameteriv(target, attachment,
1040 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1042 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1043 dumpFramebufferAttachment(json, context, target, attachment, format);
1047 glReadBuffer(read_buffer);
1050 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1051 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1054 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1059 dumpFramebuffer(JSONWriter &json, Context &context)
1061 json.beginMember("framebuffer");
1064 GLint boundDrawFbo = 0, boundReadFbo = 0;
1065 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1066 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1067 if (!boundDrawFbo) {
1068 dumpDrawableImages(json, context);
1069 } else if (context.ES) {
1070 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1072 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1073 GLint draw_buffer0 = GL_NONE;
1074 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1075 bool multisample = false;
1078 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1081 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1082 if (object_type == GL_RENDERBUFFER) {
1083 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1084 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1086 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1092 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1093 if (object_type == GL_RENDERBUFFER) {
1094 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1095 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1097 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1103 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1104 if (object_type == GL_RENDERBUFFER) {
1105 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1106 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1108 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1114 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1121 // glReadPixels doesnt support multisampled buffers so we need
1122 // to blit the fbo to a temporary one
1123 fboCopy = downsampledFramebuffer(context,
1124 boundDrawFbo, draw_buffer0,
1125 colorRb, depthRb, stencilRb,
1129 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1132 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1133 glDeleteRenderbuffers(numRbs, rbs);
1134 glDeleteFramebuffers(1, &fboCopy);
1137 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1138 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1142 json.endMember(); // framebuffer
1146 } /* namespace glstate */