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);
334 dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint level)
337 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
343 GLint active_texture = GL_TEXTURE0;
344 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
345 snprintf(label, sizeof label, "%s, %s, level = %d",
346 enumToString(active_texture), enumToString(target), level);
348 json.beginMember(label);
352 // Tell the GUI this is no ordinary object, but an image
353 json.writeStringMember("__class__", "image");
355 json.writeNumberMember("__width__", desc.width);
356 json.writeNumberMember("__height__", desc.height);
357 json.writeNumberMember("__depth__", desc.depth);
359 json.writeStringMember("__format__", enumToString(desc.internalFormat));
361 // Hardcoded for now, but we could chose types more adequate to the
362 // texture internal format
363 json.writeStringMember("__type__", "uint8");
364 json.writeBoolMember("__normalized__", true);
365 json.writeNumberMember("__channels__", 4);
367 GLubyte *pixels = new GLubyte[desc.depth*desc.width*desc.height*4];
369 context.resetPixelPackState();
372 getTexImageOES(target, level, desc, pixels);
374 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
377 context.restorePixelPackState();
379 json.beginMember("__data__");
382 image::writePixelsToBuffer(pixels, desc.width, desc.height, 4, true, &pngBuffer, &pngBufferSize);
383 json.writeBase64(pngBuffer, pngBufferSize);
385 json.endMember(); // __data__
393 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
395 GLint texture_binding = 0;
396 glGetIntegerv(binding, &texture_binding);
397 if (!glIsEnabled(target) && !texture_binding) {
404 if (!getActiveTextureLevelDesc(context, target, level, desc)) {
408 if (target == GL_TEXTURE_CUBE_MAP) {
409 for (int face = 0; face < 6; ++face) {
410 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
413 dumpActiveTextureLevel(json, context, target, level);
422 dumpTextures(JSONWriter &json, Context &context)
424 json.beginMember("textures");
426 GLint active_texture = GL_TEXTURE0;
427 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
429 GLint max_texture_coords = 0;
430 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
431 GLint max_combined_texture_image_units = 0;
432 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
433 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
436 * At least the Android software GL implementation doesn't return the
437 * proper value for this, but rather returns 0. The GL(ES) specification
438 * mandates a minimum value of 2, so use this as a fall-back value.
440 max_units = std::min(max_units, 2);
442 for (GLint unit = 0; unit < max_units; ++unit) {
443 GLenum texture = GL_TEXTURE0 + unit;
444 glActiveTexture(texture);
445 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
446 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
447 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
448 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
449 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
451 glActiveTexture(active_texture);
453 json.endMember(); // textures
458 getDrawableBounds(GLint *width, GLint *height) {
459 #if defined(__linux__)
460 if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) {
461 EGLContext currentContext = eglGetCurrentContext();
462 if (currentContext == EGL_NO_CONTEXT) {
466 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
467 if (currentSurface == EGL_NO_SURFACE) {
471 EGLDisplay currentDisplay = eglGetCurrentDisplay();
472 if (currentDisplay == EGL_NO_DISPLAY) {
476 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
477 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
487 HDC hDC = wglGetCurrentDC();
492 HWND hWnd = WindowFromDC(hDC);
495 if (!GetClientRect(hWnd, &rect)) {
499 *width = rect.right - rect.left;
500 *height = rect.bottom - rect.top;
503 #elif defined(__APPLE__)
505 CGLContextObj ctx = CGLGetCurrentContext();
514 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
520 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
524 *width = rect.size.width;
525 *height = rect.size.height;
528 #elif defined(HAVE_X11)
534 unsigned int w, h, bw, depth;
536 display = glXGetCurrentDisplay();
541 drawable = glXGetCurrentDrawable();
542 if (drawable == None) {
546 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
562 static const GLenum texture_bindings[][2] = {
563 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
564 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
565 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
566 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
567 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
572 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
575 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
576 target = texture_bindings[i][0];
578 GLenum binding = texture_bindings[i][1];
580 while (glGetError() != GL_NO_ERROR)
583 glGetIntegerv(binding, &bound_texture);
584 glBindTexture(target, texture);
586 if (glGetError() == GL_NO_ERROR) {
590 glBindTexture(target, bound_texture);
600 getTextureLevelDesc(Context &context, GLint texture, GLint level, ImageDesc &desc)
603 GLint bound_texture = 0;
604 if (!bindTexture(texture, target, bound_texture)) {
608 getActiveTextureLevelDesc(context, target, level, desc);
610 glBindTexture(target, bound_texture);
617 getBoundRenderbufferDesc(Context &context, ImageDesc &desc)
619 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &desc.width);
620 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &desc.height);
623 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &desc.internalFormat);
630 getRenderbufferDesc(Context &context, GLint renderbuffer, ImageDesc &desc)
632 GLint bound_renderbuffer = 0;
633 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
634 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
636 getBoundRenderbufferDesc(context, desc);
638 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
645 getFramebufferAttachmentDesc(Context &context, GLenum target, GLenum attachment, ImageDesc &desc)
647 GLint object_type = GL_NONE;
648 glGetFramebufferAttachmentParameteriv(target, attachment,
649 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
651 if (object_type == GL_NONE) {
655 GLint object_name = 0;
656 glGetFramebufferAttachmentParameteriv(target, attachment,
657 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
659 if (object_name == 0) {
663 if (object_type == GL_RENDERBUFFER) {
664 return getRenderbufferDesc(context, object_name, desc);
665 } else if (object_type == GL_TEXTURE) {
666 GLint texture_level = 0;
667 glGetFramebufferAttachmentParameteriv(target, attachment,
668 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
670 return getTextureLevelDesc(context, object_name, texture_level, desc);
672 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
680 getDrawBufferImage() {
681 GLenum format = GL_RGB;
682 GLint channels = __gl_format_channels(format);
689 GLenum framebuffer_binding;
690 GLenum framebuffer_target;
692 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
693 framebuffer_target = GL_FRAMEBUFFER;
695 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
696 framebuffer_target = GL_DRAW_FRAMEBUFFER;
699 GLint draw_framebuffer = 0;
700 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
702 GLint draw_buffer = GL_NONE;
704 if (draw_framebuffer) {
705 if (context.ARB_draw_buffers) {
706 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
707 if (draw_buffer == GL_NONE) {
712 if (!getFramebufferAttachmentDesc(context, framebuffer_target, draw_buffer, desc)) {
717 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
718 if (draw_buffer == GL_NONE) {
723 if (!getDrawableBounds(&desc.width, &desc.height)) {
730 GLenum type = GL_UNSIGNED_BYTE;
733 if (format == GL_DEPTH_COMPONENT) {
734 type = GL_UNSIGNED_INT;
739 image::Image *image = new image::Image(desc.width, desc.height, channels, true);
744 while (glGetError() != GL_NO_ERROR) {}
746 GLint read_framebuffer = 0;
747 GLint read_buffer = GL_NONE;
749 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
750 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
752 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
753 glReadBuffer(draw_buffer);
756 // TODO: reset imaging state too
757 context.resetPixelPackState();
759 glReadPixels(0, 0, desc.width, desc.height, format, type, image->pixels);
761 context.restorePixelPackState();
764 glReadBuffer(read_buffer);
765 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
768 GLenum error = glGetError();
769 if (error != GL_NO_ERROR) {
771 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
772 error = glGetError();
773 } while(error != GL_NO_ERROR);
783 * Dump the image of the currently bound read buffer.
786 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
787 GLint internalFormat = GL_NONE)
789 GLint channels = __gl_format_channels(format);
795 // Tell the GUI this is no ordinary object, but an image
796 json.writeStringMember("__class__", "image");
798 json.writeNumberMember("__width__", width);
799 json.writeNumberMember("__height__", height);
800 json.writeNumberMember("__depth__", 1);
802 json.writeStringMember("__format__", enumToString(internalFormat));
804 // Hardcoded for now, but we could chose types more adequate to the
805 // texture internal format
806 json.writeStringMember("__type__", "uint8");
807 json.writeBoolMember("__normalized__", true);
808 json.writeNumberMember("__channels__", channels);
810 GLenum type = GL_UNSIGNED_BYTE;
813 if (format == GL_DEPTH_COMPONENT) {
814 type = GL_UNSIGNED_INT;
819 GLubyte *pixels = new GLubyte[width*height*channels];
821 // TODO: reset imaging state too
822 context.resetPixelPackState();
824 glReadPixels(0, 0, width, height, format, type, pixels);
826 context.restorePixelPackState();
828 json.beginMember("__data__");
831 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
832 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
833 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
834 json.writeBase64(pngBuffer, pngBufferSize);
836 json.endMember(); // __data__
844 downsampledFramebuffer(Context &context,
845 GLuint oldFbo, GLint drawbuffer,
846 GLint colorRb, GLint depthRb, GLint stencilRb,
847 GLuint *rbs, GLint *numRbs)
854 glGenFramebuffers(1, &fbo);
855 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
860 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
861 getBoundRenderbufferDesc(context, desc);
863 glGenRenderbuffers(1, &rbs[*numRbs]);
864 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
865 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
866 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
867 GL_RENDERBUFFER, rbs[*numRbs]);
869 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
870 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
871 glDrawBuffer(drawbuffer);
872 glReadBuffer(drawbuffer);
873 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
874 GL_COLOR_BUFFER_BIT, GL_NEAREST);
875 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
879 if (stencilRb == depthRb && stencilRb) {
880 //combined depth and stencil buffer
882 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
883 getBoundRenderbufferDesc(context, desc);
885 glGenRenderbuffers(1, &rbs[*numRbs]);
886 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
887 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
888 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
889 GL_RENDERBUFFER, rbs[*numRbs]);
890 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
891 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
892 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
893 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
894 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
899 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
900 getBoundRenderbufferDesc(context, desc);
902 glGenRenderbuffers(1, &rbs[*numRbs]);
903 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
904 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
905 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
907 GL_RENDERBUFFER, rbs[*numRbs]);
908 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
909 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
910 glDrawBuffer(GL_DEPTH_ATTACHMENT);
911 glReadBuffer(GL_DEPTH_ATTACHMENT);
912 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
913 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
918 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
919 getBoundRenderbufferDesc(context, desc);
921 glGenRenderbuffers(1, &rbs[*numRbs]);
922 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
923 glRenderbufferStorage(GL_RENDERBUFFER, desc.internalFormat, desc.width, desc.height);
924 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
925 GL_STENCIL_ATTACHMENT,
926 GL_RENDERBUFFER, rbs[*numRbs]);
927 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
928 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
929 glDrawBuffer(GL_STENCIL_ATTACHMENT);
930 glReadBuffer(GL_STENCIL_ATTACHMENT);
931 glBlitFramebuffer(0, 0, desc.width, desc.height, 0, 0, desc.width, desc.height,
932 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
942 * Dump images of current draw drawable/window.
945 dumpDrawableImages(JSONWriter &json, Context &context)
949 if (!getDrawableBounds(&width, &height)) {
953 GLint draw_buffer = GL_NONE;
955 draw_buffer = GL_BACK;
957 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
958 glReadBuffer(draw_buffer);
961 if (draw_buffer != GL_NONE) {
962 GLint read_buffer = GL_NONE;
964 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
967 GLint alpha_bits = 0;
969 // XXX: Ignore alpha until we are able to match the traced visual
970 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
972 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
973 json.beginMember(enumToString(draw_buffer));
974 dumpReadBufferImage(json, width, height, format);
978 glReadBuffer(read_buffer);
983 GLint depth_bits = 0;
984 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
986 json.beginMember("GL_DEPTH_COMPONENT");
987 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
991 GLint stencil_bits = 0;
992 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
994 json.beginMember("GL_STENCIL_INDEX");
995 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1003 * Dump the specified framebuffer attachment.
1005 * In the case of a color attachment, it assumes it is already bound for read.
1008 dumpFramebufferAttachment(JSONWriter &json, Context &context, GLenum target, GLenum attachment, GLenum format)
1011 if (!getFramebufferAttachmentDesc(context, target, attachment, desc)) {
1015 json.beginMember(enumToString(attachment));
1016 dumpReadBufferImage(json, desc.width, desc.height, format, desc.internalFormat);
1022 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1024 GLint read_framebuffer = 0;
1025 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1027 GLint read_buffer = GL_NONE;
1028 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1030 GLint max_draw_buffers = 1;
1031 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1032 GLint max_color_attachments = 0;
1033 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1035 for (GLint i = 0; i < max_draw_buffers; ++i) {
1036 GLint draw_buffer = GL_NONE;
1037 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1038 if (draw_buffer != GL_NONE) {
1039 glReadBuffer(draw_buffer);
1041 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1042 attachment = draw_buffer;
1044 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1045 attachment = GL_COLOR_ATTACHMENT0;
1047 GLint alpha_size = 0;
1048 glGetFramebufferAttachmentParameteriv(target, attachment,
1049 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1051 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1052 dumpFramebufferAttachment(json, context, target, attachment, format);
1056 glReadBuffer(read_buffer);
1059 dumpFramebufferAttachment(json, context, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1060 dumpFramebufferAttachment(json, context, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1063 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1068 dumpFramebuffer(JSONWriter &json, Context &context)
1070 json.beginMember("framebuffer");
1073 GLint boundDrawFbo = 0, boundReadFbo = 0;
1074 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1075 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1076 if (!boundDrawFbo) {
1077 dumpDrawableImages(json, context);
1078 } else if (context.ES) {
1079 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1081 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1082 GLint draw_buffer0 = GL_NONE;
1083 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1084 bool multisample = false;
1087 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1090 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1091 if (object_type == GL_RENDERBUFFER) {
1092 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1093 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1095 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1101 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1102 if (object_type == GL_RENDERBUFFER) {
1103 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1104 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1106 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1112 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1113 if (object_type == GL_RENDERBUFFER) {
1114 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1115 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1117 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1123 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1130 // glReadPixels doesnt support multisampled buffers so we need
1131 // to blit the fbo to a temporary one
1132 fboCopy = downsampledFramebuffer(context,
1133 boundDrawFbo, draw_buffer0,
1134 colorRb, depthRb, stencilRb,
1138 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1141 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1142 glDeleteRenderbuffers(numRbs, rbs);
1143 glDeleteFramebuffers(1, &fboCopy);
1146 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1147 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1151 json.endMember(); // framebuffer
1155 } /* namespace glstate */