José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 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 |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 28 | import glapi |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 29 | |
| 30 | |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 31 | |
| 32 | class ConstRemover(base.Rebuilder): |
| 33 | |
| 34 | def visit_const(self, const): |
| 35 | return const.type |
| 36 | |
José Fonseca | f6592d7 | 2010-11-21 12:44:41 +0000 | [diff] [blame] | 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 | |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 43 | |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 44 | class ValueExtractor(base.Visitor): |
| 45 | |
José Fonseca | b11188f | 2010-11-21 01:53:58 +0000 | [diff] [blame] | 46 | def visit_literal(self, literal, lvalue, rvalue): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 47 | print ' %s = %s;' % (lvalue, rvalue) |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 48 | |
José Fonseca | b11188f | 2010-11-21 01:53:58 +0000 | [diff] [blame] | 49 | def visit_alias(self, alias, lvalue, rvalue): |
| 50 | self.visit(alias.type, lvalue, rvalue) |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 51 | |
José Fonseca | b11188f | 2010-11-21 01:53:58 +0000 | [diff] [blame] | 52 | def visit_enum(self, enum, lvalue, rvalue): |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 53 | print ' %s = %s;' % (lvalue, rvalue) |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 54 | |
José Fonseca | b11188f | 2010-11-21 01:53:58 +0000 | [diff] [blame] | 55 | def visit_bitmask(self, bitmask, lvalue, rvalue): |
| 56 | self.visit(bitmask.type, lvalue, rvalue) |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 57 | |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 58 | def visit_array(self, array, lvalue, rvalue): |
José Fonseca | 8badad0 | 2010-11-21 02:34:13 +0000 | [diff] [blame] | 59 | print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue) |
| 60 | print ' if (__a%s) {' % (array.id) |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 61 | length = '__a%s->values.size()' % array.id |
| 62 | print ' %s = new %s[%s];' % (lvalue, array.type, length) |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 63 | index = '__i' + array.id |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 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 ' }' |
José Fonseca | 50d78d8 | 2010-11-23 22:13:14 +0000 | [diff] [blame^] | 72 | |
| 73 | def visit_pointer(self, pointer, lvalue, rvalue): |
| 74 | # FIXME |
| 75 | raise NotImplementedError |
José Fonseca | c9edb83 | 2010-11-20 09:03:10 +0000 | [diff] [blame] | 76 | |
José Fonseca | 50d78d8 | 2010-11-23 22:13:14 +0000 | [diff] [blame^] | 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 | |
José Fonseca | b11188f | 2010-11-21 01:53:58 +0000 | [diff] [blame] | 81 | def visit_blob(self, blob, lvalue, rvalue): |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 82 | print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue) |
| 83 | |
| 84 | def visit_string(self, string, lvalue, rvalue): |
| 85 | print ' %s = (%s).string();' % (lvalue, rvalue) |
José Fonseca | 885f265 | 2010-11-20 11:22:25 +0000 | [diff] [blame] | 86 | |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 87 | |
| 88 | |
José Fonseca | 50d78d8 | 2010-11-23 22:13:14 +0000 | [diff] [blame^] | 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 | |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 131 | def retrace_function(function): |
| 132 | print 'static void retrace_%s(Trace::Call &call) {' % function.name |
José Fonseca | ee855d9 | 2010-11-22 17:14:47 +0000 | [diff] [blame] | 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 | print ' %s(%s);' % (function.name, arg_names) |
José Fonseca | 50d78d8 | 2010-11-23 22:13:14 +0000 | [diff] [blame^] | 149 | for arg in function.args: |
| 150 | if arg.output: |
| 151 | arg.type = ConstRemover().visit(arg.type) |
| 152 | rvalue = 'call.arg("%s")' % (arg.name,) |
| 153 | lvalue = arg.name |
| 154 | try: |
| 155 | ValueWrapper().visit(arg.type, lvalue, rvalue) |
| 156 | except NotImplementedError: |
| 157 | print ' // FIXME: %s' % arg.name |
José Fonseca | 501f286 | 2010-11-19 20:41:18 +0000 | [diff] [blame] | 158 | print '}' |
| 159 | print |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 160 | |
| 161 | |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 162 | def retrace_functions(functions): |
| 163 | for function in functions: |
José Fonseca | ee855d9 | 2010-11-22 17:14:47 +0000 | [diff] [blame] | 164 | if function.sideeffects: |
| 165 | retrace_function(function) |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 166 | |
| 167 | print 'static bool retrace_call(Trace::Call &call) {' |
| 168 | for function in functions: |
José Fonseca | ee855d9 | 2010-11-22 17:14:47 +0000 | [diff] [blame] | 169 | if not function.sideeffects: |
| 170 | print ' if (call.name == "%s") {' % function.name |
| 171 | print ' return true;' |
| 172 | print ' }' |
| 173 | print |
| 174 | print ' std::cout << call;' |
| 175 | print ' std::cout.flush();' |
| 176 | print |
| 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;' |
| 182 | print ' }' |
José Fonseca | 2defc98 | 2010-11-22 16:59:10 +0000 | [diff] [blame] | 183 | print ' std::cerr << "warning: unsupported call " << call.name << "\\n";' |
| 184 | print ' return false;' |
| 185 | print '}' |
| 186 | print |
| 187 | |
| 188 | |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 189 | if __name__ == '__main__': |
| 190 | print |
| 191 | print '#include <stdlib.h>' |
| 192 | print '#include <string.h>' |
| 193 | print '#include <GL/glew.h>' |
| 194 | print '#include <GL/glut.h>' |
| 195 | print |
| 196 | print '#include "trace_parser.hpp"' |
| 197 | print |
José Fonseca | 50d78d8 | 2010-11-23 22:13:14 +0000 | [diff] [blame^] | 198 | print 'static std::map<GLuint, GLuint> __texture_map;' |
| 199 | print |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 200 | |
José Fonseca | 8fbdd3a | 2010-11-23 20:55:07 +0000 | [diff] [blame] | 201 | retrace_functions(glapi.glapi.functions) |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 202 | |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 203 | print ''' |
| 204 | |
José Fonseca | 6f51d3b | 2010-11-22 19:56:19 +0000 | [diff] [blame] | 205 | Trace::Parser parser; |
| 206 | |
José Fonseca | 082051b | 2010-11-23 12:00:31 +0000 | [diff] [blame] | 207 | static bool insideGlBeginEnd; |
| 208 | |
José Fonseca | 6f51d3b | 2010-11-22 19:56:19 +0000 | [diff] [blame] | 209 | static void display(void) { |
| 210 | Trace::Call *call; |
| 211 | |
| 212 | while ((call = parser.parse_call())) { |
José Fonseca | 082051b | 2010-11-23 12:00:31 +0000 | [diff] [blame] | 213 | if (call->name == "glFlush" || |
José Fonseca | 6f51d3b | 2010-11-22 19:56:19 +0000 | [diff] [blame] | 214 | call->name == "glXSwapBuffers" || |
| 215 | call->name == "wglSwapBuffers") { |
| 216 | glFlush(); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | retrace_call(*call); |
| 221 | |
José Fonseca | 082051b | 2010-11-23 12:00:31 +0000 | [diff] [blame] | 222 | if (call->name == "glBegin") { |
| 223 | insideGlBeginEnd = true; |
| 224 | } |
| 225 | |
| 226 | if (call->name == "glEnd") { |
| 227 | insideGlBeginEnd = false; |
| 228 | } |
| 229 | |
| 230 | if (!insideGlBeginEnd) { |
| 231 | GLenum error = glGetError(); |
| 232 | if (error != GL_NO_ERROR) { |
| 233 | std::cerr << "warning: glGetError() = "; |
| 234 | switch (error) { |
| 235 | case GL_INVALID_ENUM: |
| 236 | std::cerr << "GL_INVALID_ENUM"; |
| 237 | break; |
| 238 | case GL_INVALID_VALUE: |
| 239 | std::cerr << "GL_INVALID_VALUE"; |
| 240 | break; |
| 241 | case GL_INVALID_OPERATION: |
| 242 | std::cerr << "GL_INVALID_OPERATION"; |
| 243 | break; |
| 244 | case GL_STACK_OVERFLOW: |
| 245 | std::cerr << "GL_STACK_OVERFLOW"; |
| 246 | break; |
| 247 | case GL_STACK_UNDERFLOW: |
| 248 | std::cerr << "GL_STACK_UNDERFLOW"; |
| 249 | break; |
| 250 | case GL_OUT_OF_MEMORY: |
| 251 | std::cerr << "GL_OUT_OF_MEMORY"; |
| 252 | break; |
José Fonseca | b2e03d8 | 2010-11-23 21:35:41 +0000 | [diff] [blame] | 253 | case GL_INVALID_FRAMEBUFFER_OPERATION: |
| 254 | std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION"; |
| 255 | break; |
José Fonseca | 082051b | 2010-11-23 12:00:31 +0000 | [diff] [blame] | 256 | case GL_TABLE_TOO_LARGE: |
| 257 | std::cerr << "GL_TABLE_TOO_LARGE"; |
| 258 | break; |
| 259 | default: |
| 260 | std::cerr << error; |
| 261 | break; |
| 262 | } |
| 263 | std::cerr << "\\n"; |
José Fonseca | 6f51d3b | 2010-11-22 19:56:19 +0000 | [diff] [blame] | 264 | } |
José Fonseca | 6f51d3b | 2010-11-22 19:56:19 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | glFlush(); |
| 269 | glutIdleFunc(NULL); |
| 270 | } |
| 271 | |
| 272 | static void idle(void) { |
| 273 | glutPostRedisplay(); |
| 274 | } |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 275 | |
| 276 | int main(int argc, char **argv) |
| 277 | { |
| 278 | glutInit(&argc, argv); |
José Fonseca | 6f51d3b | 2010-11-22 19:56:19 +0000 | [diff] [blame] | 279 | glutInitWindowPosition(0, 0); |
| 280 | glutInitWindowSize(800, 600); |
| 281 | glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE); |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 282 | glutCreateWindow(argv[0]); |
| 283 | glewInit(); |
José Fonseca | 6f51d3b | 2010-11-22 19:56:19 +0000 | [diff] [blame] | 284 | |
| 285 | glutDisplayFunc(&display); |
| 286 | glutIdleFunc(&idle); |
| 287 | |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 288 | for (int i = 1; i < argc; ++i) { |
José Fonseca | 6f51d3b | 2010-11-22 19:56:19 +0000 | [diff] [blame] | 289 | if (parser.open(argv[i])) { |
| 290 | glutMainLoop(); |
| 291 | parser.close(); |
| 292 | } |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 293 | } |
José Fonseca | 6f51d3b | 2010-11-22 19:56:19 +0000 | [diff] [blame] | 294 | |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | ''' |