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 **************************************************************************/
35 #include "glstate.hpp"
40 #include <Carbon/Carbon.h>
46 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
52 #endif /* __APPLE__ */
59 dumpShader(JSONWriter &json, GLuint shader)
65 GLint shader_type = 0;
66 glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
71 GLint source_length = 0;
72 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
77 GLchar *source = new GLchar[source_length];
80 glGetShaderSource(shader, source_length, &length, source);
82 json.beginMember(enumToString(shader_type));
83 json.writeString(source);
91 dumpShaderObj(JSONWriter &json, GLhandleARB shaderObj)
97 GLint shader_type = 0;
98 glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &shader_type);
103 GLint source_length = 0;
104 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
105 if (!source_length) {
109 GLcharARB *source = new GLcharARB[source_length];
112 glGetShaderSource(shaderObj, source_length, &length, source);
114 json.beginMember(enumToString(shader_type));
115 json.writeString(source);
123 dumpCurrentProgram(JSONWriter &json)
126 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
131 GLint attached_shaders = 0;
132 glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
133 if (!attached_shaders) {
137 GLuint *shaders = new GLuint[attached_shaders];
139 glGetAttachedShaders(program, attached_shaders, &count, shaders);
140 for (GLsizei i = 0; i < count; ++ i) {
141 dumpShader(json, shaders[i]);
148 dumpCurrentProgramObj(JSONWriter &json)
150 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
155 GLint attached_shaders = 0;
156 glGetProgramivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
157 if (!attached_shaders) {
161 GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
163 glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
164 for (GLsizei i = 0; i < count; ++ i) {
165 dumpShaderObj(json, shaderObjs[i]);
167 delete [] shaderObjs;
172 dumpArbProgram(JSONWriter &json, GLenum target)
174 if (!glIsEnabled(target)) {
178 GLint program_length = 0;
179 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
180 if (!program_length) {
184 GLchar *source = new GLchar[program_length + 1];
186 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
187 source[program_length] = 0;
189 json.beginMember(enumToString(target));
190 json.writeString(source);
198 dumpShaders(JSONWriter &json)
200 json.beginMember("shaders");
202 dumpCurrentProgram(json);
203 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
204 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
206 json.endMember(); //shaders
211 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
213 GLint width, height = 1, depth = 1;
216 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
218 if (target != GL_TEXTURE_1D) {
220 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
221 if (target == GL_TEXTURE_3D) {
223 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
227 if (width <= 0 || height <= 0 || depth <= 0) {
232 GLint active_texture = GL_TEXTURE0;
233 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
234 snprintf(label, sizeof label, "%s, %s, level = %i", enumToString(active_texture), enumToString(target), level);
236 json.beginMember(label);
240 // Tell the GUI this is no ordinary object, but an image
241 json.writeStringMember("__class__", "image");
243 json.writeNumberMember("__width__", width);
244 json.writeNumberMember("__height__", height);
245 json.writeNumberMember("__depth__", depth);
247 // Hardcoded for now, but we could chose types more adequate to the
248 // texture internal format
249 json.writeStringMember("__type__", "uint8");
250 json.writeBoolMember("__normalized__", true);
251 json.writeNumberMember("__channels__", 4);
253 GLubyte *pixels = new GLubyte[depth*width*height*4];
255 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
257 json.beginMember("__data__");
260 Image::writePixelsToBuffer(pixels, width, height, 4, false, &pngBuffer, &pngBufferSize);
261 json.writeBase64(pngBuffer, pngBufferSize);
263 json.endMember(); // __data__
272 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
274 GLint texture_binding = 0;
275 glGetIntegerv(binding, &texture_binding);
276 if (!glIsEnabled(target) && !texture_binding) {
282 GLint width = 0, height = 0;
283 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
284 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
285 if (!width || !height) {
289 if (target == GL_TEXTURE_CUBE_MAP) {
290 for (int face = 0; face < 6; ++face) {
291 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
294 dumpTextureImage(json, target, level);
303 dumpTextures(JSONWriter &json)
305 json.beginMember("textures");
307 GLint active_texture = GL_TEXTURE0;
308 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
309 GLint max_texture_coords = 0;
310 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
311 GLint max_combined_texture_image_units = 0;
312 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
313 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
314 for (GLint unit = 0; unit < max_units; ++unit) {
315 GLenum texture = GL_TEXTURE0 + unit;
316 glActiveTexture(texture);
317 dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
318 dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
319 dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
320 dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
321 dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
323 glActiveTexture(active_texture);
325 json.endMember(); // textures
330 getDrawableBounds(GLint *width, GLint *height) {
333 HDC hDC = wglGetCurrentDC();
338 HWND hWnd = WindowFromDC(hDC);
341 if (!GetClientRect(hWnd, &rect)) {
345 *width = rect.right - rect.left;
346 *height = rect.bottom - rect.top;
348 #elif defined(__APPLE__)
350 CGLContextObj ctx = CGLGetCurrentContext();
359 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
365 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
369 *width = rect.size.width;
370 *height = rect.size.height;
378 unsigned int w, h, bw, depth;
380 display = glXGetCurrentDisplay();
385 drawable = glXGetCurrentDrawable();
386 if (drawable == None) {
390 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
404 getDrawBufferImage(GLenum format) {
405 GLint channels = __gl_format_channels(format);
411 if (!getDrawableBounds(&width, &height)) {
415 Image::Image *image = new Image::Image(width, height, channels, true);
420 GLint drawbuffer = GL_NONE;
421 GLint readbuffer = GL_NONE;
422 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
423 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
424 glReadBuffer(drawbuffer);
426 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
427 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
428 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
429 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
430 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
431 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
432 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
433 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
434 glPixelStorei(GL_PACK_ALIGNMENT, 1);
436 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
440 glReadBuffer(readbuffer);
447 dumpDrawBufferImage(JSONWriter &json, GLenum format)
449 GLint channels = __gl_format_channels(format);
453 if (!getDrawableBounds(&width, &height)) {
458 // Tell the GUI this is no ordinary object, but an image
459 json.writeStringMember("__class__", "image");
461 json.writeNumberMember("__width__", width);
462 json.writeNumberMember("__height__", height);
463 json.writeNumberMember("__depth__", 1);
465 // Hardcoded for now, but we could chose types more adequate to the
466 // texture internal format
467 json.writeStringMember("__type__", "uint8");
468 json.writeBoolMember("__normalized__", true);
469 json.writeNumberMember("__channels__", channels);
471 GLubyte *pixels = new GLubyte[width*height*channels];
473 GLint drawbuffer = GL_NONE;
474 GLint readbuffer = GL_NONE;
475 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
476 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
477 glReadBuffer(drawbuffer);
479 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
480 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
481 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
482 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
483 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
484 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
485 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
486 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
487 glPixelStorei(GL_PACK_ALIGNMENT, 1);
489 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
492 glReadBuffer(readbuffer);
494 json.beginMember("__data__");
497 Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
498 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
499 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
500 json.writeBase64(pngBuffer, pngBufferSize);
502 json.endMember(); // __data__
511 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
512 GLint colorRb, GLint depthRb, GLint stencilRb,
513 GLuint *rbs, GLint *numRbs)
521 glGenFramebuffers(1, &fbo);
522 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
524 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
525 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
526 GL_RENDERBUFFER_WIDTH, &w);
527 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
528 GL_RENDERBUFFER_HEIGHT, &h);
529 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
530 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
532 glGenRenderbuffers(1, &rbs[*numRbs]);
533 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
534 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
535 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
536 GL_RENDERBUFFER, rbs[*numRbs]);
538 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
539 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
540 glDrawBuffer(drawbuffer);
541 glReadBuffer(drawbuffer);
542 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
543 GL_COLOR_BUFFER_BIT, GL_NEAREST);
544 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
547 if (stencilRb == depthRb && stencilRb) {
548 //combined depth and stencil buffer
549 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
550 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
551 GL_RENDERBUFFER_WIDTH, &w);
552 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
553 GL_RENDERBUFFER_HEIGHT, &h);
554 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
555 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
557 glGenRenderbuffers(1, &rbs[*numRbs]);
558 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
559 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
560 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
561 GL_RENDERBUFFER, rbs[*numRbs]);
562 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
563 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
564 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
565 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
566 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
570 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
571 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
572 GL_RENDERBUFFER_WIDTH, &w);
573 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
574 GL_RENDERBUFFER_HEIGHT, &h);
575 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
576 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
578 glGenRenderbuffers(1, &rbs[*numRbs]);
579 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
580 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
581 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
583 GL_RENDERBUFFER, rbs[*numRbs]);
584 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
585 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
586 glDrawBuffer(GL_DEPTH_ATTACHMENT);
587 glReadBuffer(GL_DEPTH_ATTACHMENT);
588 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
589 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
593 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
594 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
595 GL_RENDERBUFFER_WIDTH, &w);
596 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
597 GL_RENDERBUFFER_HEIGHT, &h);
598 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
599 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
601 glGenRenderbuffers(1, &rbs[*numRbs]);
602 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
603 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
604 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
605 GL_STENCIL_ATTACHMENT,
606 GL_RENDERBUFFER, rbs[*numRbs]);
607 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
608 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
609 glDrawBuffer(GL_STENCIL_ATTACHMENT);
610 glReadBuffer(GL_STENCIL_ATTACHMENT);
611 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
612 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
622 dumpDrawBuffers(JSONWriter &json, GLboolean dumpDepth, GLboolean dumpStencil)
624 json.beginMember("GL_RGBA");
625 dumpDrawBufferImage(json, GL_RGBA);
629 json.beginMember("GL_DEPTH_COMPONENT");
630 dumpDrawBufferImage(json, GL_DEPTH_COMPONENT);
635 json.beginMember("GL_STENCIL_INDEX");
636 dumpDrawBufferImage(json, GL_STENCIL_INDEX);
643 dumpFramebuffer(JSONWriter &json)
645 json.beginMember("framebuffer");
648 GLint boundDrawFbo = 0, boundReadFbo = 0;
649 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
650 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
652 GLint depth_bits = 0;
653 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
654 GLint stencil_bits = 0;
655 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
656 dumpDrawBuffers(json, depth_bits, stencil_bits);
658 GLint colorRb, stencilRb, depthRb;
660 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
661 GLint drawbuffer = GL_NONE;
662 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
664 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
666 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
668 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
670 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
672 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
673 GL_STENCIL_ATTACHMENT,
674 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
677 GLint colorSamples, depthSamples, stencilSamples;
681 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
682 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
683 GL_RENDERBUFFER_SAMPLES, &colorSamples);
684 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
685 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
686 GL_RENDERBUFFER_SAMPLES, &depthSamples);
687 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
688 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
689 GL_RENDERBUFFER_SAMPLES, &stencilSamples);
690 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
692 if (colorSamples || depthSamples || stencilSamples) {
693 //glReadPixels doesnt support multisampled buffers so we need
694 // to blit the fbo to a temporary one
695 fboCopy = downsampledFramebuffer(boundDrawFbo, drawbuffer,
696 colorRb, depthRb, stencilRb,
699 glDrawBuffer(drawbuffer);
700 glReadBuffer(drawbuffer);
702 dumpDrawBuffers(json, depthRb, stencilRb);
705 glBindFramebuffer(GL_FRAMEBUFFER, boundDrawFbo);
706 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
707 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
708 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
709 glDeleteRenderbuffers(numRbs, rbs);
710 glDeleteFramebuffers(1, &fboCopy);
715 json.endMember(); // framebuffer
719 void dumpCurrentContext(std::ostream &os)
722 dumpParameters(json);
725 dumpFramebuffer(json);
729 } /* namespace glstate */