1 ##########################################################################
3 # Copyright 2010 VMware, Inc.
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:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
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
24 ##########################################################################/
27 """GL retracer generator."""
30 import specs.stdapi as stdapi
31 import specs.glapi as glapi
32 from retrace import Retracer
35 class GlRetracer(Retracer):
37 table_name = 'glretrace::gl_callbacks'
39 def retrace_function(self, function):
40 Retracer.retrace_function(self, function)
42 array_pointer_function_names = set((
50 "glSecondaryColorPointer",
52 "glInterleavedArrays",
58 "glTexCoordPointerEXT",
59 "glEdgeFlagPointerEXT",
60 "glFogCoordPointerEXT",
61 "glSecondaryColorPointerEXT",
63 "glVertexAttribPointer",
64 "glVertexAttribPointerARB",
65 "glVertexAttribPointerNV",
66 "glVertexAttribIPointer",
67 "glVertexAttribIPointerEXT",
68 "glVertexAttribLPointer",
69 "glVertexAttribLPointerEXT",
71 #"glMatrixIndexPointerARB",
74 draw_array_function_names = set([
77 "glDrawArraysIndirect",
78 "glDrawArraysInstanced",
79 "glDrawArraysInstancedARB",
80 "glDrawArraysInstancedEXT",
81 "glDrawArraysInstancedBaseInstance",
82 "glDrawMeshArraysSUN",
84 "glMultiDrawArraysEXT",
85 "glMultiModeDrawArraysIBM",
88 draw_elements_function_names = set([
90 "glDrawElementsBaseVertex",
91 "glDrawElementsIndirect",
92 "glDrawElementsInstanced",
93 "glDrawElementsInstancedARB",
94 "glDrawElementsInstancedEXT",
95 "glDrawElementsInstancedBaseVertex",
96 "glDrawElementsInstancedBaseInstance",
97 "glDrawElementsInstancedBaseVertexBaseInstance",
98 "glDrawRangeElements",
99 "glDrawRangeElementsEXT",
100 "glDrawRangeElementsBaseVertex",
101 "glMultiDrawElements",
102 "glMultiDrawElementsBaseVertex",
103 "glMultiDrawElementsEXT",
104 "glMultiModeDrawElementsIBM",
107 draw_indirect_function_names = set([
108 "glDrawArraysIndirect",
109 "glDrawElementsIndirect",
112 misc_draw_function_names = set([
117 "glBlitFramebufferEXT",
120 bind_framebuffer_function_names = set([
122 "glBindFramebufferARB",
123 "glBindFramebufferEXT",
126 # Names of the functions that can pack into the current pixel buffer
127 # object. See also the ARB_pixel_buffer_object specification.
128 pack_function_names = set([
129 'glGetCompressedTexImage',
130 'glGetConvolutionFilter',
136 'glGetPolygonStipple',
137 'glGetSeparableFilter',
140 'glGetnCompressedTexImageARB',
141 'glGetnConvolutionFilterARB',
142 'glGetnHistogramARB',
144 'glGetnPixelMapfvARB',
145 'glGetnPixelMapuivARB',
146 'glGetnPixelMapusvARB',
147 'glGetnPolygonStippleARB',
148 'glGetnSeparableFilterARB',
153 map_function_names = set([
157 'glMapNamedBufferEXT',
158 'glMapNamedBufferRangeEXT'
161 unmap_function_names = set([
164 'glUnmapNamedBufferEXT',
167 def retrace_function_body(self, function):
168 is_array_pointer = function.name in self.array_pointer_function_names
169 is_draw_array = function.name in self.draw_array_function_names
170 is_draw_elements = function.name in self.draw_elements_function_names
171 is_misc_draw = function.name in self.misc_draw_function_names
173 if is_array_pointer or is_draw_array or is_draw_elements:
174 print ' if (glretrace::parser.version < 1) {'
176 if is_array_pointer or is_draw_array:
177 print ' GLint __array_buffer = 0;'
178 print ' glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &__array_buffer);'
179 print ' if (!__array_buffer) {'
180 self.fail_function(function)
184 print ' GLint __element_array_buffer = 0;'
185 print ' glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);'
186 print ' if (!__element_array_buffer) {'
187 self.fail_function(function)
192 # When no pack buffer object is bound, the pack functions are no-ops.
193 if function.name in self.pack_function_names:
194 print ' GLint __pack_buffer = 0;'
195 print ' glGetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &__pack_buffer);'
196 print ' if (!__pack_buffer) {'
197 if function.name == 'glReadPixels':
199 print ' if (glretrace::snapshot_frequency == glretrace::FREQUENCY_FRAME ||'
200 print ' glretrace::snapshot_frequency == glretrace::FREQUENCY_FRAMEBUFFER) {'
201 print ' glretrace::snapshot(call.no);'
207 if function.name in self.bind_framebuffer_function_names:
208 print ' if (glretrace::snapshot_frequency == glretrace::FREQUENCY_FRAMEBUFFER) {'
209 print ' glretrace::snapshot(call.no - 1);'
211 if function.name == 'glFrameTerminatorGREMEDY':
212 print ' glretrace::frame_complete(call);'
215 Retracer.retrace_function_body(self, function)
218 if function.name in ('glFlush', 'glFinish'):
219 print ' if (!glretrace::double_buffer) {'
220 print ' glretrace::frame_complete(call);'
222 if is_draw_array or is_draw_elements or is_misc_draw:
223 print ' if (glretrace::snapshot_frequency == glretrace::FREQUENCY_DRAW) {'
224 print ' glretrace::snapshot(call.no);'
228 def call_function(self, function):
229 # Infer the drawable size from GL calls
230 if function.name == "glViewport":
231 print ' glretrace::updateDrawable(x + width, y + height);'
232 if function.name in ('glBlitFramebuffer', 'glBlitFramebufferEXT'):
233 # Some applications do all their rendering in a framebuffer, and
234 # then just blit to the drawable without ever calling glViewport.
235 print ' glretrace::updateDrawable(std::max(dstX0, dstX1), std::max(dstY0, dstY1));'
237 if function.name == "glEnd":
238 print ' glretrace::insideGlBeginEnd = false;'
240 if function.name == 'memcpy':
241 print ' if (!dest || !src || !n) return;'
243 Retracer.call_function(self, function)
246 if function.name == "glBegin":
247 print ' glretrace::insideGlBeginEnd = true;'
248 elif function.name.startswith('gl'):
249 # glGetError is not allowed inside glBegin/glEnd
250 print ' if (!glretrace::benchmark && !glretrace::insideGlBeginEnd) {'
251 print ' glretrace::checkGlError(call);'
252 if function.name in ('glProgramStringARB', 'glProgramStringNV'):
253 print r' GLint error_position = -1;'
254 print r' glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_position);'
255 print r' if (error_position != -1) {'
256 print r' const char *error_string = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB);'
257 print r' retrace::warning(call) << error_string << "\n";'
259 if function.name == 'glCompileShader':
260 print r' GLint compile_status = 0;'
261 print r' glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);'
262 print r' if (!compile_status) {'
263 print r' GLint info_log_length = 0;'
264 print r' glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);'
265 print r' GLchar *infoLog = new GLchar[info_log_length];'
266 print r' glGetShaderInfoLog(shader, info_log_length, NULL, infoLog);'
267 print r' retrace::warning(call) << infoLog << "\n";'
268 print r' delete [] infoLog;'
270 if function.name == 'glLinkProgram':
271 print r' GLint link_status = 0;'
272 print r' glGetProgramiv(program, GL_LINK_STATUS, &link_status);'
273 print r' if (!link_status) {'
274 print r' GLint info_log_length = 0;'
275 print r' glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length);'
276 print r' GLchar *infoLog = new GLchar[info_log_length];'
277 print r' glGetProgramInfoLog(program, info_log_length, NULL, infoLog);'
278 print r' retrace::warning(call) << infoLog << "\n";'
279 print r' delete [] infoLog;'
281 if function.name == 'glCompileShaderARB':
282 print r' GLint compile_status = 0;'
283 print r' glGetObjectParameterivARB(shaderObj, GL_OBJECT_COMPILE_STATUS_ARB, &compile_status);'
284 print r' if (!compile_status) {'
285 print r' GLint info_log_length = 0;'
286 print r' glGetObjectParameterivARB(shaderObj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_log_length);'
287 print r' GLchar *infoLog = new GLchar[info_log_length];'
288 print r' glGetInfoLogARB(shaderObj, info_log_length, NULL, infoLog);'
289 print r' retrace::warning(call) << infoLog << "\n";'
290 print r' delete [] infoLog;'
292 if function.name == 'glLinkProgramARB':
293 print r' GLint link_status = 0;'
294 print r' glGetObjectParameterivARB(programObj, GL_OBJECT_LINK_STATUS_ARB, &link_status);'
295 print r' if (!link_status) {'
296 print r' GLint info_log_length = 0;'
297 print r' glGetObjectParameterivARB(programObj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_log_length);'
298 print r' GLchar *infoLog = new GLchar[info_log_length];'
299 print r' glGetInfoLogARB(programObj, info_log_length, NULL, infoLog);'
300 print r' retrace::warning(call) << infoLog << "\n";'
301 print r' delete [] infoLog;'
303 if function.name in self.map_function_names:
304 print r' if (!__result) {'
305 print r' retrace::warning(call) << "failed to map buffer\n";'
307 if function.name in ('glGetAttribLocation', 'glGetAttribLocationARB'):
308 print r' GLint __orig_result = call.ret->toSInt();'
309 print r' if (__result != __orig_result) {'
310 print r' retrace::warning(call) << "vertex attrib location mismatch " << __orig_result << " -> " << __result << "\n";'
312 if function.name in ('glCheckFramebufferStatus', 'glCheckFramebufferStatusEXT', 'glCheckNamedFramebufferStatusEXT'):
313 print r' GLint __orig_result = call.ret->toSInt();'
314 print r' if (__orig_result == GL_FRAMEBUFFER_COMPLETE &&'
315 print r' __result != GL_FRAMEBUFFER_COMPLETE) {'
316 print r' retrace::warning(call) << "incomplete framebuffer (" << glstate::enumToString(__result) << ")\n";'
320 # Update buffer mappings
321 if function.name in self.map_function_names:
322 print r' if (__result) {'
323 print r' unsigned long long __address = call.ret->toUIntPtr();'
324 if 'BufferRange' not in function.name:
325 print r' GLint length = 0;'
326 if function.name == 'glMapBuffer':
327 print r' glGetBufferParameteriv(target, GL_BUFFER_SIZE, &length);'
328 elif function.name == 'glMapBufferARB':
329 print r' glGetBufferParameterivARB(target, GL_BUFFER_SIZE_ARB, &length);'
330 elif function.name == 'glMapNamedBufferEXT':
331 print r' glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_SIZE, &length);'
334 print r' retrace::addRegion(__address, __result, length);'
336 if function.name in self.unmap_function_names:
337 print r' GLvoid *ptr = NULL;'
338 if function.name == 'glUnmapBuffer':
339 print r' glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &ptr);'
340 elif function.name == 'glUnmapBufferARB':
341 print r' glGetBufferPointervARB(target, GL_BUFFER_MAP_POINTER_ARB, &ptr);'
342 elif function.name == 'glUnmapNamedBufferEXT':
343 print r' glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &ptr);'
347 print r' retrace::delRegionByPointer(ptr);'
350 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
351 if function.name in self.array_pointer_function_names and arg.name == 'pointer':
352 print ' %s = static_cast<%s>(retrace::toPointer(%s, true));' % (lvalue, arg_type, rvalue)
355 if function.name in self.draw_elements_function_names and arg.name == 'indices' or\
356 function.name in self.draw_indirect_function_names and arg.name == 'indirect':
357 self.extract_opaque_arg(function, arg, arg_type, lvalue, rvalue)
360 # Handle pointer with offsets into the current pack pixel buffer
362 if function.name in self.pack_function_names and arg.output:
363 self.extract_opaque_arg(function, arg, arg_type, lvalue, rvalue)
366 if arg.type is glapi.GLlocation \
367 and 'program' not in [arg.name for arg in function.args]:
368 print ' GLint program = -1;'
369 print ' glGetIntegerv(GL_CURRENT_PROGRAM, &program);'
371 if arg.type is glapi.GLlocationARB \
372 and 'programObj' not in [arg.name for arg in function.args]:
373 print ' GLhandleARB programObj = glGetHandleARB(GL_PROGRAM_OBJECT_ARB);'
375 Retracer.extract_arg(self, function, arg, arg_type, lvalue, rvalue)
377 # Don't try to use more samples than the implementation supports
378 if arg.name == 'samples':
379 assert arg.type is glapi.GLsizei
380 print ' GLint max_samples = 0;'
381 print ' glGetIntegerv(GL_MAX_SAMPLES, &max_samples);'
382 print ' if (samples > max_samples) {'
383 print ' samples = max_samples;'
387 if __name__ == '__main__':
391 #include "glproc.hpp"
392 #include "glretrace.hpp"
393 #include "glstate.hpp"
398 retracer = GlRetracer()
399 retracer.retrace_api(api)