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 **************************************************************************/
37 #include "glstate.hpp"
38 #include "glstate_internal.hpp"
47 #include <Carbon/Carbon.h>
53 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
59 #endif /* __APPLE__ */
62 /* Change thi to one to force interpreting depth buffers as RGBA, which enables
63 * visualizing full dynamic range, until we can transmit HDR images to the GUI */
64 #define DEPTH_AS_RGBA 0
82 internalFormat(GL_NONE)
87 return width > 0 && height > 0 && depth > 0;
93 * Sames as enumToString, but with special provision to handle formatsLUMINANCE_ALPHA.
95 * OpenGL 2.1 specification states that "internalFormat may (for backwards
96 * compatibility with the 1.0 version of the GL) also take on the integer
97 * values 1, 2, 3, and 4, which are equivalent to symbolic constants LUMINANCE,
98 * LUMINANCE ALPHA, RGB, and RGBA respectively".
101 formatToString(GLenum internalFormat) {
102 switch (internalFormat) {
104 return "GL_LUMINANCE";
106 return "GL_LUMINANCE_ALPHA";
112 return enumToString(internalFormat);
118 * OpenGL ES does not support glGetTexLevelParameteriv, but it is possible to
119 * probe whether a texture has a given size by crafting a dummy glTexSubImage()
123 probeTextureLevelSizeOES(GLenum target, GLint level, const GLint size[3]) {
124 while (glGetError() != GL_NO_ERROR)
127 GLenum internalFormat = GL_RGBA;
128 GLenum type = GL_UNSIGNED_BYTE;
133 case GL_TEXTURE_CUBE_MAP:
134 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
135 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
136 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
137 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
138 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
139 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
140 glTexSubImage2D(target, level, size[0], size[1], 0, 0, internalFormat, type, &dummy);
142 case GL_TEXTURE_3D_OES:
143 glTexSubImage3DOES(target, level, size[0], size[1], size[2], 0, 0, 0, internalFormat, type, &dummy);
149 GLenum error = glGetError();
152 std::cerr << "(" << size[0] << ", " << size[1] << ", " << size[2] << ") = " << enumToString(error) << "\n";
155 if (error == GL_NO_ERROR) {
159 while (glGetError() != GL_NO_ERROR)
167 * Bisect the texture size along an axis.
169 * It is assumed that the texture exists.
172 bisectTextureLevelSizeOES(GLenum target, GLint level, GLint axis, GLint max) {
173 GLint size[3] = {0, 0, 0};
180 GLint test = (min + max) / 2;
187 if (probeTextureLevelSizeOES(target, level, size)) {
197 * Special path to obtain texture size on OpenGL ES, that does not rely on
198 * glGetTexLevelParameteriv
201 getActiveTextureLevelDescOES(Context &context, GLenum target, GLint level, ImageDesc &desc)
203 if (target == GL_TEXTURE_1D) {
204 // OpenGL ES does not support 1D textures
208 const GLint size[3] = {1, 1, 1};
209 if (!probeTextureLevelSizeOES(target, level, size)) {
214 desc.internalFormat = GL_RGBA;
219 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
220 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
221 desc.height = bisectTextureLevelSizeOES(target, level, 1, maxSize);
224 case GL_TEXTURE_CUBE_MAP:
225 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
226 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
227 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
228 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
229 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
230 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
231 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxSize);
232 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
233 desc.height = desc.width;
236 case GL_TEXTURE_3D_OES:
237 glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_OES, &maxSize);
238 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
239 desc.width = bisectTextureLevelSizeOES(target, level, 1, maxSize);
240 desc.depth = bisectTextureLevelSizeOES(target, level, 2, maxSize);
248 << enumToString(target) << " "
250 << desc.width << "x" << desc.height << "x" << desc.depth
259 getActiveTextureLevelDesc(Context &context, GLenum target, GLint level, ImageDesc &desc)
261 assert(target != GL_TEXTURE_CUBE_MAP);
264 return getActiveTextureLevelDescOES(context, target, level, desc);
267 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &desc.internalFormat);
270 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &desc.width);
272 if (target == GL_TEXTURE_1D) {
277 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &desc.height);
278 if (target != GL_TEXTURE_3D) {
282 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &desc.depth);
291 * OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
292 * texture to a framebuffer.
295 getTexImageOES(GLenum target, GLint level, ImageDesc &desc, GLubyte *pixels)
297 memset(pixels, 0x80, desc.height * desc.width * 4);
299 GLenum texture_binding = GL_NONE;
302 texture_binding = GL_TEXTURE_BINDING_2D;
304 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
305 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
306 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
307 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
308 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
309 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
310 texture_binding = GL_TEXTURE_BINDING_CUBE_MAP;
312 case GL_TEXTURE_3D_OES:
313 texture_binding = GL_TEXTURE_BINDING_3D_OES;
319 glGetIntegerv(texture_binding, &texture);
326 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
327 glGenFramebuffers(1, &fbo);
328 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
334 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
335 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
336 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
337 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
338 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
339 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
340 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
341 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
342 if (status != GL_FRAMEBUFFER_COMPLETE) {
343 std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
345 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
347 case GL_TEXTURE_3D_OES:
348 for (int i = 0; i < desc.depth; i++) {
349 glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
350 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * desc.width * desc.height);
355 glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);
357 glDeleteFramebuffers(1, &fbo);
361 static inline GLboolean
362 isDepthFormat(GLenum internalFormat)
364 switch (internalFormat) {
365 case GL_DEPTH_COMPONENT:
366 case GL_DEPTH_COMPONENT16:
367 case GL_DEPTH_COMPONENT24:
368 case GL_DEPTH_COMPONENT32:
369 case GL_DEPTH_COMPONENT32F:
370 case GL_DEPTH_COMPONENT32F_NV:
371 case GL_DEPTH_STENCIL:
372 case GL_DEPTH24_STENCIL8:
373 case GL_DEPTH32F_STENCIL8:
374 case GL_DEPTH32F_STENCIL8_NV:
382 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
385 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
391 GLint active_texture = GL_TEXTURE0;
392 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
393 snprintf(label, sizeof label, "%s, %s, level = %d",
394 enumToString(active_texture), enumToString(target), level);
396 json.beginMember(label);
402 if (!context.ES && isDepthFormat(desc.internalFormat)) {
403 format = GL_DEPTH_COMPONENT;
410 // Tell the GUI this is no ordinary object, but an image
411 json.writeStringMember("__class__", "image");
413 json.writeIntMember("__width__", desc.width);
414 json.writeIntMember("__height__", desc.height);
415 json.writeIntMember("__depth__", desc.depth);
417 json.writeStringMember("__format__", formatToString(desc.internalFormat));
419 image::Image *image = new image::Image(desc.width, desc.height*desc.depth, channels, true);
421 context.resetPixelPackState();
424 getTexImageOES(target, level, desc, image->pixels);
426 glGetTexImage(target, level, format, GL_UNSIGNED_BYTE, image->pixels);
429 context.restorePixelPackState();
431 json.beginMember("__data__");
432 std::stringstream ss;
434 const std::string & s = ss.str();
435 json.writeBase64(s.data(), s.size());
436 json.endMember(); // __data__
444 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
446 GLint texture_binding = 0;
447 glGetIntegerv(binding, &texture_binding);
448 if (!glIsEnabled(target) && !texture_binding) {
456 if (target == GL_TEXTURE_CUBE_MAP) {
457 for (int face = 0; face < 6; ++face) {
458 if (!getActiveTextureLevelDesc(context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, desc)) {
461 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
464 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
467 dumpActiveTextureLevel(json, context, target, level);
476 dumpTextures(JSONWriter &json, Context &context)
478 json.beginMember("textures");
480 GLint active_texture = GL_TEXTURE0;
481 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
483 GLint max_texture_coords = 0;
484 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
485 GLint max_combined_texture_image_units = 0;
486 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
487 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
490 * At least the Android software GL implementation doesn't return the
491 * proper value for this, but rather returns 0. The GL(ES) specification
492 * mandates a minimum value of 2, so use this as a fall-back value.
494 max_units = std::max(max_units, 2);
496 for (GLint unit = 0; unit < max_units; ++unit) {
497 GLenum texture = GL_TEXTURE0 + unit;
498 glActiveTexture(texture);
499 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
500 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
501 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
502 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
503 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
505 glActiveTexture(active_texture);
507 json.endMember(); // textures
512 getDrawableBounds(GLint *width, GLint *height) {
513 #if defined(__linux__)
514 if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) {
515 EGLContext currentContext = eglGetCurrentContext();
516 if (currentContext == EGL_NO_CONTEXT) {
520 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
521 if (currentSurface == EGL_NO_SURFACE) {
525 EGLDisplay currentDisplay = eglGetCurrentDisplay();
526 if (currentDisplay == EGL_NO_DISPLAY) {
530 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
531 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
541 HDC hDC = wglGetCurrentDC();
546 HWND hWnd = WindowFromDC(hDC);
549 if (!GetClientRect(hWnd, &rect)) {
553 *width = rect.right - rect.left;
554 *height = rect.bottom - rect.top;
557 #elif defined(__APPLE__)
559 CGLContextObj ctx = CGLGetCurrentContext();
568 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
574 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
578 *width = rect.size.width;
579 *height = rect.size.height;
582 #elif defined(HAVE_X11)
588 unsigned int w, h, bw, depth;
590 display = glXGetCurrentDisplay();
595 drawable = glXGetCurrentDrawable();
596 if (drawable == None) {
600 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
616 static const GLenum texture_bindings[][2] = {
617 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
618 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
619 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
620 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
621 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
626 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
629 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
630 target = texture_bindings[i][0];
632 GLenum binding = texture_bindings[i][1];
634 while (glGetError() != GL_NO_ERROR)
637 glGetIntegerv(binding, &bound_texture);
638 glBindTexture(target, texture);
640 if (glGetError() == GL_NO_ERROR) {
644 glBindTexture(target, bound_texture);
654 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
657 GLint bound_texture = 0;
658 if (!bindTexture(texture, target, bound_texture)) {
662 getActiveTextureLevelDesc(context, target, level, desc);
664 glBindTexture(target, bound_texture);
671 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
673 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
674 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
677 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
684 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
686 GLint bound_renderbuffer = 0;
687 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
688 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
690 getBoundRenderbufferDesc(context, desc);
692 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
699 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
701 GLint object_type = GL_NONE;
702 glGetFramebufferAttachmentParameteriv(target, attachment,
703 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
705 if (object_type == GL_NONE) {
709 GLint object_name = 0;
710 glGetFramebufferAttachmentParameteriv(target, attachment,
711 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
713 if (object_name == 0) {
717 if (object_type == GL_RENDERBUFFER) {
718 return getRenderbufferDesc(context, object_name, desc);
719 } else if (object_type == GL_TEXTURE) {
720 GLint texture_level = 0;
721 glGetFramebufferAttachmentParameteriv(target, attachment,
722 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
724 return getTextureLevelDesc(context, object_name, texture_level, desc);
726 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
734 getDrawBufferImage() {
735 GLenum format = GL_RGB;
736 GLint channels = _gl_format_channels(format);
743 GLenum framebuffer_binding;
744 GLenum framebuffer_target;
746 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
747 framebuffer_target = GL_FRAMEBUFFER;
749 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
750 framebuffer_target = GL_DRAW_FRAMEBUFFER;
753 GLint draw_framebuffer = 0;
754 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
756 GLint draw_buffer = GL_NONE;
758 if (draw_framebuffer) {
759 if (context.ARB_draw_buffers) {
760 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
761 if (draw_buffer == GL_NONE) {
766 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
771 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
772 if (draw_buffer == GL_NONE) {
777 if (!getDrawableBounds(&desc.width, &desc.height)) {
784 GLenum type = GL_UNSIGNED_BYTE;
787 if (format == GL_DEPTH_COMPONENT) {
788 type = GL_UNSIGNED_INT;
793 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
798 while (glGetError() != GL_NO_ERROR) {}
800 GLint read_framebuffer = 0;
801 GLint read_buffer = GL_NONE;
803 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
804 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
806 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
807 glReadBuffer(draw_buffer);
810 // TODO: reset imaging state too
811 context.resetPixelPackState();
813 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
815 context.restorePixelPackState();
818 glReadBuffer(read_buffer);
819 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
822 GLenum error = glGetError();
823 if (error != GL_NO_ERROR) {
825 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
826 error = glGetError();
827 } while(error != GL_NO_ERROR);
837 * Dump the image of the currently bound read buffer.
840 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
841 GLint internalFormat = GL_NONE)
843 GLint channels = _gl_format_channels(format);
845 if (internalFormat == GL_NONE) {
846 internalFormat = format;
853 // Tell the GUI this is no ordinary object, but an image
854 json.writeStringMember("__class__", "image");
856 json.writeIntMember("__width__", width);
857 json.writeIntMember("__height__", height);
858 json.writeIntMember("__depth__", 1);
860 json.writeStringMember("__format__", formatToString(internalFormat));
862 GLenum type = GL_UNSIGNED_BYTE;
865 if (format == GL_DEPTH_COMPONENT) {
866 type = GL_UNSIGNED_INT;
871 image::Image *image = new image::Image(width, height, channels, true);
873 // TODO: reset imaging state too
874 context.resetPixelPackState();
876 glReadPixels(0, 0, width, height, format, type, image->pixels);
878 context.restorePixelPackState();
880 json.beginMember("__data__");
881 std::stringstream ss;
883 const std::string & s = ss.str();
884 json.writeBase64(s.data(), s.size());
885 json.endMember(); // __data__
893 downsampledFramebuffer(Context &context,
894 GLuint oldFbo, GLint drawbuffer,
895 GLint colorRb, GLint depthRb, GLint stencilRb,
896 GLuint *rbs, GLint *numRbs)
903 glGenFramebuffers(1, &fbo);
904 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
909 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
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, drawbuffer,
916 GL_RENDERBUFFER, rbs[*numRbs]);
918 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
919 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
920 glDrawBuffer(drawbuffer);
921 glReadBuffer(drawbuffer);
922 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
923 GL_COLOR_BUFFER_BIT, GL_NEAREST);
924 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
928 if (stencilRb == depthRb && stencilRb) {
929 //combined depth and stencil buffer
931 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
932 getBoundRenderbufferDesc(context, desc);
934 glGenRenderbuffers(1, &rbs[*numRbs]);
935 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
936 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
937 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
938 GL_RENDERBUFFER, rbs[*numRbs]);
939 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
940 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
941 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
942 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
943 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
948 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
949 getBoundRenderbufferDesc(context, desc);
951 glGenRenderbuffers(1, &rbs[*numRbs]);
952 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
953 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
954 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
956 GL_RENDERBUFFER, rbs[*numRbs]);
957 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
958 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
959 glDrawBuffer(GL_DEPTH_ATTACHMENT);
960 glReadBuffer(GL_DEPTH_ATTACHMENT);
961 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
962 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
967 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
968 getBoundRenderbufferDesc(context, desc);
970 glGenRenderbuffers(1, &rbs[*numRbs]);
971 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
972 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
973 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
974 GL_STENCIL_ATTACHMENT,
975 GL_RENDERBUFFER, rbs[*numRbs]);
976 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
977 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
978 glDrawBuffer(GL_STENCIL_ATTACHMENT);
979 glReadBuffer(GL_STENCIL_ATTACHMENT);
980 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
981 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
991 * Dump images of current draw drawable/window.
994 dumpDrawableImages(JSONWriter &json, Context &context)
998 if (!getDrawableBounds(&width, &height)) {
1002 GLint draw_buffer = GL_NONE;
1004 draw_buffer = GL_BACK;
1006 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1007 glReadBuffer(draw_buffer);
1010 if (draw_buffer != GL_NONE) {
1011 GLint read_buffer = GL_NONE;
1013 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1016 GLint alpha_bits = 0;
1018 // XXX: Ignore alpha until we are able to match the traced visual
1019 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1021 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1022 json.beginMember(enumToString(draw_buffer));
1023 dumpReadBufferImage(json, width, height, format);
1027 glReadBuffer(read_buffer);
1032 GLint depth_bits = 0;
1033 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1035 json.beginMember("GL_DEPTH_COMPONENT");
1036 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1040 GLint stencil_bits = 0;
1041 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1043 json.beginMember("GL_STENCIL_INDEX");
1044 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1052 * Dump the specified framebuffer attachment.
1054 * In the case of a color attachment, it assumes it is already bound for read.
1057 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
1060 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1064 json.beginMember(enumToString(attachment));
1065 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1071 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1073 GLint read_framebuffer = 0;
1074 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1076 GLint read_buffer = GL_NONE;
1077 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1079 GLint max_draw_buffers = 1;
1080 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1081 GLint max_color_attachments = 0;
1082 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1084 for (GLint i = 0; i < max_draw_buffers; ++i) {
1085 GLint draw_buffer = GL_NONE;
1086 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1087 if (draw_buffer != GL_NONE) {
1088 glReadBuffer(draw_buffer);
1090 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1091 attachment = draw_buffer;
1093 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1094 attachment = GL_COLOR_ATTACHMENT0;
1096 GLint alpha_size = 0;
1097 glGetFramebufferAttachmentParameteriv(target, attachment,
1098 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1100 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1101 dumpFramebufferAttachment(json, context, target, attachment, format);
1105 glReadBuffer(read_buffer);
1108 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1109 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1112 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1117 dumpFramebuffer(JSONWriter &json, Context &context)
1119 json.beginMember("framebuffer");
1122 GLint boundDrawFbo = 0, boundReadFbo = 0;
1123 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1124 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1125 if (!boundDrawFbo) {
1126 dumpDrawableImages(json, context);
1127 } else if (context.ES) {
1128 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1130 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1131 GLint draw_buffer0 = GL_NONE;
1132 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1133 bool multisample = false;
1136 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1139 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1140 if (object_type == GL_RENDERBUFFER) {
1141 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1142 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1144 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1150 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1151 if (object_type == GL_RENDERBUFFER) {
1152 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1153 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1155 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1161 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1162 if (object_type == GL_RENDERBUFFER) {
1163 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1164 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1166 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1172 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1179 // glReadPixels doesnt support multisampled buffers so we need
1180 // to blit the fbo to a temporary one
1181 fboCopy = downsampledFramebuffer(context,
1182 boundDrawFbo, draw_buffer0,
1183 colorRb, depthRb, stencilRb,
1187 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1190 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1191 glDeleteRenderbuffers(numRbs, rbs);
1192 glDeleteFramebuffers(1, &fboCopy);
1195 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1196 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1200 json.endMember(); // framebuffer
1204 } /* namespace glstate */