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"
42 dumpShader(JSONWriter &json, GLuint shader)
48 GLint shader_type = 0;
49 glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
54 GLint source_length = 0;
55 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
60 GLchar *source = new GLchar[source_length];
63 glGetShaderSource(shader, source_length, &length, source);
65 json.beginMember(enumToString(shader_type));
66 json.writeString(source);
74 dumpShaderObj(JSONWriter &json, GLhandleARB shaderObj)
80 GLint shader_type = 0;
81 glGetObjectParameterivARB(shaderObj, GL_OBJECT_TYPE_ARB, &shader_type);
86 GLint source_length = 0;
87 glGetObjectParameterivARB(shaderObj, GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &source_length);
92 GLcharARB *source = new GLcharARB[source_length];
95 glGetShaderSource(shaderObj, source_length, &length, source);
97 json.beginMember(enumToString(shader_type));
98 json.writeString(source);
106 dumpCurrentProgram(JSONWriter &json)
109 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
114 GLint attached_shaders = 0;
115 glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
116 if (!attached_shaders) {
120 GLuint *shaders = new GLuint[attached_shaders];
122 glGetAttachedShaders(program, attached_shaders, &count, shaders);
123 for (GLsizei i = 0; i < count; ++ i) {
124 dumpShader(json, shaders[i]);
131 dumpCurrentProgramObj(JSONWriter &json)
133 GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
138 GLint attached_shaders = 0;
139 glGetProgramivARB(programObj, GL_OBJECT_ATTACHED_OBJECTS_ARB, &attached_shaders);
140 if (!attached_shaders) {
144 GLhandleARB *shaderObjs = new GLhandleARB[attached_shaders];
146 glGetAttachedObjectsARB(programObj, attached_shaders, &count, shaderObjs);
147 for (GLsizei i = 0; i < count; ++ i) {
148 dumpShaderObj(json, shaderObjs[i]);
150 delete [] shaderObjs;
155 dumpArbProgram(JSONWriter &json, GLenum target)
157 if (!glIsEnabled(target)) {
161 GLint program_length = 0;
162 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
163 if (!program_length) {
167 GLchar *source = new GLchar[program_length + 1];
169 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
170 source[program_length] = 0;
172 json.beginMember(enumToString(target));
173 json.writeString(source);
181 dumpShaders(JSONWriter &json)
183 json.beginMember("shaders");
185 dumpCurrentProgram(json);
186 dumpArbProgram(json, GL_FRAGMENT_PROGRAM_ARB);
187 dumpArbProgram(json, GL_VERTEX_PROGRAM_ARB);
189 json.endMember(); //shaders
194 dumpTextureImage(JSONWriter &json, GLenum target, GLint level)
196 GLint width, height = 1, depth = 1;
199 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
201 if (target != GL_TEXTURE_1D) {
203 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
204 if (target == GL_TEXTURE_3D) {
206 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
210 if (width <= 0 || height <= 0 || depth <= 0) {
215 GLint active_texture = GL_TEXTURE0;
216 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
217 snprintf(label, sizeof label, "%s, %s, level = %i", enumToString(active_texture), enumToString(target), level);
219 json.beginMember(label);
223 // Tell the GUI this is no ordinary object, but an image
224 json.writeStringMember("__class__", "image");
226 json.writeNumberMember("__width__", width);
227 json.writeNumberMember("__height__", height);
228 json.writeNumberMember("__depth__", depth);
230 // Hardcoded for now, but we could chose types more adequate to the
231 // texture internal format
232 json.writeStringMember("__type__", "uint8");
233 json.writeBoolMember("__normalized__", true);
234 json.writeNumberMember("__channels__", 4);
236 GLubyte *pixels = new GLubyte[depth*width*height*4];
238 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
240 json.beginMember("__data__");
243 Image::writePixelsToBuffer(pixels, width, height, 4, false, &pngBuffer, &pngBufferSize);
244 json.writeBase64(pngBuffer, pngBufferSize);
246 json.endMember(); // __data__
255 dumpTexture(JSONWriter &json, GLenum target, GLenum binding)
257 GLint texture_binding = 0;
258 glGetIntegerv(binding, &texture_binding);
259 if (!glIsEnabled(target) && !texture_binding) {
265 GLint width = 0, height = 0;
266 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
267 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
268 if (!width || !height) {
272 if (target == GL_TEXTURE_CUBE_MAP) {
273 for (int face = 0; face < 6; ++face) {
274 dumpTextureImage(json, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
277 dumpTextureImage(json, target, level);
286 dumpTextures(JSONWriter &json)
288 json.beginMember("textures");
290 GLint active_texture = GL_TEXTURE0;
291 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
292 GLint max_texture_coords = 0;
293 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);
294 GLint max_combined_texture_image_units = 0;
295 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);
296 GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);
297 for (GLint unit = 0; unit < max_units; ++unit) {
298 GLenum texture = GL_TEXTURE0 + unit;
299 glActiveTexture(texture);
300 dumpTexture(json, GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D);
301 dumpTexture(json, GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D);
302 dumpTexture(json, GL_TEXTURE_3D, GL_TEXTURE_BINDING_3D);
303 dumpTexture(json, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BINDING_RECTANGLE);
304 dumpTexture(json, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP);
306 glActiveTexture(active_texture);
308 json.endMember(); // textures
313 getDrawableBounds(GLint *width, GLint *height) {
316 HDC hDC = wglGetCurrentDC();
321 HWND hWnd = WindowFromDC(hDC);
324 if (!GetClientRect(hWnd, &rect)) {
328 *width = rect.right - rect.left;
329 *height = rect.bottom - rect.top;
331 #elif 0 /* __APPLE__ */
333 CGLError CGLGetSurface(CGLContextObj, CGSConnectionID*, CGSWindowID*, CGSSurfaceID*);
334 CGError CGSGetWindowBounds(CGSConnectionID, CGWindowID, CGRect *ret);
342 unsigned int w, h, bw, depth;
344 display = glXGetCurrentDisplay();
349 drawable = glXGetCurrentDrawable();
350 if (drawable == None) {
354 if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) {
368 dumpDrawBufferImage(JSONWriter &json, GLenum format)
370 GLint channels = __gl_format_channels(format);
374 if (!getDrawableBounds(&width, &height)) {
379 // Tell the GUI this is no ordinary object, but an image
380 json.writeStringMember("__class__", "image");
382 json.writeNumberMember("__width__", width);
383 json.writeNumberMember("__height__", height);
384 json.writeNumberMember("__depth__", 1);
386 // Hardcoded for now, but we could chose types more adequate to the
387 // texture internal format
388 json.writeStringMember("__type__", "uint8");
389 json.writeBoolMember("__normalized__", true);
390 json.writeNumberMember("__channels__", channels);
392 GLubyte *pixels = new GLubyte[width*height*channels];
394 GLint drawbuffer = GL_NONE;
395 GLint readbuffer = GL_NONE;
396 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
397 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
398 glReadBuffer(drawbuffer);
400 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
401 glPixelStorei(GL_PACK_ALIGNMENT, 1);
403 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
406 glReadBuffer(readbuffer);
408 json.beginMember("__data__");
411 Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
412 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
413 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
414 json.writeBase64(pngBuffer, pngBufferSize);
416 json.endMember(); // __data__
425 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
426 GLint colorRb, GLint depthRb, GLint stencilRb,
427 GLuint *rbs, GLint *numRbs)
435 glGenFramebuffers(1, &fbo);
436 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
438 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
439 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
440 GL_RENDERBUFFER_WIDTH, &w);
441 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
442 GL_RENDERBUFFER_HEIGHT, &h);
443 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
444 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
446 glGenRenderbuffers(1, &rbs[*numRbs]);
447 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
448 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
449 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
450 GL_RENDERBUFFER, rbs[*numRbs]);
452 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
453 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
454 glDrawBuffer(drawbuffer);
455 glReadBuffer(drawbuffer);
456 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
457 GL_COLOR_BUFFER_BIT, GL_NEAREST);
458 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
461 if (stencilRb == depthRb && stencilRb) {
462 //combined depth and stencil buffer
463 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
464 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
465 GL_RENDERBUFFER_WIDTH, &w);
466 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
467 GL_RENDERBUFFER_HEIGHT, &h);
468 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
469 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
471 glGenRenderbuffers(1, &rbs[*numRbs]);
472 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
473 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
474 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
475 GL_RENDERBUFFER, rbs[*numRbs]);
476 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
477 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
478 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
479 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
480 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
484 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
485 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
486 GL_RENDERBUFFER_WIDTH, &w);
487 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
488 GL_RENDERBUFFER_HEIGHT, &h);
489 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
490 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
492 glGenRenderbuffers(1, &rbs[*numRbs]);
493 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
494 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
495 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
497 GL_RENDERBUFFER, rbs[*numRbs]);
498 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
499 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
500 glDrawBuffer(GL_DEPTH_ATTACHMENT);
501 glReadBuffer(GL_DEPTH_ATTACHMENT);
502 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
503 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
507 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
508 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
509 GL_RENDERBUFFER_WIDTH, &w);
510 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
511 GL_RENDERBUFFER_HEIGHT, &h);
512 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
513 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
515 glGenRenderbuffers(1, &rbs[*numRbs]);
516 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
517 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
518 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
519 GL_STENCIL_ATTACHMENT,
520 GL_RENDERBUFFER, rbs[*numRbs]);
521 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
522 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
523 glDrawBuffer(GL_STENCIL_ATTACHMENT);
524 glReadBuffer(GL_STENCIL_ATTACHMENT);
525 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
526 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
536 dumpDrawBuffers(JSONWriter &json, GLboolean dumpDepth, GLboolean dumpStencil)
538 json.beginMember("GL_RGBA");
539 dumpDrawBufferImage(json, GL_RGBA);
543 json.beginMember("GL_DEPTH_COMPONENT");
544 dumpDrawBufferImage(json, GL_DEPTH_COMPONENT);
549 json.beginMember("GL_STENCIL_INDEX");
550 dumpDrawBufferImage(json, GL_STENCIL_INDEX);
557 dumpFramebuffer(JSONWriter &json)
559 json.beginMember("framebuffer");
562 GLint boundDrawFbo = 0, boundReadFbo = 0;
563 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);
564 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);
566 GLint depth_bits = 0;
567 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
568 GLint stencil_bits = 0;
569 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
570 dumpDrawBuffers(json, depth_bits, stencil_bits);
572 GLint colorRb, stencilRb, depthRb;
574 glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);
575 GLint drawbuffer = GL_NONE;
576 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
578 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
580 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
582 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
584 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
586 glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
587 GL_STENCIL_ATTACHMENT,
588 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
591 GLint colorSamples, depthSamples, stencilSamples;
595 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
596 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
597 GL_RENDERBUFFER_SAMPLES, &colorSamples);
598 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
599 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
600 GL_RENDERBUFFER_SAMPLES, &depthSamples);
601 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
602 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
603 GL_RENDERBUFFER_SAMPLES, &stencilSamples);
604 glBindRenderbuffer(GL_RENDERBUFFER, boundRb);
606 if (colorSamples || depthSamples || stencilSamples) {
607 //glReadPixels doesnt support multisampled buffers so we need
608 // to blit the fbo to a temporary one
609 fboCopy = downsampledFramebuffer(boundDrawFbo, drawbuffer,
610 colorRb, depthRb, stencilRb,
613 glDrawBuffer(drawbuffer);
614 glReadBuffer(drawbuffer);
616 dumpDrawBuffers(json, depthRb, stencilRb);
619 glBindFramebuffer(GL_FRAMEBUFFER, boundDrawFbo);
620 glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);
621 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);
622 glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);
623 glDeleteRenderbuffers(numRbs, rbs);
624 glDeleteFramebuffers(1, &fboCopy);
629 json.endMember(); // framebuffer
633 void dumpCurrentContext(std::ostream &os)
636 dumpParameters(json);
639 dumpFramebuffer(json);
643 } /* namespace glstate */