blob: c5ec3aee463b8638b733e743a680707ef4d34873 [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é Fonseca9796b842010-11-25 11:44:50 +000027import stdapi
José Fonseca8fbdd3a2010-11-23 20:55:07 +000028import glapi
José Fonsecadacd8dd2010-11-25 17:50:26 +000029from retrace import Retracer
José Fonseca7e329022010-11-19 17:05:18 +000030
31
José Fonsecadacd8dd2010-11-25 17:50:26 +000032class GlRetracer(Retracer):
José Fonsecac9edb832010-11-20 09:03:10 +000033
José Fonseca3d245f42010-11-28 00:08:23 +000034 def retrace_function(self, function):
35 Retracer.retrace_function(self, function)
36
José Fonsecafa15d332010-11-25 20:22:39 +000037 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
José Fonsecacdb574a2010-11-29 12:23:35 +000045 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
José Fonseca3d245f42010-11-28 00:08:23 +000055 if function.name == "glEnd":
56 print ' insideGlBeginEnd = false;'
José Fonsecacdb574a2010-11-29 12:23:35 +000057
José Fonsecafa15d332010-11-25 20:22:39 +000058 Retracer.call_function(self, function)
José Fonsecacdb574a2010-11-29 12:23:35 +000059
José Fonseca3d245f42010-11-28 00:08:23 +000060 if function.name == "glBegin":
61 print ' insideGlBeginEnd = true;'
62 else:
63 # glGetError is not allowed inside glBegin/glEnd
64 print ' checkGlError();'
65
José Fonsecafa15d332010-11-25 20:22:39 +000066
José Fonsecadacd8dd2010-11-25 17:50:26 +000067 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
68 if function.name in [
José Fonseca833d14a2010-11-25 18:37:38 +000069 "glColorPointer",
70 "glEdgeFlagPointer",
José Fonsecadacd8dd2010-11-25 17:50:26 +000071 "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 ' }'
José Fonsecae6a50bd2010-11-24 10:12:22 +000089
90
José Fonseca7e329022010-11-19 17:05:18 +000091if __name__ == '__main__':
92 print
José Fonseca7e329022010-11-19 17:05:18 +000093 print '#include <string.h>'
José Fonseca3d245f42010-11-28 00:08:23 +000094 print '#include <iostream>'
José Fonseca094cb2d2010-11-24 15:55:03 +000095 print
José Fonsecac6b49ce2010-11-28 16:43:04 +000096 print '#include "glproc.hpp"'
José Fonseca7e329022010-11-19 17:05:18 +000097 print '#include <GL/glut.h>'
98 print
José Fonsecafeb5d992010-11-25 17:14:18 +000099 print 'static bool double_buffer = false;'
José Fonseca3d245f42010-11-28 00:08:23 +0000100 print 'static bool insideGlBeginEnd = false;'
José Fonsecacdb574a2010-11-29 12:23:35 +0000101 print 'static int __window_width = 256, __window_height = 256;'
102 print 'bool __reshape_window = false;'
José Fonsecafeb5d992010-11-25 17:14:18 +0000103 print
José Fonseca3d245f42010-11-28 00:08:23 +0000104 print '''
105static void
106checkGlError(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'''
José Fonsecae0e61402010-11-25 15:03:23 +0000149 api = glapi.glapi
150 retracer = GlRetracer()
151 retracer.retrace_api(glapi.glapi)
José Fonseca7e329022010-11-19 17:05:18 +0000152 print '''
153
José Fonseca3d245f42010-11-28 00:08:23 +0000154static Trace::Parser parser;
José Fonseca082051b2010-11-23 12:00:31 +0000155
José Fonseca6f51d3b2010-11-22 19:56:19 +0000156static void display(void) {
José Fonseca3d245f42010-11-28 00:08:23 +0000157 Trace::Call *call;
José Fonseca6f51d3b2010-11-22 19:56:19 +0000158
José Fonseca3d245f42010-11-28 00:08:23 +0000159 while ((call = parser.parse_call())) {
160 if (call->name() == "glFlush") {
José Fonsecafeb5d992010-11-25 17:14:18 +0000161 glFlush();
José Fonseca3d245f42010-11-28 00:08:23 +0000162 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;
José Fonseca082051b2010-11-23 12:00:31 +0000173 }
José Fonseca3d245f42010-11-28 00:08:23 +0000174 }
175 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000176
José Fonseca3d245f42010-11-28 00:08:23 +0000177 glFlush();
178 glutIdleFunc(NULL);
José Fonseca6f51d3b2010-11-22 19:56:19 +0000179}
180
181static void idle(void) {
José Fonsecacdb574a2010-11-29 12:23:35 +0000182 if (__reshape_window) {
183 // XXX: doesn't quite work
184 glutReshapeWindow(__window_width, __window_height);
185 __reshape_window = false;
186 }
José Fonseca3d245f42010-11-28 00:08:23 +0000187 glutPostRedisplay();
José Fonseca6f51d3b2010-11-22 19:56:19 +0000188}
José Fonseca7e329022010-11-19 17:05:18 +0000189
190int main(int argc, char **argv)
191{
José Fonseca6f51d3b2010-11-22 19:56:19 +0000192
José Fonseca3d245f42010-11-28 00:08:23 +0000193 int i;
194 for (i = 1; i < argc; ++i) {
195 const char *arg = argv[i];
José Fonseca094cb2d2010-11-24 15:55:03 +0000196
José Fonseca3d245f42010-11-28 00:08:23 +0000197 if (arg[0] != '-') {
198 break;
199 }
José Fonseca094cb2d2010-11-24 15:55:03 +0000200
José Fonseca3d245f42010-11-28 00:08:23 +0000201 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 }
José Fonseca094cb2d2010-11-24 15:55:03 +0000213
José Fonseca3d245f42010-11-28 00:08:23 +0000214 glutInit(&argc, argv);
215 glutInitWindowPosition(0, 0);
José Fonsecacdb574a2010-11-29 12:23:35 +0000216 glutInitWindowSize(__window_width, __window_height);
José Fonseca3d245f42010-11-28 00:08:23 +0000217 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
218 glutCreateWindow(argv[0]);
José Fonsecafeb5d992010-11-25 17:14:18 +0000219
José Fonseca3d245f42010-11-28 00:08:23 +0000220 glutDisplayFunc(&display);
221 glutIdleFunc(&idle);
José Fonsecafeb5d992010-11-25 17:14:18 +0000222
José Fonseca3d245f42010-11-28 00:08:23 +0000223 for (GLuint h = 0; h < 1024; ++h) {
224 __list_map[h] = h;
225 }
José Fonsecafeb5d992010-11-25 17:14:18 +0000226
José Fonseca3d245f42010-11-28 00:08:23 +0000227 for ( ; i < argc; ++i) {
228 if (parser.open(argv[i])) {
229 glutMainLoop();
230 parser.close();
231 }
232 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000233
José Fonseca3d245f42010-11-28 00:08:23 +0000234 return 0;
José Fonseca7e329022010-11-19 17:05:18 +0000235}
236
José Fonseca3d245f42010-11-28 00:08:23 +0000237'''