blob: f809b9e9502bb0b518f1d9f2da1154e14462dad3 [file] [log] [blame]
José Fonseca7e329022010-11-19 17:05:18 +00001##########################################################################
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
José Fonseca4a826ed2010-11-30 16:58:22 +000027"""GL retracer generator."""
28
29
José Fonseca9796b842010-11-25 11:44:50 +000030import stdapi
José Fonseca8fbdd3a2010-11-23 20:55:07 +000031import glapi
José Fonsecadacd8dd2010-11-25 17:50:26 +000032from retrace import Retracer
José Fonseca7e329022010-11-19 17:05:18 +000033
34
José Fonsecadacd8dd2010-11-25 17:50:26 +000035class GlRetracer(Retracer):
José Fonsecac9edb832010-11-20 09:03:10 +000036
José Fonseca3d245f42010-11-28 00:08:23 +000037 def retrace_function(self, function):
38 Retracer.retrace_function(self, function)
39
José Fonseca8caf2c82010-11-30 12:09:12 +000040 draw_array_function_names = set([
41 "glDrawArrays",
42 "glDrawArraysEXT",
43 "glDrawArraysIndirect",
44 "glDrawArraysInstanced",
45 "glDrawArraysInstancedARB",
46 "glDrawArraysInstancedEXT",
47 "glDrawMeshArraysSUN",
48 "glMultiDrawArrays",
49 "glMultiDrawArraysEXT",
50 "glMultiModeDrawArraysIBM",
51 ])
52
53 draw_elements_function_names = set([
54 "glDrawElements",
55 "glDrawElementsBaseVertex",
56 "glDrawElementsIndirect",
57 "glDrawElementsInstanced",
58 "glDrawElementsInstancedARB",
59 "glDrawElementsInstancedBaseVertex",
60 "glDrawElementsInstancedEXT",
61 "glDrawRangeElements",
62 "glDrawRangeElementsBaseVertex",
63 "glDrawRangeElementsEXT",
64 "glMultiDrawElements",
65 "glMultiDrawElementsBaseVertex",
66 "glMultiDrawElementsEXT",
67 "glMultiModeDrawElementsIBM",
68 ])
69
José Fonsecafa15d332010-11-25 20:22:39 +000070 def call_function(self, function):
José Fonseca8caf2c82010-11-30 12:09:12 +000071 if (function.name in self.draw_array_function_names or
72 function.name in self.draw_elements_function_names):
José Fonsecafa15d332010-11-25 20:22:39 +000073 print ' GLint __array_buffer = 0;'
74 print ' glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);'
75 print ' if (!__array_buffer) {'
76 self.fail_function(function)
77 print ' }'
78
José Fonseca8caf2c82010-11-30 12:09:12 +000079 if function.name in self.draw_elements_function_names:
80 print ' GLint __element_array_buffer = 0;'
81 print ' glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);'
82 print ' if (!__element_array_buffer) {'
83 self.fail_function(function)
84 print ' }'
85
José Fonsecacdb574a2010-11-29 12:23:35 +000086 if function.name == "glViewport":
87 print ' if (x + width > __window_width) {'
88 print ' __window_width = x + width;'
89 print ' __reshape_window = true;'
90 print ' }'
91 print ' if (y + height > __window_height) {'
92 print ' __window_height = y + height;'
93 print ' __reshape_window = true;'
94 print ' }'
95
José Fonseca3d245f42010-11-28 00:08:23 +000096 if function.name == "glEnd":
97 print ' insideGlBeginEnd = false;'
José Fonsecacdb574a2010-11-29 12:23:35 +000098
José Fonsecafa15d332010-11-25 20:22:39 +000099 Retracer.call_function(self, function)
José Fonsecacdb574a2010-11-29 12:23:35 +0000100
José Fonseca3d245f42010-11-28 00:08:23 +0000101 if function.name == "glBegin":
102 print ' insideGlBeginEnd = true;'
103 else:
104 # glGetError is not allowed inside glBegin/glEnd
105 print ' checkGlError();'
106
José Fonseca8caf2c82010-11-30 12:09:12 +0000107 pointer_function_names = set([
108 "glColorPointer",
109 "glColorPointerEXT",
110 "glEdgeFlagPointer",
111 "glEdgeFlagPointerEXT",
112 "glFogCoordPointer",
113 "glFogCoordPointerEXT",
114 "glIndexPointer",
115 "glIndexPointerEXT",
116 "glMatrixIndexPointerARB",
117 "glNormalPointer",
118 "glNormalPointerEXT",
119 "glSecondaryColorPointer",
120 "glSecondaryColorPointerEXT",
121 "glTexCoordPointer",
122 "glTexCoordPointerEXT",
123 "glVertexAttribLPointer",
124 "glVertexAttribPointer",
125 "glVertexAttribPointerARB",
126 "glVertexAttribPointerNV",
127 "glVertexPointer",
128 "glVertexPointerEXT",
129 ])
José Fonsecafa15d332010-11-25 20:22:39 +0000130
José Fonsecadacd8dd2010-11-25 17:50:26 +0000131 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
José Fonseca8caf2c82010-11-30 12:09:12 +0000132 if (function.name in self.pointer_function_names and arg.name == 'pointer' or
133 function.name in self.draw_elements_function_names and arg.name == 'indices'):
José Fonseca8a844ae2010-12-06 18:50:52 +0000134 print ' if (dynamic_cast<Trace::Null *>(&%s)) {' % rvalue
135 print ' %s = 0;' % (lvalue)
136 print ' } else {'
137 print ' %s = (%s)(uintptr_t)(%s);' % (lvalue, arg_type, rvalue)
138 print ' }'
139 return
José Fonsecadacd8dd2010-11-25 17:50:26 +0000140
José Fonseca8a844ae2010-12-06 18:50:52 +0000141 if function.name.startswith('glUniform') and function.args[0].name == arg.name == 'location':
142 print ' GLint program = -1;'
143 print ' glGetIntegerv(GL_CURRENT_PROGRAM, &program);'
144
145 Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
José Fonsecae6a50bd2010-11-24 10:12:22 +0000146
147
José Fonseca7e329022010-11-19 17:05:18 +0000148if __name__ == '__main__':
José Fonseca796a3042010-11-29 14:21:06 +0000149 print r'''
José Fonsecadf66a902010-11-29 13:24:20 +0000150#include <string.h>
José Fonsecae0081492010-12-04 13:24:17 +0000151#include <stdio.h>
José Fonsecadf66a902010-11-29 13:24:20 +0000152#include <iostream>
153
154#include "glproc.hpp"
155#include <GL/glut.h>
156
157static bool double_buffer = false;
158static bool insideGlBeginEnd = false;
159
160static int __window_width = 256, __window_height = 256;
161bool __reshape_window = false;
162
163unsigned __frame = 0;
164long long __startTime = 0;
José Fonseca4c04b642011-02-01 19:35:41 +0000165bool __wait = false;
José Fonseca796a3042010-11-29 14:21:06 +0000166
José Fonseca87f19712011-02-09 14:49:50 +0000167const char *__compare_prefix = NULL;
168const char *__snapshot_prefix = NULL;
José Fonseca7fe1dc52010-12-13 20:18:39 +0000169
José Fonsecadf66a902010-11-29 13:24:20 +0000170
José Fonseca3d245f42010-11-28 00:08:23 +0000171static void
172checkGlError(void) {
173 if (insideGlBeginEnd) {
174 return;
175 }
176
177 GLenum error = glGetError();
178 if (error == GL_NO_ERROR) {
179 return;
180 }
181
182 std::cerr << "warning: glGetError() = ";
183 switch (error) {
184 case GL_INVALID_ENUM:
185 std::cerr << "GL_INVALID_ENUM";
186 break;
187 case GL_INVALID_VALUE:
188 std::cerr << "GL_INVALID_VALUE";
189 break;
190 case GL_INVALID_OPERATION:
191 std::cerr << "GL_INVALID_OPERATION";
192 break;
193 case GL_STACK_OVERFLOW:
194 std::cerr << "GL_STACK_OVERFLOW";
195 break;
196 case GL_STACK_UNDERFLOW:
197 std::cerr << "GL_STACK_UNDERFLOW";
198 break;
199 case GL_OUT_OF_MEMORY:
200 std::cerr << "GL_OUT_OF_MEMORY";
201 break;
202 case GL_INVALID_FRAMEBUFFER_OPERATION:
203 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
204 break;
205 case GL_TABLE_TOO_LARGE:
206 std::cerr << "GL_TABLE_TOO_LARGE";
207 break;
208 default:
209 std::cerr << error;
210 break;
211 }
José Fonseca796a3042010-11-29 14:21:06 +0000212 std::cerr << "\n";
José Fonseca3d245f42010-11-28 00:08:23 +0000213}
214'''
José Fonsecae0e61402010-11-25 15:03:23 +0000215 api = glapi.glapi
216 retracer = GlRetracer()
217 retracer.retrace_api(glapi.glapi)
José Fonseca796a3042010-11-29 14:21:06 +0000218 print r'''
José Fonseca7e329022010-11-19 17:05:18 +0000219
José Fonseca3d245f42010-11-28 00:08:23 +0000220static Trace::Parser parser;
José Fonseca082051b2010-11-23 12:00:31 +0000221
José Fonsecadf66a902010-11-29 13:24:20 +0000222static void display_noop(void) {
223}
224
José Fonseca20f35e02010-12-04 12:21:12 +0000225#include "image.hpp"
José Fonseca796a3042010-11-29 14:21:06 +0000226
227static void frame_complete(void) {
228 ++__frame;
229
José Fonseca4fe63f72011-02-08 16:01:10 +0000230 if (!__reshape_window && (__snapshot_prefix || __compare_prefix)) {
José Fonseca5780c442010-12-14 14:13:53 +0000231 Image::Image *ref = NULL;
José Fonseca4fe63f72011-02-08 16:01:10 +0000232 if (__compare_prefix) {
233 char filename[PATH_MAX];
234 snprintf(filename, sizeof filename, "%s%04u.png", __compare_prefix, __frame);
José Fonseca077142e2010-12-12 11:13:33 +0000235 ref = Image::readPNG(filename);
236 if (!ref) {
237 return;
238 }
239 if (verbosity)
240 std::cout << "Read " << filename << "\n";
241 }
242
243 Image::Image src(__window_width, __window_height, true);
244 glReadPixels(0, 0, __window_width, __window_height, GL_RGBA, GL_UNSIGNED_BYTE, src.pixels);
245
José Fonseca4fe63f72011-02-08 16:01:10 +0000246 if (__snapshot_prefix) {
247 char filename[PATH_MAX];
248 snprintf(filename, sizeof filename, "%s%04u.png", __snapshot_prefix, __frame);
José Fonseca077142e2010-12-12 11:13:33 +0000249 if (src.writePNG(filename) && verbosity) {
250 std::cout << "Wrote " << filename << "\n";
251 }
252 }
253
José Fonseca4fe63f72011-02-08 16:01:10 +0000254 if (ref) {
José Fonseca077142e2010-12-12 11:13:33 +0000255 std::cout << "Frame " << __frame << " average precision of " << src.compare(*ref) << " bits\n";
256 delete ref;
257 }
José Fonseca796a3042010-11-29 14:21:06 +0000258 }
259
260}
261
José Fonseca6f51d3b2010-11-22 19:56:19 +0000262static void display(void) {
José Fonseca3d245f42010-11-28 00:08:23 +0000263 Trace::Call *call;
José Fonseca6f51d3b2010-11-22 19:56:19 +0000264
José Fonseca3d245f42010-11-28 00:08:23 +0000265 while ((call = parser.parse_call())) {
José Fonseca83a1e152010-12-13 20:26:51 +0000266 const std::string &name = call->name();
267
268 if ((name[0] == 'w' && name[1] == 'g' && name[2] == 'l') ||
269 (name[0] == 'g' && name[1] == 'l' && name[2] == 'X')) {
270 // XXX: We ignore the majority of the OS-specific calls for now
271 if (name == "glXSwapBuffers" ||
272 name == "wglSwapBuffers") {
José Fonseca3d245f42010-11-28 00:08:23 +0000273 if (double_buffer)
274 glutSwapBuffers();
275 else
276 glFlush();
José Fonseca796a3042010-11-29 14:21:06 +0000277 frame_complete();
José Fonseca3d245f42010-11-28 00:08:23 +0000278 return;
José Fonseca83a1e152010-12-13 20:26:51 +0000279 } else {
280 continue;
José Fonseca082051b2010-11-23 12:00:31 +0000281 }
José Fonseca3d245f42010-11-28 00:08:23 +0000282 }
José Fonseca83a1e152010-12-13 20:26:51 +0000283
284 if (name == "glFlush") {
285 glFlush();
286 if (!double_buffer) {
287 frame_complete();
288 }
289 }
290
291 retrace_call(*call);
José Fonseca3d245f42010-11-28 00:08:23 +0000292 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000293
José Fonsecadf66a902010-11-29 13:24:20 +0000294 // Reached the end of trace
José Fonseca3d245f42010-11-28 00:08:23 +0000295 glFlush();
José Fonsecadf66a902010-11-29 13:24:20 +0000296
297 long long endTime = OS::GetTime();
298 float timeInterval = (endTime - __startTime) * 1.0E-6;
299
300 std::cout <<
301 "Rendered " << __frame << " frames"
302 " in " << timeInterval << " secs,"
José Fonseca796a3042010-11-29 14:21:06 +0000303 " average of " << (__frame/timeInterval) << " fps\n";
José Fonsecadf66a902010-11-29 13:24:20 +0000304
José Fonseca4c04b642011-02-01 19:35:41 +0000305 if (__wait) {
José Fonseca077142e2010-12-12 11:13:33 +0000306 glutDisplayFunc(&display_noop);
307 glutIdleFunc(NULL);
308 } else {
309 exit(0);
310 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000311}
312
313static void idle(void) {
José Fonsecacdb574a2010-11-29 12:23:35 +0000314 if (__reshape_window) {
315 // XXX: doesn't quite work
316 glutReshapeWindow(__window_width, __window_height);
317 __reshape_window = false;
318 }
José Fonseca3d245f42010-11-28 00:08:23 +0000319 glutPostRedisplay();
José Fonseca6f51d3b2010-11-22 19:56:19 +0000320}
José Fonseca7e329022010-11-19 17:05:18 +0000321
José Fonseca077142e2010-12-12 11:13:33 +0000322static void usage(void) {
323 std::cout <<
324 "Usage: glretrace [OPTION] TRACE\n"
325 "Replay TRACE.\n"
326 "\n"
José Fonseca4fe63f72011-02-08 16:01:10 +0000327 " -c PREFIX compare against snapshots\n"
José Fonseca077142e2010-12-12 11:13:33 +0000328 " -db use a double buffer visual\n"
José Fonseca4fe63f72011-02-08 16:01:10 +0000329 " -s PREFIX take snapshots\n"
José Fonseca077142e2010-12-12 11:13:33 +0000330 " -v verbose output\n";
331}
332
José Fonseca7e329022010-11-19 17:05:18 +0000333int main(int argc, char **argv)
334{
José Fonseca6f51d3b2010-11-22 19:56:19 +0000335
José Fonseca3d245f42010-11-28 00:08:23 +0000336 int i;
337 for (i = 1; i < argc; ++i) {
338 const char *arg = argv[i];
José Fonseca094cb2d2010-11-24 15:55:03 +0000339
José Fonseca3d245f42010-11-28 00:08:23 +0000340 if (arg[0] != '-') {
341 break;
342 }
José Fonseca094cb2d2010-11-24 15:55:03 +0000343
José Fonseca3d245f42010-11-28 00:08:23 +0000344 if (!strcmp(arg, "--")) {
345 break;
José Fonseca077142e2010-12-12 11:13:33 +0000346 } else if (!strcmp(arg, "-c")) {
José Fonseca4fe63f72011-02-08 16:01:10 +0000347 __compare_prefix = argv[++i];
José Fonseca796a3042010-11-29 14:21:06 +0000348 } else if (!strcmp(arg, "-db")) {
José Fonseca3d245f42010-11-28 00:08:23 +0000349 double_buffer = true;
José Fonseca077142e2010-12-12 11:13:33 +0000350 } else if (!strcmp(arg, "--help")) {
351 usage();
352 return 0;
José Fonseca796a3042010-11-29 14:21:06 +0000353 } else if (!strcmp(arg, "-s")) {
José Fonseca4fe63f72011-02-08 16:01:10 +0000354 __snapshot_prefix = argv[++i];
José Fonseca3d245f42010-11-28 00:08:23 +0000355 } else if (!strcmp(arg, "-v")) {
356 ++verbosity;
José Fonseca4c04b642011-02-01 19:35:41 +0000357 } else if (!strcmp(arg, "-w")) {
358 __wait = true;
José Fonseca3d245f42010-11-28 00:08:23 +0000359 } else {
José Fonseca796a3042010-11-29 14:21:06 +0000360 std::cerr << "error: unknown option " << arg << "\n";
José Fonseca077142e2010-12-12 11:13:33 +0000361 usage();
José Fonseca3d245f42010-11-28 00:08:23 +0000362 return 1;
363 }
364 }
José Fonseca094cb2d2010-11-24 15:55:03 +0000365
José Fonseca3d245f42010-11-28 00:08:23 +0000366 glutInit(&argc, argv);
367 glutInitWindowPosition(0, 0);
José Fonsecacdb574a2010-11-29 12:23:35 +0000368 glutInitWindowSize(__window_width, __window_height);
José Fonseca3d245f42010-11-28 00:08:23 +0000369 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
370 glutCreateWindow(argv[0]);
José Fonsecafeb5d992010-11-25 17:14:18 +0000371
José Fonseca3d245f42010-11-28 00:08:23 +0000372 glutDisplayFunc(&display);
373 glutIdleFunc(&idle);
José Fonsecafeb5d992010-11-25 17:14:18 +0000374
José Fonseca3d245f42010-11-28 00:08:23 +0000375 for (GLuint h = 0; h < 1024; ++h) {
376 __list_map[h] = h;
377 }
José Fonsecafeb5d992010-11-25 17:14:18 +0000378
José Fonseca3d245f42010-11-28 00:08:23 +0000379 for ( ; i < argc; ++i) {
380 if (parser.open(argv[i])) {
José Fonsecadf66a902010-11-29 13:24:20 +0000381 __startTime = OS::GetTime();
José Fonseca3d245f42010-11-28 00:08:23 +0000382 glutMainLoop();
383 parser.close();
384 }
385 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000386
José Fonseca3d245f42010-11-28 00:08:23 +0000387 return 0;
José Fonseca7e329022010-11-19 17:05:18 +0000388}
389
José Fonseca3d245f42010-11-28 00:08:23 +0000390'''