]> git.cworth.org Git - apitrace/blob - glretrace.py
More extensions.
[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 stdapi
28 import glapi
29 from retrace import Retracer
30
31
32 class GlRetracer(Retracer):
33
34     def filter_function(self, function):
35         return function.name not in [
36             "glNewBufferRegion",
37             "glDeleteBufferRegion",
38             "glReadBufferRegion",
39             "glDrawBufferRegion",
40             "glBufferRegionEnabled",
41         ]
42
43     def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
44         if function.name in [
45             "glColorPointer",
46             "glEdgeFlagPointer",
47             "glIndexPointer",
48             "glNormalPointer",
49             "glTexCoordPointer",
50             "glVertexPointer",
51             "glFogCoordPointer",
52             "glSecondaryColorPointer",
53             "glVertexAttribPointer",
54         ] and arg.name == 'pointer':
55             self.extract_pointer(function, arg, arg_type, lvalue, rvalue)
56         else:
57             Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
58
59     def extract_pointer(self, function, arg, arg_type, lvalue, rvalue):
60         print '    if (dynamic_cast<Trace::Null *>(&%s)) {' % rvalue
61         print '        %s = 0;' % (lvalue)
62         print '    } else {'
63         print '        %s = (%s)(uintptr_t)(%s);' % (lvalue, arg_type, rvalue)
64         print '    }'
65
66
67 if __name__ == '__main__':
68     print
69     print '#include <stdlib.h>'
70     print '#include <string.h>'
71     print
72     print '#ifdef WIN32'
73     print '#include <windows.h>'
74     print '#endif'
75     print
76     print '#include <GL/glew.h>'
77     print '#include <GL/glut.h>'
78     print
79     print 'static bool double_buffer = false;'
80     print
81     api = glapi.glapi
82     retracer = GlRetracer()
83     retracer.retrace_api(glapi.glapi)
84     print '''
85
86 Trace::Parser parser;
87
88 static bool insideGlBeginEnd;
89
90 static void display(void) {
91    Trace::Call *call;
92
93    while ((call = parser.parse_call())) {
94       if (call->name == "glFlush") {
95          glFlush();
96          return;
97       }
98       
99       if (call->name == "glXSwapBuffers" ||
100           call->name == "wglSwapBuffers") {
101          if (double_buffer)
102             glutSwapBuffers();
103          else
104             glFlush();
105          return;
106       }
107       
108       retrace_call(*call);
109
110       if (call->name == "glBegin") {
111          insideGlBeginEnd = true;
112       }
113       
114       if (call->name == "glEnd") {
115          insideGlBeginEnd = false;
116       }
117
118       if (!insideGlBeginEnd) {
119          GLenum error = glGetError();
120          if (error != GL_NO_ERROR) {
121             std::cerr << "warning: glGetError() = ";
122             switch (error) {
123             case GL_INVALID_ENUM:
124                std::cerr << "GL_INVALID_ENUM";
125                break;
126             case GL_INVALID_VALUE:
127                std::cerr << "GL_INVALID_VALUE";
128                break;
129             case GL_INVALID_OPERATION:
130                std::cerr << "GL_INVALID_OPERATION";
131                break;
132             case GL_STACK_OVERFLOW:
133                std::cerr << "GL_STACK_OVERFLOW";
134                break;
135             case GL_STACK_UNDERFLOW:
136                std::cerr << "GL_STACK_UNDERFLOW";
137                break;
138             case GL_OUT_OF_MEMORY:
139                std::cerr << "GL_OUT_OF_MEMORY";
140                break;
141             case GL_INVALID_FRAMEBUFFER_OPERATION:
142                std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
143                break;
144             case GL_TABLE_TOO_LARGE:
145                std::cerr << "GL_TABLE_TOO_LARGE";
146                break;
147             default:
148                std::cerr << error;
149                break;
150             }
151             std::cerr << "\\n";
152          }
153       }
154    }
155
156    glFlush();
157    glutIdleFunc(NULL);
158 }
159
160 static void idle(void) {
161    glutPostRedisplay();
162 }
163
164 int main(int argc, char **argv)
165 {
166
167    int i;
168    for (i = 1; i < argc; ++i) {
169       const char *arg = argv[i];
170
171       if (arg[0] != '-') {
172          break;
173       }
174
175       if (!strcmp(arg, "--")) {
176          break;
177       }
178       else if (!strcmp(arg, "-db")) {
179          double_buffer = true;
180       } else if (!strcmp(arg, "-v")) {
181          ++verbosity;
182       } else {
183          std::cerr << "error: unknown option " << arg << "\\n";
184          return 1;
185       }
186    }
187
188    glutInit(&argc, argv);
189    glutInitWindowPosition(0, 0);
190    glutInitWindowSize(800, 600);
191    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
192    glutCreateWindow(argv[0]);
193    glewInit();
194
195    glutDisplayFunc(&display);
196    glutIdleFunc(&idle);
197
198    for (GLuint h = 0; h < 1024; ++h) {
199       __list_map[h] = h;
200    }
201
202    for ( ; i < argc; ++i) {
203       if (parser.open(argv[i])) {
204          glutMainLoop();
205          parser.close();
206       }
207    }
208
209    return 0;
210 }
211
212 '''