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"
39 #include "glstate_internal.hpp"
44 #include <Carbon/Carbon.h>
50 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
56 #endif /* __APPLE__ */
62 Context::Context(void) {
63 memset(this, 0, sizeof *this);
65 const char *version = (const char *)glGetString(GL_VERSION);
67 if (version[0] == 'O' &&
81 ARB_draw_buffers = !ES;
83 // TODO: Check extensions we use below
87 Context::resetPixelPackState(void) {
89 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
90 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
91 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
92 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
93 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
94 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
95 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
96 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
97 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
100 glGetIntegerv(GL_PACK_ALIGNMENT, &packAlignment);
102 glPixelStorei(GL_PACK_ALIGNMENT, 1);
106 Context::restorePixelPackState(void) {
110 glPixelStorei(GL_PACK_ALIGNMENT, packAlignment);
115 // Mapping from shader type to shader source, used to accumulated the sources
116 // of different shaders with same type.
117 typedef std::map<std::string, std::string> ShaderMap;
121 getShaderSource(ShaderMap &shaderMap, GLuint shader)
127 GLint shader_type = 0;
128 glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
133 GLint source_length = 0;
134 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
135 if (!source_length) {
139 GLchar *source = new GLchar[source_length];
142 glGetShaderSource(shader, source_length, &length, source);
144 shaderMap[enumToString(shader_type)] += source;
151 getShaderObjSource(ShaderMap &shaderMap, GLhandleARB shaderObj)
157 GLint object_type = 0;
158 glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &object_type);
159 if (object_type != GL_SHADER_OBJECT_ARB) {
163 GLint shader_type = 0;
164 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SUBTYPE_ARB, &shader_type);
169 GLint source_length = 0;
170 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
171 if (!source_length) {
175 GLcharARB *source = new GLcharARB[source_length];
178 glGetShaderSource(shaderObj, source_length, &length, source);
180 shaderMap[enumToString(shader_type)] += source;
187 dumpProgram(JSONWriter &json, GLint program)
189 GLint attached_shaders = 0;
190 glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
191 if (!attached_shaders) {
197 GLuint *shaders = new GLuint[attached_shaders];
199 glGetAttachedShaders(program, attached_shaders, &count, shaders);
200 std::sort(shaders, shaders + count);
201 for (GLsizei i = 0; i < count; ++ i) {
202 getShaderSource(shaderMap, shaders[i]);
206 for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
207 json.beginMember(it->first);
208 json.writeString(it->second);
215 dumpProgramObj(JSONWriter &json, GLhandleARB programObj)
217 GLint attached_shaders = 0;
218 glGetObjectParameterivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
219 if (!attached_shaders) {
225 GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
227 glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
228 std::sort(shaderObjs, shaderObjs + count);
229 for (GLsizei i = 0; i < count; ++ i) {
230 getShaderObjSource(shaderMap, shaderObjs[i]);
232 delete [] shaderObjs;
234 for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
235 json.beginMember(it->first);
236 json.writeString(it->second);
242 * When fetching the uniform name of an array we usually get name[0]
243 * so we need to cut the trailing "[0]" in order to properly construct
244 * array names later. Otherwise we endup with stuff like
245 * uniformArray[0][0],
246 * uniformArray[0][1],
252 resolveUniformName(const GLchar *name, GLint size)
254 std::string qualifiedName(name);
256 std::string::size_type nameLength = qualifiedName.length();
257 static const char * const arrayStart = "[0]";
258 static const int arrayStartLen = 3;
259 if (qualifiedName.rfind(arrayStart) == (nameLength - arrayStartLen)) {
260 qualifiedName = qualifiedName.substr(0, nameLength - 3);
263 return qualifiedName;
267 dumpUniform(JSONWriter &json, GLint program, GLint size, GLenum type, const GLchar *name) {
270 __gl_uniform_size(type, elemType, numElems);
271 if (elemType == GL_NONE) {
275 GLfloat fvalues[4*4];
276 GLdouble dvalues[4*4];
278 GLuint uivalues[4*4];
282 std::string qualifiedName = resolveUniformName(name, size);
284 for (i = 0; i < size; ++i) {
285 std::stringstream ss;
289 ss << '[' << i << ']';
292 std::string elemName = ss.str();
294 json.beginMember(elemName);
296 GLint location = glGetUniformLocation(program, elemName.c_str());
304 glGetUniformfv(program, location, fvalues);
305 for (j = 0; j < numElems; ++j) {
306 json.writeNumber(fvalues[j]);
310 glGetUniformdv(program, location, dvalues);
311 for (j = 0; j < numElems; ++j) {
312 json.writeNumber(dvalues[j]);
316 glGetUniformiv(program, location, ivalues);
317 for (j = 0; j < numElems; ++j) {
318 json.writeNumber(ivalues[j]);
321 case GL_UNSIGNED_INT:
322 glGetUniformuiv(program, location, uivalues);
323 for (j = 0; j < numElems; ++j) {
324 json.writeNumber(uivalues[j]);
328 glGetUniformiv(program, location, ivalues);
329 for (j = 0; j < numElems; ++j) {
330 json.writeBool(ivalues[j]);
348 dumpUniformARB(JSONWriter &json, GLhandleARB programObj, GLint size, GLenum type, const GLchar *name) {
352 __gl_uniform_size(type, elemType, numElems);
353 if (elemType == GL_NONE) {
357 GLfloat fvalues[4*4];
362 std::string qualifiedName = resolveUniformName(name, size);
364 for (i = 0; i < size; ++i) {
365 std::stringstream ss;
369 ss << '[' << i << ']';
372 std::string elemName = ss.str();
374 json.beginMember(elemName);
376 GLint location = glGetUniformLocationARB(programObj, elemName.c_str());
384 // glGetUniformdvARB does not exists
386 glGetUniformfvARB(programObj, location, fvalues);
387 for (j = 0; j < numElems; ++j) {
388 json.writeNumber(fvalues[j]);
391 case GL_UNSIGNED_INT:
392 // glGetUniformuivARB does not exists
394 glGetUniformivARB(programObj, location, ivalues);
395 for (j = 0; j < numElems; ++j) {
396 json.writeNumber(ivalues[j]);
400 glGetUniformivARB(programObj, location, ivalues);
401 for (j = 0; j < numElems; ++j) {
402 json.writeBool(ivalues[j]);
420 dumpProgramUniforms(JSONWriter &json, GLint program)
422 GLint active_uniforms = 0;
423 glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &active_uniforms);
424 if (!active_uniforms) {
428 GLint active_uniform_max_length = 0;
429 glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &active_uniform_max_length);
430 GLchar *name = new GLchar[active_uniform_max_length];
435 for (GLint index = 0; index < active_uniforms; ++index) {
438 GLenum type = GL_NONE;
439 glGetActiveUniform(program, index, active_uniform_max_length, &length, &size, &type, name);
441 dumpUniform(json, program, size, type, name);
449 dumpProgramObjUniforms(JSONWriter &json, GLhandleARB programObj)
451 GLint active_uniforms = 0;
452 glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &active_uniforms);
453 if (!active_uniforms) {
457 GLint active_uniform_max_length = 0;
458 glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB, &active_uniform_max_length);
459 GLchar *name = new GLchar[active_uniform_max_length];
464 for (GLint index = 0; index < active_uniforms; ++index) {
467 GLenum type = GL_NONE;
468 glGetActiveUniformARB(programObj, index, active_uniform_max_length, &length, &size, &type, name);
470 dumpUniformARB(json, programObj, size, type, name);
478 dumpArbProgram(JSONWriter &json, GLenum target)
480 if (!glIsEnabled(target)) {
484 GLint program_length = 0;
485 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
486 if (!program_length) {
490 GLchar *source = new GLchar[program_length + 1];
492 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
493 source[program_length] = 0;
495 json.beginMember(enumToString(target));
496 json.writeString(source);
504 dumpArbProgramUniforms(JSONWriter &json, GLenum target, const char *prefix)
506 if (!glIsEnabled(target)) {
510 GLint program_parameters = 0;
511 glGetProgramivARB(target, GL_PROGRAM_PARAMETERS_ARB, &program_parameters);
512 if (!program_parameters) {
516 GLint max_program_local_parameters = 0;
517 glGetProgramivARB(target, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &max_program_local_parameters);
518 for (GLint index = 0; index < max_program_local_parameters; ++index) {
519 GLdouble params[4] = {0, 0, 0, 0};
520 glGetProgramLocalParameterdvARB(target, index, params);
522 if (!params[0] && !params[1] && !params[2] && !params[3]) {
527 snprintf(name, sizeof name, "%sprogram.local[%i]", prefix, index);
529 json.beginMember(name);
531 json.writeNumber(params[0]);
532 json.writeNumber(params[1]);
533 json.writeNumber(params[2]);
534 json.writeNumber(params[3]);
539 GLint max_program_env_parameters = 0;
540 glGetProgramivARB(target, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, &max_program_env_parameters);
541 for (GLint index = 0; index < max_program_env_parameters; ++index) {
542 GLdouble params[4] = {0, 0, 0, 0};
543 glGetProgramEnvParameterdvARB(target, index, params);
545 if (!params[0] && !params[1] && !params[2] && !params[3]) {
550 snprintf(name, sizeof name, "%sprogram.env[%i]", prefix, index);
552 json.beginMember(name);
554 json.writeNumber(params[0]);
555 json.writeNumber(params[1]);
556 json.writeNumber(params[2]);
557 json.writeNumber(params[3]);
565 dumpShadersUniforms(JSONWriter &json)
568 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
570 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
572 json.beginMember("shaders");
575 dumpProgram(json, program);
576 } else if (programObj) {
577 dumpProgramObj(json, programObj);
579 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
580 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
583 json.endMember(); // shaders
585 json.beginMember("uniforms");
588 dumpProgramUniforms(json, program);
589 } else if (programObj) {
590 dumpProgramObjUniforms(json, programObj);
592 dumpArbProgramUniforms(json, GL_FRAGMENT_PROGRAM_ARB, "fp.");
593 dumpArbProgramUniforms(json, GL_VERTEX_PROGRAM_ARB, "vp.");
596 json.endMember(); // uniforms
601 dumpTextureImage(JSONWriter &json, Context &context, GLenum target, GLint level)
603 GLint width, height = 1, depth = 1;
606 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &format);
609 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
611 if (target != GL_TEXTURE_1D) {
613 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
614 if (target == GL_TEXTURE_3D) {
616 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
620 if (width <= 0 || height <= 0 || depth <= 0) {
625 GLint active_texture = GL_TEXTURE0;
626 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
627 snprintf(label, sizeof label, "%s, %s, level = %d",
628 enumToString(active_texture), enumToString(target), level);
630 json.beginMember(label);
634 // Tell the GUI this is no ordinary object, but an image
635 json.writeStringMember("__class__", "image");
637 json.writeNumberMember("__width__", width);
638 json.writeNumberMember("__height__", height);
639 json.writeNumberMember("__depth__", depth);
641 json.writeStringMember("__format__", enumToString(format));
643 // Hardcoded for now, but we could chose types more adequate to the
644 // texture internal format
645 json.writeStringMember("__type__", "uint8");
646 json.writeBoolMember("__normalized__", true);
647 json.writeNumberMember("__channels__", 4);
649 GLubyte *pixels = new GLubyte[depth*width*height*4];
651 context.resetPixelPackState();
653 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
655 context.restorePixelPackState();
657 json.beginMember("__data__");
660 image::writePixelsToBuffer(pixels, width, height, 4, true, &pngBuffer, &pngBufferSize);
661 json.writeBase64(pngBuffer, pngBufferSize);
663 json.endMember(); // __data__
672 dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
674 GLint texture_binding = 0;
675 glGetIntegerv(binding, &texture_binding);
676 if (!glIsEnabled(target) && !texture_binding) {
682 GLint width = 0, height = 0;
683 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
684 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
685 if (!width || !height) {
689 if (target == GL_TEXTURE_CUBE_MAP) {
690 for (int face = 0; face < 6; ++face) {
691 dumpTextureImage(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
694 dumpTextureImage(json, context, target, level);
703 dumpTextures(JSONWriter &json, Context &context)
705 json.beginMember("textures");
707 GLint active_texture = GL_TEXTURE0;
708 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
709 GLint max_texture_coords = 0;
710 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
711 GLint max_combined_texture_image_units = 0;
712 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
713 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
714 for (GLint unit = 0; unit < max_units; ++unit) {
715 GLenum texture = GL_TEXTURE0 + unit;
716 glActiveTexture(texture);
717 dumpTexture(json, context, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
718 dumpTexture(json, context, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
719 dumpTexture(json, context, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
720 dumpTexture(json, context, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
721 dumpTexture(json, context, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
723 glActiveTexture(active_texture);
725 json.endMember(); // textures
730 getDrawableBounds(GLint *width, GLint *height) {
731 #if defined(TRACE_EGL)
733 EGLContext currentContext = eglGetCurrentContext();
734 if (currentContext == EGL_NO_CONTEXT) {
738 EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW);
739 if (currentSurface == EGL_NO_SURFACE) {
743 EGLDisplay currentDisplay = eglGetCurrentDisplay();
744 if (currentDisplay == EGL_NO_DISPLAY) {
748 if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) ||
749 !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) {
755 #elif defined(_WIN32)
757 HDC hDC = wglGetCurrentDC();
762 HWND hWnd = WindowFromDC(hDC);
765 if (!GetClientRect(hWnd, &rect)) {
769 *width = rect.right - rect.left;
770 *height = rect.bottom - rect.top;
773 #elif defined(__APPLE__)
775 CGLContextObj ctx = CGLGetCurrentContext();
784 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
790 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
794 *width = rect.size.width;
795 *height = rect.size.height;
798 #elif defined(HAVE_X11)
804 unsigned int w, h, bw, depth;
806 display = glXGetCurrentDisplay();
811 drawable = glXGetCurrentDrawable();
812 if (drawable == None) {
816 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
832 static const GLenum texture_bindings[][2] = {
833 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
834 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
835 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
836 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
837 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
842 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
845 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
846 target = texture_bindings[i][0];
848 GLenum binding = texture_bindings[i][1];
850 while (glGetError() != GL_NO_ERROR)
853 glGetIntegerv(binding, &bound_texture);
854 glBindTexture(target, texture);
856 if (glGetError() == GL_NO_ERROR) {
860 glBindTexture(target, bound_texture);
870 getTextureLevelSize(GLint texture, GLint level, GLint *width, GLint *height)
876 GLint bound_texture = 0;
877 if (!bindTexture(texture, target, bound_texture)) {
881 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, width);
882 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, height);
884 glBindTexture(target, bound_texture);
886 return *width > 0 && *height > 0;
891 getTextureLevelFormat(GLint texture, GLint level)
894 GLint bound_texture = 0;
895 if (!bindTexture(texture, target, bound_texture)) {
899 GLint format = GL_NONE;
900 glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &format);
902 glBindTexture(target, bound_texture);
910 getRenderbufferSize(GLint renderbuffer, GLint *width, GLint *height)
912 GLint bound_renderbuffer = 0;
913 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
914 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
918 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, width);
919 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, height);
921 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
923 return *width > 0 && *height > 0;
928 getRenderbufferFormat(GLint renderbuffer)
930 GLint bound_renderbuffer = 0;
931 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
932 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
934 GLint format = GL_NONE;
935 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
937 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
944 getFramebufferAttachmentSize(GLenum target, GLenum attachment, GLint *width, GLint *height)
946 GLint object_type = GL_NONE;
947 glGetFramebufferAttachmentParameteriv(target, attachment,
948 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
950 if (object_type == GL_NONE) {
954 GLint object_name = 0;
955 glGetFramebufferAttachmentParameteriv(target, attachment,
956 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
958 if (object_name == 0) {
962 if (object_type == GL_RENDERBUFFER) {
963 return getRenderbufferSize(object_name, width, height);
964 } else if (object_type == GL_TEXTURE) {
965 GLint texture_level = 0;
966 glGetFramebufferAttachmentParameteriv(target, attachment,
967 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
969 return getTextureLevelSize(object_name, texture_level, width, height);
971 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
979 getFramebufferAttachmentFormat(GLenum target, GLenum attachment)
981 GLint object_type = GL_NONE;
982 glGetFramebufferAttachmentParameteriv(target, attachment,
983 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
985 if (object_type == GL_NONE) {
989 GLint object_name = 0;
990 glGetFramebufferAttachmentParameteriv(target, attachment,
991 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
993 if (object_name == 0) {
997 if (object_type == GL_RENDERBUFFER) {
998 return getRenderbufferFormat(object_name);
999 } else if (object_type == GL_TEXTURE) {
1000 GLint texture_level = 0;
1001 glGetFramebufferAttachmentParameteriv(target, attachment,
1002 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
1004 return getTextureLevelFormat(object_name, texture_level);
1006 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
1014 getDrawBufferImage() {
1015 GLenum format = GL_RGB;
1016 GLint channels = __gl_format_channels(format);
1023 GLenum framebuffer_binding;
1024 GLenum framebuffer_target;
1026 framebuffer_binding = GL_FRAMEBUFFER_BINDING;
1027 framebuffer_target = GL_FRAMEBUFFER;
1029 framebuffer_binding = GL_DRAW_FRAMEBUFFER_BINDING;
1030 framebuffer_target = GL_DRAW_FRAMEBUFFER;
1033 GLint draw_framebuffer = 0;
1034 glGetIntegerv(framebuffer_binding, &draw_framebuffer);
1036 GLint draw_buffer = GL_NONE;
1037 GLint width, height;
1038 if (draw_framebuffer) {
1039 if (context.ARB_draw_buffers) {
1040 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
1041 if (draw_buffer == GL_NONE) {
1046 if (!getFramebufferAttachmentSize(framebuffer_target, draw_buffer, &width, &height)) {
1051 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1052 if (draw_buffer == GL_NONE) {
1057 if (!getDrawableBounds(&width, &height)) {
1062 image::Image *image = new image::Image(width, height, channels, true);
1067 while (glGetError() != GL_NO_ERROR) {}
1069 GLint read_framebuffer = 0;
1070 GLint read_buffer = 0;
1072 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1073 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
1075 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1076 glReadBuffer(draw_buffer);
1079 // TODO: reset imaging state too
1080 context.resetPixelPackState();
1082 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
1084 context.restorePixelPackState();
1087 glReadBuffer(read_buffer);
1088 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1091 GLenum error = glGetError();
1092 if (error != GL_NO_ERROR) {
1094 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
1095 error = glGetError();
1096 } while(error != GL_NO_ERROR);
1106 * Dump the image of the currently bound read buffer.
1109 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
1110 GLint internalFormat = GL_NONE)
1112 GLint channels = __gl_format_channels(format);
1118 // Tell the GUI this is no ordinary object, but an image
1119 json.writeStringMember("__class__", "image");
1121 json.writeNumberMember("__width__", width);
1122 json.writeNumberMember("__height__", height);
1123 json.writeNumberMember("__depth__", 1);
1125 json.writeStringMember("__format__", enumToString(internalFormat));
1127 // Hardcoded for now, but we could chose types more adequate to the
1128 // texture internal format
1129 json.writeStringMember("__type__", "uint8");
1130 json.writeBoolMember("__normalized__", true);
1131 json.writeNumberMember("__channels__", channels);
1133 GLubyte *pixels = new GLubyte[width*height*channels];
1135 // TODO: reset imaging state too
1136 context.resetPixelPackState();
1138 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
1140 context.restorePixelPackState();
1142 json.beginMember("__data__");
1145 image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
1146 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
1147 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
1148 json.writeBase64(pngBuffer, pngBufferSize);
1150 json.endMember(); // __data__
1157 static inline GLuint
1158 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
1159 GLint colorRb, GLint depthRb, GLint stencilRb,
1160 GLuint *rbs, GLint *numRbs)
1168 glGenFramebuffers(1, &fbo);
1169 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1171 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1172 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1173 GL_RENDERBUFFER_WIDTH, &w);
1174 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1175 GL_RENDERBUFFER_HEIGHT, &h);
1176 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1177 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1179 glGenRenderbuffers(1, &rbs[*numRbs]);
1180 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1181 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1182 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
1183 GL_RENDERBUFFER, rbs[*numRbs]);
1185 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1186 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1187 glDrawBuffer(drawbuffer);
1188 glReadBuffer(drawbuffer);
1189 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1190 GL_COLOR_BUFFER_BIT, GL_NEAREST);
1191 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1194 if (stencilRb == depthRb && stencilRb) {
1195 //combined depth and stencil buffer
1196 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1197 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1198 GL_RENDERBUFFER_WIDTH, &w);
1199 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1200 GL_RENDERBUFFER_HEIGHT, &h);
1201 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1202 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1204 glGenRenderbuffers(1, &rbs[*numRbs]);
1205 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1206 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1207 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
1208 GL_RENDERBUFFER, rbs[*numRbs]);
1209 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1210 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1211 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1212 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1213 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1217 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1218 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1219 GL_RENDERBUFFER_WIDTH, &w);
1220 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1221 GL_RENDERBUFFER_HEIGHT, &h);
1222 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1223 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1225 glGenRenderbuffers(1, &rbs[*numRbs]);
1226 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1227 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1228 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1229 GL_DEPTH_ATTACHMENT,
1230 GL_RENDERBUFFER, rbs[*numRbs]);
1231 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1232 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1233 glDrawBuffer(GL_DEPTH_ATTACHMENT);
1234 glReadBuffer(GL_DEPTH_ATTACHMENT);
1235 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1236 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
1240 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1241 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1242 GL_RENDERBUFFER_WIDTH, &w);
1243 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1244 GL_RENDERBUFFER_HEIGHT, &h);
1245 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1246 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1248 glGenRenderbuffers(1, &rbs[*numRbs]);
1249 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1250 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1251 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1252 GL_STENCIL_ATTACHMENT,
1253 GL_RENDERBUFFER, rbs[*numRbs]);
1254 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1255 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1256 glDrawBuffer(GL_STENCIL_ATTACHMENT);
1257 glReadBuffer(GL_STENCIL_ATTACHMENT);
1258 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1259 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1269 * Dump images of current draw drawable/window.
1272 dumpDrawableImages(JSONWriter &json, Context &context)
1274 GLint width, height;
1276 if (!getDrawableBounds(&width, &height)) {
1280 GLint draw_buffer = GL_NONE;
1282 draw_buffer = GL_BACK;
1284 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1285 glReadBuffer(draw_buffer);
1288 if (draw_buffer != GL_NONE) {
1289 GLint read_buffer = GL_NONE;
1291 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1294 GLint alpha_bits = 0;
1296 // XXX: Ignore alpha until we are able to match the traced visual
1297 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1299 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1300 json.beginMember(enumToString(draw_buffer));
1301 dumpReadBufferImage(json, width, height, format);
1305 glReadBuffer(read_buffer);
1310 GLint depth_bits = 0;
1311 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1313 json.beginMember("GL_DEPTH_COMPONENT");
1314 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1318 GLint stencil_bits = 0;
1319 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1321 json.beginMember("GL_STENCIL_INDEX");
1322 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1330 * Dump the specified framebuffer attachment.
1332 * In the case of a color attachment, it assumes it is already bound for read.
1335 dumpFramebufferAttachment(JSONWriter &json, GLenum target, GLenum attachment, GLenum format)
1337 GLint width = 0, height = 0;
1338 if (!getFramebufferAttachmentSize(target, attachment, &width, &height)) {
1342 GLint internalFormat = getFramebufferAttachmentFormat(target, attachment);
1344 json.beginMember(enumToString(attachment));
1345 dumpReadBufferImage(json, width, height, format, internalFormat);
1351 dumpFramebufferAttachments(JSONWriter &json, Context &context, GLenum target)
1353 GLint read_framebuffer = 0;
1354 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1356 GLint read_buffer = GL_NONE;
1357 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1359 GLint max_draw_buffers = 1;
1360 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1361 GLint max_color_attachments = 0;
1362 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1364 for (GLint i = 0; i < max_draw_buffers; ++i) {
1365 GLint draw_buffer = GL_NONE;
1366 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1367 if (draw_buffer != GL_NONE) {
1368 glReadBuffer(draw_buffer);
1370 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1371 attachment = draw_buffer;
1373 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1374 attachment = GL_COLOR_ATTACHMENT0;
1376 GLint alpha_size = 0;
1377 glGetFramebufferAttachmentParameteriv(target, attachment,
1378 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1380 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1381 dumpFramebufferAttachment(json, target, attachment, format);
1385 glReadBuffer(read_buffer);
1388 dumpFramebufferAttachment(json, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1389 dumpFramebufferAttachment(json, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1392 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1397 dumpFramebuffer(JSONWriter &json, Context &context)
1399 json.beginMember("framebuffer");
1402 GLint boundDrawFbo = 0, boundReadFbo = 0;
1403 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1404 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1405 if (!boundDrawFbo) {
1406 dumpDrawableImages(json, context);
1407 } else if (context.ES) {
1408 dumpFramebufferAttachments(json, context, GL_FRAMEBUFFER);
1410 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1411 GLint draw_buffer0 = GL_NONE;
1412 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1413 bool multisample = false;
1416 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1419 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1420 if (object_type == GL_RENDERBUFFER) {
1421 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1422 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1424 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1430 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1431 if (object_type == GL_RENDERBUFFER) {
1432 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1433 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1435 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1441 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1442 if (object_type == GL_RENDERBUFFER) {
1443 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1444 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1446 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1452 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1459 // glReadPixels doesnt support multisampled buffers so we need
1460 // to blit the fbo to a temporary one
1461 fboCopy = downsampledFramebuffer(boundDrawFbo, draw_buffer0,
1462 colorRb, depthRb, stencilRb,
1466 dumpFramebufferAttachments(json, context, GL_DRAW_FRAMEBUFFER);
1469 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1470 glDeleteRenderbuffers(numRbs, rbs);
1471 glDeleteFramebuffers(1, &fboCopy);
1474 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1475 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1479 json.endMember(); // framebuffer
1483 static const GLenum bindings[] = {
1486 GL_PIXEL_PACK_BUFFER_BINDING,
1487 GL_PIXEL_UNPACK_BUFFER_BINDING,
1488 GL_TEXTURE_BINDING_1D,
1489 GL_TEXTURE_BINDING_2D,
1490 GL_TEXTURE_BINDING_3D,
1491 GL_TEXTURE_BINDING_RECTANGLE,
1492 GL_TEXTURE_BINDING_CUBE_MAP,
1493 GL_DRAW_FRAMEBUFFER_BINDING,
1494 GL_READ_FRAMEBUFFER_BINDING,
1495 GL_RENDERBUFFER_BINDING,
1507 #define NUM_BINDINGS sizeof(bindings)/sizeof(bindings[0])
1510 void dumpCurrentContext(std::ostream &os)
1512 JSONWriter json(os);
1515 GLint old_bindings[NUM_BINDINGS];
1516 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1517 old_bindings[i] = 0;
1518 glGetIntegerv(bindings[i], &old_bindings[i]);
1524 dumpParameters(json, context);
1525 dumpShadersUniforms(json);
1526 dumpTextures(json, context);
1527 dumpFramebuffer(json, context);
1530 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1531 GLint new_binding = 0;
1532 glGetIntegerv(bindings[i], &new_binding);
1533 if (new_binding != old_bindings[i]) {
1534 std::cerr << "warning: " << enumToString(bindings[i]) << " was clobbered\n";
1542 } /* namespace glstate */