- glActiveTexture(active_texture);
- json.endObject();
- json.endMember(); // textures
-}
-
-
-static bool
-getDrawableBounds(GLint *width, GLint *height) {
-#if defined(_WIN32)
-
- HDC hDC = wglGetCurrentDC();
- if (!hDC) {
- return false;
- }
-
- HWND hWnd = WindowFromDC(hDC);
- RECT rect;
-
- if (!GetClientRect(hWnd, &rect)) {
- return false;
- }
-
- *width = rect.right - rect.left;
- *height = rect.bottom - rect.top;
-
-#elif defined(__APPLE__)
-
- CGLContextObj ctx = CGLGetCurrentContext();
- if (ctx == NULL) {
- return false;
- }
-
- CGSConnectionID cid;
- CGSWindowID wid;
- CGSSurfaceID sid;
-
- if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
- return false;
- }
-
- CGRect rect;
-
- if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
- return false;
- }
-
- *width = rect.size.width;
- *height = rect.size.height;
-
-#else
-
-#if !TRACE_EGL
- Display *display;
- Drawable drawable;
- Window root;
- int x, y;
- unsigned int w, h, bw, depth;
-
- display = glXGetCurrentDisplay();
- if (!display) {
- return false;
- }
-
- drawable = glXGetCurrentDrawable();
- if (drawable == None) {
- return false;
- }
-
- if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
- return false;
- }
-
- *width = w;
- *height = h;
-#else
- return false;
-#endif
-
-#endif
-
- return true;
-}
-
-
-static const GLenum texture_bindings[][2] = {
- {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D},
- {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D},
- {GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D},
- {GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE},
- {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP}
-};
-
-
-static bool
-bindTexture(GLint texture, GLenum &target, GLint &bound_texture)
-{
-
- for (unsigned i = 0; i < sizeof(texture_bindings)/sizeof(texture_bindings[0]); ++i) {
- target = texture_bindings[i][0];
-
- GLenum binding = texture_bindings[i][1];
-
- while (glGetError() != GL_NO_ERROR)
- ;
-
- glGetIntegerv(binding, &bound_texture);
- glBindTexture(target, texture);
-
- if (glGetError() == GL_NO_ERROR) {
- return true;
- }
-
- glBindTexture(target, bound_texture);
- }
-
- target = GL_NONE;
-
- return false;
-}
-
-
-static bool
-getTextureLevelSize(GLint texture, GLint level, GLint *width, GLint *height)
-{
- *width = 0;
- *height = 0;
-
- GLenum target;
- GLint bound_texture = 0;
- if (!bindTexture(texture, target, bound_texture)) {
- return false;
- }
-
- glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, width);
- glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, height);
-
- glBindTexture(target, bound_texture);
-
- return *width > 0 && *height > 0;
-}
-
-
-static GLenum
-getTextureLevelFormat(GLint texture, GLint level)
-{
- GLenum target;
- GLint bound_texture = 0;
- if (!bindTexture(texture, target, bound_texture)) {
- return GL_NONE;
- }
-
- GLint format = GL_NONE;
- glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &format);
-
- glBindTexture(target, bound_texture);
-
- return format;
-}
-
-
-
-static bool
-getRenderbufferSize(GLint renderbuffer, GLint *width, GLint *height)
-{
- GLint bound_renderbuffer = 0;
- glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
- glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
-
- *width = 0;
- *height = 0;
- glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, width);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, height);
-
- glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
-
- return *width > 0 && *height > 0;
-}
-
-
-static GLenum
-getRenderbufferFormat(GLint renderbuffer)
-{
- GLint bound_renderbuffer = 0;
- glGetIntegerv(GL_RENDERBUFFER_BINDING, &bound_renderbuffer);
- glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
-
- GLint format = GL_NONE;
- glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
-
- glBindRenderbuffer(GL_RENDERBUFFER, bound_renderbuffer);
-
- return format;
-}
-
-
-static bool
-getFramebufferAttachmentSize(GLenum target, GLenum attachment, GLint *width, GLint *height)
-{
- GLint object_type = GL_NONE;
- glGetFramebufferAttachmentParameteriv(target, attachment,
- GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
- &object_type);
- if (object_type == GL_NONE) {
- return false;
- }
-
- GLint object_name = 0;
- glGetFramebufferAttachmentParameteriv(target, attachment,
- GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
- &object_name);
- if (object_name == 0) {
- return false;
- }
-
- if (object_type == GL_RENDERBUFFER) {
- return getRenderbufferSize(object_name, width, height);
- } else if (object_type == GL_TEXTURE) {
- GLint texture_level = 0;
- glGetFramebufferAttachmentParameteriv(target, attachment,
- GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
- &texture_level);
- return getTextureLevelSize(object_name, texture_level, width, height);
- } else {
- std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
- return false;
- }
-}
-
-
-
-static GLint
-getFramebufferAttachmentFormat(GLenum target, GLenum attachment)
-{
- GLint object_type = GL_NONE;
- glGetFramebufferAttachmentParameteriv(target, attachment,
- GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
- &object_type);
- if (object_type == GL_NONE) {
- return GL_NONE;
- }
-
- GLint object_name = 0;
- glGetFramebufferAttachmentParameteriv(target, attachment,
- GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
- &object_name);
- if (object_name == 0) {
- return GL_NONE;
- }
-
- if (object_type == GL_RENDERBUFFER) {
- return getRenderbufferFormat(object_name);
- } else if (object_type == GL_TEXTURE) {
- GLint texture_level = 0;
- glGetFramebufferAttachmentParameteriv(target, attachment,
- GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL,
- &texture_level);
- return getTextureLevelFormat(object_name, texture_level);
- } else {
- std::cerr << "warning: unexpected GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = " << object_type << "\n";
- return GL_NONE;
- }
-}
-
-
-
-image::Image *
-getDrawBufferImage() {
- GLenum format = GL_RGB;
- GLint channels = __gl_format_channels(format);
- if (channels > 4) {
- return NULL;
- }
-
- GLint draw_framebuffer = 0;
- glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
-
- GLint draw_buffer = GL_NONE;
- GLint width, height;
- if (draw_framebuffer) {
- glGetIntegerv(GL_DRAW_BUFFER0, &draw_buffer);
- if (draw_buffer == GL_NONE) {
- return NULL;
- }
-
- if (!getFramebufferAttachmentSize(GL_DRAW_FRAMEBUFFER, draw_buffer, &width, &height)) {
- return NULL;
- }
- } else {
- glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
- if (draw_buffer == GL_NONE) {
- return NULL;
- }
-
- if (!getDrawableBounds(&width, &height)) {
- return NULL;
- }
- }
-
- image::Image *image = new image::Image(width, height, channels, true);
- if (!image) {
- return NULL;
- }
-
- while (glGetError() != GL_NO_ERROR) {}
-
- GLint read_framebuffer = 0;
- glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
- glBindFramebuffer(GL_READ_FRAMEBUFFER, draw_framebuffer);
-
- GLint read_buffer = 0;
- glGetIntegerv(GL_READ_BUFFER, &read_buffer);
- glReadBuffer(draw_buffer);
-
- // TODO: reset imaging state too
- resetPixelPackState();
-
- glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
-
- restorePixelPackState();
- glReadBuffer(read_buffer);
- glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);
-
- GLenum error = glGetError();
- if (error != GL_NO_ERROR) {
- do {
- std::cerr << "warning: " << enumToString(error) << " while getting snapshot\n";
- error = glGetError();
- } while(error != GL_NO_ERROR);
- delete image;
- return NULL;
- }
-
- return image;
-}
-
-
-/**
- * Dump the image of the currently bound read buffer.
- */
-static inline void
-dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
- GLint internalFormat = GL_NONE)
-{
- GLint channels = __gl_format_channels(format);
-
- json.beginObject();
-
- // Tell the GUI this is no ordinary object, but an image
- json.writeStringMember("__class__", "image");
-
- json.writeNumberMember("__width__", width);
- json.writeNumberMember("__height__", height);
- json.writeNumberMember("__depth__", 1);
-
- json.writeStringMember("__format__", enumToString(internalFormat));
-
- // Hardcoded for now, but we could chose types more adequate to the
- // texture internal format
- json.writeStringMember("__type__", "uint8");
- json.writeBoolMember("__normalized__", true);
- json.writeNumberMember("__channels__", channels);
-
- GLubyte *pixels = new GLubyte[width*height*channels];
-
- // TODO: reset imaging state too
- resetPixelPackState();
-
- glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
-
- restorePixelPackState();
-
- json.beginMember("__data__");
- char *pngBuffer;
- int pngBufferSize;
- image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
- //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
- // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
- json.writeBase64(pngBuffer, pngBufferSize);
- free(pngBuffer);
- json.endMember(); // __data__
-
- delete [] pixels;
- json.endObject();
-}
-
-
-static inline GLuint
-downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
- GLint colorRb, GLint depthRb, GLint stencilRb,
- GLuint *rbs, GLint *numRbs)
-{
- GLuint fbo;
- GLint format;
- GLint w, h;
-
- *numRbs = 0;
-
- glGenFramebuffers(1, &fbo);
- glBindFramebuffer(GL_FRAMEBUFFER, fbo);
-
- glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_WIDTH, &w);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_HEIGHT, &h);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
-
- glGenRenderbuffers(1, &rbs[*numRbs]);
- glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
- glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
- GL_RENDERBUFFER, rbs[*numRbs]);
-
- glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
- glDrawBuffer(drawbuffer);
- glReadBuffer(drawbuffer);
- glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
- GL_COLOR_BUFFER_BIT, GL_NEAREST);
- glBindFramebuffer(GL_FRAMEBUFFER, fbo);
- ++*numRbs;
-
- if (stencilRb == depthRb && stencilRb) {
- //combined depth and stencil buffer
- glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_WIDTH, &w);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_HEIGHT, &h);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
-
- glGenRenderbuffers(1, &rbs[*numRbs]);
- glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
- glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
- GL_RENDERBUFFER, rbs[*numRbs]);
- glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
- glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
- GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
- glBindFramebuffer(GL_FRAMEBUFFER, fbo);
- ++*numRbs;
- } else {
- if (depthRb) {
- glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_WIDTH, &w);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_HEIGHT, &h);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
-
- glGenRenderbuffers(1, &rbs[*numRbs]);
- glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
- glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER,
- GL_DEPTH_ATTACHMENT,
- GL_RENDERBUFFER, rbs[*numRbs]);
- glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
- glDrawBuffer(GL_DEPTH_ATTACHMENT);
- glReadBuffer(GL_DEPTH_ATTACHMENT);
- glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
- GL_DEPTH_BUFFER_BIT, GL_NEAREST);
- ++*numRbs;
- }
- if (stencilRb) {
- glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_WIDTH, &w);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_HEIGHT, &h);
- glGetRenderbufferParameteriv(GL_RENDERBUFFER,
- GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
-
- glGenRenderbuffers(1, &rbs[*numRbs]);
- glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
- glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
- glFramebufferRenderbuffer(GL_FRAMEBUFFER,
- GL_STENCIL_ATTACHMENT,
- GL_RENDERBUFFER, rbs[*numRbs]);
- glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
- glDrawBuffer(GL_STENCIL_ATTACHMENT);
- glReadBuffer(GL_STENCIL_ATTACHMENT);
- glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
- GL_STENCIL_BUFFER_BIT, GL_NEAREST);
- ++*numRbs;
- }
- }
-
- return fbo;
-}
-
-
-/**
- * Dump images of current draw drawable/window.
- */
-static void
-dumpDrawableImages(JSONWriter &json)
-{
- GLint width, height;
-
- if (!getDrawableBounds(&width, &height)) {
- return;
- }
-
- GLint draw_buffer = GL_NONE;
- glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
- glReadBuffer(draw_buffer);
-
- if (draw_buffer != GL_NONE) {
- GLint read_buffer = GL_NONE;
- glGetIntegerv(GL_READ_BUFFER, &read_buffer);
-
- GLint alpha_bits = 0;
-#if 0
- // XXX: Ignore alpha until we are able to match the traced visual
- glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
-#endif
- GLenum format = alpha_bits ? GL_RGBA : GL_RGB;
- json.beginMember(enumToString(draw_buffer));
- dumpReadBufferImage(json, width, height, format);
- json.endMember();
-
- glReadBuffer(read_buffer);
- }
-
- GLint depth_bits = 0;
- glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
- if (depth_bits) {
- json.beginMember("GL_DEPTH_COMPONENT");
- dumpReadBufferImage(json, width, height, GL_DEPTH_COMPONENT);
- json.endMember();
- }
-
- GLint stencil_bits = 0;
- glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
- if (stencil_bits) {
- json.beginMember("GL_STENCIL_INDEX");
- dumpReadBufferImage(json, width, height, GL_STENCIL_INDEX);
- json.endMember();
- }
-}
-
-
-/**
- * Dump the specified framebuffer attachment.
- *
- * In the case of a color attachment, it assumes it is already bound for read.
- */
-static void
-dumpFramebufferAttachment(JSONWriter &json, GLenum target, GLenum attachment, GLenum format)
-{
- GLint width = 0, height = 0;
- if (!getFramebufferAttachmentSize(target, attachment, &width, &height)) {
- return;
- }
-
- GLint internalFormat = getFramebufferAttachmentFormat(target, attachment);
-
- json.beginMember(enumToString(attachment));
- dumpReadBufferImage(json, width, height, format, internalFormat);
- json.endMember();
-}
-
-
-static void
-dumpFramebufferAttachments(JSONWriter &json, GLenum target)
-{
- GLint read_framebuffer = 0;
- glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_framebuffer);
-
- GLint read_buffer = GL_NONE;
- glGetIntegerv(GL_READ_BUFFER, &read_buffer);
-
- GLint max_draw_buffers = 1;
- glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
- GLint max_color_attachments = 0;
- glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);
-
- for (GLint i = 0; i < max_draw_buffers; ++i) {
- GLint draw_buffer = GL_NONE;
- glGetIntegerv(GL_DRAW_BUFFER0 + i, &draw_buffer);
- if (draw_buffer != GL_NONE) {
- glReadBuffer(draw_buffer);
- GLint attachment;
- if (draw_buffer >= GL_COLOR_ATTACHMENT0 && draw_buffer < GL_COLOR_ATTACHMENT0 + max_color_attachments) {
- attachment = draw_buffer;
- } else {
- std::cerr << "warning: unexpected GL_DRAW_BUFFER" << i << " = " << draw_buffer << "\n";
- attachment = GL_COLOR_ATTACHMENT0;
- }
- GLint alpha_size = 0;
- glGetFramebufferAttachmentParameteriv(target, attachment,
- GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE,
- &alpha_size);
- GLenum format = alpha_size ? GL_RGBA : GL_RGB;
- dumpFramebufferAttachment(json, target, attachment, format);
- }
- }
-
- glReadBuffer(read_buffer);
-
- dumpFramebufferAttachment(json, target, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT);
- dumpFramebufferAttachment(json, target, GL_STENCIL_ATTACHMENT, GL_STENCIL_INDEX);
-
- glBindFramebuffer(GL_READ_FRAMEBUFFER, read_framebuffer);