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