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"
43 #include <Carbon/Carbon.h>
49 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
55 #endif /* __APPLE__ */
62 resetPixelPackState(void) {
63 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
64 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
65 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
66 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
67 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
68 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
69 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
70 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
71 glPixelStorei(GL_PACK_ALIGNMENT, 1);
72 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
77 restorePixelPackState(void) {
82 // Mapping from shader type to shader source, used to accumulated the sources
83 // of different shaders with same type.
84 typedef std::map<std::string, std::string> ShaderMap;
88 getShaderSource(ShaderMap &shaderMap, GLuint shader)
94 GLint shader_type = 0;
95 glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
100 GLint source_length = 0;
101 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
102 if (!source_length) {
106 GLchar *source = new GLchar[source_length];
109 glGetShaderSource(shader, source_length, &length, source);
111 shaderMap[enumToString(shader_type)] += source;
118 getShaderObjSource(ShaderMap &shaderMap, GLhandleARB shaderObj)
124 GLint shader_type = 0;
125 glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &shader_type);
130 GLint source_length = 0;
131 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
132 if (!source_length) {
136 GLcharARB *source = new GLcharARB[source_length];
139 glGetShaderSource(shaderObj, source_length, &length, source);
141 shaderMap[enumToString(shader_type)] += source;
148 dumpCurrentProgram(JSONWriter &json)
151 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
156 GLint attached_shaders = 0;
157 glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
158 if (!attached_shaders) {
164 GLuint *shaders = new GLuint[attached_shaders];
166 glGetAttachedShaders(program, attached_shaders, &count, shaders);
167 std::sort(shaders, shaders + count);
168 for (GLsizei i = 0; i < count; ++ i) {
169 getShaderSource(shaderMap, shaders[i]);
173 for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
174 json.beginMember(it->first);
175 json.writeString(it->second);
182 dumpCurrentProgramObj(JSONWriter &json)
184 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
189 GLint attached_shaders = 0;
190 glGetProgramivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
191 if (!attached_shaders) {
197 GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
199 glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
200 std::sort(shaderObjs, shaderObjs + count);
201 for (GLsizei i = 0; i < count; ++ i) {
202 getShaderObjSource(shaderMap, shaderObjs[i]);
204 delete [] shaderObjs;
206 for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
207 json.beginMember(it->first);
208 json.writeString(it->second);
215 dumpUniform(JSONWriter &json, GLint program, GLint size, GLenum type, const GLchar *name) {
219 __gl_uniform_size(type, elemType, numElems);
220 if (elemType == GL_NONE) {
224 GLfloat fvalues[4*4];
225 GLdouble dvalues[4*4];
227 GLuint uivalues[4*4];
231 for (i = 0; i < size; ++i) {
232 std::stringstream ss;
236 ss << '[' << i << ']';
239 std::string elemName = ss.str();
241 json.beginMember(elemName);
243 GLint location = glGetUniformLocation(program, elemName.c_str());
251 glGetUniformfv(program, location, fvalues);
252 for (j = 0; j < numElems; ++j) {
253 json.writeNumber(fvalues[j]);
257 glGetUniformdv(program, location, dvalues);
258 for (j = 0; j < numElems; ++j) {
259 json.writeNumber(dvalues[j]);
263 glGetUniformiv(program, location, ivalues);
264 for (j = 0; j < numElems; ++j) {
265 json.writeNumber(ivalues[j]);
268 case GL_UNSIGNED_INT:
269 glGetUniformuiv(program, location, uivalues);
270 for (j = 0; j < numElems; ++j) {
271 json.writeNumber(uivalues[j]);
275 glGetUniformiv(program, location, ivalues);
276 for (j = 0; j < numElems; ++j) {
277 json.writeBool(ivalues[j]);
295 dumpCurrentProgramUniforms(JSONWriter &json)
298 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
303 GLint active_uniforms = 0;
304 glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &active_uniforms);
305 if (!active_uniforms) {
309 GLint active_uniform_max_length = 0;
310 glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &active_uniform_max_length);
311 GLchar *name = new GLchar[active_uniform_max_length];
316 for (GLint index = 0; index < active_uniforms; ++index) {
319 GLenum type = GL_NONE;
320 glGetActiveUniform(program, index, active_uniform_max_length, &length, &size, &type, name);
322 dumpUniform(json, program, size, type, name);
330 dumpArbProgram(JSONWriter &json, GLenum target)
332 if (!glIsEnabled(target)) {
336 GLint program_length = 0;
337 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
338 if (!program_length) {
342 GLchar *source = new GLchar[program_length + 1];
344 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
345 source[program_length] = 0;
347 json.beginMember(enumToString(target));
348 json.writeString(source);
356 dumpShaders(JSONWriter &json)
358 json.beginMember("shaders");
360 dumpCurrentProgram(json);
361 dumpCurrentProgramObj(json);
362 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
363 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
365 json.endMember(); // shaders
370 dumpUniforms(JSONWriter &json)
372 json.beginMember("uniforms");
374 dumpCurrentProgramUniforms(json);
376 json.endMember(); // uniforms
381 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
383 GLint width, height = 1, depth = 1;
386 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
388 if (target != GL_TEXTURE_1D) {
390 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
391 if (target == GL_TEXTURE_3D) {
393 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
397 if (width <= 0 || height <= 0 || depth <= 0) {
402 GLint active_texture = GL_TEXTURE0;
403 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
404 snprintf(label, sizeof label, "%s, %s, level = %i", enumToString(active_texture), enumToString(target), level);
406 json.beginMember(label);
410 // Tell the GUI this is no ordinary object, but an image
411 json.writeStringMember("__class__", "image");
413 json.writeNumberMember("__width__", width);
414 json.writeNumberMember("__height__", height);
415 json.writeNumberMember("__depth__", depth);
417 // Hardcoded for now, but we could chose types more adequate to the
418 // texture internal format
419 json.writeStringMember("__type__", "uint8");
420 json.writeBoolMember("__normalized__", true);
421 json.writeNumberMember("__channels__", 4);
423 GLubyte *pixels = new GLubyte[depth*width*height*4];
425 resetPixelPackState();
427 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
429 restorePixelPackState();
431 json.beginMember("__data__");
434 Image::writePixelsToBuffer(pixels, width, height, 4, false, &pngBuffer, &pngBufferSize);
435 json.writeBase64(pngBuffer, pngBufferSize);
437 json.endMember(); // __data__
446 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
448 GLint texture_binding = 0;
449 glGetIntegerv(binding, &texture_binding);
450 if (!glIsEnabled(target) && !texture_binding) {
456 GLint width = 0, height = 0;
457 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
458 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
459 if (!width || !height) {
463 if (target == GL_TEXTURE_CUBE_MAP) {
464 for (int face = 0; face < 6; ++face) {
465 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
468 dumpTextureImage(json, target, level);
477 dumpTextures(JSONWriter &json)
479 json.beginMember("textures");
481 GLint active_texture = GL_TEXTURE0;
482 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
483 GLint max_texture_coords = 0;
484 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
485 GLint max_combined_texture_image_units = 0;
486 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
487 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
488 for (GLint unit = 0; unit < max_units; ++unit) {
489 GLenum texture = GL_TEXTURE0 + unit;
490 glActiveTexture(texture);
491 dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
492 dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
493 dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
494 dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
495 dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
497 glActiveTexture(active_texture);
499 json.endMember(); // textures
504 getDrawableBounds(GLint *width, GLint *height) {
507 HDC hDC = wglGetCurrentDC();
512 HWND hWnd = WindowFromDC(hDC);
515 if (!GetClientRect(hWnd, &rect)) {
519 *width = rect.right - rect.left;
520 *height = rect.bottom - rect.top;
522 #elif defined(__APPLE__)
524 CGLContextObj ctx = CGLGetCurrentContext();
533 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
539 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
543 *width = rect.size.width;
544 *height = rect.size.height;
552 unsigned int w, h, bw, depth;
554 display = glXGetCurrentDisplay();
559 drawable = glXGetCurrentDrawable();
560 if (drawable == None) {
564 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
577 static const GLenum texture_bindings[][2] = {
578 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
579 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
580 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
581 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
582 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
587 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
590 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
591 target = texture_bindings[i][0];
593 GLenum binding = texture_bindings[i][1];
595 while (glGetError() != GL_NO_ERROR)
598 glGetIntegerv(binding, &bound_texture);
599 glBindTexture(target, texture);
601 if (glGetError() == GL_NO_ERROR) {
605 glBindTexture(target, bound_texture);
615 getTextureLevelSize(GLint texture, GLint level, GLint *width, GLint *height)
621 GLint bound_texture = 0;
622 if (!bindTexture(texture, target, bound_texture)) {
626 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, width);
627 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, height);
629 glBindTexture(target, bound_texture);
631 return *width > 0 && *height > 0;
636 getRenderbufferSize(GLint renderbuffer, GLint *width, GLint *height)
638 GLint bound_renderbuffer = 0;
639 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
640 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
644 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, width);
645 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, height);
647 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
649 return *width > 0 && *height > 0;
654 getFramebufferAttachmentSize(GLenum target, GLenum attachment, GLint *width, GLint *height)
656 GLint object_type = GL_NONE;
657 glGetFramebufferAttachmentParameteriv(target, attachment,
658 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
660 if (object_type == GL_NONE) {
664 GLint object_name = 0;
665 glGetFramebufferAttachmentParameteriv(target, attachment,
666 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
668 if (object_name == 0) {
672 if (object_type == GL_RENDERBUFFER) {
673 return getRenderbufferSize(object_name, width, height);
674 } else if (object_type == GL_TEXTURE) {
675 GLint texture_level = 0;
676 glGetFramebufferAttachmentParameteriv(target, attachment,
677 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
679 return getTextureLevelSize(object_name, texture_level, width, height);
681 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
688 getDrawBufferImage(GLenum format) {
689 GLint channels = __gl_format_channels(format);
694 GLint draw_framebuffer = 0;
695 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
697 GLint draw_buffer = GL_NONE;
699 if (draw_framebuffer) {
700 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
701 if (draw_buffer == GL_NONE) {
705 if (!getFramebufferAttachmentSize(GL_DRAW_FRAMEBUFFER, draw_buffer, &width, &height)) {
709 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
710 if (draw_buffer == GL_NONE) {
714 if (!getDrawableBounds(&width, &height)) {
719 Image::Image *image = new Image::Image(width, height, channels, true);
724 while (glGetError() != GL_NO_ERROR) {}
726 GLint read_framebuffer = 0;
727 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
728 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
730 GLint read_buffer = 0;
731 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
732 glReadBuffer(draw_buffer);
734 // TODO: reset imaging state too
735 resetPixelPackState();
737 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
739 restorePixelPackState();
740 glReadBuffer(read_buffer);
741 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
743 GLenum error = glGetError();
744 if (error != GL_NO_ERROR) {
746 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
747 error = glGetError();
748 } while(error != GL_NO_ERROR);
758 * Dump the image of the currently bound read buffer.
761 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format)
763 GLint channels = __gl_format_channels(format);
767 // Tell the GUI this is no ordinary object, but an image
768 json.writeStringMember("__class__", "image");
770 json.writeNumberMember("__width__", width);
771 json.writeNumberMember("__height__", height);
772 json.writeNumberMember("__depth__", 1);
774 // Hardcoded for now, but we could chose types more adequate to the
775 // texture internal format
776 json.writeStringMember("__type__", "uint8");
777 json.writeBoolMember("__normalized__", true);
778 json.writeNumberMember("__channels__", channels);
780 GLubyte *pixels = new GLubyte[width*height*channels];
782 // TODO: reset imaging state too
783 resetPixelPackState();
785 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
787 restorePixelPackState();
789 json.beginMember("__data__");
792 Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
793 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
794 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
795 json.writeBase64(pngBuffer, pngBufferSize);
797 json.endMember(); // __data__
805 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
806 GLint colorRb, GLint depthRb, GLint stencilRb,
807 GLuint *rbs, GLint *numRbs)
815 glGenFramebuffers(1, &fbo);
816 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
818 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
819 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
820 GL_RENDERBUFFER_WIDTH, &w);
821 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
822 GL_RENDERBUFFER_HEIGHT, &h);
823 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
824 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
826 glGenRenderbuffers(1, &rbs[*numRbs]);
827 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
828 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
829 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
830 GL_RENDERBUFFER, rbs[*numRbs]);
832 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
833 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
834 glDrawBuffer(drawbuffer);
835 glReadBuffer(drawbuffer);
836 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
837 GL_COLOR_BUFFER_BIT, GL_NEAREST);
838 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
841 if (stencilRb == depthRb && stencilRb) {
842 //combined depth and stencil buffer
843 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
844 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
845 GL_RENDERBUFFER_WIDTH, &w);
846 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
847 GL_RENDERBUFFER_HEIGHT, &h);
848 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
849 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
851 glGenRenderbuffers(1, &rbs[*numRbs]);
852 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
853 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
854 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
855 GL_RENDERBUFFER, rbs[*numRbs]);
856 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
857 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
858 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
859 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
860 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
864 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
865 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
866 GL_RENDERBUFFER_WIDTH, &w);
867 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
868 GL_RENDERBUFFER_HEIGHT, &h);
869 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
870 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
872 glGenRenderbuffers(1, &rbs[*numRbs]);
873 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
874 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
875 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
877 GL_RENDERBUFFER, rbs[*numRbs]);
878 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
879 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
880 glDrawBuffer(GL_DEPTH_ATTACHMENT);
881 glReadBuffer(GL_DEPTH_ATTACHMENT);
882 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
883 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
887 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
888 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
889 GL_RENDERBUFFER_WIDTH, &w);
890 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
891 GL_RENDERBUFFER_HEIGHT, &h);
892 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
893 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
895 glGenRenderbuffers(1, &rbs[*numRbs]);
896 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
897 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
898 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
899 GL_STENCIL_ATTACHMENT,
900 GL_RENDERBUFFER, rbs[*numRbs]);
901 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
902 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
903 glDrawBuffer(GL_STENCIL_ATTACHMENT);
904 glReadBuffer(GL_STENCIL_ATTACHMENT);
905 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
906 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
916 * Dump images of current draw drawable/window.
919 dumpDrawableImages(JSONWriter &json)
923 if (!getDrawableBounds(&width, &height)) {
927 GLint draw_buffer = GL_NONE;
928 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
929 glReadBuffer(draw_buffer);
931 if (draw_buffer != GL_NONE) {
932 GLint read_buffer = GL_NONE;
933 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
935 GLint alpha_bits = 0;
936 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
937 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
938 json.beginMember(enumToString(draw_buffer));
939 dumpReadBufferImage(json, width, height, format);
942 glReadBuffer(read_buffer);
945 GLint depth_bits = 0;
946 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
948 json.beginMember("GL_DEPTH_COMPONENT");
949 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
953 GLint stencil_bits = 0;
954 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
956 json.beginMember("GL_STENCIL_INDEX");
957 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
962 * Dump the specified framebuffer attachment.
964 * In the case of a color attachment, it assumes it is already bound for read.
967 dumpFramebufferAttachment(JSONWriter &json, GLenum target, GLenum attachment, GLenum format)
969 GLint width = 0, height = 0;
970 if (!getFramebufferAttachmentSize(target, attachment, &width, &height)) {
974 json.beginMember(enumToString(attachment));
975 dumpReadBufferImage(json, width, height, format);
981 dumpFramebufferAttachments(JSONWriter &json, GLenum target)
983 GLint read_framebuffer = 0;
984 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
986 GLint read_buffer = GL_NONE;
987 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
989 GLint max_draw_buffers = 1;
990 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
991 GLint max_color_attachments = 0;
992 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
994 for (GLint i = 0; i < max_draw_buffers; ++i) {
995 GLint draw_buffer = GL_NONE;
996 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
997 if (draw_buffer != GL_NONE) {
998 glReadBuffer(draw_buffer);
1000 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1001 attachment = draw_buffer;
1003 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1004 attachment = GL_COLOR_ATTACHMENT0;
1006 GLint alpha_size = 0;
1007 glGetFramebufferAttachmentParameteriv(target, attachment,
1008 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1010 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1011 dumpFramebufferAttachment(json, target, attachment, format);
1015 glReadBuffer(read_buffer);
1017 dumpFramebufferAttachment(json, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1018 dumpFramebufferAttachment(json, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1020 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1025 dumpFramebuffer(JSONWriter &json)
1027 json.beginMember("framebuffer");
1030 GLint boundDrawFbo = 0, boundReadFbo = 0;
1031 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1032 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1033 if (!boundDrawFbo) {
1034 dumpDrawableImages(json);
1036 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1037 GLint draw_buffer0 = GL_NONE;
1038 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1039 bool multisample = false;
1042 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1045 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1046 if (object_type == GL_RENDERBUFFER) {
1047 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1048 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1050 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1056 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1057 if (object_type == GL_RENDERBUFFER) {
1058 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1059 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1061 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1067 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1068 if (object_type == GL_RENDERBUFFER) {
1069 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1070 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1072 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1078 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1085 // glReadPixels doesnt support multisampled buffers so we need
1086 // to blit the fbo to a temporary one
1087 fboCopy = downsampledFramebuffer(boundDrawFbo, draw_buffer0,
1088 colorRb, depthRb, stencilRb,
1092 dumpFramebufferAttachments(json, GL_DRAW_FRAMEBUFFER);
1095 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1096 glDeleteRenderbuffers(numRbs, rbs);
1097 glDeleteFramebuffers(1, &fboCopy);
1100 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1101 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1105 json.endMember(); // framebuffer
1109 static const GLenum bindings[] = {
1112 GL_PIXEL_PACK_BUFFER_BINDING,
1113 GL_PIXEL_UNPACK_BUFFER_BINDING,
1114 GL_TEXTURE_BINDING_1D,
1115 GL_TEXTURE_BINDING_2D,
1116 GL_TEXTURE_BINDING_3D,
1117 GL_TEXTURE_BINDING_RECTANGLE,
1118 GL_TEXTURE_BINDING_CUBE_MAP,
1119 GL_DRAW_FRAMEBUFFER_BINDING,
1120 GL_READ_FRAMEBUFFER_BINDING,
1121 GL_RENDERBUFFER_BINDING,
1133 #define NUM_BINDINGS sizeof(bindings)/sizeof(bindings[0])
1136 void dumpCurrentContext(std::ostream &os)
1138 JSONWriter json(os);
1141 GLint old_bindings[NUM_BINDINGS];
1142 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1143 old_bindings[i] = 0;
1144 glGetIntegerv(bindings[i], &old_bindings[i]);
1148 dumpParameters(json);
1152 dumpFramebuffer(json);
1155 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1156 GLint new_binding = 0;
1157 glGetIntegerv(bindings[i], &new_binding);
1158 if (new_binding != old_bindings[i]) {
1159 std::cerr << "warning: " << enumToString(bindings[i]) << " was clobbered\n";
1167 } /* namespace glstate */