1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
38 #include "glstate.hpp"
43 #include <Carbon/Carbon.h>
49 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
55 #endif /* __APPLE__ */
62 resetPixelPackState(void) {
63 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
64 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
65 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
66 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
67 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
68 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
69 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
70 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
71 glPixelStorei(GL_PACK_ALIGNMENT, 1);
72 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
77 restorePixelPackState(void) {
82 // Mapping from shader type to shader source, used to accumulated the sources
83 // of different shaders with same type.
84 typedef std::map<std::string, std::string> ShaderMap;
88 getShaderSource(ShaderMap &shaderMap, GLuint shader)
94 GLint shader_type = 0;
95 glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
100 GLint source_length = 0;
101 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
102 if (!source_length) {
106 GLchar *source = new GLchar[source_length];
109 glGetShaderSource(shader, source_length, &length, source);
111 shaderMap[enumToString(shader_type)] += source;
118 getShaderObjSource(ShaderMap &shaderMap, GLhandleARB shaderObj)
124 GLint shader_type = 0;
125 glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &shader_type);
130 GLint source_length = 0;
131 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
132 if (!source_length) {
136 GLcharARB *source = new GLcharARB[source_length];
139 glGetShaderSource(shaderObj, source_length, &length, source);
141 shaderMap[enumToString(shader_type)] += source;
148 dumpCurrentProgram(JSONWriter &json)
151 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
156 GLint attached_shaders = 0;
157 glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
158 if (!attached_shaders) {
164 GLuint *shaders = new GLuint[attached_shaders];
166 glGetAttachedShaders(program, attached_shaders, &count, shaders);
167 std::sort(shaders, shaders + count);
168 for (GLsizei i = 0; i < count; ++ i) {
169 getShaderSource(shaderMap, shaders[i]);
173 for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
174 json.beginMember(it->first);
175 json.writeString(it->second);
182 dumpCurrentProgramObj(JSONWriter &json)
184 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
189 GLint attached_shaders = 0;
190 glGetObjectParameterivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
191 if (!attached_shaders) {
197 GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
199 glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
200 std::sort(shaderObjs, shaderObjs + count);
201 for (GLsizei i = 0; i < count; ++ i) {
202 getShaderObjSource(shaderMap, shaderObjs[i]);
204 delete [] shaderObjs;
206 for (ShaderMap::const_iterator it = shaderMap.begin(); it != shaderMap.end(); ++it) {
207 json.beginMember(it->first);
208 json.writeString(it->second);
215 dumpUniform(JSONWriter &json, GLint program, GLint size, GLenum type, const GLchar *name) {
219 __gl_uniform_size(type, elemType, numElems);
220 if (elemType == GL_NONE) {
224 GLfloat fvalues[4*4];
225 GLdouble dvalues[4*4];
227 GLuint uivalues[4*4];
231 for (i = 0; i < size; ++i) {
232 std::stringstream ss;
236 ss << '[' << i << ']';
239 std::string elemName = ss.str();
241 json.beginMember(elemName);
243 GLint location = glGetUniformLocation(program, elemName.c_str());
251 glGetUniformfv(program, location, fvalues);
252 for (j = 0; j < numElems; ++j) {
253 json.writeNumber(fvalues[j]);
257 glGetUniformdv(program, location, dvalues);
258 for (j = 0; j < numElems; ++j) {
259 json.writeNumber(dvalues[j]);
263 glGetUniformiv(program, location, ivalues);
264 for (j = 0; j < numElems; ++j) {
265 json.writeNumber(ivalues[j]);
268 case GL_UNSIGNED_INT:
269 glGetUniformuiv(program, location, uivalues);
270 for (j = 0; j < numElems; ++j) {
271 json.writeNumber(uivalues[j]);
275 glGetUniformiv(program, location, ivalues);
276 for (j = 0; j < numElems; ++j) {
277 json.writeBool(ivalues[j]);
295 dumpUniformARB(JSONWriter &json, GLhandleARB programObj, GLint size, GLenum type, const GLchar *name) {
299 __gl_uniform_size(type, elemType, numElems);
300 if (elemType == GL_NONE) {
304 GLfloat fvalues[4*4];
309 for (i = 0; i < size; ++i) {
310 std::stringstream ss;
314 ss << '[' << i << ']';
317 std::string elemName = ss.str();
319 json.beginMember(elemName);
321 GLint location = glGetUniformLocationARB(programObj, elemName.c_str());
329 // glGetUniformdvARB does not exists
331 glGetUniformfvARB(programObj, location, fvalues);
332 for (j = 0; j < numElems; ++j) {
333 json.writeNumber(fvalues[j]);
336 case GL_UNSIGNED_INT:
337 // glGetUniformuivARB does not exists
339 glGetUniformivARB(programObj, location, ivalues);
340 for (j = 0; j < numElems; ++j) {
341 json.writeNumber(ivalues[j]);
345 glGetUniformivARB(programObj, location, ivalues);
346 for (j = 0; j < numElems; ++j) {
347 json.writeBool(ivalues[j]);
365 dumpCurrentProgramUniforms(JSONWriter &json)
368 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
373 GLint active_uniforms = 0;
374 glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &active_uniforms);
375 if (!active_uniforms) {
379 GLint active_uniform_max_length = 0;
380 glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &active_uniform_max_length);
381 GLchar *name = new GLchar[active_uniform_max_length];
386 for (GLint index = 0; index < active_uniforms; ++index) {
389 GLenum type = GL_NONE;
390 glGetActiveUniform(program, index, active_uniform_max_length, &length, &size, &type, name);
392 dumpUniform(json, program, size, type, name);
400 dumpCurrentProgramUniformsARB(JSONWriter &json)
402 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
407 GLint active_uniforms = 0;
408 glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &active_uniforms);
409 if (!active_uniforms) {
413 GLint active_uniform_max_length = 0;
414 glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_UNIFORM_MAX_LENGTH_ARB, &active_uniform_max_length);
415 GLchar *name = new GLchar[active_uniform_max_length];
420 for (GLint index = 0; index < active_uniforms; ++index) {
423 GLenum type = GL_NONE;
424 glGetActiveUniformARB(programObj, index, active_uniform_max_length, &length, &size, &type, name);
426 dumpUniformARB(json, programObj, size, type, name);
434 dumpArbProgram(JSONWriter &json, GLenum target)
436 if (!glIsEnabled(target)) {
440 GLint program_length = 0;
441 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
442 if (!program_length) {
446 GLchar *source = new GLchar[program_length + 1];
448 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
449 source[program_length] = 0;
451 json.beginMember(enumToString(target));
452 json.writeString(source);
460 dumpProgramUniformsARB(JSONWriter &json, GLenum target, const char *prefix)
462 if (!glIsEnabled(target)) {
466 GLint program_parameters = 0;
467 glGetProgramivARB(target, GL_PROGRAM_PARAMETERS_ARB, &program_parameters);
468 if (!program_parameters) {
472 GLint max_program_local_parameters = 0;
473 glGetProgramivARB(target, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, &max_program_local_parameters);
474 for (GLuint index = 0; index < max_program_local_parameters; ++index) {
475 GLdouble params[4] = {0, 0, 0, 0};
476 glGetProgramLocalParameterdvARB(target, index, params);
478 if (!params[0] && !params[1] && !params[2] && !params[3]) {
483 snprintf(name, sizeof name, "%sprogram.local[%u]", prefix, index);
485 json.beginMember(name);
487 json.writeNumber(params[0]);
488 json.writeNumber(params[1]);
489 json.writeNumber(params[2]);
490 json.writeNumber(params[3]);
495 GLint max_program_env_parameters = 0;
496 glGetProgramivARB(target, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, &max_program_env_parameters);
497 for (GLuint index = 0; index < max_program_env_parameters; ++index) {
498 GLdouble params[4] = {0, 0, 0, 0};
499 glGetProgramEnvParameterdvARB(target, index, params);
501 if (!params[0] && !params[1] && !params[2] && !params[3]) {
506 snprintf(name, sizeof name, "%sprogram.env[%u]", prefix, index);
508 json.beginMember(name);
510 json.writeNumber(params[0]);
511 json.writeNumber(params[1]);
512 json.writeNumber(params[2]);
513 json.writeNumber(params[3]);
521 dumpShaders(JSONWriter &json)
523 json.beginMember("shaders");
525 dumpCurrentProgram(json);
526 dumpCurrentProgramObj(json);
527 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
528 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
530 json.endMember(); // shaders
535 dumpUniforms(JSONWriter &json)
537 json.beginMember("uniforms");
539 dumpCurrentProgramUniforms(json);
540 dumpCurrentProgramUniformsARB(json);
541 dumpProgramUniformsARB(json, GL_FRAGMENT_PROGRAM_ARB, "fp.");
542 dumpProgramUniformsARB(json, GL_VERTEX_PROGRAM_ARB, "vp.");
544 json.endMember(); // uniforms
549 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
551 GLint width, height = 1, depth = 1;
554 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
556 if (target != GL_TEXTURE_1D) {
558 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
559 if (target == GL_TEXTURE_3D) {
561 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
565 if (width <= 0 || height <= 0 || depth <= 0) {
570 GLint active_texture = GL_TEXTURE0;
571 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
572 snprintf(label, sizeof label, "%s, %s, level = %i", enumToString(active_texture), enumToString(target), level);
574 json.beginMember(label);
578 // Tell the GUI this is no ordinary object, but an image
579 json.writeStringMember("__class__", "image");
581 json.writeNumberMember("__width__", width);
582 json.writeNumberMember("__height__", height);
583 json.writeNumberMember("__depth__", depth);
585 // Hardcoded for now, but we could chose types more adequate to the
586 // texture internal format
587 json.writeStringMember("__type__", "uint8");
588 json.writeBoolMember("__normalized__", true);
589 json.writeNumberMember("__channels__", 4);
591 GLubyte *pixels = new GLubyte[depth*width*height*4];
593 resetPixelPackState();
595 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
597 restorePixelPackState();
599 json.beginMember("__data__");
602 Image::writePixelsToBuffer(pixels, width, height, 4, false, &pngBuffer, &pngBufferSize);
603 json.writeBase64(pngBuffer, pngBufferSize);
605 json.endMember(); // __data__
614 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
616 GLint texture_binding = 0;
617 glGetIntegerv(binding, &texture_binding);
618 if (!glIsEnabled(target) && !texture_binding) {
624 GLint width = 0, height = 0;
625 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
626 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
627 if (!width || !height) {
631 if (target == GL_TEXTURE_CUBE_MAP) {
632 for (int face = 0; face < 6; ++face) {
633 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
636 dumpTextureImage(json, target, level);
645 dumpTextures(JSONWriter &json)
647 json.beginMember("textures");
649 GLint active_texture = GL_TEXTURE0;
650 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
651 GLint max_texture_coords = 0;
652 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
653 GLint max_combined_texture_image_units = 0;
654 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
655 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
656 for (GLint unit = 0; unit < max_units; ++unit) {
657 GLenum texture = GL_TEXTURE0 + unit;
658 glActiveTexture(texture);
659 dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
660 dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
661 dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
662 dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
663 dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
665 glActiveTexture(active_texture);
667 json.endMember(); // textures
672 getDrawableBounds(GLint *width, GLint *height) {
675 HDC hDC = wglGetCurrentDC();
680 HWND hWnd = WindowFromDC(hDC);
683 if (!GetClientRect(hWnd, &rect)) {
687 *width = rect.right - rect.left;
688 *height = rect.bottom - rect.top;
690 #elif defined(__APPLE__)
692 CGLContextObj ctx = CGLGetCurrentContext();
701 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
707 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
711 *width = rect.size.width;
712 *height = rect.size.height;
720 unsigned int w, h, bw, depth;
722 display = glXGetCurrentDisplay();
727 drawable = glXGetCurrentDrawable();
728 if (drawable == None) {
732 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
745 static const GLenum texture_bindings[][2] = {
746 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
747 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
748 {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
749 {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
750 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
755 bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
758 for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
759 target = texture_bindings[i][0];
761 GLenum binding = texture_bindings[i][1];
763 while (glGetError() != GL_NO_ERROR)
766 glGetIntegerv(binding, &bound_texture);
767 glBindTexture(target, texture);
769 if (glGetError() == GL_NO_ERROR) {
773 glBindTexture(target, bound_texture);
783 getTextureLevelSize(GLint texture, GLint level, GLint *width, GLint *height)
789 GLint bound_texture = 0;
790 if (!bindTexture(texture, target, bound_texture)) {
794 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, width);
795 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, height);
797 glBindTexture(target, bound_texture);
799 return *width > 0 && *height > 0;
804 getRenderbufferSize(GLint renderbuffer, GLint *width, GLint *height)
806 GLint bound_renderbuffer = 0;
807 glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
808 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
812 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, width);
813 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, height);
815 glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
817 return *width > 0 && *height > 0;
822 getFramebufferAttachmentSize(GLenum target, GLenum attachment, GLint *width, GLint *height)
824 GLint object_type = GL_NONE;
825 glGetFramebufferAttachmentParameteriv(target, attachment,
826 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
828 if (object_type == GL_NONE) {
832 GLint object_name = 0;
833 glGetFramebufferAttachmentParameteriv(target, attachment,
834 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
836 if (object_name == 0) {
840 if (object_type == GL_RENDERBUFFER) {
841 return getRenderbufferSize(object_name, width, height);
842 } else if (object_type == GL_TEXTURE) {
843 GLint texture_level = 0;
844 glGetFramebufferAttachmentParameteriv(target, attachment,
845 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
847 return getTextureLevelSize(object_name, texture_level, width, height);
849 std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
856 getDrawBufferImage(GLenum format) {
857 GLint channels = __gl_format_channels(format);
862 GLint draw_framebuffer = 0;
863 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
865 GLint draw_buffer = GL_NONE;
867 if (draw_framebuffer) {
868 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
869 if (draw_buffer == GL_NONE) {
873 if (!getFramebufferAttachmentSize(GL_DRAW_FRAMEBUFFER, draw_buffer, &width, &height)) {
877 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
878 if (draw_buffer == GL_NONE) {
882 if (!getDrawableBounds(&width, &height)) {
887 Image::Image *image = new Image::Image(width, height, channels, true);
892 while (glGetError() != GL_NO_ERROR) {}
894 GLint read_framebuffer = 0;
895 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
896 glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
898 GLint read_buffer = 0;
899 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
900 glReadBuffer(draw_buffer);
902 // TODO: reset imaging state too
903 resetPixelPackState();
905 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
907 restorePixelPackState();
908 glReadBuffer(read_buffer);
909 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
911 GLenum error = glGetError();
912 if (error != GL_NO_ERROR) {
914 std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
915 error = glGetError();
916 } while(error != GL_NO_ERROR);
926 * Dump the image of the currently bound read buffer.
929 dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format)
931 GLint channels = __gl_format_channels(format);
935 // Tell the GUI this is no ordinary object, but an image
936 json.writeStringMember("__class__", "image");
938 json.writeNumberMember("__width__", width);
939 json.writeNumberMember("__height__", height);
940 json.writeNumberMember("__depth__", 1);
942 // Hardcoded for now, but we could chose types more adequate to the
943 // texture internal format
944 json.writeStringMember("__type__", "uint8");
945 json.writeBoolMember("__normalized__", true);
946 json.writeNumberMember("__channels__", channels);
948 GLubyte *pixels = new GLubyte[width*height*channels];
950 // TODO: reset imaging state too
951 resetPixelPackState();
953 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
955 restorePixelPackState();
957 json.beginMember("__data__");
960 Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
961 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
962 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
963 json.writeBase64(pngBuffer, pngBufferSize);
965 json.endMember(); // __data__
973 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
974 GLint colorRb, GLint depthRb, GLint stencilRb,
975 GLuint *rbs, GLint *numRbs)
983 glGenFramebuffers(1, &fbo);
984 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
986 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
987 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
988 GL_RENDERBUFFER_WIDTH, &w);
989 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
990 GL_RENDERBUFFER_HEIGHT, &h);
991 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
992 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
994 glGenRenderbuffers(1, &rbs[*numRbs]);
995 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
996 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
997 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
998 GL_RENDERBUFFER, rbs[*numRbs]);
1000 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1001 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1002 glDrawBuffer(drawbuffer);
1003 glReadBuffer(drawbuffer);
1004 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1005 GL_COLOR_BUFFER_BIT, GL_NEAREST);
1006 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1009 if (stencilRb == depthRb && stencilRb) {
1010 //combined depth and stencil buffer
1011 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1012 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1013 GL_RENDERBUFFER_WIDTH, &w);
1014 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1015 GL_RENDERBUFFER_HEIGHT, &h);
1016 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1017 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1019 glGenRenderbuffers(1, &rbs[*numRbs]);
1020 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1021 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1022 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
1023 GL_RENDERBUFFER, rbs[*numRbs]);
1024 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1025 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1026 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1027 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1028 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1032 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1033 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1034 GL_RENDERBUFFER_WIDTH, &w);
1035 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1036 GL_RENDERBUFFER_HEIGHT, &h);
1037 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1038 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1040 glGenRenderbuffers(1, &rbs[*numRbs]);
1041 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1042 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1043 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1044 GL_DEPTH_ATTACHMENT,
1045 GL_RENDERBUFFER, rbs[*numRbs]);
1046 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1047 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1048 glDrawBuffer(GL_DEPTH_ATTACHMENT);
1049 glReadBuffer(GL_DEPTH_ATTACHMENT);
1050 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1051 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
1055 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1056 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1057 GL_RENDERBUFFER_WIDTH, &w);
1058 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1059 GL_RENDERBUFFER_HEIGHT, &h);
1060 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
1061 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
1063 glGenRenderbuffers(1, &rbs[*numRbs]);
1064 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
1065 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
1066 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
1067 GL_STENCIL_ATTACHMENT,
1068 GL_RENDERBUFFER, rbs[*numRbs]);
1069 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
1070 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
1071 glDrawBuffer(GL_STENCIL_ATTACHMENT);
1072 glReadBuffer(GL_STENCIL_ATTACHMENT);
1073 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
1074 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
1084 * Dump images of current draw drawable/window.
1087 dumpDrawableImages(JSONWriter &json)
1089 GLint width, height;
1091 if (!getDrawableBounds(&width, &height)) {
1095 GLint draw_buffer = GL_NONE;
1096 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
1097 glReadBuffer(draw_buffer);
1099 if (draw_buffer != GL_NONE) {
1100 GLint read_buffer = GL_NONE;
1101 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1103 GLint alpha_bits = 0;
1104 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
1105 GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
1106 json.beginMember(enumToString(draw_buffer));
1107 dumpReadBufferImage(json, width, height, format);
1110 glReadBuffer(read_buffer);
1113 GLint depth_bits = 0;
1114 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
1116 json.beginMember("GL_DEPTH_COMPONENT");
1117 dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
1121 GLint stencil_bits = 0;
1122 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
1124 json.beginMember("GL_STENCIL_INDEX");
1125 dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
1130 * Dump the specified framebuffer attachment.
1132 * In the case of a color attachment, it assumes it is already bound for read.
1135 dumpFramebufferAttachment(JSONWriter &json, GLenum target, GLenum attachment, GLenum format)
1137 GLint width = 0, height = 0;
1138 if (!getFramebufferAttachmentSize(target, attachment, &width, &height)) {
1142 json.beginMember(enumToString(attachment));
1143 dumpReadBufferImage(json, width, height, format);
1149 dumpFramebufferAttachments(JSONWriter &json, GLenum target)
1151 GLint read_framebuffer = 0;
1152 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
1154 GLint read_buffer = GL_NONE;
1155 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
1157 GLint max_draw_buffers = 1;
1158 glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
1159 GLint max_color_attachments = 0;
1160 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
1162 for (GLint i = 0; i < max_draw_buffers; ++i) {
1163 GLint draw_buffer = GL_NONE;
1164 glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
1165 if (draw_buffer != GL_NONE) {
1166 glReadBuffer(draw_buffer);
1168 if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
1169 attachment = draw_buffer;
1171 std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
1172 attachment = GL_COLOR_ATTACHMENT0;
1174 GLint alpha_size = 0;
1175 glGetFramebufferAttachmentParameteriv(target, attachment,
1176 GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
1178 GLenum format = alpha_size ? GL_RGBA : GL_RGB;
1179 dumpFramebufferAttachment(json, target, attachment, format);
1183 glReadBuffer(read_buffer);
1185 dumpFramebufferAttachment(json, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
1186 dumpFramebufferAttachment(json, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
1188 glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
1193 dumpFramebuffer(JSONWriter &json)
1195 json.beginMember("framebuffer");
1198 GLint boundDrawFbo = 0, boundReadFbo = 0;
1199 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
1200 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
1201 if (!boundDrawFbo) {
1202 dumpDrawableImages(json);
1204 GLint colorRb = 0, stencilRb = 0, depthRb = 0;
1205 GLint draw_buffer0 = GL_NONE;
1206 glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer0);
1207 bool multisample = false;
1210 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
1213 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1214 if (object_type == GL_RENDERBUFFER) {
1215 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, draw_buffer0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &colorRb);
1216 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
1218 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1224 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1225 if (object_type == GL_RENDERBUFFER) {
1226 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthRb);
1227 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
1229 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1235 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);
1236 if (object_type == GL_RENDERBUFFER) {
1237 glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &stencilRb);
1238 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
1240 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
1246 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
1253 // glReadPixels doesnt support multisampled buffers so we need
1254 // to blit the fbo to a temporary one
1255 fboCopy = downsampledFramebuffer(boundDrawFbo, draw_buffer0,
1256 colorRb, depthRb, stencilRb,
1260 dumpFramebufferAttachments(json, GL_DRAW_FRAMEBUFFER);
1263 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
1264 glDeleteRenderbuffers(numRbs, rbs);
1265 glDeleteFramebuffers(1, &fboCopy);
1268 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
1269 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
1273 json.endMember(); // framebuffer
1277 static const GLenum bindings[] = {
1280 GL_PIXEL_PACK_BUFFER_BINDING,
1281 GL_PIXEL_UNPACK_BUFFER_BINDING,
1282 GL_TEXTURE_BINDING_1D,
1283 GL_TEXTURE_BINDING_2D,
1284 GL_TEXTURE_BINDING_3D,
1285 GL_TEXTURE_BINDING_RECTANGLE,
1286 GL_TEXTURE_BINDING_CUBE_MAP,
1287 GL_DRAW_FRAMEBUFFER_BINDING,
1288 GL_READ_FRAMEBUFFER_BINDING,
1289 GL_RENDERBUFFER_BINDING,
1301 #define NUM_BINDINGS sizeof(bindings)/sizeof(bindings[0])
1304 void dumpCurrentContext(std::ostream &os)
1306 JSONWriter json(os);
1309 GLint old_bindings[NUM_BINDINGS];
1310 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1311 old_bindings[i] = 0;
1312 glGetIntegerv(bindings[i], &old_bindings[i]);
1316 dumpParameters(json);
1320 dumpFramebuffer(json);
1323 for (unsigned i = 0; i < NUM_BINDINGS; ++i) {
1324 GLint new_binding = 0;
1325 glGetIntegerv(bindings[i], &new_binding);
1326 if (new_binding != old_bindings[i]) {
1327 std::cerr << "warning: " << enumToString(bindings[i]) << " was clobbered\n";
1335 } /* namespace glstate */