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__ */
79 internalFormat(GL_NONE)
83 operator == (const ImageDesc &other) const {
84 return width == other.width &&
85 height == other.height &&
86 depth == other.depth &&
87 samples == other.samples &&
88 internalFormat == other.internalFormat;
93 return width > 0 && height > 0 && depth > 0;
99 * Sames as enumToString, but with special provision to handle formatsLUMINANCE_ALPHA.
101 * OpenGL 2.1 specification states that "internalFormat may (for backwards
102 * compatibility with the 1.0 version of the GL) also take on the integer
103 * values 1, 2, 3, and 4, which are equivalent to symbolic constants LUMINANCE,
104 * LUMINANCE ALPHA, RGB, and RGBA respectively".
107 formatToString(GLenum internalFormat) {
108 switch (internalFormat) {
110 return "GL_LUMINANCE";
112 return "GL_LUMINANCE_ALPHA";
118 return enumToString(internalFormat);
124 * OpenGL ES does not support glGetTexLevelParameteriv, but it is possible to
125 * probe whether a texture has a given size by crafting a dummy glTexSubImage()
129 probeTextureLevelSizeOES(GLenum target, GLint level, const GLint size[3]) {
130 while (glGetError() != GL_NO_ERROR)
133 GLenum internalFormat = GL_RGBA;
134 GLenum type = GL_UNSIGNED_BYTE;
139 case GL_TEXTURE_CUBE_MAP:
140 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
141 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
142 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
143 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
144 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
145 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
146 glTexSubImage2D(target, level, size[0], size[1], 0, 0, internalFormat, type, &dummy);
148 case GL_TEXTURE_3D_OES:
149 glTexSubImage3DOES(target, level, size[0], size[1], size[2], 0, 0, 0, internalFormat, type, &dummy);
155 GLenum error = glGetError();
158 std::cerr << "(" << size[0] << ", " << size[1] << ", " << size[2] << ") = " << enumToString(error) << "\n";
161 if (error == GL_NO_ERROR) {
165 while (glGetError() != GL_NO_ERROR)
173 * Bisect the texture size along an axis.
175 * It is assumed that the texture exists.
178 bisectTextureLevelSizeOES(GLenum target, GLint level, GLint axis, GLint max) {
179 GLint size[3] = {0, 0, 0};
186 GLint test = (min + max) / 2;
193 if (probeTextureLevelSizeOES(target, level, size)) {
203 * Special path to obtain texture size on OpenGL ES, that does not rely on
204 * glGetTexLevelParameteriv
207 getActiveTextureLevelDescOES(Context &context, GLenum target, GLint level, ImageDesc &desc)
209 if (target == GL_TEXTURE_1D) {
210 // OpenGL ES does not support 1D textures
214 const GLint size[3] = {1, 1, 1};
215 if (!probeTextureLevelSizeOES(target, level, size)) {
220 desc.internalFormat = GL_RGBA;
225 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize);
226 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
227 desc.height = bisectTextureLevelSizeOES(target, level, 1, maxSize);
230 case GL_TEXTURE_CUBE_MAP:
231 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
232 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
233 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
234 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
235 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
236 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
237 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxSize);
238 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
239 desc.height = desc.width;
242 case GL_TEXTURE_3D_OES:
243 glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_OES, &maxSize);
244 desc.width = bisectTextureLevelSizeOES(target, level, 0, maxSize);
245 desc.width = bisectTextureLevelSizeOES(target, level, 1, maxSize);
246 desc.depth = bisectTextureLevelSizeOES(target, level, 2, maxSize);
254 << enumToString(target) << " "
256 << desc.width << "x" << desc.height << "x" << desc.depth
265 getActiveTextureLevelDesc(Context &context, GLenum target, GLint level, ImageDesc &desc)
267 assert(target != GL_TEXTURE_CUBE_MAP);
270 return getActiveTextureLevelDescOES(context, target, level, desc);
273 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &desc.internalFormat);
276 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &desc.width);
278 if (target == GL_TEXTURE_1D) {
283 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &desc.height);
284 if (target != GL_TEXTURE_3D) {
288 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &desc.depth);
292 glGetTexLevelParameteriv(target, level, GL_TEXTURE_SAMPLES, &desc.samples);
299 getTextureBinding(GLenum target)
303 return GL_TEXTURE_BINDING_1D;
304 case GL_TEXTURE_1D_ARRAY:
305 return GL_TEXTURE_BINDING_1D_ARRAY;
307 return GL_TEXTURE_BINDING_2D;
308 case GL_TEXTURE_2D_MULTISAMPLE:
309 return GL_TEXTURE_BINDING_2D_MULTISAMPLE;
310 case GL_TEXTURE_2D_ARRAY:
311 return GL_TEXTURE_BINDING_2D_ARRAY;
312 case GL_TEXTURE_RECTANGLE:
313 return GL_TEXTURE_BINDING_RECTANGLE;
314 case GL_TEXTURE_CUBE_MAP:
315 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
316 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
317 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
318 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
319 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
320 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
321 return GL_TEXTURE_BINDING_CUBE_MAP;
322 case GL_TEXTURE_CUBE_MAP_ARRAY:
323 return GL_TEXTURE_BINDING_CUBE_MAP_ARRAY;
325 return GL_TEXTURE_BINDING_3D;
334 * OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
335 * texture to a framebuffer.
338 getTexImageOES(GLenum target, GLint level, ImageDesc &desc, GLubyte *pixels)
340 memset(pixels, 0x80, desc.height * desc.width * 4);
342 GLenum texture_binding = getTextureBinding(target);
343 if (texture_binding == GL_NONE) {
348 glGetIntegerv(texture_binding, &texture);
355 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
356 glGenFramebuffers(1, &fbo);
357 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
363 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
364 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
365 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
366 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
367 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
368 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
369 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
370 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
371 if (status != GL_FRAMEBUFFER_COMPLETE) {
372 std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
374 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
376 case GL_TEXTURE_3D_OES:
377 for (int i = 0; i < desc.depth; i++) {
378 glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
379 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * desc.width * desc.height);
384 glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);
386 glDeleteFramebuffers(1, &fbo);
390 struct InternalFormatDesc
392 GLenum internalFormat;
397 static const InternalFormatDesc
398 internalFormatDescs[] = {
406 {GL_GREEN, GL_GREEN},
408 {GL_ALPHA, GL_ALPHA},
414 {GL_LUMINANCE, GL_LUMINANCE},
415 {GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA},
416 {GL_INTENSITY, GL_INTENSITY},
423 {GL_RGBA16, GL_RGBA},
424 {GL_RGB10_A2, GL_RGBA},
425 {GL_LUMINANCE8, GL_LUMINANCE},
426 {GL_LUMINANCE16, GL_LUMINANCE},
427 {GL_ALPHA8, GL_ALPHA},
428 {GL_ALPHA16, GL_ALPHA},
429 {GL_LUMINANCE8_ALPHA8, GL_LUMINANCE_ALPHA},
430 {GL_LUMINANCE16_ALPHA16, GL_LUMINANCE_ALPHA},
431 {GL_INTENSITY8, GL_INTENSITY},
432 {GL_INTENSITY16, GL_INTENSITY},
434 {GL_RED_INTEGER, GL_RED_INTEGER},
435 {GL_GREEN_INTEGER, GL_GREEN_INTEGER},
436 {GL_BLUE_INTEGER, GL_BLUE_INTEGER},
437 {GL_ALPHA_INTEGER, GL_ALPHA_INTEGER},
438 {GL_RG_INTEGER, GL_RG_INTEGER},
439 {GL_RGB_INTEGER, GL_RGB_INTEGER},
440 {GL_BGR_INTEGER, GL_RGB_INTEGER},
441 {GL_RGBA_INTEGER, GL_RGBA_INTEGER},
442 {GL_BGRA_INTEGER, GL_RGBA_INTEGER},
443 {GL_LUMINANCE_INTEGER_EXT, GL_LUMINANCE_INTEGER_EXT},
444 {GL_LUMINANCE_ALPHA_INTEGER_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT},
446 {GL_R8I, GL_RED_INTEGER},
447 {GL_R8UI, GL_RED_INTEGER},
448 {GL_R16I, GL_RED_INTEGER},
449 {GL_R16UI, GL_RED_INTEGER},
450 {GL_R32I, GL_RED_INTEGER},
451 {GL_R32UI, GL_RED_INTEGER},
452 {GL_RG8I, GL_RG_INTEGER},
453 {GL_RG8UI, GL_RG_INTEGER},
454 {GL_RG16I, GL_RG_INTEGER},
455 {GL_RG16UI, GL_RG_INTEGER},
456 {GL_RG32I, GL_RG_INTEGER},
457 {GL_RG32UI, GL_RG_INTEGER},
458 {GL_RGB8I, GL_RGB_INTEGER},
459 {GL_RGB8UI, GL_RGB_INTEGER},
460 {GL_RGB16I, GL_RGB_INTEGER},
461 {GL_RGB16UI, GL_RGB_INTEGER},
462 {GL_RGB32I, GL_RGB_INTEGER},
463 {GL_RGB32UI, GL_RGB_INTEGER},
464 {GL_RGBA8I, GL_RGBA_INTEGER},
465 {GL_RGBA8UI, GL_RGBA_INTEGER},
466 {GL_RGBA16I, GL_RGBA_INTEGER},
467 {GL_RGBA16UI, GL_RGBA_INTEGER},
468 {GL_RGBA32I, GL_RGBA_INTEGER},
469 {GL_RGBA32UI, GL_RGBA_INTEGER},
470 {GL_RGB10_A2UI, GL_RGBA_INTEGER},
471 {GL_LUMINANCE8I_EXT, GL_LUMINANCE_INTEGER_EXT},
472 {GL_LUMINANCE8UI_EXT, GL_LUMINANCE_INTEGER_EXT},
473 {GL_LUMINANCE16I_EXT, GL_LUMINANCE_INTEGER_EXT},
474 {GL_LUMINANCE16UI_EXT, GL_LUMINANCE_INTEGER_EXT},
475 {GL_LUMINANCE32I_EXT, GL_LUMINANCE_INTEGER_EXT},
476 {GL_LUMINANCE32UI_EXT, GL_LUMINANCE_INTEGER_EXT},
477 {GL_ALPHA8I_EXT, GL_ALPHA_INTEGER_EXT},
478 {GL_ALPHA8UI_EXT, GL_ALPHA_INTEGER_EXT},
479 {GL_ALPHA16I_EXT, GL_ALPHA_INTEGER_EXT},
480 {GL_ALPHA16UI_EXT, GL_ALPHA_INTEGER_EXT},
481 {GL_ALPHA32I_EXT, GL_ALPHA_INTEGER_EXT},
482 {GL_ALPHA32UI_EXT, GL_ALPHA_INTEGER_EXT},
483 {GL_LUMINANCE_ALPHA8I_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT},
484 {GL_LUMINANCE_ALPHA8UI_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT},
485 {GL_LUMINANCE_ALPHA16I_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT},
486 {GL_LUMINANCE_ALPHA16UI_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT},
487 {GL_LUMINANCE_ALPHA32I_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT},
488 {GL_LUMINANCE_ALPHA32UI_EXT, GL_LUMINANCE_ALPHA_INTEGER_EXT},
489 {GL_INTENSITY8I_EXT, GL_RED_INTEGER},
490 {GL_INTENSITY8UI_EXT, GL_RED_INTEGER},
491 {GL_INTENSITY16I_EXT, GL_RED_INTEGER},
492 {GL_INTENSITY16UI_EXT, GL_RED_INTEGER},
493 {GL_INTENSITY32I_EXT, GL_RED_INTEGER},
494 {GL_INTENSITY32UI_EXT, GL_RED_INTEGER},
496 {GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT},
497 {GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT},
498 {GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT},
499 {GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT},
500 {GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT},
501 {GL_DEPTH_COMPONENT32F_NV, GL_DEPTH_COMPONENT},
502 {GL_DEPTH_STENCIL, GL_DEPTH_COMPONENT},
503 {GL_DEPTH24_STENCIL8, GL_DEPTH_COMPONENT},
504 {GL_DEPTH32F_STENCIL8, GL_DEPTH_COMPONENT},
505 {GL_DEPTH32F_STENCIL8_NV, GL_DEPTH_COMPONENT},
510 * Choose the glReadPixels/glGetTexImage format appropriate for the given
514 getFormat(GLenum internalFormat)
516 for (unsigned i = 0; i < sizeof internalFormatDescs / sizeof internalFormatDescs[0]; ++i) {
517 if (internalFormatDescs[i].internalFormat == internalFormat) {
518 return internalFormatDescs[i].format;
526 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
529 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
534 GLint active_texture = GL_TEXTURE0;
535 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
536 snprintf(label, sizeof label, "%s, %s, level = %d",
537 enumToString(active_texture), enumToString(target), level);
539 json.beginMember(label);
541 GLenum format = getFormat(desc.internalFormat);
542 if (context.ES && format == GL_DEPTH_COMPONENT) {
545 GLuint channels = _gl_format_channels(format);
547 image::Image *image = new image::Image(desc.width, desc.height*desc.depth, channels, true);
549 context.resetPixelPackState();
552 getTexImageOES(target, level, desc, image->pixels);
554 glGetTexImage(target, level, format, GL_UNSIGNED_BYTE, image->pixels);
557 context.restorePixelPackState();
559 json.writeImage(image, formatToString(desc.internalFormat), desc.depth);
563 json.endMember(); // label
568 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
570 GLint texture_binding = 0;
571 glGetIntegerv(binding, &texture_binding);
572 if (!glIsEnabled(target) && !texture_binding) {
580 if (target == GL_TEXTURE_CUBE_MAP) {
581 for (int face = 0; face < 6; ++face) {
582 if (!getActiveTextureLevelDesc(context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, desc)) {
585 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
588 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
591 dumpActiveTextureLevel(json, context, target, level);
600 dumpTextures(JSONWriter &json, Context &context)
602 json.beginMember("textures");
604 GLint active_texture = GL_TEXTURE0;
605 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
607 GLint max_texture_coords = 0;
608 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
609 GLint max_combined_texture_image_units = 0;
610 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
611 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
614 * At least the Android software GL implementation doesn't return the
615 * proper value for this, but rather returns 0. The GL(ES) specification
616 * mandates a minimum value of 2, so use this as a fall-back value.
618 max_units = std::max(max_units, 2);
620 for (GLint unit = 0; unit < max_units; ++unit) {
621 GLenum texture = GL_TEXTURE0 + unit;
622 glActiveTexture(texture);
623 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
624 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
625 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
626 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
627 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
629 glActiveTexture(active_texture);
631 json.endMember(); // textures
636 getDrawableBounds(GLint *width, GLint *height) {
637 #if defined(__linux__)
638 if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) {
639 EGLContext currentContext = eglGetCurrentContext();
640 if (currentContext != EGL_NO_CONTEXT) {
641 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
642 if (currentSurface == EGL_NO_SURFACE) {
646 EGLDisplay currentDisplay = eglGetCurrentDisplay();
647 if (currentDisplay == EGL_NO_DISPLAY) {
651 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
652 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
663 HDC hDC = wglGetCurrentDC();
668 HWND hWnd = WindowFromDC(hDC);
671 if (!GetClientRect(hWnd, &rect)) {
675 *width = rect.right - rect.left;
676 *height = rect.bottom - rect.top;
679 #elif defined(__APPLE__)
681 CGLContextObj ctx = CGLGetCurrentContext();
690 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
696 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
700 *width = rect.size.width;
701 *height = rect.size.height;
704 #elif defined(HAVE_X11)
710 unsigned int w, h, bw, depth;
712 display = glXGetCurrentDisplay();
717 drawable = glXGetCurrentDrawable();
718 if (drawable == None) {
722 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
738 struct TextureTargetBinding
749 GL_TEXTURE_RECTANGLE,
752 GL_TEXTURE_2D_MULTISAMPLE,
755 GL_TEXTURE_CUBE_MAP_ARRAY,
760 getTextureTarget(GLint texture)
762 if (!glIsTexture(texture)) {
766 for (unsigned i = 0; i < sizeof(textureTargets)/sizeof(textureTargets[0]); ++i) {
767 GLenum target = textureTargets[i];
768 GLenum binding = getTextureBinding(target);
770 while (glGetError() != GL_NO_ERROR)
773 GLint bound_texture = 0;
774 glGetIntegerv(binding, &bound_texture);
775 glBindTexture(target, texture);
777 bool succeeded = glGetError() == GL_NO_ERROR;
779 glBindTexture(target, bound_texture);
791 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
793 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
794 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
797 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &desc.samples);
799 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
806 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
808 GLint bound_renderbuffer = 0;
809 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
810 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
812 getBoundRenderbufferDesc(context, desc);
814 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
821 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
823 GLint object_type = GL_NONE;
824 glGetFramebufferAttachmentParameteriv(target, attachment,
825 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
827 if (object_type == GL_NONE) {
831 GLint object_name = 0;
832 glGetFramebufferAttachmentParameteriv(target, attachment,
833 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
835 if (object_name == 0) {
839 if (object_type == GL_RENDERBUFFER) {
840 return getRenderbufferDesc(context, object_name, desc);
841 } else if (object_type == GL_TEXTURE) {
842 GLint texture_face = 0;
843 glGetFramebufferAttachmentParameteriv(target, attachment,
844 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE,
847 GLint texture_level = 0;
848 glGetFramebufferAttachmentParameteriv(target, attachment,
849 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
852 GLint bound_texture = 0;
853 if (texture_face != 0) {
854 glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &bound_texture);
855 glBindTexture(GL_TEXTURE_CUBE_MAP, object_name);
856 getActiveTextureLevelDesc(context, texture_face, texture_level, desc);
857 glBindTexture(GL_TEXTURE_CUBE_MAP, bound_texture);
859 GLenum texture_target = getTextureTarget(object_name);
860 GLenum texture_binding = getTextureBinding(texture_target);
861 glGetIntegerv(texture_binding, &bound_texture);
862 glBindTexture(texture_target, object_name);
863 getActiveTextureLevelDesc(context, texture_target, texture_level, desc);
864 glBindTexture(texture_target, bound_texture);
869 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
877 getDrawBufferImage() {
878 GLenum format = GL_RGB;
879 GLint channels = _gl_format_channels(format);
886 GLenum framebuffer_binding;
887 GLenum framebuffer_target;
889 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
890 framebuffer_target = GL_FRAMEBUFFER;
892 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
893 framebuffer_target = GL_DRAW_FRAMEBUFFER;
896 GLint draw_framebuffer = 0;
897 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
899 GLint draw_buffer = GL_NONE;
901 if (draw_framebuffer) {
902 if (context.ARB_draw_buffers) {
903 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
904 if (draw_buffer == GL_NONE) {
908 // GL_COLOR_ATTACHMENT0 is implied
909 draw_buffer = GL_COLOR_ATTACHMENT0;
912 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
917 // XXX: Draw buffer is always FRONT for single buffer context, BACK
918 // for double buffered contexts. There is no way to know which (as
919 // GL_DOUBLEBUFFER state is also unavailable), so always assume
921 draw_buffer = GL_BACK;
923 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
924 if (draw_buffer == GL_NONE) {
929 if (!getDrawableBounds(&desc.width, &desc.height)) {
936 GLenum type = GL_UNSIGNED_BYTE;
937 image::ChannelType channelType = image::TYPE_UNORM8;
939 if (format == GL_DEPTH_COMPONENT) {
942 channelType = image::TYPE_FLOAT;
945 image::Image *image = new image::Image(desc.width, desc.height, channels, true, channelType);
950 while (glGetError() != GL_NO_ERROR) {}
952 GLint read_framebuffer = 0;
953 GLint read_buffer = GL_NONE;
955 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
956 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
958 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
959 glReadBuffer(draw_buffer);
962 // TODO: reset imaging state too
963 context.resetPixelPackState();
965 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
967 context.restorePixelPackState();
970 glReadBuffer(read_buffer);
971 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
974 GLenum error = glGetError();
975 if (error != GL_NO_ERROR) {
977 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
978 error = glGetError();
979 } while(error != GL_NO_ERROR);
989 * Dump the image of the currently bound read buffer.
992 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
993 GLint internalFormat = GL_NONE)
995 GLint channels = _gl_format_channels(format);
997 if (internalFormat == GL_NONE) {
998 internalFormat = format;
1003 GLenum type = GL_UNSIGNED_BYTE;
1004 image::ChannelType channelType = image::TYPE_UNORM8;
1006 if (format == GL_DEPTH_COMPONENT) {
1009 channelType = image::TYPE_FLOAT;
1012 image::Image *image = new image::Image(width, height, channels, true, channelType);
1014 while (glGetError() != GL_NO_ERROR) {}
1016 // TODO: reset imaging state too
1017 context.resetPixelPackState();
1019 glReadPixels(0, 0, width, height, format, type, image->pixels);
1021 context.restorePixelPackState();
1023 GLenum error = glGetError();
1024 if (error != GL_NO_ERROR) {
1026 std::cerr << "warning: " << enumToString(error) << " while reading framebuffer\n";
1027 error = glGetError();
1028 } while(error != GL_NO_ERROR);
1031 json.writeImage(image, formatToString(internalFormat));
1038 static inline GLuint
1039 downsampledFramebuffer(Context &context,
1040 GLuint oldFbo, GLint drawbuffer,
1041 const ImageDesc &colorDesc,
1042 const ImageDesc &depthDesc,
1043 const ImageDesc &stencilDesc,
1044 GLuint *rbs, GLint *numRbs)
1051 glGenFramebuffers(1, &fbo);
1052 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1056 glGenRenderbuffers(1, &rbs[*numRbs]);
1057 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1058 glRenderbufferStorage(GL_RENDERBUFFER, colorDesc.internalFormat, colorDesc.width, colorDesc.height);
1059 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
1060 GL_RENDERBUFFER, rbs[*numRbs]);
1062 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1063 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1064 glDrawBuffer(drawbuffer);
1065 glReadBuffer(drawbuffer);
1066 glBlitFramebuffer(0, 0, colorDesc.width, colorDesc.height, 0, 0, colorDesc.width, colorDesc.height,
1067 GL_COLOR_BUFFER_BIT, GL_NEAREST);
1068 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1072 if (stencilDesc == depthDesc &&
1073 depthDesc.valid()) {
1074 //combined depth and stencil buffer
1075 glGenRenderbuffers(1, &rbs[*numRbs]);
1076 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1077 glRenderbufferStorage(GL_RENDERBUFFER, depthDesc.internalFormat, depthDesc.width, depthDesc.height);
1078 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
1079 GL_RENDERBUFFER, rbs[*numRbs]);
1080 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1081 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1082 glBlitFramebuffer(0, 0, depthDesc.width, depthDesc.height, 0, 0, depthDesc.width, depthDesc.height,
1083 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1084 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1087 if (depthDesc.valid()) {
1088 glGenRenderbuffers(1, &rbs[*numRbs]);
1089 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1090 glRenderbufferStorage(GL_RENDERBUFFER, depthDesc.internalFormat, depthDesc.width, depthDesc.height);
1091 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1092 GL_DEPTH_ATTACHMENT,
1093 GL_RENDERBUFFER, rbs[*numRbs]);
1094 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1095 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1096 glDrawBuffer(GL_DEPTH_ATTACHMENT);
1097 glReadBuffer(GL_DEPTH_ATTACHMENT);
1098 glBlitFramebuffer(0, 0, depthDesc.width, depthDesc.height, 0, 0, depthDesc.width, depthDesc.height,
1099 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
1102 if (stencilDesc.valid()) {
1103 glGenRenderbuffers(1, &rbs[*numRbs]);
1104 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1105 glRenderbufferStorage(GL_RENDERBUFFER, stencilDesc.internalFormat, stencilDesc.width, stencilDesc.height);
1106 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1107 GL_STENCIL_ATTACHMENT,
1108 GL_RENDERBUFFER, rbs[*numRbs]);
1109 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1110 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1111 glDrawBuffer(GL_STENCIL_ATTACHMENT);
1112 glReadBuffer(GL_STENCIL_ATTACHMENT);
1113 glBlitFramebuffer(0, 0, stencilDesc.width, stencilDesc.height, 0, 0, stencilDesc.width, stencilDesc.height,
1114 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1124 * Dump images of current draw drawable/window.
1127 dumpDrawableImages(JSONWriter &json, Context &context)
1129 GLint width, height;
1131 if (!getDrawableBounds(&width, &height)) {
1135 GLint draw_buffer = GL_NONE;
1137 // XXX: Draw buffer is always FRONT for single buffer context, BACK for
1138 // double buffered contexts. There is no way to know which (as
1139 // GL_DOUBLEBUFFER state is also unavailable), so always assume
1140 // double-buffering.
1141 draw_buffer = GL_BACK;
1143 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1146 if (draw_buffer != GL_NONE) {
1147 // Read from current draw buffer
1148 GLint read_buffer = GL_NONE;
1150 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1151 glReadBuffer(draw_buffer);
1154 GLint alpha_bits = 0;
1156 // XXX: Ignore alpha until we are able to match the traced visual
1157 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1159 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1160 json.beginMember(enumToString(draw_buffer));
1161 dumpReadBufferImage(json, width, height, format);
1164 // Restore original read buffer
1166 glReadBuffer(read_buffer);
1171 GLint depth_bits = 0;
1172 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1174 json.beginMember("GL_DEPTH_COMPONENT");
1175 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1179 GLint stencil_bits = 0;
1180 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1182 json.beginMember("GL_STENCIL_INDEX");
1183 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1191 * Dump the specified framebuffer attachment.
1193 * In the case of a color attachment, it assumes it is already bound for read.
1196 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format = GL_NONE)
1199 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1203 assert(desc.samples == 0);
1205 if (format == GL_NONE) {
1206 assert(desc.internalFormat != GL_NONE);
1207 format = getFormat(desc.internalFormat);
1210 json.beginMember(enumToString(attachment));
1211 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1217 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1219 GLint read_framebuffer = 0;
1220 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1222 GLint read_buffer = GL_NONE;
1223 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1225 GLint max_draw_buffers = 1;
1226 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1227 GLint max_color_attachments = 0;
1228 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1230 for (GLint i = 0; i < max_draw_buffers; ++i) {
1231 GLint draw_buffer = GL_NONE;
1232 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1233 if (draw_buffer != GL_NONE) {
1234 glReadBuffer(draw_buffer);
1236 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1237 attachment = draw_buffer;
1239 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1240 attachment = GL_COLOR_ATTACHMENT0;
1242 dumpFramebufferAttachment(json, context, target, attachment);
1246 glReadBuffer(read_buffer);
1249 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1250 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1253 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1258 dumpFramebuffer(JSONWriter &json, Context &context)
1260 json.beginMember("framebuffer");
1263 GLint boundDrawFbo = 0, boundReadFbo = 0;
1264 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1265 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1266 if (!boundDrawFbo) {
1267 dumpDrawableImages(json, context);
1268 } else if (context.ES) {
1269 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1271 GLint draw_buffer0 = GL_NONE;
1272 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1273 bool multisample = false;
1276 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1278 ImageDesc colorDesc;
1279 if (getFramebufferAttachmentDesc(context, GL_DRAW_FRAMEBUFFER, draw_buffer0, colorDesc)) {
1280 if (colorDesc.samples) {
1285 ImageDesc depthDesc;
1286 if (getFramebufferAttachmentDesc(context, GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthDesc)) {
1287 if (depthDesc.samples) {
1292 ImageDesc stencilDesc;
1293 if (getFramebufferAttachmentDesc(context, GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, stencilDesc)) {
1294 if (stencilDesc.samples) {
1304 // glReadPixels doesnt support multisampled buffers so we need
1305 // to blit the fbo to a temporary one
1306 fboCopy = downsampledFramebuffer(context,
1307 boundDrawFbo, draw_buffer0,
1308 colorDesc, depthDesc, stencilDesc,
1311 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundDrawFbo);
1314 dumpFramebufferAttachments(json, context, GL_READ_FRAMEBUFFER);
1317 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1318 glDeleteRenderbuffers(numRbs, rbs);
1319 glDeleteFramebuffers(1, &fboCopy);
1322 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1323 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1327 json.endMember(); // framebuffer
1331 } /* namespace glstate */