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 resetPixelPackState(void) {
60 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
61 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
62 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
63 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
64 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
65 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
66 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
67 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
68 glPixelStorei(GL_PACK_ALIGNMENT, 1);
69 glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
74 restorePixelPackState(void) {
80 dumpShader(JSONWriter &json, GLuint shader)
86 GLint shader_type = 0;
87 glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
92 GLint source_length = 0;
93 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
98 GLchar *source = new GLchar[source_length];
101 glGetShaderSource(shader, source_length, &length, source);
103 json.beginMember(enumToString(shader_type));
104 json.writeString(source);
112 dumpShaderObj(JSONWriter &json, GLhandleARB shaderObj)
118 GLint shader_type = 0;
119 glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &shader_type);
124 GLint source_length = 0;
125 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
126 if (!source_length) {
130 GLcharARB *source = new GLcharARB[source_length];
133 glGetShaderSource(shaderObj, source_length, &length, source);
135 json.beginMember(enumToString(shader_type));
136 json.writeString(source);
144 dumpCurrentProgram(JSONWriter &json)
147 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
152 GLint attached_shaders = 0;
153 glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
154 if (!attached_shaders) {
158 GLuint *shaders = new GLuint[attached_shaders];
160 glGetAttachedShaders(program, attached_shaders, &count, shaders);
161 for (GLsizei i = 0; i < count; ++ i) {
162 dumpShader(json, shaders[i]);
169 dumpCurrentProgramObj(JSONWriter &json)
171 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
176 GLint attached_shaders = 0;
177 glGetProgramivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
178 if (!attached_shaders) {
182 GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
184 glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
185 for (GLsizei i = 0; i < count; ++ i) {
186 dumpShaderObj(json, shaderObjs[i]);
188 delete [] shaderObjs;
193 dumpArbProgram(JSONWriter &json, GLenum target)
195 if (!glIsEnabled(target)) {
199 GLint program_length = 0;
200 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
201 if (!program_length) {
205 GLchar *source = new GLchar[program_length + 1];
207 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
208 source[program_length] = 0;
210 json.beginMember(enumToString(target));
211 json.writeString(source);
219 dumpShaders(JSONWriter &json)
221 json.beginMember("shaders");
223 dumpCurrentProgram(json);
224 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
225 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
227 json.endMember(); //shaders
232 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
234 GLint width, height = 1, depth = 1;
237 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
239 if (target != GL_TEXTURE_1D) {
241 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
242 if (target == GL_TEXTURE_3D) {
244 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
248 if (width <= 0 || height <= 0 || depth <= 0) {
253 GLint active_texture = GL_TEXTURE0;
254 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
255 snprintf(label, sizeof label, "%s, %s, level = %i", enumToString(active_texture), enumToString(target), level);
257 json.beginMember(label);
261 // Tell the GUI this is no ordinary object, but an image
262 json.writeStringMember("__class__", "image");
264 json.writeNumberMember("__width__", width);
265 json.writeNumberMember("__height__", height);
266 json.writeNumberMember("__depth__", depth);
268 // Hardcoded for now, but we could chose types more adequate to the
269 // texture internal format
270 json.writeStringMember("__type__", "uint8");
271 json.writeBoolMember("__normalized__", true);
272 json.writeNumberMember("__channels__", 4);
274 GLubyte *pixels = new GLubyte[depth*width*height*4];
276 resetPixelPackState();
278 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
280 restorePixelPackState();
282 json.beginMember("__data__");
285 Image::writePixelsToBuffer(pixels, width, height, 4, false, &pngBuffer, &pngBufferSize);
286 json.writeBase64(pngBuffer, pngBufferSize);
288 json.endMember(); // __data__
297 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
299 GLint texture_binding = 0;
300 glGetIntegerv(binding, &texture_binding);
301 if (!glIsEnabled(target) && !texture_binding) {
307 GLint width = 0, height = 0;
308 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
309 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
310 if (!width || !height) {
314 if (target == GL_TEXTURE_CUBE_MAP) {
315 for (int face = 0; face < 6; ++face) {
316 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
319 dumpTextureImage(json, target, level);
328 dumpTextures(JSONWriter &json)
330 json.beginMember("textures");
332 GLint active_texture = GL_TEXTURE0;
333 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
334 GLint max_texture_coords = 0;
335 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
336 GLint max_combined_texture_image_units = 0;
337 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
338 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
339 for (GLint unit = 0; unit < max_units; ++unit) {
340 GLenum texture = GL_TEXTURE0 + unit;
341 glActiveTexture(texture);
342 dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
343 dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
344 dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
345 dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
346 dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
348 glActiveTexture(active_texture);
350 json.endMember(); // textures
355 getDrawableBounds(GLint *width, GLint *height) {
358 HDC hDC = wglGetCurrentDC();
363 HWND hWnd = WindowFromDC(hDC);
366 if (!GetClientRect(hWnd, &rect)) {
370 *width = rect.right - rect.left;
371 *height = rect.bottom - rect.top;
373 #elif defined(__APPLE__)
375 CGLContextObj ctx = CGLGetCurrentContext();
384 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
390 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
394 *width = rect.size.width;
395 *height = rect.size.height;
403 unsigned int w, h, bw, depth;
405 display = glXGetCurrentDisplay();
410 drawable = glXGetCurrentDrawable();
411 if (drawable == None) {
415 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
429 getDrawBufferImage(GLenum format) {
430 GLint channels = __gl_format_channels(format);
436 if (!getDrawableBounds(&width, &height)) {
440 Image::Image *image = new Image::Image(width, height, channels, true);
445 GLint draw_buffer = GL_NONE;
446 GLint read_buffer = GL_NONE;
447 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
448 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
449 glReadBuffer(draw_buffer);
451 resetPixelPackState();
453 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image->pixels);
455 restorePixelPackState();
456 glReadBuffer(read_buffer);
463 dumpDrawBufferImage(JSONWriter &json, GLenum format)
465 GLint channels = __gl_format_channels(format);
469 if (!getDrawableBounds(&width, &height)) {
474 // Tell the GUI this is no ordinary object, but an image
475 json.writeStringMember("__class__", "image");
477 json.writeNumberMember("__width__", width);
478 json.writeNumberMember("__height__", height);
479 json.writeNumberMember("__depth__", 1);
481 // Hardcoded for now, but we could chose types more adequate to the
482 // texture internal format
483 json.writeStringMember("__type__", "uint8");
484 json.writeBoolMember("__normalized__", true);
485 json.writeNumberMember("__channels__", channels);
487 GLubyte *pixels = new GLubyte[width*height*channels];
489 GLint draw_buffer = GL_NONE;
490 GLint read_buffer = GL_NONE;
491 glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
492 glGetIntegerv(GL_READ_BUFFER, &read_buffer);
493 glReadBuffer(draw_buffer);
495 resetPixelPackState();
497 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
499 restorePixelPackState();
500 glReadBuffer(read_buffer);
502 json.beginMember("__data__");
505 Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
506 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
507 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
508 json.writeBase64(pngBuffer, pngBufferSize);
510 json.endMember(); // __data__
519 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
520 GLint colorRb, GLint depthRb, GLint stencilRb,
521 GLuint *rbs, GLint *numRbs)
529 glGenFramebuffers(1, &fbo);
530 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
532 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
533 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
534 GL_RENDERBUFFER_WIDTH, &w);
535 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
536 GL_RENDERBUFFER_HEIGHT, &h);
537 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
538 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
540 glGenRenderbuffers(1, &rbs[*numRbs]);
541 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
542 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
543 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
544 GL_RENDERBUFFER, rbs[*numRbs]);
546 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
547 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
548 glDrawBuffer(drawbuffer);
549 glReadBuffer(drawbuffer);
550 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
551 GL_COLOR_BUFFER_BIT, GL_NEAREST);
552 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
555 if (stencilRb == depthRb && stencilRb) {
556 //combined depth and stencil buffer
557 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
558 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
559 GL_RENDERBUFFER_WIDTH, &w);
560 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
561 GL_RENDERBUFFER_HEIGHT, &h);
562 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
563 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
565 glGenRenderbuffers(1, &rbs[*numRbs]);
566 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
567 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
568 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
569 GL_RENDERBUFFER, rbs[*numRbs]);
570 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
571 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
572 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
573 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
574 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
578 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
579 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
580 GL_RENDERBUFFER_WIDTH, &w);
581 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
582 GL_RENDERBUFFER_HEIGHT, &h);
583 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
584 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
586 glGenRenderbuffers(1, &rbs[*numRbs]);
587 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
588 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
589 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
591 GL_RENDERBUFFER, rbs[*numRbs]);
592 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
593 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
594 glDrawBuffer(GL_DEPTH_ATTACHMENT);
595 glReadBuffer(GL_DEPTH_ATTACHMENT);
596 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
597 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
601 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
602 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
603 GL_RENDERBUFFER_WIDTH, &w);
604 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
605 GL_RENDERBUFFER_HEIGHT, &h);
606 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
607 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
609 glGenRenderbuffers(1, &rbs[*numRbs]);
610 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
611 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
612 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
613 GL_STENCIL_ATTACHMENT,
614 GL_RENDERBUFFER, rbs[*numRbs]);
615 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
616 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
617 glDrawBuffer(GL_STENCIL_ATTACHMENT);
618 glReadBuffer(GL_STENCIL_ATTACHMENT);
619 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
620 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
630 dumpDrawBuffers(JSONWriter &json, GLboolean dumpDepth, GLboolean dumpStencil)
632 json.beginMember("GL_RGBA");
633 dumpDrawBufferImage(json, GL_RGBA);
637 json.beginMember("GL_DEPTH_COMPONENT");
638 dumpDrawBufferImage(json, GL_DEPTH_COMPONENT);
643 json.beginMember("GL_STENCIL_INDEX");
644 dumpDrawBufferImage(json, GL_STENCIL_INDEX);
651 dumpFramebuffer(JSONWriter &json)
653 json.beginMember("framebuffer");
656 GLint boundDrawFbo = 0, boundReadFbo = 0;
657 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
658 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
660 GLint depth_bits = 0;
661 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
662 GLint stencil_bits = 0;
663 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
664 dumpDrawBuffers(json, depth_bits, stencil_bits);
666 GLint colorRb, stencilRb, depthRb;
668 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
669 GLint drawbuffer = GL_NONE;
670 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
672 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
674 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
676 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
678 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
680 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
681 GL_STENCIL_ATTACHMENT,
682 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
685 GLint colorSamples, depthSamples, stencilSamples;
689 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
690 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
691 GL_RENDERBUFFER_SAMPLES, &colorSamples);
692 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
693 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
694 GL_RENDERBUFFER_SAMPLES, &depthSamples);
695 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
696 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
697 GL_RENDERBUFFER_SAMPLES, &stencilSamples);
698 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
700 if (colorSamples || depthSamples || stencilSamples) {
701 //glReadPixels doesnt support multisampled buffers so we need
702 // to blit the fbo to a temporary one
703 fboCopy = downsampledFramebuffer(boundDrawFbo, drawbuffer,
704 colorRb, depthRb, stencilRb,
707 glDrawBuffer(drawbuffer);
708 glReadBuffer(drawbuffer);
710 dumpDrawBuffers(json, depthRb, stencilRb);
713 glBindFramebuffer(GL_FRAMEBUFFER, boundDrawFbo);
714 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
715 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
716 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
717 glDeleteRenderbuffers(numRbs, rbs);
718 glDeleteFramebuffers(1, &fboCopy);
723 json.endMember(); // framebuffer
727 void dumpCurrentContext(std::ostream &os)
730 dumpParameters(json);
733 dumpFramebuffer(json);
737 } /* namespace glstate */