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);
209 * When fetching the uniform name of an array we usually get name[0]
210 * so we need to cut the trailing "[0]" in order to properly construct
211 * array names later. Otherwise we endup with stuff like
212 * uniformArray[0][0],
213 * uniformArray[0][1],
219 resolveUniformName(const GLchar *name, GLint size)
221 std::string qualifiedName(name);
223 std::string::size_type nameLength = qualifiedName.length();
224 static const char * const arrayStart = "[0]";
225 static const int arrayStartLen = 3;
226 if (qualifiedName.rfind(arrayStart) == (nameLength - arrayStartLen)) {
227 qualifiedName = qualifiedName.substr(0, nameLength - 3);
230 return qualifiedName;
234 dumpUniform(JSONWriter &json, GLint program, GLint size, GLenum type, const GLchar *name) {
237 __gl_uniform_size(type, elemType, numElems);
238 if (elemType == GL_NONE) {
242 GLfloat fvalues[4*4];
243 GLdouble dvalues[4*4];
245 GLuint uivalues[4*4];
249 std::string qualifiedName = resolveUniformName(name, size);
251 for (i = 0; i < size; ++i) {
252 std::stringstream ss;
256 ss << '[' << i << ']';
259 std::string elemName = ss.str();
261 json.beginMember(elemName);
263 GLint location = glGetUniformLocation(program, elemName.c_str());
271 glGetUniformfv(program, location, fvalues);
272 for (j = 0; j < numElems; ++j) {
273 json.writeNumber(fvalues[j]);
277 glGetUniformdv(program, location, dvalues);
278 for (j = 0; j < numElems; ++j) {
279 json.writeNumber(dvalues[j]);
283 glGetUniformiv(program, location, ivalues);
284 for (j = 0; j < numElems; ++j) {
285 json.writeNumber(ivalues[j]);
288 case GL_UNSIGNED_INT:
289 glGetUniformuiv(program, location, uivalues);
290 for (j = 0; j < numElems; ++j) {
291 json.writeNumber(uivalues[j]);
295 glGetUniformiv(program, location, ivalues);
296 for (j = 0; j < numElems; ++j) {
297 json.writeBool(ivalues[j]);
315 dumpUniformARB(JSONWriter &json, GLhandleARB programObj, GLint size, GLenum type, const GLchar *name) {
319 __gl_uniform_size(type, elemType, numElems);
320 if (elemType == GL_NONE) {
324 GLfloat fvalues[4*4];
329 std::string qualifiedName = resolveUniformName(name, size);
331 for (i = 0; i < size; ++i) {
332 std::stringstream ss;
336 ss << '[' << i << ']';
339 std::string elemName = ss.str();
341 json.beginMember(elemName);
343 GLint location = glGetUniformLocationARB(programObj, elemName.c_str());
351 // glGetUniformdvARB does not exists
353 glGetUniformfvARB(programObj, location, fvalues);
354 for (j = 0; j < numElems; ++j) {
355 json.writeNumber(fvalues[j]);
358 case GL_UNSIGNED_INT:
359 // glGetUniformuivARB does not exists
361 glGetUniformivARB(programObj, location, ivalues);
362 for (j = 0; j < numElems; ++j) {
363 json.writeNumber(ivalues[j]);
367 glGetUniformivARB(programObj, location, ivalues);
368 for (j = 0; j < numElems; ++j) {
369 json.writeBool(ivalues[j]);
387 dumpProgramUniforms(JSONWriter &json, GLint program)
389 GLint active_uniforms = 0;
390 glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &active_uniforms);
391 if (!active_uniforms) {
395 GLint active_uniform_max_length = 0;
396 glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &active_uniform_max_length);
397 GLchar *name = new GLchar[active_uniform_max_length];
402 for (GLint index = 0; index < active_uniforms; ++index) {
405 GLenum type = GL_NONE;
406 glGetActiveUniform(program, index, active_uniform_max_length, &length, &size, &type, name);
408 dumpUniform(json, program, size, type, name);
416 dumpProgramObjUniforms(JSONWriter &json, GLhandleARB programObj)
418 GLint active_uniforms = 0;
419 glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &active_uniforms);
420 if (!active_uniforms) {
424 GLint active_uniform_max_length = 0;
425 glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB, &active_uniform_max_length);
426 GLchar *name = new GLchar[active_uniform_max_length];
431 for (GLint index = 0; index < active_uniforms; ++index) {
434 GLenum type = GL_NONE;
435 glGetActiveUniformARB(programObj, index, active_uniform_max_length, &length, &size, &type, name);
437 dumpUniformARB(json, programObj, size, type, name);
445 dumpArbProgram(JSONWriter &json, GLenum target)
447 if (!glIsEnabled(target)) {
451 GLint program_length = 0;
452 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
453 if (!program_length) {
457 GLchar *source = new GLchar[program_length + 1];
459 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
460 source[program_length] = 0;
462 json.beginMember(enumToString(target));
463 json.writeString(source);
471 dumpArbProgramUniforms(JSONWriter &json, GLenum target, const char *prefix)
473 if (!glIsEnabled(target)) {
477 GLint program_parameters = 0;
478 glGetProgramivARB(target, GL_PROGRAM_PARAMETERS_ARB, &program_parameters);
479 if (!program_parameters) {
483 GLint max_program_local_parameters = 0;
484 glGetProgramivARB(target, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &max_program_local_parameters);
485 for (GLint index = 0; index < max_program_local_parameters; ++index) {
486 GLdouble params[4] = {0, 0, 0, 0};
487 glGetProgramLocalParameterdvARB(target, index, params);
489 if (!params[0] && !params[1] && !params[2] && !params[3]) {
494 snprintf(name, sizeof name, "%sprogram.local[%i]", prefix, index);
496 json.beginMember(name);
498 json.writeNumber(params[0]);
499 json.writeNumber(params[1]);
500 json.writeNumber(params[2]);
501 json.writeNumber(params[3]);
506 GLint max_program_env_parameters = 0;
507 glGetProgramivARB(target, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, &max_program_env_parameters);
508 for (GLint index = 0; index < max_program_env_parameters; ++index) {
509 GLdouble params[4] = {0, 0, 0, 0};
510 glGetProgramEnvParameterdvARB(target, index, params);
512 if (!params[0] && !params[1] && !params[2] && !params[3]) {
517 snprintf(name, sizeof name, "%sprogram.env[%i]", prefix, index);
519 json.beginMember(name);
521 json.writeNumber(params[0]);
522 json.writeNumber(params[1]);
523 json.writeNumber(params[2]);
524 json.writeNumber(params[3]);
532 dumpShadersUniforms(JSONWriter &json)
535 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
537 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
539 json.beginMember("shaders");
542 dumpProgram(json, program);
543 } else if (programObj) {
544 dumpProgramObj(json, programObj);
546 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
547 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
550 json.endMember(); // shaders
552 json.beginMember("uniforms");
555 dumpProgramUniforms(json, program);
556 } else if (programObj) {
557 dumpProgramObjUniforms(json, programObj);
559 dumpArbProgramUniforms(json, GL_FRAGMENT_PROGRAM_ARB, "fp.");
560 dumpArbProgramUniforms(json, GL_VERTEX_PROGRAM_ARB, "vp.");
563 json.endMember(); // uniforms
568 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
570 GLint width, height = 1, depth = 1;
573 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &format);
576 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
578 if (target != GL_TEXTURE_1D) {
580 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
581 if (target == GL_TEXTURE_3D) {
583 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
587 if (width <= 0 || height <= 0 || depth <= 0) {
592 GLint active_texture = GL_TEXTURE0;
593 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
594 snprintf(label, sizeof label, "%s, %s, level = %d",
595 enumToString(active_texture), enumToString(target), level);
597 json.beginMember(label);
601 // Tell the GUI this is no ordinary object, but an image
602 json.writeStringMember("__class__", "image");
604 json.writeNumberMember("__width__", width);
605 json.writeNumberMember("__height__", height);
606 json.writeNumberMember("__depth__", depth);
608 json.writeStringMember("__format__", enumToString(format));
610 // Hardcoded for now, but we could chose types more adequate to the
611 // texture internal format
612 json.writeStringMember("__type__", "uint8");
613 json.writeBoolMember("__normalized__", true);
614 json.writeNumberMember("__channels__", 4);
616 GLubyte *pixels = new GLubyte[depth*width*height*4];
618 resetPixelPackState();
620 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
622 restorePixelPackState();
624 json.beginMember("__data__");
627 image::writePixelsToBuffer(pixels, width, height, 4, true, &pngBuffer, &pngBufferSize);
628 json.writeBase64(pngBuffer, pngBufferSize);
630 json.endMember(); // __data__
639 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
641 GLint texture_binding = 0;
642 glGetIntegerv(binding, &texture_binding);
643 if (!glIsEnabled(target) && !texture_binding) {
649 GLint width = 0, height = 0;
650 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
651 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
652 if (!width || !height) {
656 if (target == GL_TEXTURE_CUBE_MAP) {
657 for (int face = 0; face < 6; ++face) {
658 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
661 dumpTextureImage(json, target, level);
670 dumpTextures(JSONWriter &json)
672 json.beginMember("textures");
674 GLint active_texture = GL_TEXTURE0;
675 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
676 GLint max_texture_coords = 0;
677 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
678 GLint max_combined_texture_image_units = 0;
679 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
680 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
681 for (GLint unit = 0; unit < max_units; ++unit) {
682 GLenum texture = GL_TEXTURE0 + unit;
683 glActiveTexture(texture);
684 dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
685 dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
686 dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
687 dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
688 dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
690 glActiveTexture(active_texture);
692 json.endMember(); // textures
697 getDrawableBounds(GLint *width, GLint *height) {
700 HDC hDC = wglGetCurrentDC();
705 HWND hWnd = WindowFromDC(hDC);
708 if (!GetClientRect(hWnd, &rect)) {
712 *width = rect.right - rect.left;
713 *height = rect.bottom - rect.top;
715 #elif defined(__APPLE__)
717 CGLContextObj ctx = CGLGetCurrentContext();
726 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
732 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
736 *width = rect.size.width;
737 *height = rect.size.height;
745 unsigned int w, h, bw, depth;
747 display = glXGetCurrentDisplay();
752 drawable = glXGetCurrentDrawable();
753 if (drawable == None) {
757 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
770 static const GLenum texture_bindings[][2] = {
771 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
772 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
773 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
774 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
775 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
780 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
783 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
784 target = texture_bindings[i][0];
786 GLenum binding = texture_bindings[i][1];
788 while (glGetError() != GL_NO_ERROR)
791 glGetIntegerv(binding, &bound_texture);
792 glBindTexture(target, texture);
794 if (glGetError() == GL_NO_ERROR) {
798 glBindTexture(target, bound_texture);
808 getTextureLevelSize(GLint texture, GLint level, GLint *width, GLint *height)
814 GLint bound_texture = 0;
815 if (!bindTexture(texture, target, bound_texture)) {
819 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, width);
820 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, height);
822 glBindTexture(target, bound_texture);
824 return *width > 0 && *height > 0;
829 getTextureLevelFormat(GLint texture, GLint level)
832 GLint bound_texture = 0;
833 if (!bindTexture(texture, target, bound_texture)) {
837 GLint format = GL_NONE;
838 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &format);
840 glBindTexture(target, bound_texture);
848 getRenderbufferSize(GLint renderbuffer, GLint *width, GLint *height)
850 GLint bound_renderbuffer = 0;
851 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
852 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
856 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, width);
857 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, height);
859 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
861 return *width > 0 && *height > 0;
866 getRenderbufferFormat(GLint renderbuffer)
868 GLint bound_renderbuffer = 0;
869 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
870 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
872 GLint format = GL_NONE;
873 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
875 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
882 getFramebufferAttachmentSize(GLenum target, GLenum attachment, GLint *width, GLint *height)
884 GLint object_type = GL_NONE;
885 glGetFramebufferAttachmentParameteriv(target, attachment,
886 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
888 if (object_type == GL_NONE) {
892 GLint object_name = 0;
893 glGetFramebufferAttachmentParameteriv(target, attachment,
894 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
896 if (object_name == 0) {
900 if (object_type == GL_RENDERBUFFER) {
901 return getRenderbufferSize(object_name, width, height);
902 } else if (object_type == GL_TEXTURE) {
903 GLint texture_level = 0;
904 glGetFramebufferAttachmentParameteriv(target, attachment,
905 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
907 return getTextureLevelSize(object_name, texture_level, width, height);
909 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
917 getFramebufferAttachmentFormat(GLenum target, GLenum attachment)
919 GLint object_type = GL_NONE;
920 glGetFramebufferAttachmentParameteriv(target, attachment,
921 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
923 if (object_type == GL_NONE) {
927 GLint object_name = 0;
928 glGetFramebufferAttachmentParameteriv(target, attachment,
929 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
931 if (object_name == 0) {
935 if (object_type == GL_RENDERBUFFER) {
936 return getRenderbufferFormat(object_name);
937 } else if (object_type == GL_TEXTURE) {
938 GLint texture_level = 0;
939 glGetFramebufferAttachmentParameteriv(target, attachment,
940 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
942 return getTextureLevelFormat(object_name, texture_level);
944 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
952 getDrawBufferImage(GLenum format) {
953 GLint channels = __gl_format_channels(format);
958 GLint draw_framebuffer = 0;
959 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
961 GLint draw_buffer = GL_NONE;
963 if (draw_framebuffer) {
964 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
965 if (draw_buffer == GL_NONE) {
969 if (!getFramebufferAttachmentSize(GL_DRAW_FRAMEBUFFER, draw_buffer, &width, &height)) {
973 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
974 if (draw_buffer == GL_NONE) {
978 if (!getDrawableBounds(&width, &height)) {
983 image::Image *image = new image::Image(width, height, channels, true);
988 while (glGetError() != GL_NO_ERROR) {}
990 GLint read_framebuffer = 0;
991 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
992 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
994 GLint read_buffer = 0;
995 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
996 glReadBuffer(draw_buffer);
998 // TODO: reset imaging state too
999 resetPixelPackState();
1001 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
1003 restorePixelPackState();
1004 glReadBuffer(read_buffer);
1005 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1007 GLenum error = glGetError();
1008 if (error != GL_NO_ERROR) {
1010 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
1011 error = glGetError();
1012 } while(error != GL_NO_ERROR);
1022 * Dump the image of the currently bound read buffer.
1025 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
1026 GLint internalFormat = GL_NONE)
1028 GLint channels = __gl_format_channels(format);
1032 // Tell the GUI this is no ordinary object, but an image
1033 json.writeStringMember("__class__", "image");
1035 json.writeNumberMember("__width__", width);
1036 json.writeNumberMember("__height__", height);
1037 json.writeNumberMember("__depth__", 1);
1039 json.writeStringMember("__format__", enumToString(internalFormat));
1041 // Hardcoded for now, but we could chose types more adequate to the
1042 // texture internal format
1043 json.writeStringMember("__type__", "uint8");
1044 json.writeBoolMember("__normalized__", true);
1045 json.writeNumberMember("__channels__", channels);
1047 GLubyte *pixels = new GLubyte[width*height*channels];
1049 // TODO: reset imaging state too
1050 resetPixelPackState();
1052 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
1054 restorePixelPackState();
1056 json.beginMember("__data__");
1059 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
1060 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
1061 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
1062 json.writeBase64(pngBuffer, pngBufferSize);
1064 json.endMember(); // __data__
1071 static inline GLuint
1072 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
1073 GLint colorRb, GLint depthRb, GLint stencilRb,
1074 GLuint *rbs, GLint *numRbs)
1082 glGenFramebuffers(1, &fbo);
1083 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1085 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1086 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1087 GL_RENDERBUFFER_WIDTH, &w);
1088 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1089 GL_RENDERBUFFER_HEIGHT, &h);
1090 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1091 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1093 glGenRenderbuffers(1, &rbs[*numRbs]);
1094 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1095 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1096 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
1097 GL_RENDERBUFFER, rbs[*numRbs]);
1099 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1100 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1101 glDrawBuffer(drawbuffer);
1102 glReadBuffer(drawbuffer);
1103 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1104 GL_COLOR_BUFFER_BIT, GL_NEAREST);
1105 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1108 if (stencilRb == depthRb && stencilRb) {
1109 //combined depth and stencil buffer
1110 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1111 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1112 GL_RENDERBUFFER_WIDTH, &w);
1113 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1114 GL_RENDERBUFFER_HEIGHT, &h);
1115 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1116 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1118 glGenRenderbuffers(1, &rbs[*numRbs]);
1119 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1120 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1121 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
1122 GL_RENDERBUFFER, rbs[*numRbs]);
1123 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1124 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1125 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1126 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1127 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1131 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1132 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1133 GL_RENDERBUFFER_WIDTH, &w);
1134 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1135 GL_RENDERBUFFER_HEIGHT, &h);
1136 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1137 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1139 glGenRenderbuffers(1, &rbs[*numRbs]);
1140 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1141 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1142 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1143 GL_DEPTH_ATTACHMENT,
1144 GL_RENDERBUFFER, rbs[*numRbs]);
1145 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1146 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1147 glDrawBuffer(GL_DEPTH_ATTACHMENT);
1148 glReadBuffer(GL_DEPTH_ATTACHMENT);
1149 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1150 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
1154 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1155 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1156 GL_RENDERBUFFER_WIDTH, &w);
1157 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1158 GL_RENDERBUFFER_HEIGHT, &h);
1159 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1160 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1162 glGenRenderbuffers(1, &rbs[*numRbs]);
1163 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1164 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1165 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1166 GL_STENCIL_ATTACHMENT,
1167 GL_RENDERBUFFER, rbs[*numRbs]);
1168 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1169 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1170 glDrawBuffer(GL_STENCIL_ATTACHMENT);
1171 glReadBuffer(GL_STENCIL_ATTACHMENT);
1172 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1173 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1183 * Dump images of current draw drawable/window.
1186 dumpDrawableImages(JSONWriter &json)
1188 GLint width, height;
1190 if (!getDrawableBounds(&width, &height)) {
1194 GLint draw_buffer = GL_NONE;
1195 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1196 glReadBuffer(draw_buffer);
1198 if (draw_buffer != GL_NONE) {
1199 GLint read_buffer = GL_NONE;
1200 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1202 GLint alpha_bits = 0;
1203 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1204 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1205 json.beginMember(enumToString(draw_buffer));
1206 dumpReadBufferImage(json, width, height, format);
1209 glReadBuffer(read_buffer);
1212 GLint depth_bits = 0;
1213 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1215 json.beginMember("GL_DEPTH_COMPONENT");
1216 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1220 GLint stencil_bits = 0;
1221 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1223 json.beginMember("GL_STENCIL_INDEX");
1224 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1229 * Dump the specified framebuffer attachment.
1231 * In the case of a color attachment, it assumes it is already bound for read.
1234 dumpFramebufferAttachment(JSONWriter &json, GLenum target, GLenum attachment, GLenum format)
1236 GLint width = 0, height = 0;
1237 if (!getFramebufferAttachmentSize(target, attachment, &width, &height)) {
1241 GLint internalFormat = getFramebufferAttachmentFormat(target, attachment);
1243 json.beginMember(enumToString(attachment));
1244 dumpReadBufferImage(json, width, height, format, internalFormat);
1250 dumpFramebufferAttachments(JSONWriter &json, GLenum target)
1252 GLint read_framebuffer = 0;
1253 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1255 GLint read_buffer = GL_NONE;
1256 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1258 GLint max_draw_buffers = 1;
1259 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1260 GLint max_color_attachments = 0;
1261 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1263 for (GLint i = 0; i < max_draw_buffers; ++i) {
1264 GLint draw_buffer = GL_NONE;
1265 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1266 if (draw_buffer != GL_NONE) {
1267 glReadBuffer(draw_buffer);
1269 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1270 attachment = draw_buffer;
1272 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1273 attachment = GL_COLOR_ATTACHMENT0;
1275 GLint alpha_size = 0;
1276 glGetFramebufferAttachmentParameteriv(target, attachment,
1277 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1279 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1280 dumpFramebufferAttachment(json, target, attachment, format);
1284 glReadBuffer(read_buffer);
1286 dumpFramebufferAttachment(json, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1287 dumpFramebufferAttachment(json, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1289 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1294 dumpFramebuffer(JSONWriter &json)
1296 json.beginMember("framebuffer");
1299 GLint boundDrawFbo = 0, boundReadFbo = 0;
1300 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1301 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1302 if (!boundDrawFbo) {
1303 dumpDrawableImages(json);
1305 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1306 GLint draw_buffer0 = GL_NONE;
1307 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1308 bool multisample = false;
1311 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1314 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1315 if (object_type == GL_RENDERBUFFER) {
1316 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1317 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1319 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1325 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1326 if (object_type == GL_RENDERBUFFER) {
1327 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1328 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1330 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1336 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1337 if (object_type == GL_RENDERBUFFER) {
1338 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1339 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1341 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1347 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1354 // glReadPixels doesnt support multisampled buffers so we need
1355 // to blit the fbo to a temporary one
1356 fboCopy = downsampledFramebuffer(boundDrawFbo, draw_buffer0,
1357 colorRb, depthRb, stencilRb,
1361 dumpFramebufferAttachments(json, GL_DRAW_FRAMEBUFFER);
1364 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1365 glDeleteRenderbuffers(numRbs, rbs);
1366 glDeleteFramebuffers(1, &fboCopy);
1369 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1370 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1374 json.endMember(); // framebuffer
1378 static const GLenum bindings[] = {
1381 GL_PIXEL_PACK_BUFFER_BINDING,
1382 GL_PIXEL_UNPACK_BUFFER_BINDING,
1383 GL_TEXTURE_BINDING_1D,
1384 GL_TEXTURE_BINDING_2D,
1385 GL_TEXTURE_BINDING_3D,
1386 GL_TEXTURE_BINDING_RECTANGLE,
1387 GL_TEXTURE_BINDING_CUBE_MAP,
1388 GL_DRAW_FRAMEBUFFER_BINDING,
1389 GL_READ_FRAMEBUFFER_BINDING,
1390 GL_RENDERBUFFER_BINDING,
1402 #define NUM_BINDINGS sizeof(bindings)/sizeof(bindings[0])
1405 void dumpCurrentContext(std::ostream &os)
1407 JSONWriter json(os);
1410 GLint old_bindings[NUM_BINDINGS];
1411 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1412 old_bindings[i] = 0;
1413 glGetIntegerv(bindings[i], &old_bindings[i]);
1417 dumpParameters(json);
1418 dumpShadersUniforms(json);
1420 dumpFramebuffer(json);
1423 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1424 GLint new_binding = 0;
1425 glGetIntegerv(bindings[i], &new_binding);
1426 if (new_binding != old_bindings[i]) {
1427 std::cerr << "warning: " << enumToString(bindings[i]) << " was clobbered\n";
1435 } /* namespace glstate */