]> git.cworth.org Git - apitrace/blob - glstate.py
Add a new "apitrace trace" command to the command-line interface.
[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 __call__(self, *args):
94         pname = args[-1]
95
96         for function, type, count, name in parameters:
97             if type is X:
98                 continue
99             if name == pname:
100                 if count != 1:
101                     type = Array(type, str(count))
102
103                 return type, self.visit(type, args)
104
105         raise NotImplementedError
106
107     def temp_name(self, args):
108         '''Return the name of a temporary variable to hold the state.'''
109         pname = args[-1]
110
111         return pname[3:].lower()
112
113     def visit_const(self, const, args):
114         return self.visit(const.type, args)
115
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)
123         else:
124             print '    %s %s = %s(%s);' % (elem_type, temp_name, inflection + self.suffix, ', '.join(args))
125         return temp_name
126
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))
132         return temp_name
133
134     def visit_alias(self, alias, args):
135         return self.visit_scalar(alias, args)
136
137     def visit_enum(self, enum, args):
138         return self.visit(GLint, args)
139
140     def visit_bitmask(self, bitmask, args):
141         return self.visit(GLint, args)
142
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)
153         return temp_name
154
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)
161         return temp_name
162
163
164 glGet = StateGetter('glGet', {
165     B: 'Booleanv',
166     I: 'Integerv',
167     F: 'Floatv',
168     D: 'Doublev',
169     S: 'String',
170     P: 'Pointerv',
171 })
172
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'})
183
184
185 class JsonWriter(Visitor):
186     '''Type visitor that will dump a value of the specified type through the
187     JSON writer.
188     
189     It expects a previously declared JSONWriter instance named "json".'''
190
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
196         else:
197             raise NotImplementedError
198
199     def visit_string(self, string, instance):
200         assert string.length is None
201         print '    json.writeString((const char *)%s);' % instance
202
203     def visit_enum(self, enum, instance):
204         if enum.expr == 'GLenum':
205             print '    dumpEnum(json, %s);' % instance
206         else:
207             print '    json.writeNumber(%s);' % instance
208
209     def visit_bitmask(self, bitmask, instance):
210         raise NotImplementedError
211
212     def visit_alias(self, alias, instance):
213         self.visit(alias.type, instance)
214
215     def visit_opaque(self, opaque, instance):
216         print '    json.writeNumber((size_t)%s);' % instance
217
218     __index = 0
219
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))
226         print '    }'
227         print '    json.endArray();'
228
229
230
231 class StateDumper:
232     '''Class to generate code to dump all GL state in JSON format via
233     stdout.'''
234
235     def __init__(self):
236         pass
237
238     def dump(self):
239         print '#include <string.h>'
240         print
241         print '#include "json.hpp"'
242         print '#include "glproc.hpp"'
243         print '#include "glsize.hpp"'
244         print '#include "glstate.hpp"'
245         print
246         print 'namespace glstate {'
247         print
248
249         print 'const char *'
250         print 'enumToString(GLenum pname)'
251         print '{'
252         print '    switch (pname) {'
253         for name in GLenum.values:
254             print '    case %s:' % name
255             print '        return "%s";' % name
256         print '    default:'
257         print '        return NULL;'
258         print '    }'
259         print '}'
260         print
261
262         print 'static void'
263         print 'dumpFramebufferAttachementParameters(JSONWriter &json, GLenum target, GLenum attachment)'
264         print '{'
265         self.dump_attachment_parameters('target', 'attachment')
266         print '}'
267         print
268
269         print 'void'
270         print 'dumpEnum(JSONWriter &json, GLenum pname)'
271         print '{'
272         print '    const char *s = enumToString(pname);'
273         print '    if (s) {'
274         print '        json.writeString(s);'
275         print '    } else {'
276         print '        json.writeNumber(pname);'
277         print '    }'
278         print '}'
279         print
280
281         print 'void dumpParameters(JSONWriter &json)'
282         print '{'
283         print '    json.beginMember("parameters");'
284         print '    json.beginObject();'
285         
286         self.dump_atoms(glGet)
287         
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()
295
296         print '    json.endObject();'
297         print '    json.endMember(); // parameters'
298         print '}'
299         print
300         
301         print '} /*namespace glstate */'
302
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();'
309         print
310
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'
324         print '        }'
325         print '    }'
326         print
327
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
332             else:
333                 print '    {'
334             print '        json.beginMember("%s");' % target
335             print '        json.beginObject();'
336             self.dump_atoms(glGetTexEnv, target)
337             print '        json.endObject();'
338             print '    }'
339
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'
351         print '    }'
352         print
353
354     program_targets = [
355         'GL_FRAGMENT_PROGRAM_ARB',
356         'GL_VERTEX_PROGRAM_ARB',
357     ]
358
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();'
366             print '    }'
367
368     def dump_texture_parameters(self):
369         print '    {'
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;'
385         print
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
402             print '            }'
403             print
404         print '            json.endObject();'
405         print '            json.endMember(); // GL_TEXTUREi'
406         print '        }'
407         print '        glActiveTexture(active_texture);'
408         print '    }'
409         print
410
411     def dump_framebuffer_parameters(self):
412         print '    {'
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
426             print '                }'
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
431             print '            }'
432             print
433         print '    }'
434         print
435
436     def dump_attachment_parameters(self, target, attachment):
437         print '            {'
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'
446         print '                }'
447         print '            }'
448
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(','):
453                 continue
454             if type is X:
455                 continue
456             print '        // %s' % name
457             print '        {'
458             type, value = getter(*(args + (name,)))
459             print '            if (glGetError() != GL_NO_ERROR) {'
460             #print '                std::cerr << "warning: %s(%s) failed\\n";' % (inflection, name)
461             print '            } else {'
462             print '                json.beginMember("%s");' % name
463             JsonWriter().visit(type, value)
464             print '                json.endMember();'
465             print '            }'
466             print '        }'
467             print
468
469
470 if __name__ == '__main__':
471     StateDumper().dump()