blob: 63b21fe7ec95e70c06dc0d0b66d97e1de1065ae9 [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",
José Fonseca99221832011-03-22 22:15:46 +000064 #"glMultiDrawElements",
65 #"glMultiDrawElementsBaseVertex",
66 #"glMultiDrawElementsEXT",
67 #"glMultiModeDrawElementsIBM",
José Fonseca8caf2c82010-11-30 12:09:12 +000068 ])
69
José Fonsecafa15d332010-11-25 20:22:39 +000070 def call_function(self, function):
José Fonseca99221832011-03-22 22:15:46 +000071 print ' if (Trace::Parser::version < 1) {'
72
73 if function.name in self.draw_array_function_names or \
74 function.name in self.draw_elements_function_names:
José Fonsecafa15d332010-11-25 20:22:39 +000075 print ' GLint __array_buffer = 0;'
76 print ' glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);'
77 print ' if (!__array_buffer) {'
78 self.fail_function(function)
79 print ' }'
80
José Fonseca8caf2c82010-11-30 12:09:12 +000081 if function.name in self.draw_elements_function_names:
82 print ' GLint __element_array_buffer = 0;'
83 print ' glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);'
84 print ' if (!__element_array_buffer) {'
85 self.fail_function(function)
86 print ' }'
87
José Fonseca99221832011-03-22 22:15:46 +000088 print ' }'
89
José Fonsecacdb574a2010-11-29 12:23:35 +000090 if function.name == "glViewport":
91 print ' if (x + width > __window_width) {'
92 print ' __window_width = x + width;'
93 print ' __reshape_window = true;'
94 print ' }'
95 print ' if (y + height > __window_height) {'
96 print ' __window_height = y + height;'
97 print ' __reshape_window = true;'
98 print ' }'
99
José Fonseca3d245f42010-11-28 00:08:23 +0000100 if function.name == "glEnd":
101 print ' insideGlBeginEnd = false;'
José Fonsecacdb574a2010-11-29 12:23:35 +0000102
José Fonsecafa15d332010-11-25 20:22:39 +0000103 Retracer.call_function(self, function)
José Fonsecacdb574a2010-11-29 12:23:35 +0000104
José Fonseca3d245f42010-11-28 00:08:23 +0000105 if function.name == "glBegin":
106 print ' insideGlBeginEnd = true;'
107 else:
108 # glGetError is not allowed inside glBegin/glEnd
109 print ' checkGlError();'
110
José Fonseca8caf2c82010-11-30 12:09:12 +0000111 pointer_function_names = set([
112 "glColorPointer",
José Fonseca14c21bc2011-02-20 23:32:22 +0000113 #"glColorPointerEXT",
José Fonseca8caf2c82010-11-30 12:09:12 +0000114 "glEdgeFlagPointer",
José Fonseca14c21bc2011-02-20 23:32:22 +0000115 #"glEdgeFlagPointerEXT",
José Fonseca8caf2c82010-11-30 12:09:12 +0000116 "glFogCoordPointer",
José Fonseca14c21bc2011-02-20 23:32:22 +0000117 #"glFogCoordPointerEXT",
José Fonseca8caf2c82010-11-30 12:09:12 +0000118 "glIndexPointer",
José Fonseca14c21bc2011-02-20 23:32:22 +0000119 #"glIndexPointerEXT",
120 #"glMatrixIndexPointerARB",
José Fonseca8caf2c82010-11-30 12:09:12 +0000121 "glNormalPointer",
José Fonseca14c21bc2011-02-20 23:32:22 +0000122 #"glNormalPointerEXT",
José Fonseca8caf2c82010-11-30 12:09:12 +0000123 "glSecondaryColorPointer",
José Fonseca14c21bc2011-02-20 23:32:22 +0000124 #"glSecondaryColorPointerEXT",
José Fonseca8caf2c82010-11-30 12:09:12 +0000125 "glTexCoordPointer",
José Fonseca14c21bc2011-02-20 23:32:22 +0000126 #"glTexCoordPointerEXT",
127 #"glVertexAttribLPointer",
128 #"glVertexAttribPointer",
129 #"glVertexAttribPointerARB",
130 #"glVertexAttribPointerNV",
José Fonseca8caf2c82010-11-30 12:09:12 +0000131 "glVertexPointer",
José Fonseca14c21bc2011-02-20 23:32:22 +0000132 #"glVertexPointerEXT",
José Fonseca8caf2c82010-11-30 12:09:12 +0000133 ])
José Fonsecafa15d332010-11-25 20:22:39 +0000134
José Fonsecadacd8dd2010-11-25 17:50:26 +0000135 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
José Fonseca14c21bc2011-02-20 23:32:22 +0000136 if function.name in self.pointer_function_names and arg.name == 'pointer':
José Fonseca99221832011-03-22 22:15:46 +0000137 print ' %s = %s.blob();' % (lvalue, rvalue)
José Fonseca14c21bc2011-02-20 23:32:22 +0000138 return
139
140 if function.name in self.draw_elements_function_names and arg.name == 'indices':
José Fonseca99221832011-03-22 22:15:46 +0000141 print ' %s = %s.blob();' % (lvalue, rvalue)
José Fonseca8a844ae2010-12-06 18:50:52 +0000142 return
José Fonsecadacd8dd2010-11-25 17:50:26 +0000143
José Fonseca8a844ae2010-12-06 18:50:52 +0000144 if function.name.startswith('glUniform') and function.args[0].name == arg.name == 'location':
145 print ' GLint program = -1;'
146 print ' glGetIntegerv(GL_CURRENT_PROGRAM, &program);'
147
148 Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
José Fonsecae6a50bd2010-11-24 10:12:22 +0000149
150
José Fonseca7e329022010-11-19 17:05:18 +0000151if __name__ == '__main__':
José Fonseca796a3042010-11-29 14:21:06 +0000152 print r'''
José Fonsecadf66a902010-11-29 13:24:20 +0000153#include <string.h>
José Fonsecae0081492010-12-04 13:24:17 +0000154#include <stdio.h>
José Fonsecadf66a902010-11-29 13:24:20 +0000155#include <iostream>
156
José Fonseca68ec4122011-02-20 11:25:25 +0000157#define RETRACE
158
José Fonsecadf66a902010-11-29 13:24:20 +0000159#include "glproc.hpp"
160#include <GL/glut.h>
161
162static bool double_buffer = false;
163static bool insideGlBeginEnd = false;
164
165static int __window_width = 256, __window_height = 256;
166bool __reshape_window = false;
167
168unsigned __frame = 0;
169long long __startTime = 0;
José Fonseca4c04b642011-02-01 19:35:41 +0000170bool __wait = false;
José Fonseca796a3042010-11-29 14:21:06 +0000171
José Fonsecaecf2f742011-02-09 14:51:07 +0000172bool __benchmark = false;
José Fonseca87f19712011-02-09 14:49:50 +0000173const char *__compare_prefix = NULL;
174const char *__snapshot_prefix = NULL;
José Fonseca7fe1dc52010-12-13 20:18:39 +0000175
José Fonsecadf66a902010-11-29 13:24:20 +0000176
José Fonseca3d245f42010-11-28 00:08:23 +0000177static void
178checkGlError(void) {
José Fonsecaecf2f742011-02-09 14:51:07 +0000179 if (__benchmark || insideGlBeginEnd) {
José Fonseca3d245f42010-11-28 00:08:23 +0000180 return;
181 }
182
183 GLenum error = glGetError();
184 if (error == GL_NO_ERROR) {
185 return;
186 }
187
188 std::cerr << "warning: glGetError() = ";
189 switch (error) {
190 case GL_INVALID_ENUM:
191 std::cerr << "GL_INVALID_ENUM";
192 break;
193 case GL_INVALID_VALUE:
194 std::cerr << "GL_INVALID_VALUE";
195 break;
196 case GL_INVALID_OPERATION:
197 std::cerr << "GL_INVALID_OPERATION";
198 break;
199 case GL_STACK_OVERFLOW:
200 std::cerr << "GL_STACK_OVERFLOW";
201 break;
202 case GL_STACK_UNDERFLOW:
203 std::cerr << "GL_STACK_UNDERFLOW";
204 break;
205 case GL_OUT_OF_MEMORY:
206 std::cerr << "GL_OUT_OF_MEMORY";
207 break;
208 case GL_INVALID_FRAMEBUFFER_OPERATION:
209 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
210 break;
211 case GL_TABLE_TOO_LARGE:
212 std::cerr << "GL_TABLE_TOO_LARGE";
213 break;
214 default:
215 std::cerr << error;
216 break;
217 }
José Fonseca796a3042010-11-29 14:21:06 +0000218 std::cerr << "\n";
José Fonseca3d245f42010-11-28 00:08:23 +0000219}
220'''
José Fonsecae0e61402010-11-25 15:03:23 +0000221 api = glapi.glapi
222 retracer = GlRetracer()
223 retracer.retrace_api(glapi.glapi)
José Fonseca796a3042010-11-29 14:21:06 +0000224 print r'''
José Fonseca7e329022010-11-19 17:05:18 +0000225
José Fonseca3d245f42010-11-28 00:08:23 +0000226static Trace::Parser parser;
José Fonseca082051b2010-11-23 12:00:31 +0000227
José Fonsecadf66a902010-11-29 13:24:20 +0000228static void display_noop(void) {
229}
230
José Fonseca20f35e02010-12-04 12:21:12 +0000231#include "image.hpp"
José Fonseca796a3042010-11-29 14:21:06 +0000232
José Fonseca1ff16732011-02-09 15:58:51 +0000233static void snapshot(Image::Image &image) {
234 GLint drawbuffer = double_buffer ? GL_BACK : GL_FRONT;
235 GLint readbuffer = double_buffer ? GL_BACK : GL_FRONT;
236 glGetIntegerv(GL_READ_BUFFER, &drawbuffer);
237 glGetIntegerv(GL_READ_BUFFER, &readbuffer);
238 glReadBuffer(drawbuffer);
239 glReadPixels(0, 0, image.width, image.height, GL_RGBA, GL_UNSIGNED_BYTE, image.pixels);
240 checkGlError();
241 glReadBuffer(readbuffer);
242}
243
José Fonseca796a3042010-11-29 14:21:06 +0000244static void frame_complete(void) {
245 ++__frame;
246
José Fonseca4fe63f72011-02-08 16:01:10 +0000247 if (!__reshape_window && (__snapshot_prefix || __compare_prefix)) {
José Fonseca5780c442010-12-14 14:13:53 +0000248 Image::Image *ref = NULL;
José Fonseca4fe63f72011-02-08 16:01:10 +0000249 if (__compare_prefix) {
250 char filename[PATH_MAX];
251 snprintf(filename, sizeof filename, "%s%04u.png", __compare_prefix, __frame);
José Fonseca077142e2010-12-12 11:13:33 +0000252 ref = Image::readPNG(filename);
253 if (!ref) {
254 return;
255 }
José Fonsecaecf2f742011-02-09 14:51:07 +0000256 if (verbosity >= 0)
José Fonseca077142e2010-12-12 11:13:33 +0000257 std::cout << "Read " << filename << "\n";
258 }
259
260 Image::Image src(__window_width, __window_height, true);
José Fonseca1ff16732011-02-09 15:58:51 +0000261 snapshot(src);
José Fonseca077142e2010-12-12 11:13:33 +0000262
José Fonseca4fe63f72011-02-08 16:01:10 +0000263 if (__snapshot_prefix) {
264 char filename[PATH_MAX];
265 snprintf(filename, sizeof filename, "%s%04u.png", __snapshot_prefix, __frame);
José Fonsecaecf2f742011-02-09 14:51:07 +0000266 if (src.writePNG(filename) && verbosity >= 0) {
José Fonseca077142e2010-12-12 11:13:33 +0000267 std::cout << "Wrote " << filename << "\n";
268 }
269 }
270
José Fonseca4fe63f72011-02-08 16:01:10 +0000271 if (ref) {
José Fonseca077142e2010-12-12 11:13:33 +0000272 std::cout << "Frame " << __frame << " average precision of " << src.compare(*ref) << " bits\n";
273 delete ref;
274 }
José Fonseca796a3042010-11-29 14:21:06 +0000275 }
276
277}
278
José Fonseca6f51d3b2010-11-22 19:56:19 +0000279static void display(void) {
José Fonseca3d245f42010-11-28 00:08:23 +0000280 Trace::Call *call;
José Fonseca6f51d3b2010-11-22 19:56:19 +0000281
José Fonseca3d245f42010-11-28 00:08:23 +0000282 while ((call = parser.parse_call())) {
José Fonseca83a1e152010-12-13 20:26:51 +0000283 const std::string &name = call->name();
284
285 if ((name[0] == 'w' && name[1] == 'g' && name[2] == 'l') ||
286 (name[0] == 'g' && name[1] == 'l' && name[2] == 'X')) {
287 // XXX: We ignore the majority of the OS-specific calls for now
288 if (name == "glXSwapBuffers" ||
289 name == "wglSwapBuffers") {
José Fonseca1ff16732011-02-09 15:58:51 +0000290 frame_complete();
José Fonseca3d245f42010-11-28 00:08:23 +0000291 if (double_buffer)
292 glutSwapBuffers();
293 else
294 glFlush();
295 return;
José Fonseca83a1e152010-12-13 20:26:51 +0000296 } else {
297 continue;
José Fonseca082051b2010-11-23 12:00:31 +0000298 }
José Fonseca3d245f42010-11-28 00:08:23 +0000299 }
José Fonseca83a1e152010-12-13 20:26:51 +0000300
301 if (name == "glFlush") {
José Fonseca83a1e152010-12-13 20:26:51 +0000302 if (!double_buffer) {
303 frame_complete();
304 }
José Fonseca1ff16732011-02-09 15:58:51 +0000305 glFlush();
José Fonseca83a1e152010-12-13 20:26:51 +0000306 }
307
308 retrace_call(*call);
José Fonsecaae9668b2011-02-09 15:15:08 +0000309
310 delete call;
José Fonseca3d245f42010-11-28 00:08:23 +0000311 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000312
José Fonsecadf66a902010-11-29 13:24:20 +0000313 // Reached the end of trace
José Fonseca3d245f42010-11-28 00:08:23 +0000314 glFlush();
José Fonsecadf66a902010-11-29 13:24:20 +0000315
316 long long endTime = OS::GetTime();
317 float timeInterval = (endTime - __startTime) * 1.0E-6;
318
319 std::cout <<
320 "Rendered " << __frame << " frames"
321 " in " << timeInterval << " secs,"
José Fonseca796a3042010-11-29 14:21:06 +0000322 " average of " << (__frame/timeInterval) << " fps\n";
José Fonsecadf66a902010-11-29 13:24:20 +0000323
José Fonseca4c04b642011-02-01 19:35:41 +0000324 if (__wait) {
José Fonseca077142e2010-12-12 11:13:33 +0000325 glutDisplayFunc(&display_noop);
326 glutIdleFunc(NULL);
327 } else {
328 exit(0);
329 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000330}
331
332static void idle(void) {
José Fonsecacdb574a2010-11-29 12:23:35 +0000333 if (__reshape_window) {
334 // XXX: doesn't quite work
335 glutReshapeWindow(__window_width, __window_height);
336 __reshape_window = false;
337 }
José Fonseca3d245f42010-11-28 00:08:23 +0000338 glutPostRedisplay();
José Fonseca6f51d3b2010-11-22 19:56:19 +0000339}
José Fonseca7e329022010-11-19 17:05:18 +0000340
José Fonseca077142e2010-12-12 11:13:33 +0000341static void usage(void) {
342 std::cout <<
343 "Usage: glretrace [OPTION] TRACE\n"
344 "Replay TRACE.\n"
345 "\n"
José Fonsecaecf2f742011-02-09 14:51:07 +0000346 " -b benchmark (no glgeterror; no messages)\n"
José Fonseca4fe63f72011-02-08 16:01:10 +0000347 " -c PREFIX compare against snapshots\n"
José Fonseca077142e2010-12-12 11:13:33 +0000348 " -db use a double buffer visual\n"
José Fonseca4fe63f72011-02-08 16:01:10 +0000349 " -s PREFIX take snapshots\n"
José Fonsecafa0162d2011-03-23 13:21:01 +0000350 " -v verbose output\n"
351 " -w wait on final frame\n";
José Fonseca077142e2010-12-12 11:13:33 +0000352}
353
José Fonseca7e329022010-11-19 17:05:18 +0000354int main(int argc, char **argv)
355{
José Fonseca6f51d3b2010-11-22 19:56:19 +0000356
José Fonseca3d245f42010-11-28 00:08:23 +0000357 int i;
358 for (i = 1; i < argc; ++i) {
359 const char *arg = argv[i];
José Fonseca094cb2d2010-11-24 15:55:03 +0000360
José Fonseca3d245f42010-11-28 00:08:23 +0000361 if (arg[0] != '-') {
362 break;
363 }
José Fonseca094cb2d2010-11-24 15:55:03 +0000364
José Fonseca3d245f42010-11-28 00:08:23 +0000365 if (!strcmp(arg, "--")) {
366 break;
José Fonsecaecf2f742011-02-09 14:51:07 +0000367 } else if (!strcmp(arg, "-b")) {
368 __benchmark = true;
369 --verbosity;
José Fonseca077142e2010-12-12 11:13:33 +0000370 } else if (!strcmp(arg, "-c")) {
José Fonseca4fe63f72011-02-08 16:01:10 +0000371 __compare_prefix = argv[++i];
José Fonseca796a3042010-11-29 14:21:06 +0000372 } else if (!strcmp(arg, "-db")) {
José Fonseca3d245f42010-11-28 00:08:23 +0000373 double_buffer = true;
José Fonseca077142e2010-12-12 11:13:33 +0000374 } else if (!strcmp(arg, "--help")) {
375 usage();
376 return 0;
José Fonseca796a3042010-11-29 14:21:06 +0000377 } else if (!strcmp(arg, "-s")) {
José Fonseca4fe63f72011-02-08 16:01:10 +0000378 __snapshot_prefix = argv[++i];
José Fonseca3d245f42010-11-28 00:08:23 +0000379 } else if (!strcmp(arg, "-v")) {
380 ++verbosity;
José Fonseca4c04b642011-02-01 19:35:41 +0000381 } else if (!strcmp(arg, "-w")) {
382 __wait = true;
José Fonseca3d245f42010-11-28 00:08:23 +0000383 } else {
José Fonseca796a3042010-11-29 14:21:06 +0000384 std::cerr << "error: unknown option " << arg << "\n";
José Fonseca077142e2010-12-12 11:13:33 +0000385 usage();
José Fonseca3d245f42010-11-28 00:08:23 +0000386 return 1;
387 }
388 }
José Fonseca094cb2d2010-11-24 15:55:03 +0000389
José Fonseca3d245f42010-11-28 00:08:23 +0000390 glutInit(&argc, argv);
391 glutInitWindowPosition(0, 0);
José Fonsecacdb574a2010-11-29 12:23:35 +0000392 glutInitWindowSize(__window_width, __window_height);
José Fonseca3d245f42010-11-28 00:08:23 +0000393 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
394 glutCreateWindow(argv[0]);
José Fonsecafeb5d992010-11-25 17:14:18 +0000395
José Fonseca3d245f42010-11-28 00:08:23 +0000396 glutDisplayFunc(&display);
397 glutIdleFunc(&idle);
José Fonsecafeb5d992010-11-25 17:14:18 +0000398
José Fonseca3d245f42010-11-28 00:08:23 +0000399 for (GLuint h = 0; h < 1024; ++h) {
400 __list_map[h] = h;
401 }
José Fonsecafeb5d992010-11-25 17:14:18 +0000402
José Fonseca3d245f42010-11-28 00:08:23 +0000403 for ( ; i < argc; ++i) {
404 if (parser.open(argv[i])) {
José Fonsecadf66a902010-11-29 13:24:20 +0000405 __startTime = OS::GetTime();
José Fonseca3d245f42010-11-28 00:08:23 +0000406 glutMainLoop();
407 parser.close();
408 }
409 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000410
José Fonseca3d245f42010-11-28 00:08:23 +0000411 return 0;
José Fonseca7e329022010-11-19 17:05:18 +0000412}
413
José Fonseca3d245f42010-11-28 00:08:23 +0000414'''