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.'''
30 import retrace # to adjust sys.path
32 from specs.stdapi import *
34 from specs.gltypes import *
35 from specs.glparams import *
39 ('GL_TEXTURE_1D', 'GL_TEXTURE_BINDING_1D'),
40 ('GL_TEXTURE_1D_ARRAY', 'GL_TEXTURE_BINDING_1D_ARRAY'),
41 ('GL_TEXTURE_2D', 'GL_TEXTURE_BINDING_2D'),
42 ('GL_TEXTURE_2D_ARRAY', 'GL_TEXTURE_BINDING_2D_ARRAY'),
43 ('GL_TEXTURE_2D_MULTISAMPLE', 'GL_TEXTURE_BINDING_2D_MULTISAMPLE'),
44 ('GL_TEXTURE_2D_MULTISAMPLE_ARRAY', 'GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY'),
45 ('GL_TEXTURE_3D', 'GL_TEXTURE_BINDING_3D'),
46 ('GL_TEXTURE_RECTANGLE', 'GL_TEXTURE_BINDING_RECTANGLE'),
47 ('GL_TEXTURE_CUBE_MAP', 'GL_TEXTURE_BINDING_CUBE_MAP'),
48 ('GL_TEXTURE_CUBE_MAP_ARRAY', 'GL_TEXTURE_BINDING_CUBE_MAP_ARRAY'),
51 framebuffer_targets = [
52 ('GL_DRAW_FRAMEBUFFER', 'GL_DRAW_FRAMEBUFFER_BINDING'),
53 ('GL_READ_FRAMEBUFFER', 'GL_READ_FRAMEBUFFER_BINDING'),
57 '''Objects that describes how to inflect.'''
65 def __init__(self, radical, inflections, suffix = ''):
66 self.radical = radical
67 self.inflections = inflections
70 def reduced_type(self, type):
71 if type in self.inflections:
73 if type in self.reduced_types:
74 return self.reduced_type(self.reduced_types[type])
75 raise NotImplementedError
77 def inflect(self, type):
78 return self.radical + self.inflection(type) + self.suffix
80 def inflection(self, type):
81 type = self.reduced_type(type)
82 assert type in self.inflections
83 return self.inflections[type]
86 return self.radical + self.suffix
89 class StateGetter(Visitor):
90 '''Type visitor that is able to extract the state via one of the glGet*
93 It will declare any temporary variable
96 def __init__(self, radical, inflections, suffix=''):
97 self.inflector = GetInflector(radical, inflections)
101 for function, type, count, name in parameters:
102 inflection = self.inflector.radical + self.suffix
103 if inflection not in function.split(','):
107 yield type, count, name
109 def __call__(self, *args):
112 for type, count, name in self.iter():
115 type = Array(type, str(count))
117 return type, self.visit(type, args)
119 raise NotImplementedError
121 def temp_name(self, args):
122 '''Return the name of a temporary variable to hold the state.'''
125 return pname[3:].lower()
127 def visitConst(self, const, args):
128 return self.visit(const.type, args)
130 def visitScalar(self, type, args):
131 temp_name = self.temp_name(args)
132 elem_type = self.inflector.reduced_type(type)
133 inflection = self.inflector.inflect(type)
134 if inflection.endswith('v'):
135 print ' %s %s = 0;' % (elem_type, temp_name)
136 print ' %s(%s, &%s);' % (inflection + self.suffix, ', '.join(args), temp_name)
138 print ' %s %s = %s(%s);' % (elem_type, temp_name, inflection + self.suffix, ', '.join(args))
141 def visitString(self, string, args):
142 temp_name = self.temp_name(args)
143 inflection = self.inflector.inflect(string)
144 assert not inflection.endswith('v')
145 print ' %s %s = (%s)%s(%s);' % (string, temp_name, string, inflection + self.suffix, ', '.join(args))
148 def visitAlias(self, alias, args):
149 return self.visitScalar(alias, args)
151 def visitEnum(self, enum, args):
152 return self.visitScalar(enum, args)
154 def visitBitmask(self, bitmask, args):
155 return self.visit(GLint, args)
157 def visitArray(self, array, args):
158 temp_name = self.temp_name(args)
159 if array.length == '1':
160 return self.visit(array.type)
161 elem_type = self.inflector.reduced_type(array.type)
162 inflection = self.inflector.inflect(array.type)
163 assert inflection.endswith('v')
164 array_length = array.length
165 if array_length.isdigit():
166 # Static integer length
167 print ' %s %s[%s + 1];' % (elem_type, temp_name, array_length)
169 # Put the length in a variable to avoid recomputing it every time
170 print ' size_t _%s_length = %s;' % (temp_name, array_length)
171 array_length = '_%s_length' % temp_name
172 # Allocate a dynamic sized array
173 print ' %s *%s = _allocator.alloc<%s>(%s + 1);' % (elem_type, temp_name, elem_type, array_length)
174 print ' memset(%s, 0, %s * sizeof *%s);' % (temp_name, array_length, temp_name)
175 print ' %s[%s] = (%s)0xdeadc0de;' % (temp_name, array_length, elem_type)
176 print ' if (%s) {' % array_length
177 print ' %s(%s, %s);' % (inflection + self.suffix, ', '.join(args), temp_name)
179 # Simple buffer overflow detection
180 print ' assert(%s[%s] == (%s)0xdeadc0de);' % (temp_name, array_length, elem_type)
183 def visitOpaque(self, pointer, args):
184 temp_name = self.temp_name(args)
185 inflection = self.inflector.inflect(pointer)
186 assert inflection.endswith('v')
187 print ' GLvoid *%s;' % temp_name
188 print ' %s(%s, &%s);' % (inflection + self.suffix, ', '.join(args), temp_name)
192 glGet = StateGetter('glGet', {
201 glGetMaterial = StateGetter('glGetMaterial', {I: 'iv', F: 'fv'})
202 glGetLight = StateGetter('glGetLight', {I: 'iv', F: 'fv'})
203 glGetVertexAttrib = StateGetter('glGetVertexAttrib', {I: 'iv', F: 'fv', D: 'dv', P: 'Pointerv'})
204 glGetTexParameter = StateGetter('glGetTexParameter', {I: 'iv', F: 'fv'})
205 glGetTexEnv = StateGetter('glGetTexEnv', {I: 'iv', F: 'fv'})
206 glGetTexLevelParameter = StateGetter('glGetTexLevelParameter', {I: 'iv', F: 'fv'})
207 glGetShader = StateGetter('glGetShaderiv', {I: 'iv'})
208 glGetProgram = StateGetter('glGetProgram', {I: 'iv'})
209 glGetProgramARB = StateGetter('glGetProgram', {I: 'iv', F: 'fv', S: 'Stringv'}, 'ARB')
210 glGetFramebufferAttachmentParameter = StateGetter('glGetFramebufferAttachmentParameter', {I: 'iv'})
211 glGetSamplerParameter = StateGetter('glGetSamplerParameter', {I: 'iv', F: 'fv'})
214 class JsonWriter(Visitor):
215 '''Type visitor that will dump a value of the specified type through the
218 It expects a previously declared JSONWriter instance named "json".'''
220 def visitLiteral(self, literal, instance):
221 if literal.kind == 'Bool':
222 print ' json.writeBool(%s);' % instance
223 elif literal.kind in ('SInt', 'Uint'):
224 print ' json.writeInt(%s);' % instance
225 elif literal.kind in ('Float', 'Double'):
226 print ' json.writeFloat(%s);' % instance
228 raise NotImplementedError
230 def visitString(self, string, instance):
231 assert string.length is None
232 print ' json.writeString((const char *)%s);' % instance
234 def visitEnum(self, enum, instance):
235 if enum is GLboolean:
236 print ' dumpBoolean(json, %s);' % instance
238 print ' dumpEnum(json, %s);' % instance
241 print ' json.writeInt(%s);' % instance
243 def visitBitmask(self, bitmask, instance):
244 raise NotImplementedError
246 def visitAlias(self, alias, instance):
247 self.visit(alias.type, instance)
249 def visitOpaque(self, opaque, instance):
250 print ' json.writeInt((size_t)%s);' % instance
254 def visitArray(self, array, instance):
255 index = '_i%u' % JsonWriter.__index
256 JsonWriter.__index += 1
257 print ' json.beginArray();'
258 print ' for (unsigned %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
259 self.visit(array.type, '%s[%s]' % (instance, index))
261 print ' json.endArray();'
266 '''Class to generate code to dump all GL state in JSON format via
273 print '#include <assert.h>'
274 print '#include <string.h>'
276 print '#include "json.hpp"'
277 print '#include "scoped_allocator.hpp"'
278 print '#include "glproc.hpp"'
279 print '#include "glsize.hpp"'
280 print '#include "glstate.hpp"'
281 print '#include "glstate_internal.hpp"'
283 print 'namespace glstate {'
287 print 'flushErrors(void) {'
288 print ' while (glGetError() != GL_NO_ERROR) {}'
293 print 'dumpBoolean(JSONWriter &json, GLboolean value)'
295 print ' switch (value) {'
296 print ' case GL_FALSE:'
297 print ' json.writeString("GL_FALSE");'
299 print ' case GL_TRUE:'
300 print ' json.writeString("GL_TRUE");'
303 print ' json.writeInt(static_cast<GLint>(value));'
310 print 'enumToString(GLenum pname)'
312 print ' switch (pname) {'
313 for name in GLenum.values:
314 print ' case %s:' % name
315 print ' return "%s";' % name
317 print ' return NULL;'
323 print 'dumpEnum(JSONWriter &json, GLenum pname)'
325 print ' const char *s = enumToString(pname);'
327 print ' json.writeString(s);'
329 print ' json.writeInt(pname);'
335 print 'dumpTextureTargetParameters(JSONWriter &json, Context &context, GLenum target, GLenum binding_param)'
337 print ' GLboolean enabled = GL_FALSE;'
338 print ' GLint binding = 0;'
339 print ' glGetBooleanv(target, &enabled);'
340 print ' json.beginMember(enumToString(target));'
341 print ' dumpBoolean(json, enabled);'
342 print ' json.endMember();'
343 print ' glGetIntegerv(binding_param, &binding);'
344 print ' json.writeIntMember(enumToString(binding_param), binding);'
345 print ' if (enabled || binding) {'
346 print ' json.beginMember(enumToString(target));'
347 print ' json.beginObject();'
348 self.dump_atoms(glGetTexParameter, 'target')
349 print ' if (!context.ES) {'
350 print ' GLenum levelTarget;'
351 print ' if (target == GL_TEXTURE_CUBE_MAP ||'
352 print ' target == GL_TEXTURE_CUBE_MAP_ARRAY) {'
353 print ' // Must pick a face'
354 print ' levelTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X;'
356 print ' levelTarget = target;'
358 self.dump_atoms(glGetTexLevelParameter, 'levelTarget', '0')
360 print ' json.endObject();'
361 print ' json.endMember(); // target'
367 print 'dumpFramebufferAttachementParameters(JSONWriter &json, GLenum target, GLenum attachment)'
369 self.dump_attachment_parameters('target', 'attachment')
373 print 'void dumpParameters(JSONWriter &json, Context &context)'
375 print ' ScopedAllocator _allocator;'
376 print ' (void)_allocator;'
378 print ' json.beginMember("parameters");'
379 print ' json.beginObject();'
381 self.dump_atoms(glGet)
383 self.dump_material_params()
384 self.dump_light_params()
385 self.dump_vertex_attribs()
386 self.dump_program_params()
387 self.dump_texture_parameters()
388 self.dump_framebuffer_parameters()
390 print ' json.endObject();'
391 print ' json.endMember(); // parameters'
395 print '} /*namespace glstate */'
397 def dump_material_params(self):
398 print ' if (!context.ES) {'
399 for face in ['GL_FRONT', 'GL_BACK']:
400 print ' json.beginMember("%s");' % face
401 print ' json.beginObject();'
402 self.dump_atoms(glGetMaterial, face)
403 print ' json.endObject();'
407 def dump_light_params(self):
408 print ' GLint max_lights = 0;'
409 print ' _glGetIntegerv(GL_MAX_LIGHTS, &max_lights);'
410 print ' for (GLint index = 0; index < max_lights; ++index) {'
411 print ' GLenum light = GL_LIGHT0 + index;'
412 print ' if (glIsEnabled(light)) {'
413 print ' char name[32];'
414 print ' snprintf(name, sizeof name, "GL_LIGHT%i", index);'
415 print ' json.beginMember(name);'
416 print ' json.beginObject();'
417 self.dump_atoms(glGetLight, ' GL_LIGHT0 + index')
418 print ' json.endObject();'
419 print ' json.endMember(); // GL_LIGHTi'
424 def dump_sampler_params(self):
425 print ' // GL_SAMPLER_BINDING'
426 print ' if (context.ARB_sampler_objects) {'
427 print ' GLint sampler_binding = 0;'
428 print ' glGetIntegerv(GL_SAMPLER_BINDING, &sampler_binding);'
429 print ' json.beginMember("GL_SAMPLER_BINDING");'
430 print ' json.writeInt(sampler_binding);'
431 print ' json.endMember();'
432 print ' if (sampler_binding) {'
433 print ' json.beginMember("GL_SAMPLER");'
434 print ' json.beginObject();'
435 for _, _, name in glGetSamplerParameter.iter():
436 self.dump_atom(glGetSamplerParameter, 'sampler_binding', name)
437 print ' json.endObject();'
438 print ' json.endMember(); // GL_SAMPLER'
442 def texenv_param_target(self, name):
443 if name == 'GL_TEXTURE_LOD_BIAS':
444 return 'GL_TEXTURE_FILTER_CONTROL'
445 elif name == 'GL_COORD_REPLACE':
446 return 'GL_POINT_SPRITE'
448 return 'GL_TEXTURE_ENV'
450 def dump_texenv_params(self):
451 for target in ['GL_TEXTURE_ENV', 'GL_TEXTURE_FILTER_CONTROL', 'GL_POINT_SPRITE']:
452 print ' if (!context.ES) {'
453 print ' json.beginMember("%s");' % target
454 print ' json.beginObject();'
455 for _, _, name in glGetTexEnv.iter():
456 if self.texenv_param_target(name) == target:
457 self.dump_atom(glGetTexEnv, target, name)
458 print ' json.endObject();'
461 def dump_vertex_attribs(self):
462 print ' GLint max_vertex_attribs = 0;'
463 print ' _glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_vertex_attribs);'
464 print ' for (GLint index = 0; index < max_vertex_attribs; ++index) {'
465 print ' char name[32];'
466 print ' snprintf(name, sizeof name, "GL_VERTEX_ATTRIB_ARRAY%i", index);'
467 print ' json.beginMember(name);'
468 print ' json.beginObject();'
469 self.dump_atoms(glGetVertexAttrib, 'index')
470 print ' json.endObject();'
471 print ' json.endMember(); // GL_VERTEX_ATTRIB_ARRAYi'
476 'GL_FRAGMENT_PROGRAM_ARB',
477 'GL_VERTEX_PROGRAM_ARB',
480 def dump_program_params(self):
481 for target in self.program_targets:
482 print ' if (glIsEnabled(%s)) {' % target
483 print ' json.beginMember("%s");' % target
484 print ' json.beginObject();'
485 self.dump_atoms(glGetProgramARB, target)
486 print ' json.endObject();'
489 def dump_texture_parameters(self):
491 print ' GLint active_texture = GL_TEXTURE0;'
492 print ' glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);'
493 print ' GLint max_texture_coords = 0;'
494 print ' glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);'
495 print ' GLint max_combined_texture_image_units = 0;'
496 print ' glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);'
497 print ' GLint max_units = std::max(std::max(max_combined_texture_image_units, max_texture_coords), 2);'
498 print ' for (GLint unit = 0; unit < max_units; ++unit) {'
499 print ' char name[32];'
500 print ' snprintf(name, sizeof name, "GL_TEXTURE%i", unit);'
501 print ' json.beginMember(name);'
502 print ' glActiveTexture(GL_TEXTURE0 + unit);'
503 print ' json.beginObject();'
505 for target, binding in texture_targets:
506 print ' dumpTextureTargetParameters(json, context, %s, %s);' % (target, binding)
507 print ' if (unit < max_texture_coords) {'
508 self.dump_sampler_params()
509 self.dump_texenv_params()
511 print ' json.endObject();'
512 print ' json.endMember(); // GL_TEXTUREi'
514 print ' glActiveTexture(active_texture);'
518 def dump_framebuffer_parameters(self):
520 print ' GLint max_color_attachments = 0;'
521 print ' glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);'
522 print ' GLint framebuffer;'
523 for target, binding in framebuffer_targets:
524 print ' // %s' % target
525 print ' framebuffer = 0;'
526 print ' glGetIntegerv(%s, &framebuffer);' % binding
527 print ' if (framebuffer) {'
528 print ' json.beginMember("%s");' % target
529 print ' json.beginObject();'
530 print ' for (GLint i = 0; i < max_color_attachments; ++i) {'
531 print ' GLint color_attachment = GL_COLOR_ATTACHMENT0 + i;'
532 print ' dumpFramebufferAttachementParameters(json, %s, color_attachment);' % target
534 print ' dumpFramebufferAttachementParameters(json, %s, GL_DEPTH_ATTACHMENT);' % target
535 print ' dumpFramebufferAttachementParameters(json, %s, GL_STENCIL_ATTACHMENT);' % target
536 print ' json.endObject();'
537 print ' json.endMember(); // %s' % target
543 def dump_attachment_parameters(self, target, attachment):
545 print ' GLint object_type = GL_NONE;'
546 print ' glGetFramebufferAttachmentParameteriv(%s, %s, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);' % (target, attachment)
547 print ' if (object_type != GL_NONE) {'
548 print ' json.beginMember(enumToString(%s));' % attachment
549 print ' json.beginObject();'
550 self.dump_atoms(glGetFramebufferAttachmentParameter, target, attachment)
551 print ' json.endObject();'
552 print ' json.endMember(); // GL_x_ATTACHMENT'
556 def dump_atoms(self, getter, *args):
557 for _, _, name in getter.iter():
558 self.dump_atom(getter, *(args + (name,)))
560 def dump_atom(self, getter, *args):
563 print ' // %s' % name
565 print ' flushErrors();'
566 type, value = getter(*args)
567 print ' if (glGetError() != GL_NO_ERROR) {'
568 #print ' std::cerr << "warning: %s(%s) failed\\n";' % (inflection, name)
569 print ' flushErrors();'
571 print ' json.beginMember("%s");' % name
572 JsonWriter().visit(type, value)
573 print ' json.endMember();'
579 if __name__ == '__main__':