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 from specs.stdapi import *
32 from specs.gltypes import *
33 from specs.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')
44 framebuffer_targets = [
45 ('GL_DRAW_FRAMEBUFFER', 'GL_DRAW_FRAMEBUFFER_BINDING'),
46 ('GL_READ_FRAMEBUFFER', 'GL_READ_FRAMEBUFFER_BINDING'),
50 '''Objects that describes how to inflect.'''
58 def __init__(self, radical, inflections, suffix = ''):
59 self.radical = radical
60 self.inflections = inflections
63 def reduced_type(self, type):
64 if type in self.inflections:
66 if type in self.reduced_types:
67 return self.reduced_type(self.reduced_types[type])
68 raise NotImplementedError
70 def inflect(self, type):
71 return self.radical + self.inflection(type) + self.suffix
73 def inflection(self, type):
74 type = self.reduced_type(type)
75 assert type in self.inflections
76 return self.inflections[type]
79 return self.radical + self.suffix
82 class StateGetter(Visitor):
83 '''Type visitor that is able to extract the state via one of the glGet*
86 It will declare any temporary variable
89 def __init__(self, radical, inflections, suffix=''):
90 self.inflector = GetInflector(radical, inflections)
93 def __call__(self, *args):
96 for function, type, count, name in parameters:
101 type = Array(type, str(count))
103 return type, self.visit(type, args)
105 raise NotImplementedError
107 def temp_name(self, args):
108 '''Return the name of a temporary variable to hold the state.'''
111 return pname[3:].lower()
113 def visit_const(self, const, args):
114 return self.visit(const.type, args)
116 def visit_scalar(self, type, args):
117 temp_name = self.temp_name(args)
118 elem_type = self.inflector.reduced_type(type)
119 inflection = self.inflector.inflect(type)
120 if inflection.endswith('v'):
121 print ' %s %s = 0;' % (elem_type, temp_name)
122 print ' %s(%s, &%s);' % (inflection + self.suffix, ', '.join(args), temp_name)
124 print ' %s %s = %s(%s);' % (elem_type, temp_name, inflection + self.suffix, ', '.join(args))
127 def visit_string(self, string, args):
128 temp_name = self.temp_name(args)
129 inflection = self.inflector.inflect(string)
130 assert not inflection.endswith('v')
131 print ' %s %s = (%s)%s(%s);' % (string, temp_name, string, inflection + self.suffix, ', '.join(args))
134 def visit_alias(self, alias, args):
135 return self.visit_scalar(alias, args)
137 def visit_enum(self, enum, args):
138 return self.visit(GLint, args)
140 def visit_bitmask(self, bitmask, args):
141 return self.visit(GLint, args)
143 def visit_array(self, array, args):
144 temp_name = self.temp_name(args)
145 if array.length == '1':
146 return self.visit(array.type)
147 elem_type = self.inflector.reduced_type(array.type)
148 inflection = self.inflector.inflect(array.type)
149 assert inflection.endswith('v')
150 print ' %s %s[%s];' % (elem_type, temp_name, array.length)
151 print ' memset(%s, 0, %s * sizeof *%s);' % (temp_name, array.length, temp_name)
152 print ' %s(%s, %s);' % (inflection + self.suffix, ', '.join(args), temp_name)
155 def visit_opaque(self, pointer, args):
156 temp_name = self.temp_name(args)
157 inflection = self.inflector.inflect(pointer)
158 assert inflection.endswith('v')
159 print ' GLvoid *%s;' % temp_name
160 print ' %s(%s, &%s);' % (inflection + self.suffix, ', '.join(args), temp_name)
164 glGet = StateGetter('glGet', {
173 glGetMaterial = StateGetter('glGetMaterial', {I: 'iv', F: 'fv'})
174 glGetLight = StateGetter('glGetLight', {I: 'iv', F: 'fv'})
175 glGetVertexAttrib = StateGetter('glGetVertexAttrib', {I: 'iv', F: 'fv', D: 'dv', P: 'Pointerv'})
176 glGetTexParameter = StateGetter('glGetTexParameter', {I: 'iv', F: 'fv'})
177 glGetTexEnv = StateGetter('glGetTexEnv', {I: 'iv', F: 'fv'})
178 glGetTexLevelParameter = StateGetter('glGetTexLevelParameter', {I: 'iv', F: 'fv'})
179 glGetShader = StateGetter('glGetShaderiv', {I: 'iv'})
180 glGetProgram = StateGetter('glGetProgram', {I: 'iv'})
181 glGetProgramARB = StateGetter('glGetProgram', {I: 'iv', F: 'fv', S: 'Stringv'}, 'ARB')
182 glGetFramebufferAttachmentParameter = StateGetter('glGetFramebufferAttachmentParameter', {I: 'iv'})
185 class JsonWriter(Visitor):
186 '''Type visitor that will dump a value of the specified type through the
189 It expects a previously declared JSONWriter instance named "json".'''
191 def visit_literal(self, literal, instance):
192 if literal.kind == 'Bool':
193 print ' json.writeBool(%s);' % instance
194 elif literal.kind in ('SInt', 'Uint', 'Float', 'Double'):
195 print ' json.writeNumber(%s);' % instance
197 raise NotImplementedError
199 def visit_string(self, string, instance):
200 assert string.length is None
201 print ' json.writeString((const char *)%s);' % instance
203 def visit_enum(self, enum, instance):
204 if enum.expr == 'GLenum':
205 print ' dumpEnum(json, %s);' % instance
207 print ' json.writeNumber(%s);' % instance
209 def visit_bitmask(self, bitmask, instance):
210 raise NotImplementedError
212 def visit_alias(self, alias, instance):
213 self.visit(alias.type, instance)
215 def visit_opaque(self, opaque, instance):
216 print ' json.writeNumber((size_t)%s);' % instance
220 def visit_array(self, array, instance):
221 index = '__i%u' % JsonWriter.__index
222 JsonWriter.__index += 1
223 print ' json.beginArray();'
224 print ' for (unsigned %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
225 self.visit(array.type, '%s[%s]' % (instance, index))
227 print ' json.endArray();'
232 '''Class to generate code to dump all GL state in JSON format via
239 print '#include <string.h>'
241 print '#include "json.hpp"'
242 print '#include "glproc.hpp"'
243 print '#include "glsize.hpp"'
244 print '#include "glstate.hpp"'
246 print 'namespace glstate {'
250 print 'enumToString(GLenum pname)'
252 print ' switch (pname) {'
253 for name in GLenum.values:
254 print ' case %s:' % name
255 print ' return "%s";' % name
257 print ' return NULL;'
263 print 'dumpFramebufferAttachementParameters(JSONWriter &json, GLenum target, GLenum attachment)'
265 self.dump_attachment_parameters('target', 'attachment')
270 print 'dumpEnum(JSONWriter &json, GLenum pname)'
272 print ' const char *s = enumToString(pname);'
274 print ' json.writeString(s);'
276 print ' json.writeNumber(pname);'
281 print 'void dumpParameters(JSONWriter &json)'
283 print ' json.beginMember("parameters");'
284 print ' json.beginObject();'
286 self.dump_atoms(glGet)
288 self.dump_material_params()
289 self.dump_light_params()
290 self.dump_vertex_attribs()
291 self.dump_texenv_params()
292 self.dump_program_params()
293 self.dump_texture_parameters()
294 self.dump_framebuffer_parameters()
296 print ' json.endObject();'
297 print ' json.endMember(); // parameters'
301 print '} /*namespace glstate */'
303 def dump_material_params(self):
304 for face in ['GL_FRONT', 'GL_BACK']:
305 print ' json.beginMember("%s");' % face
306 print ' json.beginObject();'
307 self.dump_atoms(glGetMaterial, face)
308 print ' json.endObject();'
311 def dump_light_params(self):
312 print ' GLint max_lights = 0;'
313 print ' __glGetIntegerv(GL_MAX_LIGHTS, &max_lights);'
314 print ' for (GLint index = 0; index < max_lights; ++index) {'
315 print ' GLenum light = GL_LIGHT0 + index;'
316 print ' if (glIsEnabled(light)) {'
317 print ' char name[32];'
318 print ' snprintf(name, sizeof name, "GL_LIGHT%i", index);'
319 print ' json.beginMember(name);'
320 print ' json.beginObject();'
321 self.dump_atoms(glGetLight, ' GL_LIGHT0 + index')
322 print ' json.endObject();'
323 print ' json.endMember(); // GL_LIGHTi'
328 def dump_texenv_params(self):
329 for target in ['GL_TEXTURE_ENV', 'GL_TEXTURE_FILTER_CONTROL', 'GL_POINT_SPRITE']:
330 if target != 'GL_TEXTURE_FILTER_CONTROL':
331 print ' if (glIsEnabled(%s)) {' % target
334 print ' json.beginMember("%s");' % target
335 print ' json.beginObject();'
336 self.dump_atoms(glGetTexEnv, target)
337 print ' json.endObject();'
340 def dump_vertex_attribs(self):
341 print ' GLint max_vertex_attribs = 0;'
342 print ' __glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_vertex_attribs);'
343 print ' for (GLint index = 0; index < max_vertex_attribs; ++index) {'
344 print ' char name[32];'
345 print ' snprintf(name, sizeof name, "GL_VERTEX_ATTRIB_ARRAY%i", index);'
346 print ' json.beginMember(name);'
347 print ' json.beginObject();'
348 self.dump_atoms(glGetVertexAttrib, 'index')
349 print ' json.endObject();'
350 print ' json.endMember(); // GL_VERTEX_ATTRIB_ARRAYi'
355 'GL_FRAGMENT_PROGRAM_ARB',
356 'GL_VERTEX_PROGRAM_ARB',
359 def dump_program_params(self):
360 for target in self.program_targets:
361 print ' if (glIsEnabled(%s)) {' % target
362 print ' json.beginMember("%s");' % target
363 print ' json.beginObject();'
364 self.dump_atoms(glGetProgramARB, target)
365 print ' json.endObject();'
368 def dump_texture_parameters(self):
370 print ' GLint active_texture = GL_TEXTURE0;'
371 print ' glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);'
372 print ' GLint max_texture_coords = 0;'
373 print ' glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);'
374 print ' GLint max_combined_texture_image_units = 0;'
375 print ' glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);'
376 print ' GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);'
377 print ' for (GLint unit = 0; unit < max_units; ++unit) {'
378 print ' char name[32];'
379 print ' snprintf(name, sizeof name, "GL_TEXTURE%i", unit);'
380 print ' json.beginMember(name);'
381 print ' glActiveTexture(GL_TEXTURE0 + unit);'
382 print ' json.beginObject();'
383 print ' GLboolean enabled;'
384 print ' GLint binding;'
386 for target, binding in texture_targets:
387 print ' // %s' % target
388 print ' enabled = GL_FALSE;'
389 print ' glGetBooleanv(%s, &enabled);' % target
390 print ' json.writeBoolMember("%s", enabled);' % target
391 print ' binding = 0;'
392 print ' glGetIntegerv(%s, &binding);' % binding
393 print ' json.writeNumberMember("%s", binding);' % binding
394 print ' if (enabled || binding) {'
395 print ' json.beginMember("%s");' % target
396 print ' json.beginObject();'
397 self.dump_atoms(glGetTexParameter, target)
398 # We only dump the first level parameters
399 self.dump_atoms(glGetTexLevelParameter, target, "0")
400 print ' json.endObject();'
401 print ' json.endMember(); // %s' % target
404 print ' json.endObject();'
405 print ' json.endMember(); // GL_TEXTUREi'
407 print ' glActiveTexture(active_texture);'
411 def dump_framebuffer_parameters(self):
413 print ' GLint max_color_attachments = 0;'
414 print ' glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);'
415 print ' GLint framebuffer;'
416 for target, binding in framebuffer_targets:
417 print ' // %s' % target
418 print ' framebuffer = 0;'
419 print ' glGetIntegerv(%s, &framebuffer);' % binding
420 print ' if (framebuffer) {'
421 print ' json.beginMember("%s");' % target
422 print ' json.beginObject();'
423 print ' for (GLint i = 0; i < max_color_attachments; ++i) {'
424 print ' GLint color_attachment = GL_COLOR_ATTACHMENT0 + i;'
425 print ' dumpFramebufferAttachementParameters(json, %s, color_attachment);' % target
427 print ' dumpFramebufferAttachementParameters(json, %s, GL_DEPTH_ATTACHMENT);' % target
428 print ' dumpFramebufferAttachementParameters(json, %s, GL_STENCIL_ATTACHMENT);' % target
429 print ' json.endObject();'
430 print ' json.endMember(); // %s' % target
436 def dump_attachment_parameters(self, target, attachment):
438 print ' GLint object_type = GL_NONE;'
439 print ' glGetFramebufferAttachmentParameteriv(%s, %s, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);' % (target, attachment)
440 print ' if (object_type != GL_NONE) {'
441 print ' json.beginMember(enumToString(%s));' % attachment
442 print ' json.beginObject();'
443 self.dump_atoms(glGetFramebufferAttachmentParameter, target, attachment)
444 print ' json.endObject();'
445 print ' json.endMember(); // GL_x_ATTACHMENT'
449 def dump_atoms(self, getter, *args):
450 for function, type, count, name in parameters:
451 inflection = getter.inflector.radical + getter.suffix
452 if inflection not in function.split(','):
456 print ' // %s' % name
458 type, value = getter(*(args + (name,)))
459 print ' if (glGetError() != GL_NO_ERROR) {'
460 #print ' std::cerr << "warning: %s(%s) failed\\n";' % (inflection, name)
462 print ' json.beginMember("%s");' % name
463 JsonWriter().visit(type, value)
464 print ' json.endMember();'
470 if __name__ == '__main__':