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 getTextureBinding(GLenum target)
295 return GL_TEXTURE_BINDING_1D;
296 case GL_TEXTURE_1D_ARRAY:
297 return GL_TEXTURE_BINDING_1D_ARRAY;
299 return GL_TEXTURE_BINDING_2D;
300 case GL_TEXTURE_2D_MULTISAMPLE:
301 return GL_TEXTURE_BINDING_2D_MULTISAMPLE;
302 case GL_TEXTURE_2D_ARRAY:
303 return GL_TEXTURE_BINDING_2D_ARRAY;
304 case GL_TEXTURE_RECTANGLE:
305 return GL_TEXTURE_BINDING_RECTANGLE;
306 case GL_TEXTURE_CUBE_MAP:
307 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
308 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
309 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
310 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
311 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
312 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
313 return GL_TEXTURE_BINDING_CUBE_MAP;
314 case GL_TEXTURE_CUBE_MAP_ARRAY:
315 return GL_TEXTURE_BINDING_CUBE_MAP_ARRAY;
317 return GL_TEXTURE_BINDING_3D;
326 * OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
327 * texture to a framebuffer.
330 getTexImageOES(GLenum target, GLint level, ImageDesc &desc, GLubyte *pixels)
332 memset(pixels, 0x80, desc.height * desc.width * 4);
334 GLenum texture_binding = getTextureBinding(target);
335 if (texture_binding == GL_NONE) {
340 glGetIntegerv(texture_binding, &texture);
347 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
348 glGenFramebuffers(1, &fbo);
349 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
355 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
356 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
357 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
358 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
359 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
360 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
361 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
362 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
363 if (status != GL_FRAMEBUFFER_COMPLETE) {
364 std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
366 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
368 case GL_TEXTURE_3D_OES:
369 for (int i = 0; i < desc.depth; i++) {
370 glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
371 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * desc.width * desc.height);
376 glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);
378 glDeleteFramebuffers(1, &fbo);
382 static inline GLboolean
383 isDepthFormat(GLenum internalFormat)
385 switch (internalFormat) {
386 case GL_DEPTH_COMPONENT:
387 case GL_DEPTH_COMPONENT16:
388 case GL_DEPTH_COMPONENT24:
389 case GL_DEPTH_COMPONENT32:
390 case GL_DEPTH_COMPONENT32F:
391 case GL_DEPTH_COMPONENT32F_NV:
392 case GL_DEPTH_STENCIL:
393 case GL_DEPTH24_STENCIL8:
394 case GL_DEPTH32F_STENCIL8:
395 case GL_DEPTH32F_STENCIL8_NV:
403 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
406 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
411 GLint active_texture = GL_TEXTURE0;
412 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
413 snprintf(label, sizeof label, "%s, %s, level = %d",
414 enumToString(active_texture), enumToString(target), level);
416 json.beginMember(label);
420 if (!context.ES && isDepthFormat(desc.internalFormat)) {
421 format = GL_DEPTH_COMPONENT;
428 image::Image *image = new image::Image(desc.width, desc.height*desc.depth, channels, true);
430 context.resetPixelPackState();
433 getTexImageOES(target, level, desc, image->pixels);
435 glGetTexImage(target, level, format, GL_UNSIGNED_BYTE, image->pixels);
438 context.restorePixelPackState();
440 json.writeImage(image, formatToString(desc.internalFormat), desc.depth);
444 json.endMember(); // label
449 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
451 GLint texture_binding = 0;
452 glGetIntegerv(binding, &texture_binding);
453 if (!glIsEnabled(target) && !texture_binding) {
461 if (target == GL_TEXTURE_CUBE_MAP) {
462 for (int face = 0; face < 6; ++face) {
463 if (!getActiveTextureLevelDesc(context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, desc)) {
466 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
469 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
472 dumpActiveTextureLevel(json, context, target, level);
481 dumpTextures(JSONWriter &json, Context &context)
483 json.beginMember("textures");
485 GLint active_texture = GL_TEXTURE0;
486 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
488 GLint max_texture_coords = 0;
489 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
490 GLint max_combined_texture_image_units = 0;
491 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
492 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
495 * At least the Android software GL implementation doesn't return the
496 * proper value for this, but rather returns 0. The GL(ES) specification
497 * mandates a minimum value of 2, so use this as a fall-back value.
499 max_units = std::max(max_units, 2);
501 for (GLint unit = 0; unit < max_units; ++unit) {
502 GLenum texture = GL_TEXTURE0 + unit;
503 glActiveTexture(texture);
504 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
505 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
506 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
507 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
508 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
510 glActiveTexture(active_texture);
512 json.endMember(); // textures
517 getDrawableBounds(GLint *width, GLint *height) {
518 #if defined(__linux__)
519 if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) {
520 EGLContext currentContext = eglGetCurrentContext();
521 if (currentContext == EGL_NO_CONTEXT) {
525 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
526 if (currentSurface == EGL_NO_SURFACE) {
530 EGLDisplay currentDisplay = eglGetCurrentDisplay();
531 if (currentDisplay == EGL_NO_DISPLAY) {
535 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
536 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
546 HDC hDC = wglGetCurrentDC();
551 HWND hWnd = WindowFromDC(hDC);
554 if (!GetClientRect(hWnd, &rect)) {
558 *width = rect.right - rect.left;
559 *height = rect.bottom - rect.top;
562 #elif defined(__APPLE__)
564 CGLContextObj ctx = CGLGetCurrentContext();
573 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
579 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
583 *width = rect.size.width;
584 *height = rect.size.height;
587 #elif defined(HAVE_X11)
593 unsigned int w, h, bw, depth;
595 display = glXGetCurrentDisplay();
600 drawable = glXGetCurrentDrawable();
601 if (drawable == None) {
605 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
621 struct TextureTargetBinding
632 GL_TEXTURE_RECTANGLE,
635 GL_TEXTURE_2D_MULTISAMPLE,
638 GL_TEXTURE_CUBE_MAP_ARRAY,
643 getTextureTarget(GLint texture)
645 if (!glIsTexture(texture)) {
649 for (unsigned i = 0; i < sizeof(textureTargets)/sizeof(textureTargets[0]); ++i) {
650 GLenum target = textureTargets[i];
651 GLenum binding = getTextureBinding(target);
653 while (glGetError() != GL_NO_ERROR)
656 GLint bound_texture = 0;
657 glGetIntegerv(binding, &bound_texture);
658 glBindTexture(target, texture);
660 bool succeeded = glGetError() == GL_NO_ERROR;
662 glBindTexture(target, bound_texture);
674 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
676 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
677 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
680 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
687 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
689 GLint bound_renderbuffer = 0;
690 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
691 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
693 getBoundRenderbufferDesc(context, desc);
695 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
702 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
704 GLint object_type = GL_NONE;
705 glGetFramebufferAttachmentParameteriv(target, attachment,
706 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
708 if (object_type == GL_NONE) {
712 GLint object_name = 0;
713 glGetFramebufferAttachmentParameteriv(target, attachment,
714 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
716 if (object_name == 0) {
720 if (object_type == GL_RENDERBUFFER) {
721 return getRenderbufferDesc(context, object_name, desc);
722 } else if (object_type == GL_TEXTURE) {
723 GLint texture_face = 0;
724 glGetFramebufferAttachmentParameteriv(target, attachment,
725 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
728 GLint texture_level = 0;
729 glGetFramebufferAttachmentParameteriv(target, attachment,
730 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
733 GLint bound_texture = 0;
734 if (texture_face != 0) {
735 glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &bound_texture);
736 glBindTexture(GL_TEXTURE_CUBE_MAP, object_name);
737 getActiveTextureLevelDesc(context, texture_face, texture_level, desc);
738 glBindTexture(GL_TEXTURE_CUBE_MAP, bound_texture);
740 GLenum texture_target = getTextureTarget(object_name);
741 GLenum texture_binding = getTextureBinding(texture_target);
742 glGetIntegerv(texture_binding, &bound_texture);
743 glBindTexture(texture_target, object_name);
744 getActiveTextureLevelDesc(context, texture_target, texture_level, desc);
745 glBindTexture(texture_target, bound_texture);
750 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
758 getDrawBufferImage() {
759 GLenum format = GL_RGB;
760 GLint channels = _gl_format_channels(format);
767 GLenum framebuffer_binding;
768 GLenum framebuffer_target;
770 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
771 framebuffer_target = GL_FRAMEBUFFER;
773 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
774 framebuffer_target = GL_DRAW_FRAMEBUFFER;
777 GLint draw_framebuffer = 0;
778 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
780 GLint draw_buffer = GL_NONE;
782 if (draw_framebuffer) {
783 if (context.ARB_draw_buffers) {
784 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
785 if (draw_buffer == GL_NONE) {
789 // GL_COLOR_ATTACHMENT0 is implied
790 draw_buffer = GL_COLOR_ATTACHMENT0;
793 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
798 // XXX: Draw buffer is always FRONT for single buffer context, BACK
799 // for double buffered contexts. There is no way to know which (as
800 // GL_DOUBLEBUFFER state is also unavailable), so always assume
802 draw_buffer = GL_BACK;
804 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
805 if (draw_buffer == GL_NONE) {
810 if (!getDrawableBounds(&desc.width, &desc.height)) {
817 GLenum type = GL_UNSIGNED_BYTE;
820 if (format == GL_DEPTH_COMPONENT) {
821 type = GL_UNSIGNED_INT;
826 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
831 while (glGetError() != GL_NO_ERROR) {}
833 GLint read_framebuffer = 0;
834 GLint read_buffer = GL_NONE;
836 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
837 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
839 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
840 glReadBuffer(draw_buffer);
843 // TODO: reset imaging state too
844 context.resetPixelPackState();
846 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
848 context.restorePixelPackState();
851 glReadBuffer(read_buffer);
852 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
855 GLenum error = glGetError();
856 if (error != GL_NO_ERROR) {
858 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
859 error = glGetError();
860 } while(error != GL_NO_ERROR);
870 * Dump the image of the currently bound read buffer.
873 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
874 GLint internalFormat = GL_NONE)
876 GLint channels = _gl_format_channels(format);
878 if (internalFormat == GL_NONE) {
879 internalFormat = format;
884 GLenum type = GL_UNSIGNED_BYTE;
887 if (format == GL_DEPTH_COMPONENT) {
888 type = GL_UNSIGNED_INT;
893 image::Image *image = new image::Image(width, height, channels, true);
895 // TODO: reset imaging state too
896 context.resetPixelPackState();
898 glReadPixels(0, 0, width, height, format, type, image->pixels);
900 context.restorePixelPackState();
902 json.writeImage(image, formatToString(internalFormat));
909 downsampledFramebuffer(Context &context,
910 GLuint oldFbo, GLint drawbuffer,
911 GLint colorRb, GLint depthRb, GLint stencilRb,
912 GLuint *rbs, GLint *numRbs)
919 glGenFramebuffers(1, &fbo);
920 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
925 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
926 getBoundRenderbufferDesc(context, desc);
928 glGenRenderbuffers(1, &rbs[*numRbs]);
929 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
930 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
931 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
932 GL_RENDERBUFFER, rbs[*numRbs]);
934 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
935 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
936 glDrawBuffer(drawbuffer);
937 glReadBuffer(drawbuffer);
938 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
939 GL_COLOR_BUFFER_BIT, GL_NEAREST);
940 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
944 if (stencilRb == depthRb && stencilRb) {
945 //combined depth and stencil buffer
947 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
948 getBoundRenderbufferDesc(context, desc);
950 glGenRenderbuffers(1, &rbs[*numRbs]);
951 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
952 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
953 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
954 GL_RENDERBUFFER, rbs[*numRbs]);
955 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
956 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
957 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
958 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
959 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
964 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
965 getBoundRenderbufferDesc(context, desc);
967 glGenRenderbuffers(1, &rbs[*numRbs]);
968 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
969 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
970 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
972 GL_RENDERBUFFER, rbs[*numRbs]);
973 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
974 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
975 glDrawBuffer(GL_DEPTH_ATTACHMENT);
976 glReadBuffer(GL_DEPTH_ATTACHMENT);
977 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
978 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
983 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
984 getBoundRenderbufferDesc(context, desc);
986 glGenRenderbuffers(1, &rbs[*numRbs]);
987 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
988 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
989 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
990 GL_STENCIL_ATTACHMENT,
991 GL_RENDERBUFFER, rbs[*numRbs]);
992 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
993 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
994 glDrawBuffer(GL_STENCIL_ATTACHMENT);
995 glReadBuffer(GL_STENCIL_ATTACHMENT);
996 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
997 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1007 * Dump images of current draw drawable/window.
1010 dumpDrawableImages(JSONWriter &json, Context &context)
1012 GLint width, height;
1014 if (!getDrawableBounds(&width, &height)) {
1018 GLint draw_buffer = GL_NONE;
1020 // XXX: Draw buffer is always FRONT for single buffer context, BACK for
1021 // double buffered contexts. There is no way to know which (as
1022 // GL_DOUBLEBUFFER state is also unavailable), so always assume
1023 // double-buffering.
1024 draw_buffer = GL_BACK;
1026 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1029 if (draw_buffer != GL_NONE) {
1030 // Read from current draw buffer
1031 GLint read_buffer = GL_NONE;
1033 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1034 glReadBuffer(draw_buffer);
1037 GLint alpha_bits = 0;
1039 // XXX: Ignore alpha until we are able to match the traced visual
1040 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1042 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1043 json.beginMember(enumToString(draw_buffer));
1044 dumpReadBufferImage(json, width, height, format);
1047 // Restore original read buffer
1049 glReadBuffer(read_buffer);
1054 GLint depth_bits = 0;
1055 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1057 json.beginMember("GL_DEPTH_COMPONENT");
1058 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1062 GLint stencil_bits = 0;
1063 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1065 json.beginMember("GL_STENCIL_INDEX");
1066 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1074 * Dump the specified framebuffer attachment.
1076 * In the case of a color attachment, it assumes it is already bound for read.
1079 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
1082 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1086 json.beginMember(enumToString(attachment));
1087 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1093 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1095 GLint read_framebuffer = 0;
1096 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1098 GLint read_buffer = GL_NONE;
1099 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1101 GLint max_draw_buffers = 1;
1102 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1103 GLint max_color_attachments = 0;
1104 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1106 for (GLint i = 0; i < max_draw_buffers; ++i) {
1107 GLint draw_buffer = GL_NONE;
1108 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1109 if (draw_buffer != GL_NONE) {
1110 glReadBuffer(draw_buffer);
1112 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1113 attachment = draw_buffer;
1115 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1116 attachment = GL_COLOR_ATTACHMENT0;
1118 GLint alpha_size = 0;
1119 glGetFramebufferAttachmentParameteriv(target, attachment,
1120 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1122 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1123 dumpFramebufferAttachment(json, context, target, attachment, format);
1127 glReadBuffer(read_buffer);
1130 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1131 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1134 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1139 dumpFramebuffer(JSONWriter &json, Context &context)
1141 json.beginMember("framebuffer");
1144 GLint boundDrawFbo = 0, boundReadFbo = 0;
1145 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1146 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1147 if (!boundDrawFbo) {
1148 dumpDrawableImages(json, context);
1149 } else if (context.ES) {
1150 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1152 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1153 GLint draw_buffer0 = GL_NONE;
1154 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1155 bool multisample = false;
1158 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1161 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1162 if (object_type == GL_RENDERBUFFER) {
1163 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1164 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1166 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1172 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1173 if (object_type == GL_RENDERBUFFER) {
1174 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1175 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1177 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1183 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1184 if (object_type == GL_RENDERBUFFER) {
1185 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1186 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1188 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1194 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1201 // glReadPixels doesnt support multisampled buffers so we need
1202 // to blit the fbo to a temporary one
1203 fboCopy = downsampledFramebuffer(context,
1204 boundDrawFbo, draw_buffer0,
1205 colorRb, depthRb, stencilRb,
1209 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1212 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1213 glDeleteRenderbuffers(numRbs, rbs);
1214 glDeleteFramebuffers(1, &fboCopy);
1217 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1218 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1222 json.endMember(); // framebuffer
1226 } /* namespace glstate */