]> git.cworth.org Git - apitrace/blob - glretrace.py
Cleanup/remove dead code.
[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 r'''
93 #include <string.h>
94 #include <iostream>
95
96 #include "glproc.hpp"
97 #include <GL/glut.h>
98
99 static bool double_buffer = false;
100 static bool insideGlBeginEnd = false;
101
102 static int __window_width = 256, __window_height = 256;
103 bool __reshape_window = false;
104
105 unsigned __frame = 0;
106 long long __startTime = 0;
107 bool __screenshots = 0;
108
109
110 static void
111 checkGlError(void) {
112     if (insideGlBeginEnd) {
113         return;
114     }
115
116     GLenum error = glGetError();
117     if (error == GL_NO_ERROR) {
118         return;
119     }
120
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     api = glapi.glapi
155     retracer = GlRetracer()
156     retracer.retrace_api(glapi.glapi)
157     print r'''
158
159 static Trace::Parser parser;
160
161 static void display_noop(void) {
162 }
163
164 #include "bmp.hpp"
165
166 static void frame_complete(void) {
167     ++__frame;
168     
169     if (__screenshots && !__reshape_window) {
170         char filename[PATH_MAX];
171         snprintf(filename, sizeof filename, "screenshot_%04u.bmp", __frame);
172         unsigned char *pixels = new unsigned char[__window_height*__window_width*4];
173         glReadPixels(0, 0, __window_width, __window_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
174         BMP::write(filename, pixels, __window_width, __window_height, __window_width*4);
175     }
176
177 }
178
179 static void display(void) {
180     Trace::Call *call;
181
182     while ((call = parser.parse_call())) {
183         if (call->name() == "glFlush") {
184             glFlush();
185             if (!double_buffer) {
186                 frame_complete();
187             }
188         }
189         
190         if (!retrace_call(*call)) {
191             if (call->name() == "glXSwapBuffers" ||
192                 call->name() == "wglSwapBuffers") {
193                 if (double_buffer)
194                     glutSwapBuffers();
195                 else
196                     glFlush();
197                 frame_complete();
198                 return;
199             }
200         }
201     }
202
203     // Reached the end of trace
204     glFlush();
205
206     long long endTime = OS::GetTime();
207     float timeInterval = (endTime - __startTime) * 1.0E-6;
208
209     std::cout << 
210         "Rendered " << __frame << " frames"
211         " in " <<  timeInterval << " secs,"
212         " average of " << (__frame/timeInterval) << " fps\n";
213
214     glutDisplayFunc(&display_noop);
215     glutIdleFunc(NULL);
216 }
217
218 static void idle(void) {
219     if (__reshape_window) {
220         // XXX: doesn't quite work
221         glutReshapeWindow(__window_width, __window_height);
222         __reshape_window = false;
223     }
224     glutPostRedisplay();
225 }
226
227 int main(int argc, char **argv)
228 {
229
230     int i;
231     for (i = 1; i < argc; ++i) {
232         const char *arg = argv[i];
233
234         if (arg[0] != '-') {
235             break;
236         }
237
238         if (!strcmp(arg, "--")) {
239             break;
240         } else if (!strcmp(arg, "-db")) {
241             double_buffer = true;
242         } else if (!strcmp(arg, "-s")) {
243             __screenshots = true;
244         } else if (!strcmp(arg, "-v")) {
245             ++verbosity;
246         } else {
247             std::cerr << "error: unknown option " << arg << "\n";
248             return 1;
249         }
250     }
251
252     glutInit(&argc, argv);
253     glutInitWindowPosition(0, 0);
254     glutInitWindowSize(__window_width, __window_height);
255     glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
256     glutCreateWindow(argv[0]);
257
258     glutDisplayFunc(&display);
259     glutIdleFunc(&idle);
260
261     for (GLuint h = 0; h < 1024; ++h) {
262         __list_map[h] = h;
263     }
264
265     for ( ; i < argc; ++i) {
266         if (parser.open(argv[i])) {
267             __startTime = OS::GetTime();
268             glutMainLoop();
269             parser.close();
270         }
271     }
272
273     return 0;
274 }
275
276 '''