blob: ff22e996acc4e6fc09fe4070a58c3b266b606beb [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é Fonseca4441baf2010-11-25 19:55:27 +000034 def filter_function(self, function):
35 return function.name not in [
36 "glNewBufferRegion",
37 "glDeleteBufferRegion",
38 "glReadBufferRegion",
39 "glDrawBufferRegion",
40 "glBufferRegionEnabled",
41 ]
42
José Fonseca3d245f42010-11-28 00:08:23 +000043 def retrace_function(self, function):
44 Retracer.retrace_function(self, function)
45
José Fonsecafa15d332010-11-25 20:22:39 +000046 def call_function(self, function):
47 if function.name in ("glDrawArrays", "glDrawElements", "glDrawRangeElements", "glMultiDrawElements"):
48 print ' GLint __array_buffer = 0;'
49 print ' glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);'
50 print ' if (!__array_buffer) {'
51 self.fail_function(function)
52 print ' }'
53
José Fonseca3d245f42010-11-28 00:08:23 +000054 if function.name == "glEnd":
55 print ' insideGlBeginEnd = false;'
José Fonsecafa15d332010-11-25 20:22:39 +000056 Retracer.call_function(self, function)
José Fonseca3d245f42010-11-28 00:08:23 +000057 if function.name == "glBegin":
58 print ' insideGlBeginEnd = true;'
59 else:
60 # glGetError is not allowed inside glBegin/glEnd
61 print ' checkGlError();'
62
José Fonsecafa15d332010-11-25 20:22:39 +000063
José Fonsecadacd8dd2010-11-25 17:50:26 +000064 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
65 if function.name in [
José Fonseca833d14a2010-11-25 18:37:38 +000066 "glColorPointer",
67 "glEdgeFlagPointer",
José Fonsecadacd8dd2010-11-25 17:50:26 +000068 "glIndexPointer",
69 "glNormalPointer",
70 "glTexCoordPointer",
71 "glVertexPointer",
72 "glFogCoordPointer",
73 "glSecondaryColorPointer",
74 "glVertexAttribPointer",
75 ] and arg.name == 'pointer':
76 self.extract_pointer(function, arg, arg_type, lvalue, rvalue)
77 else:
78 Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
79
80 def extract_pointer(self, function, arg, arg_type, lvalue, rvalue):
81 print ' if (dynamic_cast<Trace::Null *>(&%s)) {' % rvalue
82 print ' %s = 0;' % (lvalue)
83 print ' } else {'
84 print ' %s = (%s)(uintptr_t)(%s);' % (lvalue, arg_type, rvalue)
85 print ' }'
José Fonsecae6a50bd2010-11-24 10:12:22 +000086
87
José Fonseca7e329022010-11-19 17:05:18 +000088if __name__ == '__main__':
89 print
José Fonseca7e329022010-11-19 17:05:18 +000090 print '#include <string.h>'
José Fonseca3d245f42010-11-28 00:08:23 +000091 print '#include <iostream>'
José Fonseca094cb2d2010-11-24 15:55:03 +000092 print
93 print '#ifdef WIN32'
94 print '#include <windows.h>'
95 print '#endif'
96 print
José Fonseca7e329022010-11-19 17:05:18 +000097 print '#include <GL/glew.h>'
98 print '#include <GL/glut.h>'
99 print
José Fonsecafeb5d992010-11-25 17:14:18 +0000100 print 'static bool double_buffer = false;'
José Fonseca3d245f42010-11-28 00:08:23 +0000101 print 'static bool insideGlBeginEnd = false;'
José Fonsecafeb5d992010-11-25 17:14:18 +0000102 print
José Fonseca3d245f42010-11-28 00:08:23 +0000103 print '''
104static void
105checkGlError(void) {
106 if (insideGlBeginEnd) {
107 return;
108 }
109
110 GLenum error = glGetError();
111 if (error == GL_NO_ERROR) {
112 return;
113 }
114
115 std::cerr << "warning: glGetError() = ";
116 switch (error) {
117 case GL_INVALID_ENUM:
118 std::cerr << "GL_INVALID_ENUM";
119 break;
120 case GL_INVALID_VALUE:
121 std::cerr << "GL_INVALID_VALUE";
122 break;
123 case GL_INVALID_OPERATION:
124 std::cerr << "GL_INVALID_OPERATION";
125 break;
126 case GL_STACK_OVERFLOW:
127 std::cerr << "GL_STACK_OVERFLOW";
128 break;
129 case GL_STACK_UNDERFLOW:
130 std::cerr << "GL_STACK_UNDERFLOW";
131 break;
132 case GL_OUT_OF_MEMORY:
133 std::cerr << "GL_OUT_OF_MEMORY";
134 break;
135 case GL_INVALID_FRAMEBUFFER_OPERATION:
136 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
137 break;
138 case GL_TABLE_TOO_LARGE:
139 std::cerr << "GL_TABLE_TOO_LARGE";
140 break;
141 default:
142 std::cerr << error;
143 break;
144 }
145 std::cerr << "\\n";
146}
147'''
José Fonsecae0e61402010-11-25 15:03:23 +0000148 api = glapi.glapi
149 retracer = GlRetracer()
150 retracer.retrace_api(glapi.glapi)
José Fonseca7e329022010-11-19 17:05:18 +0000151 print '''
152
José Fonseca3d245f42010-11-28 00:08:23 +0000153static Trace::Parser parser;
José Fonseca082051b2010-11-23 12:00:31 +0000154
José Fonseca6f51d3b2010-11-22 19:56:19 +0000155static void display(void) {
José Fonseca3d245f42010-11-28 00:08:23 +0000156 Trace::Call *call;
José Fonseca6f51d3b2010-11-22 19:56:19 +0000157
José Fonseca3d245f42010-11-28 00:08:23 +0000158 while ((call = parser.parse_call())) {
159 if (call->name() == "glFlush") {
José Fonsecafeb5d992010-11-25 17:14:18 +0000160 glFlush();
José Fonseca3d245f42010-11-28 00:08:23 +0000161 return;
162 }
163
164 if (!retrace_call(*call)) {
165 if (call->name() == "glXSwapBuffers" ||
166 call->name() == "wglSwapBuffers") {
167 if (double_buffer)
168 glutSwapBuffers();
169 else
170 glFlush();
171 return;
José Fonseca082051b2010-11-23 12:00:31 +0000172 }
José Fonseca3d245f42010-11-28 00:08:23 +0000173 }
174 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000175
José Fonseca3d245f42010-11-28 00:08:23 +0000176 glFlush();
177 glutIdleFunc(NULL);
José Fonseca6f51d3b2010-11-22 19:56:19 +0000178}
179
180static void idle(void) {
José Fonseca3d245f42010-11-28 00:08:23 +0000181 glutPostRedisplay();
José Fonseca6f51d3b2010-11-22 19:56:19 +0000182}
José Fonseca7e329022010-11-19 17:05:18 +0000183
184int main(int argc, char **argv)
185{
José Fonseca6f51d3b2010-11-22 19:56:19 +0000186
José Fonseca3d245f42010-11-28 00:08:23 +0000187 int i;
188 for (i = 1; i < argc; ++i) {
189 const char *arg = argv[i];
José Fonseca094cb2d2010-11-24 15:55:03 +0000190
José Fonseca3d245f42010-11-28 00:08:23 +0000191 if (arg[0] != '-') {
192 break;
193 }
José Fonseca094cb2d2010-11-24 15:55:03 +0000194
José Fonseca3d245f42010-11-28 00:08:23 +0000195 if (!strcmp(arg, "--")) {
196 break;
197 }
198 else if (!strcmp(arg, "-db")) {
199 double_buffer = true;
200 } else if (!strcmp(arg, "-v")) {
201 ++verbosity;
202 } else {
203 std::cerr << "error: unknown option " << arg << "\\n";
204 return 1;
205 }
206 }
José Fonseca094cb2d2010-11-24 15:55:03 +0000207
José Fonseca3d245f42010-11-28 00:08:23 +0000208 glutInit(&argc, argv);
209 glutInitWindowPosition(0, 0);
210 glutInitWindowSize(800, 600);
211 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
212 glutCreateWindow(argv[0]);
213 glewInit();
José Fonsecafeb5d992010-11-25 17:14:18 +0000214
José Fonseca3d245f42010-11-28 00:08:23 +0000215 glutDisplayFunc(&display);
216 glutIdleFunc(&idle);
José Fonsecafeb5d992010-11-25 17:14:18 +0000217
José Fonseca3d245f42010-11-28 00:08:23 +0000218 for (GLuint h = 0; h < 1024; ++h) {
219 __list_map[h] = h;
220 }
José Fonsecafeb5d992010-11-25 17:14:18 +0000221
José Fonseca3d245f42010-11-28 00:08:23 +0000222 for ( ; i < argc; ++i) {
223 if (parser.open(argv[i])) {
224 glutMainLoop();
225 parser.close();
226 }
227 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000228
José Fonseca3d245f42010-11-28 00:08:23 +0000229 return 0;
José Fonseca7e329022010-11-19 17:05:18 +0000230}
231
José Fonseca3d245f42010-11-28 00:08:23 +0000232'''