1 ##########################################################################
3 # Copyright 2010 VMware, Inc.
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:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
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
24 ##########################################################################/
32 class ConstRemover(base.Rebuilder):
34 def visit_const(self, const):
37 def visit_opaque(self, opaque):
39 if expr.startswith('const '):
41 return base.Opaque(expr)
44 class ValueExtractor(base.Visitor):
46 def visit_literal(self, literal, lvalue, rvalue):
47 print ' %s = %s;' % (lvalue, rvalue)
49 def visit_alias(self, alias, lvalue, rvalue):
50 self.visit(alias.type, lvalue, rvalue)
52 def visit_enum(self, enum, lvalue, rvalue):
53 print ' %s = %s;' % (lvalue, rvalue)
55 def visit_bitmask(self, bitmask, lvalue, rvalue):
56 self.visit(bitmask.type, lvalue, rvalue)
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)
66 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
70 print ' %s = NULL;' % lvalue
73 def visit_pointer(self, pointer, lvalue, rvalue):
75 raise NotImplementedError
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)
81 def visit_blob(self, blob, lvalue, rvalue):
82 print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue)
84 def visit_string(self, string, lvalue, rvalue):
85 print ' %s = (%s).string();' % (lvalue, rvalue)
89 class ValueWrapper(base.Visitor):
91 def visit_literal(self, literal, lvalue, rvalue):
94 def visit_alias(self, alias, lvalue, rvalue):
95 self.visit(alias.type, lvalue, rvalue)
97 def visit_enum(self, enum, lvalue, rvalue):
100 def visit_bitmask(self, bitmask, lvalue, rvalue):
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)
110 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
115 def visit_pointer(self, pointer, lvalue, rvalue):
117 raise NotImplementedError
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)
123 def visit_blob(self, blob, lvalue, rvalue):
126 def visit_string(self, string, lvalue, rvalue):
131 def retrace_function(function):
132 print 'static void retrace_%s(Trace::Call &call) {' % function.name
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,)
140 ValueExtractor().visit(arg.type, lvalue, rvalue)
141 except NotImplementedError:
143 print ' %s = 0; // FIXME' % arg.name
145 print ' std::cerr << "warning: unsupported call %s\\n";' % function.name
147 arg_names = ", ".join([arg.name for arg in function.args])
148 print ' %s(%s);' % (function.name, arg_names)
149 for arg in function.args:
151 arg.type = ConstRemover().visit(arg.type)
152 rvalue = 'call.arg("%s")' % (arg.name,)
155 ValueWrapper().visit(arg.type, lvalue, rvalue)
156 except NotImplementedError:
157 print ' // FIXME: %s' % arg.name
162 def retrace_functions(functions):
163 for function in functions:
164 if function.sideeffects:
165 retrace_function(function)
167 print 'static bool retrace_call(Trace::Call &call) {'
168 for function in functions:
169 if not function.sideeffects:
170 print ' if (call.name == "%s") {' % function.name
171 print ' return true;'
174 print ' std::cout << call;'
175 print ' std::cout.flush();'
177 for function in functions:
178 if function.sideeffects:
179 print ' if (call.name == "%s") {' % function.name
180 print ' retrace_%s(call);' % function.name
181 print ' return true;'
183 print ' std::cerr << "warning: unsupported call " << call.name << "\\n";'
184 print ' return false;'
189 if __name__ == '__main__':
191 print '#include <stdlib.h>'
192 print '#include <string.h>'
193 print '#include <GL/glew.h>'
194 print '#include <GL/glut.h>'
196 print '#include "trace_parser.hpp"'
198 for name in ['texture', 'framebuffer', 'renderbuffer']:
199 print 'static std::map<GLuint, GLuint> __%s_map;' % name
202 retrace_functions(glapi.glapi.functions)
206 Trace::Parser parser;
208 static bool insideGlBeginEnd;
210 static void display(void) {
213 while ((call = parser.parse_call())) {
214 if (call->name == "glFlush" ||
215 call->name == "glXSwapBuffers" ||
216 call->name == "wglSwapBuffers") {
223 if (call->name == "glBegin") {
224 insideGlBeginEnd = true;
227 if (call->name == "glEnd") {
228 insideGlBeginEnd = false;
231 if (!insideGlBeginEnd) {
232 GLenum error = glGetError();
233 if (error != GL_NO_ERROR) {
234 std::cerr << "warning: glGetError() = ";
236 case GL_INVALID_ENUM:
237 std::cerr << "GL_INVALID_ENUM";
239 case GL_INVALID_VALUE:
240 std::cerr << "GL_INVALID_VALUE";
242 case GL_INVALID_OPERATION:
243 std::cerr << "GL_INVALID_OPERATION";
245 case GL_STACK_OVERFLOW:
246 std::cerr << "GL_STACK_OVERFLOW";
248 case GL_STACK_UNDERFLOW:
249 std::cerr << "GL_STACK_UNDERFLOW";
251 case GL_OUT_OF_MEMORY:
252 std::cerr << "GL_OUT_OF_MEMORY";
254 case GL_INVALID_FRAMEBUFFER_OPERATION:
255 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
257 case GL_TABLE_TOO_LARGE:
258 std::cerr << "GL_TABLE_TOO_LARGE";
273 static void idle(void) {
277 int main(int argc, char **argv)
279 glutInit(&argc, argv);
280 glutInitWindowPosition(0, 0);
281 glutInitWindowSize(800, 600);
282 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
283 glutCreateWindow(argv[0]);
286 glutDisplayFunc(&display);
289 for (int i = 1; i < argc; ++i) {
290 if (parser.open(argv[i])) {