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)
236 return getActiveTextureLevelDescOES(context, target, level, desc);
239 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &desc.internalFormat);
242 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &desc.width);
244 if (target == GL_TEXTURE_1D) {
249 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &desc.height);
250 if (target != GL_TEXTURE_3D) {
254 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &desc.depth);
263 * OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
264 * texture to a framebuffer.
267 getTexImageOES(GLenum target, GLint level, ImageDesc &desc, GLubyte *pixels)
269 memset(pixels, 0x80, desc.height * desc.width * 4);
271 GLenum texture_binding = GL_NONE;
274 texture_binding = GL_TEXTURE_BINDING_2D;
276 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
277 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
278 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
279 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
280 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
281 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
282 texture_binding = GL_TEXTURE_BINDING_CUBE_MAP;
284 case GL_TEXTURE_3D_OES:
285 texture_binding = GL_TEXTURE_BINDING_3D_OES;
291 glGetIntegerv(texture_binding, &texture);
298 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
299 glGenFramebuffers(1, &fbo);
300 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
306 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
307 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
308 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
309 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
310 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
311 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
312 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
313 status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
314 if (status != GL_FRAMEBUFFER_COMPLETE) {
315 std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
317 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
319 case GL_TEXTURE_3D_OES:
320 for (int i = 0; i < desc.depth; i++) {
321 glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
322 glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * desc.width * desc.height);
327 glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);
329 glDeleteFramebuffers(1, &fbo);
333 static inline GLboolean
334 isDepthFormat(GLenum internalFormat)
336 switch (internalFormat) {
337 case GL_DEPTH_COMPONENT:
338 case GL_DEPTH_COMPONENT16:
339 case GL_DEPTH_COMPONENT24:
340 case GL_DEPTH_COMPONENT32:
341 case GL_DEPTH_COMPONENT32F:
342 case GL_DEPTH_COMPONENT32F_NV:
343 case GL_DEPTH_STENCIL:
344 case GL_DEPTH24_STENCIL8:
345 case GL_DEPTH32F_STENCIL8:
346 case GL_DEPTH32F_STENCIL8_NV:
354 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
357 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
363 GLint active_texture = GL_TEXTURE0;
364 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
365 snprintf(label, sizeof label, "%s, %s, level = %d",
366 enumToString(active_texture), enumToString(target), level);
368 json.beginMember(label);
374 if (!context.ES && isDepthFormat(desc.internalFormat)) {
375 format = GL_DEPTH_COMPONENT;
382 // Tell the GUI this is no ordinary object, but an image
383 json.writeStringMember("__class__", "image");
385 json.writeNumberMember("__width__", desc.width);
386 json.writeNumberMember("__height__", desc.height);
387 json.writeNumberMember("__depth__", desc.depth);
389 json.writeStringMember("__format__", enumToString(desc.internalFormat));
391 // Hardcoded for now, but we could chose types more adequate to the
392 // texture internal format
393 json.writeStringMember("__type__", "uint8");
394 json.writeBoolMember("__normalized__", true);
395 json.writeNumberMember("__channels__", channels);
397 GLubyte *pixels = new GLubyte[desc.depth*desc.width*desc.height*channels];
399 context.resetPixelPackState();
402 getTexImageOES(target, level, desc, pixels);
404 glGetTexImage(target, level, format, GL_UNSIGNED_BYTE, pixels);
407 context.restorePixelPackState();
409 json.beginMember("__data__");
412 image::writePixelsToBuffer(pixels, desc.width, desc.height, channels, true, &pngBuffer, &pngBufferSize);
413 json.writeBase64(pngBuffer, pngBufferSize);
415 json.endMember(); // __data__
423 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
425 GLint texture_binding = 0;
426 glGetIntegerv(binding, &texture_binding);
427 if (!glIsEnabled(target) && !texture_binding) {
434 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
438 if (target == GL_TEXTURE_CUBE_MAP) {
439 for (int face = 0; face < 6; ++face) {
440 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
443 dumpActiveTextureLevel(json, context, target, level);
452 dumpTextures(JSONWriter &json, Context &context)
454 json.beginMember("textures");
456 GLint active_texture = GL_TEXTURE0;
457 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
459 GLint max_texture_coords = 0;
460 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
461 GLint max_combined_texture_image_units = 0;
462 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
463 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
466 * At least the Android software GL implementation doesn't return the
467 * proper value for this, but rather returns 0. The GL(ES) specification
468 * mandates a minimum value of 2, so use this as a fall-back value.
470 max_units = std::max(max_units, 2);
472 for (GLint unit = 0; unit < max_units; ++unit) {
473 GLenum texture = GL_TEXTURE0 + unit;
474 glActiveTexture(texture);
475 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
476 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
477 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
478 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
479 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
481 glActiveTexture(active_texture);
483 json.endMember(); // textures
488 getDrawableBounds(GLint *width, GLint *height) {
489 #if defined(__linux__)
490 if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) {
491 EGLContext currentContext = eglGetCurrentContext();
492 if (currentContext == EGL_NO_CONTEXT) {
496 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
497 if (currentSurface == EGL_NO_SURFACE) {
501 EGLDisplay currentDisplay = eglGetCurrentDisplay();
502 if (currentDisplay == EGL_NO_DISPLAY) {
506 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
507 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
517 HDC hDC = wglGetCurrentDC();
522 HWND hWnd = WindowFromDC(hDC);
525 if (!GetClientRect(hWnd, &rect)) {
529 *width = rect.right - rect.left;
530 *height = rect.bottom - rect.top;
533 #elif defined(__APPLE__)
535 CGLContextObj ctx = CGLGetCurrentContext();
544 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
550 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
554 *width = rect.size.width;
555 *height = rect.size.height;
558 #elif defined(HAVE_X11)
564 unsigned int w, h, bw, depth;
566 display = glXGetCurrentDisplay();
571 drawable = glXGetCurrentDrawable();
572 if (drawable == None) {
576 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
592 static const GLenum texture_bindings[][2] = {
593 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
594 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
595 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
596 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
597 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
602 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
605 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
606 target = texture_bindings[i][0];
608 GLenum binding = texture_bindings[i][1];
610 while (glGetError() != GL_NO_ERROR)
613 glGetIntegerv(binding, &bound_texture);
614 glBindTexture(target, texture);
616 if (glGetError() == GL_NO_ERROR) {
620 glBindTexture(target, bound_texture);
630 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
633 GLint bound_texture = 0;
634 if (!bindTexture(texture, target, bound_texture)) {
638 getActiveTextureLevelDesc(context, target, level, desc);
640 glBindTexture(target, bound_texture);
647 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
649 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
650 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
653 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
660 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
662 GLint bound_renderbuffer = 0;
663 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
664 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
666 getBoundRenderbufferDesc(context, desc);
668 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
675 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
677 GLint object_type = GL_NONE;
678 glGetFramebufferAttachmentParameteriv(target, attachment,
679 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
681 if (object_type == GL_NONE) {
685 GLint object_name = 0;
686 glGetFramebufferAttachmentParameteriv(target, attachment,
687 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
689 if (object_name == 0) {
693 if (object_type == GL_RENDERBUFFER) {
694 return getRenderbufferDesc(context, object_name, desc);
695 } else if (object_type == GL_TEXTURE) {
696 GLint texture_level = 0;
697 glGetFramebufferAttachmentParameteriv(target, attachment,
698 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
700 return getTextureLevelDesc(context, object_name, texture_level, desc);
702 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
710 getDrawBufferImage() {
711 GLenum format = GL_RGB;
712 GLint channels = _gl_format_channels(format);
719 GLenum framebuffer_binding;
720 GLenum framebuffer_target;
722 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
723 framebuffer_target = GL_FRAMEBUFFER;
725 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
726 framebuffer_target = GL_DRAW_FRAMEBUFFER;
729 GLint draw_framebuffer = 0;
730 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
732 GLint draw_buffer = GL_NONE;
734 if (draw_framebuffer) {
735 if (context.ARB_draw_buffers) {
736 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
737 if (draw_buffer == GL_NONE) {
742 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
747 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
748 if (draw_buffer == GL_NONE) {
753 if (!getDrawableBounds(&desc.width, &desc.height)) {
760 GLenum type = GL_UNSIGNED_BYTE;
763 if (format == GL_DEPTH_COMPONENT) {
764 type = GL_UNSIGNED_INT;
769 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
774 while (glGetError() != GL_NO_ERROR) {}
776 GLint read_framebuffer = 0;
777 GLint read_buffer = GL_NONE;
779 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
780 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
782 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
783 glReadBuffer(draw_buffer);
786 // TODO: reset imaging state too
787 context.resetPixelPackState();
789 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
791 context.restorePixelPackState();
794 glReadBuffer(read_buffer);
795 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
798 GLenum error = glGetError();
799 if (error != GL_NO_ERROR) {
801 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
802 error = glGetError();
803 } while(error != GL_NO_ERROR);
813 * Dump the image of the currently bound read buffer.
816 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
817 GLint internalFormat = GL_NONE)
819 GLint channels = _gl_format_channels(format);
825 // Tell the GUI this is no ordinary object, but an image
826 json.writeStringMember("__class__", "image");
828 json.writeNumberMember("__width__", width);
829 json.writeNumberMember("__height__", height);
830 json.writeNumberMember("__depth__", 1);
832 json.writeStringMember("__format__", enumToString(internalFormat));
834 // Hardcoded for now, but we could chose types more adequate to the
835 // texture internal format
836 json.writeStringMember("__type__", "uint8");
837 json.writeBoolMember("__normalized__", true);
838 json.writeNumberMember("__channels__", channels);
840 GLenum type = GL_UNSIGNED_BYTE;
843 if (format == GL_DEPTH_COMPONENT) {
844 type = GL_UNSIGNED_INT;
849 GLubyte *pixels = new GLubyte[width*height*channels];
851 // TODO: reset imaging state too
852 context.resetPixelPackState();
854 glReadPixels(0, 0, width, height, format, type, pixels);
856 context.restorePixelPackState();
858 json.beginMember("__data__");
861 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
862 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
863 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
864 json.writeBase64(pngBuffer, pngBufferSize);
866 json.endMember(); // __data__
874 downsampledFramebuffer(Context &context,
875 GLuint oldFbo, GLint drawbuffer,
876 GLint colorRb, GLint depthRb, GLint stencilRb,
877 GLuint *rbs, GLint *numRbs)
884 glGenFramebuffers(1, &fbo);
885 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
890 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
891 getBoundRenderbufferDesc(context, desc);
893 glGenRenderbuffers(1, &rbs[*numRbs]);
894 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
895 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
896 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
897 GL_RENDERBUFFER, rbs[*numRbs]);
899 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
900 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
901 glDrawBuffer(drawbuffer);
902 glReadBuffer(drawbuffer);
903 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
904 GL_COLOR_BUFFER_BIT, GL_NEAREST);
905 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
909 if (stencilRb == depthRb && stencilRb) {
910 //combined depth and stencil buffer
912 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
913 getBoundRenderbufferDesc(context, desc);
915 glGenRenderbuffers(1, &rbs[*numRbs]);
916 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
917 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
918 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
919 GL_RENDERBUFFER, rbs[*numRbs]);
920 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
921 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
922 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
923 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
924 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
929 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
930 getBoundRenderbufferDesc(context, desc);
932 glGenRenderbuffers(1, &rbs[*numRbs]);
933 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
934 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
935 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
937 GL_RENDERBUFFER, rbs[*numRbs]);
938 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
939 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
940 glDrawBuffer(GL_DEPTH_ATTACHMENT);
941 glReadBuffer(GL_DEPTH_ATTACHMENT);
942 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
943 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
948 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
949 getBoundRenderbufferDesc(context, desc);
951 glGenRenderbuffers(1, &rbs[*numRbs]);
952 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
953 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
954 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
955 GL_STENCIL_ATTACHMENT,
956 GL_RENDERBUFFER, rbs[*numRbs]);
957 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
958 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
959 glDrawBuffer(GL_STENCIL_ATTACHMENT);
960 glReadBuffer(GL_STENCIL_ATTACHMENT);
961 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
962 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
972 * Dump images of current draw drawable/window.
975 dumpDrawableImages(JSONWriter &json, Context &context)
979 if (!getDrawableBounds(&width, &height)) {
983 GLint draw_buffer = GL_NONE;
985 draw_buffer = GL_BACK;
987 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
988 glReadBuffer(draw_buffer);
991 if (draw_buffer != GL_NONE) {
992 GLint read_buffer = GL_NONE;
994 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
997 GLint alpha_bits = 0;
999 // XXX: Ignore alpha until we are able to match the traced visual
1000 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1002 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1003 json.beginMember(enumToString(draw_buffer));
1004 dumpReadBufferImage(json, width, height, format);
1008 glReadBuffer(read_buffer);
1013 GLint depth_bits = 0;
1014 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1016 json.beginMember("GL_DEPTH_COMPONENT");
1017 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1021 GLint stencil_bits = 0;
1022 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1024 json.beginMember("GL_STENCIL_INDEX");
1025 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1033 * Dump the specified framebuffer attachment.
1035 * In the case of a color attachment, it assumes it is already bound for read.
1038 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
1041 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1045 json.beginMember(enumToString(attachment));
1046 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1052 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1054 GLint read_framebuffer = 0;
1055 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1057 GLint read_buffer = GL_NONE;
1058 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1060 GLint max_draw_buffers = 1;
1061 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1062 GLint max_color_attachments = 0;
1063 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1065 for (GLint i = 0; i < max_draw_buffers; ++i) {
1066 GLint draw_buffer = GL_NONE;
1067 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1068 if (draw_buffer != GL_NONE) {
1069 glReadBuffer(draw_buffer);
1071 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1072 attachment = draw_buffer;
1074 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1075 attachment = GL_COLOR_ATTACHMENT0;
1077 GLint alpha_size = 0;
1078 glGetFramebufferAttachmentParameteriv(target, attachment,
1079 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1081 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1082 dumpFramebufferAttachment(json, context, target, attachment, format);
1086 glReadBuffer(read_buffer);
1089 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1090 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1093 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1098 dumpFramebuffer(JSONWriter &json, Context &context)
1100 json.beginMember("framebuffer");
1103 GLint boundDrawFbo = 0, boundReadFbo = 0;
1104 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1105 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1106 if (!boundDrawFbo) {
1107 dumpDrawableImages(json, context);
1108 } else if (context.ES) {
1109 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1111 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1112 GLint draw_buffer0 = GL_NONE;
1113 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1114 bool multisample = false;
1117 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1120 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1121 if (object_type == GL_RENDERBUFFER) {
1122 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1123 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1125 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1131 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1132 if (object_type == GL_RENDERBUFFER) {
1133 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1134 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1136 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1142 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1143 if (object_type == GL_RENDERBUFFER) {
1144 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1145 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1147 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1153 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1160 // glReadPixels doesnt support multisampled buffers so we need
1161 // to blit the fbo to a temporary one
1162 fboCopy = downsampledFramebuffer(context,
1163 boundDrawFbo, draw_buffer0,
1164 colorRb, depthRb, stencilRb,
1168 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1171 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1172 glDeleteRenderbuffers(numRbs, rbs);
1173 glDeleteFramebuffers(1, &fboCopy);
1176 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1177 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1181 json.endMember(); // framebuffer
1185 } /* namespace glstate */