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 typedef int CGSConnectionID;
47 typedef int CGSWindowID;
48 typedef int CGSSurfaceID;
50 CGLError CGLGetSurface(CGLContextObj, CGSConnectionID*, CGSWindowID*, CGSSurfaceID*);
51 OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *);
57 #endif /* __APPLE__ */
64 dumpShader(JSONWriter &json, GLuint shader)
70 GLint shader_type = 0;
71 glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
76 GLint source_length = 0;
77 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
82 GLchar *source = new GLchar[source_length];
85 glGetShaderSource(shader, source_length, &length, source);
87 json.beginMember(enumToString(shader_type));
88 json.writeString(source);
96 dumpShaderObj(JSONWriter &json, GLhandleARB shaderObj)
102 GLint shader_type = 0;
103 glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &shader_type);
108 GLint source_length = 0;
109 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
110 if (!source_length) {
114 GLcharARB *source = new GLcharARB[source_length];
117 glGetShaderSource(shaderObj, source_length, &length, source);
119 json.beginMember(enumToString(shader_type));
120 json.writeString(source);
128 dumpCurrentProgram(JSONWriter &json)
131 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
136 GLint attached_shaders = 0;
137 glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
138 if (!attached_shaders) {
142 GLuint *shaders = new GLuint[attached_shaders];
144 glGetAttachedShaders(program, attached_shaders, &count, shaders);
145 for (GLsizei i = 0; i < count; ++ i) {
146 dumpShader(json, shaders[i]);
153 dumpCurrentProgramObj(JSONWriter &json)
155 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
160 GLint attached_shaders = 0;
161 glGetProgramivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
162 if (!attached_shaders) {
166 GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
168 glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
169 for (GLsizei i = 0; i < count; ++ i) {
170 dumpShaderObj(json, shaderObjs[i]);
172 delete [] shaderObjs;
177 dumpArbProgram(JSONWriter &json, GLenum target)
179 if (!glIsEnabled(target)) {
183 GLint program_length = 0;
184 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
185 if (!program_length) {
189 GLchar *source = new GLchar[program_length + 1];
191 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
192 source[program_length] = 0;
194 json.beginMember(enumToString(target));
195 json.writeString(source);
203 dumpShaders(JSONWriter &json)
205 json.beginMember("shaders");
207 dumpCurrentProgram(json);
208 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
209 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
211 json.endMember(); //shaders
216 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
218 GLint width, height = 1, depth = 1;
221 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
223 if (target != GL_TEXTURE_1D) {
225 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
226 if (target == GL_TEXTURE_3D) {
228 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
232 if (width <= 0 || height <= 0 || depth <= 0) {
237 GLint active_texture = GL_TEXTURE0;
238 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
239 snprintf(label, sizeof label, "%s, %s, level = %i", enumToString(active_texture), enumToString(target), level);
241 json.beginMember(label);
245 // Tell the GUI this is no ordinary object, but an image
246 json.writeStringMember("__class__", "image");
248 json.writeNumberMember("__width__", width);
249 json.writeNumberMember("__height__", height);
250 json.writeNumberMember("__depth__", depth);
252 // Hardcoded for now, but we could chose types more adequate to the
253 // texture internal format
254 json.writeStringMember("__type__", "uint8");
255 json.writeBoolMember("__normalized__", true);
256 json.writeNumberMember("__channels__", 4);
258 GLubyte *pixels = new GLubyte[depth*width*height*4];
260 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
262 json.beginMember("__data__");
265 Image::writePixelsToBuffer(pixels, width, height, 4, false, &pngBuffer, &pngBufferSize);
266 json.writeBase64(pngBuffer, pngBufferSize);
268 json.endMember(); // __data__
277 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
279 GLint texture_binding = 0;
280 glGetIntegerv(binding, &texture_binding);
281 if (!glIsEnabled(target) && !texture_binding) {
287 GLint width = 0, height = 0;
288 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
289 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
290 if (!width || !height) {
294 if (target == GL_TEXTURE_CUBE_MAP) {
295 for (int face = 0; face < 6; ++face) {
296 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
299 dumpTextureImage(json, target, level);
308 dumpTextures(JSONWriter &json)
310 json.beginMember("textures");
312 GLint active_texture = GL_TEXTURE0;
313 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
314 GLint max_texture_coords = 0;
315 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
316 GLint max_combined_texture_image_units = 0;
317 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
318 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
319 for (GLint unit = 0; unit < max_units; ++unit) {
320 GLenum texture = GL_TEXTURE0 + unit;
321 glActiveTexture(texture);
322 dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
323 dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
324 dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
325 dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
326 dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
328 glActiveTexture(active_texture);
330 json.endMember(); // textures
335 getDrawableBounds(GLint *width, GLint *height) {
338 HDC hDC = wglGetCurrentDC();
343 HWND hWnd = WindowFromDC(hDC);
346 if (!GetClientRect(hWnd, &rect)) {
350 *width = rect.right - rect.left;
351 *height = rect.bottom - rect.top;
353 #elif defined(__APPLE__)
355 CGLContextObj ctx = CGLGetCurrentContext();
364 if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) {
370 if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) {
374 *width = rect.size.width;
375 *height = rect.size.height;
383 unsigned int w, h, bw, depth;
385 display = glXGetCurrentDisplay();
390 drawable = glXGetCurrentDrawable();
391 if (drawable == None) {
395 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
409 getDrawBufferImage(GLenum format) {
412 if (format != GL_RGBA) {
413 // FIXME: this function can only handle 4-channel images
417 if (!getDrawableBounds(&width, &height)) {
421 Image::Image *image = new Image::Image(width, height, true);
426 GLint drawbuffer = GL_NONE;
427 GLint readbuffer = GL_NONE;
428 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
429 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
430 glReadBuffer(drawbuffer);
432 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
433 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
434 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
435 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
436 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
437 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
438 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
439 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
440 glPixelStorei(GL_PACK_ALIGNMENT, 1);
442 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
446 glReadBuffer(readbuffer);
453 dumpDrawBufferImage(JSONWriter &json, GLenum format)
455 GLint channels = __gl_format_channels(format);
459 if (!getDrawableBounds(&width, &height)) {
464 // Tell the GUI this is no ordinary object, but an image
465 json.writeStringMember("__class__", "image");
467 json.writeNumberMember("__width__", width);
468 json.writeNumberMember("__height__", height);
469 json.writeNumberMember("__depth__", 1);
471 // Hardcoded for now, but we could chose types more adequate to the
472 // texture internal format
473 json.writeStringMember("__type__", "uint8");
474 json.writeBoolMember("__normalized__", true);
475 json.writeNumberMember("__channels__", channels);
477 GLubyte *pixels = new GLubyte[width*height*channels];
479 GLint drawbuffer = GL_NONE;
480 GLint readbuffer = GL_NONE;
481 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
482 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
483 glReadBuffer(drawbuffer);
485 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
486 glPixelStorei(GL_PACK_SWAP_BYTES, GL_FALSE);
487 glPixelStorei(GL_PACK_LSB_FIRST, GL_FALSE);
488 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
489 glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
490 glPixelStorei(GL_PACK_SKIP_ROWS, 0);
491 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
492 glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
493 glPixelStorei(GL_PACK_ALIGNMENT, 1);
495 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
498 glReadBuffer(readbuffer);
500 json.beginMember("__data__");
503 Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
504 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
505 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
506 json.writeBase64(pngBuffer, pngBufferSize);
508 json.endMember(); // __data__
517 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
518 GLint colorRb, GLint depthRb, GLint stencilRb,
519 GLuint *rbs, GLint *numRbs)
527 glGenFramebuffers(1, &fbo);
528 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
530 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
531 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
532 GL_RENDERBUFFER_WIDTH, &w);
533 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
534 GL_RENDERBUFFER_HEIGHT, &h);
535 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
536 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
538 glGenRenderbuffers(1, &rbs[*numRbs]);
539 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
540 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
541 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
542 GL_RENDERBUFFER, rbs[*numRbs]);
544 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
545 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
546 glDrawBuffer(drawbuffer);
547 glReadBuffer(drawbuffer);
548 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
549 GL_COLOR_BUFFER_BIT, GL_NEAREST);
550 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
553 if (stencilRb == depthRb && stencilRb) {
554 //combined depth and stencil buffer
555 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
556 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
557 GL_RENDERBUFFER_WIDTH, &w);
558 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
559 GL_RENDERBUFFER_HEIGHT, &h);
560 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
561 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
563 glGenRenderbuffers(1, &rbs[*numRbs]);
564 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
565 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
566 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
567 GL_RENDERBUFFER, rbs[*numRbs]);
568 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
569 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
570 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
571 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
572 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
576 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
577 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
578 GL_RENDERBUFFER_WIDTH, &w);
579 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
580 GL_RENDERBUFFER_HEIGHT, &h);
581 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
582 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
584 glGenRenderbuffers(1, &rbs[*numRbs]);
585 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
586 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
587 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
589 GL_RENDERBUFFER, rbs[*numRbs]);
590 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
591 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
592 glDrawBuffer(GL_DEPTH_ATTACHMENT);
593 glReadBuffer(GL_DEPTH_ATTACHMENT);
594 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
595 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
599 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
600 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
601 GL_RENDERBUFFER_WIDTH, &w);
602 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
603 GL_RENDERBUFFER_HEIGHT, &h);
604 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
605 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
607 glGenRenderbuffers(1, &rbs[*numRbs]);
608 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
609 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
610 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
611 GL_STENCIL_ATTACHMENT,
612 GL_RENDERBUFFER, rbs[*numRbs]);
613 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
614 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
615 glDrawBuffer(GL_STENCIL_ATTACHMENT);
616 glReadBuffer(GL_STENCIL_ATTACHMENT);
617 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
618 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
628 dumpDrawBuffers(JSONWriter &json, GLboolean dumpDepth, GLboolean dumpStencil)
630 json.beginMember("GL_RGBA");
631 dumpDrawBufferImage(json, GL_RGBA);
635 json.beginMember("GL_DEPTH_COMPONENT");
636 dumpDrawBufferImage(json, GL_DEPTH_COMPONENT);
641 json.beginMember("GL_STENCIL_INDEX");
642 dumpDrawBufferImage(json, GL_STENCIL_INDEX);
649 dumpFramebuffer(JSONWriter &json)
651 json.beginMember("framebuffer");
654 GLint boundDrawFbo = 0, boundReadFbo = 0;
655 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
656 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
658 GLint depth_bits = 0;
659 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
660 GLint stencil_bits = 0;
661 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
662 dumpDrawBuffers(json, depth_bits, stencil_bits);
664 GLint colorRb, stencilRb, depthRb;
666 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
667 GLint drawbuffer = GL_NONE;
668 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
670 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
672 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
674 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
676 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
678 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
679 GL_STENCIL_ATTACHMENT,
680 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
683 GLint colorSamples, depthSamples, stencilSamples;
687 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
688 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
689 GL_RENDERBUFFER_SAMPLES, &colorSamples);
690 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
691 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
692 GL_RENDERBUFFER_SAMPLES, &depthSamples);
693 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
694 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
695 GL_RENDERBUFFER_SAMPLES, &stencilSamples);
696 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
698 if (colorSamples || depthSamples || stencilSamples) {
699 //glReadPixels doesnt support multisampled buffers so we need
700 // to blit the fbo to a temporary one
701 fboCopy = downsampledFramebuffer(boundDrawFbo, drawbuffer,
702 colorRb, depthRb, stencilRb,
705 glDrawBuffer(drawbuffer);
706 glReadBuffer(drawbuffer);
708 dumpDrawBuffers(json, depthRb, stencilRb);
711 glBindFramebuffer(GL_FRAMEBUFFER, boundDrawFbo);
712 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
713 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
714 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
715 glDeleteRenderbuffers(numRbs, rbs);
716 glDeleteFramebuffers(1, &fboCopy);
721 json.endMember(); // framebuffer
725 void dumpCurrentContext(std::ostream &os)
728 dumpParameters(json);
731 dumpFramebuffer(json);
735 } /* namespace glstate */