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 ##########################################################################/
27 '''Generate code to dump most GL state into JSON.'''
33 from glparams import *
37 ('GL_TEXTURE_1D', 'GL_TEXTURE_BINDING_1D'),
38 ('GL_TEXTURE_2D', 'GL_TEXTURE_BINDING_2D'),
39 ('GL_TEXTURE_3D', 'GL_TEXTURE_BINDING_3D'),
40 ('GL_TEXTURE_RECTANGLE', 'GL_TEXTURE_BINDING_RECTANGLE'),
41 ('GL_TEXTURE_CUBE_MAP', 'GL_TEXTURE_BINDING_CUBE_MAP')
46 '''Objects that describes how to inflect.'''
54 def __init__(self, radical, inflections, suffix = ''):
55 self.radical = radical
56 self.inflections = inflections
59 def reduced_type(self, type):
60 if type in self.inflections:
62 if type in self.reduced_types:
63 return self.reduced_type(self.reduced_types[type])
64 raise NotImplementedError
66 def inflect(self, type):
67 return self.radical + self.inflection(type) + self.suffix
69 def inflection(self, type):
70 type = self.reduced_type(type)
71 assert type in self.inflections
72 return self.inflections[type]
75 return self.radical + self.suffix
78 class StateGetter(Visitor):
79 '''Type visitor that is able to extract the state via one of the glGet*
82 It will declare any temporary variable
85 def __init__(self, radical, inflections, suffix=''):
86 self.inflector = GetInflector(radical, inflections)
89 def __call__(self, *args):
92 for function, type, count, name in parameters:
97 type = Array(type, str(count))
99 return type, self.visit(type, args)
101 raise NotImplementedError
103 def temp_name(self, args):
104 '''Return the name of a temporary variable to hold the state.'''
107 return pname[3:].lower()
109 def visit_const(self, const, args):
110 return self.visit(const.type, args)
112 def visit_scalar(self, type, args):
113 temp_name = self.temp_name(args)
114 elem_type = self.inflector.reduced_type(type)
115 inflection = self.inflector.inflect(type)
116 if inflection.endswith('v'):
117 print ' %s %s = 0;' % (elem_type, temp_name)
118 print ' %s(%s, &%s);' % (inflection + self.suffix, ', '.join(args), temp_name)
120 print ' %s %s = %s(%s);' % (elem_type, temp_name, inflection + self.suffix, ', '.join(args))
123 def visit_string(self, string, args):
124 temp_name = self.temp_name(args)
125 inflection = self.inflector.inflect(string)
126 assert not inflection.endswith('v')
127 print ' %s %s = (%s)%s(%s);' % (string, temp_name, string, inflection + self.suffix, ', '.join(args))
130 def visit_alias(self, alias, args):
131 return self.visit_scalar(alias, args)
133 def visit_enum(self, enum, args):
134 return self.visit(GLint, args)
136 def visit_bitmask(self, bitmask, args):
137 return self.visit(GLint, args)
139 def visit_array(self, array, args):
140 temp_name = self.temp_name(args)
141 if array.length == '1':
142 return self.visit(array.type)
143 elem_type = self.inflector.reduced_type(array.type)
144 inflection = self.inflector.inflect(array.type)
145 assert inflection.endswith('v')
146 print ' %s %s[%s];' % (elem_type, temp_name, array.length)
147 print ' memset(%s, 0, %s * sizeof *%s);' % (temp_name, array.length, temp_name)
148 print ' %s(%s, %s);' % (inflection + self.suffix, ', '.join(args), temp_name)
151 def visit_opaque(self, pointer, args):
152 temp_name = self.temp_name(args)
153 inflection = self.inflector.inflect(pointer)
154 assert inflection.endswith('v')
155 print ' GLvoid *%s;' % temp_name
156 print ' %s(%s, &%s);' % (inflection + self.suffix, ', '.join(args), temp_name)
160 glGet = StateGetter('glGet', {
169 glGetMaterial = StateGetter('glGetMaterial', {I: 'iv', F: 'fv'})
170 glGetLight = StateGetter('glGetLight', {I: 'iv', F: 'fv'})
171 glGetVertexAttrib = StateGetter('glGetVertexAttrib', {I: 'iv', F: 'fv', D: 'dv', P: 'Pointerv'})
172 glGetTexParameter = StateGetter('glGetTexParameter', {I: 'iv', F: 'fv'})
173 glGetTexEnv = StateGetter('glGetTexEnv', {I: 'iv', F: 'fv'})
174 glGetTexLevelParameter = StateGetter('glGetTexLevelParameter', {I: 'iv', F: 'fv'})
175 glGetProgramARB = StateGetter('glGetProgram', {I: 'iv', F: 'fv', S: 'Stringv'}, 'ARB')
178 class JsonWriter(Visitor):
179 '''Type visitor that will dump a value of the specified type through the
182 It expects a previously declared JSONWriter instance named "json".'''
184 def visit_literal(self, literal, instance):
185 if literal.format == 'Bool':
186 print ' json.writeBool(%s);' % instance
187 elif literal.format in ('SInt', 'Uint', 'Float', 'Double'):
188 print ' json.writeNumber(%s);' % instance
190 raise NotImplementedError
192 def visit_string(self, string, instance):
193 assert string.length is None
194 print ' json.writeString((const char *)%s);' % instance
196 def visit_enum(self, enum, instance):
197 if enum.expr == 'GLenum':
198 print ' writeEnum(json, %s);' % instance
200 print ' json.writeNumber(%s);' % instance
202 def visit_bitmask(self, bitmask, instance):
203 raise NotImplementedError
205 def visit_alias(self, alias, instance):
206 self.visit(alias.type, instance)
208 def visit_opaque(self, opaque, instance):
209 print ' json.writeNumber((size_t)%s);' % instance
213 def visit_array(self, array, instance):
214 index = '__i%u' % JsonWriter.__index
215 JsonWriter.__index += 1
216 print ' json.beginArray();'
217 print ' for (unsigned %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
218 self.visit(array.type, '%s[%s]' % (instance, index))
220 print ' json.endArray();'
225 '''Class to generate code to dump all GL state in JSON format via
232 print '#include <string.h>'
233 print '#include <iostream>'
234 print '#include <algorithm>'
236 print '#include "image.hpp"'
237 print '#include "json.hpp"'
238 print '#include "glimports.hpp"'
239 print '#include "glproc.hpp"'
240 print '#include "glsize.hpp"'
241 print '#include "glretrace.hpp"'
244 print 'static const char *'
245 print '_enum_string(GLenum pname)'
247 print ' switch(pname) {'
248 for name in GLenum.values:
249 print ' case %s:' % name
250 print ' return "%s";' % name
252 print ' return NULL;'
257 print 'static const char *'
258 print 'enum_string(GLenum pname)'
260 print ' const char *s = _enum_string(pname);'
264 print ' static char buf[16];'
265 print ' snprintf(buf, sizeof buf, "0x%04x", pname);'
271 print 'static inline void'
272 print 'writeEnum(JSONWriter &json, GLenum pname)'
274 print ' const char *s = _enum_string(pname);'
276 print ' json.writeString(s);'
278 print ' json.writeNumber(pname);'
286 writeShader(JSONWriter &json, GLuint shader)
292 GLint shader_type = 0;
293 glGetShaderiv(shader, GL_SHADER_TYPE, &shader_type);
298 GLint source_length = 0;
299 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &source_length);
300 if (!source_length) {
304 GLchar *source = new GLchar[source_length];
307 glGetShaderSource(shader, source_length, &length, source);
309 json.beginMember(enum_string(shader_type));
310 json.writeString(source);
317 writeCurrentProgram(JSONWriter &json)
320 glGetIntegerv(GL_CURRENT_PROGRAM, &program);
325 GLint attached_shaders = 0;
326 glGetProgramiv(program, GL_ATTACHED_SHADERS, &attached_shaders);
327 if (!attached_shaders) {
331 GLuint *shaders = new GLuint[attached_shaders];
333 glGetAttachedShaders(program, attached_shaders, &count, shaders);
334 for (GLsizei i = 0; i < count; ++ i) {
335 writeShader(json, shaders[i]);
341 writeArbProgram(JSONWriter &json, GLenum target)
343 if (!glIsEnabled(target)) {
347 GLint program_length = 0;
348 glGetProgramivARB(target, GL_PROGRAM_LENGTH_ARB, &program_length);
349 if (!program_length) {
353 GLchar *source = new GLchar[program_length + 1];
355 glGetProgramStringARB(target, GL_PROGRAM_STRING_ARB, source);
356 source[program_length] = 0;
358 json.beginMember(enum_string(target));
359 json.writeString(source);
369 writeTextureImage(JSONWriter &json, GLenum target, GLint level)
371 GLint width, height = 1, depth = 1;
374 glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
376 if (target != GL_TEXTURE_1D) {
378 glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
379 if (target == GL_TEXTURE_3D) {
381 glGetTexLevelParameteriv(target, level, GL_TEXTURE_DEPTH, &depth);
385 if (width <= 0 || height <= 0 || depth <= 0) {
390 // Tell the GUI this is no ordinary object, but an image
391 json.writeStringMember("__class__", "image");
393 json.writeNumberMember("__width__", width);
394 json.writeNumberMember("__height__", height);
395 json.writeNumberMember("__depth__", depth);
397 // Hardcoded for now, but we could chose types more adequate to the
398 // texture internal format
399 json.writeStringMember("__type__", "uint8");
400 json.writeBoolMember("__normalized__", true);
401 json.writeNumberMember("__channels__", 4);
403 GLubyte *pixels = new GLubyte[depth*width*height*4];
405 if (target == GL_TEXTURE_CUBE_MAP) {
406 // TODO: dump other faces too
407 target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
410 glGetTexImage(target, level, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
412 json.beginMember("__data__");
415 Image::writePixelsToBuffer(pixels, width, height, 4, false, &pngBuffer, &pngBufferSize);
416 json.writeBase64(pngBuffer, pngBufferSize);
418 json.endMember(); // __data__
426 writeDrawBufferImage(JSONWriter &json, GLenum format)
428 GLint width = glretrace::window_width;
429 GLint height = glretrace::window_height;
431 GLint channels = __gl_format_channels(format);
433 if (!width || !height) {
438 // Tell the GUI this is no ordinary object, but an image
439 json.writeStringMember("__class__", "image");
441 json.writeNumberMember("__width__", width);
442 json.writeNumberMember("__height__", height);
443 json.writeNumberMember("__depth__", 1);
445 // Hardcoded for now, but we could chose types more adequate to the
446 // texture internal format
447 json.writeStringMember("__type__", "uint8");
448 json.writeBoolMember("__normalized__", true);
449 json.writeNumberMember("__channels__", channels);
451 GLubyte *pixels = new GLubyte[width*height*channels];
453 GLint drawbuffer = glretrace::double_buffer ? GL_BACK : GL_FRONT;
454 GLint readbuffer = glretrace::double_buffer ? GL_BACK : GL_FRONT;
455 glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);
456 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
457 glReadBuffer(drawbuffer);
459 glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
460 glPixelStorei(GL_PACK_ALIGNMENT, 1);
462 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, pixels);
465 glReadBuffer(readbuffer);
467 json.beginMember("__data__");
470 Image::writePixelsToBuffer(pixels, width, height, channels, false, &pngBuffer, &pngBufferSize);
471 //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
472 // <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
473 json.writeBase64(pngBuffer, pngBufferSize);
475 json.endMember(); // __data__
483 downsampledFramebuffer(GLuint oldFbo, GLint drawbuffer,
484 GLint colorRb, GLint depthRb, GLint stencilRb,
485 GLuint *rbs, GLint *numRbs)
493 glGenFramebuffers(1, &fbo);
494 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
496 glBindRenderbuffer(GL_RENDERBUFFER, colorRb);
497 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
498 GL_RENDERBUFFER_WIDTH, &w);
499 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
500 GL_RENDERBUFFER_HEIGHT, &h);
501 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
502 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
504 glGenRenderbuffers(1, &rbs[*numRbs]);
505 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
506 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
507 glFramebufferRenderbuffer(GL_FRAMEBUFFER, drawbuffer,
508 GL_RENDERBUFFER, rbs[*numRbs]);
510 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
511 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
512 glDrawBuffer(drawbuffer);
513 glReadBuffer(drawbuffer);
514 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
515 GL_COLOR_BUFFER_BIT, GL_NEAREST);
516 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
519 if (stencilRb == depthRb && stencilRb) {
520 //combined depth and stencil buffer
521 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
522 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
523 GL_RENDERBUFFER_WIDTH, &w);
524 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
525 GL_RENDERBUFFER_HEIGHT, &h);
526 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
527 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
529 glGenRenderbuffers(1, &rbs[*numRbs]);
530 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
531 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
532 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
533 GL_RENDERBUFFER, rbs[*numRbs]);
534 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
535 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
536 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
537 GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST);
538 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
542 glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
543 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
544 GL_RENDERBUFFER_WIDTH, &w);
545 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
546 GL_RENDERBUFFER_HEIGHT, &h);
547 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
548 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
550 glGenRenderbuffers(1, &rbs[*numRbs]);
551 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
552 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
553 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
555 GL_RENDERBUFFER, rbs[*numRbs]);
556 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
557 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
558 glDrawBuffer(GL_DEPTH_ATTACHMENT);
559 glReadBuffer(GL_DEPTH_ATTACHMENT);
560 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
561 GL_DEPTH_BUFFER_BIT, GL_NEAREST);
565 glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);
566 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
567 GL_RENDERBUFFER_WIDTH, &w);
568 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
569 GL_RENDERBUFFER_HEIGHT, &h);
570 glGetRenderbufferParameteriv(GL_RENDERBUFFER,
571 GL_RENDERBUFFER_INTERNAL_FORMAT, &format);
573 glGenRenderbuffers(1, &rbs[*numRbs]);
574 glBindRenderbuffer(GL_RENDERBUFFER, rbs[*numRbs]);
575 glRenderbufferStorage(GL_RENDERBUFFER, format, w, h);
576 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
577 GL_STENCIL_ATTACHMENT,
578 GL_RENDERBUFFER, rbs[*numRbs]);
579 glBindFramebuffer(GL_READ_FRAMEBUFFER, oldFbo);
580 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
581 glDrawBuffer(GL_STENCIL_ATTACHMENT);
582 glReadBuffer(GL_STENCIL_ATTACHMENT);
583 glBlitFramebuffer(0, 0, w, h, 0, 0, w, h,
584 GL_STENCIL_BUFFER_BIT, GL_NEAREST);
593 writeDrawBuffers(JSONWriter &json, GLboolean writeDepth, GLboolean writeStencil)
595 json.beginMember("GL_RGBA");
596 writeDrawBufferImage(json, GL_RGBA);
600 json.beginMember("GL_DEPTH_COMPONENT");
601 writeDrawBufferImage(json, GL_DEPTH_COMPONENT);
606 json.beginMember("GL_STENCIL_INDEX");
607 writeDrawBufferImage(json, GL_STENCIL_INDEX);
615 print 'static inline void'
616 print 'writeTexture(JSONWriter &json, GLenum target, GLenum binding)'
618 print ' GLint texture = 0;'
619 print ' glGetIntegerv(binding, &texture);'
620 print ' if (!glIsEnabled(target) && !texture) {'
621 print ' json.writeNull();'
625 print ' json.beginObject();'
626 print ' json.beginMember("levels");'
627 print ' json.beginArray();'
628 print ' GLint level = 0;'
630 print ' GLint width = 0, height = 0;'
631 print ' glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);'
632 print ' glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);'
633 print ' if (!width || !height) {'
636 print ' json.beginObject();'
638 # FIXME: This is not the original texture name in the trace -- we need
639 # to do a reverse lookup of the texture mappings to find the original one
640 print ' json.beginMember("binding");'
641 print ' json.writeNumber(texture);'
642 print ' json.endMember();'
644 print ' json.beginMember("image");'
645 print ' writeTextureImage(json, target, level);'
646 print ' json.endMember();'
648 print ' json.endObject();'
650 print ' } while(true);'
651 print ' json.endArray();'
652 print ' json.endMember(); // levels'
654 print ' json.endObject();'
658 print 'void glretrace::state_dump(std::ostream &os)'
660 print ' JSONWriter json(os);'
661 self.dump_parameters()
662 self.dump_current_program()
664 self.dump_framebuffer()
668 def dump_parameters(self):
669 print ' json.beginMember("parameters");'
670 print ' json.beginObject();'
672 self.dump_atoms(glGet)
674 self.dump_material_params()
675 self.dump_light_params()
676 self.dump_vertex_attribs()
677 self.dump_texenv_params()
678 self.dump_program_params()
679 self.dump_texture_parameters()
681 print ' json.endObject();'
682 print ' json.endMember(); // parameters'
685 def dump_material_params(self):
686 for face in ['GL_FRONT', 'GL_BACK']:
687 print ' json.beginMember("%s");' % face
688 print ' json.beginObject();'
689 self.dump_atoms(glGetMaterial, face)
690 print ' json.endObject();'
693 def dump_light_params(self):
694 print ' GLint max_lights = 0;'
695 print ' __glGetIntegerv(GL_MAX_LIGHTS, &max_lights);'
696 print ' for (GLint index = 0; index < max_lights; ++index) {'
697 print ' GLenum light = GL_LIGHT0 + index;'
698 print ' if (glIsEnabled(light)) {'
699 print ' char name[32];'
700 print ' snprintf(name, sizeof name, "GL_LIGHT%i", index);'
701 print ' json.beginMember(name);'
702 print ' json.beginObject();'
703 self.dump_atoms(glGetLight, ' GL_LIGHT0 + index')
704 print ' json.endObject();'
705 print ' json.endMember(); // GL_LIGHTi'
710 def dump_texenv_params(self):
711 for target in ['GL_TEXTURE_ENV', 'GL_TEXTURE_FILTER_CONTROL', 'GL_POINT_SPRITE']:
712 if target != 'GL_TEXTURE_FILTER_CONTROL':
713 print ' if (glIsEnabled(%s)) {' % target
716 print ' json.beginMember("%s");' % target
717 print ' json.beginObject();'
718 self.dump_atoms(glGetTexEnv, target)
719 print ' json.endObject();'
722 def dump_vertex_attribs(self):
723 print ' GLint max_vertex_attribs = 0;'
724 print ' __glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_vertex_attribs);'
725 print ' for (GLint index = 0; index < max_vertex_attribs; ++index) {'
726 print ' char name[32];'
727 print ' snprintf(name, sizeof name, "GL_VERTEX_ATTRIB_ARRAY%i", index);'
728 print ' json.beginMember(name);'
729 print ' json.beginObject();'
730 self.dump_atoms(glGetVertexAttrib, 'index')
731 print ' json.endObject();'
732 print ' json.endMember(); // GL_VERTEX_ATTRIB_ARRAYi'
737 'GL_FRAGMENT_PROGRAM_ARB',
738 'GL_VERTEX_PROGRAM_ARB',
741 def dump_program_params(self):
742 for target in self.program_targets:
743 print ' if (glIsEnabled(%s)) {' % target
744 print ' json.beginMember("%s");' % target
745 print ' json.beginObject();'
746 self.dump_atoms(glGetProgramARB, target)
747 print ' json.endObject();'
750 def dump_texture_parameters(self):
752 print ' GLint active_texture = GL_TEXTURE0;'
753 print ' glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);'
754 print ' GLint max_texture_coords = 0;'
755 print ' glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);'
756 print ' GLint max_combined_texture_image_units = 0;'
757 print ' glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);'
758 print ' GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);'
759 print ' for (GLint unit = 0; unit < max_units; ++unit) {'
760 print ' char name[32];'
761 print ' snprintf(name, sizeof name, "GL_TEXTURE%i", unit);'
762 print ' json.beginMember(name);'
763 print ' glActiveTexture(GL_TEXTURE0 + unit);'
764 print ' json.beginObject();'
765 print ' GLint texture;'
767 for target, binding in texture_targets:
768 print ' // %s' % target
769 print ' texture = 0;'
770 print ' glGetIntegerv(%s, &texture);' % binding
771 print ' if (glIsEnabled(%s) || texture) {' % target
772 print ' json.beginMember("%s");' % target
773 print ' json.beginObject();'
774 self.dump_atoms(glGetTexParameter, target)
775 # We only dump the first level parameters
776 self.dump_atoms(glGetTexLevelParameter, target, "0")
777 print ' json.endObject();'
778 print ' json.endMember(); // %s' % target
781 print ' json.endObject();'
782 print ' json.endMember(); // GL_TEXTUREi'
784 print ' glActiveTexture(active_texture);'
788 def dump_current_program(self):
789 print ' json.beginMember("shaders");'
790 print ' json.beginObject();'
791 print ' writeCurrentProgram(json);'
792 for target in self.program_targets:
793 print ' writeArbProgram(json, %s);' % target
794 print ' json.endObject();'
795 print ' json.endMember(); //shaders'
798 def dump_textures(self):
800 print ' json.beginMember("textures");'
801 print ' json.beginArray();'
802 print ' GLint active_texture = GL_TEXTURE0;'
803 print ' glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);'
804 print ' GLint max_texture_coords = 0;'
805 print ' glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);'
806 print ' GLint max_combined_texture_image_units = 0;'
807 print ' glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);'
808 print ' GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);'
809 print ' for (GLint unit = 0; unit < max_units; ++unit) {'
810 print ' glActiveTexture(GL_TEXTURE0 + unit);'
811 print ' json.beginObject();'
812 for target, binding in texture_targets:
813 print ' json.beginMember("%s");' % target
814 print ' writeTexture(json, %s, %s);' % (target, binding)
815 print ' json.endMember();'
816 print ' json.endObject();'
818 print ' glActiveTexture(active_texture);'
819 print ' json.endArray();'
820 print ' json.endMember(); // texture'
824 def dump_framebuffer(self):
825 print ' json.beginMember("framebuffer");'
826 print ' json.beginObject();'
827 # TODO: Handle real FBOs
829 print ' GLint boundDrawFbo = 0, boundReadFbo = 0;'
830 print ' glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundDrawFbo);'
831 print ' glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundReadFbo);'
832 print ' if (!boundDrawFbo) {'
833 print ' GLint depth_bits = 0;'
834 print ' glGetIntegerv(GL_DEPTH_BITS, &depth_bits);'
835 print ' GLint stencil_bits = 0;'
836 print ' glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);'
837 print ' writeDrawBuffers(json, depth_bits, stencil_bits);'
839 print ' GLint colorRb, stencilRb, depthRb;'
840 print ' GLint boundRb;'
841 print ' glGetIntegerv(GL_RENDERBUFFER_BINDING, &boundRb);'
842 print ' GLint drawbuffer = glretrace::double_buffer ? GL_BACK : GL_FRONT;'
843 print ' glGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);'
845 print ' glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,'
847 print ' GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,'
849 print ' glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,'
850 print ' GL_DEPTH_ATTACHMENT,'
851 print ' GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,'
853 print ' glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,'
854 print ' GL_STENCIL_ATTACHMENT,'
855 print ' GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,'
856 print ' &stencilRb);'
858 print ' GLint colorSamples, depthSamples, stencilSamples;'
859 print ' GLuint rbs[3];'
860 print ' GLint numRbs = 0;'
861 print ' GLuint fboCopy = 0;'
862 print ' glBindRenderbuffer(GL_RENDERBUFFER, colorRb);'
863 print ' glGetRenderbufferParameteriv(GL_RENDERBUFFER,'
864 print ' GL_RENDERBUFFER_SAMPLES, &colorSamples);'
865 print ' glBindRenderbuffer(GL_RENDERBUFFER, depthRb);'
866 print ' glGetRenderbufferParameteriv(GL_RENDERBUFFER,'
867 print ' GL_RENDERBUFFER_SAMPLES, &depthSamples);'
868 print ' glBindRenderbuffer(GL_RENDERBUFFER, stencilRb);'
869 print ' glGetRenderbufferParameteriv(GL_RENDERBUFFER,'
870 print ' GL_RENDERBUFFER_SAMPLES, &stencilSamples);'
871 print ' glBindRenderbuffer(GL_RENDERBUFFER, boundRb);'
873 print ' if (colorSamples || depthSamples || stencilSamples) {'
874 print ' //glReadPixels doesnt support multisampled buffers so we need'
875 print ' // to blit the fbo to a temporary one'
876 print ' fboCopy = downsampledFramebuffer(boundDrawFbo, drawbuffer,'
877 print ' colorRb, depthRb, stencilRb,'
878 print ' rbs, &numRbs);'
880 print ' glDrawBuffer(drawbuffer);'
881 print ' glReadBuffer(drawbuffer);'
883 print ' writeDrawBuffers(json, depthRb, stencilRb);'
885 print ' if (fboCopy) {'
886 print ' glBindFramebuffer(GL_FRAMEBUFFER, boundDrawFbo);'
887 print ' glBindFramebuffer(GL_READ_FRAMEBUFFER, boundReadFbo);'
888 print ' glBindFramebuffer(GL_DRAW_FRAMEBUFFER, boundDrawFbo);'
889 print ' glBindRenderbuffer(GL_RENDERBUFFER_BINDING, boundRb);'
890 print ' glDeleteRenderbuffers(numRbs, rbs);'
891 print ' glDeleteFramebuffers(1, &fboCopy);'
895 print ' json.endObject();'
896 print ' json.endMember(); // framebuffer'
899 def dump_atoms(self, getter, *args):
900 for function, type, count, name in parameters:
901 inflection = getter.inflector.radical + getter.suffix
902 if inflection not in function.split(','):
906 print ' // %s' % name
908 type, value = getter(*(args + (name,)))
909 print ' if (glGetError() != GL_NO_ERROR) {'
910 #print ' std::cerr << "warning: %s(%s) failed\\n";' % (inflection, name)
912 print ' json.beginMember("%s");' % name
913 JsonWriter().visit(type, value)
914 print ' json.endMember();'
920 if __name__ == '__main__':