blob: 5442003ef51fc8198dbb994d1ffcb616a0fb8c9b [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é Fonseca3d245f42010-11-28 00:08:23 +000045 if function.name == "glEnd":
46 print ' insideGlBeginEnd = false;'
José Fonsecafa15d332010-11-25 20:22:39 +000047 Retracer.call_function(self, function)
José Fonseca3d245f42010-11-28 00:08:23 +000048 if function.name == "glBegin":
49 print ' insideGlBeginEnd = true;'
50 else:
51 # glGetError is not allowed inside glBegin/glEnd
52 print ' checkGlError();'
53
José Fonsecafa15d332010-11-25 20:22:39 +000054
José Fonsecadacd8dd2010-11-25 17:50:26 +000055 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
56 if function.name in [
José Fonseca833d14a2010-11-25 18:37:38 +000057 "glColorPointer",
58 "glEdgeFlagPointer",
José Fonsecadacd8dd2010-11-25 17:50:26 +000059 "glIndexPointer",
60 "glNormalPointer",
61 "glTexCoordPointer",
62 "glVertexPointer",
63 "glFogCoordPointer",
64 "glSecondaryColorPointer",
65 "glVertexAttribPointer",
66 ] and arg.name == 'pointer':
67 self.extract_pointer(function, arg, arg_type, lvalue, rvalue)
68 else:
69 Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
70
71 def extract_pointer(self, function, arg, arg_type, lvalue, rvalue):
72 print ' if (dynamic_cast<Trace::Null *>(&%s)) {' % rvalue
73 print ' %s = 0;' % (lvalue)
74 print ' } else {'
75 print ' %s = (%s)(uintptr_t)(%s);' % (lvalue, arg_type, rvalue)
76 print ' }'
José Fonsecae6a50bd2010-11-24 10:12:22 +000077
78
José Fonseca7e329022010-11-19 17:05:18 +000079if __name__ == '__main__':
80 print
José Fonseca7e329022010-11-19 17:05:18 +000081 print '#include <string.h>'
José Fonseca3d245f42010-11-28 00:08:23 +000082 print '#include <iostream>'
José Fonseca094cb2d2010-11-24 15:55:03 +000083 print
José Fonsecac6b49ce2010-11-28 16:43:04 +000084 print '#include "glproc.hpp"'
José Fonseca7e329022010-11-19 17:05:18 +000085 print '#include <GL/glut.h>'
86 print
José Fonsecafeb5d992010-11-25 17:14:18 +000087 print 'static bool double_buffer = false;'
José Fonseca3d245f42010-11-28 00:08:23 +000088 print 'static bool insideGlBeginEnd = false;'
José Fonsecafeb5d992010-11-25 17:14:18 +000089 print
José Fonseca3d245f42010-11-28 00:08:23 +000090 print '''
91static void
92checkGlError(void) {
93 if (insideGlBeginEnd) {
94 return;
95 }
96
97 GLenum error = glGetError();
98 if (error == GL_NO_ERROR) {
99 return;
100 }
101
102 std::cerr << "warning: glGetError() = ";
103 switch (error) {
104 case GL_INVALID_ENUM:
105 std::cerr << "GL_INVALID_ENUM";
106 break;
107 case GL_INVALID_VALUE:
108 std::cerr << "GL_INVALID_VALUE";
109 break;
110 case GL_INVALID_OPERATION:
111 std::cerr << "GL_INVALID_OPERATION";
112 break;
113 case GL_STACK_OVERFLOW:
114 std::cerr << "GL_STACK_OVERFLOW";
115 break;
116 case GL_STACK_UNDERFLOW:
117 std::cerr << "GL_STACK_UNDERFLOW";
118 break;
119 case GL_OUT_OF_MEMORY:
120 std::cerr << "GL_OUT_OF_MEMORY";
121 break;
122 case GL_INVALID_FRAMEBUFFER_OPERATION:
123 std::cerr << "GL_INVALID_FRAMEBUFFER_OPERATION";
124 break;
125 case GL_TABLE_TOO_LARGE:
126 std::cerr << "GL_TABLE_TOO_LARGE";
127 break;
128 default:
129 std::cerr << error;
130 break;
131 }
132 std::cerr << "\\n";
133}
134'''
José Fonsecae0e61402010-11-25 15:03:23 +0000135 api = glapi.glapi
136 retracer = GlRetracer()
137 retracer.retrace_api(glapi.glapi)
José Fonseca7e329022010-11-19 17:05:18 +0000138 print '''
139
José Fonseca3d245f42010-11-28 00:08:23 +0000140static Trace::Parser parser;
José Fonseca082051b2010-11-23 12:00:31 +0000141
José Fonseca6f51d3b2010-11-22 19:56:19 +0000142static void display(void) {
José Fonseca3d245f42010-11-28 00:08:23 +0000143 Trace::Call *call;
José Fonseca6f51d3b2010-11-22 19:56:19 +0000144
José Fonseca3d245f42010-11-28 00:08:23 +0000145 while ((call = parser.parse_call())) {
146 if (call->name() == "glFlush") {
José Fonsecafeb5d992010-11-25 17:14:18 +0000147 glFlush();
José Fonseca3d245f42010-11-28 00:08:23 +0000148 return;
149 }
150
151 if (!retrace_call(*call)) {
152 if (call->name() == "glXSwapBuffers" ||
153 call->name() == "wglSwapBuffers") {
154 if (double_buffer)
155 glutSwapBuffers();
156 else
157 glFlush();
158 return;
José Fonseca082051b2010-11-23 12:00:31 +0000159 }
José Fonseca3d245f42010-11-28 00:08:23 +0000160 }
161 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000162
José Fonseca3d245f42010-11-28 00:08:23 +0000163 glFlush();
164 glutIdleFunc(NULL);
José Fonseca6f51d3b2010-11-22 19:56:19 +0000165}
166
167static void idle(void) {
José Fonseca3d245f42010-11-28 00:08:23 +0000168 glutPostRedisplay();
José Fonseca6f51d3b2010-11-22 19:56:19 +0000169}
José Fonseca7e329022010-11-19 17:05:18 +0000170
171int main(int argc, char **argv)
172{
José Fonseca6f51d3b2010-11-22 19:56:19 +0000173
José Fonseca3d245f42010-11-28 00:08:23 +0000174 int i;
175 for (i = 1; i < argc; ++i) {
176 const char *arg = argv[i];
José Fonseca094cb2d2010-11-24 15:55:03 +0000177
José Fonseca3d245f42010-11-28 00:08:23 +0000178 if (arg[0] != '-') {
179 break;
180 }
José Fonseca094cb2d2010-11-24 15:55:03 +0000181
José Fonseca3d245f42010-11-28 00:08:23 +0000182 if (!strcmp(arg, "--")) {
183 break;
184 }
185 else if (!strcmp(arg, "-db")) {
186 double_buffer = true;
187 } else if (!strcmp(arg, "-v")) {
188 ++verbosity;
189 } else {
190 std::cerr << "error: unknown option " << arg << "\\n";
191 return 1;
192 }
193 }
José Fonseca094cb2d2010-11-24 15:55:03 +0000194
José Fonseca3d245f42010-11-28 00:08:23 +0000195 glutInit(&argc, argv);
196 glutInitWindowPosition(0, 0);
197 glutInitWindowSize(800, 600);
198 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | (double_buffer ? GLUT_DOUBLE : GLUT_SINGLE));
199 glutCreateWindow(argv[0]);
José Fonsecafeb5d992010-11-25 17:14:18 +0000200
José Fonseca3d245f42010-11-28 00:08:23 +0000201 glutDisplayFunc(&display);
202 glutIdleFunc(&idle);
José Fonsecafeb5d992010-11-25 17:14:18 +0000203
José Fonseca3d245f42010-11-28 00:08:23 +0000204 for (GLuint h = 0; h < 1024; ++h) {
205 __list_map[h] = h;
206 }
José Fonsecafeb5d992010-11-25 17:14:18 +0000207
José Fonseca3d245f42010-11-28 00:08:23 +0000208 for ( ; i < argc; ++i) {
209 if (parser.open(argv[i])) {
210 glutMainLoop();
211 parser.close();
212 }
213 }
José Fonseca6f51d3b2010-11-22 19:56:19 +0000214
José Fonseca3d245f42010-11-28 00:08:23 +0000215 return 0;
José Fonseca7e329022010-11-19 17:05:18 +0000216}
217
José Fonseca3d245f42010-11-28 00:08:23 +0000218'''