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