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)) {
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);
399 if (!context.ES && isDepthFormat(desc.internalFormat)) {
400 format = GL_DEPTH_COMPONENT;
407 image::Image *image = new image::Image(desc.width, desc.height*desc.depth, channels, true);
409 context.resetPixelPackState();
412 getTexImageOES(target, level, desc, image->pixels);
414 glGetTexImage(target, level, format, GL_UNSIGNED_BYTE, image->pixels);
417 context.restorePixelPackState();
419 json.writeImage(image, formatToString(desc.internalFormat), desc.depth);
423 json.endMember(); // label
428 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
430 GLint texture_binding = 0;
431 glGetIntegerv(binding, &texture_binding);
432 if (!glIsEnabled(target) && !texture_binding) {
440 if (target == GL_TEXTURE_CUBE_MAP) {
441 for (int face = 0; face < 6; ++face) {
442 if (!getActiveTextureLevelDesc(context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, desc)) {
445 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
448 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
451 dumpActiveTextureLevel(json, context, target, level);
460 dumpTextures(JSONWriter &json, Context &context)
462 json.beginMember("textures");
464 GLint active_texture = GL_TEXTURE0;
465 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
467 GLint max_texture_coords = 0;
468 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
469 GLint max_combined_texture_image_units = 0;
470 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
471 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
474 * At least the Android software GL implementation doesn't return the
475 * proper value for this, but rather returns 0. The GL(ES) specification
476 * mandates a minimum value of 2, so use this as a fall-back value.
478 max_units = std::max(max_units, 2);
480 for (GLint unit = 0; unit < max_units; ++unit) {
481 GLenum texture = GL_TEXTURE0 + unit;
482 glActiveTexture(texture);
483 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
484 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
485 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
486 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
487 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
489 glActiveTexture(active_texture);
491 json.endMember(); // textures
496 getDrawableBounds(GLint *width, GLint *height) {
497 #if defined(__linux__)
498 if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) {
499 EGLContext currentContext = eglGetCurrentContext();
500 if (currentContext == EGL_NO_CONTEXT) {
504 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
505 if (currentSurface == EGL_NO_SURFACE) {
509 EGLDisplay currentDisplay = eglGetCurrentDisplay();
510 if (currentDisplay == EGL_NO_DISPLAY) {
514 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
515 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
525 HDC hDC = wglGetCurrentDC();
530 HWND hWnd = WindowFromDC(hDC);
533 if (!GetClientRect(hWnd, &rect)) {
537 *width = rect.right - rect.left;
538 *height = rect.bottom - rect.top;
541 #elif defined(__APPLE__)
543 CGLContextObj ctx = CGLGetCurrentContext();
552 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
558 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
562 *width = rect.size.width;
563 *height = rect.size.height;
566 #elif defined(HAVE_X11)
572 unsigned int w, h, bw, depth;
574 display = glXGetCurrentDisplay();
579 drawable = glXGetCurrentDrawable();
580 if (drawable == None) {
584 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
600 static const GLenum texture_bindings[][2] = {
601 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
602 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
603 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
604 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
605 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
610 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
613 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
614 target = texture_bindings[i][0];
616 GLenum binding = texture_bindings[i][1];
618 while (glGetError() != GL_NO_ERROR)
621 glGetIntegerv(binding, &bound_texture);
622 glBindTexture(target, texture);
624 if (glGetError() == GL_NO_ERROR) {
628 glBindTexture(target, bound_texture);
638 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
641 GLint bound_texture = 0;
642 if (!bindTexture(texture, target, bound_texture)) {
646 getActiveTextureLevelDesc(context, target, level, desc);
648 glBindTexture(target, bound_texture);
655 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
657 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
658 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
661 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
668 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
670 GLint bound_renderbuffer = 0;
671 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
672 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
674 getBoundRenderbufferDesc(context, desc);
676 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
683 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
685 GLint object_type = GL_NONE;
686 glGetFramebufferAttachmentParameteriv(target, attachment,
687 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
689 if (object_type == GL_NONE) {
693 GLint object_name = 0;
694 glGetFramebufferAttachmentParameteriv(target, attachment,
695 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
697 if (object_name == 0) {
701 if (object_type == GL_RENDERBUFFER) {
702 return getRenderbufferDesc(context, object_name, desc);
703 } else if (object_type == GL_TEXTURE) {
704 GLint texture_level = 0;
705 glGetFramebufferAttachmentParameteriv(target, attachment,
706 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
708 return getTextureLevelDesc(context, object_name, texture_level, desc);
710 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
718 getDrawBufferImage() {
719 GLenum format = GL_RGB;
720 GLint channels = _gl_format_channels(format);
727 GLenum framebuffer_binding;
728 GLenum framebuffer_target;
730 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
731 framebuffer_target = GL_FRAMEBUFFER;
733 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
734 framebuffer_target = GL_DRAW_FRAMEBUFFER;
737 GLint draw_framebuffer = 0;
738 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
740 GLint draw_buffer = GL_NONE;
742 if (draw_framebuffer) {
743 if (context.ARB_draw_buffers) {
744 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
745 if (draw_buffer == GL_NONE) {
750 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
755 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
756 if (draw_buffer == GL_NONE) {
761 if (!getDrawableBounds(&desc.width, &desc.height)) {
768 GLenum type = GL_UNSIGNED_BYTE;
771 if (format == GL_DEPTH_COMPONENT) {
772 type = GL_UNSIGNED_INT;
777 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
782 while (glGetError() != GL_NO_ERROR) {}
784 GLint read_framebuffer = 0;
785 GLint read_buffer = GL_NONE;
787 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
788 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
790 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
791 glReadBuffer(draw_buffer);
794 // TODO: reset imaging state too
795 context.resetPixelPackState();
797 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
799 context.restorePixelPackState();
802 glReadBuffer(read_buffer);
803 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
806 GLenum error = glGetError();
807 if (error != GL_NO_ERROR) {
809 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
810 error = glGetError();
811 } while(error != GL_NO_ERROR);
821 * Dump the image of the currently bound read buffer.
824 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
825 GLint internalFormat = GL_NONE)
827 GLint channels = _gl_format_channels(format);
829 if (internalFormat == GL_NONE) {
830 internalFormat = format;
835 GLenum type = GL_UNSIGNED_BYTE;
838 if (format == GL_DEPTH_COMPONENT) {
839 type = GL_UNSIGNED_INT;
844 image::Image *image = new image::Image(width, height, channels, true);
846 // TODO: reset imaging state too
847 context.resetPixelPackState();
849 glReadPixels(0, 0, width, height, format, type, image->pixels);
851 context.restorePixelPackState();
853 json.writeImage(image, formatToString(internalFormat));
860 downsampledFramebuffer(Context &context,
861 GLuint oldFbo, GLint drawbuffer,
862 GLint colorRb, GLint depthRb, GLint stencilRb,
863 GLuint *rbs, GLint *numRbs)
870 glGenFramebuffers(1, &fbo);
871 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
876 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
877 getBoundRenderbufferDesc(context, desc);
879 glGenRenderbuffers(1, &rbs[*numRbs]);
880 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
881 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
882 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
883 GL_RENDERBUFFER, rbs[*numRbs]);
885 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
886 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
887 glDrawBuffer(drawbuffer);
888 glReadBuffer(drawbuffer);
889 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
890 GL_COLOR_BUFFER_BIT, GL_NEAREST);
891 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
895 if (stencilRb == depthRb && stencilRb) {
896 //combined depth and stencil buffer
898 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
899 getBoundRenderbufferDesc(context, desc);
901 glGenRenderbuffers(1, &rbs[*numRbs]);
902 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
903 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
904 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
905 GL_RENDERBUFFER, rbs[*numRbs]);
906 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
907 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
908 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
909 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
910 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
915 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
916 getBoundRenderbufferDesc(context, desc);
918 glGenRenderbuffers(1, &rbs[*numRbs]);
919 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
920 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
921 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
923 GL_RENDERBUFFER, rbs[*numRbs]);
924 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
925 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
926 glDrawBuffer(GL_DEPTH_ATTACHMENT);
927 glReadBuffer(GL_DEPTH_ATTACHMENT);
928 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
929 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
934 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
935 getBoundRenderbufferDesc(context, desc);
937 glGenRenderbuffers(1, &rbs[*numRbs]);
938 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
939 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
940 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
941 GL_STENCIL_ATTACHMENT,
942 GL_RENDERBUFFER, rbs[*numRbs]);
943 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
944 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
945 glDrawBuffer(GL_STENCIL_ATTACHMENT);
946 glReadBuffer(GL_STENCIL_ATTACHMENT);
947 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
948 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
958 * Dump images of current draw drawable/window.
961 dumpDrawableImages(JSONWriter &json, Context &context)
965 if (!getDrawableBounds(&width, &height)) {
969 GLint draw_buffer = GL_NONE;
971 draw_buffer = GL_BACK;
973 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
974 glReadBuffer(draw_buffer);
977 if (draw_buffer != GL_NONE) {
978 GLint read_buffer = GL_NONE;
980 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
983 GLint alpha_bits = 0;
985 // XXX: Ignore alpha until we are able to match the traced visual
986 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
988 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
989 json.beginMember(enumToString(draw_buffer));
990 dumpReadBufferImage(json, width, height, format);
994 glReadBuffer(read_buffer);
999 GLint depth_bits = 0;
1000 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1002 json.beginMember("GL_DEPTH_COMPONENT");
1003 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1007 GLint stencil_bits = 0;
1008 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1010 json.beginMember("GL_STENCIL_INDEX");
1011 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1019 * Dump the specified framebuffer attachment.
1021 * In the case of a color attachment, it assumes it is already bound for read.
1024 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
1027 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1031 json.beginMember(enumToString(attachment));
1032 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1038 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1040 GLint read_framebuffer = 0;
1041 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1043 GLint read_buffer = GL_NONE;
1044 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1046 GLint max_draw_buffers = 1;
1047 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1048 GLint max_color_attachments = 0;
1049 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1051 for (GLint i = 0; i < max_draw_buffers; ++i) {
1052 GLint draw_buffer = GL_NONE;
1053 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1054 if (draw_buffer != GL_NONE) {
1055 glReadBuffer(draw_buffer);
1057 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1058 attachment = draw_buffer;
1060 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1061 attachment = GL_COLOR_ATTACHMENT0;
1063 GLint alpha_size = 0;
1064 glGetFramebufferAttachmentParameteriv(target, attachment,
1065 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1067 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1068 dumpFramebufferAttachment(json, context, target, attachment, format);
1072 glReadBuffer(read_buffer);
1075 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1076 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1079 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1084 dumpFramebuffer(JSONWriter &json, Context &context)
1086 json.beginMember("framebuffer");
1089 GLint boundDrawFbo = 0, boundReadFbo = 0;
1090 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1091 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1092 if (!boundDrawFbo) {
1093 dumpDrawableImages(json, context);
1094 } else if (context.ES) {
1095 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1097 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1098 GLint draw_buffer0 = GL_NONE;
1099 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1100 bool multisample = false;
1103 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1106 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1107 if (object_type == GL_RENDERBUFFER) {
1108 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1109 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1111 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1117 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1118 if (object_type == GL_RENDERBUFFER) {
1119 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1120 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1122 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1128 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1129 if (object_type == GL_RENDERBUFFER) {
1130 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1131 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1133 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1139 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1146 // glReadPixels doesnt support multisampled buffers so we need
1147 // to blit the fbo to a temporary one
1148 fboCopy = downsampledFramebuffer(context,
1149 boundDrawFbo, draw_buffer0,
1150 colorRb, depthRb, stencilRb,
1154 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1157 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1158 glDeleteRenderbuffers(numRbs, rbs);
1159 glDeleteFramebuffers(1, &fboCopy);
1162 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1163 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1167 json.endMember(); // framebuffer
1171 } /* namespace glstate */