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_2D', 'GL_TEXTURE_BINDING_2D'),
41 ('GL_TEXTURE_3D', 'GL_TEXTURE_BINDING_3D'),
42 ('GL_TEXTURE_RECTANGLE', 'GL_TEXTURE_BINDING_RECTANGLE'),
43 ('GL_TEXTURE_CUBE_MAP', 'GL_TEXTURE_BINDING_CUBE_MAP')
46 framebuffer_targets = [
47 ('GL_DRAW_FRAMEBUFFER', 'GL_DRAW_FRAMEBUFFER_BINDING'),
48 ('GL_READ_FRAMEBUFFER', 'GL_READ_FRAMEBUFFER_BINDING'),
52 '''Objects that describes how to inflect.'''
60 def __init__(self, radical, inflections, suffix = ''):
61 self.radical = radical
62 self.inflections = inflections
65 def reduced_type(self, type):
66 if type in self.inflections:
68 if type in self.reduced_types:
69 return self.reduced_type(self.reduced_types[type])
70 raise NotImplementedError
72 def inflect(self, type):
73 return self.radical + self.inflection(type) + self.suffix
75 def inflection(self, type):
76 type = self.reduced_type(type)
77 assert type in self.inflections
78 return self.inflections[type]
81 return self.radical + self.suffix
84 class StateGetter(Visitor):
85 '''Type visitor that is able to extract the state via one of the glGet*
88 It will declare any temporary variable
91 def __init__(self, radical, inflections, suffix=''):
92 self.inflector = GetInflector(radical, inflections)
96 for function, type, count, name in parameters:
97 inflection = self.inflector.radical + self.suffix
98 if inflection not in function.split(','):
102 yield type, count, name
104 def __call__(self, *args):
107 for type, count, name in self.iter():
110 type = Array(type, str(count))
112 return type, self.visit(type, args)
114 raise NotImplementedError
116 def temp_name(self, args):
117 '''Return the name of a temporary variable to hold the state.'''
120 return pname[3:].lower()
122 def visitConst(self, const, args):
123 return self.visit(const.type, args)
125 def visitScalar(self, type, args):
126 temp_name = self.temp_name(args)
127 elem_type = self.inflector.reduced_type(type)
128 inflection = self.inflector.inflect(type)
129 if inflection.endswith('v'):
130 print ' %s %s = 0;' % (elem_type, temp_name)
131 print ' %s(%s, &%s);' % (inflection + self.suffix, ', '.join(args), temp_name)
133 print ' %s %s = %s(%s);' % (elem_type, temp_name, inflection + self.suffix, ', '.join(args))
136 def visitString(self, string, args):
137 temp_name = self.temp_name(args)
138 inflection = self.inflector.inflect(string)
139 assert not inflection.endswith('v')
140 print ' %s %s = (%s)%s(%s);' % (string, temp_name, string, inflection + self.suffix, ', '.join(args))
143 def visitAlias(self, alias, args):
144 return self.visitScalar(alias, args)
146 def visitEnum(self, enum, args):
147 return self.visitScalar(enum, args)
149 def visitBitmask(self, bitmask, args):
150 return self.visit(GLint, args)
152 def visitArray(self, array, args):
153 temp_name = self.temp_name(args)
154 if array.length == '1':
155 return self.visit(array.type)
156 elem_type = self.inflector.reduced_type(array.type)
157 inflection = self.inflector.inflect(array.type)
158 assert inflection.endswith('v')
159 print ' %s %s[%s + 1];' % (elem_type, temp_name, array.length)
160 print ' memset(%s, 0, %s * sizeof *%s);' % (temp_name, array.length, temp_name)
161 print ' %s[%s] = (%s)0xdeadc0de;' % (temp_name, array.length, elem_type)
162 print ' %s(%s, %s);' % (inflection + self.suffix, ', '.join(args), temp_name)
163 # Simple buffer overflow detection
164 print ' assert(%s[%s] == (%s)0xdeadc0de);' % (temp_name, array.length, elem_type)
167 def visitOpaque(self, pointer, args):
168 temp_name = self.temp_name(args)
169 inflection = self.inflector.inflect(pointer)
170 assert inflection.endswith('v')
171 print ' GLvoid *%s;' % temp_name
172 print ' %s(%s, &%s);' % (inflection + self.suffix, ', '.join(args), temp_name)
176 glGet = StateGetter('glGet', {
185 glGetMaterial = StateGetter('glGetMaterial', {I: 'iv', F: 'fv'})
186 glGetLight = StateGetter('glGetLight', {I: 'iv', F: 'fv'})
187 glGetVertexAttrib = StateGetter('glGetVertexAttrib', {I: 'iv', F: 'fv', D: 'dv', P: 'Pointerv'})
188 glGetTexParameter = StateGetter('glGetTexParameter', {I: 'iv', F: 'fv'})
189 glGetTexEnv = StateGetter('glGetTexEnv', {I: 'iv', F: 'fv'})
190 glGetTexLevelParameter = StateGetter('glGetTexLevelParameter', {I: 'iv', F: 'fv'})
191 glGetShader = StateGetter('glGetShaderiv', {I: 'iv'})
192 glGetProgram = StateGetter('glGetProgram', {I: 'iv'})
193 glGetProgramARB = StateGetter('glGetProgram', {I: 'iv', F: 'fv', S: 'Stringv'}, 'ARB')
194 glGetFramebufferAttachmentParameter = StateGetter('glGetFramebufferAttachmentParameter', {I: 'iv'})
197 class JsonWriter(Visitor):
198 '''Type visitor that will dump a value of the specified type through the
201 It expects a previously declared JSONWriter instance named "json".'''
203 def visitLiteral(self, literal, instance):
204 if literal.kind == 'Bool':
205 print ' json.writeBool(%s);' % instance
206 elif literal.kind in ('SInt', 'Uint'):
207 print ' json.writeInt(%s);' % instance
208 elif literal.kind in ('Float', 'Double'):
209 print ' json.writeFloat(%s);' % instance
211 raise NotImplementedError
213 def visitString(self, string, instance):
214 assert string.length is None
215 print ' json.writeString((const char *)%s);' % instance
217 def visitEnum(self, enum, instance):
218 if enum is GLboolean:
219 print ' dumpBoolean(json, %s);' % instance
221 print ' dumpEnum(json, %s);' % instance
224 print ' json.writeInt(%s);' % instance
226 def visitBitmask(self, bitmask, instance):
227 raise NotImplementedError
229 def visitAlias(self, alias, instance):
230 self.visit(alias.type, instance)
232 def visitOpaque(self, opaque, instance):
233 print ' json.writeInt((size_t)%s);' % instance
237 def visitArray(self, array, instance):
238 index = '_i%u' % JsonWriter.__index
239 JsonWriter.__index += 1
240 print ' json.beginArray();'
241 print ' for (unsigned %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
242 self.visit(array.type, '%s[%s]' % (instance, index))
244 print ' json.endArray();'
249 '''Class to generate code to dump all GL state in JSON format via
256 print '#include <assert.h>'
257 print '#include <string.h>'
259 print '#include "json.hpp"'
260 print '#include "glproc.hpp"'
261 print '#include "glsize.hpp"'
262 print '#include "glstate.hpp"'
263 print '#include "glstate_internal.hpp"'
265 print 'namespace glstate {'
269 print 'dumpBoolean(JSONWriter &json, GLboolean value)'
271 print ' switch (value) {'
272 print ' case GL_FALSE:'
273 print ' json.writeString("GL_FALSE");'
275 print ' case GL_TRUE:'
276 print ' json.writeString("GL_TRUE");'
279 print ' json.writeInt(static_cast<GLint>(value));'
286 print 'enumToString(GLenum pname)'
288 print ' switch (pname) {'
289 for name in GLenum.values:
290 print ' case %s:' % name
291 print ' return "%s";' % name
293 print ' return NULL;'
299 print 'dumpEnum(JSONWriter &json, GLenum pname)'
301 print ' const char *s = enumToString(pname);'
303 print ' json.writeString(s);'
305 print ' json.writeInt(pname);'
311 print 'dumpFramebufferAttachementParameters(JSONWriter &json, GLenum target, GLenum attachment)'
313 self.dump_attachment_parameters('target', 'attachment')
317 print 'void dumpParameters(JSONWriter &json, Context &context)'
319 print ' json.beginMember("parameters");'
320 print ' json.beginObject();'
322 self.dump_atoms(glGet)
324 self.dump_material_params()
325 self.dump_light_params()
326 self.dump_vertex_attribs()
327 self.dump_program_params()
328 self.dump_texture_parameters()
329 self.dump_framebuffer_parameters()
331 print ' json.endObject();'
332 print ' json.endMember(); // parameters'
336 print '} /*namespace glstate */'
338 def dump_material_params(self):
339 print ' if (!context.ES) {'
340 for face in ['GL_FRONT', 'GL_BACK']:
341 print ' json.beginMember("%s");' % face
342 print ' json.beginObject();'
343 self.dump_atoms(glGetMaterial, face)
344 print ' json.endObject();'
348 def dump_light_params(self):
349 print ' GLint max_lights = 0;'
350 print ' _glGetIntegerv(GL_MAX_LIGHTS, &max_lights);'
351 print ' for (GLint index = 0; index < max_lights; ++index) {'
352 print ' GLenum light = GL_LIGHT0 + index;'
353 print ' if (glIsEnabled(light)) {'
354 print ' char name[32];'
355 print ' snprintf(name, sizeof name, "GL_LIGHT%i", index);'
356 print ' json.beginMember(name);'
357 print ' json.beginObject();'
358 self.dump_atoms(glGetLight, ' GL_LIGHT0 + index')
359 print ' json.endObject();'
360 print ' json.endMember(); // GL_LIGHTi'
365 def texenv_param_target(self, name):
366 if name == 'GL_TEXTURE_LOD_BIAS':
367 return 'GL_TEXTURE_FILTER_CONTROL'
368 elif name == 'GL_COORD_REPLACE':
369 return 'GL_POINT_SPRITE'
371 return 'GL_TEXTURE_ENV'
373 def dump_texenv_params(self):
374 for target in ['GL_TEXTURE_ENV', 'GL_TEXTURE_FILTER_CONTROL', 'GL_POINT_SPRITE']:
375 print ' if (!context.ES) {'
376 print ' json.beginMember("%s");' % target
377 print ' json.beginObject();'
378 for _, _, name in glGetTexEnv.iter():
379 if self.texenv_param_target(name) == target:
380 self.dump_atom(glGetTexEnv, target, name)
381 print ' json.endObject();'
384 def dump_vertex_attribs(self):
385 print ' GLint max_vertex_attribs = 0;'
386 print ' _glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_vertex_attribs);'
387 print ' for (GLint index = 0; index < max_vertex_attribs; ++index) {'
388 print ' char name[32];'
389 print ' snprintf(name, sizeof name, "GL_VERTEX_ATTRIB_ARRAY%i", index);'
390 print ' json.beginMember(name);'
391 print ' json.beginObject();'
392 self.dump_atoms(glGetVertexAttrib, 'index')
393 print ' json.endObject();'
394 print ' json.endMember(); // GL_VERTEX_ATTRIB_ARRAYi'
399 'GL_FRAGMENT_PROGRAM_ARB',
400 'GL_VERTEX_PROGRAM_ARB',
403 def dump_program_params(self):
404 for target in self.program_targets:
405 print ' if (glIsEnabled(%s)) {' % target
406 print ' json.beginMember("%s");' % target
407 print ' json.beginObject();'
408 self.dump_atoms(glGetProgramARB, target)
409 print ' json.endObject();'
412 def dump_texture_parameters(self):
414 print ' GLint active_texture = GL_TEXTURE0;'
415 print ' glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);'
416 print ' GLint max_texture_coords = 0;'
417 print ' glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);'
418 print ' GLint max_combined_texture_image_units = 0;'
419 print ' glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);'
420 print ' GLint max_units = std::max(std::max(max_combined_texture_image_units, max_texture_coords), 2);'
421 print ' for (GLint unit = 0; unit < max_units; ++unit) {'
422 print ' char name[32];'
423 print ' snprintf(name, sizeof name, "GL_TEXTURE%i", unit);'
424 print ' json.beginMember(name);'
425 print ' glActiveTexture(GL_TEXTURE0 + unit);'
426 print ' json.beginObject();'
427 print ' GLboolean enabled;'
428 print ' GLint binding;'
430 for target, binding in texture_targets:
431 print ' // %s' % target
432 print ' enabled = GL_FALSE;'
433 print ' glGetBooleanv(%s, &enabled);' % target
434 print ' json.beginMember("%s");' % target
435 print ' dumpBoolean(json, enabled);'
436 print ' json.endMember();'
437 print ' binding = 0;'
438 print ' glGetIntegerv(%s, &binding);' % binding
439 print ' json.writeIntMember("%s", binding);' % binding
440 print ' if (enabled || binding) {'
441 print ' json.beginMember("%s");' % target
442 print ' json.beginObject();'
443 self.dump_atoms(glGetTexParameter, target)
444 print ' if (!context.ES) {'
445 # We only dump the first level parameters
446 self.dump_atoms(glGetTexLevelParameter, target, "0")
448 print ' json.endObject();'
449 print ' json.endMember(); // %s' % target
452 print ' if (unit < max_texture_coords) {'
453 self.dump_texenv_params()
455 print ' json.endObject();'
456 print ' json.endMember(); // GL_TEXTUREi'
458 print ' glActiveTexture(active_texture);'
462 def dump_framebuffer_parameters(self):
464 print ' GLint max_color_attachments = 0;'
465 print ' glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);'
466 print ' GLint framebuffer;'
467 for target, binding in framebuffer_targets:
468 print ' // %s' % target
469 print ' framebuffer = 0;'
470 print ' glGetIntegerv(%s, &framebuffer);' % binding
471 print ' if (framebuffer) {'
472 print ' json.beginMember("%s");' % target
473 print ' json.beginObject();'
474 print ' for (GLint i = 0; i < max_color_attachments; ++i) {'
475 print ' GLint color_attachment = GL_COLOR_ATTACHMENT0 + i;'
476 print ' dumpFramebufferAttachementParameters(json, %s, color_attachment);' % target
478 print ' dumpFramebufferAttachementParameters(json, %s, GL_DEPTH_ATTACHMENT);' % target
479 print ' dumpFramebufferAttachementParameters(json, %s, GL_STENCIL_ATTACHMENT);' % target
480 print ' json.endObject();'
481 print ' json.endMember(); // %s' % target
487 def dump_attachment_parameters(self, target, attachment):
489 print ' GLint object_type = GL_NONE;'
490 print ' glGetFramebufferAttachmentParameteriv(%s, %s, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);' % (target, attachment)
491 print ' if (object_type != GL_NONE) {'
492 print ' json.beginMember(enumToString(%s));' % attachment
493 print ' json.beginObject();'
494 self.dump_atoms(glGetFramebufferAttachmentParameter, target, attachment)
495 print ' json.endObject();'
496 print ' json.endMember(); // GL_x_ATTACHMENT'
500 def dump_atoms(self, getter, *args):
501 for _, _, name in getter.iter():
502 self.dump_atom(getter, *(args + (name,)))
504 def dump_atom(self, getter, *args):
507 # Avoid crash on MacOSX
508 # XXX: The right fix would be to look at the support extensions..
510 if name == 'GL_SAMPLER_BINDING' and platform.system() == 'Darwin':
513 print ' // %s' % name
515 #print ' assert(glGetError() == GL_NO_ERROR);'
516 type, value = getter(*args)
517 print ' if (glGetError() != GL_NO_ERROR) {'
518 #print ' std::cerr << "warning: %s(%s) failed\\n";' % (inflection, name)
519 print ' while (glGetError() != GL_NO_ERROR) {}'
521 print ' json.beginMember("%s");' % name
522 JsonWriter().visit(type, value)
523 print ' json.endMember();'
529 if __name__ == '__main__':