blob: 5b0444161622c1dd4ee9db2a4312e7635e49af9d [file] [log] [blame]
José Fonseca669b1222011-02-20 09:05:10 +00001##########################################################################
2#
3# Copyright 2008-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
27"""GL tracing generator."""
28
29
José Fonsecac5bf77a2014-08-14 16:10:02 +010030import re
31import sys
32
José Fonseca452d3252012-04-14 15:55:40 +010033from trace import Tracer
José Fonseca1b6c8752012-04-15 14:33:00 +010034from dispatch import function_pointer_type, function_pointer_value
José Fonsecabd86a222011-09-27 09:21:38 +010035import specs.stdapi as stdapi
36import specs.glapi as glapi
37import specs.glparams as glparams
38from specs.glxapi import glxapi
José Fonseca669b1222011-02-20 09:05:10 +000039
40
José Fonseca99221832011-03-22 22:15:46 +000041class TypeGetter(stdapi.Visitor):
42 '''Determine which glGet*v function that matches the specified type.'''
43
José Fonsecac493e3e2011-06-29 12:57:06 +010044 def __init__(self, prefix = 'glGet', long_suffix = True, ext_suffix = ''):
José Fonseca1a2fdd22011-04-01 00:55:09 +010045 self.prefix = prefix
46 self.long_suffix = long_suffix
José Fonsecac493e3e2011-06-29 12:57:06 +010047 self.ext_suffix = ext_suffix
José Fonseca1a2fdd22011-04-01 00:55:09 +010048
José Fonseca54f304a2012-01-14 19:33:08 +000049 def visitConst(self, const):
José Fonseca99221832011-03-22 22:15:46 +000050 return self.visit(const.type)
51
José Fonseca54f304a2012-01-14 19:33:08 +000052 def visitAlias(self, alias):
José Fonseca99221832011-03-22 22:15:46 +000053 if alias.expr == 'GLboolean':
José Fonseca1a2fdd22011-04-01 00:55:09 +010054 if self.long_suffix:
José Fonsecac493e3e2011-06-29 12:57:06 +010055 suffix = 'Booleanv'
56 arg_type = alias.expr
José Fonseca1a2fdd22011-04-01 00:55:09 +010057 else:
José Fonsecac493e3e2011-06-29 12:57:06 +010058 suffix = 'iv'
59 arg_type = 'GLint'
José Fonseca99221832011-03-22 22:15:46 +000060 elif alias.expr == 'GLdouble':
José Fonseca1a2fdd22011-04-01 00:55:09 +010061 if self.long_suffix:
José Fonsecac493e3e2011-06-29 12:57:06 +010062 suffix = 'Doublev'
63 arg_type = alias.expr
José Fonseca1a2fdd22011-04-01 00:55:09 +010064 else:
José Fonsecac493e3e2011-06-29 12:57:06 +010065 suffix = 'dv'
66 arg_type = alias.expr
José Fonseca99221832011-03-22 22:15:46 +000067 elif alias.expr == 'GLfloat':
José Fonseca1a2fdd22011-04-01 00:55:09 +010068 if self.long_suffix:
José Fonsecac493e3e2011-06-29 12:57:06 +010069 suffix = 'Floatv'
70 arg_type = alias.expr
José Fonseca1a2fdd22011-04-01 00:55:09 +010071 else:
José Fonsecac493e3e2011-06-29 12:57:06 +010072 suffix = 'fv'
73 arg_type = alias.expr
José Fonseca7f5163e2011-03-31 23:37:26 +010074 elif alias.expr in ('GLint', 'GLuint', 'GLsizei'):
José Fonseca1a2fdd22011-04-01 00:55:09 +010075 if self.long_suffix:
José Fonsecac493e3e2011-06-29 12:57:06 +010076 suffix = 'Integerv'
77 arg_type = 'GLint'
José Fonseca1a2fdd22011-04-01 00:55:09 +010078 else:
José Fonsecac493e3e2011-06-29 12:57:06 +010079 suffix = 'iv'
80 arg_type = 'GLint'
José Fonseca99221832011-03-22 22:15:46 +000081 else:
82 print alias.expr
83 assert False
José Fonsecac493e3e2011-06-29 12:57:06 +010084 function_name = self.prefix + suffix + self.ext_suffix
85 return function_name, arg_type
José Fonseca99221832011-03-22 22:15:46 +000086
José Fonseca54f304a2012-01-14 19:33:08 +000087 def visitEnum(self, enum):
José Fonseca1a2fdd22011-04-01 00:55:09 +010088 return self.visit(glapi.GLint)
José Fonseca99221832011-03-22 22:15:46 +000089
José Fonseca54f304a2012-01-14 19:33:08 +000090 def visitBitmask(self, bitmask):
José Fonseca1a2fdd22011-04-01 00:55:09 +010091 return self.visit(glapi.GLint)
José Fonseca99221832011-03-22 22:15:46 +000092
José Fonseca54f304a2012-01-14 19:33:08 +000093 def visitOpaque(self, pointer):
José Fonsecac493e3e2011-06-29 12:57:06 +010094 return self.prefix + 'Pointerv' + self.ext_suffix, 'GLvoid *'
José Fonseca99221832011-03-22 22:15:46 +000095
96
José Fonseca669b1222011-02-20 09:05:10 +000097class GlTracer(Tracer):
98
José Fonseca99221832011-03-22 22:15:46 +000099 arrays = [
100 ("Vertex", "VERTEX"),
101 ("Normal", "NORMAL"),
102 ("Color", "COLOR"),
103 ("Index", "INDEX"),
104 ("TexCoord", "TEXTURE_COORD"),
105 ("EdgeFlag", "EDGE_FLAG"),
106 ("FogCoord", "FOG_COORD"),
107 ("SecondaryColor", "SECONDARY_COLOR"),
José Fonseca14c21bc2011-02-20 23:32:22 +0000108 ]
José Fonsecac9f12232011-03-25 20:07:42 +0000109 arrays.reverse()
José Fonseca669b1222011-02-20 09:05:10 +0000110
Chia-I Wub3d218d2011-11-03 01:37:36 +0800111 # arrays available in PROFILE_ES1
112 arrays_es1 = ("Vertex", "Normal", "Color", "TexCoord")
113
José Fonseca4c938c22011-04-30 22:44:38 +0100114 def header(self, api):
115 Tracer.header(self, api)
116
José Fonseca707630d2014-03-07 14:20:35 +0000117 print '#include <algorithm>'
118 print
José Fonseca1b3d3752011-07-15 10:15:19 +0100119 print '#include "gltrace.hpp"'
120 print
José Fonseca5a568a92011-06-29 16:43:36 +0100121
122 # Which glVertexAttrib* variant to use
123 print 'enum vertex_attrib {'
124 print ' VERTEX_ATTRIB,'
José Fonseca5a568a92011-06-29 16:43:36 +0100125 print ' VERTEX_ATTRIB_NV,'
126 print '};'
127 print
José Fonseca632a78d2012-04-19 07:18:59 +0100128 print 'static vertex_attrib _get_vertex_attrib(void) {'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000129 print ' gltrace::Context *ctx = gltrace::getContext();'
José Fonseca74b661a2014-07-17 19:10:24 +0100130 print ' if (ctx->user_arrays_nv) {'
José Fonseca632a78d2012-04-19 07:18:59 +0100131 print ' GLboolean _vertex_program = GL_FALSE;'
132 print ' _glGetBooleanv(GL_VERTEX_PROGRAM_ARB, &_vertex_program);'
133 print ' if (_vertex_program) {'
Chia-I Wu8ef66972011-11-03 01:19:46 +0800134 print ' if (ctx->user_arrays_nv) {'
José Fonseca26be8f92014-03-07 14:08:50 +0000135 print ' GLint _vertex_program_binding_nv = _glGetInteger(GL_VERTEX_PROGRAM_BINDING_NV);'
José Fonseca632a78d2012-04-19 07:18:59 +0100136 print ' if (_vertex_program_binding_nv) {'
José Fonseca5a568a92011-06-29 16:43:36 +0100137 print ' return VERTEX_ATTRIB_NV;'
138 print ' }'
139 print ' }'
José Fonseca5a568a92011-06-29 16:43:36 +0100140 print ' }'
141 print ' }'
142 print ' return VERTEX_ATTRIB;'
143 print '}'
144 print
145
Imre Deakd4937372012-04-24 14:06:48 +0300146 self.defineShadowBufferHelper()
147
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000148 # Whether we need user arrays
José Fonseca632a78d2012-04-19 07:18:59 +0100149 print 'static inline bool _need_user_arrays(void)'
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000150 print '{'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000151 print ' gltrace::Context *ctx = gltrace::getContext();'
Chia-I Wu8ef66972011-11-03 01:19:46 +0800152 print ' if (!ctx->user_arrays) {'
José Fonseca25ebe542011-04-24 10:08:22 +0100153 print ' return false;'
154 print ' }'
155 print
José Fonseca8b04b5a2014-07-17 19:25:21 +0100156 print ' enum gltrace::Profile profile = ctx->profile;'
157 print
José Fonseca1a2fdd22011-04-01 00:55:09 +0100158
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000159 for camelcase_name, uppercase_name in self.arrays:
Chia-I Wub3d218d2011-11-03 01:37:36 +0800160 # in which profile is the array available?
José Fonseca8b04b5a2014-07-17 19:25:21 +0100161 profile_check = 'profile == gltrace::PROFILE_COMPAT'
Chia-I Wub3d218d2011-11-03 01:37:36 +0800162 if camelcase_name in self.arrays_es1:
José Fonseca8b04b5a2014-07-17 19:25:21 +0100163 profile_check = '(' + profile_check + ' || profile == gltrace::PROFILE_ES1)';
Chia-I Wub3d218d2011-11-03 01:37:36 +0800164
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000165 function_name = 'gl%sPointer' % camelcase_name
166 enable_name = 'GL_%s_ARRAY' % uppercase_name
167 binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name
168 print ' // %s' % function_name
Chia-I Wub3d218d2011-11-03 01:37:36 +0800169 print ' if (%s) {' % profile_check
José Fonsecafb6744f2011-04-15 11:18:37 +0100170 self.array_prolog(api, uppercase_name)
José Fonseca8b04b5a2014-07-17 19:25:21 +0100171 print ' if (_glIsEnabled(%s) &&' % enable_name
172 print ' _glGetInteger(%s) == 0) {' % binding_name
José Fonsecafb6744f2011-04-15 11:18:37 +0100173 self.array_cleanup(api, uppercase_name)
José Fonseca8b04b5a2014-07-17 19:25:21 +0100174 print ' return true;'
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000175 print ' }'
José Fonsecafb6744f2011-04-15 11:18:37 +0100176 self.array_epilog(api, uppercase_name)
Chia-I Wub3d218d2011-11-03 01:37:36 +0800177 print ' }'
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000178 print
José Fonseca7f5163e2011-03-31 23:37:26 +0100179
Chia-I Wub3d218d2011-11-03 01:37:36 +0800180 print ' // ES1 does not support generic vertex attributes'
José Fonseca8b04b5a2014-07-17 19:25:21 +0100181 print ' if (profile == gltrace::PROFILE_ES1)'
Chia-I Wub3d218d2011-11-03 01:37:36 +0800182 print ' return false;'
183 print
José Fonseca632a78d2012-04-19 07:18:59 +0100184 print ' vertex_attrib _vertex_attrib = _get_vertex_attrib();'
José Fonseca5a568a92011-06-29 16:43:36 +0100185 print
186 print ' // glVertexAttribPointer'
José Fonseca632a78d2012-04-19 07:18:59 +0100187 print ' if (_vertex_attrib == VERTEX_ATTRIB) {'
José Fonseca26be8f92014-03-07 14:08:50 +0000188 print ' GLint _max_vertex_attribs = _glGetInteger(GL_MAX_VERTEX_ATTRIBS);'
José Fonseca632a78d2012-04-19 07:18:59 +0100189 print ' for (GLint index = 0; index < _max_vertex_attribs; ++index) {'
José Fonseca8b04b5a2014-07-17 19:25:21 +0100190 print ' if (_glGetVertexAttribi(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED) &&'
191 print ' _glGetVertexAttribi(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING) == 0) {'
192 print ' return true;'
José Fonseca5a568a92011-06-29 16:43:36 +0100193 print ' }'
194 print ' }'
195 print ' }'
196 print
José Fonseca5a568a92011-06-29 16:43:36 +0100197 print ' // glVertexAttribPointerNV'
José Fonseca632a78d2012-04-19 07:18:59 +0100198 print ' if (_vertex_attrib == VERTEX_ATTRIB_NV) {'
José Fonseca5a568a92011-06-29 16:43:36 +0100199 print ' for (GLint index = 0; index < 16; ++index) {'
José Fonseca8b04b5a2014-07-17 19:25:21 +0100200 print ' if (_glIsEnabled(GL_VERTEX_ATTRIB_ARRAY0_NV + index)) {'
José Fonseca5a568a92011-06-29 16:43:36 +0100201 print ' return true;'
José Fonseca1a2fdd22011-04-01 00:55:09 +0100202 print ' }'
203 print ' }'
204 print ' }'
205 print
206
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000207 print ' return false;'
208 print '}'
209 print
José Fonseca669b1222011-02-20 09:05:10 +0000210
José Fonseca14cb9ef2012-05-17 21:33:14 +0100211 print 'static void _trace_user_arrays(GLuint count);'
José Fonseca14c21bc2011-02-20 23:32:22 +0000212 print
José Fonseca867b1b72011-04-24 11:58:04 +0100213
José Fonseca707630d2014-03-07 14:20:35 +0000214 print '// whether glLockArraysEXT() has ever been called'
215 print 'static bool _checkLockArraysEXT = false;'
216 print
217
José Fonseca9c536b02012-02-29 20:54:13 +0000218 # Buffer mappings
219 print '// whether glMapBufferRange(GL_MAP_WRITE_BIT) has ever been called'
José Fonseca632a78d2012-04-19 07:18:59 +0100220 print 'static bool _checkBufferMapRange = false;'
José Fonseca9c536b02012-02-29 20:54:13 +0000221 print
222 print '// whether glBufferParameteriAPPLE(GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE) has ever been called'
José Fonseca632a78d2012-04-19 07:18:59 +0100223 print 'static bool _checkBufferFlushingUnmapAPPLE = false;'
José Fonseca9c536b02012-02-29 20:54:13 +0000224 print
225 # Buffer mapping information, necessary for old Mesa 2.1 drivers which
226 # do not support glGetBufferParameteriv(GL_BUFFER_ACCESS_FLAGS/GL_BUFFER_MAP_LENGTH)
José Fonseca867b1b72011-04-24 11:58:04 +0100227 print 'struct buffer_mapping {'
228 print ' void *map;'
229 print ' GLint length;'
230 print ' bool write;'
José Fonseca73373602011-05-20 17:45:26 +0100231 print ' bool explicit_flush;'
José Fonseca867b1b72011-04-24 11:58:04 +0100232 print '};'
233 print
234 for target in self.buffer_targets:
José Fonseca632a78d2012-04-19 07:18:59 +0100235 print 'struct buffer_mapping _%s_mapping;' % target.lower();
José Fonseca867b1b72011-04-24 11:58:04 +0100236 print
237 print 'static inline struct buffer_mapping *'
238 print 'get_buffer_mapping(GLenum target) {'
José Fonseca06e85192011-10-16 14:15:36 +0100239 print ' switch (target) {'
José Fonseca867b1b72011-04-24 11:58:04 +0100240 for target in self.buffer_targets:
241 print ' case GL_%s:' % target
José Fonseca632a78d2012-04-19 07:18:59 +0100242 print ' return & _%s_mapping;' % target.lower()
José Fonseca867b1b72011-04-24 11:58:04 +0100243 print ' default:'
José Fonseca559d5342011-10-27 08:10:56 +0100244 print ' os::log("apitrace: warning: unknown buffer target 0x%04X\\n", target);'
José Fonseca867b1b72011-04-24 11:58:04 +0100245 print ' return NULL;'
246 print ' }'
247 print '}'
248 print
249
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100250 # Generate a helper function to determine whether a parameter name
251 # refers to a symbolic value or not
252 print 'static bool'
253 print 'is_symbolic_pname(GLenum pname) {'
José Fonseca06e85192011-10-16 14:15:36 +0100254 print ' switch (pname) {'
José Fonseca5ea91872011-05-04 09:41:55 +0100255 for function, type, count, name in glparams.parameters:
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100256 if type is glapi.GLenum:
José Fonseca4c938c22011-04-30 22:44:38 +0100257 print ' case %s:' % name
258 print ' return true;'
259 print ' default:'
260 print ' return false;'
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100261 print ' }'
262 print '}'
263 print
264
265 # Generate a helper function to determine whether a parameter value is
266 # potentially symbolic or not; i.e., if the value can be represented in
267 # an enum or not
268 print 'template<class T>'
269 print 'static inline bool'
270 print 'is_symbolic_param(T param) {'
271 print ' return static_cast<T>(static_cast<GLenum>(param)) == param;'
272 print '}'
273 print
José Fonseca4c938c22011-04-30 22:44:38 +0100274
275 # Generate a helper function to know how many elements a parameter has
276 print 'static size_t'
José Fonseca632a78d2012-04-19 07:18:59 +0100277 print '_gl_param_size(GLenum pname) {'
José Fonseca06e85192011-10-16 14:15:36 +0100278 print ' switch (pname) {'
José Fonseca5ea91872011-05-04 09:41:55 +0100279 for function, type, count, name in glparams.parameters:
José Fonseca4c938c22011-04-30 22:44:38 +0100280 if type is not None:
José Fonsecaddf6d2c2012-12-20 15:34:50 +0000281 print ' case %s: return %s;' % (name, count)
José Fonseca4c938c22011-04-30 22:44:38 +0100282 print ' default:'
José Fonseca559d5342011-10-27 08:10:56 +0100283 print r' os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, pname);'
José Fonseca4c938c22011-04-30 22:44:38 +0100284 print ' return 1;'
285 print ' }'
286 print '}'
287 print
288
Chia-I Wu335efb42011-11-03 01:59:22 +0800289 # states such as GL_UNPACK_ROW_LENGTH are not available in GLES
290 print 'static inline bool'
291 print 'can_unpack_subimage(void) {'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000292 print ' gltrace::Context *ctx = gltrace::getContext();'
293 print ' return (ctx->profile == gltrace::PROFILE_COMPAT);'
Chia-I Wu335efb42011-11-03 01:59:22 +0800294 print '}'
295 print
296
José Fonseca631dbd12014-12-15 16:34:45 +0000297 # VMWX_map_buffer_debug
298 print r'extern "C" PUBLIC'
299 print r'void APIENTRY'
300 print r'glNotifyMappedBufferRangeVMWX(const void * start, GLsizeiptr length) {'
301 self.emit_memcpy('start', 'length')
302 print r'}'
303 print
304
José Fonseca1b6c8752012-04-15 14:33:00 +0100305 getProcAddressFunctionNames = []
306
307 def traceApi(self, api):
308 if self.getProcAddressFunctionNames:
309 # Generate a function to wrap proc addresses
310 getProcAddressFunction = api.getFunctionByName(self.getProcAddressFunctionNames[0])
311 argType = getProcAddressFunction.args[0].type
312 retType = getProcAddressFunction.type
313
314 print 'static %s _wrapProcAddress(%s procName, %s procPtr);' % (retType, argType, retType)
315 print
316
317 Tracer.traceApi(self, api)
318
319 print 'static %s _wrapProcAddress(%s procName, %s procPtr) {' % (retType, argType, retType)
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700320
321 # Provide fallback functions to missing debug functions
José Fonseca1b6c8752012-04-15 14:33:00 +0100322 print ' if (!procPtr) {'
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700323 else_ = ''
324 for function_name in self.debug_functions:
325 if self.api.getFunctionByName(function_name):
326 print ' %sif (strcmp("%s", (const char *)procName) == 0) {' % (else_, function_name)
327 print ' return (%s)&%s;' % (retType, function_name)
328 print ' }'
329 else_ = 'else '
330 print ' %s{' % else_
331 print ' return NULL;'
332 print ' }'
José Fonseca1b6c8752012-04-15 14:33:00 +0100333 print ' }'
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700334
José Fonseca81301932012-11-11 00:10:20 +0000335 for function in api.getAllFunctions():
José Fonseca1b6c8752012-04-15 14:33:00 +0100336 ptype = function_pointer_type(function)
337 pvalue = function_pointer_value(function)
338 print ' if (strcmp("%s", (const char *)procName) == 0) {' % function.name
339 print ' %s = (%s)procPtr;' % (pvalue, ptype)
340 print ' return (%s)&%s;' % (retType, function.name,)
341 print ' }'
342 print ' os::log("apitrace: warning: unknown function \\"%s\\"\\n", (const char *)procName);'
343 print ' return procPtr;'
344 print '}'
345 print
346 else:
347 Tracer.traceApi(self, api)
348
Imre Deakd4937372012-04-24 14:06:48 +0300349 def defineShadowBufferHelper(self):
350 print 'void _shadow_glGetBufferSubData(GLenum target, GLintptr offset,'
351 print ' GLsizeiptr size, GLvoid *data)'
352 print '{'
José Fonsecaa33d0bb2012-11-10 09:11:42 +0000353 print ' gltrace::Context *ctx = gltrace::getContext();'
Imre Deakd4937372012-04-24 14:06:48 +0300354 print ' if (!ctx->needsShadowBuffers() || target != GL_ELEMENT_ARRAY_BUFFER) {'
José Fonseca219c9f22012-11-03 10:13:17 +0000355 print ' _glGetBufferSubData(target, offset, size, data);'
Imre Deakd4937372012-04-24 14:06:48 +0300356 print ' return;'
357 print ' }'
358 print
José Fonseca26be8f92014-03-07 14:08:50 +0000359 print ' GLint buffer_binding = _glGetInteger(GL_ELEMENT_ARRAY_BUFFER_BINDING);'
José Fonseca219c9f22012-11-03 10:13:17 +0000360 print ' if (buffer_binding > 0) {'
361 print ' gltrace::Buffer & buf = ctx->buffers[buffer_binding];'
362 print ' buf.getSubData(offset, size, data);'
363 print ' }'
Imre Deakd4937372012-04-24 14:06:48 +0300364 print '}'
365
José Fonseca219c9f22012-11-03 10:13:17 +0000366 def shadowBufferMethod(self, method):
367 # Emit code to fetch the shadow buffer, and invoke a method
368 print ' gltrace::Context *ctx = gltrace::getContext();'
369 print ' if (ctx->needsShadowBuffers() && target == GL_ELEMENT_ARRAY_BUFFER) {'
José Fonseca26be8f92014-03-07 14:08:50 +0000370 print ' GLint buffer_binding = _glGetInteger(GL_ELEMENT_ARRAY_BUFFER_BINDING);'
José Fonseca219c9f22012-11-03 10:13:17 +0000371 print ' if (buffer_binding > 0) {'
372 print ' gltrace::Buffer & buf = ctx->buffers[buffer_binding];'
373 print ' buf.' + method + ';'
374 print ' }'
375 print ' }'
376 print
377
Imre Deakd4937372012-04-24 14:06:48 +0300378 def shadowBufferProlog(self, function):
379 if function.name == 'glBufferData':
José Fonseca219c9f22012-11-03 10:13:17 +0000380 self.shadowBufferMethod('bufferData(size, data)')
Imre Deakd4937372012-04-24 14:06:48 +0300381
382 if function.name == 'glBufferSubData':
José Fonseca219c9f22012-11-03 10:13:17 +0000383 self.shadowBufferMethod('bufferSubData(offset, size, data)')
Imre Deakd4937372012-04-24 14:06:48 +0300384
385 if function.name == 'glDeleteBuffers':
386 print ' gltrace::Context *ctx = gltrace::getContext();'
387 print ' if (ctx->needsShadowBuffers()) {'
José Fonseca219c9f22012-11-03 10:13:17 +0000388 print ' for (GLsizei i = 0; i < n; i++) {'
389 print ' ctx->buffers.erase(buffer[i]);'
Imre Deakd4937372012-04-24 14:06:48 +0300390 print ' }'
391 print ' }'
392
José Fonseca99221832011-03-22 22:15:46 +0000393 array_pointer_function_names = set((
394 "glVertexPointer",
395 "glNormalPointer",
396 "glColorPointer",
397 "glIndexPointer",
398 "glTexCoordPointer",
399 "glEdgeFlagPointer",
400 "glFogCoordPointer",
401 "glSecondaryColorPointer",
José Fonseca7f5163e2011-03-31 23:37:26 +0100402
José Fonsecaac5285b2011-05-04 11:09:08 +0100403 "glInterleavedArrays",
404
José Fonseca7e0bfd92011-04-30 23:09:03 +0100405 "glVertexPointerEXT",
406 "glNormalPointerEXT",
407 "glColorPointerEXT",
408 "glIndexPointerEXT",
409 "glTexCoordPointerEXT",
410 "glEdgeFlagPointerEXT",
411 "glFogCoordPointerEXT",
412 "glSecondaryColorPointerEXT",
José Fonseca99221832011-03-22 22:15:46 +0000413
José Fonseca7f5163e2011-03-31 23:37:26 +0100414 "glVertexAttribPointer",
415 "glVertexAttribPointerARB",
416 "glVertexAttribPointerNV",
José Fonsecaac5285b2011-05-04 11:09:08 +0100417 "glVertexAttribIPointer",
418 "glVertexAttribIPointerEXT",
José Fonseca7f5163e2011-03-31 23:37:26 +0100419 "glVertexAttribLPointer",
420 "glVertexAttribLPointerEXT",
José Fonseca99221832011-03-22 22:15:46 +0000421
422 #"glMatrixIndexPointerARB",
423 ))
424
José Fonsecac5bf77a2014-08-14 16:10:02 +0100425 # XXX: We currently ignore the gl*Draw*ElementArray* functions
426 draw_function_regex = re.compile(r'^gl([A-Z][a-z]+)*Draw(Range)?(Arrays|Elements)([A-Z][a-zA-Z]*)?$' )
José Fonseca99221832011-03-22 22:15:46 +0000427
José Fonsecac9f12232011-03-25 20:07:42 +0000428 interleaved_formats = [
429 'GL_V2F',
430 'GL_V3F',
431 'GL_C4UB_V2F',
432 'GL_C4UB_V3F',
433 'GL_C3F_V3F',
434 'GL_N3F_V3F',
435 'GL_C4F_N3F_V3F',
436 'GL_T2F_V3F',
437 'GL_T4F_V4F',
438 'GL_T2F_C4UB_V3F',
439 'GL_T2F_C3F_V3F',
440 'GL_T2F_N3F_V3F',
441 'GL_T2F_C4F_N3F_V3F',
442 'GL_T4F_C4F_N3F_V4F',
443 ]
444
José Fonseca54f304a2012-01-14 19:33:08 +0000445 def traceFunctionImplBody(self, function):
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000446 # Defer tracing of user array pointers...
José Fonseca99221832011-03-22 22:15:46 +0000447 if function.name in self.array_pointer_function_names:
José Fonseca26be8f92014-03-07 14:08:50 +0000448 print ' GLint _array_buffer = _glGetInteger(GL_ARRAY_BUFFER_BINDING);'
José Fonseca632a78d2012-04-19 07:18:59 +0100449 print ' if (!_array_buffer) {'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000450 print ' gltrace::Context *ctx = gltrace::getContext();'
Chia-I Wu8ef66972011-11-03 01:19:46 +0800451 print ' ctx->user_arrays = true;'
José Fonseca5a568a92011-06-29 16:43:36 +0100452 if function.name == "glVertexAttribPointerNV":
Chia-I Wu8ef66972011-11-03 01:19:46 +0800453 print ' ctx->user_arrays_nv = true;'
José Fonseca54f304a2012-01-14 19:33:08 +0000454 self.invokeFunction(function)
José Fonsecaac5285b2011-05-04 11:09:08 +0100455
456 # And also break down glInterleavedArrays into the individual calls
457 if function.name == 'glInterleavedArrays':
458 print
459
460 # Initialize the enable flags
461 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100462 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100463 print ' GLboolean %s = GL_FALSE;' % flag_name
464 print
465
466 # Switch for the interleaved formats
467 print ' switch (format) {'
468 for format in self.interleaved_formats:
469 print ' case %s:' % format
470 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100471 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100472 if format.find('_' + uppercase_name[0]) >= 0:
473 print ' %s = GL_TRUE;' % flag_name
474 print ' break;'
475 print ' default:'
476 print ' return;'
477 print ' }'
478 print
479
480 # Emit fake glEnableClientState/glDisableClientState flags
481 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100482 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100483 enable_name = 'GL_%s_ARRAY' % uppercase_name
484
485 # Emit a fake function
486 print ' {'
José Fonseca632a78d2012-04-19 07:18:59 +0100487 print ' static const trace::FunctionSig &_sig = %s ? _glEnableClientState_sig : _glDisableClientState_sig;' % flag_name
José Fonseca7a5f23a2014-06-24 19:20:36 +0100488 print ' unsigned _call = trace::localWriter.beginEnter(&_sig, true);'
José Fonsecab4a3d142011-10-27 07:43:19 +0100489 print ' trace::localWriter.beginArg(0);'
José Fonseca54f304a2012-01-14 19:33:08 +0000490 self.serializeValue(glapi.GLenum, enable_name)
José Fonsecab4a3d142011-10-27 07:43:19 +0100491 print ' trace::localWriter.endArg();'
492 print ' trace::localWriter.endEnter();'
José Fonseca632a78d2012-04-19 07:18:59 +0100493 print ' trace::localWriter.beginLeave(_call);'
José Fonsecab4a3d142011-10-27 07:43:19 +0100494 print ' trace::localWriter.endLeave();'
José Fonsecaac5285b2011-05-04 11:09:08 +0100495 print ' }'
496
José Fonsecac629a8c2014-06-01 21:12:27 +0100497 # Warn about buggy glGet(GL_*ARRAY_SIZE) not returning GL_BGRA
498 buggyFunctions = {
499 'glColorPointer': ('glGetIntegerv', '', 'GL_COLOR_ARRAY_SIZE'),
500 'glSecondaryColorPointer': ('glGetIntegerv', '', 'GL_SECONDARY_COLOR_ARRAY_SIZE'),
501 'glVertexAttribPointer': ('glGetVertexAttribiv', 'index, ', 'GL_VERTEX_ATTRIB_ARRAY_SIZE'),
502 'glVertexAttribPointerARB': ('glGetVertexAttribivARB', 'index, ', 'GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB'),
503 }
504 if function.name in buggyFunctions:
505 getter, extraArg, pname = buggyFunctions[function.name]
506 print r' static bool _checked = false;'
507 print r' if (!_checked && size == GL_BGRA) {'
508 print r' GLint _size = 0;'
509 print r' _%s(%s%s, &_size);' % (getter, extraArg, pname)
510 print r' if (_size != GL_BGRA) {'
511 print r' os::log("apitrace: warning: %s(%s) does not return GL_BGRA; trace will be incorrect (https://github.com/apitrace/apitrace/issues/261)\n");' % (getter, pname)
512 print r' }'
513 print r' _checked = true;'
514 print r' }'
515
José Fonseca99221832011-03-22 22:15:46 +0000516 print ' return;'
517 print ' }'
José Fonseca14c21bc2011-02-20 23:32:22 +0000518
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000519 # ... to the draw calls
José Fonsecac5bf77a2014-08-14 16:10:02 +0100520 if self.draw_function_regex.match(function.name):
José Fonseca632a78d2012-04-19 07:18:59 +0100521 print ' if (_need_user_arrays()) {'
José Fonseca246508e2014-08-14 16:07:46 +0100522 if 'Indirect' in function.name:
523 print r' os::log("apitrace: warning: %s: indirect user arrays not supported\n");' % (function.name,)
524 else:
525 arg_names = ', '.join([arg.name for arg in function.args[1:]])
526 print ' GLuint _count = _%s_count(%s);' % (function.name, arg_names)
527 # Some apps, in particular Quake3, can tell the driver to lock more
528 # vertices than those actually required for the draw call.
529 print ' if (_checkLockArraysEXT) {'
530 print ' GLuint _locked_count = _glGetInteger(GL_ARRAY_ELEMENT_LOCK_FIRST_EXT)'
531 print ' + _glGetInteger(GL_ARRAY_ELEMENT_LOCK_COUNT_EXT);'
532 print ' _count = std::max(_count, _locked_count);'
533 print ' }'
534 print ' _trace_user_arrays(_count);'
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000535 print ' }'
José Fonseca707630d2014-03-07 14:20:35 +0000536 if function.name == 'glLockArraysEXT':
537 print ' _checkLockArraysEXT = true;'
José Fonseca4a7d8602014-06-18 16:03:44 +0100538
539 # Warn if user arrays are used with glBegin/glArrayElement/glEnd.
540 if function.name == 'glBegin':
José Fonseca7c39d012014-11-07 19:46:53 +0000541 print r' gltrace::Context *ctx = gltrace::getContext();'
542 print r' ctx->userArraysOnBegin = _need_user_arrays();'
543 if function.name.startswith('glArrayElement'):
544 print r' gltrace::Context *ctx = gltrace::getContext();'
545 print r' if (ctx->userArraysOnBegin) {'
José Fonseca4a7d8602014-06-18 16:03:44 +0100546 print r' os::log("apitrace: warning: user arrays with glArrayElement not supported (https://github.com/apitrace/apitrace/issues/276)\n");'
José Fonseca7c39d012014-11-07 19:46:53 +0000547 print r' ctx->userArraysOnBegin = false;'
José Fonseca4a7d8602014-06-18 16:03:44 +0100548 print r' }'
José Fonseca14c21bc2011-02-20 23:32:22 +0000549
José Fonseca73373602011-05-20 17:45:26 +0100550 # Emit a fake memcpy on buffer uploads
José Fonseca9c536b02012-02-29 20:54:13 +0000551 if function.name == 'glBufferParameteriAPPLE':
552 print ' if (pname == GL_BUFFER_FLUSHING_UNMAP_APPLE && param == GL_FALSE) {'
José Fonseca632a78d2012-04-19 07:18:59 +0100553 print ' _checkBufferFlushingUnmapAPPLE = true;'
José Fonseca9c536b02012-02-29 20:54:13 +0000554 print ' }'
José Fonsecacdc322c2012-02-29 19:29:51 +0000555 if function.name in ('glUnmapBuffer', 'glUnmapBufferARB'):
José Fonseca9c536b02012-02-29 20:54:13 +0000556 if function.name.endswith('ARB'):
557 suffix = 'ARB'
558 else:
559 suffix = ''
560 print ' GLint access = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100561 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_ACCESS, &access);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000562 print ' if (access != GL_READ_ONLY) {'
563 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100564 print ' _glGetBufferPointerv%s(target, GL_BUFFER_MAP_POINTER, &map);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000565 print ' if (map) {'
566 print ' GLint length = -1;'
567 print ' bool flush = true;'
José Fonseca632a78d2012-04-19 07:18:59 +0100568 print ' if (_checkBufferMapRange) {'
569 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_MAP_LENGTH, &length);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000570 print ' GLint access_flags = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100571 print ' _glGetBufferParameteriv(target, GL_BUFFER_ACCESS_FLAGS, &access_flags);'
José Fonsecaa4210e22014-12-13 15:49:37 +0000572 print ' flush = flush && !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT));'
José Fonseca9c536b02012-02-29 20:54:13 +0000573 print ' if (length == -1) {'
José Fonsecacb07e2d2014-02-04 15:14:09 +0000574 print ' // Mesa drivers refuse GL_BUFFER_MAP_LENGTH without GL 3.0 up-to'
575 print ' // http://cgit.freedesktop.org/mesa/mesa/commit/?id=ffee498fb848b253a7833373fe5430f8c7ca0c5f'
José Fonsecadb1ccce2012-02-29 21:09:24 +0000576 print ' static bool warned = false;'
577 print ' if (!warned) {'
578 print ' os::log("apitrace: warning: glGetBufferParameteriv%s(GL_BUFFER_MAP_LENGTH) failed\\n");' % suffix
579 print ' warned = true;'
580 print ' }'
José Fonseca9c536b02012-02-29 20:54:13 +0000581 print ' struct buffer_mapping *mapping = get_buffer_mapping(target);'
582 print ' if (mapping) {'
583 print ' length = mapping->length;'
584 print ' flush = flush && !mapping->explicit_flush;'
585 print ' } else {'
586 print ' length = 0;'
587 print ' flush = false;'
588 print ' }'
589 print ' }'
590 print ' } else {'
591 print ' length = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100592 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_SIZE, &length);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000593 print ' }'
José Fonseca632a78d2012-04-19 07:18:59 +0100594 print ' if (_checkBufferFlushingUnmapAPPLE) {'
José Fonseca9c536b02012-02-29 20:54:13 +0000595 print ' GLint flushing_unmap = GL_TRUE;'
José Fonseca632a78d2012-04-19 07:18:59 +0100596 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_FLUSHING_UNMAP_APPLE, &flushing_unmap);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000597 print ' flush = flush && flushing_unmap;'
598 print ' }'
599 print ' if (flush && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100600 self.emit_memcpy('map', 'length')
José Fonseca9c536b02012-02-29 20:54:13 +0000601 print ' }'
602 print ' }'
603 print ' }'
José Fonsecacdc322c2012-02-29 19:29:51 +0000604 if function.name == 'glUnmapBufferOES':
605 print ' GLint access = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100606 print ' _glGetBufferParameteriv(target, GL_BUFFER_ACCESS_OES, &access);'
José Fonsecacdc322c2012-02-29 19:29:51 +0000607 print ' if (access == GL_WRITE_ONLY_OES) {'
608 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100609 print ' _glGetBufferPointervOES(target, GL_BUFFER_MAP_POINTER_OES, &map);'
José Fonsecacdc322c2012-02-29 19:29:51 +0000610 print ' GLint size = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100611 print ' _glGetBufferParameteriv(target, GL_BUFFER_SIZE, &size);'
José Fonsecacdc322c2012-02-29 19:29:51 +0000612 print ' if (map && size > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100613 self.emit_memcpy('map', 'size')
José Fonseca219c9f22012-11-03 10:13:17 +0000614 self.shadowBufferMethod('bufferSubData(0, size, map)')
José Fonsecacdc322c2012-02-29 19:29:51 +0000615 print ' }'
José Fonseca867b1b72011-04-24 11:58:04 +0100616 print ' }'
José Fonseca4920c302014-08-13 18:35:57 +0100617 if function.name == 'glUnmapNamedBuffer':
618 print ' GLint access_flags = 0;'
619 print ' _glGetNamedBufferParameteriv(buffer, GL_BUFFER_ACCESS_FLAGS, &access_flags);'
José Fonsecaa4210e22014-12-13 15:49:37 +0000620 print ' if ((access_flags & GL_MAP_WRITE_BIT) &&'
621 print ' !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT))) {'
José Fonseca4920c302014-08-13 18:35:57 +0100622 print ' GLvoid *map = NULL;'
623 print ' _glGetNamedBufferPointerv(buffer, GL_BUFFER_MAP_POINTER, &map);'
624 print ' GLint length = 0;'
625 print ' _glGetNamedBufferParameteriv(buffer, GL_BUFFER_MAP_LENGTH, &length);'
626 print ' if (map && length > 0) {'
627 self.emit_memcpy('map', 'length')
628 print ' }'
629 print ' }'
José Fonsecafb3bd602012-01-15 13:56:28 +0000630 if function.name == 'glUnmapNamedBufferEXT':
José Fonseca024aff42012-02-29 18:00:06 +0000631 print ' GLint access_flags = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100632 print ' _glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_ACCESS_FLAGS, &access_flags);'
José Fonsecaa4210e22014-12-13 15:49:37 +0000633 print ' if ((access_flags & GL_MAP_WRITE_BIT) &&'
634 print ' !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT))) {'
José Fonsecafb3bd602012-01-15 13:56:28 +0000635 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100636 print ' _glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &map);'
José Fonsecafb3bd602012-01-15 13:56:28 +0000637 print ' GLint length = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100638 print ' _glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_MAP_LENGTH, &length);'
José Fonseca024aff42012-02-29 18:00:06 +0000639 print ' if (map && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100640 self.emit_memcpy('map', 'length')
José Fonseca024aff42012-02-29 18:00:06 +0000641 print ' }'
José Fonsecafb3bd602012-01-15 13:56:28 +0000642 print ' }'
José Fonseca77ef0ce2012-02-29 18:08:48 +0000643 if function.name == 'glFlushMappedBufferRange':
644 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100645 print ' _glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &map);'
José Fonseca77ef0ce2012-02-29 18:08:48 +0000646 print ' if (map && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100647 self.emit_memcpy('(const char *)map + offset', 'length')
José Fonseca77ef0ce2012-02-29 18:08:48 +0000648 print ' }'
649 if function.name == 'glFlushMappedBufferRangeAPPLE':
650 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100651 print ' _glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &map);'
José Fonseca77ef0ce2012-02-29 18:08:48 +0000652 print ' if (map && size > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100653 self.emit_memcpy('(const char *)map + offset', 'size')
José Fonseca73373602011-05-20 17:45:26 +0100654 print ' }'
José Fonseca4920c302014-08-13 18:35:57 +0100655 if function.name == 'glFlushMappedNamedBufferRange':
656 print ' GLvoid *map = NULL;'
657 print ' _glGetNamedBufferPointerv(buffer, GL_BUFFER_MAP_POINTER, &map);'
658 print ' if (map && length > 0) {'
659 self.emit_memcpy('(const char *)map + offset', 'length')
660 print ' }'
José Fonsecafb3bd602012-01-15 13:56:28 +0000661 if function.name == 'glFlushMappedNamedBufferRangeEXT':
662 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100663 print ' _glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &map);'
José Fonseca024aff42012-02-29 18:00:06 +0000664 print ' if (map && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100665 self.emit_memcpy('(const char *)map + offset', 'length')
José Fonsecafb3bd602012-01-15 13:56:28 +0000666 print ' }'
José Fonseca867b1b72011-04-24 11:58:04 +0100667
José Fonseca3522cbd2014-02-28 14:45:32 +0000668 # FIXME: We don't support coherent/pinned memory mappings
José Fonseca631dbd12014-12-15 16:34:45 +0000669 if function.name in ('glBufferStorage', 'glNamedBufferStorage', 'glNamedBufferStorageEXT'):
670 print r' if (!(flags & GL_MAP_PERSISTENT_BIT)) {'
671 print r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_PERSISTENT_BIT\n", __FUNCTION__);'
672 print r' }'
673 print r' flags &= ~GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX;'
José Fonsecaa4210e22014-12-13 15:49:37 +0000674 if function.name in ('glMapBufferRange', 'glMapNamedBufferRange', 'glMapNamedBufferRangeEXT'):
José Fonseca631dbd12014-12-15 16:34:45 +0000675 print r' if (access & GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX) {'
676 print r' if (!(access & GL_MAP_PERSISTENT_BIT)) {'
677 print r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_PERSISTENT_BIT\n", __FUNCTION__);'
678 print r' }'
679 print r' if (access & GL_MAP_FLUSH_EXPLICIT_BIT) {'
680 print r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/ MAP_FLUSH_EXPLICIT_BIT\n", __FUNCTION__);'
681 print r' }'
682 print r' access &= ~GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX;'
683 print r' } else if (access & GL_MAP_COHERENT_BIT) {'
José Fonsecaa4210e22014-12-13 15:49:37 +0000684 print r' os::log("apitrace: warning: %s: MAP_COHERENT_BIT unsupported (https://github.com/apitrace/apitrace/issues/232)\n", __FUNCTION__);'
685 print r' } else if ((access & GL_MAP_PERSISTENT_BIT) &&'
686 print r' !(access & GL_MAP_FLUSH_EXPLICIT_BIT)) {'
687 print r' os::log("apitrace: warning: %s: MAP_PERSISTENT_BIT w/o FLUSH_EXPLICIT_BIT unsupported (https://github.com/apitrace/apitrace/issues/232)\n", __FUNCTION__);'
José Fonseca3522cbd2014-02-28 14:45:32 +0000688 print r' }'
689 if function.name in ('glBufferData', 'glBufferDataARB'):
690 print r' if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {'
691 print r' os::log("apitrace: warning: GL_AMD_pinned_memory not fully supported\n");'
692 print r' }'
693
José Fonsecad0f1e292014-11-13 13:21:51 +0000694 # TODO: We don't track GL_INTEL_map_texture mappings
695 if function.name == 'glMapTexture2DINTEL':
696 print r' if (access & GL_MAP_WRITE_BIT) {'
697 print r' os::log("apitrace: warning: GL_INTEL_map_texture not fully supported\n");'
698 print r' }'
699
José Fonseca91492d22011-05-23 21:20:31 +0100700 # Don't leave vertex attrib locations to chance. Instead emit fake
701 # glBindAttribLocation calls to ensure that the same locations will be
702 # used when retracing. Trying to remap locations after the fact would
703 # be an herculian task given that vertex attrib locations appear in
704 # many entry-points, including non-shader related ones.
705 if function.name == 'glLinkProgram':
José Fonseca54f304a2012-01-14 19:33:08 +0000706 Tracer.invokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100707 print ' GLint active_attributes = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100708 print ' _glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &active_attributes);'
José Fonseca7525e6f2011-09-28 09:04:56 +0100709 print ' for (GLint attrib = 0; attrib < active_attributes; ++attrib) {'
José Fonseca91492d22011-05-23 21:20:31 +0100710 print ' GLint size = 0;'
711 print ' GLenum type = 0;'
712 print ' GLchar name[256];'
713 # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
José Fonseca632a78d2012-04-19 07:18:59 +0100714 print ' _glGetActiveAttrib(program, attrib, sizeof name, NULL, &size, &type, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100715 print " if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {"
José Fonseca632a78d2012-04-19 07:18:59 +0100716 print ' GLint location = _glGetAttribLocation(program, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100717 print ' if (location >= 0) {'
José Fonseca1b6c8752012-04-15 14:33:00 +0100718 bind_function = glapi.glapi.getFunctionByName('glBindAttribLocation')
José Fonseca91492d22011-05-23 21:20:31 +0100719 self.fake_call(bind_function, ['program', 'location', 'name'])
José Fonseca2a794f52011-05-26 20:54:29 +0100720 print ' }'
José Fonseca91492d22011-05-23 21:20:31 +0100721 print ' }'
722 print ' }'
723 if function.name == 'glLinkProgramARB':
José Fonseca54f304a2012-01-14 19:33:08 +0000724 Tracer.invokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100725 print ' GLint active_attributes = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100726 print ' _glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_ATTRIBUTES_ARB, &active_attributes);'
José Fonseca7525e6f2011-09-28 09:04:56 +0100727 print ' for (GLint attrib = 0; attrib < active_attributes; ++attrib) {'
José Fonseca91492d22011-05-23 21:20:31 +0100728 print ' GLint size = 0;'
729 print ' GLenum type = 0;'
730 print ' GLcharARB name[256];'
731 # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
José Fonseca632a78d2012-04-19 07:18:59 +0100732 print ' _glGetActiveAttribARB(programObj, attrib, sizeof name, NULL, &size, &type, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100733 print " if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {"
José Fonseca632a78d2012-04-19 07:18:59 +0100734 print ' GLint location = _glGetAttribLocationARB(programObj, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100735 print ' if (location >= 0) {'
José Fonseca1b6c8752012-04-15 14:33:00 +0100736 bind_function = glapi.glapi.getFunctionByName('glBindAttribLocationARB')
José Fonseca91492d22011-05-23 21:20:31 +0100737 self.fake_call(bind_function, ['programObj', 'location', 'name'])
José Fonseca2a794f52011-05-26 20:54:29 +0100738 print ' }'
José Fonseca91492d22011-05-23 21:20:31 +0100739 print ' }'
740 print ' }'
741
Imre Deakd4937372012-04-24 14:06:48 +0300742 self.shadowBufferProlog(function)
743
José Fonseca54f304a2012-01-14 19:33:08 +0000744 Tracer.traceFunctionImplBody(self, function)
José Fonseca73373602011-05-20 17:45:26 +0100745
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700746 # These entrypoints are only expected to be implemented by tools;
747 # drivers will probably not implement them.
José Fonsecaf028a8f2012-02-15 23:33:35 +0000748 marker_functions = [
749 # GL_GREMEDY_string_marker
José Fonseca8f34d342011-07-15 20:16:40 +0100750 'glStringMarkerGREMEDY',
José Fonsecaf028a8f2012-02-15 23:33:35 +0000751 # GL_GREMEDY_frame_terminator
José Fonseca8f34d342011-07-15 20:16:40 +0100752 'glFrameTerminatorGREMEDY',
753 ]
754
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700755 # These entrypoints may be implemented by drivers, but are also very useful
756 # for debugging / analysis tools.
757 debug_functions = [
758 # GL_KHR_debug
759 'glDebugMessageControl',
760 'glDebugMessageInsert',
761 'glDebugMessageCallback',
762 'glGetDebugMessageLog',
763 'glPushDebugGroup',
764 'glPopDebugGroup',
765 'glObjectLabel',
766 'glGetObjectLabel',
767 'glObjectPtrLabel',
768 'glGetObjectPtrLabel',
769 # GL_ARB_debug_output
770 'glDebugMessageControlARB',
771 'glDebugMessageInsertARB',
772 'glDebugMessageCallbackARB',
773 'glGetDebugMessageLogARB',
774 # GL_AMD_debug_output
775 'glDebugMessageEnableAMD',
776 'glDebugMessageInsertAMD',
777 'glDebugMessageCallbackAMD',
778 'glGetDebugMessageLogAMD',
José Fonseca71f5a352014-07-28 12:19:50 +0100779 # GL_EXT_debug_label
780 'glLabelObjectEXT',
781 'glGetObjectLabelEXT',
782 # GL_EXT_debug_marker
783 'glInsertEventMarkerEXT',
784 'glPushGroupMarkerEXT',
785 'glPopGroupMarkerEXT',
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700786 ]
787
José Fonseca54f304a2012-01-14 19:33:08 +0000788 def invokeFunction(self, function):
José Fonseca91492d22011-05-23 21:20:31 +0100789 if function.name in ('glLinkProgram', 'glLinkProgramARB'):
790 # These functions have been dispatched already
791 return
792
José Fonseca2cfa02c2013-06-10 08:05:29 +0100793 Tracer.invokeFunction(self, function)
794
795 def doInvokeFunction(self, function):
796 # Same as invokeFunction() but called both when trace is enabled or disabled.
797 #
798 # Used to modify the behavior of GL entry-points.
799
800 # Override GL extensions
801 if function.name in ('glGetString', 'glGetIntegerv', 'glGetStringi'):
802 Tracer.doInvokeFunction(self, function, prefix = 'gltrace::_', suffix = '_override')
803 return
804
José Fonseca71f5a352014-07-28 12:19:50 +0100805 # We implement GL_GREMEDY_*, etc., and not the driver
José Fonsecaf028a8f2012-02-15 23:33:35 +0000806 if function.name in self.marker_functions:
José Fonseca8f34d342011-07-15 20:16:40 +0100807 return
808
Peter Lohrmann9d9eb812013-07-12 16:15:25 -0400809 # We may be faking KHR_debug, so ensure the pointer queries result is
810 # always zeroed to prevent dereference of unitialized pointers
811 if function.name == 'glGetPointerv':
812 print ' if (params &&'
813 print ' (pname == GL_DEBUG_CALLBACK_FUNCTION ||'
814 print ' pname == GL_DEBUG_CALLBACK_USER_PARAM)) {'
815 print ' *params = NULL;'
816 print ' }'
817
José Fonseca2cfa02c2013-06-10 08:05:29 +0100818 if function.name in self.getProcAddressFunctionNames:
José Fonseca631dbd12014-12-15 16:34:45 +0000819 nameArg = function.args[0].name
820 print ' if (strcmp("glNotifyMappedBufferRangeVMWX", (const char *)%s) == 0) {' % (nameArg,)
821 print ' _result = (%s)&glNotifyMappedBufferRangeVMWX;' % (function.type,)
José Fonsecaf028a8f2012-02-15 23:33:35 +0000822 for marker_function in self.marker_functions:
José Fonseca1b6c8752012-04-15 14:33:00 +0100823 if self.api.getFunctionByName(marker_function):
José Fonseca631dbd12014-12-15 16:34:45 +0000824 print ' } else if (strcmp("%s", (const char *)%s) == 0) {' % (marker_function, nameArg)
José Fonseca632a78d2012-04-19 07:18:59 +0100825 print ' _result = (%s)&%s;' % (function.type, marker_function)
José Fonseca631dbd12014-12-15 16:34:45 +0000826 print ' } else {'
José Fonseca2cfa02c2013-06-10 08:05:29 +0100827 Tracer.doInvokeFunction(self, function)
828
829 # Replace function addresses with ours
830 # XXX: Doing this here instead of wrapRet means that the trace will
831 # contain the addresses of the wrapper functions, and not the real
832 # functions, but in practice this should make no difference.
833 if function.name in self.getProcAddressFunctionNames:
José Fonseca631dbd12014-12-15 16:34:45 +0000834 print ' _result = _wrapProcAddress(%s, _result);' % (nameArg,)
José Fonseca2cfa02c2013-06-10 08:05:29 +0100835
José Fonseca8f34d342011-07-15 20:16:40 +0100836 print ' }'
José Fonseca1b3d3752011-07-15 10:15:19 +0100837 return
838
José Fonseca2cfa02c2013-06-10 08:05:29 +0100839 Tracer.doInvokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100840
José Fonseca867b1b72011-04-24 11:58:04 +0100841 buffer_targets = [
842 'ARRAY_BUFFER',
843 'ELEMENT_ARRAY_BUFFER',
844 'PIXEL_PACK_BUFFER',
845 'PIXEL_UNPACK_BUFFER',
José Fonseca7b20d672012-01-10 19:13:58 +0000846 'UNIFORM_BUFFER',
847 'TEXTURE_BUFFER',
848 'TRANSFORM_FEEDBACK_BUFFER',
849 'COPY_READ_BUFFER',
850 'COPY_WRITE_BUFFER',
851 'DRAW_INDIRECT_BUFFER',
852 'ATOMIC_COUNTER_BUFFER',
José Fonseca867b1b72011-04-24 11:58:04 +0100853 ]
854
José Fonseca54f304a2012-01-14 19:33:08 +0000855 def wrapRet(self, function, instance):
856 Tracer.wrapRet(self, function, instance)
José Fonseca1b3d3752011-07-15 10:15:19 +0100857
José Fonsecacdc322c2012-02-29 19:29:51 +0000858 # Keep track of buffer mappings
859 if function.name in ('glMapBuffer', 'glMapBufferARB'):
José Fonseca867b1b72011-04-24 11:58:04 +0100860 print ' struct buffer_mapping *mapping = get_buffer_mapping(target);'
861 print ' if (mapping) {'
862 print ' mapping->map = %s;' % (instance)
863 print ' mapping->length = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100864 print ' _glGetBufferParameteriv(target, GL_BUFFER_SIZE, &mapping->length);'
José Fonseca867b1b72011-04-24 11:58:04 +0100865 print ' mapping->write = (access != GL_READ_ONLY);'
José Fonseca73373602011-05-20 17:45:26 +0100866 print ' mapping->explicit_flush = false;'
José Fonseca867b1b72011-04-24 11:58:04 +0100867 print ' }'
José Fonseca867b1b72011-04-24 11:58:04 +0100868 if function.name == 'glMapBufferRange':
José Fonseca9c536b02012-02-29 20:54:13 +0000869 print ' if (access & GL_MAP_WRITE_BIT) {'
José Fonseca632a78d2012-04-19 07:18:59 +0100870 print ' _checkBufferMapRange = true;'
José Fonseca9c536b02012-02-29 20:54:13 +0000871 print ' }'
José Fonseca867b1b72011-04-24 11:58:04 +0100872 print ' struct buffer_mapping *mapping = get_buffer_mapping(target);'
873 print ' if (mapping) {'
874 print ' mapping->map = %s;' % (instance)
875 print ' mapping->length = length;'
876 print ' mapping->write = access & GL_MAP_WRITE_BIT;'
José Fonseca73373602011-05-20 17:45:26 +0100877 print ' mapping->explicit_flush = access & GL_MAP_FLUSH_EXPLICIT_BIT;'
José Fonseca867b1b72011-04-24 11:58:04 +0100878 print ' }'
José Fonseca669b1222011-02-20 09:05:10 +0000879
José Fonsecac9f12232011-03-25 20:07:42 +0000880 boolean_names = [
881 'GL_FALSE',
882 'GL_TRUE',
883 ]
884
885 def gl_boolean(self, value):
886 return self.boolean_names[int(bool(value))]
887
José Fonsecaa442a462014-11-12 21:16:22 +0000888 # Regular expression for the names of the functions that unpack from a
889 # pixel buffer object. See the ARB_pixel_buffer_object specification.
890 unpack_function_regex = re.compile(r'^gl(' + r'|'.join([
891 r'Bitmap',
892 r'PolygonStipple',
893 r'PixelMap[a-z]+v',
894 r'DrawPixels',
895 r'Color(Sub)?Table',
896 r'(Convolution|Separable)Filter[12]D',
897 r'(Compressed)?(Multi)?Tex(ture)?(Sub)?Image[1-4]D',
898 ]) + r')[0-9A-Z]*$')
José Fonsecae97bab92011-06-02 23:15:11 +0100899
José Fonseca54f304a2012-01-14 19:33:08 +0000900 def serializeArgValue(self, function, arg):
José Fonsecae97bab92011-06-02 23:15:11 +0100901 # Recognize offsets instead of blobs when a PBO is bound
José Fonsecaa442a462014-11-12 21:16:22 +0000902 if self.unpack_function_regex.match(function.name) \
José Fonsecae97bab92011-06-02 23:15:11 +0100903 and (isinstance(arg.type, stdapi.Blob) \
904 or (isinstance(arg.type, stdapi.Const) \
905 and isinstance(arg.type.type, stdapi.Blob))):
José Fonsecac29f4f12011-06-11 12:19:05 +0100906 print ' {'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000907 print ' gltrace::Context *ctx = gltrace::getContext();'
José Fonseca632a78d2012-04-19 07:18:59 +0100908 print ' GLint _unpack_buffer = 0;'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000909 print ' if (ctx->profile == gltrace::PROFILE_COMPAT)'
José Fonseca632a78d2012-04-19 07:18:59 +0100910 print ' _glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &_unpack_buffer);'
911 print ' if (_unpack_buffer) {'
José Fonsecad559f022012-04-15 16:13:51 +0100912 print ' trace::localWriter.writePointer((uintptr_t)%s);' % arg.name
José Fonsecac29f4f12011-06-11 12:19:05 +0100913 print ' } else {'
José Fonseca54f304a2012-01-14 19:33:08 +0000914 Tracer.serializeArgValue(self, function, arg)
José Fonsecac29f4f12011-06-11 12:19:05 +0100915 print ' }'
José Fonsecae97bab92011-06-02 23:15:11 +0100916 print ' }'
917 return
918
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100919 # Several GL state functions take GLenum symbolic names as
920 # integer/floats; so dump the symbolic name whenever possible
José Fonseca3bcb33c2011-05-27 20:14:31 +0100921 if function.name.startswith('gl') \
José Fonsecae3571092011-10-13 08:26:27 +0100922 and arg.type in (glapi.GLint, glapi.GLfloat, glapi.GLdouble) \
José Fonseca3bcb33c2011-05-27 20:14:31 +0100923 and arg.name == 'param':
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100924 assert arg.index > 0
925 assert function.args[arg.index - 1].name == 'pname'
926 assert function.args[arg.index - 1].type == glapi.GLenum
927 print ' if (is_symbolic_pname(pname) && is_symbolic_param(%s)) {' % arg.name
José Fonseca54f304a2012-01-14 19:33:08 +0000928 self.serializeValue(glapi.GLenum, arg.name)
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100929 print ' } else {'
José Fonseca54f304a2012-01-14 19:33:08 +0000930 Tracer.serializeArgValue(self, function, arg)
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100931 print ' }'
932 return
933
José Fonseca54f304a2012-01-14 19:33:08 +0000934 Tracer.serializeArgValue(self, function, arg)
José Fonseca99221832011-03-22 22:15:46 +0000935
José Fonseca4c938c22011-04-30 22:44:38 +0100936 def footer(self, api):
937 Tracer.footer(self, api)
José Fonseca669b1222011-02-20 09:05:10 +0000938
José Fonseca4c938c22011-04-30 22:44:38 +0100939 # A simple state tracker to track the pointer values
José Fonseca669b1222011-02-20 09:05:10 +0000940 # update the state
José Fonseca14cb9ef2012-05-17 21:33:14 +0100941 print 'static void _trace_user_arrays(GLuint count)'
José Fonseca669b1222011-02-20 09:05:10 +0000942 print '{'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000943 print ' gltrace::Context *ctx = gltrace::getContext();'
José Fonseca8d1408b2014-02-03 19:57:18 +0000944 print
945
946 # Temporarily unbind the array buffer
José Fonseca26be8f92014-03-07 14:08:50 +0000947 print ' GLint _array_buffer = _glGetInteger(GL_ARRAY_BUFFER_BINDING);'
José Fonseca8d1408b2014-02-03 19:57:18 +0000948 print ' if (_array_buffer) {'
949 self.fake_glBindBuffer(api, 'GL_ARRAY_BUFFER', '0')
950 print ' }'
951 print
José Fonseca1a2fdd22011-04-01 00:55:09 +0100952
José Fonseca99221832011-03-22 22:15:46 +0000953 for camelcase_name, uppercase_name in self.arrays:
Chia-I Wub3d218d2011-11-03 01:37:36 +0800954 # in which profile is the array available?
José Fonsecaf028a8f2012-02-15 23:33:35 +0000955 profile_check = 'ctx->profile == gltrace::PROFILE_COMPAT'
Chia-I Wub3d218d2011-11-03 01:37:36 +0800956 if camelcase_name in self.arrays_es1:
José Fonsecaf028a8f2012-02-15 23:33:35 +0000957 profile_check = '(' + profile_check + ' || ctx->profile == gltrace::PROFILE_ES1)';
Chia-I Wub3d218d2011-11-03 01:37:36 +0800958
José Fonseca99221832011-03-22 22:15:46 +0000959 function_name = 'gl%sPointer' % camelcase_name
960 enable_name = 'GL_%s_ARRAY' % uppercase_name
961 binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name
José Fonseca1b6c8752012-04-15 14:33:00 +0100962 function = api.getFunctionByName(function_name)
José Fonseca99221832011-03-22 22:15:46 +0000963
José Fonseca06e85192011-10-16 14:15:36 +0100964 print ' // %s' % function.prototype()
Chia-I Wub3d218d2011-11-03 01:37:36 +0800965 print ' if (%s) {' % profile_check
José Fonsecafb6744f2011-04-15 11:18:37 +0100966 self.array_trace_prolog(api, uppercase_name)
967 self.array_prolog(api, uppercase_name)
José Fonseca632a78d2012-04-19 07:18:59 +0100968 print ' if (_glIsEnabled(%s)) {' % enable_name
José Fonseca26be8f92014-03-07 14:08:50 +0000969 print ' GLint _binding = _glGetInteger(%s);' % binding_name
José Fonseca632a78d2012-04-19 07:18:59 +0100970 print ' if (!_binding) {'
José Fonseca99221832011-03-22 22:15:46 +0000971
972 # Get the arguments via glGet*
973 for arg in function.args:
974 arg_get_enum = 'GL_%s_ARRAY_%s' % (uppercase_name, arg.name.upper())
975 arg_get_function, arg_type = TypeGetter().visit(arg.type)
José Fonseca7f5163e2011-03-31 23:37:26 +0100976 print ' %s %s = 0;' % (arg_type, arg.name)
José Fonseca632a78d2012-04-19 07:18:59 +0100977 print ' _%s(%s, &%s);' % (arg_get_function, arg_get_enum, arg.name)
José Fonseca99221832011-03-22 22:15:46 +0000978
979 arg_names = ', '.join([arg.name for arg in function.args[:-1]])
José Fonseca14cb9ef2012-05-17 21:33:14 +0100980 print ' size_t _size = _%s_size(%s, count);' % (function.name, arg_names)
José Fonseca99221832011-03-22 22:15:46 +0000981
982 # Emit a fake function
José Fonsecafb6744f2011-04-15 11:18:37 +0100983 self.array_trace_intermezzo(api, uppercase_name)
José Fonseca7a5f23a2014-06-24 19:20:36 +0100984 print ' unsigned _call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,)
José Fonseca669b1222011-02-20 09:05:10 +0000985 for arg in function.args:
986 assert not arg.output
José Fonsecab4a3d142011-10-27 07:43:19 +0100987 print ' trace::localWriter.beginArg(%u);' % (arg.index,)
José Fonseca14c21bc2011-02-20 23:32:22 +0000988 if arg.name != 'pointer':
José Fonseca54f304a2012-01-14 19:33:08 +0000989 self.serializeValue(arg.type, arg.name)
José Fonseca14c21bc2011-02-20 23:32:22 +0000990 else:
José Fonseca632a78d2012-04-19 07:18:59 +0100991 print ' trace::localWriter.writeBlob((const void *)%s, _size);' % (arg.name)
José Fonsecab4a3d142011-10-27 07:43:19 +0100992 print ' trace::localWriter.endArg();'
José Fonseca99221832011-03-22 22:15:46 +0000993
José Fonsecab4a3d142011-10-27 07:43:19 +0100994 print ' trace::localWriter.endEnter();'
José Fonseca632a78d2012-04-19 07:18:59 +0100995 print ' trace::localWriter.beginLeave(_call);'
José Fonsecab4a3d142011-10-27 07:43:19 +0100996 print ' trace::localWriter.endLeave();'
José Fonseca99221832011-03-22 22:15:46 +0000997 print ' }'
José Fonseca669b1222011-02-20 09:05:10 +0000998 print ' }'
José Fonsecafb6744f2011-04-15 11:18:37 +0100999 self.array_epilog(api, uppercase_name)
1000 self.array_trace_epilog(api, uppercase_name)
Chia-I Wub3d218d2011-11-03 01:37:36 +08001001 print ' }'
José Fonseca99221832011-03-22 22:15:46 +00001002 print
José Fonseca1a2fdd22011-04-01 00:55:09 +01001003
José Fonseca1601c412011-05-10 10:38:19 +01001004 # Samething, but for glVertexAttribPointer*
1005 #
1006 # Some variants of glVertexAttribPointer alias conventional and generic attributes:
1007 # - glVertexAttribPointer: no
1008 # - glVertexAttribPointerARB: implementation dependent
1009 # - glVertexAttribPointerNV: yes
1010 #
1011 # This means that the implementations of these functions do not always
1012 # alias, and they need to be considered independently.
1013 #
Chia-I Wub3d218d2011-11-03 01:37:36 +08001014 print ' // ES1 does not support generic vertex attributes'
José Fonsecaf028a8f2012-02-15 23:33:35 +00001015 print ' if (ctx->profile == gltrace::PROFILE_ES1)'
Chia-I Wub3d218d2011-11-03 01:37:36 +08001016 print ' return;'
1017 print
José Fonseca632a78d2012-04-19 07:18:59 +01001018 print ' vertex_attrib _vertex_attrib = _get_vertex_attrib();'
José Fonseca5a568a92011-06-29 16:43:36 +01001019 print
José Fonseca74b661a2014-07-17 19:10:24 +01001020 for suffix in ['', 'NV']:
José Fonseca5a568a92011-06-29 16:43:36 +01001021 if suffix:
José Fonsecad94aaac2011-06-28 20:50:49 +01001022 SUFFIX = '_' + suffix
José Fonsecad94aaac2011-06-28 20:50:49 +01001023 else:
José Fonseca5a568a92011-06-29 16:43:36 +01001024 SUFFIX = suffix
José Fonseca1601c412011-05-10 10:38:19 +01001025 function_name = 'glVertexAttribPointer' + suffix
José Fonseca1b6c8752012-04-15 14:33:00 +01001026 function = api.getFunctionByName(function_name)
José Fonseca06e85192011-10-16 14:15:36 +01001027
1028 print ' // %s' % function.prototype()
José Fonseca632a78d2012-04-19 07:18:59 +01001029 print ' if (_vertex_attrib == VERTEX_ATTRIB%s) {' % SUFFIX
José Fonseca5a568a92011-06-29 16:43:36 +01001030 if suffix == 'NV':
José Fonseca632a78d2012-04-19 07:18:59 +01001031 print ' GLint _max_vertex_attribs = 16;'
José Fonseca5a568a92011-06-29 16:43:36 +01001032 else:
José Fonseca26be8f92014-03-07 14:08:50 +00001033 print ' GLint _max_vertex_attribs = _glGetInteger(GL_MAX_VERTEX_ATTRIBS);'
José Fonseca632a78d2012-04-19 07:18:59 +01001034 print ' for (GLint index = 0; index < _max_vertex_attribs; ++index) {'
1035 print ' GLint _enabled = 0;'
José Fonseca5a568a92011-06-29 16:43:36 +01001036 if suffix == 'NV':
José Fonseca632a78d2012-04-19 07:18:59 +01001037 print ' _glGetIntegerv(GL_VERTEX_ATTRIB_ARRAY0_NV + index, &_enabled);'
José Fonseca5a568a92011-06-29 16:43:36 +01001038 else:
José Fonseca632a78d2012-04-19 07:18:59 +01001039 print ' _glGetVertexAttribiv%s(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED%s, &_enabled);' % (suffix, SUFFIX)
1040 print ' if (_enabled) {'
1041 print ' GLint _binding = 0;'
José Fonseca5a568a92011-06-29 16:43:36 +01001042 if suffix != 'NV':
1043 # It doesn't seem possible to use VBOs with NV_vertex_program.
José Fonseca632a78d2012-04-19 07:18:59 +01001044 print ' _glGetVertexAttribiv%s(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING%s, &_binding);' % (suffix, SUFFIX)
1045 print ' if (!_binding) {'
José Fonseca1a2fdd22011-04-01 00:55:09 +01001046
José Fonseca1601c412011-05-10 10:38:19 +01001047 # Get the arguments via glGet*
1048 for arg in function.args[1:]:
José Fonseca5a568a92011-06-29 16:43:36 +01001049 if suffix == 'NV':
1050 arg_get_enum = 'GL_ATTRIB_ARRAY_%s%s' % (arg.name.upper(), SUFFIX)
1051 else:
1052 arg_get_enum = 'GL_VERTEX_ATTRIB_ARRAY_%s%s' % (arg.name.upper(), SUFFIX)
José Fonsecac493e3e2011-06-29 12:57:06 +01001053 arg_get_function, arg_type = TypeGetter('glGetVertexAttrib', False, suffix).visit(arg.type)
José Fonseca5a568a92011-06-29 16:43:36 +01001054 print ' %s %s = 0;' % (arg_type, arg.name)
José Fonseca632a78d2012-04-19 07:18:59 +01001055 print ' _%s(index, %s, &%s);' % (arg_get_function, arg_get_enum, arg.name)
José Fonseca1601c412011-05-10 10:38:19 +01001056
1057 arg_names = ', '.join([arg.name for arg in function.args[1:-1]])
José Fonseca14cb9ef2012-05-17 21:33:14 +01001058 print ' size_t _size = _%s_size(%s, count);' % (function.name, arg_names)
José Fonseca1a2fdd22011-04-01 00:55:09 +01001059
José Fonseca1601c412011-05-10 10:38:19 +01001060 # Emit a fake function
José Fonseca7a5f23a2014-06-24 19:20:36 +01001061 print ' unsigned _call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,)
José Fonseca1601c412011-05-10 10:38:19 +01001062 for arg in function.args:
1063 assert not arg.output
José Fonsecab4a3d142011-10-27 07:43:19 +01001064 print ' trace::localWriter.beginArg(%u);' % (arg.index,)
José Fonseca1601c412011-05-10 10:38:19 +01001065 if arg.name != 'pointer':
José Fonseca54f304a2012-01-14 19:33:08 +00001066 self.serializeValue(arg.type, arg.name)
José Fonseca1601c412011-05-10 10:38:19 +01001067 else:
José Fonseca632a78d2012-04-19 07:18:59 +01001068 print ' trace::localWriter.writeBlob((const void *)%s, _size);' % (arg.name)
José Fonsecab4a3d142011-10-27 07:43:19 +01001069 print ' trace::localWriter.endArg();'
José Fonseca1601c412011-05-10 10:38:19 +01001070
José Fonsecab4a3d142011-10-27 07:43:19 +01001071 print ' trace::localWriter.endEnter();'
José Fonseca632a78d2012-04-19 07:18:59 +01001072 print ' trace::localWriter.beginLeave(_call);'
José Fonsecab4a3d142011-10-27 07:43:19 +01001073 print ' trace::localWriter.endLeave();'
José Fonseca1601c412011-05-10 10:38:19 +01001074 print ' }'
1075 print ' }'
1076 print ' }'
1077 print ' }'
1078 print
José Fonseca1a2fdd22011-04-01 00:55:09 +01001079
José Fonseca8d1408b2014-02-03 19:57:18 +00001080 # Restore the original array_buffer
1081 print ' if (_array_buffer) {'
1082 self.fake_glBindBuffer(api, 'GL_ARRAY_BUFFER', '_array_buffer')
1083 print ' }'
1084 print
1085
José Fonseca669b1222011-02-20 09:05:10 +00001086 print '}'
1087 print
1088
José Fonsecafb6744f2011-04-15 11:18:37 +01001089 #
1090 # Hooks for glTexCoordPointer, which is identical to the other array
1091 # pointers except the fact that it is indexed by glClientActiveTexture.
1092 #
1093
1094 def array_prolog(self, api, uppercase_name):
1095 if uppercase_name == 'TEXTURE_COORD':
José Fonseca26be8f92014-03-07 14:08:50 +00001096 print ' GLint client_active_texture = _glGetInteger(GL_CLIENT_ACTIVE_TEXTURE);'
José Fonsecafb6744f2011-04-15 11:18:37 +01001097 print ' GLint max_texture_coords = 0;'
José Fonsecaf028a8f2012-02-15 23:33:35 +00001098 print ' if (ctx->profile == gltrace::PROFILE_COMPAT)'
José Fonseca632a78d2012-04-19 07:18:59 +01001099 print ' _glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);'
Chia-I Wub3d218d2011-11-03 01:37:36 +08001100 print ' else'
José Fonseca632a78d2012-04-19 07:18:59 +01001101 print ' _glGetIntegerv(GL_MAX_TEXTURE_UNITS, &max_texture_coords);'
José Fonsecafb6744f2011-04-15 11:18:37 +01001102 print ' for (GLint unit = 0; unit < max_texture_coords; ++unit) {'
José Fonseca7525e6f2011-09-28 09:04:56 +01001103 print ' GLint texture = GL_TEXTURE0 + unit;'
José Fonseca632a78d2012-04-19 07:18:59 +01001104 print ' _glClientActiveTexture(texture);'
José Fonsecafb6744f2011-04-15 11:18:37 +01001105
1106 def array_trace_prolog(self, api, uppercase_name):
1107 if uppercase_name == 'TEXTURE_COORD':
1108 print ' bool client_active_texture_dirty = false;'
1109
1110 def array_epilog(self, api, uppercase_name):
1111 if uppercase_name == 'TEXTURE_COORD':
1112 print ' }'
1113 self.array_cleanup(api, uppercase_name)
1114
1115 def array_cleanup(self, api, uppercase_name):
1116 if uppercase_name == 'TEXTURE_COORD':
José Fonseca632a78d2012-04-19 07:18:59 +01001117 print ' _glClientActiveTexture(client_active_texture);'
José Fonsecafb6744f2011-04-15 11:18:37 +01001118
1119 def array_trace_intermezzo(self, api, uppercase_name):
1120 if uppercase_name == 'TEXTURE_COORD':
1121 print ' if (texture != client_active_texture || client_active_texture_dirty) {'
1122 print ' client_active_texture_dirty = true;'
1123 self.fake_glClientActiveTexture_call(api, "texture");
1124 print ' }'
1125
1126 def array_trace_epilog(self, api, uppercase_name):
1127 if uppercase_name == 'TEXTURE_COORD':
1128 print ' if (client_active_texture_dirty) {'
1129 self.fake_glClientActiveTexture_call(api, "client_active_texture");
1130 print ' }'
1131
José Fonseca8d1408b2014-02-03 19:57:18 +00001132 def fake_glBindBuffer(self, api, target, buffer):
1133 function = api.getFunctionByName('glBindBuffer')
1134 self.fake_call(function, [target, buffer])
1135
José Fonsecafb6744f2011-04-15 11:18:37 +01001136 def fake_glClientActiveTexture_call(self, api, texture):
José Fonseca1b6c8752012-04-15 14:33:00 +01001137 function = api.getFunctionByName('glClientActiveTexture')
José Fonsecafb6744f2011-04-15 11:18:37 +01001138 self.fake_call(function, [texture])
1139
José Fonseca151c3702013-05-10 08:28:15 +01001140 def emitFakeTexture2D(self):
1141 function = glapi.glapi.getFunctionByName('glTexImage2D')
1142 instances = function.argNames()
José Fonseca7a5f23a2014-06-24 19:20:36 +01001143 print ' unsigned _fake_call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,)
José Fonseca151c3702013-05-10 08:28:15 +01001144 for arg in function.args:
1145 assert not arg.output
1146 self.serializeArg(function, arg)
1147 print ' trace::localWriter.endEnter();'
1148 print ' trace::localWriter.beginLeave(_fake_call);'
1149 print ' trace::localWriter.endLeave();'
José Fonsecafb6744f2011-04-15 11:18:37 +01001150
1151
1152
1153
1154
José Fonseca669b1222011-02-20 09:05:10 +00001155
1156
1157
1158
1159
1160