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)
235 assert(target != GL_TEXTURE_CUBE_MAP);
238 return getActiveTextureLevelDescOES(context, target, level, desc);
241 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &desc.internalFormat);
244 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &desc.width);
246 if (target == GL_TEXTURE_1D) {
251 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &desc.height);
252 if (target != GL_TEXTURE_3D) {
256 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &desc.depth);
265 * OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
266 * texture to a framebuffer.
269 getTexImageOES(GLenum target, GLint level, ImageDesc &desc, GLubyte *pixels)
271 memset(pixels, 0x80, desc.height * desc.width * 4);
273 GLenum texture_binding = GL_NONE;
276 texture_binding = GL_TEXTURE_BINDING_2D;
278 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
279 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
280 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
281 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
282 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
283 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
284 texture_binding = GL_TEXTURE_BINDING_CUBE_MAP;
286 case GL_TEXTURE_3D_OES:
287 texture_binding = GL_TEXTURE_BINDING_3D_OES;
293 glGetIntegerv(texture_binding, &texture);
300 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
301 glGenFramebuffers(1, &fbo);
302 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
308 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
309 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
310 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
311 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
312 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
313 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
314 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
315 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
316 if (status != GL_FRAMEBUFFER_COMPLETE) {
317 std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
319 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
321 case GL_TEXTURE_3D_OES:
322 for (int i = 0; i < desc.depth; i++) {
323 glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
324 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * desc.width * desc.height);
329 glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);
331 glDeleteFramebuffers(1, &fbo);
335 static inline GLboolean
336 isDepthFormat(GLenum internalFormat)
338 switch (internalFormat) {
339 case GL_DEPTH_COMPONENT:
340 case GL_DEPTH_COMPONENT16:
341 case GL_DEPTH_COMPONENT24:
342 case GL_DEPTH_COMPONENT32:
343 case GL_DEPTH_COMPONENT32F:
344 case GL_DEPTH_COMPONENT32F_NV:
345 case GL_DEPTH_STENCIL:
346 case GL_DEPTH24_STENCIL8:
347 case GL_DEPTH32F_STENCIL8:
348 case GL_DEPTH32F_STENCIL8_NV:
356 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
359 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
365 GLint active_texture = GL_TEXTURE0;
366 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
367 snprintf(label, sizeof label, "%s, %s, level = %d",
368 enumToString(active_texture), enumToString(target), level);
370 json.beginMember(label);
376 if (!context.ES && isDepthFormat(desc.internalFormat)) {
377 format = GL_DEPTH_COMPONENT;
384 // Tell the GUI this is no ordinary object, but an image
385 json.writeStringMember("__class__", "image");
387 json.writeNumberMember("__width__", desc.width);
388 json.writeNumberMember("__height__", desc.height);
389 json.writeNumberMember("__depth__", desc.depth);
391 json.writeStringMember("__format__", enumToString(desc.internalFormat));
393 // Hardcoded for now, but we could chose types more adequate to the
394 // texture internal format
395 json.writeStringMember("__type__", "uint8");
396 json.writeBoolMember("__normalized__", true);
397 json.writeNumberMember("__channels__", channels);
399 GLubyte *pixels = new GLubyte[desc.depth*desc.width*desc.height*channels];
401 context.resetPixelPackState();
404 getTexImageOES(target, level, desc, pixels);
406 glGetTexImage(target, level, format, GL_UNSIGNED_BYTE, pixels);
409 context.restorePixelPackState();
411 json.beginMember("__data__");
414 image::writePixelsToBuffer(pixels, desc.width, desc.height, channels, true, &pngBuffer, &pngBufferSize);
415 json.writeBase64(pngBuffer, pngBufferSize);
417 json.endMember(); // __data__
425 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
427 GLint texture_binding = 0;
428 glGetIntegerv(binding, &texture_binding);
429 if (!glIsEnabled(target) && !texture_binding) {
437 if (target == GL_TEXTURE_CUBE_MAP) {
438 for (int face = 0; face < 6; ++face) {
439 if (!getActiveTextureLevelDesc(context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, desc)) {
442 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
445 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
448 dumpActiveTextureLevel(json, context, target, level);
457 dumpTextures(JSONWriter &json, Context &context)
459 json.beginMember("textures");
461 GLint active_texture = GL_TEXTURE0;
462 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
464 GLint max_texture_coords = 0;
465 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
466 GLint max_combined_texture_image_units = 0;
467 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
468 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
471 * At least the Android software GL implementation doesn't return the
472 * proper value for this, but rather returns 0. The GL(ES) specification
473 * mandates a minimum value of 2, so use this as a fall-back value.
475 max_units = std::max(max_units, 2);
477 for (GLint unit = 0; unit < max_units; ++unit) {
478 GLenum texture = GL_TEXTURE0 + unit;
479 glActiveTexture(texture);
480 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
481 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
482 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
483 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
484 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
486 glActiveTexture(active_texture);
488 json.endMember(); // textures
493 getDrawableBounds(GLint *width, GLint *height) {
494 #if defined(__linux__)
495 if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) {
496 EGLContext currentContext = eglGetCurrentContext();
497 if (currentContext == EGL_NO_CONTEXT) {
501 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
502 if (currentSurface == EGL_NO_SURFACE) {
506 EGLDisplay currentDisplay = eglGetCurrentDisplay();
507 if (currentDisplay == EGL_NO_DISPLAY) {
511 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
512 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
522 HDC hDC = wglGetCurrentDC();
527 HWND hWnd = WindowFromDC(hDC);
530 if (!GetClientRect(hWnd, &rect)) {
534 *width = rect.right - rect.left;
535 *height = rect.bottom - rect.top;
538 #elif defined(__APPLE__)
540 CGLContextObj ctx = CGLGetCurrentContext();
549 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
555 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
559 *width = rect.size.width;
560 *height = rect.size.height;
563 #elif defined(HAVE_X11)
569 unsigned int w, h, bw, depth;
571 display = glXGetCurrentDisplay();
576 drawable = glXGetCurrentDrawable();
577 if (drawable == None) {
581 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
597 static const GLenum texture_bindings[][2] = {
598 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
599 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
600 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
601 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
602 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
607 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
610 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
611 target = texture_bindings[i][0];
613 GLenum binding = texture_bindings[i][1];
615 while (glGetError() != GL_NO_ERROR)
618 glGetIntegerv(binding, &bound_texture);
619 glBindTexture(target, texture);
621 if (glGetError() == GL_NO_ERROR) {
625 glBindTexture(target, bound_texture);
635 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
638 GLint bound_texture = 0;
639 if (!bindTexture(texture, target, bound_texture)) {
643 getActiveTextureLevelDesc(context, target, level, desc);
645 glBindTexture(target, bound_texture);
652 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
654 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
655 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
658 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
665 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
667 GLint bound_renderbuffer = 0;
668 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
669 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
671 getBoundRenderbufferDesc(context, desc);
673 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
680 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
682 GLint object_type = GL_NONE;
683 glGetFramebufferAttachmentParameteriv(target, attachment,
684 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
686 if (object_type == GL_NONE) {
690 GLint object_name = 0;
691 glGetFramebufferAttachmentParameteriv(target, attachment,
692 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
694 if (object_name == 0) {
698 if (object_type == GL_RENDERBUFFER) {
699 return getRenderbufferDesc(context, object_name, desc);
700 } else if (object_type == GL_TEXTURE) {
701 GLint texture_level = 0;
702 glGetFramebufferAttachmentParameteriv(target, attachment,
703 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
705 return getTextureLevelDesc(context, object_name, texture_level, desc);
707 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
715 getDrawBufferImage() {
716 GLenum format = GL_RGB;
717 GLint channels = _gl_format_channels(format);
724 GLenum framebuffer_binding;
725 GLenum framebuffer_target;
727 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
728 framebuffer_target = GL_FRAMEBUFFER;
730 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
731 framebuffer_target = GL_DRAW_FRAMEBUFFER;
734 GLint draw_framebuffer = 0;
735 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
737 GLint draw_buffer = GL_NONE;
739 if (draw_framebuffer) {
740 if (context.ARB_draw_buffers) {
741 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
742 if (draw_buffer == GL_NONE) {
747 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
752 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
753 if (draw_buffer == GL_NONE) {
758 if (!getDrawableBounds(&desc.width, &desc.height)) {
765 GLenum type = GL_UNSIGNED_BYTE;
768 if (format == GL_DEPTH_COMPONENT) {
769 type = GL_UNSIGNED_INT;
774 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
779 while (glGetError() != GL_NO_ERROR) {}
781 GLint read_framebuffer = 0;
782 GLint read_buffer = GL_NONE;
784 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
785 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
787 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
788 glReadBuffer(draw_buffer);
791 // TODO: reset imaging state too
792 context.resetPixelPackState();
794 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
796 context.restorePixelPackState();
799 glReadBuffer(read_buffer);
800 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
803 GLenum error = glGetError();
804 if (error != GL_NO_ERROR) {
806 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
807 error = glGetError();
808 } while(error != GL_NO_ERROR);
818 * Dump the image of the currently bound read buffer.
821 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
822 GLint internalFormat = GL_NONE)
824 GLint channels = _gl_format_channels(format);
826 if (internalFormat == GL_NONE) {
827 internalFormat = format;
834 // Tell the GUI this is no ordinary object, but an image
835 json.writeStringMember("__class__", "image");
837 json.writeNumberMember("__width__", width);
838 json.writeNumberMember("__height__", height);
839 json.writeNumberMember("__depth__", 1);
841 json.writeStringMember("__format__", enumToString(internalFormat));
843 // Hardcoded for now, but we could chose types more adequate to the
844 // texture internal format
845 json.writeStringMember("__type__", "uint8");
846 json.writeBoolMember("__normalized__", true);
847 json.writeNumberMember("__channels__", channels);
849 GLenum type = GL_UNSIGNED_BYTE;
852 if (format == GL_DEPTH_COMPONENT) {
853 type = GL_UNSIGNED_INT;
858 GLubyte *pixels = new GLubyte[width*height*channels];
860 // TODO: reset imaging state too
861 context.resetPixelPackState();
863 glReadPixels(0, 0, width, height, format, type, pixels);
865 context.restorePixelPackState();
867 json.beginMember("__data__");
870 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
871 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
872 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
873 json.writeBase64(pngBuffer, pngBufferSize);
875 json.endMember(); // __data__
883 downsampledFramebuffer(Context &context,
884 GLuint oldFbo, GLint drawbuffer,
885 GLint colorRb, GLint depthRb, GLint stencilRb,
886 GLuint *rbs, GLint *numRbs)
893 glGenFramebuffers(1, &fbo);
894 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
899 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
900 getBoundRenderbufferDesc(context, desc);
902 glGenRenderbuffers(1, &rbs[*numRbs]);
903 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
904 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
905 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
906 GL_RENDERBUFFER, rbs[*numRbs]);
908 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
909 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
910 glDrawBuffer(drawbuffer);
911 glReadBuffer(drawbuffer);
912 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
913 GL_COLOR_BUFFER_BIT, GL_NEAREST);
914 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
918 if (stencilRb == depthRb && stencilRb) {
919 //combined depth and stencil buffer
921 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
922 getBoundRenderbufferDesc(context, desc);
924 glGenRenderbuffers(1, &rbs[*numRbs]);
925 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
926 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
927 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
928 GL_RENDERBUFFER, rbs[*numRbs]);
929 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
930 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
931 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
932 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
933 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
938 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
939 getBoundRenderbufferDesc(context, desc);
941 glGenRenderbuffers(1, &rbs[*numRbs]);
942 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
943 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
944 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
946 GL_RENDERBUFFER, rbs[*numRbs]);
947 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
948 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
949 glDrawBuffer(GL_DEPTH_ATTACHMENT);
950 glReadBuffer(GL_DEPTH_ATTACHMENT);
951 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
952 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
957 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
958 getBoundRenderbufferDesc(context, desc);
960 glGenRenderbuffers(1, &rbs[*numRbs]);
961 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
962 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
963 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
964 GL_STENCIL_ATTACHMENT,
965 GL_RENDERBUFFER, rbs[*numRbs]);
966 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
967 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
968 glDrawBuffer(GL_STENCIL_ATTACHMENT);
969 glReadBuffer(GL_STENCIL_ATTACHMENT);
970 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
971 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
981 * Dump images of current draw drawable/window.
984 dumpDrawableImages(JSONWriter &json, Context &context)
988 if (!getDrawableBounds(&width, &height)) {
992 GLint draw_buffer = GL_NONE;
994 draw_buffer = GL_BACK;
996 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
997 glReadBuffer(draw_buffer);
1000 if (draw_buffer != GL_NONE) {
1001 GLint read_buffer = GL_NONE;
1003 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1006 GLint alpha_bits = 0;
1008 // XXX: Ignore alpha until we are able to match the traced visual
1009 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1011 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1012 json.beginMember(enumToString(draw_buffer));
1013 dumpReadBufferImage(json, width, height, format);
1017 glReadBuffer(read_buffer);
1022 GLint depth_bits = 0;
1023 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1025 json.beginMember("GL_DEPTH_COMPONENT");
1026 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1030 GLint stencil_bits = 0;
1031 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1033 json.beginMember("GL_STENCIL_INDEX");
1034 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1042 * Dump the specified framebuffer attachment.
1044 * In the case of a color attachment, it assumes it is already bound for read.
1047 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
1050 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1054 json.beginMember(enumToString(attachment));
1055 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1061 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1063 GLint read_framebuffer = 0;
1064 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1066 GLint read_buffer = GL_NONE;
1067 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1069 GLint max_draw_buffers = 1;
1070 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1071 GLint max_color_attachments = 0;
1072 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1074 for (GLint i = 0; i < max_draw_buffers; ++i) {
1075 GLint draw_buffer = GL_NONE;
1076 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1077 if (draw_buffer != GL_NONE) {
1078 glReadBuffer(draw_buffer);
1080 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1081 attachment = draw_buffer;
1083 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1084 attachment = GL_COLOR_ATTACHMENT0;
1086 GLint alpha_size = 0;
1087 glGetFramebufferAttachmentParameteriv(target, attachment,
1088 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1090 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1091 dumpFramebufferAttachment(json, context, target, attachment, format);
1095 glReadBuffer(read_buffer);
1098 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1099 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1102 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1107 dumpFramebuffer(JSONWriter &json, Context &context)
1109 json.beginMember("framebuffer");
1112 GLint boundDrawFbo = 0, boundReadFbo = 0;
1113 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1114 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1115 if (!boundDrawFbo) {
1116 dumpDrawableImages(json, context);
1117 } else if (context.ES) {
1118 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1120 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1121 GLint draw_buffer0 = GL_NONE;
1122 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1123 bool multisample = false;
1126 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1129 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1130 if (object_type == GL_RENDERBUFFER) {
1131 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1132 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1134 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1140 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1141 if (object_type == GL_RENDERBUFFER) {
1142 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1143 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1145 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1151 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1152 if (object_type == GL_RENDERBUFFER) {
1153 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1154 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1156 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1162 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1169 // glReadPixels doesnt support multisampled buffers so we need
1170 // to blit the fbo to a temporary one
1171 fboCopy = downsampledFramebuffer(context,
1172 boundDrawFbo, draw_buffer0,
1173 colorRb, depthRb, stencilRb,
1177 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1180 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1181 glDeleteRenderbuffers(numRbs, rbs);
1182 glDeleteFramebuffers(1, &fboCopy);
1185 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1186 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1190 json.endMember(); // framebuffer
1194 } /* namespace glstate */