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 **************************************************************************/
38 #include "glstate.hpp"
39 #include "glstate_internal.hpp"
48 #include <Carbon/Carbon.h>
54 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
60 #endif /* __APPLE__ */
63 /* Change thi to one to force interpreting depth buffers as RGBA, which enables
64 * visualizing full dynamic range, until we can transmit HDR images to the GUI */
65 #define DEPTH_AS_RGBA 0
83 internalFormat(GL_NONE)
88 return width > 0 && height > 0 && depth > 0;
94 * Sames as enumToString, but with special provision to handle formatsLUMINANCE_ALPHA.
96 * OpenGL 2.1 specification states that "internalFormat may (for backwards
97 * compatibility with the 1.0 version of the GL) also take on the integer
98 * values 1, 2, 3, and 4, which are equivalent to symbolic constants LUMINANCE,
99 * LUMINANCE ALPHA, RGB, and RGBA respectively".
102 formatToString(GLenum internalFormat) {
103 switch (internalFormat) {
105 return "GL_LUMINANCE";
107 return "GL_LUMINANCE_ALPHA";
113 return enumToString(internalFormat);
119 * OpenGL ES does not support glGetTexLevelParameteriv, but it is possible to
120 * probe whether a texture has a given size by crafting a dummy glTexSubImage()
124 probeTextureLevelSizeOES(GLenum target, GLint level, const GLint size[3]) {
125 while (glGetError() != GL_NO_ERROR)
128 GLenum internalFormat = GL_RGBA;
129 GLenum type = GL_UNSIGNED_BYTE;
134 case GL_TEXTURE_CUBE_MAP:
135 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
136 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
137 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
138 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
139 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
140 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
141 glTexSubImage2D(target, level, size[0], size[1], 0, 0, internalFormat, type, &dummy);
143 case GL_TEXTURE_3D_OES:
144 glTexSubImage3DOES(target, level, size[0], size[1], size[2], 0, 0, 0, internalFormat, type, &dummy);
150 GLenum error = glGetError();
153 std::cerr << "(" << size[0] << ", " << size[1] << ", " << size[2] << ") = " << enumToString(error) << "\n";
156 if (error == GL_NO_ERROR) {
160 while (glGetError() != GL_NO_ERROR)
168 * Bisect the texture size along an axis.
170 * It is assumed that the texture exists.
173 bisectTextureLevelSizeOES(GLenum target, GLint level, GLint axis, GLint max) {
174 GLint size[3] = {0, 0, 0};
181 GLint test = (min + max) / 2;
188 if (probeTextureLevelSizeOES(target, level, size)) {
198 * Special path to obtain texture size on OpenGL ES, that does not rely on
199 * glGetTexLevelParameteriv
202 getActiveTextureLevelDescOES(Context &context, GLenum target, GLint level, ImageDesc &desc)
204 if (target == GL_TEXTURE_1D) {
205 // OpenGL ES does not support 1D textures
209 const GLint size[3] = {1, 1, 1};
210 if (!probeTextureLevelSizeOES(target, level, size)) {
215 desc.internalFormat = GL_RGBA;
220 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
221 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
222 desc.height = bisectTextureLevelSizeOES(target, level, 1, maxSize);
225 case GL_TEXTURE_CUBE_MAP:
226 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
227 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
228 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
229 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
230 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
231 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
232 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxSize);
233 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
234 desc.height = desc.width;
237 case GL_TEXTURE_3D_OES:
238 glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_OES, &maxSize);
239 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
240 desc.width = bisectTextureLevelSizeOES(target, level, 1, maxSize);
241 desc.depth = bisectTextureLevelSizeOES(target, level, 2, maxSize);
249 << enumToString(target) << " "
251 << desc.width << "x" << desc.height << "x" << desc.depth
260 getActiveTextureLevelDesc(Context &context, GLenum target, GLint level, ImageDesc &desc)
262 assert(target != GL_TEXTURE_CUBE_MAP);
265 return getActiveTextureLevelDescOES(context, target, level, desc);
268 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &desc.internalFormat);
271 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &desc.width);
273 if (target == GL_TEXTURE_1D) {
278 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &desc.height);
279 if (target != GL_TEXTURE_3D) {
283 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &desc.depth);
292 * OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
293 * texture to a framebuffer.
296 getTexImageOES(GLenum target, GLint level, ImageDesc &desc, GLubyte *pixels)
298 memset(pixels, 0x80, desc.height * desc.width * 4);
300 GLenum texture_binding = GL_NONE;
303 texture_binding = GL_TEXTURE_BINDING_2D;
305 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
306 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
307 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
308 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
309 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
310 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
311 texture_binding = GL_TEXTURE_BINDING_CUBE_MAP;
313 case GL_TEXTURE_3D_OES:
314 texture_binding = GL_TEXTURE_BINDING_3D_OES;
320 glGetIntegerv(texture_binding, &texture);
327 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
328 glGenFramebuffers(1, &fbo);
329 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
335 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
336 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
337 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
338 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
339 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
340 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
341 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
342 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
343 if (status != GL_FRAMEBUFFER_COMPLETE) {
344 std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
346 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
348 case GL_TEXTURE_3D_OES:
349 for (int i = 0; i < desc.depth; i++) {
350 glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
351 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * desc.width * desc.height);
356 glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);
358 glDeleteFramebuffers(1, &fbo);
362 static inline GLboolean
363 isDepthFormat(GLenum internalFormat)
365 switch (internalFormat) {
366 case GL_DEPTH_COMPONENT:
367 case GL_DEPTH_COMPONENT16:
368 case GL_DEPTH_COMPONENT24:
369 case GL_DEPTH_COMPONENT32:
370 case GL_DEPTH_COMPONENT32F:
371 case GL_DEPTH_COMPONENT32F_NV:
372 case GL_DEPTH_STENCIL:
373 case GL_DEPTH24_STENCIL8:
374 case GL_DEPTH32F_STENCIL8:
375 case GL_DEPTH32F_STENCIL8_NV:
383 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
386 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
392 GLint active_texture = GL_TEXTURE0;
393 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
394 snprintf(label, sizeof label, "%s, %s, level = %d",
395 enumToString(active_texture), enumToString(target), level);
397 json.beginMember(label);
403 if (!context.ES && isDepthFormat(desc.internalFormat)) {
404 format = GL_DEPTH_COMPONENT;
411 // Tell the GUI this is no ordinary object, but an image
412 json.writeStringMember("__class__", "image");
414 json.writeIntMember("__width__", desc.width);
415 json.writeIntMember("__height__", desc.height);
416 json.writeIntMember("__depth__", desc.depth);
418 json.writeStringMember("__format__", formatToString(desc.internalFormat));
420 image::Image *image = new image::Image(desc.width, desc.height*desc.depth, channels, true);
422 context.resetPixelPackState();
425 getTexImageOES(target, level, desc, image->pixels);
427 glGetTexImage(target, level, format, GL_UNSIGNED_BYTE, image->pixels);
430 context.restorePixelPackState();
432 json.beginMember("__data__");
433 std::stringstream ss;
435 const std::string & s = ss.str();
436 json.writeBase64(s.data(), s.size());
437 json.endMember(); // __data__
445 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
447 GLint texture_binding = 0;
448 glGetIntegerv(binding, &texture_binding);
449 if (!glIsEnabled(target) && !texture_binding) {
457 if (target == GL_TEXTURE_CUBE_MAP) {
458 for (int face = 0; face < 6; ++face) {
459 if (!getActiveTextureLevelDesc(context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, desc)) {
462 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
465 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
468 dumpActiveTextureLevel(json, context, target, level);
477 dumpTextures(JSONWriter &json, Context &context)
479 json.beginMember("textures");
481 GLint active_texture = GL_TEXTURE0;
482 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
484 GLint max_texture_coords = 0;
485 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
486 GLint max_combined_texture_image_units = 0;
487 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
488 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
491 * At least the Android software GL implementation doesn't return the
492 * proper value for this, but rather returns 0. The GL(ES) specification
493 * mandates a minimum value of 2, so use this as a fall-back value.
495 max_units = std::max(max_units, 2);
497 for (GLint unit = 0; unit < max_units; ++unit) {
498 GLenum texture = GL_TEXTURE0 + unit;
499 glActiveTexture(texture);
500 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
501 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
502 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
503 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
504 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
506 glActiveTexture(active_texture);
508 json.endMember(); // textures
513 getDrawableBounds(GLint *width, GLint *height) {
514 #if defined(__linux__)
515 if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) {
516 EGLContext currentContext = eglGetCurrentContext();
517 if (currentContext == EGL_NO_CONTEXT) {
521 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
522 if (currentSurface == EGL_NO_SURFACE) {
526 EGLDisplay currentDisplay = eglGetCurrentDisplay();
527 if (currentDisplay == EGL_NO_DISPLAY) {
531 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
532 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
542 HDC hDC = wglGetCurrentDC();
547 HWND hWnd = WindowFromDC(hDC);
550 if (!GetClientRect(hWnd, &rect)) {
554 *width = rect.right - rect.left;
555 *height = rect.bottom - rect.top;
558 #elif defined(__APPLE__)
560 CGLContextObj ctx = CGLGetCurrentContext();
569 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
575 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
579 *width = rect.size.width;
580 *height = rect.size.height;
583 #elif defined(HAVE_X11)
589 unsigned int w, h, bw, depth;
591 display = glXGetCurrentDisplay();
596 drawable = glXGetCurrentDrawable();
597 if (drawable == None) {
601 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
617 static const GLenum texture_bindings[][2] = {
618 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
619 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
620 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
621 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
622 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
627 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
630 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
631 target = texture_bindings[i][0];
633 GLenum binding = texture_bindings[i][1];
635 while (glGetError() != GL_NO_ERROR)
638 glGetIntegerv(binding, &bound_texture);
639 glBindTexture(target, texture);
641 if (glGetError() == GL_NO_ERROR) {
645 glBindTexture(target, bound_texture);
655 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
658 GLint bound_texture = 0;
659 if (!bindTexture(texture, target, bound_texture)) {
663 getActiveTextureLevelDesc(context, target, level, desc);
665 glBindTexture(target, bound_texture);
672 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
674 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
675 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
678 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
685 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
687 GLint bound_renderbuffer = 0;
688 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
689 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
691 getBoundRenderbufferDesc(context, desc);
693 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
700 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
702 GLint object_type = GL_NONE;
703 glGetFramebufferAttachmentParameteriv(target, attachment,
704 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
706 if (object_type == GL_NONE) {
710 GLint object_name = 0;
711 glGetFramebufferAttachmentParameteriv(target, attachment,
712 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
714 if (object_name == 0) {
718 if (object_type == GL_RENDERBUFFER) {
719 return getRenderbufferDesc(context, object_name, desc);
720 } else if (object_type == GL_TEXTURE) {
721 GLint texture_level = 0;
722 glGetFramebufferAttachmentParameteriv(target, attachment,
723 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
725 return getTextureLevelDesc(context, object_name, texture_level, desc);
727 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
735 getDrawBufferImage() {
736 GLenum format = GL_RGB;
737 GLint channels = _gl_format_channels(format);
744 GLenum framebuffer_binding;
745 GLenum framebuffer_target;
747 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
748 framebuffer_target = GL_FRAMEBUFFER;
750 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
751 framebuffer_target = GL_DRAW_FRAMEBUFFER;
754 GLint draw_framebuffer = 0;
755 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
757 GLint draw_buffer = GL_NONE;
759 if (draw_framebuffer) {
760 if (context.ARB_draw_buffers) {
761 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
762 if (draw_buffer == GL_NONE) {
767 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
772 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
773 if (draw_buffer == GL_NONE) {
778 if (!getDrawableBounds(&desc.width, &desc.height)) {
785 GLenum type = GL_UNSIGNED_BYTE;
788 if (format == GL_DEPTH_COMPONENT) {
789 type = GL_UNSIGNED_INT;
794 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
799 while (glGetError() != GL_NO_ERROR) {}
801 GLint read_framebuffer = 0;
802 GLint read_buffer = GL_NONE;
804 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
805 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
807 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
808 glReadBuffer(draw_buffer);
811 // TODO: reset imaging state too
812 context.resetPixelPackState();
814 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
816 context.restorePixelPackState();
819 glReadBuffer(read_buffer);
820 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
823 GLenum error = glGetError();
824 if (error != GL_NO_ERROR) {
826 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
827 error = glGetError();
828 } while(error != GL_NO_ERROR);
838 * Dump the image of the currently bound read buffer.
841 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
842 GLint internalFormat = GL_NONE)
844 GLint channels = _gl_format_channels(format);
846 if (internalFormat == GL_NONE) {
847 internalFormat = format;
854 // Tell the GUI this is no ordinary object, but an image
855 json.writeStringMember("__class__", "image");
857 json.writeIntMember("__width__", width);
858 json.writeIntMember("__height__", height);
859 json.writeIntMember("__depth__", 1);
861 json.writeStringMember("__format__", formatToString(internalFormat));
863 GLenum type = GL_UNSIGNED_BYTE;
866 if (format == GL_DEPTH_COMPONENT) {
867 type = GL_UNSIGNED_INT;
872 image::Image *image = new image::Image(width, height, channels, true);
874 // TODO: reset imaging state too
875 context.resetPixelPackState();
877 glReadPixels(0, 0, width, height, format, type, image->pixels);
879 context.restorePixelPackState();
881 json.beginMember("__data__");
882 std::stringstream ss;
884 const std::string & s = ss.str();
885 json.writeBase64(s.data(), s.size());
886 json.endMember(); // __data__
894 downsampledFramebuffer(Context &context,
895 GLuint oldFbo, GLint drawbuffer,
896 GLint colorRb, GLint depthRb, GLint stencilRb,
897 GLuint *rbs, GLint *numRbs)
904 glGenFramebuffers(1, &fbo);
905 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
910 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
911 getBoundRenderbufferDesc(context, desc);
913 glGenRenderbuffers(1, &rbs[*numRbs]);
914 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
915 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
916 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
917 GL_RENDERBUFFER, rbs[*numRbs]);
919 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
920 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
921 glDrawBuffer(drawbuffer);
922 glReadBuffer(drawbuffer);
923 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
924 GL_COLOR_BUFFER_BIT, GL_NEAREST);
925 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
929 if (stencilRb == depthRb && stencilRb) {
930 //combined depth and stencil buffer
932 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
933 getBoundRenderbufferDesc(context, desc);
935 glGenRenderbuffers(1, &rbs[*numRbs]);
936 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
937 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
938 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
939 GL_RENDERBUFFER, rbs[*numRbs]);
940 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
941 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
942 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
943 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
944 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
949 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
950 getBoundRenderbufferDesc(context, desc);
952 glGenRenderbuffers(1, &rbs[*numRbs]);
953 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
954 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
955 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
957 GL_RENDERBUFFER, rbs[*numRbs]);
958 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
959 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
960 glDrawBuffer(GL_DEPTH_ATTACHMENT);
961 glReadBuffer(GL_DEPTH_ATTACHMENT);
962 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
963 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
968 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
969 getBoundRenderbufferDesc(context, desc);
971 glGenRenderbuffers(1, &rbs[*numRbs]);
972 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
973 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
974 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
975 GL_STENCIL_ATTACHMENT,
976 GL_RENDERBUFFER, rbs[*numRbs]);
977 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
978 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
979 glDrawBuffer(GL_STENCIL_ATTACHMENT);
980 glReadBuffer(GL_STENCIL_ATTACHMENT);
981 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
982 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
992 * Dump images of current draw drawable/window.
995 dumpDrawableImages(JSONWriter &json, Context &context)
999 if (!getDrawableBounds(&width, &height)) {
1003 GLint draw_buffer = GL_NONE;
1005 draw_buffer = GL_BACK;
1007 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1008 glReadBuffer(draw_buffer);
1011 if (draw_buffer != GL_NONE) {
1012 GLint read_buffer = GL_NONE;
1014 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1017 GLint alpha_bits = 0;
1019 // XXX: Ignore alpha until we are able to match the traced visual
1020 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1022 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1023 json.beginMember(enumToString(draw_buffer));
1024 dumpReadBufferImage(json, width, height, format);
1028 glReadBuffer(read_buffer);
1033 GLint depth_bits = 0;
1034 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1036 json.beginMember("GL_DEPTH_COMPONENT");
1037 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1041 GLint stencil_bits = 0;
1042 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1044 json.beginMember("GL_STENCIL_INDEX");
1045 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1053 * Dump the specified framebuffer attachment.
1055 * In the case of a color attachment, it assumes it is already bound for read.
1058 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
1061 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1065 json.beginMember(enumToString(attachment));
1066 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1072 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1074 GLint read_framebuffer = 0;
1075 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1077 GLint read_buffer = GL_NONE;
1078 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1080 GLint max_draw_buffers = 1;
1081 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1082 GLint max_color_attachments = 0;
1083 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1085 for (GLint i = 0; i < max_draw_buffers; ++i) {
1086 GLint draw_buffer = GL_NONE;
1087 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1088 if (draw_buffer != GL_NONE) {
1089 glReadBuffer(draw_buffer);
1091 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1092 attachment = draw_buffer;
1094 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1095 attachment = GL_COLOR_ATTACHMENT0;
1097 GLint alpha_size = 0;
1098 glGetFramebufferAttachmentParameteriv(target, attachment,
1099 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1101 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1102 dumpFramebufferAttachment(json, context, target, attachment, format);
1106 glReadBuffer(read_buffer);
1109 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1110 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1113 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1118 dumpFramebuffer(JSONWriter &json, Context &context)
1120 json.beginMember("framebuffer");
1123 GLint boundDrawFbo = 0, boundReadFbo = 0;
1124 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1125 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1126 if (!boundDrawFbo) {
1127 dumpDrawableImages(json, context);
1128 } else if (context.ES) {
1129 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1131 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1132 GLint draw_buffer0 = GL_NONE;
1133 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1134 bool multisample = false;
1137 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1140 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1141 if (object_type == GL_RENDERBUFFER) {
1142 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1143 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1145 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1151 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1152 if (object_type == GL_RENDERBUFFER) {
1153 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1154 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1156 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1162 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1163 if (object_type == GL_RENDERBUFFER) {
1164 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1165 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1167 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1173 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1180 // glReadPixels doesnt support multisampled buffers so we need
1181 // to blit the fbo to a temporary one
1182 fboCopy = downsampledFramebuffer(context,
1183 boundDrawFbo, draw_buffer0,
1184 colorRb, depthRb, stencilRb,
1188 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1191 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1192 glDeleteRenderbuffers(numRbs, rbs);
1193 glDeleteFramebuffers(1, &fboCopy);
1196 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1197 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1201 json.endMember(); // framebuffer
1205 } /* namespace glstate */