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