]> git.cworth.org Git - apitrace/blob - glretrace.py
Handle VBO draw elements .
[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             "glColorPointerEXT",
71             "glEdgeFlagPointer",
72             "glEdgeFlagPointerEXT",
73             "glFogCoordPointer",
74             "glFogCoordPointerEXT",
75             "glIndexPointer",
76             "glIndexPointerEXT",
77             "glMatrixIndexPointerARB",
78             "glNormalPointer",
79             "glNormalPointerEXT",
80             "glSecondaryColorPointer",
81             "glSecondaryColorPointerEXT",
82             "glTexCoordPointer",
83             "glTexCoordPointerEXT",
84             "glVertexAttribLPointer",
85             "glVertexAttribPointer",
86             "glVertexAttribPointerARB",
87             "glVertexAttribPointerNV",
88             "glVertexPointer",
89             "glVertexPointerEXT",
90         ] and arg.name == 'pointer':
91             self.extract_pointer(function, arg, arg_type, lvalue, rvalue)
92         else:
93             Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
94
95     def extract_pointer(self, function, arg, arg_type, lvalue, rvalue):
96         print '    if (dynamic_cast<Trace::Null *>(&%s)) {' % rvalue
97         print '        %s = 0;' % (lvalue)
98         print '    } else {'
99         print '        %s = (%s)(uintptr_t)(%s);' % (lvalue, arg_type, rvalue)
100         print '    }'
101
102
103 if __name__ == '__main__':
104     print r'''
105 #include <string.h>
106 #include <iostream>
107
108 #include "glproc.hpp"
109 #include <GL/glut.h>
110
111 static bool double_buffer = false;
112 static bool insideGlBeginEnd = false;
113
114 static int __window_width = 256, __window_height = 256;
115 bool __reshape_window = false;
116
117 unsigned __frame = 0;
118 long long __startTime = 0;
119 bool __screenshots = 0;
120
121
122 static void
123 checkGlError(void) {
124     if (insideGlBeginEnd) {
125         return;
126     }
127
128     GLenum error = glGetError();
129     if (error == GL_NO_ERROR) {
130         return;
131     }
132
133     std::cerr << "warning: glGetError() = ";
134     switch (error) {
135     case GL_INVALID_ENUM:
136         std::cerr << "GL_INVALID_ENUM";
137         break;
138     case GL_INVALID_VALUE:
139         std::cerr << "GL_INVALID_VALUE";
140         break;
141     case GL_INVALID_OPERATION:
142         std::cerr << "GL_INVALID_OPERATION";
143         break;
144     case GL_STACK_OVERFLOW:
145         std::cerr << "GL_STACK_OVERFLOW";
146         break;
147     case GL_STACK_UNDERFLOW:
148         std::cerr << "GL_STACK_UNDERFLOW";
149         break;
150     case GL_OUT_OF_MEMORY:
151         std::cerr << "GL_OUT_OF_MEMORY";
152         break;
153     case GL_INVALID_FRAMEBUFFER_OPERATION:
154         std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
155         break;
156     case GL_TABLE_TOO_LARGE:
157         std::cerr << "GL_TABLE_TOO_LARGE";
158         break;
159     default:
160         std::cerr << error;
161         break;
162     }
163     std::cerr << "\n";
164 }
165 '''
166     api = glapi.glapi
167     retracer = GlRetracer()
168     retracer.retrace_api(glapi.glapi)
169     print r'''
170
171 static Trace::Parser parser;
172
173 static void display_noop(void) {
174 }
175
176 #include "bmp.hpp"
177
178 static void frame_complete(void) {
179     ++__frame;
180     
181     if (__screenshots && !__reshape_window) {
182         char filename[PATH_MAX];
183         snprintf(filename, sizeof filename, "screenshot_%04u.bmp", __frame);
184         unsigned char *pixels = new unsigned char[__window_height*__window_width*4];
185         glReadPixels(0, 0, __window_width, __window_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
186         BMP::write(filename, pixels, __window_width, __window_height, __window_width*4);
187     }
188
189 }
190
191 static void display(void) {
192     Trace::Call *call;
193
194     while ((call = parser.parse_call())) {
195         if (call->name() == "glFlush") {
196             glFlush();
197             if (!double_buffer) {
198                 frame_complete();
199             }
200         }
201         
202         if (!retrace_call(*call)) {
203             if (call->name() == "glXSwapBuffers" ||
204                 call->name() == "wglSwapBuffers") {
205                 if (double_buffer)
206                     glutSwapBuffers();
207                 else
208                     glFlush();
209                 frame_complete();
210                 return;
211             }
212         }
213     }
214
215     // Reached the end of trace
216     glFlush();
217
218     long long endTime = OS::GetTime();
219     float timeInterval = (endTime - __startTime) * 1.0E-6;
220
221     std::cout << 
222         "Rendered " << __frame << " frames"
223         " in " <<  timeInterval << " secs,"
224         " average of " << (__frame/timeInterval) << " fps\n";
225
226     glutDisplayFunc(&display_noop);
227     glutIdleFunc(NULL);
228 }
229
230 static void idle(void) {
231     if (__reshape_window) {
232         // XXX: doesn't quite work
233         glutReshapeWindow(__window_width, __window_height);
234         __reshape_window = false;
235     }
236     glutPostRedisplay();
237 }
238
239 int main(int argc, char **argv)
240 {
241
242     int i;
243     for (i = 1; i < argc; ++i) {
244         const char *arg = argv[i];
245
246         if (arg[0] != '-') {
247             break;
248         }
249
250         if (!strcmp(arg, "--")) {
251             break;
252         } else if (!strcmp(arg, "-db")) {
253             double_buffer = true;
254         } else if (!strcmp(arg, "-s")) {
255             __screenshots = true;
256         } else if (!strcmp(arg, "-v")) {
257             ++verbosity;
258         } else {
259             std::cerr << "error: unknown option " << arg << "\n";
260             return 1;
261         }
262     }
263
264     glutInit(&argc, argv);
265     glutInitWindowPosition(0, 0);
266     glutInitWindowSize(__window_width, __window_height);
267     glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
268     glutCreateWindow(argv[0]);
269
270     glutDisplayFunc(&display);
271     glutIdleFunc(&idle);
272
273     for (GLuint h = 0; h < 1024; ++h) {
274         __list_map[h] = h;
275     }
276
277     for ( ; i < argc; ++i) {
278         if (parser.open(argv[i])) {
279             __startTime = OS::GetTime();
280             glutMainLoop();
281             parser.close();
282         }
283     }
284
285     return 0;
286 }
287
288 '''