blob: 5c7d22b8cf3c8be041b2ea05cc35917d7a1142c4 [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
José Fonseca867b1b72011-04-24 11:58:04 +0100226
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100227 # Generate a helper function to determine whether a parameter name
228 # refers to a symbolic value or not
229 print 'static bool'
230 print 'is_symbolic_pname(GLenum pname) {'
José Fonseca06e85192011-10-16 14:15:36 +0100231 print ' switch (pname) {'
José Fonseca5ea91872011-05-04 09:41:55 +0100232 for function, type, count, name in glparams.parameters:
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100233 if type is glapi.GLenum:
José Fonseca4c938c22011-04-30 22:44:38 +0100234 print ' case %s:' % name
235 print ' return true;'
236 print ' default:'
237 print ' return false;'
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100238 print ' }'
239 print '}'
240 print
241
242 # Generate a helper function to determine whether a parameter value is
243 # potentially symbolic or not; i.e., if the value can be represented in
244 # an enum or not
245 print 'template<class T>'
246 print 'static inline bool'
247 print 'is_symbolic_param(T param) {'
248 print ' return static_cast<T>(static_cast<GLenum>(param)) == param;'
249 print '}'
250 print
José Fonseca4c938c22011-04-30 22:44:38 +0100251
252 # Generate a helper function to know how many elements a parameter has
253 print 'static size_t'
José Fonseca632a78d2012-04-19 07:18:59 +0100254 print '_gl_param_size(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:
Jose Fonseca11b6bfa2015-07-21 13:32:51 +0100257 if name == 'GL_PROGRAM_BINARY_FORMATS':
258 count = 0
José Fonseca4c938c22011-04-30 22:44:38 +0100259 if type is not None:
José Fonsecaddf6d2c2012-12-20 15:34:50 +0000260 print ' case %s: return %s;' % (name, count)
José Fonseca4c938c22011-04-30 22:44:38 +0100261 print ' default:'
José Fonseca559d5342011-10-27 08:10:56 +0100262 print r' os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, pname);'
José Fonseca4c938c22011-04-30 22:44:38 +0100263 print ' return 1;'
264 print ' }'
265 print '}'
266 print
267
Chia-I Wu335efb42011-11-03 01:59:22 +0800268 # states such as GL_UNPACK_ROW_LENGTH are not available in GLES
269 print 'static inline bool'
270 print 'can_unpack_subimage(void) {'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000271 print ' gltrace::Context *ctx = gltrace::getContext();'
José Fonseca34ea6162015-01-08 23:45:43 +0000272 print ' return ctx->profile.desktop();'
Chia-I Wu335efb42011-11-03 01:59:22 +0800273 print '}'
274 print
275
José Fonseca631dbd12014-12-15 16:34:45 +0000276 # VMWX_map_buffer_debug
277 print r'extern "C" PUBLIC'
278 print r'void APIENTRY'
279 print r'glNotifyMappedBufferRangeVMWX(const void * start, GLsizeiptr length) {'
280 self.emit_memcpy('start', 'length')
281 print r'}'
282 print
283
José Fonseca1b6c8752012-04-15 14:33:00 +0100284 getProcAddressFunctionNames = []
285
286 def traceApi(self, api):
287 if self.getProcAddressFunctionNames:
288 # Generate a function to wrap proc addresses
289 getProcAddressFunction = api.getFunctionByName(self.getProcAddressFunctionNames[0])
290 argType = getProcAddressFunction.args[0].type
291 retType = getProcAddressFunction.type
292
293 print 'static %s _wrapProcAddress(%s procName, %s procPtr);' % (retType, argType, retType)
294 print
295
296 Tracer.traceApi(self, api)
297
298 print 'static %s _wrapProcAddress(%s procName, %s procPtr) {' % (retType, argType, retType)
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700299
300 # Provide fallback functions to missing debug functions
José Fonseca1b6c8752012-04-15 14:33:00 +0100301 print ' if (!procPtr) {'
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700302 else_ = ''
303 for function_name in self.debug_functions:
304 if self.api.getFunctionByName(function_name):
305 print ' %sif (strcmp("%s", (const char *)procName) == 0) {' % (else_, function_name)
306 print ' return (%s)&%s;' % (retType, function_name)
307 print ' }'
308 else_ = 'else '
309 print ' %s{' % else_
310 print ' return NULL;'
311 print ' }'
José Fonseca1b6c8752012-04-15 14:33:00 +0100312 print ' }'
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700313
José Fonseca81301932012-11-11 00:10:20 +0000314 for function in api.getAllFunctions():
José Fonseca1b6c8752012-04-15 14:33:00 +0100315 ptype = function_pointer_type(function)
316 pvalue = function_pointer_value(function)
317 print ' if (strcmp("%s", (const char *)procName) == 0) {' % function.name
318 print ' %s = (%s)procPtr;' % (pvalue, ptype)
319 print ' return (%s)&%s;' % (retType, function.name,)
320 print ' }'
321 print ' os::log("apitrace: warning: unknown function \\"%s\\"\\n", (const char *)procName);'
322 print ' return procPtr;'
323 print '}'
324 print
325 else:
326 Tracer.traceApi(self, api)
327
Imre Deakd4937372012-04-24 14:06:48 +0300328 def defineShadowBufferHelper(self):
329 print 'void _shadow_glGetBufferSubData(GLenum target, GLintptr offset,'
330 print ' GLsizeiptr size, GLvoid *data)'
331 print '{'
José Fonsecaa33d0bb2012-11-10 09:11:42 +0000332 print ' gltrace::Context *ctx = gltrace::getContext();'
Imre Deakd4937372012-04-24 14:06:48 +0300333 print ' if (!ctx->needsShadowBuffers() || target != GL_ELEMENT_ARRAY_BUFFER) {'
José Fonseca219c9f22012-11-03 10:13:17 +0000334 print ' _glGetBufferSubData(target, offset, size, data);'
Imre Deakd4937372012-04-24 14:06:48 +0300335 print ' return;'
336 print ' }'
337 print
José Fonseca26be8f92014-03-07 14:08:50 +0000338 print ' GLint buffer_binding = _glGetInteger(GL_ELEMENT_ARRAY_BUFFER_BINDING);'
José Fonseca219c9f22012-11-03 10:13:17 +0000339 print ' if (buffer_binding > 0) {'
340 print ' gltrace::Buffer & buf = ctx->buffers[buffer_binding];'
341 print ' buf.getSubData(offset, size, data);'
342 print ' }'
Imre Deakd4937372012-04-24 14:06:48 +0300343 print '}'
344
José Fonseca219c9f22012-11-03 10:13:17 +0000345 def shadowBufferMethod(self, method):
346 # Emit code to fetch the shadow buffer, and invoke a method
347 print ' gltrace::Context *ctx = gltrace::getContext();'
348 print ' if (ctx->needsShadowBuffers() && target == GL_ELEMENT_ARRAY_BUFFER) {'
José Fonseca26be8f92014-03-07 14:08:50 +0000349 print ' GLint buffer_binding = _glGetInteger(GL_ELEMENT_ARRAY_BUFFER_BINDING);'
José Fonseca219c9f22012-11-03 10:13:17 +0000350 print ' if (buffer_binding > 0) {'
351 print ' gltrace::Buffer & buf = ctx->buffers[buffer_binding];'
352 print ' buf.' + method + ';'
353 print ' }'
354 print ' }'
355 print
356
Imre Deakd4937372012-04-24 14:06:48 +0300357 def shadowBufferProlog(self, function):
358 if function.name == 'glBufferData':
José Fonseca219c9f22012-11-03 10:13:17 +0000359 self.shadowBufferMethod('bufferData(size, data)')
Imre Deakd4937372012-04-24 14:06:48 +0300360
361 if function.name == 'glBufferSubData':
José Fonseca219c9f22012-11-03 10:13:17 +0000362 self.shadowBufferMethod('bufferSubData(offset, size, data)')
Imre Deakd4937372012-04-24 14:06:48 +0300363
364 if function.name == 'glDeleteBuffers':
365 print ' gltrace::Context *ctx = gltrace::getContext();'
366 print ' if (ctx->needsShadowBuffers()) {'
José Fonseca219c9f22012-11-03 10:13:17 +0000367 print ' for (GLsizei i = 0; i < n; i++) {'
368 print ' ctx->buffers.erase(buffer[i]);'
Imre Deakd4937372012-04-24 14:06:48 +0300369 print ' }'
370 print ' }'
371
José Fonseca99221832011-03-22 22:15:46 +0000372 array_pointer_function_names = set((
373 "glVertexPointer",
374 "glNormalPointer",
375 "glColorPointer",
376 "glIndexPointer",
377 "glTexCoordPointer",
378 "glEdgeFlagPointer",
379 "glFogCoordPointer",
380 "glSecondaryColorPointer",
José Fonseca7f5163e2011-03-31 23:37:26 +0100381
José Fonsecaac5285b2011-05-04 11:09:08 +0100382 "glInterleavedArrays",
383
José Fonseca7e0bfd92011-04-30 23:09:03 +0100384 "glVertexPointerEXT",
385 "glNormalPointerEXT",
386 "glColorPointerEXT",
387 "glIndexPointerEXT",
388 "glTexCoordPointerEXT",
389 "glEdgeFlagPointerEXT",
390 "glFogCoordPointerEXT",
391 "glSecondaryColorPointerEXT",
José Fonseca99221832011-03-22 22:15:46 +0000392
José Fonseca7f5163e2011-03-31 23:37:26 +0100393 "glVertexAttribPointer",
394 "glVertexAttribPointerARB",
395 "glVertexAttribPointerNV",
José Fonsecaac5285b2011-05-04 11:09:08 +0100396 "glVertexAttribIPointer",
397 "glVertexAttribIPointerEXT",
José Fonseca7f5163e2011-03-31 23:37:26 +0100398 "glVertexAttribLPointer",
399 "glVertexAttribLPointerEXT",
José Fonseca99221832011-03-22 22:15:46 +0000400
401 #"glMatrixIndexPointerARB",
402 ))
403
José Fonsecac5bf77a2014-08-14 16:10:02 +0100404 # XXX: We currently ignore the gl*Draw*ElementArray* functions
405 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 +0000406
José Fonsecac9f12232011-03-25 20:07:42 +0000407 interleaved_formats = [
408 'GL_V2F',
409 'GL_V3F',
410 'GL_C4UB_V2F',
411 'GL_C4UB_V3F',
412 'GL_C3F_V3F',
413 'GL_N3F_V3F',
414 'GL_C4F_N3F_V3F',
415 'GL_T2F_V3F',
416 'GL_T4F_V4F',
417 'GL_T2F_C4UB_V3F',
418 'GL_T2F_C3F_V3F',
419 'GL_T2F_N3F_V3F',
420 'GL_T2F_C4F_N3F_V3F',
421 'GL_T4F_C4F_N3F_V4F',
422 ]
423
José Fonseca54f304a2012-01-14 19:33:08 +0000424 def traceFunctionImplBody(self, function):
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000425 # Defer tracing of user array pointers...
José Fonseca99221832011-03-22 22:15:46 +0000426 if function.name in self.array_pointer_function_names:
José Fonseca26be8f92014-03-07 14:08:50 +0000427 print ' GLint _array_buffer = _glGetInteger(GL_ARRAY_BUFFER_BINDING);'
José Fonseca632a78d2012-04-19 07:18:59 +0100428 print ' if (!_array_buffer) {'
José Fonsecaf15546b2015-01-20 13:37:45 +0000429 print ' static bool warned = false;'
430 print ' if (!warned) {'
431 print ' warned = true;'
José Fonsecafe91ec32015-01-20 18:17:34 +0000432 print ' os::log("apitrace: warning: %s: call will be faked due to pointer to user memory (https://github.com/apitrace/apitrace/blob/master/docs/BUGS.markdown#tracing)\\n", __FUNCTION__);'
José Fonsecaf15546b2015-01-20 13:37:45 +0000433 print ' }'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000434 print ' gltrace::Context *ctx = gltrace::getContext();'
Chia-I Wu8ef66972011-11-03 01:19:46 +0800435 print ' ctx->user_arrays = true;'
José Fonseca5a568a92011-06-29 16:43:36 +0100436 if function.name == "glVertexAttribPointerNV":
Chia-I Wu8ef66972011-11-03 01:19:46 +0800437 print ' ctx->user_arrays_nv = true;'
José Fonseca54f304a2012-01-14 19:33:08 +0000438 self.invokeFunction(function)
José Fonsecaac5285b2011-05-04 11:09:08 +0100439
440 # And also break down glInterleavedArrays into the individual calls
441 if function.name == 'glInterleavedArrays':
442 print
443
444 # Initialize the enable flags
445 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100446 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100447 print ' GLboolean %s = GL_FALSE;' % flag_name
448 print
449
450 # Switch for the interleaved formats
451 print ' switch (format) {'
452 for format in self.interleaved_formats:
453 print ' case %s:' % format
454 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100455 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100456 if format.find('_' + uppercase_name[0]) >= 0:
457 print ' %s = GL_TRUE;' % flag_name
458 print ' break;'
459 print ' default:'
460 print ' return;'
461 print ' }'
462 print
463
464 # Emit fake glEnableClientState/glDisableClientState flags
465 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100466 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100467 enable_name = 'GL_%s_ARRAY' % uppercase_name
468
469 # Emit a fake function
470 print ' {'
José Fonseca632a78d2012-04-19 07:18:59 +0100471 print ' static const trace::FunctionSig &_sig = %s ? _glEnableClientState_sig : _glDisableClientState_sig;' % flag_name
José Fonseca7a5f23a2014-06-24 19:20:36 +0100472 print ' unsigned _call = trace::localWriter.beginEnter(&_sig, true);'
José Fonsecab4a3d142011-10-27 07:43:19 +0100473 print ' trace::localWriter.beginArg(0);'
José Fonseca54f304a2012-01-14 19:33:08 +0000474 self.serializeValue(glapi.GLenum, enable_name)
José Fonsecab4a3d142011-10-27 07:43:19 +0100475 print ' trace::localWriter.endArg();'
476 print ' trace::localWriter.endEnter();'
José Fonseca632a78d2012-04-19 07:18:59 +0100477 print ' trace::localWriter.beginLeave(_call);'
José Fonsecab4a3d142011-10-27 07:43:19 +0100478 print ' trace::localWriter.endLeave();'
José Fonsecaac5285b2011-05-04 11:09:08 +0100479 print ' }'
480
José Fonsecac629a8c2014-06-01 21:12:27 +0100481 # Warn about buggy glGet(GL_*ARRAY_SIZE) not returning GL_BGRA
482 buggyFunctions = {
483 'glColorPointer': ('glGetIntegerv', '', 'GL_COLOR_ARRAY_SIZE'),
484 'glSecondaryColorPointer': ('glGetIntegerv', '', 'GL_SECONDARY_COLOR_ARRAY_SIZE'),
485 'glVertexAttribPointer': ('glGetVertexAttribiv', 'index, ', 'GL_VERTEX_ATTRIB_ARRAY_SIZE'),
486 'glVertexAttribPointerARB': ('glGetVertexAttribivARB', 'index, ', 'GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB'),
487 }
488 if function.name in buggyFunctions:
489 getter, extraArg, pname = buggyFunctions[function.name]
490 print r' static bool _checked = false;'
491 print r' if (!_checked && size == GL_BGRA) {'
492 print r' GLint _size = 0;'
493 print r' _%s(%s%s, &_size);' % (getter, extraArg, pname)
494 print r' if (_size != GL_BGRA) {'
495 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)
496 print r' }'
497 print r' _checked = true;'
498 print r' }'
499
José Fonseca99221832011-03-22 22:15:46 +0000500 print ' return;'
501 print ' }'
José Fonseca14c21bc2011-02-20 23:32:22 +0000502
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000503 # ... to the draw calls
José Fonsecac5bf77a2014-08-14 16:10:02 +0100504 if self.draw_function_regex.match(function.name):
José Fonseca632a78d2012-04-19 07:18:59 +0100505 print ' if (_need_user_arrays()) {'
José Fonseca246508e2014-08-14 16:07:46 +0100506 if 'Indirect' in function.name:
507 print r' os::log("apitrace: warning: %s: indirect user arrays not supported\n");' % (function.name,)
508 else:
509 arg_names = ', '.join([arg.name for arg in function.args[1:]])
510 print ' GLuint _count = _%s_count(%s);' % (function.name, arg_names)
511 # Some apps, in particular Quake3, can tell the driver to lock more
512 # vertices than those actually required for the draw call.
513 print ' if (_checkLockArraysEXT) {'
514 print ' GLuint _locked_count = _glGetInteger(GL_ARRAY_ELEMENT_LOCK_FIRST_EXT)'
515 print ' + _glGetInteger(GL_ARRAY_ELEMENT_LOCK_COUNT_EXT);'
516 print ' _count = std::max(_count, _locked_count);'
517 print ' }'
518 print ' _trace_user_arrays(_count);'
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000519 print ' }'
José Fonseca707630d2014-03-07 14:20:35 +0000520 if function.name == 'glLockArraysEXT':
521 print ' _checkLockArraysEXT = true;'
José Fonseca4a7d8602014-06-18 16:03:44 +0100522
523 # Warn if user arrays are used with glBegin/glArrayElement/glEnd.
524 if function.name == 'glBegin':
José Fonseca7c39d012014-11-07 19:46:53 +0000525 print r' gltrace::Context *ctx = gltrace::getContext();'
526 print r' ctx->userArraysOnBegin = _need_user_arrays();'
527 if function.name.startswith('glArrayElement'):
528 print r' gltrace::Context *ctx = gltrace::getContext();'
529 print r' if (ctx->userArraysOnBegin) {'
José Fonseca4a7d8602014-06-18 16:03:44 +0100530 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 +0000531 print r' ctx->userArraysOnBegin = false;'
José Fonseca4a7d8602014-06-18 16:03:44 +0100532 print r' }'
José Fonseca14c21bc2011-02-20 23:32:22 +0000533
José Fonseca73373602011-05-20 17:45:26 +0100534 # Emit a fake memcpy on buffer uploads
José Fonseca9c536b02012-02-29 20:54:13 +0000535 if function.name == 'glBufferParameteriAPPLE':
536 print ' if (pname == GL_BUFFER_FLUSHING_UNMAP_APPLE && param == GL_FALSE) {'
José Fonseca632a78d2012-04-19 07:18:59 +0100537 print ' _checkBufferFlushingUnmapAPPLE = true;'
José Fonseca9c536b02012-02-29 20:54:13 +0000538 print ' }'
José Fonsecacdc322c2012-02-29 19:29:51 +0000539 if function.name in ('glUnmapBuffer', 'glUnmapBufferARB'):
José Fonseca9c536b02012-02-29 20:54:13 +0000540 if function.name.endswith('ARB'):
541 suffix = 'ARB'
542 else:
543 suffix = ''
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000544 print ' GLint access_flags = 0;'
José Fonseca9c536b02012-02-29 20:54:13 +0000545 print ' GLint access = 0;'
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000546 print ' bool flush;'
547 print ' // GLES3 does not have GL_BUFFER_ACCESS;'
548 print ' if (_checkBufferMapRange) {'
549 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_ACCESS_FLAGS, &access_flags);' % suffix
550 print ' flush = (access_flags & GL_MAP_WRITE_BIT) && !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT));'
551 print ' } else {'
552 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_ACCESS, &access);' % suffix
553 print ' flush = access != GL_READ_ONLY;'
554 print ' }'
555 print ' if (flush) {'
José Fonseca9c536b02012-02-29 20:54:13 +0000556 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100557 print ' _glGetBufferPointerv%s(target, GL_BUFFER_MAP_POINTER, &map);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000558 print ' if (map) {'
559 print ' GLint length = -1;'
José Fonseca632a78d2012-04-19 07:18:59 +0100560 print ' if (_checkBufferMapRange) {'
561 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_MAP_LENGTH, &length);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000562 print ' if (length == -1) {'
José Fonsecacb07e2d2014-02-04 15:14:09 +0000563 print ' // Mesa drivers refuse GL_BUFFER_MAP_LENGTH without GL 3.0 up-to'
564 print ' // http://cgit.freedesktop.org/mesa/mesa/commit/?id=ffee498fb848b253a7833373fe5430f8c7ca0c5f'
José Fonsecadb1ccce2012-02-29 21:09:24 +0000565 print ' static bool warned = false;'
566 print ' if (!warned) {'
567 print ' os::log("apitrace: warning: glGetBufferParameteriv%s(GL_BUFFER_MAP_LENGTH) failed\\n");' % suffix
568 print ' warned = true;'
569 print ' }'
José Fonseca9c536b02012-02-29 20:54:13 +0000570 print ' }'
571 print ' } else {'
572 print ' length = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100573 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_SIZE, &length);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000574 print ' }'
José Fonseca632a78d2012-04-19 07:18:59 +0100575 print ' if (_checkBufferFlushingUnmapAPPLE) {'
José Fonseca9c536b02012-02-29 20:54:13 +0000576 print ' GLint flushing_unmap = GL_TRUE;'
José Fonseca632a78d2012-04-19 07:18:59 +0100577 print ' _glGetBufferParameteriv%s(target, GL_BUFFER_FLUSHING_UNMAP_APPLE, &flushing_unmap);' % suffix
José Fonseca9c536b02012-02-29 20:54:13 +0000578 print ' flush = flush && flushing_unmap;'
579 print ' }'
580 print ' if (flush && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100581 self.emit_memcpy('map', 'length')
José Fonseca9c536b02012-02-29 20:54:13 +0000582 print ' }'
583 print ' }'
584 print ' }'
José Fonsecacdc322c2012-02-29 19:29:51 +0000585 if function.name == 'glUnmapBufferOES':
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000586 print ' GLint access_flags = 0;'
José Fonsecacdc322c2012-02-29 19:29:51 +0000587 print ' GLint access = 0;'
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000588 print ' bool flush;'
589 print ' // GLES3 does not have GL_BUFFER_ACCESS;'
590 print ' if (_checkBufferMapRange) {'
591 print ' _glGetBufferParameteriv(target, GL_BUFFER_ACCESS_FLAGS, &access_flags);'
592 print ' flush = (access_flags & GL_MAP_WRITE_BIT) && !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT));'
593 print ' } else {'
594 print ' _glGetBufferParameteriv(target, GL_BUFFER_ACCESS, &access);'
595 print ' flush = access != GL_READ_ONLY;'
596 print ' }'
597 print ' if (flush) {'
José Fonsecacdc322c2012-02-29 19:29:51 +0000598 print ' GLvoid *map = NULL;'
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000599 print ' _glGetBufferPointervOES(target, GL_BUFFER_MAP_POINTER, &map);'
600 print ' if (map) {'
601 print ' GLint length = 0;'
602 print ' GLint offset = 0;'
603 print ' if (_checkBufferMapRange) {'
604 print ' _glGetBufferParameteriv(target, GL_BUFFER_MAP_LENGTH, &length);'
605 print ' _glGetBufferParameteriv(target, GL_BUFFER_MAP_OFFSET, &offset);'
606 print ' } else {'
607 print ' _glGetBufferParameteriv(target, GL_BUFFER_SIZE, &length);'
608 print ' }'
609 print ' if (flush && length > 0) {'
610 self.emit_memcpy('map', 'length')
611 self.shadowBufferMethod('bufferSubData(offset, length, map)')
612 print ' }'
José Fonsecacdc322c2012-02-29 19:29:51 +0000613 print ' }'
José Fonseca867b1b72011-04-24 11:58:04 +0100614 print ' }'
José Fonseca4920c302014-08-13 18:35:57 +0100615 if function.name == 'glUnmapNamedBuffer':
616 print ' GLint access_flags = 0;'
617 print ' _glGetNamedBufferParameteriv(buffer, GL_BUFFER_ACCESS_FLAGS, &access_flags);'
José Fonsecaa4210e22014-12-13 15:49:37 +0000618 print ' if ((access_flags & GL_MAP_WRITE_BIT) &&'
619 print ' !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT))) {'
José Fonseca4920c302014-08-13 18:35:57 +0100620 print ' GLvoid *map = NULL;'
621 print ' _glGetNamedBufferPointerv(buffer, GL_BUFFER_MAP_POINTER, &map);'
622 print ' GLint length = 0;'
623 print ' _glGetNamedBufferParameteriv(buffer, GL_BUFFER_MAP_LENGTH, &length);'
624 print ' if (map && length > 0) {'
625 self.emit_memcpy('map', 'length')
626 print ' }'
627 print ' }'
José Fonsecafb3bd602012-01-15 13:56:28 +0000628 if function.name == 'glUnmapNamedBufferEXT':
José Fonseca024aff42012-02-29 18:00:06 +0000629 print ' GLint access_flags = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100630 print ' _glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_ACCESS_FLAGS, &access_flags);'
José Fonsecaa4210e22014-12-13 15:49:37 +0000631 print ' if ((access_flags & GL_MAP_WRITE_BIT) &&'
632 print ' !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT))) {'
José Fonsecafb3bd602012-01-15 13:56:28 +0000633 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100634 print ' _glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &map);'
José Fonsecafb3bd602012-01-15 13:56:28 +0000635 print ' GLint length = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100636 print ' _glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_MAP_LENGTH, &length);'
José Fonseca024aff42012-02-29 18:00:06 +0000637 print ' if (map && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100638 self.emit_memcpy('map', 'length')
José Fonseca024aff42012-02-29 18:00:06 +0000639 print ' }'
José Fonsecafb3bd602012-01-15 13:56:28 +0000640 print ' }'
José Fonseca77ef0ce2012-02-29 18:08:48 +0000641 if function.name == 'glFlushMappedBufferRange':
642 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100643 print ' _glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &map);'
José Fonseca77ef0ce2012-02-29 18:08:48 +0000644 print ' if (map && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100645 self.emit_memcpy('(const char *)map + offset', 'length')
José Fonseca77ef0ce2012-02-29 18:08:48 +0000646 print ' }'
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000647 if function.name == 'glFlushMappedBufferRangeEXT':
648 print ' GLvoid *map = NULL;'
649 print ' _glGetBufferPointervOES(target, GL_BUFFER_MAP_POINTER_OES, &map);'
650 print ' if (map && length > 0) {'
651 self.emit_memcpy('(const char *)map + offset', 'length')
652 print ' }'
José Fonseca77ef0ce2012-02-29 18:08:48 +0000653 if function.name == 'glFlushMappedBufferRangeAPPLE':
654 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100655 print ' _glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &map);'
José Fonseca77ef0ce2012-02-29 18:08:48 +0000656 print ' if (map && size > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100657 self.emit_memcpy('(const char *)map + offset', 'size')
José Fonseca73373602011-05-20 17:45:26 +0100658 print ' }'
José Fonseca4920c302014-08-13 18:35:57 +0100659 if function.name == 'glFlushMappedNamedBufferRange':
660 print ' GLvoid *map = NULL;'
661 print ' _glGetNamedBufferPointerv(buffer, GL_BUFFER_MAP_POINTER, &map);'
662 print ' if (map && length > 0) {'
663 self.emit_memcpy('(const char *)map + offset', 'length')
664 print ' }'
José Fonsecafb3bd602012-01-15 13:56:28 +0000665 if function.name == 'glFlushMappedNamedBufferRangeEXT':
666 print ' GLvoid *map = NULL;'
José Fonseca632a78d2012-04-19 07:18:59 +0100667 print ' _glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &map);'
José Fonseca024aff42012-02-29 18:00:06 +0000668 print ' if (map && length > 0) {'
José Fonseca6f0e3032014-06-25 01:00:35 +0100669 self.emit_memcpy('(const char *)map + offset', 'length')
José Fonsecafb3bd602012-01-15 13:56:28 +0000670 print ' }'
José Fonseca867b1b72011-04-24 11:58:04 +0100671
José Fonseca3522cbd2014-02-28 14:45:32 +0000672 # FIXME: We don't support coherent/pinned memory mappings
José Fonseca631dbd12014-12-15 16:34:45 +0000673 if function.name in ('glBufferStorage', 'glNamedBufferStorage', 'glNamedBufferStorageEXT'):
674 print r' if (!(flags & GL_MAP_PERSISTENT_BIT)) {'
675 print r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_PERSISTENT_BIT\n", __FUNCTION__);'
676 print r' }'
677 print r' flags &= ~GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX;'
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000678 if function.name in ('glMapBufferRange', 'glMapBufferRangeEXT', 'glMapNamedBufferRange', 'glMapNamedBufferRangeEXT'):
José Fonseca631dbd12014-12-15 16:34:45 +0000679 print r' if (access & GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX) {'
680 print r' if (!(access & GL_MAP_PERSISTENT_BIT)) {'
681 print r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_PERSISTENT_BIT\n", __FUNCTION__);'
682 print r' }'
683 print r' if (access & GL_MAP_FLUSH_EXPLICIT_BIT) {'
684 print r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/ MAP_FLUSH_EXPLICIT_BIT\n", __FUNCTION__);'
685 print r' }'
686 print r' access &= ~GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX;'
687 print r' } else if (access & GL_MAP_COHERENT_BIT) {'
José Fonsecaa4210e22014-12-13 15:49:37 +0000688 print r' os::log("apitrace: warning: %s: MAP_COHERENT_BIT unsupported (https://github.com/apitrace/apitrace/issues/232)\n", __FUNCTION__);'
689 print r' } else if ((access & GL_MAP_PERSISTENT_BIT) &&'
690 print r' !(access & GL_MAP_FLUSH_EXPLICIT_BIT)) {'
691 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 +0000692 print r' }'
693 if function.name in ('glBufferData', 'glBufferDataARB'):
694 print r' if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {'
695 print r' os::log("apitrace: warning: GL_AMD_pinned_memory not fully supported\n");'
696 print r' }'
697
José Fonsecad0f1e292014-11-13 13:21:51 +0000698 # TODO: We don't track GL_INTEL_map_texture mappings
699 if function.name == 'glMapTexture2DINTEL':
700 print r' if (access & GL_MAP_WRITE_BIT) {'
701 print r' os::log("apitrace: warning: GL_INTEL_map_texture not fully supported\n");'
702 print r' }'
703
José Fonseca91492d22011-05-23 21:20:31 +0100704 # Don't leave vertex attrib locations to chance. Instead emit fake
705 # glBindAttribLocation calls to ensure that the same locations will be
706 # used when retracing. Trying to remap locations after the fact would
707 # be an herculian task given that vertex attrib locations appear in
708 # many entry-points, including non-shader related ones.
709 if function.name == 'glLinkProgram':
José Fonseca54f304a2012-01-14 19:33:08 +0000710 Tracer.invokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100711 print ' GLint active_attributes = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100712 print ' _glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &active_attributes);'
José Fonseca7525e6f2011-09-28 09:04:56 +0100713 print ' for (GLint attrib = 0; attrib < active_attributes; ++attrib) {'
José Fonseca91492d22011-05-23 21:20:31 +0100714 print ' GLint size = 0;'
715 print ' GLenum type = 0;'
716 print ' GLchar name[256];'
717 # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
José Fonseca632a78d2012-04-19 07:18:59 +0100718 print ' _glGetActiveAttrib(program, attrib, sizeof name, NULL, &size, &type, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100719 print " if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {"
José Fonseca632a78d2012-04-19 07:18:59 +0100720 print ' GLint location = _glGetAttribLocation(program, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100721 print ' if (location >= 0) {'
José Fonseca1b6c8752012-04-15 14:33:00 +0100722 bind_function = glapi.glapi.getFunctionByName('glBindAttribLocation')
José Fonseca91492d22011-05-23 21:20:31 +0100723 self.fake_call(bind_function, ['program', 'location', 'name'])
José Fonseca2a794f52011-05-26 20:54:29 +0100724 print ' }'
José Fonseca91492d22011-05-23 21:20:31 +0100725 print ' }'
726 print ' }'
727 if function.name == 'glLinkProgramARB':
José Fonseca54f304a2012-01-14 19:33:08 +0000728 Tracer.invokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100729 print ' GLint active_attributes = 0;'
José Fonseca632a78d2012-04-19 07:18:59 +0100730 print ' _glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_ATTRIBUTES_ARB, &active_attributes);'
José Fonseca7525e6f2011-09-28 09:04:56 +0100731 print ' for (GLint attrib = 0; attrib < active_attributes; ++attrib) {'
José Fonseca91492d22011-05-23 21:20:31 +0100732 print ' GLint size = 0;'
733 print ' GLenum type = 0;'
734 print ' GLcharARB name[256];'
735 # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
José Fonseca632a78d2012-04-19 07:18:59 +0100736 print ' _glGetActiveAttribARB(programObj, attrib, sizeof name, NULL, &size, &type, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100737 print " if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {"
José Fonseca632a78d2012-04-19 07:18:59 +0100738 print ' GLint location = _glGetAttribLocationARB(programObj, name);'
José Fonseca2a794f52011-05-26 20:54:29 +0100739 print ' if (location >= 0) {'
José Fonseca1b6c8752012-04-15 14:33:00 +0100740 bind_function = glapi.glapi.getFunctionByName('glBindAttribLocationARB')
José Fonseca91492d22011-05-23 21:20:31 +0100741 self.fake_call(bind_function, ['programObj', 'location', 'name'])
José Fonseca2a794f52011-05-26 20:54:29 +0100742 print ' }'
José Fonseca91492d22011-05-23 21:20:31 +0100743 print ' }'
744 print ' }'
745
Imre Deakd4937372012-04-24 14:06:48 +0300746 self.shadowBufferProlog(function)
747
José Fonseca54f304a2012-01-14 19:33:08 +0000748 Tracer.traceFunctionImplBody(self, function)
José Fonseca73373602011-05-20 17:45:26 +0100749
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700750 # These entrypoints are only expected to be implemented by tools;
751 # drivers will probably not implement them.
José Fonsecaf028a8f2012-02-15 23:33:35 +0000752 marker_functions = [
753 # GL_GREMEDY_string_marker
José Fonseca8f34d342011-07-15 20:16:40 +0100754 'glStringMarkerGREMEDY',
José Fonsecaf028a8f2012-02-15 23:33:35 +0000755 # GL_GREMEDY_frame_terminator
José Fonseca8f34d342011-07-15 20:16:40 +0100756 'glFrameTerminatorGREMEDY',
757 ]
758
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700759 # These entrypoints may be implemented by drivers, but are also very useful
760 # for debugging / analysis tools.
761 debug_functions = [
762 # GL_KHR_debug
763 'glDebugMessageControl',
764 'glDebugMessageInsert',
765 'glDebugMessageCallback',
766 'glGetDebugMessageLog',
767 'glPushDebugGroup',
768 'glPopDebugGroup',
769 'glObjectLabel',
770 'glGetObjectLabel',
771 'glObjectPtrLabel',
772 'glGetObjectPtrLabel',
Jose Fonsecae01aa3b2015-06-27 11:07:05 +0100773 # GL_KHR_debug (for OpenGL ES)
774 'glDebugMessageControlKHR',
775 'glDebugMessageInsertKHR',
776 'glDebugMessageCallbackKHR',
777 'glGetDebugMessageLogKHR',
778 'glPushDebugGroupKHR',
779 'glPopDebugGroupKHR',
780 'glObjectLabelKHR',
781 'glGetObjectLabelKHR',
782 'glObjectPtrLabelKHR',
783 'glGetObjectPtrLabelKHR',
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700784 # GL_ARB_debug_output
785 'glDebugMessageControlARB',
786 'glDebugMessageInsertARB',
787 'glDebugMessageCallbackARB',
788 'glGetDebugMessageLogARB',
789 # GL_AMD_debug_output
790 'glDebugMessageEnableAMD',
791 'glDebugMessageInsertAMD',
792 'glDebugMessageCallbackAMD',
793 'glGetDebugMessageLogAMD',
José Fonseca71f5a352014-07-28 12:19:50 +0100794 # GL_EXT_debug_label
795 'glLabelObjectEXT',
796 'glGetObjectLabelEXT',
797 # GL_EXT_debug_marker
798 'glInsertEventMarkerEXT',
799 'glPushGroupMarkerEXT',
800 'glPopGroupMarkerEXT',
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700801 ]
802
José Fonseca54f304a2012-01-14 19:33:08 +0000803 def invokeFunction(self, function):
José Fonseca91492d22011-05-23 21:20:31 +0100804 if function.name in ('glLinkProgram', 'glLinkProgramARB'):
805 # These functions have been dispatched already
806 return
807
José Fonsecae0c55fd2015-01-26 22:46:13 +0000808 # Force glProgramBinary to fail. Per ARB_get_program_binary this
809 # should signal the app that it needs to recompile.
810 if function.name in ('glProgramBinary', 'glProgramBinaryOES'):
811 print r' binaryFormat = 0xDEADDEAD;'
812 print r' binary = &binaryFormat;'
813 print r' length = sizeof binaryFormat;'
814
José Fonseca2cfa02c2013-06-10 08:05:29 +0100815 Tracer.invokeFunction(self, function)
816
817 def doInvokeFunction(self, function):
818 # Same as invokeFunction() but called both when trace is enabled or disabled.
819 #
820 # Used to modify the behavior of GL entry-points.
821
822 # Override GL extensions
823 if function.name in ('glGetString', 'glGetIntegerv', 'glGetStringi'):
824 Tracer.doInvokeFunction(self, function, prefix = 'gltrace::_', suffix = '_override')
825 return
826
José Fonseca71f5a352014-07-28 12:19:50 +0100827 # We implement GL_GREMEDY_*, etc., and not the driver
José Fonsecaf028a8f2012-02-15 23:33:35 +0000828 if function.name in self.marker_functions:
José Fonseca8f34d342011-07-15 20:16:40 +0100829 return
830
Peter Lohrmann9d9eb812013-07-12 16:15:25 -0400831 # We may be faking KHR_debug, so ensure the pointer queries result is
832 # always zeroed to prevent dereference of unitialized pointers
833 if function.name == 'glGetPointerv':
834 print ' if (params &&'
835 print ' (pname == GL_DEBUG_CALLBACK_FUNCTION ||'
836 print ' pname == GL_DEBUG_CALLBACK_USER_PARAM)) {'
837 print ' *params = NULL;'
838 print ' }'
839
José Fonseca2cfa02c2013-06-10 08:05:29 +0100840 if function.name in self.getProcAddressFunctionNames:
José Fonseca631dbd12014-12-15 16:34:45 +0000841 nameArg = function.args[0].name
842 print ' if (strcmp("glNotifyMappedBufferRangeVMWX", (const char *)%s) == 0) {' % (nameArg,)
843 print ' _result = (%s)&glNotifyMappedBufferRangeVMWX;' % (function.type,)
José Fonsecaf028a8f2012-02-15 23:33:35 +0000844 for marker_function in self.marker_functions:
José Fonseca1b6c8752012-04-15 14:33:00 +0100845 if self.api.getFunctionByName(marker_function):
José Fonseca631dbd12014-12-15 16:34:45 +0000846 print ' } else if (strcmp("%s", (const char *)%s) == 0) {' % (marker_function, nameArg)
José Fonseca632a78d2012-04-19 07:18:59 +0100847 print ' _result = (%s)&%s;' % (function.type, marker_function)
José Fonseca631dbd12014-12-15 16:34:45 +0000848 print ' } else {'
José Fonseca2cfa02c2013-06-10 08:05:29 +0100849 Tracer.doInvokeFunction(self, function)
850
851 # Replace function addresses with ours
852 # XXX: Doing this here instead of wrapRet means that the trace will
853 # contain the addresses of the wrapper functions, and not the real
854 # functions, but in practice this should make no difference.
855 if function.name in self.getProcAddressFunctionNames:
José Fonseca631dbd12014-12-15 16:34:45 +0000856 print ' _result = _wrapProcAddress(%s, _result);' % (nameArg,)
José Fonseca2cfa02c2013-06-10 08:05:29 +0100857
José Fonseca8f34d342011-07-15 20:16:40 +0100858 print ' }'
José Fonseca1b3d3752011-07-15 10:15:19 +0100859 return
860
José Fonsecae0c55fd2015-01-26 22:46:13 +0000861 if function.name in ('glGetProgramBinary', 'glGetProgramBinaryOES'):
862 print r' bufSize = 0;'
863
José Fonseca2cfa02c2013-06-10 08:05:29 +0100864 Tracer.doInvokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100865
José Fonsecae0c55fd2015-01-26 22:46:13 +0000866 if function.name == 'glGetProgramiv':
867 print r' if (params && pname == GL_PROGRAM_BINARY_LENGTH) {'
868 print r' *params = 0;'
869 print r' }'
870 if function.name in ('glGetProgramBinary', 'glGetProgramBinaryOES'):
871 print r' if (length) {'
872 print r' *length = 0;'
873 print r' }'
874
José Fonseca867b1b72011-04-24 11:58:04 +0100875 buffer_targets = [
876 'ARRAY_BUFFER',
877 'ELEMENT_ARRAY_BUFFER',
878 'PIXEL_PACK_BUFFER',
879 'PIXEL_UNPACK_BUFFER',
José Fonseca7b20d672012-01-10 19:13:58 +0000880 'UNIFORM_BUFFER',
881 'TEXTURE_BUFFER',
882 'TRANSFORM_FEEDBACK_BUFFER',
883 'COPY_READ_BUFFER',
884 'COPY_WRITE_BUFFER',
885 'DRAW_INDIRECT_BUFFER',
886 'ATOMIC_COUNTER_BUFFER',
José Fonseca867b1b72011-04-24 11:58:04 +0100887 ]
888
José Fonseca54f304a2012-01-14 19:33:08 +0000889 def wrapRet(self, function, instance):
890 Tracer.wrapRet(self, function, instance)
José Fonseca1b3d3752011-07-15 10:15:19 +0100891
José Fonsecacdc322c2012-02-29 19:29:51 +0000892 # Keep track of buffer mappings
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000893 if function.name in ('glMapBufferRange', 'glMapBufferRangeEXT'):
José Fonseca9c536b02012-02-29 20:54:13 +0000894 print ' if (access & GL_MAP_WRITE_BIT) {'
José Fonseca632a78d2012-04-19 07:18:59 +0100895 print ' _checkBufferMapRange = true;'
José Fonseca9c536b02012-02-29 20:54:13 +0000896 print ' }'
José Fonseca669b1222011-02-20 09:05:10 +0000897
José Fonsecac9f12232011-03-25 20:07:42 +0000898 boolean_names = [
899 'GL_FALSE',
900 'GL_TRUE',
901 ]
902
903 def gl_boolean(self, value):
904 return self.boolean_names[int(bool(value))]
905
José Fonsecaa442a462014-11-12 21:16:22 +0000906 # Regular expression for the names of the functions that unpack from a
907 # pixel buffer object. See the ARB_pixel_buffer_object specification.
908 unpack_function_regex = re.compile(r'^gl(' + r'|'.join([
909 r'Bitmap',
910 r'PolygonStipple',
911 r'PixelMap[a-z]+v',
912 r'DrawPixels',
913 r'Color(Sub)?Table',
914 r'(Convolution|Separable)Filter[12]D',
915 r'(Compressed)?(Multi)?Tex(ture)?(Sub)?Image[1-4]D',
916 ]) + r')[0-9A-Z]*$')
José Fonsecae97bab92011-06-02 23:15:11 +0100917
José Fonseca54f304a2012-01-14 19:33:08 +0000918 def serializeArgValue(self, function, arg):
José Fonsecae97bab92011-06-02 23:15:11 +0100919 # Recognize offsets instead of blobs when a PBO is bound
José Fonsecaa442a462014-11-12 21:16:22 +0000920 if self.unpack_function_regex.match(function.name) \
José Fonsecae97bab92011-06-02 23:15:11 +0100921 and (isinstance(arg.type, stdapi.Blob) \
922 or (isinstance(arg.type, stdapi.Const) \
923 and isinstance(arg.type.type, stdapi.Blob))):
José Fonsecac29f4f12011-06-11 12:19:05 +0100924 print ' {'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000925 print ' gltrace::Context *ctx = gltrace::getContext();'
José Fonseca632a78d2012-04-19 07:18:59 +0100926 print ' GLint _unpack_buffer = 0;'
José Fonseca34ea6162015-01-08 23:45:43 +0000927 print ' if (ctx->profile.desktop())'
José Fonseca632a78d2012-04-19 07:18:59 +0100928 print ' _glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &_unpack_buffer);'
929 print ' if (_unpack_buffer) {'
José Fonsecad559f022012-04-15 16:13:51 +0100930 print ' trace::localWriter.writePointer((uintptr_t)%s);' % arg.name
José Fonsecac29f4f12011-06-11 12:19:05 +0100931 print ' } else {'
José Fonseca54f304a2012-01-14 19:33:08 +0000932 Tracer.serializeArgValue(self, function, arg)
José Fonsecac29f4f12011-06-11 12:19:05 +0100933 print ' }'
José Fonsecae97bab92011-06-02 23:15:11 +0100934 print ' }'
935 return
936
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100937 # Several GL state functions take GLenum symbolic names as
938 # integer/floats; so dump the symbolic name whenever possible
José Fonseca3bcb33c2011-05-27 20:14:31 +0100939 if function.name.startswith('gl') \
José Fonsecae3571092011-10-13 08:26:27 +0100940 and arg.type in (glapi.GLint, glapi.GLfloat, glapi.GLdouble) \
José Fonseca3bcb33c2011-05-27 20:14:31 +0100941 and arg.name == 'param':
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100942 assert arg.index > 0
943 assert function.args[arg.index - 1].name == 'pname'
944 assert function.args[arg.index - 1].type == glapi.GLenum
945 print ' if (is_symbolic_pname(pname) && is_symbolic_param(%s)) {' % arg.name
José Fonseca54f304a2012-01-14 19:33:08 +0000946 self.serializeValue(glapi.GLenum, arg.name)
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100947 print ' } else {'
José Fonseca54f304a2012-01-14 19:33:08 +0000948 Tracer.serializeArgValue(self, function, arg)
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100949 print ' }'
950 return
951
José Fonseca54f304a2012-01-14 19:33:08 +0000952 Tracer.serializeArgValue(self, function, arg)
José Fonseca99221832011-03-22 22:15:46 +0000953
José Fonseca4c938c22011-04-30 22:44:38 +0100954 def footer(self, api):
955 Tracer.footer(self, api)
José Fonseca669b1222011-02-20 09:05:10 +0000956
José Fonseca4c938c22011-04-30 22:44:38 +0100957 # A simple state tracker to track the pointer values
José Fonseca669b1222011-02-20 09:05:10 +0000958 # update the state
José Fonseca14cb9ef2012-05-17 21:33:14 +0100959 print 'static void _trace_user_arrays(GLuint count)'
José Fonseca669b1222011-02-20 09:05:10 +0000960 print '{'
José Fonsecaf028a8f2012-02-15 23:33:35 +0000961 print ' gltrace::Context *ctx = gltrace::getContext();'
José Fonseca8d1408b2014-02-03 19:57:18 +0000962 print
José Fonsecab0c59722015-01-05 20:45:41 +0000963 print ' glprofile::Profile profile = ctx->profile;'
964 print ' bool es1 = profile.es() && profile.major == 1;'
965 print
José Fonseca8d1408b2014-02-03 19:57:18 +0000966
967 # Temporarily unbind the array buffer
José Fonseca26be8f92014-03-07 14:08:50 +0000968 print ' GLint _array_buffer = _glGetInteger(GL_ARRAY_BUFFER_BINDING);'
José Fonseca8d1408b2014-02-03 19:57:18 +0000969 print ' if (_array_buffer) {'
970 self.fake_glBindBuffer(api, 'GL_ARRAY_BUFFER', '0')
971 print ' }'
972 print
José Fonseca1a2fdd22011-04-01 00:55:09 +0100973
José Fonseca99221832011-03-22 22:15:46 +0000974 for camelcase_name, uppercase_name in self.arrays:
Chia-I Wub3d218d2011-11-03 01:37:36 +0800975 # in which profile is the array available?
José Fonseca34ea6162015-01-08 23:45:43 +0000976 profile_check = 'profile.desktop()'
Chia-I Wub3d218d2011-11-03 01:37:36 +0800977 if camelcase_name in self.arrays_es1:
José Fonsecab0c59722015-01-05 20:45:41 +0000978 profile_check = '(' + profile_check + ' || es1)';
Chia-I Wub3d218d2011-11-03 01:37:36 +0800979
José Fonseca99221832011-03-22 22:15:46 +0000980 function_name = 'gl%sPointer' % camelcase_name
981 enable_name = 'GL_%s_ARRAY' % uppercase_name
982 binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name
José Fonseca1b6c8752012-04-15 14:33:00 +0100983 function = api.getFunctionByName(function_name)
José Fonseca99221832011-03-22 22:15:46 +0000984
José Fonseca06e85192011-10-16 14:15:36 +0100985 print ' // %s' % function.prototype()
Chia-I Wub3d218d2011-11-03 01:37:36 +0800986 print ' if (%s) {' % profile_check
José Fonsecafb6744f2011-04-15 11:18:37 +0100987 self.array_trace_prolog(api, uppercase_name)
988 self.array_prolog(api, uppercase_name)
José Fonseca632a78d2012-04-19 07:18:59 +0100989 print ' if (_glIsEnabled(%s)) {' % enable_name
José Fonseca26be8f92014-03-07 14:08:50 +0000990 print ' GLint _binding = _glGetInteger(%s);' % binding_name
José Fonseca632a78d2012-04-19 07:18:59 +0100991 print ' if (!_binding) {'
José Fonseca99221832011-03-22 22:15:46 +0000992
993 # Get the arguments via glGet*
994 for arg in function.args:
995 arg_get_enum = 'GL_%s_ARRAY_%s' % (uppercase_name, arg.name.upper())
996 arg_get_function, arg_type = TypeGetter().visit(arg.type)
José Fonseca7f5163e2011-03-31 23:37:26 +0100997 print ' %s %s = 0;' % (arg_type, arg.name)
José Fonseca632a78d2012-04-19 07:18:59 +0100998 print ' _%s(%s, &%s);' % (arg_get_function, arg_get_enum, arg.name)
José Fonseca99221832011-03-22 22:15:46 +0000999
1000 arg_names = ', '.join([arg.name for arg in function.args[:-1]])
José Fonseca14cb9ef2012-05-17 21:33:14 +01001001 print ' size_t _size = _%s_size(%s, count);' % (function.name, arg_names)
José Fonseca99221832011-03-22 22:15:46 +00001002
1003 # Emit a fake function
José Fonsecafb6744f2011-04-15 11:18:37 +01001004 self.array_trace_intermezzo(api, uppercase_name)
José Fonseca7a5f23a2014-06-24 19:20:36 +01001005 print ' unsigned _call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,)
José Fonseca669b1222011-02-20 09:05:10 +00001006 for arg in function.args:
1007 assert not arg.output
José Fonsecab4a3d142011-10-27 07:43:19 +01001008 print ' trace::localWriter.beginArg(%u);' % (arg.index,)
José Fonseca14c21bc2011-02-20 23:32:22 +00001009 if arg.name != 'pointer':
José Fonseca54f304a2012-01-14 19:33:08 +00001010 self.serializeValue(arg.type, arg.name)
José Fonseca14c21bc2011-02-20 23:32:22 +00001011 else:
José Fonseca632a78d2012-04-19 07:18:59 +01001012 print ' trace::localWriter.writeBlob((const void *)%s, _size);' % (arg.name)
José Fonsecab4a3d142011-10-27 07:43:19 +01001013 print ' trace::localWriter.endArg();'
José Fonseca99221832011-03-22 22:15:46 +00001014
José Fonsecab4a3d142011-10-27 07:43:19 +01001015 print ' trace::localWriter.endEnter();'
José Fonseca632a78d2012-04-19 07:18:59 +01001016 print ' trace::localWriter.beginLeave(_call);'
José Fonsecab4a3d142011-10-27 07:43:19 +01001017 print ' trace::localWriter.endLeave();'
José Fonseca99221832011-03-22 22:15:46 +00001018 print ' }'
José Fonseca669b1222011-02-20 09:05:10 +00001019 print ' }'
José Fonsecafb6744f2011-04-15 11:18:37 +01001020 self.array_epilog(api, uppercase_name)
1021 self.array_trace_epilog(api, uppercase_name)
Chia-I Wub3d218d2011-11-03 01:37:36 +08001022 print ' }'
José Fonseca99221832011-03-22 22:15:46 +00001023 print
José Fonseca1a2fdd22011-04-01 00:55:09 +01001024
José Fonseca1601c412011-05-10 10:38:19 +01001025 # Samething, but for glVertexAttribPointer*
1026 #
1027 # Some variants of glVertexAttribPointer alias conventional and generic attributes:
1028 # - glVertexAttribPointer: no
1029 # - glVertexAttribPointerARB: implementation dependent
1030 # - glVertexAttribPointerNV: yes
1031 #
1032 # This means that the implementations of these functions do not always
1033 # alias, and they need to be considered independently.
1034 #
Chia-I Wub3d218d2011-11-03 01:37:36 +08001035 print ' // ES1 does not support generic vertex attributes'
José Fonsecab0c59722015-01-05 20:45:41 +00001036 print ' if (es1)'
Chia-I Wub3d218d2011-11-03 01:37:36 +08001037 print ' return;'
1038 print
José Fonseca632a78d2012-04-19 07:18:59 +01001039 print ' vertex_attrib _vertex_attrib = _get_vertex_attrib();'
José Fonseca5a568a92011-06-29 16:43:36 +01001040 print
José Fonseca74b661a2014-07-17 19:10:24 +01001041 for suffix in ['', 'NV']:
José Fonseca5a568a92011-06-29 16:43:36 +01001042 if suffix:
José Fonsecad94aaac2011-06-28 20:50:49 +01001043 SUFFIX = '_' + suffix
José Fonsecad94aaac2011-06-28 20:50:49 +01001044 else:
José Fonseca5a568a92011-06-29 16:43:36 +01001045 SUFFIX = suffix
José Fonseca1601c412011-05-10 10:38:19 +01001046 function_name = 'glVertexAttribPointer' + suffix
José Fonseca1b6c8752012-04-15 14:33:00 +01001047 function = api.getFunctionByName(function_name)
José Fonseca06e85192011-10-16 14:15:36 +01001048
1049 print ' // %s' % function.prototype()
José Fonseca632a78d2012-04-19 07:18:59 +01001050 print ' if (_vertex_attrib == VERTEX_ATTRIB%s) {' % SUFFIX
José Fonseca5a568a92011-06-29 16:43:36 +01001051 if suffix == 'NV':
José Fonseca632a78d2012-04-19 07:18:59 +01001052 print ' GLint _max_vertex_attribs = 16;'
José Fonseca5a568a92011-06-29 16:43:36 +01001053 else:
José Fonseca26be8f92014-03-07 14:08:50 +00001054 print ' GLint _max_vertex_attribs = _glGetInteger(GL_MAX_VERTEX_ATTRIBS);'
José Fonseca632a78d2012-04-19 07:18:59 +01001055 print ' for (GLint index = 0; index < _max_vertex_attribs; ++index) {'
1056 print ' GLint _enabled = 0;'
José Fonseca5a568a92011-06-29 16:43:36 +01001057 if suffix == 'NV':
José Fonseca632a78d2012-04-19 07:18:59 +01001058 print ' _glGetIntegerv(GL_VERTEX_ATTRIB_ARRAY0_NV + index, &_enabled);'
José Fonseca5a568a92011-06-29 16:43:36 +01001059 else:
José Fonseca632a78d2012-04-19 07:18:59 +01001060 print ' _glGetVertexAttribiv%s(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED%s, &_enabled);' % (suffix, SUFFIX)
1061 print ' if (_enabled) {'
1062 print ' GLint _binding = 0;'
José Fonseca5a568a92011-06-29 16:43:36 +01001063 if suffix != 'NV':
1064 # It doesn't seem possible to use VBOs with NV_vertex_program.
José Fonseca632a78d2012-04-19 07:18:59 +01001065 print ' _glGetVertexAttribiv%s(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING%s, &_binding);' % (suffix, SUFFIX)
1066 print ' if (!_binding) {'
José Fonseca1a2fdd22011-04-01 00:55:09 +01001067
José Fonseca1601c412011-05-10 10:38:19 +01001068 # Get the arguments via glGet*
1069 for arg in function.args[1:]:
José Fonseca5a568a92011-06-29 16:43:36 +01001070 if suffix == 'NV':
1071 arg_get_enum = 'GL_ATTRIB_ARRAY_%s%s' % (arg.name.upper(), SUFFIX)
1072 else:
1073 arg_get_enum = 'GL_VERTEX_ATTRIB_ARRAY_%s%s' % (arg.name.upper(), SUFFIX)
José Fonsecac493e3e2011-06-29 12:57:06 +01001074 arg_get_function, arg_type = TypeGetter('glGetVertexAttrib', False, suffix).visit(arg.type)
José Fonseca5a568a92011-06-29 16:43:36 +01001075 print ' %s %s = 0;' % (arg_type, arg.name)
José Fonseca632a78d2012-04-19 07:18:59 +01001076 print ' _%s(index, %s, &%s);' % (arg_get_function, arg_get_enum, arg.name)
José Fonseca1601c412011-05-10 10:38:19 +01001077
1078 arg_names = ', '.join([arg.name for arg in function.args[1:-1]])
José Fonseca14cb9ef2012-05-17 21:33:14 +01001079 print ' size_t _size = _%s_size(%s, count);' % (function.name, arg_names)
José Fonseca1a2fdd22011-04-01 00:55:09 +01001080
José Fonseca1601c412011-05-10 10:38:19 +01001081 # Emit a fake function
José Fonseca7a5f23a2014-06-24 19:20:36 +01001082 print ' unsigned _call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,)
José Fonseca1601c412011-05-10 10:38:19 +01001083 for arg in function.args:
1084 assert not arg.output
José Fonsecab4a3d142011-10-27 07:43:19 +01001085 print ' trace::localWriter.beginArg(%u);' % (arg.index,)
José Fonseca1601c412011-05-10 10:38:19 +01001086 if arg.name != 'pointer':
José Fonseca54f304a2012-01-14 19:33:08 +00001087 self.serializeValue(arg.type, arg.name)
José Fonseca1601c412011-05-10 10:38:19 +01001088 else:
José Fonseca632a78d2012-04-19 07:18:59 +01001089 print ' trace::localWriter.writeBlob((const void *)%s, _size);' % (arg.name)
José Fonsecab4a3d142011-10-27 07:43:19 +01001090 print ' trace::localWriter.endArg();'
José Fonseca1601c412011-05-10 10:38:19 +01001091
José Fonsecab4a3d142011-10-27 07:43:19 +01001092 print ' trace::localWriter.endEnter();'
José Fonseca632a78d2012-04-19 07:18:59 +01001093 print ' trace::localWriter.beginLeave(_call);'
José Fonsecab4a3d142011-10-27 07:43:19 +01001094 print ' trace::localWriter.endLeave();'
José Fonseca1601c412011-05-10 10:38:19 +01001095 print ' }'
1096 print ' }'
1097 print ' }'
1098 print ' }'
1099 print
José Fonseca1a2fdd22011-04-01 00:55:09 +01001100
José Fonseca8d1408b2014-02-03 19:57:18 +00001101 # Restore the original array_buffer
1102 print ' if (_array_buffer) {'
1103 self.fake_glBindBuffer(api, 'GL_ARRAY_BUFFER', '_array_buffer')
1104 print ' }'
1105 print
1106
José Fonseca669b1222011-02-20 09:05:10 +00001107 print '}'
1108 print
1109
José Fonsecafb6744f2011-04-15 11:18:37 +01001110 #
1111 # Hooks for glTexCoordPointer, which is identical to the other array
1112 # pointers except the fact that it is indexed by glClientActiveTexture.
1113 #
1114
1115 def array_prolog(self, api, uppercase_name):
1116 if uppercase_name == 'TEXTURE_COORD':
Jose Fonseca6ac76c12015-06-26 10:27:30 +01001117 print ' GLint max_units = 0;'
José Fonseca34ea6162015-01-08 23:45:43 +00001118 print ' if (ctx->profile.desktop())'
Jose Fonseca6ac76c12015-06-26 10:27:30 +01001119 print ' _glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_units);'
Chia-I Wub3d218d2011-11-03 01:37:36 +08001120 print ' else'
Jose Fonseca6ac76c12015-06-26 10:27:30 +01001121 print ' _glGetIntegerv(GL_MAX_TEXTURE_UNITS, &max_units);'
1122 print ' GLint client_active_texture = GL_TEXTURE0;'
1123 print ' if (max_units > 0) {'
1124 print ' _glGetIntegerv(GL_CLIENT_ACTIVE_TEXTURE, &client_active_texture);'
1125 print ' }'
1126 print ' GLint unit = 0;'
1127 print ' do {'
José Fonseca7525e6f2011-09-28 09:04:56 +01001128 print ' GLint texture = GL_TEXTURE0 + unit;'
Jose Fonseca6ac76c12015-06-26 10:27:30 +01001129 print ' if (max_units > 0) {'
1130 print ' _glClientActiveTexture(texture);'
1131 print ' }'
José Fonsecafb6744f2011-04-15 11:18:37 +01001132
1133 def array_trace_prolog(self, api, uppercase_name):
1134 if uppercase_name == 'TEXTURE_COORD':
1135 print ' bool client_active_texture_dirty = false;'
1136
1137 def array_epilog(self, api, uppercase_name):
1138 if uppercase_name == 'TEXTURE_COORD':
Jose Fonseca6ac76c12015-06-26 10:27:30 +01001139 print ' } while (++unit < max_units);'
José Fonsecafb6744f2011-04-15 11:18:37 +01001140 self.array_cleanup(api, uppercase_name)
1141
1142 def array_cleanup(self, api, uppercase_name):
1143 if uppercase_name == 'TEXTURE_COORD':
Jose Fonseca6ac76c12015-06-26 10:27:30 +01001144 print ' if (max_units > 0) {'
1145 print ' _glClientActiveTexture(client_active_texture);'
1146 print ' }'
José Fonsecafb6744f2011-04-15 11:18:37 +01001147
1148 def array_trace_intermezzo(self, api, uppercase_name):
1149 if uppercase_name == 'TEXTURE_COORD':
1150 print ' if (texture != client_active_texture || client_active_texture_dirty) {'
1151 print ' client_active_texture_dirty = true;'
1152 self.fake_glClientActiveTexture_call(api, "texture");
1153 print ' }'
1154
1155 def array_trace_epilog(self, api, uppercase_name):
1156 if uppercase_name == 'TEXTURE_COORD':
1157 print ' if (client_active_texture_dirty) {'
1158 self.fake_glClientActiveTexture_call(api, "client_active_texture");
1159 print ' }'
1160
José Fonseca8d1408b2014-02-03 19:57:18 +00001161 def fake_glBindBuffer(self, api, target, buffer):
1162 function = api.getFunctionByName('glBindBuffer')
1163 self.fake_call(function, [target, buffer])
1164
José Fonsecafb6744f2011-04-15 11:18:37 +01001165 def fake_glClientActiveTexture_call(self, api, texture):
José Fonseca1b6c8752012-04-15 14:33:00 +01001166 function = api.getFunctionByName('glClientActiveTexture')
José Fonsecafb6744f2011-04-15 11:18:37 +01001167 self.fake_call(function, [texture])
1168
José Fonseca151c3702013-05-10 08:28:15 +01001169 def emitFakeTexture2D(self):
1170 function = glapi.glapi.getFunctionByName('glTexImage2D')
1171 instances = function.argNames()
José Fonseca7a5f23a2014-06-24 19:20:36 +01001172 print ' unsigned _fake_call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,)
José Fonseca151c3702013-05-10 08:28:15 +01001173 for arg in function.args:
1174 assert not arg.output
1175 self.serializeArg(function, arg)
1176 print ' trace::localWriter.endEnter();'
1177 print ' trace::localWriter.beginLeave(_fake_call);'
1178 print ' trace::localWriter.endLeave();'
José Fonsecafb6744f2011-04-15 11:18:37 +01001179
1180
1181
1182
1183
José Fonseca669b1222011-02-20 09:05:10 +00001184
1185
1186
1187
1188
1189