blob: 760ba077a62997208323ac8ed2e56ea63eef59d2 [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:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010082 print(alias.expr)
José Fonseca99221832011-03-22 22:15:46 +000083 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
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300114 buffer_targets = [
115 "GL_ARRAY_BUFFER",
116 "GL_ATOMIC_COUNTER_BUFFER",
117 "GL_COPY_READ_BUFFER",
118 "GL_COPY_WRITE_BUFFER",
119 "GL_DRAW_INDIRECT_BUFFER",
120 "GL_DISPATCH_INDIRECT_BUFFER",
121 "GL_ELEMENT_ARRAY_BUFFER",
122 "GL_PIXEL_PACK_BUFFER",
123 "GL_PIXEL_UNPACK_BUFFER",
124 "GL_QUERY_BUFFER",
125 "GL_SHADER_STORAGE_BUFFER",
126 "GL_TEXTURE_BUFFER",
127 "GL_TRANSFORM_FEEDBACK_BUFFER",
128 "GL_UNIFORM_BUFFER",
129 ]
130
131 # Names of the functions that can pack into the current pixel buffer
132 # object. See also the ARB_pixel_buffer_object specification.
133 pack_function_regex = re.compile(r'^gl(' + r'|'.join([
134 r'Getn?Histogram',
135 r'Getn?PolygonStipple',
136 r'Getn?PixelMap[a-z]+v',
137 r'Getn?Minmax',
138 r'Getn?(Convolution|Separable)Filter',
139 r'Getn?(Compressed)?(Multi)?Tex(ture)?(Sub)?Image',
140 r'Readn?Pixels',
141 ]) + r')[0-9A-Z]*$')
142
José Fonseca4c938c22011-04-30 22:44:38 +0100143 def header(self, api):
144 Tracer.header(self, api)
145
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100146 print('#include <algorithm>')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300147 print('#include "cxx_compat.hpp"')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100148 print()
149 print('#include "gltrace.hpp"')
150 print('#include "gltrace_arrays.hpp"')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300151 print('#include "glmemshadow.hpp"')
werman29f6af32020-01-31 01:23:38 +0200152 print('#include "gltrace_unpack_compressed.hpp"')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100153 print()
José Fonseca5a568a92011-06-29 16:43:36 +0100154
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000155 # Whether we need user arrays
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100156 print('static inline bool _need_user_arrays(gltrace::Context *_ctx)')
157 print('{')
158 print(' if (!_ctx->user_arrays) {')
159 print(' return false;')
160 print(' }')
161 print()
162 print(' glfeatures::Profile profile = _ctx->profile;')
163 print(' bool es1 = profile.es() && profile.major == 1;')
164 print()
José Fonseca1a2fdd22011-04-01 00:55:09 +0100165
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000166 for camelcase_name, uppercase_name in self.arrays:
Chia-I Wub3d218d2011-11-03 01:37:36 +0800167 # in which profile is the array available?
José Fonseca34ea6162015-01-08 23:45:43 +0000168 profile_check = 'profile.desktop()'
Chia-I Wub3d218d2011-11-03 01:37:36 +0800169 if camelcase_name in self.arrays_es1:
José Fonsecab0c59722015-01-05 20:45:41 +0000170 profile_check = '(' + profile_check + ' || es1)';
Chia-I Wub3d218d2011-11-03 01:37:36 +0800171
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000172 function_name = 'gl%sPointer' % camelcase_name
173 enable_name = 'GL_%s_ARRAY' % uppercase_name
174 binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100175 print(' // %s' % function_name)
176 print(' if (%s) {' % profile_check)
José Fonsecafb6744f2011-04-15 11:18:37 +0100177 self.array_prolog(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100178 print(' if (_glIsEnabled(%s) &&' % enable_name)
179 print(' _glGetInteger(%s) == 0) {' % binding_name)
José Fonsecafb6744f2011-04-15 11:18:37 +0100180 self.array_cleanup(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100181 print(' return true;')
182 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +0100183 self.array_epilog(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100184 print(' }')
185 print()
José Fonseca7f5163e2011-03-31 23:37:26 +0100186
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100187 print(' // ES1 does not support generic vertex attributes')
188 print(' if (es1)')
189 print(' return false;')
190 print()
191 print(' // glVertexAttribPointer')
192 print(' GLint _max_vertex_attribs = _glGetInteger(GL_MAX_VERTEX_ATTRIBS);')
193 print(' for (GLint index = 0; index < _max_vertex_attribs; ++index) {')
194 print(' if (_glGetVertexAttribi(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED) &&')
195 print(' _glGetVertexAttribi(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING) == 0) {')
196 print(' return true;')
197 print(' }')
198 print(' }')
199 print()
José Fonseca1a2fdd22011-04-01 00:55:09 +0100200
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100201 print(' return false;')
202 print('}')
203 print()
José Fonseca669b1222011-02-20 09:05:10 +0000204
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100205 print(r'static void _trace_user_arrays(gltrace::Context *_ctx, GLuint count);')
206 print()
Jose Fonseca6ef9b3c2016-01-27 23:18:52 +0000207
Jose Fonseca27b8d812016-05-13 07:09:09 -0700208 # Declare helper functions to emit fake function calls into the trace
209 for function in api.getAllFunctions():
210 if function.name in self.fake_function_names:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100211 print(function.prototype('_fake_' + function.name) + ';')
212 print()
213 print(r'static inline void')
214 print(r'_fakeStringMarker(const std::string &s) {')
215 print(r' _fake_glStringMarkerGREMEDY(s.length(), s.data());')
216 print(r'}')
217 print()
José Fonseca867b1b72011-04-24 11:58:04 +0100218
José Fonseca9c536b02012-02-29 20:54:13 +0000219 # Buffer mappings
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100220 print('// whether glMapBufferRange(GL_MAP_WRITE_BIT) has ever been called')
221 print('static bool _checkBufferMapRange = false;')
222 print()
223 print('// whether glBufferParameteriAPPLE(GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE) has ever been called')
224 print('static bool _checkBufferFlushingUnmapAPPLE = false;')
225 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
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100229 print('static bool')
230 print('is_symbolic_pname(GLenum pname) {')
231 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:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100234 print(' case %s:' % name)
235 print(' return true;')
236 print(' default:')
237 print(' return false;')
238 print(' }')
239 print('}')
240 print()
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100241
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
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100245 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
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100253 print('static size_t')
254 print('_gl_param_size(GLenum pname) {')
255 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:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100260 print(' case %s: return %s;' % (name, count))
261 print(' default:')
262 print(r' os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, pname);')
263 print(' return 1;')
264 print(' }')
265 print('}')
266 print()
José Fonseca4c938c22011-04-30 22:44:38 +0100267
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300268 # Generate a helper function to get buffer binding
269 print('static GLenum')
270 print('getBufferBinding(GLenum target) {')
271 print(' switch (target) {')
272 for target in self.buffer_targets:
273 print(' case %s:' % target)
274 print(' return %s_BINDING;' % target)
275 print(' default:')
276 print(' assert(false);')
277 print(' return 0;')
278 print(' }')
279 print('}')
280 print()
281
282 print('static GLint')
283 print('getBufferName(GLenum target) {')
284 print(' GLint bufferName = 0;')
285 print(' _glGetIntegerv(getBufferBinding(target), &bufferName);')
286 print(' assert(bufferName != 0);')
287 print(' return bufferName;')
288 print('}')
289 print()
290
Chia-I Wu335efb42011-11-03 01:59:22 +0800291 # states such as GL_UNPACK_ROW_LENGTH are not available in GLES
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100292 print('static inline bool')
293 print('can_unpack_subimage(void) {')
294 print(' gltrace::Context *_ctx = gltrace::getContext();')
Jose Fonseca487e9c52019-06-17 10:36:16 +0100295 print(' return _ctx->features.unpack_subimage;')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100296 print('}')
297 print()
Chia-I Wu335efb42011-11-03 01:59:22 +0800298
José Fonseca631dbd12014-12-15 16:34:45 +0000299 # VMWX_map_buffer_debug
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100300 print(r'extern "C" PUBLIC')
301 print(r'void APIENTRY')
302 print(r'glNotifyMappedBufferRangeVMWX(const void * start, GLsizeiptr length) {')
José Fonseca631dbd12014-12-15 16:34:45 +0000303 self.emit_memcpy('start', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100304 print(r'}')
305 print()
José Fonseca631dbd12014-12-15 16:34:45 +0000306
José Fonseca1b6c8752012-04-15 14:33:00 +0100307 getProcAddressFunctionNames = []
308
309 def traceApi(self, api):
310 if self.getProcAddressFunctionNames:
311 # Generate a function to wrap proc addresses
312 getProcAddressFunction = api.getFunctionByName(self.getProcAddressFunctionNames[0])
313 argType = getProcAddressFunction.args[0].type
314 retType = getProcAddressFunction.type
315
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100316 print('static %s _wrapProcAddress(%s procName, %s procPtr);' % (retType, argType, retType))
317 print()
José Fonseca1b6c8752012-04-15 14:33:00 +0100318
319 Tracer.traceApi(self, api)
320
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100321 print('static %s _wrapProcAddress(%s procName, %s procPtr) {' % (retType, argType, retType))
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700322
323 # Provide fallback functions to missing debug functions
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100324 print(' if (!procPtr) {')
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700325 else_ = ''
326 for function_name in self.debug_functions:
327 if self.api.getFunctionByName(function_name):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100328 print(' %sif (strcmp("%s", (const char *)procName) == 0) {' % (else_, function_name))
329 print(' return (%s)&%s;' % (retType, function_name))
330 print(' }')
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700331 else_ = 'else '
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100332 print(' %s{' % else_)
333 print(' return NULL;')
334 print(' }')
335 print(' }')
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700336
José Fonseca81301932012-11-11 00:10:20 +0000337 for function in api.getAllFunctions():
José Fonseca1b6c8752012-04-15 14:33:00 +0100338 ptype = function_pointer_type(function)
339 pvalue = function_pointer_value(function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100340 print(' if (strcmp("%s", (const char *)procName) == 0) {' % function.name)
341 print(' assert(procPtr != (%s)&%s);' % (retType, function.name))
342 print(' %s = (%s)procPtr;' % (pvalue, ptype))
343 print(' return (%s)&%s;' % (retType, function.name,))
344 print(' }')
345 print(' os::log("apitrace: warning: unknown function \\"%s\\"\\n", (const char *)procName);')
346 print(' return procPtr;')
347 print('}')
348 print()
José Fonseca1b6c8752012-04-15 14:33:00 +0100349 else:
350 Tracer.traceApi(self, api)
351
José Fonseca99221832011-03-22 22:15:46 +0000352 array_pointer_function_names = set((
353 "glVertexPointer",
354 "glNormalPointer",
355 "glColorPointer",
356 "glIndexPointer",
357 "glTexCoordPointer",
358 "glEdgeFlagPointer",
359 "glFogCoordPointer",
360 "glSecondaryColorPointer",
José Fonseca7f5163e2011-03-31 23:37:26 +0100361
José Fonsecaac5285b2011-05-04 11:09:08 +0100362 "glInterleavedArrays",
363
José Fonseca7e0bfd92011-04-30 23:09:03 +0100364 "glVertexPointerEXT",
365 "glNormalPointerEXT",
366 "glColorPointerEXT",
367 "glIndexPointerEXT",
368 "glTexCoordPointerEXT",
369 "glEdgeFlagPointerEXT",
370 "glFogCoordPointerEXT",
371 "glSecondaryColorPointerEXT",
José Fonseca99221832011-03-22 22:15:46 +0000372
José Fonseca7f5163e2011-03-31 23:37:26 +0100373 "glVertexAttribPointer",
374 "glVertexAttribPointerARB",
375 "glVertexAttribPointerNV",
José Fonsecaac5285b2011-05-04 11:09:08 +0100376 "glVertexAttribIPointer",
377 "glVertexAttribIPointerEXT",
José Fonseca7f5163e2011-03-31 23:37:26 +0100378 "glVertexAttribLPointer",
379 "glVertexAttribLPointerEXT",
José Fonseca99221832011-03-22 22:15:46 +0000380
381 #"glMatrixIndexPointerARB",
382 ))
383
José Fonsecac5bf77a2014-08-14 16:10:02 +0100384 # XXX: We currently ignore the gl*Draw*ElementArray* functions
Jose Fonsecadddc5b82016-05-08 22:33:44 +0100385 draw_function_regex = re.compile(r'^(?P<radical>gl([A-Z][a-z]+)*Draw(Range)?(Arrays|Elements))(?P<suffix>[A-Z][a-zA-Z]*)?$' )
José Fonseca99221832011-03-22 22:15:46 +0000386
José Fonsecac9f12232011-03-25 20:07:42 +0000387 interleaved_formats = [
388 'GL_V2F',
389 'GL_V3F',
390 'GL_C4UB_V2F',
391 'GL_C4UB_V3F',
392 'GL_C3F_V3F',
393 'GL_N3F_V3F',
394 'GL_C4F_N3F_V3F',
395 'GL_T2F_V3F',
396 'GL_T4F_V4F',
397 'GL_T2F_C4UB_V3F',
398 'GL_T2F_C3F_V3F',
399 'GL_T2F_N3F_V3F',
400 'GL_T2F_C4F_N3F_V3F',
401 'GL_T4F_C4F_N3F_V4F',
402 ]
403
José Fonseca54f304a2012-01-14 19:33:08 +0000404 def traceFunctionImplBody(self, function):
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000405 # Defer tracing of user array pointers...
José Fonseca99221832011-03-22 22:15:46 +0000406 if function.name in self.array_pointer_function_names:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100407 print(' GLint _array_buffer = _glGetInteger(GL_ARRAY_BUFFER_BINDING);')
408 print(' if (!_array_buffer) {')
409 print(' static bool warned = false;')
410 print(' if (!warned) {')
411 print(' warned = true;')
412 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__);')
413 print(' }')
414 print(' gltrace::Context *_ctx = gltrace::getContext();')
415 print(' _ctx->user_arrays = true;')
José Fonseca5a568a92011-06-29 16:43:36 +0100416 if function.name == "glVertexAttribPointerNV":
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100417 print(r' os::log("apitrace: warning: %s: user memory arrays with NV_vertex_program longer supported\n", __FUNCTION__);')
José Fonseca54f304a2012-01-14 19:33:08 +0000418 self.invokeFunction(function)
José Fonsecaac5285b2011-05-04 11:09:08 +0100419
420 # And also break down glInterleavedArrays into the individual calls
421 if function.name == 'glInterleavedArrays':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100422 print()
José Fonsecaac5285b2011-05-04 11:09:08 +0100423
424 # Initialize the enable flags
425 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100426 flag_name = '_' + uppercase_name.lower()
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100427 print(' GLboolean %s = GL_FALSE;' % flag_name)
428 print()
José Fonsecaac5285b2011-05-04 11:09:08 +0100429
430 # Switch for the interleaved formats
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100431 print(' switch (format) {')
José Fonsecaac5285b2011-05-04 11:09:08 +0100432 for format in self.interleaved_formats:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100433 print(' case %s:' % format)
José Fonsecaac5285b2011-05-04 11:09:08 +0100434 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100435 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100436 if format.find('_' + uppercase_name[0]) >= 0:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100437 print(' %s = GL_TRUE;' % flag_name)
438 print(' break;')
439 print(' default:')
440 print(' return;')
441 print(' }')
442 print()
José Fonsecaac5285b2011-05-04 11:09:08 +0100443
444 # Emit fake glEnableClientState/glDisableClientState 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 enable_name = 'GL_%s_ARRAY' % uppercase_name
448
449 # Emit a fake function
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100450 print(' if (%s) {' % flag_name)
451 print(' _fake_glEnableClientState(%s);' % enable_name)
452 print(' } else {')
453 print(' _fake_glDisableClientState(%s);' % enable_name)
454 print(' }')
José Fonsecaac5285b2011-05-04 11:09:08 +0100455
José Fonsecac629a8c2014-06-01 21:12:27 +0100456 # Warn about buggy glGet(GL_*ARRAY_SIZE) not returning GL_BGRA
457 buggyFunctions = {
458 'glColorPointer': ('glGetIntegerv', '', 'GL_COLOR_ARRAY_SIZE'),
459 'glSecondaryColorPointer': ('glGetIntegerv', '', 'GL_SECONDARY_COLOR_ARRAY_SIZE'),
460 'glVertexAttribPointer': ('glGetVertexAttribiv', 'index, ', 'GL_VERTEX_ATTRIB_ARRAY_SIZE'),
461 'glVertexAttribPointerARB': ('glGetVertexAttribivARB', 'index, ', 'GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB'),
462 }
463 if function.name in buggyFunctions:
464 getter, extraArg, pname = buggyFunctions[function.name]
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100465 print(r' static bool _checked = false;')
466 print(r' if (!_checked && size == GL_BGRA) {')
467 print(r' GLint _size = 0;')
468 print(r' _%s(%s%s, &_size);' % (getter, extraArg, pname))
469 print(r' if (_size != GL_BGRA) {')
470 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))
471 print(r' }')
472 print(r' _checked = true;')
473 print(r' }')
José Fonsecac629a8c2014-06-01 21:12:27 +0100474
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100475 print(' return;')
476 print(' }')
José Fonseca14c21bc2011-02-20 23:32:22 +0000477
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000478 # ... to the draw calls
Jose Fonsecadddc5b82016-05-08 22:33:44 +0100479 mo = self.draw_function_regex.match(function.name)
480 if mo:
481 functionRadical = mo.group('radical')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100482 print(' gltrace::Context *_ctx = gltrace::getContext();')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300483
484 print(' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
485
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100486 print(' if (_need_user_arrays(_ctx)) {')
José Fonseca246508e2014-08-14 16:07:46 +0100487 if 'Indirect' in function.name:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100488 print(r' os::log("apitrace: warning: %s: indirect user arrays not supported\n");' % (function.name,))
José Fonseca246508e2014-08-14 16:07:46 +0100489 else:
Jose Fonsecadddc5b82016-05-08 22:33:44 +0100490 # Pick the corresponding *Params
491 if 'Arrays' in functionRadical:
492 paramsType = 'DrawArraysParams'
493 elif 'Elements' in functionRadical:
494 paramsType = 'DrawElementsParams'
495 else:
496 assert 0
497 if 'Multi' in functionRadical:
498 assert 'drawcount' in function.argNames()
499 paramsType = 'Multi' + paramsType
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100500 print(r' %s _params;' % paramsType)
Jose Fonsecadddc5b82016-05-08 22:33:44 +0100501
502 for arg in function.args:
503 paramsMember = arg.name.lower()
504 if paramsMember in ('mode', 'modestride'):
505 continue
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100506 print(r' _params.%s = %s;' % (paramsMember, arg.name))
Jose Fonsecadddc5b82016-05-08 22:33:44 +0100507
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100508 print(' GLuint _count = _glDraw_count(_ctx, _params);')
509 print(' _trace_user_arrays(_ctx, _count);')
510 print(' }')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300511
512 if function.name.startswith("glDispatchCompute"):
513 print(' gltrace::Context *_ctx = gltrace::getContext();')
514 print(' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
515
José Fonseca707630d2014-03-07 14:20:35 +0000516 if function.name == 'glLockArraysEXT':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100517 print(' gltrace::Context *_ctx = gltrace::getContext();')
518 print(' if (_ctx) {')
519 print(' _ctx->lockedArrayCount = first + count;')
520 print(' }')
José Fonseca4a7d8602014-06-18 16:03:44 +0100521
522 # Warn if user arrays are used with glBegin/glArrayElement/glEnd.
523 if function.name == 'glBegin':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100524 print(r' gltrace::Context *_ctx = gltrace::getContext();')
525 print(r' _ctx->userArraysOnBegin = _need_user_arrays(_ctx);')
José Fonseca7c39d012014-11-07 19:46:53 +0000526 if function.name.startswith('glArrayElement'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100527 print(r' gltrace::Context *_ctx = gltrace::getContext();')
528 print(r' if (_ctx->userArraysOnBegin) {')
529 print(r' os::log("apitrace: warning: user arrays with glArrayElement not supported (https://github.com/apitrace/apitrace/issues/276)\n");')
530 print(r' _ctx->userArraysOnBegin = false;')
531 print(r' }')
José Fonseca14c21bc2011-02-20 23:32:22 +0000532
José Fonseca73373602011-05-20 17:45:26 +0100533 # Emit a fake memcpy on buffer uploads
José Fonseca9c536b02012-02-29 20:54:13 +0000534 if function.name == 'glBufferParameteriAPPLE':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100535 print(' if (pname == GL_BUFFER_FLUSHING_UNMAP_APPLE && param == GL_FALSE) {')
536 print(' _checkBufferFlushingUnmapAPPLE = true;')
537 print(' }')
José Fonsecacdc322c2012-02-29 19:29:51 +0000538 if function.name in ('glUnmapBuffer', 'glUnmapBufferARB'):
José Fonseca9c536b02012-02-29 20:54:13 +0000539 if function.name.endswith('ARB'):
540 suffix = 'ARB'
541 else:
542 suffix = ''
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100543 print(' GLint access_flags = 0;')
544 print(' GLint access = 0;')
545 print(' bool flush;')
546 print(' // GLES3 does not have GL_BUFFER_ACCESS;')
547 print(' if (_checkBufferMapRange) {')
548 print(' _glGetBufferParameteriv%s(target, GL_BUFFER_ACCESS_FLAGS, &access_flags);' % suffix)
549 print(' flush = (access_flags & GL_MAP_WRITE_BIT) && !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT));')
550 print(' } else {')
551 print(' _glGetBufferParameteriv%s(target, GL_BUFFER_ACCESS, &access);' % suffix)
552 print(' flush = access != GL_READ_ONLY;')
553 print(' }')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300554 print(' if ((access_flags & GL_MAP_COHERENT_BIT) && (access_flags & GL_MAP_WRITE_BIT)) {')
555 print(' gltrace::Context *_ctx = gltrace::getContext();')
556 print(' GLint buffer = getBufferName(target);')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800557 print(' auto it = _ctx->sharedRes->bufferToShadowMemory.find(buffer);')
558 print(' if (it != _ctx->sharedRes->bufferToShadowMemory.end()) {')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300559 print(' it->second->unmap(trace::fakeMemcpy);')
560 print(' } else {')
561 print(r' os::log("apitrace: error: %s: cannot find memory shadow\n", __FUNCTION__);')
562 print(' }')
563 print(' flush = false;')
564 print(' }')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100565 print(' if (flush) {')
566 print(' GLvoid *map = NULL;')
567 print(' _glGetBufferPointerv%s(target, GL_BUFFER_MAP_POINTER, &map);' % suffix)
568 print(' if (map) {')
569 print(' GLint length = -1;')
570 print(' if (_checkBufferMapRange) {')
571 print(' _glGetBufferParameteriv%s(target, GL_BUFFER_MAP_LENGTH, &length);' % suffix)
572 print(' if (length == -1) {')
573 print(' // Mesa drivers refuse GL_BUFFER_MAP_LENGTH without GL 3.0 up-to')
574 print(' // http://cgit.freedesktop.org/mesa/mesa/commit/?id=ffee498fb848b253a7833373fe5430f8c7ca0c5f')
575 print(' static bool warned = false;')
576 print(' if (!warned) {')
577 print(' os::log("apitrace: warning: glGetBufferParameteriv%s(GL_BUFFER_MAP_LENGTH) failed\\n");' % suffix)
578 print(' warned = true;')
579 print(' }')
580 print(' }')
581 print(' } else {')
582 print(' length = 0;')
583 print(' _glGetBufferParameteriv%s(target, GL_BUFFER_SIZE, &length);' % suffix)
584 print(' }')
585 print(' if (_checkBufferFlushingUnmapAPPLE) {')
586 print(' GLint flushing_unmap = GL_TRUE;')
587 print(' _glGetBufferParameteriv%s(target, GL_BUFFER_FLUSHING_UNMAP_APPLE, &flushing_unmap);' % suffix)
588 print(' flush = flush && flushing_unmap;')
589 print(' }')
590 print(' if (flush && length > 0) {')
José Fonseca6f0e3032014-06-25 01:00:35 +0100591 self.emit_memcpy('map', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100592 print(' }')
593 print(' }')
594 print(' }')
José Fonsecacdc322c2012-02-29 19:29:51 +0000595 if function.name == 'glUnmapBufferOES':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100596 print(' GLint access_flags = 0;')
597 print(' GLint access = 0;')
598 print(' bool flush;')
599 print(' // GLES3 does not have GL_BUFFER_ACCESS;')
600 print(' if (_checkBufferMapRange) {')
601 print(' _glGetBufferParameteriv(target, GL_BUFFER_ACCESS_FLAGS, &access_flags);')
602 print(' flush = (access_flags & GL_MAP_WRITE_BIT) && !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT));')
603 print(' } else {')
604 print(' _glGetBufferParameteriv(target, GL_BUFFER_ACCESS, &access);')
605 print(' flush = access != GL_READ_ONLY;')
606 print(' }')
607 print(' if (flush) {')
608 print(' GLvoid *map = NULL;')
609 print(' _glGetBufferPointervOES(target, GL_BUFFER_MAP_POINTER, &map);')
610 print(' if (map) {')
611 print(' GLint length = 0;')
612 print(' GLint offset = 0;')
613 print(' if (_checkBufferMapRange) {')
614 print(' _glGetBufferParameteriv(target, GL_BUFFER_MAP_LENGTH, &length);')
615 print(' _glGetBufferParameteriv(target, GL_BUFFER_MAP_OFFSET, &offset);')
616 print(' } else {')
617 print(' _glGetBufferParameteriv(target, GL_BUFFER_SIZE, &length);')
618 print(' }')
619 print(' if (flush && length > 0) {')
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000620 self.emit_memcpy('map', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100621 print(' }')
622 print(' }')
623 print(' }')
José Fonseca4920c302014-08-13 18:35:57 +0100624 if function.name == 'glUnmapNamedBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100625 print(' GLint access_flags = 0;')
626 print(' _glGetNamedBufferParameteriv(buffer, GL_BUFFER_ACCESS_FLAGS, &access_flags);')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300627 print(' if ((access_flags & GL_MAP_COHERENT_BIT) && (access_flags & GL_MAP_WRITE_BIT)) {')
628 print(' gltrace::Context *_ctx = gltrace::getContext();')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800629 print(' auto it = _ctx->sharedRes->bufferToShadowMemory.find(buffer);')
630 print(' if (it != _ctx->sharedRes->bufferToShadowMemory.end()) {')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300631 print(' it->second->unmap(trace::fakeMemcpy);')
632 print(' } else {')
633 print(r' os::log("apitrace: error: %s: cannot find memory shadow\n", __FUNCTION__);')
634 print(' }')
635 print(' } else if ((access_flags & GL_MAP_WRITE_BIT) &&')
636 print(' !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT))) {')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100637 print(' GLvoid *map = NULL;')
638 print(' _glGetNamedBufferPointerv(buffer, GL_BUFFER_MAP_POINTER, &map);')
639 print(' GLint length = 0;')
640 print(' _glGetNamedBufferParameteriv(buffer, GL_BUFFER_MAP_LENGTH, &length);')
641 print(' if (map && length > 0) {')
José Fonseca4920c302014-08-13 18:35:57 +0100642 self.emit_memcpy('map', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100643 print(' }')
644 print(' }')
José Fonsecafb3bd602012-01-15 13:56:28 +0000645 if function.name == 'glUnmapNamedBufferEXT':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100646 print(' GLint access_flags = 0;')
647 print(' _glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_ACCESS_FLAGS, &access_flags);')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300648 print(' if ((access_flags & GL_MAP_COHERENT_BIT) && (access_flags & GL_MAP_WRITE_BIT)) {')
649 print(' gltrace::Context *_ctx = gltrace::getContext();')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800650 print(' auto it = _ctx->sharedRes->bufferToShadowMemory.find(buffer);')
651 print(' if (it != _ctx->sharedRes->bufferToShadowMemory.end()) {')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300652 print(' it->second->unmap(trace::fakeMemcpy);')
653 print(' } else {')
654 print(r' os::log("apitrace: error: %s: cannot find memory shadow\n", __FUNCTION__);')
655 print(' }')
656 print(' } else if ((access_flags & GL_MAP_WRITE_BIT) &&')
657 print(' !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT))) {')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100658 print(' GLvoid *map = NULL;')
659 print(' _glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &map);')
660 print(' GLint length = 0;')
661 print(' _glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_MAP_LENGTH, &length);')
662 print(' if (map && length > 0) {')
José Fonseca6f0e3032014-06-25 01:00:35 +0100663 self.emit_memcpy('map', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100664 print(' }')
665 print(' }')
José Fonseca77ef0ce2012-02-29 18:08:48 +0000666 if function.name == 'glFlushMappedBufferRange':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100667 print(' GLvoid *map = NULL;')
668 print(' _glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &map);')
669 print(' if (map && length > 0) {')
José Fonseca6f0e3032014-06-25 01:00:35 +0100670 self.emit_memcpy('(const char *)map + offset', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100671 print(' }')
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000672 if function.name == 'glFlushMappedBufferRangeEXT':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100673 print(' GLvoid *map = NULL;')
674 print(' _glGetBufferPointervOES(target, GL_BUFFER_MAP_POINTER_OES, &map);')
675 print(' if (map && length > 0) {')
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000676 self.emit_memcpy('(const char *)map + offset', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100677 print(' }')
José Fonseca77ef0ce2012-02-29 18:08:48 +0000678 if function.name == 'glFlushMappedBufferRangeAPPLE':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100679 print(' GLvoid *map = NULL;')
680 print(' _glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &map);')
681 print(' if (map && size > 0) {')
José Fonseca6f0e3032014-06-25 01:00:35 +0100682 self.emit_memcpy('(const char *)map + offset', 'size')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100683 print(' }')
José Fonseca4920c302014-08-13 18:35:57 +0100684 if function.name == 'glFlushMappedNamedBufferRange':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100685 print(' GLvoid *map = NULL;')
686 print(' _glGetNamedBufferPointerv(buffer, GL_BUFFER_MAP_POINTER, &map);')
687 print(' if (map && length > 0) {')
José Fonseca4920c302014-08-13 18:35:57 +0100688 self.emit_memcpy('(const char *)map + offset', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100689 print(' }')
José Fonsecafb3bd602012-01-15 13:56:28 +0000690 if function.name == 'glFlushMappedNamedBufferRangeEXT':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100691 print(' GLvoid *map = NULL;')
692 print(' _glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &map);')
693 print(' if (map && length > 0) {')
José Fonseca6f0e3032014-06-25 01:00:35 +0100694 self.emit_memcpy('(const char *)map + offset', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100695 print(' }')
José Fonseca867b1b72011-04-24 11:58:04 +0100696
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300697 # FIXME: We don't support AMD_pinned_memory
José Fonseca631dbd12014-12-15 16:34:45 +0000698 if function.name in ('glBufferStorage', 'glNamedBufferStorage', 'glNamedBufferStorageEXT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100699 print(r' if (flags & GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX) {')
700 print(r' if (!(flags & GL_MAP_PERSISTENT_BIT)) {')
701 print(r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_PERSISTENT_BIT\n", __FUNCTION__);')
702 print(r' }')
703 print(r' if (!(flags & GL_MAP_WRITE_BIT)) {')
704 print(r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_WRITE_BIT\n", __FUNCTION__);')
705 print(r' }')
706 print(r' flags &= ~GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX;')
707 print(r' }')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300708 print(r'')
709 print(r' if ((flags & GL_MAP_COHERENT_BIT) && (flags & GL_MAP_WRITE_BIT)) {')
710 print(r' gltrace::Context *_ctx = gltrace::getContext();')
711 if function.name in ('glBufferStorage'):
712 print(r' GLint buffer = getBufferName(target);')
713 print(r' auto memoryShadow = std::make_unique<GLMemoryShadow>();')
714 print(r' const bool success = memoryShadow->init(data, size);')
715 print(r' if (success) {')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800716 print(r' _ctx->sharedRes->bufferToShadowMemory.insert(std::make_pair(buffer, std::move(memoryShadow)));')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300717 print(r' } else {')
718 print(r' os::log("apitrace: error: %s: cannot create memory shadow\n", __FUNCTION__);')
719 print(r' }')
720 print(r' }')
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000721 if function.name in ('glMapBufferRange', 'glMapBufferRangeEXT', 'glMapNamedBufferRange', 'glMapNamedBufferRangeEXT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100722 print(r' if (access & GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX) {')
723 print(r' if (!(access & GL_MAP_PERSISTENT_BIT)) {')
724 print(r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_PERSISTENT_BIT\n", __FUNCTION__);')
725 print(r' }')
726 print(r' if (!(access & GL_MAP_WRITE_BIT)) {')
727 print(r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_WRITE_BIT\n", __FUNCTION__);')
728 print(r' }')
729 print(r' if (access & GL_MAP_FLUSH_EXPLICIT_BIT) {')
730 print(r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/ MAP_FLUSH_EXPLICIT_BIT\n", __FUNCTION__);')
731 print(r' }')
732 print(r' access &= ~GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX;')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100733 print(r' }')
José Fonseca3522cbd2014-02-28 14:45:32 +0000734 if function.name in ('glBufferData', 'glBufferDataARB'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100735 print(r' if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {')
736 print(r' os::log("apitrace: warning: GL_AMD_pinned_memory not fully supported\n");')
737 print(r' }')
José Fonseca3522cbd2014-02-28 14:45:32 +0000738
José Fonsecad0f1e292014-11-13 13:21:51 +0000739 # TODO: We don't track GL_INTEL_map_texture mappings
740 if function.name == 'glMapTexture2DINTEL':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100741 print(r' if (access & GL_MAP_WRITE_BIT) {')
742 print(r' os::log("apitrace: warning: GL_INTEL_map_texture not fully supported\n");')
743 print(r' }')
José Fonsecad0f1e292014-11-13 13:21:51 +0000744
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300745 # Operations on PBO may use coherent buffers so we must commit them first
746 if self.unpack_function_regex.match(function.name) or self.pack_function_regex.match(function.name):
747 print(' gltrace::Context *_ctx = gltrace::getContext();')
748 print(' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
749 print('')
750
José Fonseca91492d22011-05-23 21:20:31 +0100751 # Don't leave vertex attrib locations to chance. Instead emit fake
752 # glBindAttribLocation calls to ensure that the same locations will be
753 # used when retracing. Trying to remap locations after the fact would
754 # be an herculian task given that vertex attrib locations appear in
755 # many entry-points, including non-shader related ones.
756 if function.name == 'glLinkProgram':
José Fonseca54f304a2012-01-14 19:33:08 +0000757 Tracer.invokeFunction(self, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100758 print(' GLint active_attributes = 0;')
759 print(' _glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &active_attributes);')
760 print(' for (GLint attrib = 0; attrib < active_attributes; ++attrib) {')
761 print(' GLint size = 0;')
762 print(' GLenum type = 0;')
763 print(' GLchar name[256];')
José Fonseca91492d22011-05-23 21:20:31 +0100764 # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100765 print(' _glGetActiveAttrib(program, attrib, sizeof name, NULL, &size, &type, name);')
766 print(" if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {")
767 print(' GLint location = _glGetAttribLocation(program, name);')
768 print(' if (location >= 0) {')
769 print(' _fake_glBindAttribLocation(program, location, name);')
770 print(' }')
771 print(' }')
772 print(' }')
José Fonseca91492d22011-05-23 21:20:31 +0100773 if function.name == 'glLinkProgramARB':
José Fonseca54f304a2012-01-14 19:33:08 +0000774 Tracer.invokeFunction(self, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100775 print(' GLint active_attributes = 0;')
776 print(' _glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_ATTRIBUTES_ARB, &active_attributes);')
777 print(' for (GLint attrib = 0; attrib < active_attributes; ++attrib) {')
778 print(' GLint size = 0;')
779 print(' GLenum type = 0;')
780 print(' GLcharARB name[256];')
José Fonseca91492d22011-05-23 21:20:31 +0100781 # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100782 print(' _glGetActiveAttribARB(programObj, attrib, sizeof name, NULL, &size, &type, name);')
783 print(" if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {")
784 print(' GLint location = _glGetAttribLocationARB(programObj, name);')
785 print(' if (location >= 0) {')
786 print(' _fake_glBindAttribLocationARB(programObj, location, name);')
787 print(' }')
788 print(' }')
789 print(' }')
José Fonseca91492d22011-05-23 21:20:31 +0100790
José Fonseca54f304a2012-01-14 19:33:08 +0000791 Tracer.traceFunctionImplBody(self, function)
José Fonseca73373602011-05-20 17:45:26 +0100792
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700793 # These entrypoints are only expected to be implemented by tools;
794 # drivers will probably not implement them.
José Fonsecaf028a8f2012-02-15 23:33:35 +0000795 marker_functions = [
796 # GL_GREMEDY_string_marker
José Fonseca8f34d342011-07-15 20:16:40 +0100797 'glStringMarkerGREMEDY',
José Fonsecaf028a8f2012-02-15 23:33:35 +0000798 # GL_GREMEDY_frame_terminator
José Fonseca8f34d342011-07-15 20:16:40 +0100799 'glFrameTerminatorGREMEDY',
800 ]
801
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700802 # These entrypoints may be implemented by drivers, but are also very useful
803 # for debugging / analysis tools.
804 debug_functions = [
805 # GL_KHR_debug
806 'glDebugMessageControl',
807 'glDebugMessageInsert',
808 'glDebugMessageCallback',
809 'glGetDebugMessageLog',
810 'glPushDebugGroup',
811 'glPopDebugGroup',
812 'glObjectLabel',
813 'glGetObjectLabel',
814 'glObjectPtrLabel',
815 'glGetObjectPtrLabel',
Jose Fonsecae01aa3b2015-06-27 11:07:05 +0100816 # GL_KHR_debug (for OpenGL ES)
817 'glDebugMessageControlKHR',
818 'glDebugMessageInsertKHR',
819 'glDebugMessageCallbackKHR',
820 'glGetDebugMessageLogKHR',
821 'glPushDebugGroupKHR',
822 'glPopDebugGroupKHR',
823 'glObjectLabelKHR',
824 'glGetObjectLabelKHR',
825 'glObjectPtrLabelKHR',
826 'glGetObjectPtrLabelKHR',
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700827 # GL_ARB_debug_output
828 'glDebugMessageControlARB',
829 'glDebugMessageInsertARB',
830 'glDebugMessageCallbackARB',
831 'glGetDebugMessageLogARB',
832 # GL_AMD_debug_output
833 'glDebugMessageEnableAMD',
834 'glDebugMessageInsertAMD',
835 'glDebugMessageCallbackAMD',
836 'glGetDebugMessageLogAMD',
José Fonseca71f5a352014-07-28 12:19:50 +0100837 # GL_EXT_debug_label
838 'glLabelObjectEXT',
839 'glGetObjectLabelEXT',
840 # GL_EXT_debug_marker
841 'glInsertEventMarkerEXT',
842 'glPushGroupMarkerEXT',
843 'glPopGroupMarkerEXT',
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700844 ]
845
José Fonseca54f304a2012-01-14 19:33:08 +0000846 def invokeFunction(self, function):
José Fonseca91492d22011-05-23 21:20:31 +0100847 if function.name in ('glLinkProgram', 'glLinkProgramARB'):
848 # These functions have been dispatched already
849 return
850
José Fonsecae0c55fd2015-01-26 22:46:13 +0000851 # Force glProgramBinary to fail. Per ARB_get_program_binary this
852 # should signal the app that it needs to recompile.
853 if function.name in ('glProgramBinary', 'glProgramBinaryOES'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100854 print(r' binaryFormat = 0xDEADDEAD;')
855 print(r' binary = &binaryFormat;')
856 print(r' length = sizeof binaryFormat;')
José Fonsecae0c55fd2015-01-26 22:46:13 +0000857
José Fonseca2cfa02c2013-06-10 08:05:29 +0100858 Tracer.invokeFunction(self, function)
859
860 def doInvokeFunction(self, function):
861 # Same as invokeFunction() but called both when trace is enabled or disabled.
862 #
863 # Used to modify the behavior of GL entry-points.
864
865 # Override GL extensions
866 if function.name in ('glGetString', 'glGetIntegerv', 'glGetStringi'):
867 Tracer.doInvokeFunction(self, function, prefix = 'gltrace::_', suffix = '_override')
868 return
869
José Fonseca71f5a352014-07-28 12:19:50 +0100870 # We implement GL_GREMEDY_*, etc., and not the driver
José Fonsecaf028a8f2012-02-15 23:33:35 +0000871 if function.name in self.marker_functions:
José Fonseca8f34d342011-07-15 20:16:40 +0100872 return
873
Peter Lohrmann9d9eb812013-07-12 16:15:25 -0400874 # We may be faking KHR_debug, so ensure the pointer queries result is
875 # always zeroed to prevent dereference of unitialized pointers
876 if function.name == 'glGetPointerv':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100877 print(' if (params &&')
878 print(' (pname == GL_DEBUG_CALLBACK_FUNCTION ||')
879 print(' pname == GL_DEBUG_CALLBACK_USER_PARAM)) {')
880 print(' *params = NULL;')
881 print(' }')
Peter Lohrmann9d9eb812013-07-12 16:15:25 -0400882
José Fonseca2cfa02c2013-06-10 08:05:29 +0100883 if function.name in self.getProcAddressFunctionNames:
José Fonseca631dbd12014-12-15 16:34:45 +0000884 nameArg = function.args[0].name
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100885 print(' if (strcmp("glNotifyMappedBufferRangeVMWX", (const char *)%s) == 0) {' % (nameArg,))
886 print(' _result = (%s)&glNotifyMappedBufferRangeVMWX;' % (function.type,))
José Fonsecaf028a8f2012-02-15 23:33:35 +0000887 for marker_function in self.marker_functions:
José Fonseca1b6c8752012-04-15 14:33:00 +0100888 if self.api.getFunctionByName(marker_function):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100889 print(' } else if (strcmp("%s", (const char *)%s) == 0) {' % (marker_function, nameArg))
890 print(' _result = (%s)&%s;' % (function.type, marker_function))
891 print(' } else {')
José Fonseca2cfa02c2013-06-10 08:05:29 +0100892 Tracer.doInvokeFunction(self, function)
893
894 # Replace function addresses with ours
895 # XXX: Doing this here instead of wrapRet means that the trace will
896 # contain the addresses of the wrapper functions, and not the real
897 # functions, but in practice this should make no difference.
898 if function.name in self.getProcAddressFunctionNames:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100899 print(' _result = _wrapProcAddress(%s, _result);' % (nameArg,))
José Fonseca2cfa02c2013-06-10 08:05:29 +0100900
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100901 print(' }')
José Fonseca1b3d3752011-07-15 10:15:19 +0100902 return
903
José Fonsecae0c55fd2015-01-26 22:46:13 +0000904 if function.name in ('glGetProgramBinary', 'glGetProgramBinaryOES'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100905 print(r' bufSize = 0;')
José Fonsecae0c55fd2015-01-26 22:46:13 +0000906
José Fonseca2cfa02c2013-06-10 08:05:29 +0100907 Tracer.doInvokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100908
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300909 if function.name in ('glMapBufferRange', 'glMapBufferRangeEXT', 'glMapNamedBufferRange', 'glMapNamedBufferRangeEXT'):
910 print(r' if ((access & GL_MAP_COHERENT_BIT) && (access & GL_MAP_WRITE_BIT)) {')
911 print(r' gltrace::Context *_ctx = gltrace::getContext();')
912 if function.name in ('glMapBufferRange', 'glMapBufferRangeEXT'):
913 print(r' GLint buffer = getBufferName(target);')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800914 print(r' auto it = _ctx->sharedRes->bufferToShadowMemory.find(buffer);')
915 print(r' if (it != _ctx->sharedRes->bufferToShadowMemory.end()) {')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300916 print(r' _result = it->second->map(_ctx, _result, access, offset, length);')
917 print(r' } else {')
918 print(r' os::log("apitrace: error: %s: cannot find memory shadow\n", __FUNCTION__);')
919 print(r' }')
920 print(r' }')
921
922 # We should sync back readable coherent buffers only when a fence become signaled
923 # because in any other moment application cannot know if changes from operations on
924 # GPU are done and buffers are updated.
925 if function.name == 'glWaitSync':
926 print(r' gltrace::Context *_ctx = gltrace::getContext();')
Danylo Piliaiev76a24832020-02-07 16:28:53 +0200927 print(r' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300928 print(r' GLMemoryShadow::syncAllForReads(_ctx);')
929
930 if function.name == 'glClientWaitSync':
931 print(r' if (_result == GL_ALREADY_SIGNALED || _result == GL_CONDITION_SATISFIED) {')
932 print(r' gltrace::Context *_ctx = gltrace::getContext();')
Danylo Piliaiev76a24832020-02-07 16:28:53 +0200933 print(r' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300934 print(r' GLMemoryShadow::syncAllForReads(_ctx);')
935 print(r' }')
936
937 if function.name == 'glGetSynciv':
938 print(r' if (pname == GL_SYNC_STATUS && bufSize > 0 && values[0] == GL_SIGNALED) {')
939 print(r' gltrace::Context *_ctx = gltrace::getContext();')
Danylo Piliaiev76a24832020-02-07 16:28:53 +0200940 print(r' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300941 print(r' GLMemoryShadow::syncAllForReads(_ctx);')
942 print(r' }')
943
José Fonsecae0c55fd2015-01-26 22:46:13 +0000944 if function.name == 'glGetProgramiv':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100945 print(r' if (params && pname == GL_PROGRAM_BINARY_LENGTH) {')
946 print(r' *params = 0;')
947 print(r' }')
José Fonsecae0c55fd2015-01-26 22:46:13 +0000948 if function.name in ('glGetProgramBinary', 'glGetProgramBinaryOES'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100949 print(r' if (length) {')
950 print(r' *length = 0;')
951 print(r' }')
José Fonsecae0c55fd2015-01-26 22:46:13 +0000952
José Fonseca54f304a2012-01-14 19:33:08 +0000953 def wrapRet(self, function, instance):
954 Tracer.wrapRet(self, function, instance)
José Fonseca1b3d3752011-07-15 10:15:19 +0100955
José Fonsecacdc322c2012-02-29 19:29:51 +0000956 # Keep track of buffer mappings
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000957 if function.name in ('glMapBufferRange', 'glMapBufferRangeEXT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100958 print(' if (access & GL_MAP_WRITE_BIT) {')
959 print(' _checkBufferMapRange = true;')
960 print(' }')
José Fonseca669b1222011-02-20 09:05:10 +0000961
José Fonsecac9f12232011-03-25 20:07:42 +0000962 boolean_names = [
963 'GL_FALSE',
964 'GL_TRUE',
965 ]
966
967 def gl_boolean(self, value):
968 return self.boolean_names[int(bool(value))]
969
José Fonsecaa442a462014-11-12 21:16:22 +0000970 # Regular expression for the names of the functions that unpack from a
971 # pixel buffer object. See the ARB_pixel_buffer_object specification.
972 unpack_function_regex = re.compile(r'^gl(' + r'|'.join([
973 r'Bitmap',
974 r'PolygonStipple',
975 r'PixelMap[a-z]+v',
976 r'DrawPixels',
977 r'Color(Sub)?Table',
978 r'(Convolution|Separable)Filter[12]D',
979 r'(Compressed)?(Multi)?Tex(ture)?(Sub)?Image[1-4]D',
980 ]) + r')[0-9A-Z]*$')
José Fonsecae97bab92011-06-02 23:15:11 +0100981
werman29f6af32020-01-31 01:23:38 +0200982 compressed_image_function_regex = re.compile(r'^glCompressedTex(ture)?(Sub)?Image[1-4]D[0-9A-Z]*$')
983
José Fonseca54f304a2012-01-14 19:33:08 +0000984 def serializeArgValue(self, function, arg):
José Fonsecae97bab92011-06-02 23:15:11 +0100985 # Recognize offsets instead of blobs when a PBO is bound
José Fonsecaa442a462014-11-12 21:16:22 +0000986 if self.unpack_function_regex.match(function.name) \
José Fonsecae97bab92011-06-02 23:15:11 +0100987 and (isinstance(arg.type, stdapi.Blob) \
988 or (isinstance(arg.type, stdapi.Const) \
989 and isinstance(arg.type.type, stdapi.Blob))):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100990 print(' {')
991 print(' gltrace::Context *_ctx = gltrace::getContext();')
992 print(' GLint _unpack_buffer = 0;')
993 print(' if (_ctx->features.pixel_buffer_object)')
994 print(' _glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &_unpack_buffer);')
995 print(' if (_unpack_buffer) {')
996 print(' trace::localWriter.writePointer((uintptr_t)%s);' % arg.name)
997 print(' } else {')
werman29f6af32020-01-31 01:23:38 +0200998 if self.compressed_image_function_regex.match(function.name):
999 print(' %s;' % arg.type.size.format('[](const void* data, GLsizei size){ trace::localWriter.writeBlob(data, size); }'))
1000 else:
1001 Tracer.serializeArgValue(self, function, arg)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001002 print(' }')
1003 print(' }')
José Fonsecae97bab92011-06-02 23:15:11 +01001004 return
1005
Jose Fonseca0a676c52016-04-04 23:24:32 +01001006 # Recognize offsets instead of pointers when query buffer is bound
1007 if function.name.startswith('glGetQueryObject') and arg.output:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001008 print(r' gltrace::Context *_ctx = gltrace::getContext();')
1009 print(r' GLint _query_buffer = 0;')
1010 print(r' if (_ctx->features.query_buffer_object) {')
1011 print(r' _query_buffer = _glGetInteger(GL_QUERY_BUFFER_BINDING);')
1012 print(r' }')
1013 print(r' if (_query_buffer) {')
1014 print(r' trace::localWriter.writePointer((uintptr_t)%s);' % arg.name)
1015 print(r' } else {')
Jose Fonseca0a676c52016-04-04 23:24:32 +01001016 Tracer.serializeArgValue(self, function, arg)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001017 print(r' }')
Jose Fonseca0a676c52016-04-04 23:24:32 +01001018 return
1019
José Fonsecaa3f89ae2011-04-26 08:50:32 +01001020 # Several GL state functions take GLenum symbolic names as
1021 # integer/floats; so dump the symbolic name whenever possible
José Fonseca3bcb33c2011-05-27 20:14:31 +01001022 if function.name.startswith('gl') \
José Fonsecae3571092011-10-13 08:26:27 +01001023 and arg.type in (glapi.GLint, glapi.GLfloat, glapi.GLdouble) \
José Fonseca3bcb33c2011-05-27 20:14:31 +01001024 and arg.name == 'param':
José Fonsecaa3f89ae2011-04-26 08:50:32 +01001025 assert arg.index > 0
1026 assert function.args[arg.index - 1].name == 'pname'
1027 assert function.args[arg.index - 1].type == glapi.GLenum
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001028 print(' if (is_symbolic_pname(pname) && is_symbolic_param(%s)) {' % arg.name)
José Fonseca54f304a2012-01-14 19:33:08 +00001029 self.serializeValue(glapi.GLenum, arg.name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001030 print(' } else {')
José Fonseca54f304a2012-01-14 19:33:08 +00001031 Tracer.serializeArgValue(self, function, arg)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001032 print(' }')
José Fonsecaa3f89ae2011-04-26 08:50:32 +01001033 return
1034
José Fonseca54f304a2012-01-14 19:33:08 +00001035 Tracer.serializeArgValue(self, function, arg)
José Fonseca99221832011-03-22 22:15:46 +00001036
Jose Fonseca27b8d812016-05-13 07:09:09 -07001037 fake_function_names = [
1038 'glBindAttribLocation',
1039 'glBindAttribLocationARB',
1040 'glBindBuffer',
1041 'glBitmap',
1042 'glClientActiveTexture',
1043 'glDisableClientState',
1044 'glEnableClientState',
1045 'glEndList',
1046 'glNewList',
1047 'glScissor',
1048 'glStringMarkerGREMEDY',
1049 'glTexImage2D',
1050 'glViewport',
1051 ]
1052
José Fonseca4c938c22011-04-30 22:44:38 +01001053 def footer(self, api):
1054 Tracer.footer(self, api)
José Fonseca669b1222011-02-20 09:05:10 +00001055
Jose Fonseca27b8d812016-05-13 07:09:09 -07001056 # Generate helper functions to emit fake function calls into the trace
1057 for function in api.getAllFunctions():
1058 if function.name in self.fake_function_names:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001059 print(function.prototype('_fake_' + function.name))
1060 print(r'{')
Jose Fonseca27b8d812016-05-13 07:09:09 -07001061 self.fake_call(function, function.argNames())
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001062 print(r'}')
1063 print()
Jose Fonseca27b8d812016-05-13 07:09:09 -07001064
José Fonseca4c938c22011-04-30 22:44:38 +01001065 # A simple state tracker to track the pointer values
José Fonseca669b1222011-02-20 09:05:10 +00001066 # update the state
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001067 print('static void _trace_user_arrays(gltrace::Context *_ctx, GLuint count)')
1068 print('{')
1069 print(' glfeatures::Profile profile = _ctx->profile;')
1070 print(' bool es1 = profile.es() && profile.major == 1;')
1071 print()
José Fonseca8d1408b2014-02-03 19:57:18 +00001072
Jose Fonseca97d22a72016-05-10 04:47:08 -07001073 # Some apps, in particular Quake3, can tell the driver to lock more
1074 # vertices than those actually required for the draw call.
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001075 print(' count = std::max(count, _ctx->lockedArrayCount);')
1076 print()
Jose Fonseca97d22a72016-05-10 04:47:08 -07001077
José Fonseca8d1408b2014-02-03 19:57:18 +00001078 # Temporarily unbind the array buffer
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001079 print(' GLint _array_buffer = _glGetInteger(GL_ARRAY_BUFFER_BINDING);')
1080 print(' if (_array_buffer) {')
1081 print(' _fake_glBindBuffer(GL_ARRAY_BUFFER, 0);')
1082 print(' }')
1083 print()
José Fonseca1a2fdd22011-04-01 00:55:09 +01001084
José Fonseca99221832011-03-22 22:15:46 +00001085 for camelcase_name, uppercase_name in self.arrays:
Chia-I Wub3d218d2011-11-03 01:37:36 +08001086 # in which profile is the array available?
José Fonseca34ea6162015-01-08 23:45:43 +00001087 profile_check = 'profile.desktop()'
Chia-I Wub3d218d2011-11-03 01:37:36 +08001088 if camelcase_name in self.arrays_es1:
José Fonsecab0c59722015-01-05 20:45:41 +00001089 profile_check = '(' + profile_check + ' || es1)';
Chia-I Wub3d218d2011-11-03 01:37:36 +08001090
José Fonseca99221832011-03-22 22:15:46 +00001091 function_name = 'gl%sPointer' % camelcase_name
1092 enable_name = 'GL_%s_ARRAY' % uppercase_name
1093 binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name
José Fonseca1b6c8752012-04-15 14:33:00 +01001094 function = api.getFunctionByName(function_name)
José Fonseca99221832011-03-22 22:15:46 +00001095
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001096 print(' // %s' % function.prototype())
1097 print(' if (%s) {' % profile_check)
José Fonsecafb6744f2011-04-15 11:18:37 +01001098 self.array_trace_prolog(api, uppercase_name)
1099 self.array_prolog(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001100 print(' if (_glIsEnabled(%s)) {' % enable_name)
1101 print(' GLint _binding = _glGetInteger(%s);' % binding_name)
1102 print(' if (!_binding) {')
José Fonseca99221832011-03-22 22:15:46 +00001103
1104 # Get the arguments via glGet*
1105 for arg in function.args:
1106 arg_get_enum = 'GL_%s_ARRAY_%s' % (uppercase_name, arg.name.upper())
1107 arg_get_function, arg_type = TypeGetter().visit(arg.type)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001108 print(' %s %s = 0;' % (arg_type, arg.name))
1109 print(' _%s(%s, &%s);' % (arg_get_function, arg_get_enum, arg.name))
José Fonseca99221832011-03-22 22:15:46 +00001110
1111 arg_names = ', '.join([arg.name for arg in function.args[:-1]])
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001112 print(' size_t _size = _%s_size(%s, count);' % (function.name, arg_names))
José Fonseca99221832011-03-22 22:15:46 +00001113
1114 # Emit a fake function
José Fonsecafb6744f2011-04-15 11:18:37 +01001115 self.array_trace_intermezzo(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001116 print(' unsigned _call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,))
José Fonseca669b1222011-02-20 09:05:10 +00001117 for arg in function.args:
1118 assert not arg.output
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001119 print(' trace::localWriter.beginArg(%u);' % (arg.index,))
José Fonseca14c21bc2011-02-20 23:32:22 +00001120 if arg.name != 'pointer':
José Fonseca54f304a2012-01-14 19:33:08 +00001121 self.serializeValue(arg.type, arg.name)
José Fonseca14c21bc2011-02-20 23:32:22 +00001122 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001123 print(' trace::localWriter.writeBlob((const void *)%s, _size);' % (arg.name))
1124 print(' trace::localWriter.endArg();')
José Fonseca99221832011-03-22 22:15:46 +00001125
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001126 print(' trace::localWriter.endEnter();')
1127 print(' trace::localWriter.beginLeave(_call);')
1128 print(' trace::localWriter.endLeave();')
1129 print(' }')
1130 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +01001131 self.array_epilog(api, uppercase_name)
1132 self.array_trace_epilog(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001133 print(' }')
1134 print()
José Fonseca1a2fdd22011-04-01 00:55:09 +01001135
José Fonseca1601c412011-05-10 10:38:19 +01001136 # Samething, but for glVertexAttribPointer*
1137 #
1138 # Some variants of glVertexAttribPointer alias conventional and generic attributes:
1139 # - glVertexAttribPointer: no
1140 # - glVertexAttribPointerARB: implementation dependent
1141 # - glVertexAttribPointerNV: yes
1142 #
1143 # This means that the implementations of these functions do not always
1144 # alias, and they need to be considered independently.
1145 #
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001146 print(' // ES1 does not support generic vertex attributes')
1147 print(' if (es1)')
1148 print(' return;')
1149 print()
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001150
1151 function_name = 'glVertexAttribPointer'
1152 function = api.getFunctionByName(function_name)
1153
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001154 print(' // %s' % function.prototype())
1155 print(' GLint _max_vertex_attribs = _glGetInteger(GL_MAX_VERTEX_ATTRIBS);')
1156 print(' for (GLint index = 0; index < _max_vertex_attribs; ++index) {')
1157 print(' GLint _enabled = 0;')
1158 print(' _glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &_enabled);')
1159 print(' if (_enabled) {')
1160 print(' GLint _binding = 0;')
1161 print(' _glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &_binding);')
1162 print(' if (!_binding) {')
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001163
1164 # Get the arguments via glGet*
1165 for arg in function.args[1:]:
1166 arg_get_enum = 'GL_VERTEX_ATTRIB_ARRAY_%s' % (arg.name.upper())
1167 arg_get_function, arg_type = TypeGetter('glGetVertexAttrib', False).visit(arg.type)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001168 print(' %s %s = 0;' % (arg_type, arg.name))
1169 print(' _%s(index, %s, &%s);' % (arg_get_function, arg_get_enum, arg.name))
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001170
1171 arg_names = ', '.join([arg.name for arg in function.args[1:-1]])
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001172 print(' size_t _size = _%s_size(%s, count);' % (function.name, arg_names))
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001173
1174 # Emit a fake function
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001175 print(' unsigned _call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,))
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001176 for arg in function.args:
1177 assert not arg.output
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001178 print(' trace::localWriter.beginArg(%u);' % (arg.index,))
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001179 if arg.name != 'pointer':
1180 self.serializeValue(arg.type, arg.name)
1181 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001182 print(' trace::localWriter.writeBlob((const void *)%s, _size);' % (arg.name))
1183 print(' trace::localWriter.endArg();')
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001184
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001185 print(' trace::localWriter.endEnter();')
1186 print(' trace::localWriter.beginLeave(_call);')
1187 print(' trace::localWriter.endLeave();')
1188 print(' }')
1189 print(' }')
1190 print(' }')
1191 print()
José Fonseca1a2fdd22011-04-01 00:55:09 +01001192
José Fonseca8d1408b2014-02-03 19:57:18 +00001193 # Restore the original array_buffer
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001194 print(' if (_array_buffer) {')
1195 print(' _fake_glBindBuffer(GL_ARRAY_BUFFER, _array_buffer);')
1196 print(' }')
1197 print()
José Fonseca8d1408b2014-02-03 19:57:18 +00001198
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001199 print('}')
1200 print()
José Fonseca669b1222011-02-20 09:05:10 +00001201
José Fonsecafb6744f2011-04-15 11:18:37 +01001202 #
1203 # Hooks for glTexCoordPointer, which is identical to the other array
1204 # pointers except the fact that it is indexed by glClientActiveTexture.
1205 #
1206
1207 def array_prolog(self, api, uppercase_name):
1208 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001209 print(' GLint max_units = 0;')
1210 print(' if (_ctx->profile.desktop())')
1211 print(' _glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_units);')
1212 print(' else')
1213 print(' _glGetIntegerv(GL_MAX_TEXTURE_UNITS, &max_units);')
1214 print(' GLint client_active_texture = GL_TEXTURE0;')
1215 print(' if (max_units > 0) {')
1216 print(' _glGetIntegerv(GL_CLIENT_ACTIVE_TEXTURE, &client_active_texture);')
1217 print(' }')
1218 print(' GLint unit = 0;')
1219 print(' do {')
1220 print(' GLint texture = GL_TEXTURE0 + unit;')
1221 print(' if (max_units > 0) {')
1222 print(' _glClientActiveTexture(texture);')
1223 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +01001224
1225 def array_trace_prolog(self, api, uppercase_name):
1226 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001227 print(' bool client_active_texture_dirty = false;')
José Fonsecafb6744f2011-04-15 11:18:37 +01001228
1229 def array_epilog(self, api, uppercase_name):
1230 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001231 print(' } while (++unit < max_units);')
José Fonsecafb6744f2011-04-15 11:18:37 +01001232 self.array_cleanup(api, uppercase_name)
1233
1234 def array_cleanup(self, api, uppercase_name):
1235 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001236 print(' if (max_units > 0) {')
1237 print(' _glClientActiveTexture(client_active_texture);')
1238 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +01001239
1240 def array_trace_intermezzo(self, api, uppercase_name):
1241 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001242 print(' if (texture != client_active_texture || client_active_texture_dirty) {')
1243 print(' client_active_texture_dirty = true;')
1244 print(' _fake_glClientActiveTexture(texture);')
1245 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +01001246
1247 def array_trace_epilog(self, api, uppercase_name):
1248 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001249 print(' if (client_active_texture_dirty) {')
1250 print(' _fake_glClientActiveTexture(client_active_texture);')
1251 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +01001252
José Fonseca151c3702013-05-10 08:28:15 +01001253 def emitFakeTexture2D(self):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001254 print(r' _fake_glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);')
José Fonseca669b1222011-02-20 09:05:10 +00001255