]> git.cworth.org Git - apitrace/blob - glretrace.py
a9fb82cb341f79fc291f03044df749d423df861d
[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 import glapi
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         length = '__a%s->values.size()' % array.id
62         print '        %s = new %s[%s];' % (lvalue, array.type, length)
63         index = '__i' + array.id
64         print '        for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
65         try:
66             self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
67         finally:
68             print '        }'
69             print '    } else {'
70             print '        %s = NULL;' % lvalue
71             print '    }'
72     
73     def visit_pointer(self, pointer, lvalue, rvalue):
74         # FIXME
75         raise NotImplementedError
76
77     def visit_handle(self, handle, lvalue, rvalue):
78         self.visit(handle.type, lvalue, "__%s_map[%s]" %(handle.name, rvalue));
79         print '    std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
80     
81     def visit_blob(self, blob, lvalue, rvalue):
82         print '    %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue)
83     
84     def visit_string(self, string, lvalue, rvalue):
85         print '    %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue)
86
87
88
89 class ValueWrapper(base.Visitor):
90
91     def visit_literal(self, literal, lvalue, rvalue):
92         pass
93
94     def visit_alias(self, alias, lvalue, rvalue):
95         self.visit(alias.type, lvalue, rvalue)
96     
97     def visit_enum(self, enum, lvalue, rvalue):
98         pass
99
100     def visit_bitmask(self, bitmask, lvalue, rvalue):
101         pass
102
103     def visit_array(self, array, lvalue, rvalue):
104         print '    const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
105         print '    if (__a%s) {' % (array.id)
106         length = '__a%s->values.size()' % array.id
107         index = '__i' + array.id
108         print '        for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
109         try:
110             self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
111         finally:
112             print '        }'
113             print '    }'
114     
115     def visit_pointer(self, pointer, lvalue, rvalue):
116         # FIXME
117         raise NotImplementedError
118
119     def visit_handle(self, handle, lvalue, rvalue):
120         print "    __%s_map[static_cast<%s>(%s)] = %s;" % (handle.name, handle.type, rvalue, lvalue)
121         print '    std::cout << "%s " << static_cast<%s>(%s) << " -> " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
122     
123     def visit_blob(self, blob, lvalue, rvalue):
124         pass
125     
126     def visit_string(self, string, lvalue, rvalue):
127         pass
128
129
130
131 def retrace_function(function):
132     print 'static void retrace_%s(Trace::Call &call) {' % function.name
133     success = True
134     for arg in function.args:
135         arg.type = ConstRemover().visit(arg.type)
136         print '    %s %s;' % (arg.type, arg.name)
137         rvalue = 'call.arg("%s")' % (arg.name,)
138         lvalue = arg.name
139         try:
140             ValueExtractor().visit(arg.type, lvalue, rvalue)
141         except NotImplementedError:
142             success = False
143             print '    %s = 0; // FIXME' % arg.name
144     if not success:
145         print '    std::cerr << "warning: unsupported call %s\\n";' % function.name
146         print '    return;'
147     arg_names = ", ".join([arg.name for arg in function.args])
148     if function.type is not base.Void:
149         print '    %s __result;' % (function.type)
150         print '    __result = %s(%s);' % (function.name, arg_names)
151     else:
152         print '    %s(%s);' % (function.name, arg_names)
153     for arg in function.args:
154         if arg.output:
155             arg.type = ConstRemover().visit(arg.type)
156             rvalue = 'call.arg("%s")' % (arg.name,)
157             lvalue = arg.name
158             try:
159                 ValueWrapper().visit(arg.type, lvalue, rvalue)
160             except NotImplementedError:
161                 print '   // FIXME: %s' % arg.name
162     if function.type is not base.Void:
163         rvalue = '*call.ret'
164         lvalue = '__result'
165         try:
166             ValueWrapper().visit(function.type, lvalue, rvalue)
167         except NotImplementedError:
168             print '   // FIXME: result'
169     print '}'
170     print
171
172
173 def retrace_functions(functions):
174     for function in functions:
175         if function.sideeffects:
176             retrace_function(function)
177
178     print 'static bool retrace_call(Trace::Call &call) {'
179     for function in functions:
180         if not function.sideeffects:
181             print '    if (call.name == "%s") {' % function.name
182             print '        return true;'
183             print '    }'
184     print
185     print '    std::cout << call;'
186     print '    std::cout.flush();'
187     print
188     for function in functions:
189         if function.sideeffects:
190             print '    if (call.name == "%s") {' % function.name
191             print '        retrace_%s(call);' % function.name
192             print '        return true;'
193             print '    }'
194     print '    std::cerr << "warning: unsupported call " << call.name << "\\n";'
195     print '    return false;'
196     print '}'
197     print
198
199
200 def retrace_api(api):
201     types = api.all_types()
202
203     handles = [type for type in types if isinstance(type, base.Handle)]
204     for handle in handles:
205         print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name)
206     print
207
208     retrace_functions(api.functions)
209
210
211 if __name__ == '__main__':
212     print
213     print '#include <stdlib.h>'
214     print '#include <string.h>'
215     print '#include <GL/glew.h>'
216     print '#include <GL/glut.h>'
217     print
218     print '#include "trace_parser.hpp"'
219     print
220     retrace_api(glapi.glapi)
221     print '''
222
223 Trace::Parser parser;
224
225 static bool insideGlBeginEnd;
226
227 static void display(void) {
228    Trace::Call *call;
229
230    while ((call = parser.parse_call())) {
231       if (call->name == "glFlush" ||
232           call->name == "glXSwapBuffers" ||
233           call->name == "wglSwapBuffers") {
234          glFlush();
235          return;
236       }
237       
238       retrace_call(*call);
239
240       if (call->name == "glBegin") {
241          insideGlBeginEnd = true;
242       }
243       
244       if (call->name == "glEnd") {
245          insideGlBeginEnd = false;
246       }
247
248       if (!insideGlBeginEnd) {
249          GLenum error = glGetError();
250          if (error != GL_NO_ERROR) {
251             std::cerr << "warning: glGetError() = ";
252             switch (error) {
253             case GL_INVALID_ENUM:
254                std::cerr << "GL_INVALID_ENUM";
255                break;
256             case GL_INVALID_VALUE:
257                std::cerr << "GL_INVALID_VALUE";
258                break;
259             case GL_INVALID_OPERATION:
260                std::cerr << "GL_INVALID_OPERATION";
261                break;
262             case GL_STACK_OVERFLOW:
263                std::cerr << "GL_STACK_OVERFLOW";
264                break;
265             case GL_STACK_UNDERFLOW:
266                std::cerr << "GL_STACK_UNDERFLOW";
267                break;
268             case GL_OUT_OF_MEMORY:
269                std::cerr << "GL_OUT_OF_MEMORY";
270                break;
271             case GL_INVALID_FRAMEBUFFER_OPERATION:
272                std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
273                break;
274             case GL_TABLE_TOO_LARGE:
275                std::cerr << "GL_TABLE_TOO_LARGE";
276                break;
277             default:
278                std::cerr << error;
279                break;
280             }
281             std::cerr << "\\n";
282          }
283       }
284    }
285
286    glFlush();
287    glutIdleFunc(NULL);
288 }
289
290 static void idle(void) {
291    glutPostRedisplay();
292 }
293
294 int main(int argc, char **argv)
295 {
296    glutInit(&argc, argv);
297    glutInitWindowPosition(0, 0);
298    glutInitWindowSize(800, 600);
299    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
300    glutCreateWindow(argv[0]);
301    glewInit();
302
303    glutDisplayFunc(&display);
304    glutIdleFunc(&idle);
305
306    for (int i = 1; i < argc; ++i) {
307       if (parser.open(argv[i])) {
308          glutMainLoop();
309          parser.close();
310       }
311    }
312
313    return 0;
314 }
315
316 '''