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 * Sames as enumToString, but with special provision to handle formatsLUMINANCE_ALPHA.
94 * OpenGL 2.1 specification states that "internalFormat may (for backwards
95 * compatibility with the 1.0 version of the GL) also take on the integer
96 * values 1, 2, 3, and 4, which are equivalent to symbolic constants LUMINANCE,
97 * LUMINANCE ALPHA, RGB, and RGBA respectively".
100 formatToString(GLenum internalFormat) {
101 switch (internalFormat) {
103 return "GL_LUMINANCE";
105 return "GL_LUMINANCE_ALPHA";
111 return enumToString(internalFormat);
117 * OpenGL ES does not support glGetTexLevelParameteriv, but it is possible to
118 * probe whether a texture has a given size by crafting a dummy glTexSubImage()
122 probeTextureLevelSizeOES(GLenum target, GLint level, const GLint size[3]) {
123 while (glGetError() != GL_NO_ERROR)
126 GLenum internalFormat = GL_RGBA;
127 GLenum type = GL_UNSIGNED_BYTE;
132 case GL_TEXTURE_CUBE_MAP:
133 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
134 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
135 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
136 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
137 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
138 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
139 glTexSubImage2D(target, level, size[0], size[1], 0, 0, internalFormat, type, &dummy);
141 case GL_TEXTURE_3D_OES:
142 glTexSubImage3DOES(target, level, size[0], size[1], size[2], 0, 0, 0, internalFormat, type, &dummy);
148 GLenum error = glGetError();
151 std::cerr << "(" << size[0] << ", " << size[1] << ", " << size[2] << ") = " << enumToString(error) << "\n";
154 if (error == GL_NO_ERROR) {
158 while (glGetError() != GL_NO_ERROR)
166 * Bisect the texture size along an axis.
168 * It is assumed that the texture exists.
171 bisectTextureLevelSizeOES(GLenum target, GLint level, GLint axis, GLint max) {
172 GLint size[3] = {0, 0, 0};
179 GLint test = (min + max) / 2;
186 if (probeTextureLevelSizeOES(target, level, size)) {
196 * Special path to obtain texture size on OpenGL ES, that does not rely on
197 * glGetTexLevelParameteriv
200 getActiveTextureLevelDescOES(Context &context, GLenum target, GLint level, ImageDesc &desc)
202 if (target == GL_TEXTURE_1D) {
203 // OpenGL ES does not support 1D textures
207 const GLint size[3] = {1, 1, 1};
208 if (!probeTextureLevelSizeOES(target, level, size)) {
213 desc.internalFormat = GL_RGBA;
218 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
219 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
220 desc.height = bisectTextureLevelSizeOES(target, level, 1, maxSize);
223 case GL_TEXTURE_CUBE_MAP:
224 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
225 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
226 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
227 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
228 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
229 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
230 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxSize);
231 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
232 desc.height = desc.width;
235 case GL_TEXTURE_3D_OES:
236 glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_OES, &maxSize);
237 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
238 desc.width = bisectTextureLevelSizeOES(target, level, 1, maxSize);
239 desc.depth = bisectTextureLevelSizeOES(target, level, 2, maxSize);
247 << enumToString(target) << " "
249 << desc.width << "x" << desc.height << "x" << desc.depth
258 getActiveTextureLevelDesc(Context &context, GLenum target, GLint level, ImageDesc &desc)
260 assert(target != GL_TEXTURE_CUBE_MAP);
263 return getActiveTextureLevelDescOES(context, target, level, desc);
266 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &desc.internalFormat);
269 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &desc.width);
271 if (target == GL_TEXTURE_1D) {
276 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &desc.height);
277 if (target != GL_TEXTURE_3D) {
281 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &desc.depth);
290 * OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
291 * texture to a framebuffer.
294 getTexImageOES(GLenum target, GLint level, ImageDesc &desc, GLubyte *pixels)
296 memset(pixels, 0x80, desc.height * desc.width * 4);
298 GLenum texture_binding = GL_NONE;
301 texture_binding = GL_TEXTURE_BINDING_2D;
303 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
304 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
305 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
306 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
307 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
308 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
309 texture_binding = GL_TEXTURE_BINDING_CUBE_MAP;
311 case GL_TEXTURE_3D_OES:
312 texture_binding = GL_TEXTURE_BINDING_3D_OES;
318 glGetIntegerv(texture_binding, &texture);
325 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
326 glGenFramebuffers(1, &fbo);
327 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
333 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
334 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
335 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
336 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
337 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
338 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
339 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
340 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
341 if (status != GL_FRAMEBUFFER_COMPLETE) {
342 std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
344 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
346 case GL_TEXTURE_3D_OES:
347 for (int i = 0; i < desc.depth; i++) {
348 glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
349 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * desc.width * desc.height);
354 glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);
356 glDeleteFramebuffers(1, &fbo);
360 static inline GLboolean
361 isDepthFormat(GLenum internalFormat)
363 switch (internalFormat) {
364 case GL_DEPTH_COMPONENT:
365 case GL_DEPTH_COMPONENT16:
366 case GL_DEPTH_COMPONENT24:
367 case GL_DEPTH_COMPONENT32:
368 case GL_DEPTH_COMPONENT32F:
369 case GL_DEPTH_COMPONENT32F_NV:
370 case GL_DEPTH_STENCIL:
371 case GL_DEPTH24_STENCIL8:
372 case GL_DEPTH32F_STENCIL8:
373 case GL_DEPTH32F_STENCIL8_NV:
381 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
384 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
390 GLint active_texture = GL_TEXTURE0;
391 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
392 snprintf(label, sizeof label, "%s, %s, level = %d",
393 enumToString(active_texture), enumToString(target), level);
395 json.beginMember(label);
401 if (!context.ES && isDepthFormat(desc.internalFormat)) {
402 format = GL_DEPTH_COMPONENT;
409 // Tell the GUI this is no ordinary object, but an image
410 json.writeStringMember("__class__", "image");
412 json.writeNumberMember("__width__", desc.width);
413 json.writeNumberMember("__height__", desc.height);
414 json.writeNumberMember("__depth__", desc.depth);
416 json.writeStringMember("__format__", formatToString(desc.internalFormat));
418 // Hardcoded for now, but we could chose types more adequate to the
419 // texture internal format
420 json.writeStringMember("__type__", "uint8");
421 json.writeBoolMember("__normalized__", true);
422 json.writeNumberMember("__channels__", channels);
424 GLubyte *pixels = new GLubyte[desc.depth*desc.width*desc.height*channels];
426 context.resetPixelPackState();
429 getTexImageOES(target, level, desc, pixels);
431 glGetTexImage(target, level, format, GL_UNSIGNED_BYTE, pixels);
434 context.restorePixelPackState();
436 json.beginMember("__data__");
439 image::writePixelsToBuffer(pixels, desc.width, desc.depth * desc.height, channels, true, &pngBuffer, &pngBufferSize);
440 json.writeBase64(pngBuffer, pngBufferSize);
442 json.endMember(); // __data__
450 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
452 GLint texture_binding = 0;
453 glGetIntegerv(binding, &texture_binding);
454 if (!glIsEnabled(target) && !texture_binding) {
462 if (target == GL_TEXTURE_CUBE_MAP) {
463 for (int face = 0; face < 6; ++face) {
464 if (!getActiveTextureLevelDesc(context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, desc)) {
467 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
470 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
473 dumpActiveTextureLevel(json, context, target, level);
482 dumpTextures(JSONWriter &json, Context &context)
484 json.beginMember("textures");
486 GLint active_texture = GL_TEXTURE0;
487 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
489 GLint max_texture_coords = 0;
490 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
491 GLint max_combined_texture_image_units = 0;
492 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
493 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
496 * At least the Android software GL implementation doesn't return the
497 * proper value for this, but rather returns 0. The GL(ES) specification
498 * mandates a minimum value of 2, so use this as a fall-back value.
500 max_units = std::max(max_units, 2);
502 for (GLint unit = 0; unit < max_units; ++unit) {
503 GLenum texture = GL_TEXTURE0 + unit;
504 glActiveTexture(texture);
505 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
506 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
507 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
508 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
509 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
511 glActiveTexture(active_texture);
513 json.endMember(); // textures
518 getDrawableBounds(GLint *width, GLint *height) {
519 #if defined(__linux__)
520 if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) {
521 EGLContext currentContext = eglGetCurrentContext();
522 if (currentContext == EGL_NO_CONTEXT) {
526 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
527 if (currentSurface == EGL_NO_SURFACE) {
531 EGLDisplay currentDisplay = eglGetCurrentDisplay();
532 if (currentDisplay == EGL_NO_DISPLAY) {
536 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
537 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
547 HDC hDC = wglGetCurrentDC();
552 HWND hWnd = WindowFromDC(hDC);
555 if (!GetClientRect(hWnd, &rect)) {
559 *width = rect.right - rect.left;
560 *height = rect.bottom - rect.top;
563 #elif defined(__APPLE__)
565 CGLContextObj ctx = CGLGetCurrentContext();
574 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
580 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
584 *width = rect.size.width;
585 *height = rect.size.height;
588 #elif defined(HAVE_X11)
594 unsigned int w, h, bw, depth;
596 display = glXGetCurrentDisplay();
601 drawable = glXGetCurrentDrawable();
602 if (drawable == None) {
606 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
622 static const GLenum texture_bindings[][2] = {
623 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
624 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
625 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
626 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
627 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
632 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
635 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
636 target = texture_bindings[i][0];
638 GLenum binding = texture_bindings[i][1];
640 while (glGetError() != GL_NO_ERROR)
643 glGetIntegerv(binding, &bound_texture);
644 glBindTexture(target, texture);
646 if (glGetError() == GL_NO_ERROR) {
650 glBindTexture(target, bound_texture);
660 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
663 GLint bound_texture = 0;
664 if (!bindTexture(texture, target, bound_texture)) {
668 getActiveTextureLevelDesc(context, target, level, desc);
670 glBindTexture(target, bound_texture);
677 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
679 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
680 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
683 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
690 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
692 GLint bound_renderbuffer = 0;
693 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
694 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
696 getBoundRenderbufferDesc(context, desc);
698 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
705 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
707 GLint object_type = GL_NONE;
708 glGetFramebufferAttachmentParameteriv(target, attachment,
709 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
711 if (object_type == GL_NONE) {
715 GLint object_name = 0;
716 glGetFramebufferAttachmentParameteriv(target, attachment,
717 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
719 if (object_name == 0) {
723 if (object_type == GL_RENDERBUFFER) {
724 return getRenderbufferDesc(context, object_name, desc);
725 } else if (object_type == GL_TEXTURE) {
726 GLint texture_level = 0;
727 glGetFramebufferAttachmentParameteriv(target, attachment,
728 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
730 return getTextureLevelDesc(context, object_name, texture_level, desc);
732 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
740 getDrawBufferImage() {
741 GLenum format = GL_RGB;
742 GLint channels = _gl_format_channels(format);
749 GLenum framebuffer_binding;
750 GLenum framebuffer_target;
752 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
753 framebuffer_target = GL_FRAMEBUFFER;
755 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
756 framebuffer_target = GL_DRAW_FRAMEBUFFER;
759 GLint draw_framebuffer = 0;
760 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
762 GLint draw_buffer = GL_NONE;
764 if (draw_framebuffer) {
765 if (context.ARB_draw_buffers) {
766 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
767 if (draw_buffer == GL_NONE) {
772 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
777 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
778 if (draw_buffer == GL_NONE) {
783 if (!getDrawableBounds(&desc.width, &desc.height)) {
790 GLenum type = GL_UNSIGNED_BYTE;
793 if (format == GL_DEPTH_COMPONENT) {
794 type = GL_UNSIGNED_INT;
799 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
804 while (glGetError() != GL_NO_ERROR) {}
806 GLint read_framebuffer = 0;
807 GLint read_buffer = GL_NONE;
809 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
810 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
812 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
813 glReadBuffer(draw_buffer);
816 // TODO: reset imaging state too
817 context.resetPixelPackState();
819 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
821 context.restorePixelPackState();
824 glReadBuffer(read_buffer);
825 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
828 GLenum error = glGetError();
829 if (error != GL_NO_ERROR) {
831 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
832 error = glGetError();
833 } while(error != GL_NO_ERROR);
843 * Dump the image of the currently bound read buffer.
846 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
847 GLint internalFormat = GL_NONE)
849 GLint channels = _gl_format_channels(format);
851 if (internalFormat == GL_NONE) {
852 internalFormat = format;
859 // Tell the GUI this is no ordinary object, but an image
860 json.writeStringMember("__class__", "image");
862 json.writeNumberMember("__width__", width);
863 json.writeNumberMember("__height__", height);
864 json.writeNumberMember("__depth__", 1);
866 json.writeStringMember("__format__", formatToString(internalFormat));
868 // Hardcoded for now, but we could chose types more adequate to the
869 // texture internal format
870 json.writeStringMember("__type__", "uint8");
871 json.writeBoolMember("__normalized__", true);
872 json.writeNumberMember("__channels__", channels);
874 GLenum type = GL_UNSIGNED_BYTE;
877 if (format == GL_DEPTH_COMPONENT) {
878 type = GL_UNSIGNED_INT;
883 GLubyte *pixels = new GLubyte[width*height*channels];
885 // TODO: reset imaging state too
886 context.resetPixelPackState();
888 glReadPixels(0, 0, width, height, format, type, pixels);
890 context.restorePixelPackState();
892 json.beginMember("__data__");
895 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
896 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
897 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
898 json.writeBase64(pngBuffer, pngBufferSize);
900 json.endMember(); // __data__
908 downsampledFramebuffer(Context &context,
909 GLuint oldFbo, GLint drawbuffer,
910 GLint colorRb, GLint depthRb, GLint stencilRb,
911 GLuint *rbs, GLint *numRbs)
918 glGenFramebuffers(1, &fbo);
919 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
924 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
925 getBoundRenderbufferDesc(context, desc);
927 glGenRenderbuffers(1, &rbs[*numRbs]);
928 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
929 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
930 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
931 GL_RENDERBUFFER, rbs[*numRbs]);
933 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
934 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
935 glDrawBuffer(drawbuffer);
936 glReadBuffer(drawbuffer);
937 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
938 GL_COLOR_BUFFER_BIT, GL_NEAREST);
939 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
943 if (stencilRb == depthRb && stencilRb) {
944 //combined depth and stencil buffer
946 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
947 getBoundRenderbufferDesc(context, desc);
949 glGenRenderbuffers(1, &rbs[*numRbs]);
950 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
951 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
952 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
953 GL_RENDERBUFFER, rbs[*numRbs]);
954 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
955 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
956 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
957 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
958 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
963 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
964 getBoundRenderbufferDesc(context, desc);
966 glGenRenderbuffers(1, &rbs[*numRbs]);
967 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
968 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
969 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
971 GL_RENDERBUFFER, rbs[*numRbs]);
972 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
973 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
974 glDrawBuffer(GL_DEPTH_ATTACHMENT);
975 glReadBuffer(GL_DEPTH_ATTACHMENT);
976 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
977 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
982 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
983 getBoundRenderbufferDesc(context, desc);
985 glGenRenderbuffers(1, &rbs[*numRbs]);
986 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
987 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
988 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
989 GL_STENCIL_ATTACHMENT,
990 GL_RENDERBUFFER, rbs[*numRbs]);
991 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
992 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
993 glDrawBuffer(GL_STENCIL_ATTACHMENT);
994 glReadBuffer(GL_STENCIL_ATTACHMENT);
995 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
996 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1006 * Dump images of current draw drawable/window.
1009 dumpDrawableImages(JSONWriter &json, Context &context)
1011 GLint width, height;
1013 if (!getDrawableBounds(&width, &height)) {
1017 GLint draw_buffer = GL_NONE;
1019 draw_buffer = GL_BACK;
1021 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1022 glReadBuffer(draw_buffer);
1025 if (draw_buffer != GL_NONE) {
1026 GLint read_buffer = GL_NONE;
1028 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1031 GLint alpha_bits = 0;
1033 // XXX: Ignore alpha until we are able to match the traced visual
1034 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1036 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1037 json.beginMember(enumToString(draw_buffer));
1038 dumpReadBufferImage(json, width, height, format);
1042 glReadBuffer(read_buffer);
1047 GLint depth_bits = 0;
1048 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1050 json.beginMember("GL_DEPTH_COMPONENT");
1051 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1055 GLint stencil_bits = 0;
1056 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1058 json.beginMember("GL_STENCIL_INDEX");
1059 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1067 * Dump the specified framebuffer attachment.
1069 * In the case of a color attachment, it assumes it is already bound for read.
1072 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
1075 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1079 json.beginMember(enumToString(attachment));
1080 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1086 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1088 GLint read_framebuffer = 0;
1089 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1091 GLint read_buffer = GL_NONE;
1092 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1094 GLint max_draw_buffers = 1;
1095 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1096 GLint max_color_attachments = 0;
1097 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1099 for (GLint i = 0; i < max_draw_buffers; ++i) {
1100 GLint draw_buffer = GL_NONE;
1101 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1102 if (draw_buffer != GL_NONE) {
1103 glReadBuffer(draw_buffer);
1105 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1106 attachment = draw_buffer;
1108 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1109 attachment = GL_COLOR_ATTACHMENT0;
1111 GLint alpha_size = 0;
1112 glGetFramebufferAttachmentParameteriv(target, attachment,
1113 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1115 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1116 dumpFramebufferAttachment(json, context, target, attachment, format);
1120 glReadBuffer(read_buffer);
1123 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1124 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1127 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1132 dumpFramebuffer(JSONWriter &json, Context &context)
1134 json.beginMember("framebuffer");
1137 GLint boundDrawFbo = 0, boundReadFbo = 0;
1138 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1139 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1140 if (!boundDrawFbo) {
1141 dumpDrawableImages(json, context);
1142 } else if (context.ES) {
1143 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1145 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1146 GLint draw_buffer0 = GL_NONE;
1147 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1148 bool multisample = false;
1151 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1154 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1155 if (object_type == GL_RENDERBUFFER) {
1156 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1157 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1159 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1165 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1166 if (object_type == GL_RENDERBUFFER) {
1167 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1168 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1170 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1176 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1177 if (object_type == GL_RENDERBUFFER) {
1178 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1179 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1181 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1187 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1194 // glReadPixels doesnt support multisampled buffers so we need
1195 // to blit the fbo to a temporary one
1196 fboCopy = downsampledFramebuffer(context,
1197 boundDrawFbo, draw_buffer0,
1198 colorRb, depthRb, stencilRb,
1202 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1205 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1206 glDeleteRenderbuffers(numRbs, rbs);
1207 glDeleteFramebuffers(1, &fboCopy);
1210 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1211 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1215 json.endMember(); // framebuffer
1219 } /* namespace glstate */