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