blob: ed8e1e50187d91c7d3e169b82120e8484e042846 [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
José Fonsecab0c59722015-01-05 20:45:41 +0000111 # arrays available in ES1
Chia-I Wub3d218d2011-11-03 01:37:36 +0800112 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é Fonsecab0c59722015-01-05 20:45:41 +0000156 print ' glprofile::Profile profile = ctx->profile;'
157 print ' bool es1 = profile.es() && profile.major == 1;'
José Fonseca8b04b5a2014-07-17 19:25:21 +0100158 print
José Fonseca1a2fdd22011-04-01 00:55:09 +0100159
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000160 for camelcase_name, uppercase_name in self.arrays:
Chia-I Wub3d218d2011-11-03 01:37:36 +0800161 # in which profile is the array available?
José Fonseca34ea6162015-01-08 23:45:43 +0000162 profile_check = 'profile.desktop()'
Chia-I Wub3d218d2011-11-03 01:37:36 +0800163 if camelcase_name in self.arrays_es1:
José Fonsecab0c59722015-01-05 20:45:41 +0000164 profile_check = '(' + profile_check + ' || es1)';
Chia-I Wub3d218d2011-11-03 01:37:36 +0800165
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000166 function_name = 'gl%sPointer' % camelcase_name
167 enable_name = 'GL_%s_ARRAY' % uppercase_name
168 binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name
169 print ' // %s' % function_name
Chia-I Wub3d218d2011-11-03 01:37:36 +0800170 print ' if (%s) {' % profile_check
José Fonsecafb6744f2011-04-15 11:18:37 +0100171 self.array_prolog(api, uppercase_name)
José Fonseca8b04b5a2014-07-17 19:25:21 +0100172 print ' if (_glIsEnabled(%s) &&' % enable_name
173 print ' _glGetInteger(%s) == 0) {' % binding_name
José Fonsecafb6744f2011-04-15 11:18:37 +0100174 self.array_cleanup(api, uppercase_name)
José Fonseca8b04b5a2014-07-17 19:25:21 +0100175 print ' return true;'
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000176 print ' }'
José Fonsecafb6744f2011-04-15 11:18:37 +0100177 self.array_epilog(api, uppercase_name)
Chia-I Wub3d218d2011-11-03 01:37:36 +0800178 print ' }'
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000179 print
José Fonseca7f5163e2011-03-31 23:37:26 +0100180
Chia-I Wub3d218d2011-11-03 01:37:36 +0800181 print ' // ES1 does not support generic vertex attributes'
José Fonsecab0c59722015-01-05 20:45:41 +0000182 print ' if (es1)'
Chia-I Wub3d218d2011-11-03 01:37:36 +0800183 print ' return false;'
184 print
José Fonseca632a78d2012-04-19 07:18:59 +0100185 print ' vertex_attrib _vertex_attrib = _get_vertex_attrib();'
José Fonseca5a568a92011-06-29 16:43:36 +0100186 print
187 print ' // glVertexAttribPointer'
José Fonseca632a78d2012-04-19 07:18:59 +0100188 print ' if (_vertex_attrib == VERTEX_ATTRIB) {'
José Fonseca26be8f92014-03-07 14:08:50 +0000189 print ' GLint _max_vertex_attribs = _glGetInteger(GL_MAX_VERTEX_ATTRIBS);'
José Fonseca632a78d2012-04-19 07:18:59 +0100190 print ' for (GLint index = 0; index < _max_vertex_attribs; ++index) {'
José Fonseca8b04b5a2014-07-17 19:25:21 +0100191 print ' if (_glGetVertexAttribi(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED) &&'
192 print ' _glGetVertexAttribi(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING) == 0) {'
193 print ' return true;'
José Fonseca5a568a92011-06-29 16:43:36 +0100194 print ' }'
195 print ' }'
196 print ' }'
197 print
José Fonseca5a568a92011-06-29 16:43:36 +0100198 print ' // glVertexAttribPointerNV'
José Fonseca632a78d2012-04-19 07:18:59 +0100199 print ' if (_vertex_attrib == VERTEX_ATTRIB_NV) {'
José Fonseca5a568a92011-06-29 16:43:36 +0100200 print ' for (GLint index = 0; index < 16; ++index) {'
José Fonseca8b04b5a2014-07-17 19:25:21 +0100201 print ' if (_glIsEnabled(GL_VERTEX_ATTRIB_ARRAY0_NV + index)) {'
José Fonseca5a568a92011-06-29 16:43:36 +0100202 print ' return true;'
José Fonseca1a2fdd22011-04-01 00:55:09 +0100203 print ' }'
204 print ' }'
205 print ' }'
206 print
207
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000208 print ' return false;'
209 print '}'
210 print
José Fonseca669b1222011-02-20 09:05:10 +0000211
José Fonseca14cb9ef2012-05-17 21:33:14 +0100212 print 'static void _trace_user_arrays(GLuint count);'
José Fonseca14c21bc2011-02-20 23:32:22 +0000213 print
José Fonseca867b1b72011-04-24 11:58:04 +0100214
José Fonseca707630d2014-03-07 14:20:35 +0000215 print '// whether glLockArraysEXT() has ever been called'
216 print 'static bool _checkLockArraysEXT = false;'
217 print
218
José Fonseca9c536b02012-02-29 20:54:13 +0000219 # Buffer mappings
220 print '// whether glMapBufferRange(GL_MAP_WRITE_BIT) has ever been called'
José Fonseca632a78d2012-04-19 07:18:59 +0100221 print 'static bool _checkBufferMapRange = false;'
José Fonseca9c536b02012-02-29 20:54:13 +0000222 print
223 print '// whether glBufferParameteriAPPLE(GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE) has ever been called'
José Fonseca632a78d2012-04-19 07:18:59 +0100224 print 'static bool _checkBufferFlushingUnmapAPPLE = false;'
José Fonseca9c536b02012-02-29 20:54:13 +0000225 print
226 # Buffer mapping information, necessary for old Mesa 2.1 drivers which
227 # do not support glGetBufferParameteriv(GL_BUFFER_ACCESS_FLAGS/GL_BUFFER_MAP_LENGTH)
José Fonseca867b1b72011-04-24 11:58:04 +0100228 print 'struct buffer_mapping {'
229 print ' void *map;'
230 print ' GLint length;'
231 print ' bool write;'
José Fonseca73373602011-05-20 17:45:26 +0100232 print ' bool explicit_flush;'
José Fonseca867b1b72011-04-24 11:58:04 +0100233 print '};'
234 print
235 for target in self.buffer_targets:
José Fonseca632a78d2012-04-19 07:18:59 +0100236 print 'struct buffer_mapping _%s_mapping;' % target.lower();
José Fonseca867b1b72011-04-24 11:58:04 +0100237 print
238 print 'static inline struct buffer_mapping *'
239 print 'get_buffer_mapping(GLenum target) {'
José Fonseca06e85192011-10-16 14:15:36 +0100240 print ' switch (target) {'
José Fonseca867b1b72011-04-24 11:58:04 +0100241 for target in self.buffer_targets:
242 print ' case GL_%s:' % target
José Fonseca632a78d2012-04-19 07:18:59 +0100243 print ' return & _%s_mapping;' % target.lower()
José Fonseca867b1b72011-04-24 11:58:04 +0100244 print ' default:'
José Fonseca559d5342011-10-27 08:10:56 +0100245 print ' os::log("apitrace: warning: unknown buffer target 0x%04X\\n", target);'
José Fonseca867b1b72011-04-24 11:58:04 +0100246 print ' return NULL;'
247 print ' }'
248 print '}'
249 print
250
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100251 # Generate a helper function to determine whether a parameter name
252 # refers to a symbolic value or not
253 print 'static bool'
254 print 'is_symbolic_pname(GLenum pname) {'
José Fonseca06e85192011-10-16 14:15:36 +0100255 print ' switch (pname) {'
José Fonseca5ea91872011-05-04 09:41:55 +0100256 for function, type, count, name in glparams.parameters:
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100257 if type is glapi.GLenum:
José Fonseca4c938c22011-04-30 22:44:38 +0100258 print ' case %s:' % name
259 print ' return true;'
260 print ' default:'
261 print ' return false;'
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100262 print ' }'
263 print '}'
264 print
265
266 # Generate a helper function to determine whether a parameter value is
267 # potentially symbolic or not; i.e., if the value can be represented in
268 # an enum or not
269 print 'template<class T>'
270 print 'static inline bool'
271 print 'is_symbolic_param(T param) {'
272 print ' return static_cast<T>(static_cast<GLenum>(param)) == param;'
273 print '}'
274 print
José Fonseca4c938c22011-04-30 22:44:38 +0100275
276 # Generate a helper function to know how many elements a parameter has
277 print 'static size_t'
José Fonseca632a78d2012-04-19 07:18:59 +0100278 print '_gl_param_size(GLenum pname) {'
José Fonseca06e85192011-10-16 14:15:36 +0100279 print ' switch (pname) {'
José Fonseca5ea91872011-05-04 09:41:55 +0100280 for function, type, count, name in glparams.parameters:
José Fonseca4c938c22011-04-30 22:44:38 +0100281 if type is not None:
José Fonsecaddf6d2c2012-12-20 15:34:50 +0000282 print ' case %s: return %s;' % (name, count)
José Fonseca4c938c22011-04-30 22:44:38 +0100283 print ' default:'
José Fonseca559d5342011-10-27 08:10:56 +0100284 print r' os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, pname);'
José Fonseca4c938c22011-04-30 22:44:38 +0100285 print ' return 1;'
286 print ' }'
287 print '}'
288 print
289
Chia-I Wu335efb42011-11-03 01:59:22 +0800290 # states such as GL_UNPACK_ROW_LENGTH are not available in GLES
291 print 'static inline bool'
292 print 'can_unpack_subimage(void) {'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000293 print ' gltrace::Context *ctx = gltrace::getContext();'
José Fonseca34ea6162015-01-08 23:45:43 +0000294 print ' return ctx->profile.desktop();'
Chia-I Wu335efb42011-11-03 01:59:22 +0800295 print '}'
296 print
297
José Fonseca631dbd12014-12-15 16:34:45 +0000298 # VMWX_map_buffer_debug
299 print r'extern "C" PUBLIC'
300 print r'void APIENTRY'
301 print r'glNotifyMappedBufferRangeVMWX(const void * start, GLsizeiptr length) {'
302 self.emit_memcpy('start', 'length')
303 print r'}'
304 print
305
José Fonseca1b6c8752012-04-15 14:33:00 +0100306 getProcAddressFunctionNames = []
307
308 def traceApi(self, api):
309 if self.getProcAddressFunctionNames:
310 # Generate a function to wrap proc addresses
311 getProcAddressFunction = api.getFunctionByName(self.getProcAddressFunctionNames[0])
312 argType = getProcAddressFunction.args[0].type
313 retType = getProcAddressFunction.type
314
315 print 'static %s _wrapProcAddress(%s procName, %s procPtr);' % (retType, argType, retType)
316 print
317
318 Tracer.traceApi(self, api)
319
320 print 'static %s _wrapProcAddress(%s procName, %s procPtr) {' % (retType, argType, retType)
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700321
322 # Provide fallback functions to missing debug functions
José Fonseca1b6c8752012-04-15 14:33:00 +0100323 print ' if (!procPtr) {'
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700324 else_ = ''
325 for function_name in self.debug_functions:
326 if self.api.getFunctionByName(function_name):
327 print ' %sif (strcmp("%s", (const char *)procName) == 0) {' % (else_, function_name)
328 print ' return (%s)&%s;' % (retType, function_name)
329 print ' }'
330 else_ = 'else '
331 print ' %s{' % else_
332 print ' return NULL;'
333 print ' }'
José Fonseca1b6c8752012-04-15 14:33:00 +0100334 print ' }'
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700335
José Fonseca81301932012-11-11 00:10:20 +0000336 for function in api.getAllFunctions():
José Fonseca1b6c8752012-04-15 14:33:00 +0100337 ptype = function_pointer_type(function)
338 pvalue = function_pointer_value(function)
339 print ' if (strcmp("%s", (const char *)procName) == 0) {' % function.name
340 print ' %s = (%s)procPtr;' % (pvalue, ptype)
341 print ' return (%s)&%s;' % (retType, function.name,)
342 print ' }'
343 print ' os::log("apitrace: warning: unknown function \\"%s\\"\\n", (const char *)procName);'
344 print ' return procPtr;'
345 print '}'
346 print
347 else:
348 Tracer.traceApi(self, api)
349
Imre Deakd4937372012-04-24 14:06:48 +0300350 def defineShadowBufferHelper(self):
351 print 'void _shadow_glGetBufferSubData(GLenum target, GLintptr offset,'
352 print ' GLsizeiptr size, GLvoid *data)'
353 print '{'
José Fonsecaa33d0bb2012-11-10 09:11:42 +0000354 print ' gltrace::Context *ctx = gltrace::getContext();'
Imre Deakd4937372012-04-24 14:06:48 +0300355 print ' if (!ctx->needsShadowBuffers() || target != GL_ELEMENT_ARRAY_BUFFER) {'
José Fonseca219c9f22012-11-03 10:13:17 +0000356 print ' _glGetBufferSubData(target, offset, size, data);'
Imre Deakd4937372012-04-24 14:06:48 +0300357 print ' return;'
358 print ' }'
359 print
José Fonseca26be8f92014-03-07 14:08:50 +0000360 print ' GLint buffer_binding = _glGetInteger(GL_ELEMENT_ARRAY_BUFFER_BINDING);'
José Fonseca219c9f22012-11-03 10:13:17 +0000361 print ' if (buffer_binding > 0) {'
362 print ' gltrace::Buffer & buf = ctx->buffers[buffer_binding];'
363 print ' buf.getSubData(offset, size, data);'
364 print ' }'
Imre Deakd4937372012-04-24 14:06:48 +0300365 print '}'
366
José Fonseca219c9f22012-11-03 10:13:17 +0000367 def shadowBufferMethod(self, method):
368 # Emit code to fetch the shadow buffer, and invoke a method
369 print ' gltrace::Context *ctx = gltrace::getContext();'
370 print ' if (ctx->needsShadowBuffers() && target == GL_ELEMENT_ARRAY_BUFFER) {'
José Fonseca26be8f92014-03-07 14:08:50 +0000371 print ' GLint buffer_binding = _glGetInteger(GL_ELEMENT_ARRAY_BUFFER_BINDING);'
José Fonseca219c9f22012-11-03 10:13:17 +0000372 print ' if (buffer_binding > 0) {'
373 print ' gltrace::Buffer & buf = ctx->buffers[buffer_binding];'
374 print ' buf.' + method + ';'
375 print ' }'
376 print ' }'
377 print
378
Imre Deakd4937372012-04-24 14:06:48 +0300379 def shadowBufferProlog(self, function):
380 if function.name == 'glBufferData':
José Fonseca219c9f22012-11-03 10:13:17 +0000381 self.shadowBufferMethod('bufferData(size, data)')
Imre Deakd4937372012-04-24 14:06:48 +0300382
383 if function.name == 'glBufferSubData':
José Fonseca219c9f22012-11-03 10:13:17 +0000384 self.shadowBufferMethod('bufferSubData(offset, size, data)')
Imre Deakd4937372012-04-24 14:06:48 +0300385
386 if function.name == 'glDeleteBuffers':
387 print ' gltrace::Context *ctx = gltrace::getContext();'
388 print ' if (ctx->needsShadowBuffers()) {'
José Fonseca219c9f22012-11-03 10:13:17 +0000389 print ' for (GLsizei i = 0; i < n; i++) {'
390 print ' ctx->buffers.erase(buffer[i]);'
Imre Deakd4937372012-04-24 14:06:48 +0300391 print ' }'
392 print ' }'
393
José Fonseca99221832011-03-22 22:15:46 +0000394 array_pointer_function_names = set((
395 "glVertexPointer",
396 "glNormalPointer",
397 "glColorPointer",
398 "glIndexPointer",
399 "glTexCoordPointer",
400 "glEdgeFlagPointer",
401 "glFogCoordPointer",
402 "glSecondaryColorPointer",
José Fonseca7f5163e2011-03-31 23:37:26 +0100403
José Fonsecaac5285b2011-05-04 11:09:08 +0100404 "glInterleavedArrays",
405
José Fonseca7e0bfd92011-04-30 23:09:03 +0100406 "glVertexPointerEXT",
407 "glNormalPointerEXT",
408 "glColorPointerEXT",
409 "glIndexPointerEXT",
410 "glTexCoordPointerEXT",
411 "glEdgeFlagPointerEXT",
412 "glFogCoordPointerEXT",
413 "glSecondaryColorPointerEXT",
José Fonseca99221832011-03-22 22:15:46 +0000414
José Fonseca7f5163e2011-03-31 23:37:26 +0100415 "glVertexAttribPointer",
416 "glVertexAttribPointerARB",
417 "glVertexAttribPointerNV",
José Fonsecaac5285b2011-05-04 11:09:08 +0100418 "glVertexAttribIPointer",
419 "glVertexAttribIPointerEXT",
José Fonseca7f5163e2011-03-31 23:37:26 +0100420 "glVertexAttribLPointer",
421 "glVertexAttribLPointerEXT",
José Fonseca99221832011-03-22 22:15:46 +0000422
423 #"glMatrixIndexPointerARB",
424 ))
425
José Fonsecac5bf77a2014-08-14 16:10:02 +0100426 # XXX: We currently ignore the gl*Draw*ElementArray* functions
427 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 +0000428
José Fonsecac9f12232011-03-25 20:07:42 +0000429 interleaved_formats = [
430 'GL_V2F',
431 'GL_V3F',
432 'GL_C4UB_V2F',
433 'GL_C4UB_V3F',
434 'GL_C3F_V3F',
435 'GL_N3F_V3F',
436 'GL_C4F_N3F_V3F',
437 'GL_T2F_V3F',
438 'GL_T4F_V4F',
439 'GL_T2F_C4UB_V3F',
440 'GL_T2F_C3F_V3F',
441 'GL_T2F_N3F_V3F',
442 'GL_T2F_C4F_N3F_V3F',
443 'GL_T4F_C4F_N3F_V4F',
444 ]
445
José Fonseca54f304a2012-01-14 19:33:08 +0000446 def traceFunctionImplBody(self, function):
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000447 # Defer tracing of user array pointers...
José Fonseca99221832011-03-22 22:15:46 +0000448 if function.name in self.array_pointer_function_names:
José Fonseca26be8f92014-03-07 14:08:50 +0000449 print ' GLint _array_buffer = _glGetInteger(GL_ARRAY_BUFFER_BINDING);'
José Fonseca632a78d2012-04-19 07:18:59 +0100450 print ' if (!_array_buffer) {'
José Fonsecaf15546b2015-01-20 13:37:45 +0000451 print ' static bool warned = false;'
452 print ' if (!warned) {'
453 print ' warned = true;'
454 print ' os::log("apitrace: warning: %s: call will be faked due to pointer to user memory (https://github.com/apitrace/apitrace/blob/master/BUGS.markdown#tracing)\\n", __FUNCTION__);'
455 print ' }'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000456 print ' gltrace::Context *ctx = gltrace::getContext();'
Chia-I Wu8ef66972011-11-03 01:19:46 +0800457 print ' ctx->user_arrays = true;'
José Fonseca5a568a92011-06-29 16:43:36 +0100458 if function.name == "glVertexAttribPointerNV":
Chia-I Wu8ef66972011-11-03 01:19:46 +0800459 print ' ctx->user_arrays_nv = true;'
José Fonseca54f304a2012-01-14 19:33:08 +0000460 self.invokeFunction(function)
José Fonsecaac5285b2011-05-04 11:09:08 +0100461
462 # And also break down glInterleavedArrays into the individual calls
463 if function.name == 'glInterleavedArrays':
464 print
465
466 # Initialize the enable flags
467 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100468 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100469 print ' GLboolean %s = GL_FALSE;' % flag_name
470 print
471
472 # Switch for the interleaved formats
473 print ' switch (format) {'
474 for format in self.interleaved_formats:
475 print ' case %s:' % format
476 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100477 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100478 if format.find('_' + uppercase_name[0]) >= 0:
479 print ' %s = GL_TRUE;' % flag_name
480 print ' break;'
481 print ' default:'
482 print ' return;'
483 print ' }'
484 print
485
486 # Emit fake glEnableClientState/glDisableClientState flags
487 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100488 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100489 enable_name = 'GL_%s_ARRAY' % uppercase_name
490
491 # Emit a fake function
492 print ' {'
José Fonseca632a78d2012-04-19 07:18:59 +0100493 print ' static const trace::FunctionSig &_sig = %s ? _glEnableClientState_sig : _glDisableClientState_sig;' % flag_name
José Fonseca7a5f23a2014-06-24 19:20:36 +0100494 print ' unsigned _call = trace::localWriter.beginEnter(&_sig, true);'
José Fonsecab4a3d142011-10-27 07:43:19 +0100495 print ' trace::localWriter.beginArg(0);'
José Fonseca54f304a2012-01-14 19:33:08 +0000496 self.serializeValue(glapi.GLenum, enable_name)
José Fonsecab4a3d142011-10-27 07:43:19 +0100497 print ' trace::localWriter.endArg();'
498 print ' trace::localWriter.endEnter();'
José Fonseca632a78d2012-04-19 07:18:59 +0100499 print ' trace::localWriter.beginLeave(_call);'
José Fonsecab4a3d142011-10-27 07:43:19 +0100500 print ' trace::localWriter.endLeave();'
José Fonsecaac5285b2011-05-04 11:09:08 +0100501 print ' }'
502
José Fonsecac629a8c2014-06-01 21:12:27 +0100503 # Warn about buggy glGet(GL_*ARRAY_SIZE) not returning GL_BGRA
504 buggyFunctions = {
505 'glColorPointer': ('glGetIntegerv', '', 'GL_COLOR_ARRAY_SIZE'),
506 'glSecondaryColorPointer': ('glGetIntegerv', '', 'GL_SECONDARY_COLOR_ARRAY_SIZE'),
507 'glVertexAttribPointer': ('glGetVertexAttribiv', 'index, ', 'GL_VERTEX_ATTRIB_ARRAY_SIZE'),
508 'glVertexAttribPointerARB': ('glGetVertexAttribivARB', 'index, ', 'GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB'),
509 }
510 if function.name in buggyFunctions:
511 getter, extraArg, pname = buggyFunctions[function.name]
512 print r' static bool _checked = false;'
513 print r' if (!_checked && size == GL_BGRA) {'
514 print r' GLint _size = 0;'
515 print r' _%s(%s%s, &_size);' % (getter, extraArg, pname)
516 print r' if (_size != GL_BGRA) {'
517 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)
518 print r' }'
519 print r' _checked = true;'
520 print r' }'
521
José Fonseca99221832011-03-22 22:15:46 +0000522 print ' return;'
523 print ' }'
José Fonseca14c21bc2011-02-20 23:32:22 +0000524
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000525 # ... to the draw calls
José Fonsecac5bf77a2014-08-14 16:10:02 +0100526 if self.draw_function_regex.match(function.name):
José Fonseca632a78d2012-04-19 07:18:59 +0100527 print ' if (_need_user_arrays()) {'
José Fonseca246508e2014-08-14 16:07:46 +0100528 if 'Indirect' in function.name:
529 print r' os::log("apitrace: warning: %s: indirect user arrays not supported\n");' % (function.name,)
530 else:
531 arg_names = ', '.join([arg.name for arg in function.args[1:]])
532 print ' GLuint _count = _%s_count(%s);' % (function.name, arg_names)
533 # Some apps, in particular Quake3, can tell the driver to lock more
534 # vertices than those actually required for the draw call.
535 print ' if (_checkLockArraysEXT) {'
536 print ' GLuint _locked_count = _glGetInteger(GL_ARRAY_ELEMENT_LOCK_FIRST_EXT)'
537 print ' + _glGetInteger(GL_ARRAY_ELEMENT_LOCK_COUNT_EXT);'
538 print ' _count = std::max(_count, _locked_count);'
539 print ' }'
540 print ' _trace_user_arrays(_count);'
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000541 print ' }'
José Fonseca707630d2014-03-07 14:20:35 +0000542 if function.name == 'glLockArraysEXT':
543 print ' _checkLockArraysEXT = true;'
José Fonseca4a7d8602014-06-18 16:03:44 +0100544
545 # Warn if user arrays are used with glBegin/glArrayElement/glEnd.
546 if function.name == 'glBegin':
José Fonseca7c39d012014-11-07 19:46:53 +0000547 print r' gltrace::Context *ctx = gltrace::getContext();'
548 print r' ctx->userArraysOnBegin = _need_user_arrays();'
549 if function.name.startswith('glArrayElement'):
550 print r' gltrace::Context *ctx = gltrace::getContext();'
551 print r' if (ctx->userArraysOnBegin) {'
José Fonseca4a7d8602014-06-18 16:03:44 +0100552 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 +0000553 print r' ctx->userArraysOnBegin = false;'
José Fonseca4a7d8602014-06-18 16:03:44 +0100554 print r' }'
José Fonseca14c21bc2011-02-20 23:32:22 +0000555
José Fonseca73373602011-05-20 17:45:26 +0100556 # Emit a fake memcpy on buffer uploads
José Fonseca9c536b02012-02-29 20:54:13 +0000557 if function.name == 'glBufferParameteriAPPLE':
558 print ' if (pname == GL_BUFFER_FLUSHING_UNMAP_APPLE && param == GL_FALSE) {'
José Fonseca632a78d2012-04-19 07:18:59 +0100559 print ' _checkBufferFlushingUnmapAPPLE = true;'
José Fonseca9c536b02012-02-29 20:54:13 +0000560 print ' }'
José Fonsecacdc322c2012-02-29 19:29:51 +0000561 if function.name in ('glUnmapBuffer', 'glUnmapBufferARB'):
José Fonseca9c536b02012-02-29 20:54:13 +0000562 if function.name.endswith('ARB'):
563 suffix = 'ARB'
564 else:
565 suffix = ''
566 print ' GLint access = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100567 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_ACCESS, &access);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000568 print ' if (access != GL_READ_ONLY) {'
569 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100570 print ' _glGetBufferPointerv%s(target, GL_BUFFER_MAP_POINTER, &map);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000571 print ' if (map) {'
572 print ' GLint length = -1;'
573 print ' bool flush = true;'
José Fonseca632a78d2012-04-19 07:18:59 +0100574 print ' if (_checkBufferMapRange) {'
575 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_MAP_LENGTH, &length);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000576 print ' GLint access_flags = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100577 print ' _glGetBufferParameteriv(target, GL_BUFFER_ACCESS_FLAGS, &access_flags);'
José Fonsecaa4210e22014-12-13 15:49:37 +0000578 print ' flush = flush && !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT));'
José Fonseca9c536b02012-02-29 20:54:13 +0000579 print ' if (length == -1) {'
José Fonsecacb07e2d2014-02-04 15:14:09 +0000580 print ' // Mesa drivers refuse GL_BUFFER_MAP_LENGTH without GL 3.0 up-to'
581 print ' // http://cgit.freedesktop.org/mesa/mesa/commit/?id=ffee498fb848b253a7833373fe5430f8c7ca0c5f'
José Fonsecadb1ccce2012-02-29 21:09:24 +0000582 print ' static bool warned = false;'
583 print ' if (!warned) {'
584 print ' os::log("apitrace: warning: glGetBufferParameteriv%s(GL_BUFFER_MAP_LENGTH) failed\\n");' % suffix
585 print ' warned = true;'
586 print ' }'
José Fonseca9c536b02012-02-29 20:54:13 +0000587 print ' struct buffer_mapping *mapping = get_buffer_mapping(target);'
588 print ' if (mapping) {'
589 print ' length = mapping->length;'
590 print ' flush = flush && !mapping->explicit_flush;'
591 print ' } else {'
592 print ' length = 0;'
593 print ' flush = false;'
594 print ' }'
595 print ' }'
596 print ' } else {'
597 print ' length = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100598 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_SIZE, &length);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000599 print ' }'
José Fonseca632a78d2012-04-19 07:18:59 +0100600 print ' if (_checkBufferFlushingUnmapAPPLE) {'
José Fonseca9c536b02012-02-29 20:54:13 +0000601 print ' GLint flushing_unmap = GL_TRUE;'
José Fonseca632a78d2012-04-19 07:18:59 +0100602 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_FLUSHING_UNMAP_APPLE, &flushing_unmap);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000603 print ' flush = flush && flushing_unmap;'
604 print ' }'
605 print ' if (flush && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100606 self.emit_memcpy('map', 'length')
José Fonseca9c536b02012-02-29 20:54:13 +0000607 print ' }'
608 print ' }'
609 print ' }'
José Fonsecacdc322c2012-02-29 19:29:51 +0000610 if function.name == 'glUnmapBufferOES':
611 print ' GLint access = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100612 print ' _glGetBufferParameteriv(target, GL_BUFFER_ACCESS_OES, &access);'
José Fonsecacdc322c2012-02-29 19:29:51 +0000613 print ' if (access == GL_WRITE_ONLY_OES) {'
614 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100615 print ' _glGetBufferPointervOES(target, GL_BUFFER_MAP_POINTER_OES, &map);'
José Fonsecacdc322c2012-02-29 19:29:51 +0000616 print ' GLint size = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100617 print ' _glGetBufferParameteriv(target, GL_BUFFER_SIZE, &size);'
José Fonsecacdc322c2012-02-29 19:29:51 +0000618 print ' if (map && size > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100619 self.emit_memcpy('map', 'size')
José Fonseca219c9f22012-11-03 10:13:17 +0000620 self.shadowBufferMethod('bufferSubData(0, size, map)')
José Fonsecacdc322c2012-02-29 19:29:51 +0000621 print ' }'
José Fonseca867b1b72011-04-24 11:58:04 +0100622 print ' }'
José Fonseca4920c302014-08-13 18:35:57 +0100623 if function.name == 'glUnmapNamedBuffer':
624 print ' GLint access_flags = 0;'
625 print ' _glGetNamedBufferParameteriv(buffer, GL_BUFFER_ACCESS_FLAGS, &access_flags);'
José Fonsecaa4210e22014-12-13 15:49:37 +0000626 print ' if ((access_flags & GL_MAP_WRITE_BIT) &&'
627 print ' !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT))) {'
José Fonseca4920c302014-08-13 18:35:57 +0100628 print ' GLvoid *map = NULL;'
629 print ' _glGetNamedBufferPointerv(buffer, GL_BUFFER_MAP_POINTER, &map);'
630 print ' GLint length = 0;'
631 print ' _glGetNamedBufferParameteriv(buffer, GL_BUFFER_MAP_LENGTH, &length);'
632 print ' if (map && length > 0) {'
633 self.emit_memcpy('map', 'length')
634 print ' }'
635 print ' }'
José Fonsecafb3bd602012-01-15 13:56:28 +0000636 if function.name == 'glUnmapNamedBufferEXT':
José Fonseca024aff42012-02-29 18:00:06 +0000637 print ' GLint access_flags = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100638 print ' _glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_ACCESS_FLAGS, &access_flags);'
José Fonsecaa4210e22014-12-13 15:49:37 +0000639 print ' if ((access_flags & GL_MAP_WRITE_BIT) &&'
640 print ' !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT))) {'
José Fonsecafb3bd602012-01-15 13:56:28 +0000641 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100642 print ' _glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &map);'
José Fonsecafb3bd602012-01-15 13:56:28 +0000643 print ' GLint length = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100644 print ' _glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_MAP_LENGTH, &length);'
José Fonseca024aff42012-02-29 18:00:06 +0000645 print ' if (map && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100646 self.emit_memcpy('map', 'length')
José Fonseca024aff42012-02-29 18:00:06 +0000647 print ' }'
José Fonsecafb3bd602012-01-15 13:56:28 +0000648 print ' }'
José Fonseca77ef0ce2012-02-29 18:08:48 +0000649 if function.name == 'glFlushMappedBufferRange':
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 && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100653 self.emit_memcpy('(const char *)map + offset', 'length')
José Fonseca77ef0ce2012-02-29 18:08:48 +0000654 print ' }'
655 if function.name == 'glFlushMappedBufferRangeAPPLE':
656 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100657 print ' _glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &map);'
José Fonseca77ef0ce2012-02-29 18:08:48 +0000658 print ' if (map && size > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100659 self.emit_memcpy('(const char *)map + offset', 'size')
José Fonseca73373602011-05-20 17:45:26 +0100660 print ' }'
José Fonseca4920c302014-08-13 18:35:57 +0100661 if function.name == 'glFlushMappedNamedBufferRange':
662 print ' GLvoid *map = NULL;'
663 print ' _glGetNamedBufferPointerv(buffer, GL_BUFFER_MAP_POINTER, &map);'
664 print ' if (map && length > 0) {'
665 self.emit_memcpy('(const char *)map + offset', 'length')
666 print ' }'
José Fonsecafb3bd602012-01-15 13:56:28 +0000667 if function.name == 'glFlushMappedNamedBufferRangeEXT':
668 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100669 print ' _glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &map);'
José Fonseca024aff42012-02-29 18:00:06 +0000670 print ' if (map && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100671 self.emit_memcpy('(const char *)map + offset', 'length')
José Fonsecafb3bd602012-01-15 13:56:28 +0000672 print ' }'
José Fonseca867b1b72011-04-24 11:58:04 +0100673
José Fonseca3522cbd2014-02-28 14:45:32 +0000674 # FIXME: We don't support coherent/pinned memory mappings
José Fonseca631dbd12014-12-15 16:34:45 +0000675 if function.name in ('glBufferStorage', 'glNamedBufferStorage', 'glNamedBufferStorageEXT'):
676 print r' if (!(flags & 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' flags &= ~GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX;'
José Fonsecaa4210e22014-12-13 15:49:37 +0000680 if function.name in ('glMapBufferRange', 'glMapNamedBufferRange', 'glMapNamedBufferRangeEXT'):
José Fonseca631dbd12014-12-15 16:34:45 +0000681 print r' if (access & GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX) {'
682 print r' if (!(access & GL_MAP_PERSISTENT_BIT)) {'
683 print r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_PERSISTENT_BIT\n", __FUNCTION__);'
684 print r' }'
685 print r' if (access & GL_MAP_FLUSH_EXPLICIT_BIT) {'
686 print r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/ MAP_FLUSH_EXPLICIT_BIT\n", __FUNCTION__);'
687 print r' }'
688 print r' access &= ~GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX;'
689 print r' } else if (access & GL_MAP_COHERENT_BIT) {'
José Fonsecaa4210e22014-12-13 15:49:37 +0000690 print r' os::log("apitrace: warning: %s: MAP_COHERENT_BIT unsupported (https://github.com/apitrace/apitrace/issues/232)\n", __FUNCTION__);'
691 print r' } else if ((access & GL_MAP_PERSISTENT_BIT) &&'
692 print r' !(access & GL_MAP_FLUSH_EXPLICIT_BIT)) {'
693 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 +0000694 print r' }'
695 if function.name in ('glBufferData', 'glBufferDataARB'):
696 print r' if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {'
697 print r' os::log("apitrace: warning: GL_AMD_pinned_memory not fully supported\n");'
698 print r' }'
699
José Fonsecad0f1e292014-11-13 13:21:51 +0000700 # TODO: We don't track GL_INTEL_map_texture mappings
701 if function.name == 'glMapTexture2DINTEL':
702 print r' if (access & GL_MAP_WRITE_BIT) {'
703 print r' os::log("apitrace: warning: GL_INTEL_map_texture not fully supported\n");'
704 print r' }'
705
José Fonseca91492d22011-05-23 21:20:31 +0100706 # Don't leave vertex attrib locations to chance. Instead emit fake
707 # glBindAttribLocation calls to ensure that the same locations will be
708 # used when retracing. Trying to remap locations after the fact would
709 # be an herculian task given that vertex attrib locations appear in
710 # many entry-points, including non-shader related ones.
711 if function.name == 'glLinkProgram':
José Fonseca54f304a2012-01-14 19:33:08 +0000712 Tracer.invokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100713 print ' GLint active_attributes = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100714 print ' _glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &active_attributes);'
José Fonseca7525e6f2011-09-28 09:04:56 +0100715 print ' for (GLint attrib = 0; attrib < active_attributes; ++attrib) {'
José Fonseca91492d22011-05-23 21:20:31 +0100716 print ' GLint size = 0;'
717 print ' GLenum type = 0;'
718 print ' GLchar name[256];'
719 # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
José Fonseca632a78d2012-04-19 07:18:59 +0100720 print ' _glGetActiveAttrib(program, attrib, sizeof name, NULL, &size, &type, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100721 print " if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {"
José Fonseca632a78d2012-04-19 07:18:59 +0100722 print ' GLint location = _glGetAttribLocation(program, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100723 print ' if (location >= 0) {'
José Fonseca1b6c8752012-04-15 14:33:00 +0100724 bind_function = glapi.glapi.getFunctionByName('glBindAttribLocation')
José Fonseca91492d22011-05-23 21:20:31 +0100725 self.fake_call(bind_function, ['program', 'location', 'name'])
José Fonseca2a794f52011-05-26 20:54:29 +0100726 print ' }'
José Fonseca91492d22011-05-23 21:20:31 +0100727 print ' }'
728 print ' }'
729 if function.name == 'glLinkProgramARB':
José Fonseca54f304a2012-01-14 19:33:08 +0000730 Tracer.invokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100731 print ' GLint active_attributes = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100732 print ' _glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_ATTRIBUTES_ARB, &active_attributes);'
José Fonseca7525e6f2011-09-28 09:04:56 +0100733 print ' for (GLint attrib = 0; attrib < active_attributes; ++attrib) {'
José Fonseca91492d22011-05-23 21:20:31 +0100734 print ' GLint size = 0;'
735 print ' GLenum type = 0;'
736 print ' GLcharARB name[256];'
737 # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
José Fonseca632a78d2012-04-19 07:18:59 +0100738 print ' _glGetActiveAttribARB(programObj, attrib, sizeof name, NULL, &size, &type, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100739 print " if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {"
José Fonseca632a78d2012-04-19 07:18:59 +0100740 print ' GLint location = _glGetAttribLocationARB(programObj, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100741 print ' if (location >= 0) {'
José Fonseca1b6c8752012-04-15 14:33:00 +0100742 bind_function = glapi.glapi.getFunctionByName('glBindAttribLocationARB')
José Fonseca91492d22011-05-23 21:20:31 +0100743 self.fake_call(bind_function, ['programObj', 'location', 'name'])
José Fonseca2a794f52011-05-26 20:54:29 +0100744 print ' }'
José Fonseca91492d22011-05-23 21:20:31 +0100745 print ' }'
746 print ' }'
747
Imre Deakd4937372012-04-24 14:06:48 +0300748 self.shadowBufferProlog(function)
749
José Fonseca54f304a2012-01-14 19:33:08 +0000750 Tracer.traceFunctionImplBody(self, function)
José Fonseca73373602011-05-20 17:45:26 +0100751
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700752 # These entrypoints are only expected to be implemented by tools;
753 # drivers will probably not implement them.
José Fonsecaf028a8f2012-02-15 23:33:35 +0000754 marker_functions = [
755 # GL_GREMEDY_string_marker
José Fonseca8f34d342011-07-15 20:16:40 +0100756 'glStringMarkerGREMEDY',
José Fonsecaf028a8f2012-02-15 23:33:35 +0000757 # GL_GREMEDY_frame_terminator
José Fonseca8f34d342011-07-15 20:16:40 +0100758 'glFrameTerminatorGREMEDY',
759 ]
760
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700761 # These entrypoints may be implemented by drivers, but are also very useful
762 # for debugging / analysis tools.
763 debug_functions = [
764 # GL_KHR_debug
765 'glDebugMessageControl',
766 'glDebugMessageInsert',
767 'glDebugMessageCallback',
768 'glGetDebugMessageLog',
769 'glPushDebugGroup',
770 'glPopDebugGroup',
771 'glObjectLabel',
772 'glGetObjectLabel',
773 'glObjectPtrLabel',
774 'glGetObjectPtrLabel',
775 # GL_ARB_debug_output
776 'glDebugMessageControlARB',
777 'glDebugMessageInsertARB',
778 'glDebugMessageCallbackARB',
779 'glGetDebugMessageLogARB',
780 # GL_AMD_debug_output
781 'glDebugMessageEnableAMD',
782 'glDebugMessageInsertAMD',
783 'glDebugMessageCallbackAMD',
784 'glGetDebugMessageLogAMD',
José Fonseca71f5a352014-07-28 12:19:50 +0100785 # GL_EXT_debug_label
786 'glLabelObjectEXT',
787 'glGetObjectLabelEXT',
788 # GL_EXT_debug_marker
789 'glInsertEventMarkerEXT',
790 'glPushGroupMarkerEXT',
791 'glPopGroupMarkerEXT',
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700792 ]
793
José Fonseca54f304a2012-01-14 19:33:08 +0000794 def invokeFunction(self, function):
José Fonseca91492d22011-05-23 21:20:31 +0100795 if function.name in ('glLinkProgram', 'glLinkProgramARB'):
796 # These functions have been dispatched already
797 return
798
José Fonseca2cfa02c2013-06-10 08:05:29 +0100799 Tracer.invokeFunction(self, function)
800
801 def doInvokeFunction(self, function):
802 # Same as invokeFunction() but called both when trace is enabled or disabled.
803 #
804 # Used to modify the behavior of GL entry-points.
805
806 # Override GL extensions
807 if function.name in ('glGetString', 'glGetIntegerv', 'glGetStringi'):
808 Tracer.doInvokeFunction(self, function, prefix = 'gltrace::_', suffix = '_override')
809 return
810
José Fonseca71f5a352014-07-28 12:19:50 +0100811 # We implement GL_GREMEDY_*, etc., and not the driver
José Fonsecaf028a8f2012-02-15 23:33:35 +0000812 if function.name in self.marker_functions:
José Fonseca8f34d342011-07-15 20:16:40 +0100813 return
814
Peter Lohrmann9d9eb812013-07-12 16:15:25 -0400815 # We may be faking KHR_debug, so ensure the pointer queries result is
816 # always zeroed to prevent dereference of unitialized pointers
817 if function.name == 'glGetPointerv':
818 print ' if (params &&'
819 print ' (pname == GL_DEBUG_CALLBACK_FUNCTION ||'
820 print ' pname == GL_DEBUG_CALLBACK_USER_PARAM)) {'
821 print ' *params = NULL;'
822 print ' }'
823
José Fonseca2cfa02c2013-06-10 08:05:29 +0100824 if function.name in self.getProcAddressFunctionNames:
José Fonseca631dbd12014-12-15 16:34:45 +0000825 nameArg = function.args[0].name
826 print ' if (strcmp("glNotifyMappedBufferRangeVMWX", (const char *)%s) == 0) {' % (nameArg,)
827 print ' _result = (%s)&glNotifyMappedBufferRangeVMWX;' % (function.type,)
José Fonsecaf028a8f2012-02-15 23:33:35 +0000828 for marker_function in self.marker_functions:
José Fonseca1b6c8752012-04-15 14:33:00 +0100829 if self.api.getFunctionByName(marker_function):
José Fonseca631dbd12014-12-15 16:34:45 +0000830 print ' } else if (strcmp("%s", (const char *)%s) == 0) {' % (marker_function, nameArg)
José Fonseca632a78d2012-04-19 07:18:59 +0100831 print ' _result = (%s)&%s;' % (function.type, marker_function)
José Fonseca631dbd12014-12-15 16:34:45 +0000832 print ' } else {'
José Fonseca2cfa02c2013-06-10 08:05:29 +0100833 Tracer.doInvokeFunction(self, function)
834
835 # Replace function addresses with ours
836 # XXX: Doing this here instead of wrapRet means that the trace will
837 # contain the addresses of the wrapper functions, and not the real
838 # functions, but in practice this should make no difference.
839 if function.name in self.getProcAddressFunctionNames:
José Fonseca631dbd12014-12-15 16:34:45 +0000840 print ' _result = _wrapProcAddress(%s, _result);' % (nameArg,)
José Fonseca2cfa02c2013-06-10 08:05:29 +0100841
José Fonseca8f34d342011-07-15 20:16:40 +0100842 print ' }'
José Fonseca1b3d3752011-07-15 10:15:19 +0100843 return
844
José Fonseca2cfa02c2013-06-10 08:05:29 +0100845 Tracer.doInvokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100846
José Fonseca867b1b72011-04-24 11:58:04 +0100847 buffer_targets = [
848 'ARRAY_BUFFER',
849 'ELEMENT_ARRAY_BUFFER',
850 'PIXEL_PACK_BUFFER',
851 'PIXEL_UNPACK_BUFFER',
José Fonseca7b20d672012-01-10 19:13:58 +0000852 'UNIFORM_BUFFER',
853 'TEXTURE_BUFFER',
854 'TRANSFORM_FEEDBACK_BUFFER',
855 'COPY_READ_BUFFER',
856 'COPY_WRITE_BUFFER',
857 'DRAW_INDIRECT_BUFFER',
858 'ATOMIC_COUNTER_BUFFER',
José Fonseca867b1b72011-04-24 11:58:04 +0100859 ]
860
José Fonseca54f304a2012-01-14 19:33:08 +0000861 def wrapRet(self, function, instance):
862 Tracer.wrapRet(self, function, instance)
José Fonseca1b3d3752011-07-15 10:15:19 +0100863
José Fonsecacdc322c2012-02-29 19:29:51 +0000864 # Keep track of buffer mappings
865 if function.name in ('glMapBuffer', 'glMapBufferARB'):
José Fonseca867b1b72011-04-24 11:58:04 +0100866 print ' struct buffer_mapping *mapping = get_buffer_mapping(target);'
867 print ' if (mapping) {'
868 print ' mapping->map = %s;' % (instance)
869 print ' mapping->length = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100870 print ' _glGetBufferParameteriv(target, GL_BUFFER_SIZE, &mapping->length);'
José Fonseca867b1b72011-04-24 11:58:04 +0100871 print ' mapping->write = (access != GL_READ_ONLY);'
José Fonseca73373602011-05-20 17:45:26 +0100872 print ' mapping->explicit_flush = false;'
José Fonseca867b1b72011-04-24 11:58:04 +0100873 print ' }'
José Fonseca867b1b72011-04-24 11:58:04 +0100874 if function.name == 'glMapBufferRange':
José Fonseca9c536b02012-02-29 20:54:13 +0000875 print ' if (access & GL_MAP_WRITE_BIT) {'
José Fonseca632a78d2012-04-19 07:18:59 +0100876 print ' _checkBufferMapRange = true;'
José Fonseca9c536b02012-02-29 20:54:13 +0000877 print ' }'
José Fonseca867b1b72011-04-24 11:58:04 +0100878 print ' struct buffer_mapping *mapping = get_buffer_mapping(target);'
879 print ' if (mapping) {'
880 print ' mapping->map = %s;' % (instance)
881 print ' mapping->length = length;'
882 print ' mapping->write = access & GL_MAP_WRITE_BIT;'
José Fonseca73373602011-05-20 17:45:26 +0100883 print ' mapping->explicit_flush = access & GL_MAP_FLUSH_EXPLICIT_BIT;'
José Fonseca867b1b72011-04-24 11:58:04 +0100884 print ' }'
José Fonseca669b1222011-02-20 09:05:10 +0000885
José Fonsecac9f12232011-03-25 20:07:42 +0000886 boolean_names = [
887 'GL_FALSE',
888 'GL_TRUE',
889 ]
890
891 def gl_boolean(self, value):
892 return self.boolean_names[int(bool(value))]
893
José Fonsecaa442a462014-11-12 21:16:22 +0000894 # Regular expression for the names of the functions that unpack from a
895 # pixel buffer object. See the ARB_pixel_buffer_object specification.
896 unpack_function_regex = re.compile(r'^gl(' + r'|'.join([
897 r'Bitmap',
898 r'PolygonStipple',
899 r'PixelMap[a-z]+v',
900 r'DrawPixels',
901 r'Color(Sub)?Table',
902 r'(Convolution|Separable)Filter[12]D',
903 r'(Compressed)?(Multi)?Tex(ture)?(Sub)?Image[1-4]D',
904 ]) + r')[0-9A-Z]*$')
José Fonsecae97bab92011-06-02 23:15:11 +0100905
José Fonseca54f304a2012-01-14 19:33:08 +0000906 def serializeArgValue(self, function, arg):
José Fonsecae97bab92011-06-02 23:15:11 +0100907 # Recognize offsets instead of blobs when a PBO is bound
José Fonsecaa442a462014-11-12 21:16:22 +0000908 if self.unpack_function_regex.match(function.name) \
José Fonsecae97bab92011-06-02 23:15:11 +0100909 and (isinstance(arg.type, stdapi.Blob) \
910 or (isinstance(arg.type, stdapi.Const) \
911 and isinstance(arg.type.type, stdapi.Blob))):
José Fonsecac29f4f12011-06-11 12:19:05 +0100912 print ' {'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000913 print ' gltrace::Context *ctx = gltrace::getContext();'
José Fonseca632a78d2012-04-19 07:18:59 +0100914 print ' GLint _unpack_buffer = 0;'
José Fonseca34ea6162015-01-08 23:45:43 +0000915 print ' if (ctx->profile.desktop())'
José Fonseca632a78d2012-04-19 07:18:59 +0100916 print ' _glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &_unpack_buffer);'
917 print ' if (_unpack_buffer) {'
José Fonsecad559f022012-04-15 16:13:51 +0100918 print ' trace::localWriter.writePointer((uintptr_t)%s);' % arg.name
José Fonsecac29f4f12011-06-11 12:19:05 +0100919 print ' } else {'
José Fonseca54f304a2012-01-14 19:33:08 +0000920 Tracer.serializeArgValue(self, function, arg)
José Fonsecac29f4f12011-06-11 12:19:05 +0100921 print ' }'
José Fonsecae97bab92011-06-02 23:15:11 +0100922 print ' }'
923 return
924
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100925 # Several GL state functions take GLenum symbolic names as
926 # integer/floats; so dump the symbolic name whenever possible
José Fonseca3bcb33c2011-05-27 20:14:31 +0100927 if function.name.startswith('gl') \
José Fonsecae3571092011-10-13 08:26:27 +0100928 and arg.type in (glapi.GLint, glapi.GLfloat, glapi.GLdouble) \
José Fonseca3bcb33c2011-05-27 20:14:31 +0100929 and arg.name == 'param':
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100930 assert arg.index > 0
931 assert function.args[arg.index - 1].name == 'pname'
932 assert function.args[arg.index - 1].type == glapi.GLenum
933 print ' if (is_symbolic_pname(pname) && is_symbolic_param(%s)) {' % arg.name
José Fonseca54f304a2012-01-14 19:33:08 +0000934 self.serializeValue(glapi.GLenum, arg.name)
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100935 print ' } else {'
José Fonseca54f304a2012-01-14 19:33:08 +0000936 Tracer.serializeArgValue(self, function, arg)
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100937 print ' }'
938 return
939
José Fonseca54f304a2012-01-14 19:33:08 +0000940 Tracer.serializeArgValue(self, function, arg)
José Fonseca99221832011-03-22 22:15:46 +0000941
José Fonseca4c938c22011-04-30 22:44:38 +0100942 def footer(self, api):
943 Tracer.footer(self, api)
José Fonseca669b1222011-02-20 09:05:10 +0000944
José Fonseca4c938c22011-04-30 22:44:38 +0100945 # A simple state tracker to track the pointer values
José Fonseca669b1222011-02-20 09:05:10 +0000946 # update the state
José Fonseca14cb9ef2012-05-17 21:33:14 +0100947 print 'static void _trace_user_arrays(GLuint count)'
José Fonseca669b1222011-02-20 09:05:10 +0000948 print '{'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000949 print ' gltrace::Context *ctx = gltrace::getContext();'
José Fonseca8d1408b2014-02-03 19:57:18 +0000950 print
José Fonsecab0c59722015-01-05 20:45:41 +0000951 print ' glprofile::Profile profile = ctx->profile;'
952 print ' bool es1 = profile.es() && profile.major == 1;'
953 print
José Fonseca8d1408b2014-02-03 19:57:18 +0000954
955 # Temporarily unbind the array buffer
José Fonseca26be8f92014-03-07 14:08:50 +0000956 print ' GLint _array_buffer = _glGetInteger(GL_ARRAY_BUFFER_BINDING);'
José Fonseca8d1408b2014-02-03 19:57:18 +0000957 print ' if (_array_buffer) {'
958 self.fake_glBindBuffer(api, 'GL_ARRAY_BUFFER', '0')
959 print ' }'
960 print
José Fonseca1a2fdd22011-04-01 00:55:09 +0100961
José Fonseca99221832011-03-22 22:15:46 +0000962 for camelcase_name, uppercase_name in self.arrays:
Chia-I Wub3d218d2011-11-03 01:37:36 +0800963 # in which profile is the array available?
José Fonseca34ea6162015-01-08 23:45:43 +0000964 profile_check = 'profile.desktop()'
Chia-I Wub3d218d2011-11-03 01:37:36 +0800965 if camelcase_name in self.arrays_es1:
José Fonsecab0c59722015-01-05 20:45:41 +0000966 profile_check = '(' + profile_check + ' || es1)';
Chia-I Wub3d218d2011-11-03 01:37:36 +0800967
José Fonseca99221832011-03-22 22:15:46 +0000968 function_name = 'gl%sPointer' % camelcase_name
969 enable_name = 'GL_%s_ARRAY' % uppercase_name
970 binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name
José Fonseca1b6c8752012-04-15 14:33:00 +0100971 function = api.getFunctionByName(function_name)
José Fonseca99221832011-03-22 22:15:46 +0000972
José Fonseca06e85192011-10-16 14:15:36 +0100973 print ' // %s' % function.prototype()
Chia-I Wub3d218d2011-11-03 01:37:36 +0800974 print ' if (%s) {' % profile_check
José Fonsecafb6744f2011-04-15 11:18:37 +0100975 self.array_trace_prolog(api, uppercase_name)
976 self.array_prolog(api, uppercase_name)
José Fonseca632a78d2012-04-19 07:18:59 +0100977 print ' if (_glIsEnabled(%s)) {' % enable_name
José Fonseca26be8f92014-03-07 14:08:50 +0000978 print ' GLint _binding = _glGetInteger(%s);' % binding_name
José Fonseca632a78d2012-04-19 07:18:59 +0100979 print ' if (!_binding) {'
José Fonseca99221832011-03-22 22:15:46 +0000980
981 # Get the arguments via glGet*
982 for arg in function.args:
983 arg_get_enum = 'GL_%s_ARRAY_%s' % (uppercase_name, arg.name.upper())
984 arg_get_function, arg_type = TypeGetter().visit(arg.type)
José Fonseca7f5163e2011-03-31 23:37:26 +0100985 print ' %s %s = 0;' % (arg_type, arg.name)
José Fonseca632a78d2012-04-19 07:18:59 +0100986 print ' _%s(%s, &%s);' % (arg_get_function, arg_get_enum, arg.name)
José Fonseca99221832011-03-22 22:15:46 +0000987
988 arg_names = ', '.join([arg.name for arg in function.args[:-1]])
José Fonseca14cb9ef2012-05-17 21:33:14 +0100989 print ' size_t _size = _%s_size(%s, count);' % (function.name, arg_names)
José Fonseca99221832011-03-22 22:15:46 +0000990
991 # Emit a fake function
José Fonsecafb6744f2011-04-15 11:18:37 +0100992 self.array_trace_intermezzo(api, uppercase_name)
José Fonseca7a5f23a2014-06-24 19:20:36 +0100993 print ' unsigned _call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,)
José Fonseca669b1222011-02-20 09:05:10 +0000994 for arg in function.args:
995 assert not arg.output
José Fonsecab4a3d142011-10-27 07:43:19 +0100996 print ' trace::localWriter.beginArg(%u);' % (arg.index,)
José Fonseca14c21bc2011-02-20 23:32:22 +0000997 if arg.name != 'pointer':
José Fonseca54f304a2012-01-14 19:33:08 +0000998 self.serializeValue(arg.type, arg.name)
José Fonseca14c21bc2011-02-20 23:32:22 +0000999 else:
José Fonseca632a78d2012-04-19 07:18:59 +01001000 print ' trace::localWriter.writeBlob((const void *)%s, _size);' % (arg.name)
José Fonsecab4a3d142011-10-27 07:43:19 +01001001 print ' trace::localWriter.endArg();'
José Fonseca99221832011-03-22 22:15:46 +00001002
José Fonsecab4a3d142011-10-27 07:43:19 +01001003 print ' trace::localWriter.endEnter();'
José Fonseca632a78d2012-04-19 07:18:59 +01001004 print ' trace::localWriter.beginLeave(_call);'
José Fonsecab4a3d142011-10-27 07:43:19 +01001005 print ' trace::localWriter.endLeave();'
José Fonseca99221832011-03-22 22:15:46 +00001006 print ' }'
José Fonseca669b1222011-02-20 09:05:10 +00001007 print ' }'
José Fonsecafb6744f2011-04-15 11:18:37 +01001008 self.array_epilog(api, uppercase_name)
1009 self.array_trace_epilog(api, uppercase_name)
Chia-I Wub3d218d2011-11-03 01:37:36 +08001010 print ' }'
José Fonseca99221832011-03-22 22:15:46 +00001011 print
José Fonseca1a2fdd22011-04-01 00:55:09 +01001012
José Fonseca1601c412011-05-10 10:38:19 +01001013 # Samething, but for glVertexAttribPointer*
1014 #
1015 # Some variants of glVertexAttribPointer alias conventional and generic attributes:
1016 # - glVertexAttribPointer: no
1017 # - glVertexAttribPointerARB: implementation dependent
1018 # - glVertexAttribPointerNV: yes
1019 #
1020 # This means that the implementations of these functions do not always
1021 # alias, and they need to be considered independently.
1022 #
Chia-I Wub3d218d2011-11-03 01:37:36 +08001023 print ' // ES1 does not support generic vertex attributes'
José Fonsecab0c59722015-01-05 20:45:41 +00001024 print ' if (es1)'
Chia-I Wub3d218d2011-11-03 01:37:36 +08001025 print ' return;'
1026 print
José Fonseca632a78d2012-04-19 07:18:59 +01001027 print ' vertex_attrib _vertex_attrib = _get_vertex_attrib();'
José Fonseca5a568a92011-06-29 16:43:36 +01001028 print
José Fonseca74b661a2014-07-17 19:10:24 +01001029 for suffix in ['', 'NV']:
José Fonseca5a568a92011-06-29 16:43:36 +01001030 if suffix:
José Fonsecad94aaac2011-06-28 20:50:49 +01001031 SUFFIX = '_' + suffix
José Fonsecad94aaac2011-06-28 20:50:49 +01001032 else:
José Fonseca5a568a92011-06-29 16:43:36 +01001033 SUFFIX = suffix
José Fonseca1601c412011-05-10 10:38:19 +01001034 function_name = 'glVertexAttribPointer' + suffix
José Fonseca1b6c8752012-04-15 14:33:00 +01001035 function = api.getFunctionByName(function_name)
José Fonseca06e85192011-10-16 14:15:36 +01001036
1037 print ' // %s' % function.prototype()
José Fonseca632a78d2012-04-19 07:18:59 +01001038 print ' if (_vertex_attrib == VERTEX_ATTRIB%s) {' % SUFFIX
José Fonseca5a568a92011-06-29 16:43:36 +01001039 if suffix == 'NV':
José Fonseca632a78d2012-04-19 07:18:59 +01001040 print ' GLint _max_vertex_attribs = 16;'
José Fonseca5a568a92011-06-29 16:43:36 +01001041 else:
José Fonseca26be8f92014-03-07 14:08:50 +00001042 print ' GLint _max_vertex_attribs = _glGetInteger(GL_MAX_VERTEX_ATTRIBS);'
José Fonseca632a78d2012-04-19 07:18:59 +01001043 print ' for (GLint index = 0; index < _max_vertex_attribs; ++index) {'
1044 print ' GLint _enabled = 0;'
José Fonseca5a568a92011-06-29 16:43:36 +01001045 if suffix == 'NV':
José Fonseca632a78d2012-04-19 07:18:59 +01001046 print ' _glGetIntegerv(GL_VERTEX_ATTRIB_ARRAY0_NV + index, &_enabled);'
José Fonseca5a568a92011-06-29 16:43:36 +01001047 else:
José Fonseca632a78d2012-04-19 07:18:59 +01001048 print ' _glGetVertexAttribiv%s(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED%s, &_enabled);' % (suffix, SUFFIX)
1049 print ' if (_enabled) {'
1050 print ' GLint _binding = 0;'
José Fonseca5a568a92011-06-29 16:43:36 +01001051 if suffix != 'NV':
1052 # It doesn't seem possible to use VBOs with NV_vertex_program.
José Fonseca632a78d2012-04-19 07:18:59 +01001053 print ' _glGetVertexAttribiv%s(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING%s, &_binding);' % (suffix, SUFFIX)
1054 print ' if (!_binding) {'
José Fonseca1a2fdd22011-04-01 00:55:09 +01001055
José Fonseca1601c412011-05-10 10:38:19 +01001056 # Get the arguments via glGet*
1057 for arg in function.args[1:]:
José Fonseca5a568a92011-06-29 16:43:36 +01001058 if suffix == 'NV':
1059 arg_get_enum = 'GL_ATTRIB_ARRAY_%s%s' % (arg.name.upper(), SUFFIX)
1060 else:
1061 arg_get_enum = 'GL_VERTEX_ATTRIB_ARRAY_%s%s' % (arg.name.upper(), SUFFIX)
José Fonsecac493e3e2011-06-29 12:57:06 +01001062 arg_get_function, arg_type = TypeGetter('glGetVertexAttrib', False, suffix).visit(arg.type)
José Fonseca5a568a92011-06-29 16:43:36 +01001063 print ' %s %s = 0;' % (arg_type, arg.name)
José Fonseca632a78d2012-04-19 07:18:59 +01001064 print ' _%s(index, %s, &%s);' % (arg_get_function, arg_get_enum, arg.name)
José Fonseca1601c412011-05-10 10:38:19 +01001065
1066 arg_names = ', '.join([arg.name for arg in function.args[1:-1]])
José Fonseca14cb9ef2012-05-17 21:33:14 +01001067 print ' size_t _size = _%s_size(%s, count);' % (function.name, arg_names)
José Fonseca1a2fdd22011-04-01 00:55:09 +01001068
José Fonseca1601c412011-05-10 10:38:19 +01001069 # Emit a fake function
José Fonseca7a5f23a2014-06-24 19:20:36 +01001070 print ' unsigned _call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,)
José Fonseca1601c412011-05-10 10:38:19 +01001071 for arg in function.args:
1072 assert not arg.output
José Fonsecab4a3d142011-10-27 07:43:19 +01001073 print ' trace::localWriter.beginArg(%u);' % (arg.index,)
José Fonseca1601c412011-05-10 10:38:19 +01001074 if arg.name != 'pointer':
José Fonseca54f304a2012-01-14 19:33:08 +00001075 self.serializeValue(arg.type, arg.name)
José Fonseca1601c412011-05-10 10:38:19 +01001076 else:
José Fonseca632a78d2012-04-19 07:18:59 +01001077 print ' trace::localWriter.writeBlob((const void *)%s, _size);' % (arg.name)
José Fonsecab4a3d142011-10-27 07:43:19 +01001078 print ' trace::localWriter.endArg();'
José Fonseca1601c412011-05-10 10:38:19 +01001079
José Fonsecab4a3d142011-10-27 07:43:19 +01001080 print ' trace::localWriter.endEnter();'
José Fonseca632a78d2012-04-19 07:18:59 +01001081 print ' trace::localWriter.beginLeave(_call);'
José Fonsecab4a3d142011-10-27 07:43:19 +01001082 print ' trace::localWriter.endLeave();'
José Fonseca1601c412011-05-10 10:38:19 +01001083 print ' }'
1084 print ' }'
1085 print ' }'
1086 print ' }'
1087 print
José Fonseca1a2fdd22011-04-01 00:55:09 +01001088
José Fonseca8d1408b2014-02-03 19:57:18 +00001089 # Restore the original array_buffer
1090 print ' if (_array_buffer) {'
1091 self.fake_glBindBuffer(api, 'GL_ARRAY_BUFFER', '_array_buffer')
1092 print ' }'
1093 print
1094
José Fonseca669b1222011-02-20 09:05:10 +00001095 print '}'
1096 print
1097
José Fonsecafb6744f2011-04-15 11:18:37 +01001098 #
1099 # Hooks for glTexCoordPointer, which is identical to the other array
1100 # pointers except the fact that it is indexed by glClientActiveTexture.
1101 #
1102
1103 def array_prolog(self, api, uppercase_name):
1104 if uppercase_name == 'TEXTURE_COORD':
José Fonseca26be8f92014-03-07 14:08:50 +00001105 print ' GLint client_active_texture = _glGetInteger(GL_CLIENT_ACTIVE_TEXTURE);'
José Fonsecafb6744f2011-04-15 11:18:37 +01001106 print ' GLint max_texture_coords = 0;'
José Fonseca34ea6162015-01-08 23:45:43 +00001107 print ' if (ctx->profile.desktop())'
José Fonseca632a78d2012-04-19 07:18:59 +01001108 print ' _glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_texture_coords);'
Chia-I Wub3d218d2011-11-03 01:37:36 +08001109 print ' else'
José Fonseca632a78d2012-04-19 07:18:59 +01001110 print ' _glGetIntegerv(GL_MAX_TEXTURE_UNITS, &max_texture_coords);'
José Fonsecafb6744f2011-04-15 11:18:37 +01001111 print ' for (GLint unit = 0; unit < max_texture_coords; ++unit) {'
José Fonseca7525e6f2011-09-28 09:04:56 +01001112 print ' GLint texture = GL_TEXTURE0 + unit;'
José Fonseca632a78d2012-04-19 07:18:59 +01001113 print ' _glClientActiveTexture(texture);'
José Fonsecafb6744f2011-04-15 11:18:37 +01001114
1115 def array_trace_prolog(self, api, uppercase_name):
1116 if uppercase_name == 'TEXTURE_COORD':
1117 print ' bool client_active_texture_dirty = false;'
1118
1119 def array_epilog(self, api, uppercase_name):
1120 if uppercase_name == 'TEXTURE_COORD':
1121 print ' }'
1122 self.array_cleanup(api, uppercase_name)
1123
1124 def array_cleanup(self, api, uppercase_name):
1125 if uppercase_name == 'TEXTURE_COORD':
José Fonseca632a78d2012-04-19 07:18:59 +01001126 print ' _glClientActiveTexture(client_active_texture);'
José Fonsecafb6744f2011-04-15 11:18:37 +01001127
1128 def array_trace_intermezzo(self, api, uppercase_name):
1129 if uppercase_name == 'TEXTURE_COORD':
1130 print ' if (texture != client_active_texture || client_active_texture_dirty) {'
1131 print ' client_active_texture_dirty = true;'
1132 self.fake_glClientActiveTexture_call(api, "texture");
1133 print ' }'
1134
1135 def array_trace_epilog(self, api, uppercase_name):
1136 if uppercase_name == 'TEXTURE_COORD':
1137 print ' if (client_active_texture_dirty) {'
1138 self.fake_glClientActiveTexture_call(api, "client_active_texture");
1139 print ' }'
1140
José Fonseca8d1408b2014-02-03 19:57:18 +00001141 def fake_glBindBuffer(self, api, target, buffer):
1142 function = api.getFunctionByName('glBindBuffer')
1143 self.fake_call(function, [target, buffer])
1144
José Fonsecafb6744f2011-04-15 11:18:37 +01001145 def fake_glClientActiveTexture_call(self, api, texture):
José Fonseca1b6c8752012-04-15 14:33:00 +01001146 function = api.getFunctionByName('glClientActiveTexture')
José Fonsecafb6744f2011-04-15 11:18:37 +01001147 self.fake_call(function, [texture])
1148
José Fonseca151c3702013-05-10 08:28:15 +01001149 def emitFakeTexture2D(self):
1150 function = glapi.glapi.getFunctionByName('glTexImage2D')
1151 instances = function.argNames()
José Fonseca7a5f23a2014-06-24 19:20:36 +01001152 print ' unsigned _fake_call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,)
José Fonseca151c3702013-05-10 08:28:15 +01001153 for arg in function.args:
1154 assert not arg.output
1155 self.serializeArg(function, arg)
1156 print ' trace::localWriter.endEnter();'
1157 print ' trace::localWriter.beginLeave(_fake_call);'
1158 print ' trace::localWriter.endLeave();'
José Fonsecafb6744f2011-04-15 11:18:37 +01001159
1160
1161
1162
1163
José Fonseca669b1222011-02-20 09:05:10 +00001164
1165
1166
1167
1168
1169