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 object_type = 0;
125 glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &object_type);
126 if (object_type != GL_SHADER_OBJECT_ARB) {
130 GLint shader_type = 0;
131 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SUBTYPE_ARB, &shader_type);
136 GLint source_length = 0;
137 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
138 if (!source_length) {
142 GLcharARB *source = new GLcharARB[source_length];
145 glGetShaderSource(shaderObj, source_length, &length, source);
147 shaderMap[enumToString(shader_type)] += source;
154 dumpProgram(JSONWriter &json, GLint 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 dumpProgramObj(JSONWriter &json, GLhandleARB programObj)
184 GLint attached_shaders = 0;
185 glGetObjectParameterivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
186 if (!attached_shaders) {
192 GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
194 glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
195 std::sort(shaderObjs, shaderObjs + count);
196 for (GLsizei i = 0; i < count; ++ i) {
197 getShaderObjSource(shaderMap, shaderObjs[i]);
199 delete [] shaderObjs;
201 for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
202 json.beginMember(it->first);
203 json.writeString(it->second);
210 dumpUniform(JSONWriter &json, GLint program, GLint size, GLenum type, const GLchar *name) {
214 __gl_uniform_size(type, elemType, numElems);
215 if (elemType == GL_NONE) {
219 GLfloat fvalues[4*4];
220 GLdouble dvalues[4*4];
222 GLuint uivalues[4*4];
226 for (i = 0; i < size; ++i) {
227 std::stringstream ss;
231 ss << '[' << i << ']';
234 std::string elemName = ss.str();
236 json.beginMember(elemName);
238 GLint location = glGetUniformLocation(program, elemName.c_str());
246 glGetUniformfv(program, location, fvalues);
247 for (j = 0; j < numElems; ++j) {
248 json.writeNumber(fvalues[j]);
252 glGetUniformdv(program, location, dvalues);
253 for (j = 0; j < numElems; ++j) {
254 json.writeNumber(dvalues[j]);
258 glGetUniformiv(program, location, ivalues);
259 for (j = 0; j < numElems; ++j) {
260 json.writeNumber(ivalues[j]);
263 case GL_UNSIGNED_INT:
264 glGetUniformuiv(program, location, uivalues);
265 for (j = 0; j < numElems; ++j) {
266 json.writeNumber(uivalues[j]);
270 glGetUniformiv(program, location, ivalues);
271 for (j = 0; j < numElems; ++j) {
272 json.writeBool(ivalues[j]);
290 dumpUniformARB(JSONWriter &json, GLhandleARB programObj, GLint size, GLenum type, const GLchar *name) {
294 __gl_uniform_size(type, elemType, numElems);
295 if (elemType == GL_NONE) {
299 GLfloat fvalues[4*4];
304 for (i = 0; i < size; ++i) {
305 std::stringstream ss;
309 ss << '[' << i << ']';
312 std::string elemName = ss.str();
314 json.beginMember(elemName);
316 GLint location = glGetUniformLocationARB(programObj, elemName.c_str());
324 // glGetUniformdvARB does not exists
326 glGetUniformfvARB(programObj, location, fvalues);
327 for (j = 0; j < numElems; ++j) {
328 json.writeNumber(fvalues[j]);
331 case GL_UNSIGNED_INT:
332 // glGetUniformuivARB does not exists
334 glGetUniformivARB(programObj, location, ivalues);
335 for (j = 0; j < numElems; ++j) {
336 json.writeNumber(ivalues[j]);
340 glGetUniformivARB(programObj, location, ivalues);
341 for (j = 0; j < numElems; ++j) {
342 json.writeBool(ivalues[j]);
360 dumpProgramUniforms(JSONWriter &json, GLint program)
362 GLint active_uniforms = 0;
363 glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &active_uniforms);
364 if (!active_uniforms) {
368 GLint active_uniform_max_length = 0;
369 glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &active_uniform_max_length);
370 GLchar *name = new GLchar[active_uniform_max_length];
375 for (GLint index = 0; index < active_uniforms; ++index) {
378 GLenum type = GL_NONE;
379 glGetActiveUniform(program, index, active_uniform_max_length, &length, &size, &type, name);
381 dumpUniform(json, program, size, type, name);
389 dumpProgramObjUniforms(JSONWriter &json, GLhandleARB programObj)
391 GLint active_uniforms = 0;
392 glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &active_uniforms);
393 if (!active_uniforms) {
397 GLint active_uniform_max_length = 0;
398 glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB, &active_uniform_max_length);
399 GLchar *name = new GLchar[active_uniform_max_length];
404 for (GLint index = 0; index < active_uniforms; ++index) {
407 GLenum type = GL_NONE;
408 glGetActiveUniformARB(programObj, index, active_uniform_max_length, &length, &size, &type, name);
410 dumpUniformARB(json, programObj, size, type, name);
418 dumpArbProgram(JSONWriter &json, GLenum target)
420 if (!glIsEnabled(target)) {
424 GLint program_length = 0;
425 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
426 if (!program_length) {
430 GLchar *source = new GLchar[program_length + 1];
432 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
433 source[program_length] = 0;
435 json.beginMember(enumToString(target));
436 json.writeString(source);
444 dumpArbProgramUniforms(JSONWriter &json, GLenum target, const char *prefix)
446 if (!glIsEnabled(target)) {
450 GLint program_parameters = 0;
451 glGetProgramivARB(target, GL_PROGRAM_PARAMETERS_ARB, &program_parameters);
452 if (!program_parameters) {
456 GLint max_program_local_parameters = 0;
457 glGetProgramivARB(target, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &max_program_local_parameters);
458 for (GLuint index = 0; index < max_program_local_parameters; ++index) {
459 GLdouble params[4] = {0, 0, 0, 0};
460 glGetProgramLocalParameterdvARB(target, index, params);
462 if (!params[0] && !params[1] && !params[2] && !params[3]) {
467 snprintf(name, sizeof name, "%sprogram.local[%u]", prefix, index);
469 json.beginMember(name);
471 json.writeNumber(params[0]);
472 json.writeNumber(params[1]);
473 json.writeNumber(params[2]);
474 json.writeNumber(params[3]);
479 GLint max_program_env_parameters = 0;
480 glGetProgramivARB(target, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, &max_program_env_parameters);
481 for (GLuint index = 0; index < max_program_env_parameters; ++index) {
482 GLdouble params[4] = {0, 0, 0, 0};
483 glGetProgramEnvParameterdvARB(target, index, params);
485 if (!params[0] && !params[1] && !params[2] && !params[3]) {
490 snprintf(name, sizeof name, "%sprogram.env[%u]", prefix, index);
492 json.beginMember(name);
494 json.writeNumber(params[0]);
495 json.writeNumber(params[1]);
496 json.writeNumber(params[2]);
497 json.writeNumber(params[3]);
505 dumpShadersUniforms(JSONWriter &json)
508 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
510 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
512 json.beginMember("shaders");
515 dumpProgram(json, program);
516 } else if (programObj) {
517 dumpProgramObj(json, programObj);
519 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
520 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
523 json.endMember(); // shaders
525 json.beginMember("uniforms");
528 dumpProgramUniforms(json, program);
529 } else if (programObj) {
530 dumpProgramObjUniforms(json, programObj);
532 dumpArbProgramUniforms(json, GL_FRAGMENT_PROGRAM_ARB, "fp.");
533 dumpArbProgramUniforms(json, GL_VERTEX_PROGRAM_ARB, "vp.");
536 json.endMember(); // uniforms
541 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
543 GLint width, height = 1, depth = 1;
546 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
548 if (target != GL_TEXTURE_1D) {
550 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
551 if (target == GL_TEXTURE_3D) {
553 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
557 if (width <= 0 || height <= 0 || depth <= 0) {
562 GLint active_texture = GL_TEXTURE0;
563 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
564 snprintf(label, sizeof label, "%s, %s, level = %i", enumToString(active_texture), enumToString(target), level);
566 json.beginMember(label);
570 // Tell the GUI this is no ordinary object, but an image
571 json.writeStringMember("__class__", "image");
573 json.writeNumberMember("__width__", width);
574 json.writeNumberMember("__height__", height);
575 json.writeNumberMember("__depth__", depth);
577 // Hardcoded for now, but we could chose types more adequate to the
578 // texture internal format
579 json.writeStringMember("__type__", "uint8");
580 json.writeBoolMember("__normalized__", true);
581 json.writeNumberMember("__channels__", 4);
583 GLubyte *pixels = new GLubyte[depth*width*height*4];
585 resetPixelPackState();
587 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
589 restorePixelPackState();
591 json.beginMember("__data__");
594 Image::writePixelsToBuffer(pixels, width, height, 4, false, &pngBuffer, &pngBufferSize);
595 json.writeBase64(pngBuffer, pngBufferSize);
597 json.endMember(); // __data__
606 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
608 GLint texture_binding = 0;
609 glGetIntegerv(binding, &texture_binding);
610 if (!glIsEnabled(target) && !texture_binding) {
616 GLint width = 0, height = 0;
617 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
618 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
619 if (!width || !height) {
623 if (target == GL_TEXTURE_CUBE_MAP) {
624 for (int face = 0; face < 6; ++face) {
625 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
628 dumpTextureImage(json, target, level);
637 dumpTextures(JSONWriter &json)
639 json.beginMember("textures");
641 GLint active_texture = GL_TEXTURE0;
642 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
643 GLint max_texture_coords = 0;
644 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
645 GLint max_combined_texture_image_units = 0;
646 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
647 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
648 for (GLint unit = 0; unit < max_units; ++unit) {
649 GLenum texture = GL_TEXTURE0 + unit;
650 glActiveTexture(texture);
651 dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
652 dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
653 dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
654 dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
655 dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
657 glActiveTexture(active_texture);
659 json.endMember(); // textures
664 getDrawableBounds(GLint *width, GLint *height) {
667 HDC hDC = wglGetCurrentDC();
672 HWND hWnd = WindowFromDC(hDC);
675 if (!GetClientRect(hWnd, &rect)) {
679 *width = rect.right - rect.left;
680 *height = rect.bottom - rect.top;
682 #elif defined(__APPLE__)
684 CGLContextObj ctx = CGLGetCurrentContext();
693 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
699 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
703 *width = rect.size.width;
704 *height = rect.size.height;
712 unsigned int w, h, bw, depth;
714 display = glXGetCurrentDisplay();
719 drawable = glXGetCurrentDrawable();
720 if (drawable == None) {
724 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
737 static const GLenum texture_bindings[][2] = {
738 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
739 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
740 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
741 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
742 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
747 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
750 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
751 target = texture_bindings[i][0];
753 GLenum binding = texture_bindings[i][1];
755 while (glGetError() != GL_NO_ERROR)
758 glGetIntegerv(binding, &bound_texture);
759 glBindTexture(target, texture);
761 if (glGetError() == GL_NO_ERROR) {
765 glBindTexture(target, bound_texture);
775 getTextureLevelSize(GLint texture, GLint level, GLint *width, GLint *height)
781 GLint bound_texture = 0;
782 if (!bindTexture(texture, target, bound_texture)) {
786 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, width);
787 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, height);
789 glBindTexture(target, bound_texture);
791 return *width > 0 && *height > 0;
796 getRenderbufferSize(GLint renderbuffer, GLint *width, GLint *height)
798 GLint bound_renderbuffer = 0;
799 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
800 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
804 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, width);
805 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, height);
807 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
809 return *width > 0 && *height > 0;
814 getFramebufferAttachmentSize(GLenum target, GLenum attachment, GLint *width, GLint *height)
816 GLint object_type = GL_NONE;
817 glGetFramebufferAttachmentParameteriv(target, attachment,
818 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
820 if (object_type == GL_NONE) {
824 GLint object_name = 0;
825 glGetFramebufferAttachmentParameteriv(target, attachment,
826 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
828 if (object_name == 0) {
832 if (object_type == GL_RENDERBUFFER) {
833 return getRenderbufferSize(object_name, width, height);
834 } else if (object_type == GL_TEXTURE) {
835 GLint texture_level = 0;
836 glGetFramebufferAttachmentParameteriv(target, attachment,
837 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
839 return getTextureLevelSize(object_name, texture_level, width, height);
841 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
848 getDrawBufferImage(GLenum format) {
849 GLint channels = __gl_format_channels(format);
854 GLint draw_framebuffer = 0;
855 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
857 GLint draw_buffer = GL_NONE;
859 if (draw_framebuffer) {
860 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
861 if (draw_buffer == GL_NONE) {
865 if (!getFramebufferAttachmentSize(GL_DRAW_FRAMEBUFFER, draw_buffer, &width, &height)) {
869 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
870 if (draw_buffer == GL_NONE) {
874 if (!getDrawableBounds(&width, &height)) {
879 Image::Image *image = new Image::Image(width, height, channels, true);
884 while (glGetError() != GL_NO_ERROR) {}
886 GLint read_framebuffer = 0;
887 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
888 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
890 GLint read_buffer = 0;
891 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
892 glReadBuffer(draw_buffer);
894 // TODO: reset imaging state too
895 resetPixelPackState();
897 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
899 restorePixelPackState();
900 glReadBuffer(read_buffer);
901 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
903 GLenum error = glGetError();
904 if (error != GL_NO_ERROR) {
906 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
907 error = glGetError();
908 } while(error != GL_NO_ERROR);
918 * Dump the image of the currently bound read buffer.
921 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format)
923 GLint channels = __gl_format_channels(format);
927 // Tell the GUI this is no ordinary object, but an image
928 json.writeStringMember("__class__", "image");
930 json.writeNumberMember("__width__", width);
931 json.writeNumberMember("__height__", height);
932 json.writeNumberMember("__depth__", 1);
934 // Hardcoded for now, but we could chose types more adequate to the
935 // texture internal format
936 json.writeStringMember("__type__", "uint8");
937 json.writeBoolMember("__normalized__", true);
938 json.writeNumberMember("__channels__", channels);
940 GLubyte *pixels = new GLubyte[width*height*channels];
942 // TODO: reset imaging state too
943 resetPixelPackState();
945 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
947 restorePixelPackState();
949 json.beginMember("__data__");
952 Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
953 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
954 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
955 json.writeBase64(pngBuffer, pngBufferSize);
957 json.endMember(); // __data__
965 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
966 GLint colorRb, GLint depthRb, GLint stencilRb,
967 GLuint *rbs, GLint *numRbs)
975 glGenFramebuffers(1, &fbo);
976 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
978 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
979 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
980 GL_RENDERBUFFER_WIDTH, &w);
981 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
982 GL_RENDERBUFFER_HEIGHT, &h);
983 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
984 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
986 glGenRenderbuffers(1, &rbs[*numRbs]);
987 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
988 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
989 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
990 GL_RENDERBUFFER, rbs[*numRbs]);
992 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
993 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
994 glDrawBuffer(drawbuffer);
995 glReadBuffer(drawbuffer);
996 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
997 GL_COLOR_BUFFER_BIT, GL_NEAREST);
998 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1001 if (stencilRb == depthRb && stencilRb) {
1002 //combined depth and stencil buffer
1003 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1004 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1005 GL_RENDERBUFFER_WIDTH, &w);
1006 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1007 GL_RENDERBUFFER_HEIGHT, &h);
1008 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1009 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1011 glGenRenderbuffers(1, &rbs[*numRbs]);
1012 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1013 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1014 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
1015 GL_RENDERBUFFER, rbs[*numRbs]);
1016 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1017 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1018 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1019 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1020 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1024 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1025 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1026 GL_RENDERBUFFER_WIDTH, &w);
1027 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1028 GL_RENDERBUFFER_HEIGHT, &h);
1029 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1030 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1032 glGenRenderbuffers(1, &rbs[*numRbs]);
1033 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1034 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1035 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1036 GL_DEPTH_ATTACHMENT,
1037 GL_RENDERBUFFER, rbs[*numRbs]);
1038 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1039 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1040 glDrawBuffer(GL_DEPTH_ATTACHMENT);
1041 glReadBuffer(GL_DEPTH_ATTACHMENT);
1042 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1043 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
1047 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1048 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1049 GL_RENDERBUFFER_WIDTH, &w);
1050 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1051 GL_RENDERBUFFER_HEIGHT, &h);
1052 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1053 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1055 glGenRenderbuffers(1, &rbs[*numRbs]);
1056 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1057 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1058 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1059 GL_STENCIL_ATTACHMENT,
1060 GL_RENDERBUFFER, rbs[*numRbs]);
1061 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1062 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1063 glDrawBuffer(GL_STENCIL_ATTACHMENT);
1064 glReadBuffer(GL_STENCIL_ATTACHMENT);
1065 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1066 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1076 * Dump images of current draw drawable/window.
1079 dumpDrawableImages(JSONWriter &json)
1081 GLint width, height;
1083 if (!getDrawableBounds(&width, &height)) {
1087 GLint draw_buffer = GL_NONE;
1088 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1089 glReadBuffer(draw_buffer);
1091 if (draw_buffer != GL_NONE) {
1092 GLint read_buffer = GL_NONE;
1093 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1095 GLint alpha_bits = 0;
1096 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1097 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1098 json.beginMember(enumToString(draw_buffer));
1099 dumpReadBufferImage(json, width, height, format);
1102 glReadBuffer(read_buffer);
1105 GLint depth_bits = 0;
1106 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1108 json.beginMember("GL_DEPTH_COMPONENT");
1109 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1113 GLint stencil_bits = 0;
1114 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1116 json.beginMember("GL_STENCIL_INDEX");
1117 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1122 * Dump the specified framebuffer attachment.
1124 * In the case of a color attachment, it assumes it is already bound for read.
1127 dumpFramebufferAttachment(JSONWriter &json, GLenum target, GLenum attachment, GLenum format)
1129 GLint width = 0, height = 0;
1130 if (!getFramebufferAttachmentSize(target, attachment, &width, &height)) {
1134 json.beginMember(enumToString(attachment));
1135 dumpReadBufferImage(json, width, height, format);
1141 dumpFramebufferAttachments(JSONWriter &json, GLenum target)
1143 GLint read_framebuffer = 0;
1144 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1146 GLint read_buffer = GL_NONE;
1147 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1149 GLint max_draw_buffers = 1;
1150 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1151 GLint max_color_attachments = 0;
1152 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1154 for (GLint i = 0; i < max_draw_buffers; ++i) {
1155 GLint draw_buffer = GL_NONE;
1156 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1157 if (draw_buffer != GL_NONE) {
1158 glReadBuffer(draw_buffer);
1160 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1161 attachment = draw_buffer;
1163 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1164 attachment = GL_COLOR_ATTACHMENT0;
1166 GLint alpha_size = 0;
1167 glGetFramebufferAttachmentParameteriv(target, attachment,
1168 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1170 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1171 dumpFramebufferAttachment(json, target, attachment, format);
1175 glReadBuffer(read_buffer);
1177 dumpFramebufferAttachment(json, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1178 dumpFramebufferAttachment(json, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1180 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1185 dumpFramebuffer(JSONWriter &json)
1187 json.beginMember("framebuffer");
1190 GLint boundDrawFbo = 0, boundReadFbo = 0;
1191 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1192 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1193 if (!boundDrawFbo) {
1194 dumpDrawableImages(json);
1196 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1197 GLint draw_buffer0 = GL_NONE;
1198 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1199 bool multisample = false;
1202 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1205 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1206 if (object_type == GL_RENDERBUFFER) {
1207 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1208 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1210 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1216 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1217 if (object_type == GL_RENDERBUFFER) {
1218 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1219 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1221 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1227 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1228 if (object_type == GL_RENDERBUFFER) {
1229 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1230 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1232 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1238 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1245 // glReadPixels doesnt support multisampled buffers so we need
1246 // to blit the fbo to a temporary one
1247 fboCopy = downsampledFramebuffer(boundDrawFbo, draw_buffer0,
1248 colorRb, depthRb, stencilRb,
1252 dumpFramebufferAttachments(json, GL_DRAW_FRAMEBUFFER);
1255 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1256 glDeleteRenderbuffers(numRbs, rbs);
1257 glDeleteFramebuffers(1, &fboCopy);
1260 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1261 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1265 json.endMember(); // framebuffer
1269 static const GLenum bindings[] = {
1272 GL_PIXEL_PACK_BUFFER_BINDING,
1273 GL_PIXEL_UNPACK_BUFFER_BINDING,
1274 GL_TEXTURE_BINDING_1D,
1275 GL_TEXTURE_BINDING_2D,
1276 GL_TEXTURE_BINDING_3D,
1277 GL_TEXTURE_BINDING_RECTANGLE,
1278 GL_TEXTURE_BINDING_CUBE_MAP,
1279 GL_DRAW_FRAMEBUFFER_BINDING,
1280 GL_READ_FRAMEBUFFER_BINDING,
1281 GL_RENDERBUFFER_BINDING,
1293 #define NUM_BINDINGS sizeof(bindings)/sizeof(bindings[0])
1296 void dumpCurrentContext(std::ostream &os)
1298 JSONWriter json(os);
1301 GLint old_bindings[NUM_BINDINGS];
1302 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1303 old_bindings[i] = 0;
1304 glGetIntegerv(bindings[i], &old_bindings[i]);
1308 dumpParameters(json);
1309 dumpShadersUniforms(json);
1311 dumpFramebuffer(json);
1314 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1315 GLint new_binding = 0;
1316 glGetIntegerv(bindings[i], &new_binding);
1317 if (new_binding != old_bindings[i]) {
1318 std::cerr << "warning: " << enumToString(bindings[i]) << " was clobbered\n";
1326 } /* namespace glstate */