]> git.cworth.org Git - apitrace/blob - glstate.py
Define all D2D/DWRITE GUIDs
[apitrace] / glstate.py
1 ##########################################################################
2 #
3 # Copyright 2011 Jose Fonseca
4 # All Rights Reserved.
5 #
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:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
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
22 # THE SOFTWARE.
23 #
24 ##########################################################################/
25
26
27 '''Generate code to dump most GL state into JSON.'''
28
29
30 from specs.stdapi import *
31
32 from specs.gltypes import *
33 from specs.glparams import *
34
35
36 texture_targets = [
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')
42 ]
43
44 framebuffer_targets = [
45     ('GL_DRAW_FRAMEBUFFER', 'GL_DRAW_FRAMEBUFFER_BINDING'),
46     ('GL_READ_FRAMEBUFFER', 'GL_READ_FRAMEBUFFER_BINDING'),
47 ]
48
49 class GetInflector:
50     '''Objects that describes how to inflect.'''
51
52     reduced_types = {
53         B: I,
54         E: I,
55         I: F,
56     }
57
58     def __init__(self, radical, inflections, suffix = ''):
59         self.radical = radical
60         self.inflections = inflections
61         self.suffix = suffix
62
63     def reduced_type(self, type):
64         if type in self.inflections:
65             return type
66         if type in self.reduced_types:
67             return self.reduced_type(self.reduced_types[type])
68         raise NotImplementedError
69
70     def inflect(self, type):
71         return self.radical + self.inflection(type) + self.suffix
72
73     def inflection(self, type):
74         type = self.reduced_type(type)
75         assert type in self.inflections
76         return self.inflections[type]
77
78     def __str__(self):
79         return self.radical + self.suffix
80
81
82 class StateGetter(Visitor):
83     '''Type visitor that is able to extract the state via one of the glGet*
84     functions.
85
86     It will declare any temporary variable
87     '''
88
89     def __init__(self, radical, inflections, suffix=''):
90         self.inflector = GetInflector(radical, inflections)
91         self.suffix = suffix
92
93     def iter(self):
94         for function, type, count, name in parameters:
95             inflection = self.inflector.radical + self.suffix
96             if inflection not in function.split(','):
97                 continue
98             if type is X:
99                 continue
100             yield type, count, name
101
102     def __call__(self, *args):
103         pname = args[-1]
104
105         for type, count, name in self.iter():
106             if name == pname:
107                 if count != 1:
108                     type = Array(type, str(count))
109
110                 return type, self.visit(type, args)
111
112         raise NotImplementedError
113
114     def temp_name(self, args):
115         '''Return the name of a temporary variable to hold the state.'''
116         pname = args[-1]
117
118         return pname[3:].lower()
119
120     def visitConst(self, const, args):
121         return self.visit(const.type, args)
122
123     def visitScalar(self, type, args):
124         temp_name = self.temp_name(args)
125         elem_type = self.inflector.reduced_type(type)
126         inflection = self.inflector.inflect(type)
127         if inflection.endswith('v'):
128             print '    %s %s = 0;' % (elem_type, temp_name)
129             print '    %s(%s, &%s);' % (inflection + self.suffix, ', '.join(args), temp_name)
130         else:
131             print '    %s %s = %s(%s);' % (elem_type, temp_name, inflection + self.suffix, ', '.join(args))
132         return temp_name
133
134     def visitString(self, string, args):
135         temp_name = self.temp_name(args)
136         inflection = self.inflector.inflect(string)
137         assert not inflection.endswith('v')
138         print '    %s %s = (%s)%s(%s);' % (string, temp_name, string, inflection + self.suffix, ', '.join(args))
139         return temp_name
140
141     def visitAlias(self, alias, args):
142         return self.visitScalar(alias, args)
143
144     def visitEnum(self, enum, args):
145         return self.visit(GLint, args)
146
147     def visitBitmask(self, bitmask, args):
148         return self.visit(GLint, args)
149
150     def visitArray(self, array, args):
151         temp_name = self.temp_name(args)
152         if array.length == '1':
153             return self.visit(array.type)
154         elem_type = self.inflector.reduced_type(array.type)
155         inflection = self.inflector.inflect(array.type)
156         assert inflection.endswith('v')
157         print '    %s %s[%s + 1];' % (elem_type, temp_name, array.length)
158         print '    memset(%s, 0, %s * sizeof *%s);' % (temp_name, array.length, temp_name)
159         print '    %s[%s] = (%s)0xdeadc0de;' % (temp_name, array.length, elem_type)
160         print '    %s(%s, %s);' % (inflection + self.suffix, ', '.join(args), temp_name)
161         # Simple buffer overflow detection
162         print '    assert(%s[%s] == (%s)0xdeadc0de);' % (temp_name, array.length, elem_type)
163         return temp_name
164
165     def visitOpaque(self, pointer, args):
166         temp_name = self.temp_name(args)
167         inflection = self.inflector.inflect(pointer)
168         assert inflection.endswith('v')
169         print '    GLvoid *%s;' % temp_name
170         print '    %s(%s, &%s);' % (inflection + self.suffix, ', '.join(args), temp_name)
171         return temp_name
172
173
174 glGet = StateGetter('glGet', {
175     B: 'Booleanv',
176     I: 'Integerv',
177     F: 'Floatv',
178     D: 'Doublev',
179     S: 'String',
180     P: 'Pointerv',
181 })
182
183 glGetMaterial = StateGetter('glGetMaterial', {I: 'iv', F: 'fv'})
184 glGetLight = StateGetter('glGetLight', {I: 'iv', F: 'fv'})
185 glGetVertexAttrib = StateGetter('glGetVertexAttrib', {I: 'iv', F: 'fv', D: 'dv', P: 'Pointerv'})
186 glGetTexParameter = StateGetter('glGetTexParameter', {I: 'iv', F: 'fv'})
187 glGetTexEnv = StateGetter('glGetTexEnv', {I: 'iv', F: 'fv'})
188 glGetTexLevelParameter = StateGetter('glGetTexLevelParameter', {I: 'iv', F: 'fv'})
189 glGetShader = StateGetter('glGetShaderiv', {I: 'iv'})
190 glGetProgram = StateGetter('glGetProgram', {I: 'iv'})
191 glGetProgramARB = StateGetter('glGetProgram', {I: 'iv', F: 'fv', S: 'Stringv'}, 'ARB')
192 glGetFramebufferAttachmentParameter = StateGetter('glGetFramebufferAttachmentParameter', {I: 'iv'})
193
194
195 class JsonWriter(Visitor):
196     '''Type visitor that will dump a value of the specified type through the
197     JSON writer.
198     
199     It expects a previously declared JSONWriter instance named "json".'''
200
201     def visitLiteral(self, literal, instance):
202         if literal.kind == 'Bool':
203             print '    json.writeBool(%s);' % instance
204         elif literal.kind in ('SInt', 'Uint', 'Float', 'Double'):
205             print '    json.writeNumber(%s);' % instance
206         else:
207             raise NotImplementedError
208
209     def visitString(self, string, instance):
210         assert string.length is None
211         print '    json.writeString((const char *)%s);' % instance
212
213     def visitEnum(self, enum, instance):
214         if enum.expr == 'GLenum':
215             print '    dumpEnum(json, %s);' % instance
216         else:
217             print '    json.writeNumber(%s);' % instance
218
219     def visitBitmask(self, bitmask, instance):
220         raise NotImplementedError
221
222     def visitAlias(self, alias, instance):
223         self.visit(alias.type, instance)
224
225     def visitOpaque(self, opaque, instance):
226         print '    json.writeNumber((size_t)%s);' % instance
227
228     __index = 0
229
230     def visitArray(self, array, instance):
231         index = '__i%u' % JsonWriter.__index
232         JsonWriter.__index += 1
233         print '    json.beginArray();'
234         print '    for (unsigned %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
235         self.visit(array.type, '%s[%s]' % (instance, index))
236         print '    }'
237         print '    json.endArray();'
238
239
240
241 class StateDumper:
242     '''Class to generate code to dump all GL state in JSON format via
243     stdout.'''
244
245     def __init__(self):
246         pass
247
248     def dump(self):
249         print '#include <string.h>'
250         print
251         print '#include "json.hpp"'
252         print '#include "glproc.hpp"'
253         print '#include "glsize.hpp"'
254         print '#include "glstate.hpp"'
255         print
256         print 'namespace glstate {'
257         print
258
259         print 'const char *'
260         print 'enumToString(GLenum pname)'
261         print '{'
262         print '    switch (pname) {'
263         for name in GLenum.values:
264             print '    case %s:' % name
265             print '        return "%s";' % name
266         print '    default:'
267         print '        return NULL;'
268         print '    }'
269         print '}'
270         print
271
272         print 'static void'
273         print 'dumpFramebufferAttachementParameters(JSONWriter &json, GLenum target, GLenum attachment)'
274         print '{'
275         self.dump_attachment_parameters('target', 'attachment')
276         print '}'
277         print
278
279         print 'void'
280         print 'dumpEnum(JSONWriter &json, GLenum pname)'
281         print '{'
282         print '    const char *s = enumToString(pname);'
283         print '    if (s) {'
284         print '        json.writeString(s);'
285         print '    } else {'
286         print '        json.writeNumber(pname);'
287         print '    }'
288         print '}'
289         print
290
291         print 'void dumpParameters(JSONWriter &json)'
292         print '{'
293         print '    json.beginMember("parameters");'
294         print '    json.beginObject();'
295         
296         self.dump_atoms(glGet)
297         
298         self.dump_material_params()
299         self.dump_light_params()
300         self.dump_vertex_attribs()
301         self.dump_program_params()
302         self.dump_texture_parameters()
303         self.dump_framebuffer_parameters()
304
305         print '    json.endObject();'
306         print '    json.endMember(); // parameters'
307         print '}'
308         print
309         
310         print '} /*namespace glstate */'
311
312     def dump_material_params(self):
313         for face in ['GL_FRONT', 'GL_BACK']:
314             print '    json.beginMember("%s");' % face
315             print '    json.beginObject();'
316             self.dump_atoms(glGetMaterial, face)
317             print '    json.endObject();'
318         print
319
320     def dump_light_params(self):
321         print '    GLint max_lights = 0;'
322         print '    __glGetIntegerv(GL_MAX_LIGHTS, &max_lights);'
323         print '    for (GLint index = 0; index < max_lights; ++index) {'
324         print '        GLenum light = GL_LIGHT0 + index;'
325         print '        if (glIsEnabled(light)) {'
326         print '            char name[32];'
327         print '            snprintf(name, sizeof name, "GL_LIGHT%i", index);'
328         print '            json.beginMember(name);'
329         print '            json.beginObject();'
330         self.dump_atoms(glGetLight, '    GL_LIGHT0 + index')
331         print '            json.endObject();'
332         print '            json.endMember(); // GL_LIGHTi'
333         print '        }'
334         print '    }'
335         print
336
337     def texenv_param_target(self, name):
338         if name == 'GL_TEXTURE_LOD_BIAS':
339            return 'GL_TEXTURE_FILTER_CONTROL'
340         elif name == 'GL_COORD_REPLACE':
341            return 'GL_POINT_SPRITE'
342         else:
343            return 'GL_TEXTURE_ENV'
344
345     def dump_texenv_params(self):
346         for target in ['GL_TEXTURE_ENV', 'GL_TEXTURE_FILTER_CONTROL', 'GL_POINT_SPRITE']:
347             print '    {'
348             print '        json.beginMember("%s");' % target
349             print '        json.beginObject();'
350             for _, _, name in glGetTexEnv.iter():
351                 if self.texenv_param_target(name) == target:
352                     self.dump_atom(glGetTexEnv, target, name) 
353             print '        json.endObject();'
354             print '    }'
355
356     def dump_vertex_attribs(self):
357         print '    GLint max_vertex_attribs = 0;'
358         print '    __glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_vertex_attribs);'
359         print '    for (GLint index = 0; index < max_vertex_attribs; ++index) {'
360         print '        char name[32];'
361         print '        snprintf(name, sizeof name, "GL_VERTEX_ATTRIB_ARRAY%i", index);'
362         print '        json.beginMember(name);'
363         print '        json.beginObject();'
364         self.dump_atoms(glGetVertexAttrib, 'index')
365         print '        json.endObject();'
366         print '        json.endMember(); // GL_VERTEX_ATTRIB_ARRAYi'
367         print '    }'
368         print
369
370     program_targets = [
371         'GL_FRAGMENT_PROGRAM_ARB',
372         'GL_VERTEX_PROGRAM_ARB',
373     ]
374
375     def dump_program_params(self):
376         for target in self.program_targets:
377             print '    if (glIsEnabled(%s)) {' % target
378             print '        json.beginMember("%s");' % target
379             print '        json.beginObject();'
380             self.dump_atoms(glGetProgramARB, target)
381             print '        json.endObject();'
382             print '    }'
383
384     def dump_texture_parameters(self):
385         print '    {'
386         print '        GLint active_texture = GL_TEXTURE0;'
387         print '        glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);'
388         print '        GLint max_texture_coords = 0;'
389         print '        glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);'
390         print '        GLint max_combined_texture_image_units = 0;'
391         print '        glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units);'
392         print '        GLint max_units = std::max(max_combined_texture_image_units, max_texture_coords);'
393         print '        for (GLint unit = 0; unit < max_units; ++unit) {'
394         print '            char name[32];'
395         print '            snprintf(name, sizeof name, "GL_TEXTURE%i", unit);'
396         print '            json.beginMember(name);'
397         print '            glActiveTexture(GL_TEXTURE0 + unit);'
398         print '            json.beginObject();'
399         print '            GLboolean enabled;'
400         print '            GLint binding;'
401         print
402         for target, binding in texture_targets:
403             print '            // %s' % target
404             print '            enabled = GL_FALSE;'
405             print '            glGetBooleanv(%s, &enabled);' % target
406             print '            json.writeBoolMember("%s", enabled);' % target
407             print '            binding = 0;'
408             print '            glGetIntegerv(%s, &binding);' % binding
409             print '            json.writeNumberMember("%s", binding);' % binding
410             print '            if (enabled || binding) {'
411             print '                json.beginMember("%s");' % target
412             print '                json.beginObject();'
413             self.dump_atoms(glGetTexParameter, target)
414             # We only dump the first level parameters
415             self.dump_atoms(glGetTexLevelParameter, target, "0")
416             print '                json.endObject();'
417             print '                json.endMember(); // %s' % target
418             print '            }'
419             print
420         print '            if (unit < max_texture_coords) {'
421         self.dump_texenv_params()
422         print '            }'
423         print '            json.endObject();'
424         print '            json.endMember(); // GL_TEXTUREi'
425         print '        }'
426         print '        glActiveTexture(active_texture);'
427         print '    }'
428         print
429
430     def dump_framebuffer_parameters(self):
431         print '    {'
432         print '        GLint max_color_attachments = 0;'
433         print '        glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &max_color_attachments);'
434         print '        GLint framebuffer;'
435         for target, binding in framebuffer_targets:
436             print '            // %s' % target
437             print '            framebuffer = 0;'
438             print '            glGetIntegerv(%s, &framebuffer);' % binding
439             print '            if (framebuffer) {'
440             print '                json.beginMember("%s");' % target
441             print '                json.beginObject();'
442             print '                for (GLint i = 0; i < max_color_attachments; ++i) {'
443             print '                    GLint color_attachment = GL_COLOR_ATTACHMENT0 + i;'
444             print '                    dumpFramebufferAttachementParameters(json, %s, color_attachment);' % target
445             print '                }'
446             print '                dumpFramebufferAttachementParameters(json, %s, GL_DEPTH_ATTACHMENT);' % target
447             print '                dumpFramebufferAttachementParameters(json, %s, GL_STENCIL_ATTACHMENT);' % target
448             print '                json.endObject();'
449             print '                json.endMember(); // %s' % target
450             print '            }'
451             print
452         print '    }'
453         print
454
455     def dump_attachment_parameters(self, target, attachment):
456         print '            {'
457         print '                GLint object_type = GL_NONE;'
458         print '                glGetFramebufferAttachmentParameteriv(%s, %s, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &object_type);' % (target, attachment)
459         print '                if (object_type != GL_NONE) {'
460         print '                    json.beginMember(enumToString(%s));' % attachment
461         print '                    json.beginObject();'
462         self.dump_atoms(glGetFramebufferAttachmentParameter, target, attachment)
463         print '                    json.endObject();'
464         print '                    json.endMember(); // GL_x_ATTACHMENT'
465         print '                }'
466         print '            }'
467
468     def dump_atoms(self, getter, *args):
469         for _, _, name in getter.iter():
470             self.dump_atom(getter, *(args + (name,))) 
471
472     def dump_atom(self, getter, *args):
473         name = args[-1]
474
475         # Avoid crash on MacOSX
476         # XXX: The right fix would be to look at the support extensions..
477         import platform
478         if name == 'GL_SAMPLER_BINDING' and platform.system() == 'Darwin':
479             return
480
481         print '        // %s' % name
482         print '        {'
483         #print '            assert(glGetError() == GL_NO_ERROR);'
484         type, value = getter(*args)
485         print '            if (glGetError() != GL_NO_ERROR) {'
486         #print '                std::cerr << "warning: %s(%s) failed\\n";' % (inflection, name)
487         print '                while (glGetError() != GL_NO_ERROR) {}'
488         print '            } else {'
489         print '                json.beginMember("%s");' % name
490         JsonWriter().visit(type, value)
491         print '                json.endMember();'
492         print '            }'
493         print '        }'
494         print
495
496
497 if __name__ == '__main__':
498     StateDumper().dump()