]> git.cworth.org Git - apitrace/blob - glretrace.py
Use more standard names on FindDirectX.cmake
[apitrace] / glretrace.py
1 ##########################################################################
2 #
3 # Copyright 2010 VMware, Inc.
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 import base
28 from glx import libgl
29
30
31
32 class ConstRemover(base.Rebuilder):
33
34     def visit_const(self, const):
35         return const.type
36
37     def visit_opaque(self, opaque):
38         expr = opaque.expr
39         if expr.startswith('const '):
40             expr = expr[6:]
41         return base.Opaque(expr)
42
43
44 class ValueExtractor(base.Visitor):
45
46     def visit_literal(self, literal, lvalue, rvalue):
47         print '    %s = %s;' % (lvalue, rvalue)
48
49     def visit_alias(self, alias, lvalue, rvalue):
50         self.visit(alias.type, lvalue, rvalue)
51     
52     def visit_enum(self, enum, lvalue, rvalue):
53         print '    %s = %s;' % (lvalue, rvalue)
54
55     def visit_bitmask(self, bitmask, lvalue, rvalue):
56         self.visit(bitmask.type, lvalue, rvalue)
57
58     def visit_array(self, array, lvalue, rvalue):
59         print '    const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
60         print '    if (__a%s) {' % (array.id)
61         print '        %s = new %s[%s];' % (lvalue, array.type, array.length)
62         index = '__i' + array.id
63         print '        for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = '__a%s->values.size()' % array.id)
64         self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
65         print '        }'
66         print '    } else {'
67         print '        %s = NULL;' % lvalue
68         print '    }'
69
70     def visit_blob(self, blob, lvalue, rvalue):
71         print '    %s = (%s)(void *)%s;' % (lvalue, blob, rvalue)
72
73
74
75 def retrace_function(function):
76     print 'static void retrace_%s(Trace::Call &call) {' % function.name
77     if not function.name.startswith('glX'):
78         success = True
79         for arg_type, arg_name in function.args:
80             arg_type = ConstRemover().visit(arg_type)
81             print '    %s %s;' % (arg_type, arg_name)
82             rvalue = 'call.arg("%s")' % (arg_name,)
83             lvalue = arg_name
84             try:
85                 ValueExtractor().visit(arg_type, lvalue, rvalue)
86             except NotImplementedError:
87                 success = False
88                 print '    %s = 0; // FIXME' % arg_name
89         if not success:
90             print '    std::cerr << "warning: unsupported call %s\\n";' % function.name
91             print '    return;'
92         arg_names = ", ".join([arg_name for arg_type, arg_name in function.args])
93         print '    %s(%s);' % (function.name, arg_names)
94     print '}'
95     print
96
97
98 if __name__ == '__main__':
99     print
100     print '#include <stdlib.h>'
101     print '#include <string.h>'
102     print '#include <GL/glew.h>'
103     print '#include <GL/glut.h>'
104     print
105     print '#include "trace_parser.hpp"'
106     print
107
108     for function in libgl.functions:
109         retrace_function(function)
110
111     print 'static bool retrace_call(Trace::Call &call) {'
112     for function in libgl.functions:
113         print '    if (call.name == "%s") {' % function.name
114         print '        retrace_%s(call);' % function.name
115         print '        return true;'
116         print '    }'
117     print '    std::cerr << "warning: unsupported call " << call.name << "\\n";'
118     print '    return false;'
119     print '}'
120     print '''
121
122 class Retracer : public Trace::Parser
123 {
124     void handle_call(Trace::Call &call) {
125         std::cout << call;
126         std::cout.flush();
127         retrace_call(call);
128     }
129 };
130
131 int main(int argc, char **argv)
132 {
133    glutInit(&argc, argv);
134    glutInitWindowPosition( 0, 0 );
135    glutInitWindowSize( 800, 600 );
136    glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE );
137    glutCreateWindow(argv[0]);
138    glewInit();
139    for (int i = 1; i < argc; ++i) {
140       Retracer p;
141       p.parse(argv[i]);
142       glutMainLoop();
143    }
144    return 0;
145 }
146
147 '''