blob: c82c77971b495c05f11d1f7a674204ab2b92a14c [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é Fonseca62212972011-03-23 13:22:55 +000040 array_pointer_function_names = set((
41 "glVertexPointer",
42 "glNormalPointer",
43 "glColorPointer",
44 "glIndexPointer",
45 "glTexCoordPointer",
46 "glEdgeFlagPointer",
47 "glFogCoordPointer",
48 "glSecondaryColorPointer",
49
50 "glInterleavedArrays",
51
José Fonsecaac5285b2011-05-04 11:09:08 +010052 "glVertexPointerEXT",
53 "glNormalPointerEXT",
54 "glColorPointerEXT",
55 "glIndexPointerEXT",
56 "glTexCoordPointerEXT",
57 "glEdgeFlagPointerEXT",
58 "glFogCoordPointerEXT",
59 "glSecondaryColorPointerEXT",
José Fonseca62212972011-03-23 13:22:55 +000060
José Fonseca7f5163e2011-03-31 23:37:26 +010061 "glVertexAttribPointer",
62 "glVertexAttribPointerARB",
63 "glVertexAttribPointerNV",
José Fonsecaac5285b2011-05-04 11:09:08 +010064 "glVertexAttribIPointer",
65 "glVertexAttribIPointerEXT",
José Fonseca7f5163e2011-03-31 23:37:26 +010066 "glVertexAttribLPointer",
67 "glVertexAttribLPointerEXT",
José Fonseca62212972011-03-23 13:22:55 +000068
69 #"glMatrixIndexPointerARB",
70 ))
71
José Fonseca8caf2c82010-11-30 12:09:12 +000072 draw_array_function_names = set([
73 "glDrawArrays",
74 "glDrawArraysEXT",
75 "glDrawArraysIndirect",
76 "glDrawArraysInstanced",
77 "glDrawArraysInstancedARB",
78 "glDrawArraysInstancedEXT",
79 "glDrawMeshArraysSUN",
80 "glMultiDrawArrays",
81 "glMultiDrawArraysEXT",
82 "glMultiModeDrawArraysIBM",
83 ])
84
85 draw_elements_function_names = set([
86 "glDrawElements",
87 "glDrawElementsBaseVertex",
88 "glDrawElementsIndirect",
89 "glDrawElementsInstanced",
90 "glDrawElementsInstancedARB",
91 "glDrawElementsInstancedBaseVertex",
92 "glDrawElementsInstancedEXT",
93 "glDrawRangeElements",
94 "glDrawRangeElementsBaseVertex",
95 "glDrawRangeElementsEXT",
José Fonsecafd34e4e2011-06-03 19:34:29 +010096 "glMultiDrawElements",
97 "glMultiDrawElementsBaseVertex",
98 "glMultiDrawElementsEXT",
99 "glMultiModeDrawElementsIBM",
José Fonseca8caf2c82010-11-30 12:09:12 +0000100 ])
101
José Fonseca920bffd2011-06-02 10:04:52 +0100102 misc_draw_function_names = set([
103 "glClear",
104 "glEnd",
José Fonsecaf8a99a02011-06-07 20:49:56 +0100105 "glDrawPixels",
106 "glBlitFramebuffer",
107 "glBlitFramebufferEXT",
José Fonseca920bffd2011-06-02 10:04:52 +0100108 ])
109
110 bind_framebuffer_function_names = set([
111 "glBindFramebuffer",
112 "glBindFramebufferARB",
113 "glBindFramebufferEXT",
114 ])
115
José Fonseca62212972011-03-23 13:22:55 +0000116 def retrace_function_body(self, function):
117 is_array_pointer = function.name in self.array_pointer_function_names
118 is_draw_array = function.name in self.draw_array_function_names
119 is_draw_elements = function.name in self.draw_elements_function_names
José Fonseca920bffd2011-06-02 10:04:52 +0100120 is_misc_draw = function.name in self.misc_draw_function_names
José Fonseca62212972011-03-23 13:22:55 +0000121
122 if is_array_pointer or is_draw_array or is_draw_elements:
José Fonseca6117e312011-04-15 23:52:58 +0100123 print ' if (glretrace::parser.version < 1) {'
José Fonseca62212972011-03-23 13:22:55 +0000124
125 if is_array_pointer or is_draw_array:
126 print ' GLint __array_buffer = 0;'
127 print ' glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);'
128 print ' if (!__array_buffer) {'
129 self.fail_function(function)
130 print ' }'
131
132 if is_draw_elements:
133 print ' GLint __element_array_buffer = 0;'
134 print ' glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);'
135 print ' if (!__element_array_buffer) {'
136 self.fail_function(function)
137 print ' }'
138
139 print ' }'
140
José Fonseca920bffd2011-06-02 10:04:52 +0100141 # Pre-snapshots
142 if function.name in self.bind_framebuffer_function_names:
143 print ' if (glretrace::snapshot_frequency == glretrace::FREQUENCY_FRAMEBUFFER) {'
144 print ' glretrace::snapshot(call.no - 1);'
145 print ' }'
146
José Fonseca62212972011-03-23 13:22:55 +0000147 Retracer.retrace_function_body(self, function)
148
José Fonseca920bffd2011-06-02 10:04:52 +0100149 # Post-snapshots
José Fonseca4d7f1fe2011-05-09 20:54:31 +0100150 if function.name in ('glFlush', 'glFinish'):
151 print ' if (!glretrace::double_buffer) {'
152 print ' glretrace::frame_complete(call.no);'
153 print ' }'
José Fonseca4d7f1fe2011-05-09 20:54:31 +0100154 if function.name == 'glReadPixels':
155 print ' glFinish();'
José Fonseca920bffd2011-06-02 10:04:52 +0100156 print ' if (glretrace::snapshot_frequency == glretrace::FREQUENCY_FRAME ||'
157 print ' glretrace::snapshot_frequency == glretrace::FREQUENCY_FRAMEBUFFER) {'
158 print ' glretrace::snapshot(call.no);'
159 print ' }'
160 if is_draw_array or is_draw_elements or is_misc_draw:
161 print ' if (glretrace::snapshot_frequency == glretrace::FREQUENCY_DRAW) {'
162 print ' glretrace::snapshot(call.no);'
163 print ' }'
164
José Fonseca4d7f1fe2011-05-09 20:54:31 +0100165
José Fonsecafa15d332010-11-25 20:22:39 +0000166 def call_function(self, function):
José Fonsecacdb574a2010-11-29 12:23:35 +0000167 if function.name == "glViewport":
José Fonseca29d21622011-05-06 20:50:52 +0100168 print ' if (glretrace::drawable) {'
169 print ' int drawable_width = x + width;'
170 print ' int drawable_height = y + height;'
171 print ' if (drawable_width > (int)glretrace::drawable->width ||'
172 print ' drawable_height > (int)glretrace::drawable->height) {'
173 print ' glretrace::drawable->resize(drawable_width, drawable_height);'
José Fonsecad3a6f152011-05-20 23:54:13 +0100174 print ' if (!glretrace::drawable->visible) {'
175 print ' glretrace::drawable->show();'
176 print ' }'
José Fonsecaa4d77a92011-05-18 09:54:36 +0100177 print ' glScissor(0, 0, drawable_width, drawable_height);'
José Fonseca29d21622011-05-06 20:50:52 +0100178 print ' }'
José Fonsecacdb574a2010-11-29 12:23:35 +0000179 print ' }'
180
José Fonseca3d245f42010-11-28 00:08:23 +0000181 if function.name == "glEnd":
José Fonseca32871ed2011-04-10 13:40:52 +0100182 print ' glretrace::insideGlBeginEnd = false;'
José Fonsecafea81f92011-05-23 21:20:53 +0100183
184 if function.name == 'memcpy':
185 print ' if (!dest || !src || !n) return;'
José Fonsecacdb574a2010-11-29 12:23:35 +0000186
José Fonsecafa15d332010-11-25 20:22:39 +0000187 Retracer.call_function(self, function)
José Fonsecacdb574a2010-11-29 12:23:35 +0000188
José Fonsecaecf37412011-05-21 01:56:08 +0100189 # Error checking
José Fonseca3d245f42010-11-28 00:08:23 +0000190 if function.name == "glBegin":
José Fonseca32871ed2011-04-10 13:40:52 +0100191 print ' glretrace::insideGlBeginEnd = true;'
José Fonsecab8c56072011-05-05 01:13:04 +0100192 elif function.name.startswith('gl'):
José Fonseca3d245f42010-11-28 00:08:23 +0000193 # glGetError is not allowed inside glBegin/glEnd
José Fonsecaecf37412011-05-21 01:56:08 +0100194 print ' if (!glretrace::benchmark && !glretrace::insideGlBeginEnd) {'
195 print ' glretrace::checkGlError(call);'
196 if function.name in ('glProgramStringARB', 'glProgramStringNV'):
197 print r' GLint error_position = -1;'
198 print r' glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_position);'
199 print r' if (error_position != -1) {'
200 print r' const char *error_string = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB);'
201 print r' std::cerr << call.no << ": warning: " << error_string << "\n";'
202 print r' }'
203 if function.name == 'glCompileShader':
204 print r' GLint compile_status = 0;'
205 print r' glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);'
206 print r' if (!compile_status) {'
207 print r' GLint info_log_length = 0;'
208 print r' glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);'
209 print r' GLchar *infoLog = new GLchar[info_log_length];'
210 print r' glGetShaderInfoLog(shader, info_log_length, NULL, infoLog);'
211 print r' std::cerr << call.no << ": warning: " << infoLog << "\n";'
212 print r' delete [] infoLog;'
213 print r' }'
214 if function.name == 'glLinkProgram':
215 print r' GLint link_status = 0;'
216 print r' glGetProgramiv(program, GL_LINK_STATUS, &link_status);'
217 print r' if (!link_status) {'
218 print r' GLint info_log_length = 0;'
219 print r' glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length);'
220 print r' GLchar *infoLog = new GLchar[info_log_length];'
221 print r' glGetProgramInfoLog(program, info_log_length, NULL, infoLog);'
222 print r' std::cerr << call.no << ": warning: " << infoLog << "\n";'
223 print r' delete [] infoLog;'
224 print r' }'
225 if function.name == 'glCompileShaderARB':
226 print r' GLint compile_status = 0;'
227 print r' glGetObjectParameterivARB(shaderObj, GL_OBJECT_COMPILE_STATUS_ARB, &compile_status);'
228 print r' if (!compile_status) {'
229 print r' GLint info_log_length = 0;'
230 print r' glGetObjectParameterivARB(shaderObj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_log_length);'
231 print r' GLchar *infoLog = new GLchar[info_log_length];'
232 print r' glGetInfoLogARB(shaderObj, info_log_length, NULL, infoLog);'
233 print r' std::cerr << call.no << ": warning: " << infoLog << "\n";'
234 print r' delete [] infoLog;'
235 print r' }'
236 if function.name == 'glLinkProgramARB':
237 print r' GLint link_status = 0;'
238 print r' glGetObjectParameterivARB(programObj, GL_OBJECT_LINK_STATUS_ARB, &link_status);'
239 print r' if (!link_status) {'
240 print r' GLint info_log_length = 0;'
241 print r' glGetObjectParameterivARB(programObj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_log_length);'
242 print r' GLchar *infoLog = new GLchar[info_log_length];'
243 print r' glGetInfoLogARB(programObj, info_log_length, NULL, infoLog);'
244 print r' std::cerr << call.no << ": warning: " << infoLog << "\n";'
245 print r' delete [] infoLog;'
246 print r' }'
José Fonsecafea81f92011-05-23 21:20:53 +0100247 if function.name in ('glMapBuffer', 'glMapBufferARB', 'glMapBufferRange', 'glMapNamedBufferEXT', 'glMapNamedBufferRangeEXT'):
248 print r' if (!__result) {'
249 print r' std::cerr << call.no << ": warning: failed to map buffer\n";'
250 print r' }'
José Fonseca91492d22011-05-23 21:20:31 +0100251 if function.name in ('glGetAttribLocation', 'glGetAttribLocationARB'):
252 print r' GLint __orig_result = call.ret->toSInt();'
253 print r' if (__result != __orig_result) {'
254 print r' std::cerr << call.no << ": warning vertex attrib location mismatch " << __orig_result << " -> " << __result << "\n";'
255 print r' }'
José Fonsecad5377812011-06-07 20:48:55 +0100256 if function.name in ('glCheckFramebufferStatus', 'glCheckFramebufferStatusEXT', 'glCheckNamedFramebufferStatusEXT'):
257 print r' GLint __orig_result = call.ret->toSInt();'
258 print r' if (__orig_result == GL_FRAMEBUFFER_COMPLETE &&'
259 print r' __result != GL_FRAMEBUFFER_COMPLETE) {'
260 print r' std::cerr << call.no << ": incomplete framebuffer (" << __result << ")\n";'
261 print r' }'
José Fonsecaecf37412011-05-21 01:56:08 +0100262 print ' }'
José Fonsecae4999b92011-05-05 00:31:01 +0100263
José Fonsecadacd8dd2010-11-25 17:50:26 +0000264 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
José Fonseca7f5163e2011-03-31 23:37:26 +0100265 if function.name in self.array_pointer_function_names and arg.name == 'pointer':
José Fonseca7ebb9e22011-05-06 09:58:45 +0100266 print ' %s = static_cast<%s>(%s.toPointer());' % (lvalue, arg_type, rvalue)
José Fonseca14c21bc2011-02-20 23:32:22 +0000267 return
268
269 if function.name in self.draw_elements_function_names and arg.name == 'indices':
José Fonsecafd34e4e2011-06-03 19:34:29 +0100270 self.extract_opaque_arg(function, arg, arg_type, lvalue, rvalue)
José Fonseca8a844ae2010-12-06 18:50:52 +0000271 return
José Fonsecadacd8dd2010-11-25 17:50:26 +0000272
José Fonseca3c1b7ce2011-05-09 11:22:54 +0100273 if arg.type is glapi.GLlocation \
274 and 'program' not in [arg.name for arg in function.args]:
José Fonseca8a844ae2010-12-06 18:50:52 +0000275 print ' GLint program = -1;'
276 print ' glGetIntegerv(GL_CURRENT_PROGRAM, &program);'
José Fonseca3c1b7ce2011-05-09 11:22:54 +0100277
278 if arg.type is glapi.GLlocationARB \
279 and 'programObj' not in [arg.name for arg in function.args]:
José Fonseca3a84b682011-05-09 11:33:47 +0100280 print ' GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);'
José Fonseca8a844ae2010-12-06 18:50:52 +0000281
282 Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
José Fonsecae6a50bd2010-11-24 10:12:22 +0000283
José Fonseca42926462011-05-09 11:32:15 +0100284 # Don't try to use more samples than the implementation supports
285 if arg.name == 'samples':
286 assert arg.type is glapi.GLsizei
287 print ' GLint max_samples = 0;'
288 print ' glGetIntegerv(GL_MAX_SAMPLES, &max_samples);'
289 print ' if (samples > max_samples) {'
290 print ' samples = max_samples;'
291 print ' }'
292
José Fonsecae6a50bd2010-11-24 10:12:22 +0000293
José Fonseca7e329022010-11-19 17:05:18 +0000294if __name__ == '__main__':
José Fonseca796a3042010-11-29 14:21:06 +0000295 print r'''
José Fonseca867b1b72011-04-24 11:58:04 +0100296#include <string.h>
297
José Fonsecadf66a902010-11-29 13:24:20 +0000298#include "glproc.hpp"
José Fonseca32871ed2011-04-10 13:40:52 +0100299#include "glretrace.hpp"
José Fonseca589082d2011-03-30 09:10:40 +0100300
José Fonsecadf66a902010-11-29 13:24:20 +0000301
José Fonseca3d245f42010-11-28 00:08:23 +0000302'''
José Fonsecae0e61402010-11-25 15:03:23 +0000303 api = glapi.glapi
José Fonseca867b1b72011-04-24 11:58:04 +0100304 api.add_function(glapi.memcpy)
José Fonsecae0e61402010-11-25 15:03:23 +0000305 retracer = GlRetracer()
José Fonseca06aa2842011-05-05 07:55:54 +0100306 retracer.retrace_api(api)