blob: 24eb70f4136969ad27e478ad5bb055c82eae640e [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"')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100152 print()
José Fonseca5a568a92011-06-29 16:43:36 +0100153
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000154 # Whether we need user arrays
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100155 print('static inline bool _need_user_arrays(gltrace::Context *_ctx)')
156 print('{')
157 print(' if (!_ctx->user_arrays) {')
158 print(' return false;')
159 print(' }')
160 print()
161 print(' glfeatures::Profile profile = _ctx->profile;')
162 print(' bool es1 = profile.es() && profile.major == 1;')
163 print()
José Fonseca1a2fdd22011-04-01 00:55:09 +0100164
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000165 for camelcase_name, uppercase_name in self.arrays:
Chia-I Wub3d218d2011-11-03 01:37:36 +0800166 # in which profile is the array available?
José Fonseca34ea6162015-01-08 23:45:43 +0000167 profile_check = 'profile.desktop()'
Chia-I Wub3d218d2011-11-03 01:37:36 +0800168 if camelcase_name in self.arrays_es1:
José Fonsecab0c59722015-01-05 20:45:41 +0000169 profile_check = '(' + profile_check + ' || es1)';
Chia-I Wub3d218d2011-11-03 01:37:36 +0800170
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000171 function_name = 'gl%sPointer' % camelcase_name
172 enable_name = 'GL_%s_ARRAY' % uppercase_name
173 binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100174 print(' // %s' % function_name)
175 print(' if (%s) {' % profile_check)
José Fonsecafb6744f2011-04-15 11:18:37 +0100176 self.array_prolog(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100177 print(' if (_glIsEnabled(%s) &&' % enable_name)
178 print(' _glGetInteger(%s) == 0) {' % binding_name)
José Fonsecafb6744f2011-04-15 11:18:37 +0100179 self.array_cleanup(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100180 print(' return true;')
181 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +0100182 self.array_epilog(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100183 print(' }')
184 print()
José Fonseca7f5163e2011-03-31 23:37:26 +0100185
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100186 print(' // ES1 does not support generic vertex attributes')
187 print(' if (es1)')
188 print(' return false;')
189 print()
190 print(' // glVertexAttribPointer')
191 print(' GLint _max_vertex_attribs = _glGetInteger(GL_MAX_VERTEX_ATTRIBS);')
192 print(' for (GLint index = 0; index < _max_vertex_attribs; ++index) {')
193 print(' if (_glGetVertexAttribi(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED) &&')
194 print(' _glGetVertexAttribi(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING) == 0) {')
195 print(' return true;')
196 print(' }')
197 print(' }')
198 print()
José Fonseca1a2fdd22011-04-01 00:55:09 +0100199
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100200 print(' return false;')
201 print('}')
202 print()
José Fonseca669b1222011-02-20 09:05:10 +0000203
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100204 print(r'static void _trace_user_arrays(gltrace::Context *_ctx, GLuint count);')
205 print()
Jose Fonseca6ef9b3c2016-01-27 23:18:52 +0000206
Jose Fonseca27b8d812016-05-13 07:09:09 -0700207 # Declare helper functions to emit fake function calls into the trace
208 for function in api.getAllFunctions():
209 if function.name in self.fake_function_names:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100210 print(function.prototype('_fake_' + function.name) + ';')
211 print()
212 print(r'static inline void')
213 print(r'_fakeStringMarker(const std::string &s) {')
214 print(r' _fake_glStringMarkerGREMEDY(s.length(), s.data());')
215 print(r'}')
216 print()
José Fonseca867b1b72011-04-24 11:58:04 +0100217
José Fonseca9c536b02012-02-29 20:54:13 +0000218 # Buffer mappings
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100219 print('// whether glMapBufferRange(GL_MAP_WRITE_BIT) has ever been called')
220 print('static bool _checkBufferMapRange = false;')
221 print()
222 print('// whether glBufferParameteriAPPLE(GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE) has ever been called')
223 print('static bool _checkBufferFlushingUnmapAPPLE = false;')
224 print()
José Fonseca867b1b72011-04-24 11:58:04 +0100225
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100226 # Generate a helper function to determine whether a parameter name
227 # refers to a symbolic value or not
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100228 print('static bool')
229 print('is_symbolic_pname(GLenum pname) {')
230 print(' switch (pname) {')
José Fonseca5ea91872011-05-04 09:41:55 +0100231 for function, type, count, name in glparams.parameters:
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100232 if type is glapi.GLenum:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100233 print(' case %s:' % name)
234 print(' return true;')
235 print(' default:')
236 print(' return false;')
237 print(' }')
238 print('}')
239 print()
José Fonsecaa3f89ae2011-04-26 08:50:32 +0100240
241 # Generate a helper function to determine whether a parameter value is
242 # potentially symbolic or not; i.e., if the value can be represented in
243 # an enum or not
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100244 print('template<class T>')
245 print('static inline bool')
246 print('is_symbolic_param(T param) {')
247 print(' return static_cast<T>(static_cast<GLenum>(param)) == param;')
248 print('}')
249 print()
José Fonseca4c938c22011-04-30 22:44:38 +0100250
251 # Generate a helper function to know how many elements a parameter has
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100252 print('static size_t')
253 print('_gl_param_size(GLenum pname) {')
254 print(' switch (pname) {')
José Fonseca5ea91872011-05-04 09:41:55 +0100255 for function, type, count, name in glparams.parameters:
Jose Fonseca11b6bfa2015-07-21 13:32:51 +0100256 if name == 'GL_PROGRAM_BINARY_FORMATS':
257 count = 0
José Fonseca4c938c22011-04-30 22:44:38 +0100258 if type is not None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100259 print(' case %s: return %s;' % (name, count))
260 print(' default:')
261 print(r' os::log("apitrace: warning: %s: unknown GLenum 0x%04X\n", __FUNCTION__, pname);')
262 print(' return 1;')
263 print(' }')
264 print('}')
265 print()
José Fonseca4c938c22011-04-30 22:44:38 +0100266
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300267 # Generate a helper function to get buffer binding
268 print('static GLenum')
269 print('getBufferBinding(GLenum target) {')
270 print(' switch (target) {')
271 for target in self.buffer_targets:
272 print(' case %s:' % target)
273 print(' return %s_BINDING;' % target)
274 print(' default:')
275 print(' assert(false);')
276 print(' return 0;')
277 print(' }')
278 print('}')
279 print()
280
281 print('static GLint')
282 print('getBufferName(GLenum target) {')
283 print(' GLint bufferName = 0;')
284 print(' _glGetIntegerv(getBufferBinding(target), &bufferName);')
285 print(' assert(bufferName != 0);')
286 print(' return bufferName;')
287 print('}')
288 print()
289
Chia-I Wu335efb42011-11-03 01:59:22 +0800290 # states such as GL_UNPACK_ROW_LENGTH are not available in GLES
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100291 print('static inline bool')
292 print('can_unpack_subimage(void) {')
293 print(' gltrace::Context *_ctx = gltrace::getContext();')
Jose Fonseca487e9c52019-06-17 10:36:16 +0100294 print(' return _ctx->features.unpack_subimage;')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100295 print('}')
296 print()
Chia-I Wu335efb42011-11-03 01:59:22 +0800297
José Fonseca631dbd12014-12-15 16:34:45 +0000298 # VMWX_map_buffer_debug
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100299 print(r'extern "C" PUBLIC')
300 print(r'void APIENTRY')
301 print(r'glNotifyMappedBufferRangeVMWX(const void * start, GLsizeiptr length) {')
José Fonseca631dbd12014-12-15 16:34:45 +0000302 self.emit_memcpy('start', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100303 print(r'}')
304 print()
José Fonseca631dbd12014-12-15 16:34:45 +0000305
José Fonseca1b6c8752012-04-15 14:33:00 +0100306 getProcAddressFunctionNames = []
307
308 def traceApi(self, api):
309 if self.getProcAddressFunctionNames:
310 # Generate a function to wrap proc addresses
311 getProcAddressFunction = api.getFunctionByName(self.getProcAddressFunctionNames[0])
312 argType = getProcAddressFunction.args[0].type
313 retType = getProcAddressFunction.type
314
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100315 print('static %s _wrapProcAddress(%s procName, %s procPtr);' % (retType, argType, retType))
316 print()
José Fonseca1b6c8752012-04-15 14:33:00 +0100317
318 Tracer.traceApi(self, api)
319
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100320 print('static %s _wrapProcAddress(%s procName, %s procPtr) {' % (retType, argType, retType))
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700321
322 # Provide fallback functions to missing debug functions
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100323 print(' if (!procPtr) {')
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700324 else_ = ''
325 for function_name in self.debug_functions:
326 if self.api.getFunctionByName(function_name):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100327 print(' %sif (strcmp("%s", (const char *)procName) == 0) {' % (else_, function_name))
328 print(' return (%s)&%s;' % (retType, function_name))
329 print(' }')
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700330 else_ = 'else '
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100331 print(' %s{' % else_)
332 print(' return NULL;')
333 print(' }')
334 print(' }')
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700335
José Fonseca81301932012-11-11 00:10:20 +0000336 for function in api.getAllFunctions():
José Fonseca1b6c8752012-04-15 14:33:00 +0100337 ptype = function_pointer_type(function)
338 pvalue = function_pointer_value(function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100339 print(' if (strcmp("%s", (const char *)procName) == 0) {' % function.name)
340 print(' assert(procPtr != (%s)&%s);' % (retType, function.name))
341 print(' %s = (%s)procPtr;' % (pvalue, ptype))
342 print(' return (%s)&%s;' % (retType, function.name,))
343 print(' }')
344 print(' os::log("apitrace: warning: unknown function \\"%s\\"\\n", (const char *)procName);')
345 print(' return procPtr;')
346 print('}')
347 print()
José Fonseca1b6c8752012-04-15 14:33:00 +0100348 else:
349 Tracer.traceApi(self, api)
350
José Fonseca99221832011-03-22 22:15:46 +0000351 array_pointer_function_names = set((
352 "glVertexPointer",
353 "glNormalPointer",
354 "glColorPointer",
355 "glIndexPointer",
356 "glTexCoordPointer",
357 "glEdgeFlagPointer",
358 "glFogCoordPointer",
359 "glSecondaryColorPointer",
José Fonseca7f5163e2011-03-31 23:37:26 +0100360
José Fonsecaac5285b2011-05-04 11:09:08 +0100361 "glInterleavedArrays",
362
José Fonseca7e0bfd92011-04-30 23:09:03 +0100363 "glVertexPointerEXT",
364 "glNormalPointerEXT",
365 "glColorPointerEXT",
366 "glIndexPointerEXT",
367 "glTexCoordPointerEXT",
368 "glEdgeFlagPointerEXT",
369 "glFogCoordPointerEXT",
370 "glSecondaryColorPointerEXT",
José Fonseca99221832011-03-22 22:15:46 +0000371
José Fonseca7f5163e2011-03-31 23:37:26 +0100372 "glVertexAttribPointer",
373 "glVertexAttribPointerARB",
374 "glVertexAttribPointerNV",
José Fonsecaac5285b2011-05-04 11:09:08 +0100375 "glVertexAttribIPointer",
376 "glVertexAttribIPointerEXT",
José Fonseca7f5163e2011-03-31 23:37:26 +0100377 "glVertexAttribLPointer",
378 "glVertexAttribLPointerEXT",
José Fonseca99221832011-03-22 22:15:46 +0000379
380 #"glMatrixIndexPointerARB",
381 ))
382
José Fonsecac5bf77a2014-08-14 16:10:02 +0100383 # XXX: We currently ignore the gl*Draw*ElementArray* functions
Jose Fonsecadddc5b82016-05-08 22:33:44 +0100384 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 +0000385
José Fonsecac9f12232011-03-25 20:07:42 +0000386 interleaved_formats = [
387 'GL_V2F',
388 'GL_V3F',
389 'GL_C4UB_V2F',
390 'GL_C4UB_V3F',
391 'GL_C3F_V3F',
392 'GL_N3F_V3F',
393 'GL_C4F_N3F_V3F',
394 'GL_T2F_V3F',
395 'GL_T4F_V4F',
396 'GL_T2F_C4UB_V3F',
397 'GL_T2F_C3F_V3F',
398 'GL_T2F_N3F_V3F',
399 'GL_T2F_C4F_N3F_V3F',
400 'GL_T4F_C4F_N3F_V4F',
401 ]
402
José Fonseca54f304a2012-01-14 19:33:08 +0000403 def traceFunctionImplBody(self, function):
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000404 # Defer tracing of user array pointers...
José Fonseca99221832011-03-22 22:15:46 +0000405 if function.name in self.array_pointer_function_names:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100406 print(' GLint _array_buffer = _glGetInteger(GL_ARRAY_BUFFER_BINDING);')
407 print(' if (!_array_buffer) {')
408 print(' static bool warned = false;')
409 print(' if (!warned) {')
410 print(' warned = true;')
411 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__);')
412 print(' }')
413 print(' gltrace::Context *_ctx = gltrace::getContext();')
414 print(' _ctx->user_arrays = true;')
José Fonseca5a568a92011-06-29 16:43:36 +0100415 if function.name == "glVertexAttribPointerNV":
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100416 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 +0000417 self.invokeFunction(function)
José Fonsecaac5285b2011-05-04 11:09:08 +0100418
419 # And also break down glInterleavedArrays into the individual calls
420 if function.name == 'glInterleavedArrays':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100421 print()
José Fonsecaac5285b2011-05-04 11:09:08 +0100422
423 # Initialize the enable flags
424 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100425 flag_name = '_' + uppercase_name.lower()
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100426 print(' GLboolean %s = GL_FALSE;' % flag_name)
427 print()
José Fonsecaac5285b2011-05-04 11:09:08 +0100428
429 # Switch for the interleaved formats
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100430 print(' switch (format) {')
José Fonsecaac5285b2011-05-04 11:09:08 +0100431 for format in self.interleaved_formats:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100432 print(' case %s:' % format)
José Fonsecaac5285b2011-05-04 11:09:08 +0100433 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100434 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100435 if format.find('_' + uppercase_name[0]) >= 0:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100436 print(' %s = GL_TRUE;' % flag_name)
437 print(' break;')
438 print(' default:')
439 print(' return;')
440 print(' }')
441 print()
José Fonsecaac5285b2011-05-04 11:09:08 +0100442
443 # Emit fake glEnableClientState/glDisableClientState flags
444 for camelcase_name, uppercase_name in self.arrays:
José Fonseca632a78d2012-04-19 07:18:59 +0100445 flag_name = '_' + uppercase_name.lower()
José Fonsecaac5285b2011-05-04 11:09:08 +0100446 enable_name = 'GL_%s_ARRAY' % uppercase_name
447
448 # Emit a fake function
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100449 print(' if (%s) {' % flag_name)
450 print(' _fake_glEnableClientState(%s);' % enable_name)
451 print(' } else {')
452 print(' _fake_glDisableClientState(%s);' % enable_name)
453 print(' }')
José Fonsecaac5285b2011-05-04 11:09:08 +0100454
José Fonsecac629a8c2014-06-01 21:12:27 +0100455 # Warn about buggy glGet(GL_*ARRAY_SIZE) not returning GL_BGRA
456 buggyFunctions = {
457 'glColorPointer': ('glGetIntegerv', '', 'GL_COLOR_ARRAY_SIZE'),
458 'glSecondaryColorPointer': ('glGetIntegerv', '', 'GL_SECONDARY_COLOR_ARRAY_SIZE'),
459 'glVertexAttribPointer': ('glGetVertexAttribiv', 'index, ', 'GL_VERTEX_ATTRIB_ARRAY_SIZE'),
460 'glVertexAttribPointerARB': ('glGetVertexAttribivARB', 'index, ', 'GL_VERTEX_ATTRIB_ARRAY_SIZE_ARB'),
461 }
462 if function.name in buggyFunctions:
463 getter, extraArg, pname = buggyFunctions[function.name]
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100464 print(r' static bool _checked = false;')
465 print(r' if (!_checked && size == GL_BGRA) {')
466 print(r' GLint _size = 0;')
467 print(r' _%s(%s%s, &_size);' % (getter, extraArg, pname))
468 print(r' if (_size != GL_BGRA) {')
469 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))
470 print(r' }')
471 print(r' _checked = true;')
472 print(r' }')
José Fonsecac629a8c2014-06-01 21:12:27 +0100473
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100474 print(' return;')
475 print(' }')
José Fonseca14c21bc2011-02-20 23:32:22 +0000476
José Fonseca8a6c6cb2011-03-23 16:44:30 +0000477 # ... to the draw calls
Jose Fonsecadddc5b82016-05-08 22:33:44 +0100478 mo = self.draw_function_regex.match(function.name)
479 if mo:
480 functionRadical = mo.group('radical')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100481 print(' gltrace::Context *_ctx = gltrace::getContext();')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300482
483 print(' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
484
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100485 print(' if (_need_user_arrays(_ctx)) {')
José Fonseca246508e2014-08-14 16:07:46 +0100486 if 'Indirect' in function.name:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100487 print(r' os::log("apitrace: warning: %s: indirect user arrays not supported\n");' % (function.name,))
José Fonseca246508e2014-08-14 16:07:46 +0100488 else:
Jose Fonsecadddc5b82016-05-08 22:33:44 +0100489 # Pick the corresponding *Params
490 if 'Arrays' in functionRadical:
491 paramsType = 'DrawArraysParams'
492 elif 'Elements' in functionRadical:
493 paramsType = 'DrawElementsParams'
494 else:
495 assert 0
496 if 'Multi' in functionRadical:
497 assert 'drawcount' in function.argNames()
498 paramsType = 'Multi' + paramsType
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100499 print(r' %s _params;' % paramsType)
Jose Fonsecadddc5b82016-05-08 22:33:44 +0100500
501 for arg in function.args:
502 paramsMember = arg.name.lower()
503 if paramsMember in ('mode', 'modestride'):
504 continue
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100505 print(r' _params.%s = %s;' % (paramsMember, arg.name))
Jose Fonsecadddc5b82016-05-08 22:33:44 +0100506
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100507 print(' GLuint _count = _glDraw_count(_ctx, _params);')
508 print(' _trace_user_arrays(_ctx, _count);')
509 print(' }')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300510
511 if function.name.startswith("glDispatchCompute"):
512 print(' gltrace::Context *_ctx = gltrace::getContext();')
513 print(' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
514
José Fonseca707630d2014-03-07 14:20:35 +0000515 if function.name == 'glLockArraysEXT':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100516 print(' gltrace::Context *_ctx = gltrace::getContext();')
517 print(' if (_ctx) {')
518 print(' _ctx->lockedArrayCount = first + count;')
519 print(' }')
José Fonseca4a7d8602014-06-18 16:03:44 +0100520
521 # Warn if user arrays are used with glBegin/glArrayElement/glEnd.
522 if function.name == 'glBegin':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100523 print(r' gltrace::Context *_ctx = gltrace::getContext();')
524 print(r' _ctx->userArraysOnBegin = _need_user_arrays(_ctx);')
José Fonseca7c39d012014-11-07 19:46:53 +0000525 if function.name.startswith('glArrayElement'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100526 print(r' gltrace::Context *_ctx = gltrace::getContext();')
527 print(r' if (_ctx->userArraysOnBegin) {')
528 print(r' os::log("apitrace: warning: user arrays with glArrayElement not supported (https://github.com/apitrace/apitrace/issues/276)\n");')
529 print(r' _ctx->userArraysOnBegin = false;')
530 print(r' }')
José Fonseca14c21bc2011-02-20 23:32:22 +0000531
José Fonseca73373602011-05-20 17:45:26 +0100532 # Emit a fake memcpy on buffer uploads
José Fonseca9c536b02012-02-29 20:54:13 +0000533 if function.name == 'glBufferParameteriAPPLE':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100534 print(' if (pname == GL_BUFFER_FLUSHING_UNMAP_APPLE && param == GL_FALSE) {')
535 print(' _checkBufferFlushingUnmapAPPLE = true;')
536 print(' }')
José Fonsecacdc322c2012-02-29 19:29:51 +0000537 if function.name in ('glUnmapBuffer', 'glUnmapBufferARB'):
José Fonseca9c536b02012-02-29 20:54:13 +0000538 if function.name.endswith('ARB'):
539 suffix = 'ARB'
540 else:
541 suffix = ''
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100542 print(' GLint access_flags = 0;')
543 print(' GLint access = 0;')
544 print(' bool flush;')
545 print(' // GLES3 does not have GL_BUFFER_ACCESS;')
546 print(' if (_checkBufferMapRange) {')
547 print(' _glGetBufferParameteriv%s(target, GL_BUFFER_ACCESS_FLAGS, &access_flags);' % suffix)
548 print(' flush = (access_flags & GL_MAP_WRITE_BIT) && !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT));')
549 print(' } else {')
550 print(' _glGetBufferParameteriv%s(target, GL_BUFFER_ACCESS, &access);' % suffix)
551 print(' flush = access != GL_READ_ONLY;')
552 print(' }')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300553 print(' if ((access_flags & GL_MAP_COHERENT_BIT) && (access_flags & GL_MAP_WRITE_BIT)) {')
554 print(' gltrace::Context *_ctx = gltrace::getContext();')
555 print(' GLint buffer = getBufferName(target);')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800556 print(' auto it = _ctx->sharedRes->bufferToShadowMemory.find(buffer);')
557 print(' if (it != _ctx->sharedRes->bufferToShadowMemory.end()) {')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300558 print(' it->second->unmap(trace::fakeMemcpy);')
559 print(' } else {')
560 print(r' os::log("apitrace: error: %s: cannot find memory shadow\n", __FUNCTION__);')
561 print(' }')
562 print(' flush = false;')
563 print(' }')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100564 print(' if (flush) {')
565 print(' GLvoid *map = NULL;')
566 print(' _glGetBufferPointerv%s(target, GL_BUFFER_MAP_POINTER, &map);' % suffix)
567 print(' if (map) {')
568 print(' GLint length = -1;')
569 print(' if (_checkBufferMapRange) {')
570 print(' _glGetBufferParameteriv%s(target, GL_BUFFER_MAP_LENGTH, &length);' % suffix)
571 print(' if (length == -1) {')
572 print(' // Mesa drivers refuse GL_BUFFER_MAP_LENGTH without GL 3.0 up-to')
573 print(' // http://cgit.freedesktop.org/mesa/mesa/commit/?id=ffee498fb848b253a7833373fe5430f8c7ca0c5f')
574 print(' static bool warned = false;')
575 print(' if (!warned) {')
576 print(' os::log("apitrace: warning: glGetBufferParameteriv%s(GL_BUFFER_MAP_LENGTH) failed\\n");' % suffix)
577 print(' warned = true;')
578 print(' }')
579 print(' }')
580 print(' } else {')
581 print(' length = 0;')
582 print(' _glGetBufferParameteriv%s(target, GL_BUFFER_SIZE, &length);' % suffix)
583 print(' }')
584 print(' if (_checkBufferFlushingUnmapAPPLE) {')
585 print(' GLint flushing_unmap = GL_TRUE;')
586 print(' _glGetBufferParameteriv%s(target, GL_BUFFER_FLUSHING_UNMAP_APPLE, &flushing_unmap);' % suffix)
587 print(' flush = flush && flushing_unmap;')
588 print(' }')
589 print(' if (flush && length > 0) {')
José Fonseca6f0e3032014-06-25 01:00:35 +0100590 self.emit_memcpy('map', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100591 print(' }')
592 print(' }')
593 print(' }')
José Fonsecacdc322c2012-02-29 19:29:51 +0000594 if function.name == 'glUnmapBufferOES':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100595 print(' GLint access_flags = 0;')
596 print(' GLint access = 0;')
597 print(' bool flush;')
598 print(' // GLES3 does not have GL_BUFFER_ACCESS;')
599 print(' if (_checkBufferMapRange) {')
600 print(' _glGetBufferParameteriv(target, GL_BUFFER_ACCESS_FLAGS, &access_flags);')
601 print(' flush = (access_flags & GL_MAP_WRITE_BIT) && !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT));')
602 print(' } else {')
603 print(' _glGetBufferParameteriv(target, GL_BUFFER_ACCESS, &access);')
604 print(' flush = access != GL_READ_ONLY;')
605 print(' }')
606 print(' if (flush) {')
607 print(' GLvoid *map = NULL;')
608 print(' _glGetBufferPointervOES(target, GL_BUFFER_MAP_POINTER, &map);')
609 print(' if (map) {')
610 print(' GLint length = 0;')
611 print(' GLint offset = 0;')
612 print(' if (_checkBufferMapRange) {')
613 print(' _glGetBufferParameteriv(target, GL_BUFFER_MAP_LENGTH, &length);')
614 print(' _glGetBufferParameteriv(target, GL_BUFFER_MAP_OFFSET, &offset);')
615 print(' } else {')
616 print(' _glGetBufferParameteriv(target, GL_BUFFER_SIZE, &length);')
617 print(' }')
618 print(' if (flush && length > 0) {')
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000619 self.emit_memcpy('map', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100620 print(' }')
621 print(' }')
622 print(' }')
José Fonseca4920c302014-08-13 18:35:57 +0100623 if function.name == 'glUnmapNamedBuffer':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100624 print(' GLint access_flags = 0;')
625 print(' _glGetNamedBufferParameteriv(buffer, GL_BUFFER_ACCESS_FLAGS, &access_flags);')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300626 print(' if ((access_flags & GL_MAP_COHERENT_BIT) && (access_flags & GL_MAP_WRITE_BIT)) {')
627 print(' gltrace::Context *_ctx = gltrace::getContext();')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800628 print(' auto it = _ctx->sharedRes->bufferToShadowMemory.find(buffer);')
629 print(' if (it != _ctx->sharedRes->bufferToShadowMemory.end()) {')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300630 print(' it->second->unmap(trace::fakeMemcpy);')
631 print(' } else {')
632 print(r' os::log("apitrace: error: %s: cannot find memory shadow\n", __FUNCTION__);')
633 print(' }')
634 print(' } else if ((access_flags & GL_MAP_WRITE_BIT) &&')
635 print(' !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT))) {')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100636 print(' GLvoid *map = NULL;')
637 print(' _glGetNamedBufferPointerv(buffer, GL_BUFFER_MAP_POINTER, &map);')
638 print(' GLint length = 0;')
639 print(' _glGetNamedBufferParameteriv(buffer, GL_BUFFER_MAP_LENGTH, &length);')
640 print(' if (map && length > 0) {')
José Fonseca4920c302014-08-13 18:35:57 +0100641 self.emit_memcpy('map', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100642 print(' }')
643 print(' }')
José Fonsecafb3bd602012-01-15 13:56:28 +0000644 if function.name == 'glUnmapNamedBufferEXT':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100645 print(' GLint access_flags = 0;')
646 print(' _glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_ACCESS_FLAGS, &access_flags);')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300647 print(' if ((access_flags & GL_MAP_COHERENT_BIT) && (access_flags & GL_MAP_WRITE_BIT)) {')
648 print(' gltrace::Context *_ctx = gltrace::getContext();')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800649 print(' auto it = _ctx->sharedRes->bufferToShadowMemory.find(buffer);')
650 print(' if (it != _ctx->sharedRes->bufferToShadowMemory.end()) {')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300651 print(' it->second->unmap(trace::fakeMemcpy);')
652 print(' } else {')
653 print(r' os::log("apitrace: error: %s: cannot find memory shadow\n", __FUNCTION__);')
654 print(' }')
655 print(' } else if ((access_flags & GL_MAP_WRITE_BIT) &&')
656 print(' !(access_flags & (GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT))) {')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100657 print(' GLvoid *map = NULL;')
658 print(' _glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &map);')
659 print(' GLint length = 0;')
660 print(' _glGetNamedBufferParameterivEXT(buffer, GL_BUFFER_MAP_LENGTH, &length);')
661 print(' if (map && length > 0) {')
José Fonseca6f0e3032014-06-25 01:00:35 +0100662 self.emit_memcpy('map', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100663 print(' }')
664 print(' }')
José Fonseca77ef0ce2012-02-29 18:08:48 +0000665 if function.name == 'glFlushMappedBufferRange':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100666 print(' GLvoid *map = NULL;')
667 print(' _glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &map);')
668 print(' if (map && length > 0) {')
José Fonseca6f0e3032014-06-25 01:00:35 +0100669 self.emit_memcpy('(const char *)map + offset', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100670 print(' }')
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000671 if function.name == 'glFlushMappedBufferRangeEXT':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100672 print(' GLvoid *map = NULL;')
673 print(' _glGetBufferPointervOES(target, GL_BUFFER_MAP_POINTER_OES, &map);')
674 print(' if (map && length > 0) {')
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000675 self.emit_memcpy('(const char *)map + offset', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100676 print(' }')
José Fonseca77ef0ce2012-02-29 18:08:48 +0000677 if function.name == 'glFlushMappedBufferRangeAPPLE':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100678 print(' GLvoid *map = NULL;')
679 print(' _glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &map);')
680 print(' if (map && size > 0) {')
José Fonseca6f0e3032014-06-25 01:00:35 +0100681 self.emit_memcpy('(const char *)map + offset', 'size')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100682 print(' }')
José Fonseca4920c302014-08-13 18:35:57 +0100683 if function.name == 'glFlushMappedNamedBufferRange':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100684 print(' GLvoid *map = NULL;')
685 print(' _glGetNamedBufferPointerv(buffer, GL_BUFFER_MAP_POINTER, &map);')
686 print(' if (map && length > 0) {')
José Fonseca4920c302014-08-13 18:35:57 +0100687 self.emit_memcpy('(const char *)map + offset', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100688 print(' }')
José Fonsecafb3bd602012-01-15 13:56:28 +0000689 if function.name == 'glFlushMappedNamedBufferRangeEXT':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100690 print(' GLvoid *map = NULL;')
691 print(' _glGetNamedBufferPointervEXT(buffer, GL_BUFFER_MAP_POINTER, &map);')
692 print(' if (map && length > 0) {')
José Fonseca6f0e3032014-06-25 01:00:35 +0100693 self.emit_memcpy('(const char *)map + offset', 'length')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100694 print(' }')
José Fonseca867b1b72011-04-24 11:58:04 +0100695
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300696 # FIXME: We don't support AMD_pinned_memory
José Fonseca631dbd12014-12-15 16:34:45 +0000697 if function.name in ('glBufferStorage', 'glNamedBufferStorage', 'glNamedBufferStorageEXT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100698 print(r' if (flags & GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX) {')
699 print(r' if (!(flags & GL_MAP_PERSISTENT_BIT)) {')
700 print(r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_PERSISTENT_BIT\n", __FUNCTION__);')
701 print(r' }')
702 print(r' if (!(flags & GL_MAP_WRITE_BIT)) {')
703 print(r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_WRITE_BIT\n", __FUNCTION__);')
704 print(r' }')
705 print(r' flags &= ~GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX;')
706 print(r' }')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300707 print(r'')
708 print(r' if ((flags & GL_MAP_COHERENT_BIT) && (flags & GL_MAP_WRITE_BIT)) {')
709 print(r' gltrace::Context *_ctx = gltrace::getContext();')
710 if function.name in ('glBufferStorage'):
711 print(r' GLint buffer = getBufferName(target);')
712 print(r' auto memoryShadow = std::make_unique<GLMemoryShadow>();')
713 print(r' const bool success = memoryShadow->init(data, size);')
714 print(r' if (success) {')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800715 print(r' _ctx->sharedRes->bufferToShadowMemory.insert(std::make_pair(buffer, std::move(memoryShadow)));')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300716 print(r' } else {')
717 print(r' os::log("apitrace: error: %s: cannot create memory shadow\n", __FUNCTION__);')
718 print(r' }')
719 print(r' }')
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000720 if function.name in ('glMapBufferRange', 'glMapBufferRangeEXT', 'glMapNamedBufferRange', 'glMapNamedBufferRangeEXT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100721 print(r' if (access & GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX) {')
722 print(r' if (!(access & GL_MAP_PERSISTENT_BIT)) {')
723 print(r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_PERSISTENT_BIT\n", __FUNCTION__);')
724 print(r' }')
725 print(r' if (!(access & GL_MAP_WRITE_BIT)) {')
726 print(r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/o MAP_WRITE_BIT\n", __FUNCTION__);')
727 print(r' }')
728 print(r' if (access & GL_MAP_FLUSH_EXPLICIT_BIT) {')
729 print(r' os::log("apitrace: warning: %s: MAP_NOTIFY_EXPLICIT_BIT_VMWX set w/ MAP_FLUSH_EXPLICIT_BIT\n", __FUNCTION__);')
730 print(r' }')
731 print(r' access &= ~GL_MAP_NOTIFY_EXPLICIT_BIT_VMWX;')
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100732 print(r' }')
José Fonseca3522cbd2014-02-28 14:45:32 +0000733 if function.name in ('glBufferData', 'glBufferDataARB'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100734 print(r' if (target == GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD) {')
735 print(r' os::log("apitrace: warning: GL_AMD_pinned_memory not fully supported\n");')
736 print(r' }')
José Fonseca3522cbd2014-02-28 14:45:32 +0000737
José Fonsecad0f1e292014-11-13 13:21:51 +0000738 # TODO: We don't track GL_INTEL_map_texture mappings
739 if function.name == 'glMapTexture2DINTEL':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100740 print(r' if (access & GL_MAP_WRITE_BIT) {')
741 print(r' os::log("apitrace: warning: GL_INTEL_map_texture not fully supported\n");')
742 print(r' }')
José Fonsecad0f1e292014-11-13 13:21:51 +0000743
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300744 # Operations on PBO may use coherent buffers so we must commit them first
745 if self.unpack_function_regex.match(function.name) or self.pack_function_regex.match(function.name):
746 print(' gltrace::Context *_ctx = gltrace::getContext();')
747 print(' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
748 print('')
749
José Fonseca91492d22011-05-23 21:20:31 +0100750 # Don't leave vertex attrib locations to chance. Instead emit fake
751 # glBindAttribLocation calls to ensure that the same locations will be
752 # used when retracing. Trying to remap locations after the fact would
753 # be an herculian task given that vertex attrib locations appear in
754 # many entry-points, including non-shader related ones.
755 if function.name == 'glLinkProgram':
José Fonseca54f304a2012-01-14 19:33:08 +0000756 Tracer.invokeFunction(self, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100757 print(' GLint active_attributes = 0;')
758 print(' _glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &active_attributes);')
759 print(' for (GLint attrib = 0; attrib < active_attributes; ++attrib) {')
760 print(' GLint size = 0;')
761 print(' GLenum type = 0;')
762 print(' GLchar name[256];')
José Fonseca91492d22011-05-23 21:20:31 +0100763 # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100764 print(' _glGetActiveAttrib(program, attrib, sizeof name, NULL, &size, &type, name);')
765 print(" if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {")
766 print(' GLint location = _glGetAttribLocation(program, name);')
767 print(' if (location >= 0) {')
768 print(' _fake_glBindAttribLocation(program, location, name);')
769 print(' }')
770 print(' }')
771 print(' }')
José Fonseca91492d22011-05-23 21:20:31 +0100772 if function.name == 'glLinkProgramARB':
José Fonseca54f304a2012-01-14 19:33:08 +0000773 Tracer.invokeFunction(self, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100774 print(' GLint active_attributes = 0;')
775 print(' _glGetObjectParameterivARB(programObj, GL_OBJECT_ACTIVE_ATTRIBUTES_ARB, &active_attributes);')
776 print(' for (GLint attrib = 0; attrib < active_attributes; ++attrib) {')
777 print(' GLint size = 0;')
778 print(' GLenum type = 0;')
779 print(' GLcharARB name[256];')
José Fonseca91492d22011-05-23 21:20:31 +0100780 # TODO: Use ACTIVE_ATTRIBUTE_MAX_LENGTH instead of 256
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100781 print(' _glGetActiveAttribARB(programObj, attrib, sizeof name, NULL, &size, &type, name);')
782 print(" if (name[0] != 'g' || name[1] != 'l' || name[2] != '_') {")
783 print(' GLint location = _glGetAttribLocationARB(programObj, name);')
784 print(' if (location >= 0) {')
785 print(' _fake_glBindAttribLocationARB(programObj, location, name);')
786 print(' }')
787 print(' }')
788 print(' }')
José Fonseca91492d22011-05-23 21:20:31 +0100789
José Fonseca54f304a2012-01-14 19:33:08 +0000790 Tracer.traceFunctionImplBody(self, function)
José Fonseca73373602011-05-20 17:45:26 +0100791
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700792 # These entrypoints are only expected to be implemented by tools;
793 # drivers will probably not implement them.
José Fonsecaf028a8f2012-02-15 23:33:35 +0000794 marker_functions = [
795 # GL_GREMEDY_string_marker
José Fonseca8f34d342011-07-15 20:16:40 +0100796 'glStringMarkerGREMEDY',
José Fonsecaf028a8f2012-02-15 23:33:35 +0000797 # GL_GREMEDY_frame_terminator
José Fonseca8f34d342011-07-15 20:16:40 +0100798 'glFrameTerminatorGREMEDY',
799 ]
800
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700801 # These entrypoints may be implemented by drivers, but are also very useful
802 # for debugging / analysis tools.
803 debug_functions = [
804 # GL_KHR_debug
805 'glDebugMessageControl',
806 'glDebugMessageInsert',
807 'glDebugMessageCallback',
808 'glGetDebugMessageLog',
809 'glPushDebugGroup',
810 'glPopDebugGroup',
811 'glObjectLabel',
812 'glGetObjectLabel',
813 'glObjectPtrLabel',
814 'glGetObjectPtrLabel',
Jose Fonsecae01aa3b2015-06-27 11:07:05 +0100815 # GL_KHR_debug (for OpenGL ES)
816 'glDebugMessageControlKHR',
817 'glDebugMessageInsertKHR',
818 'glDebugMessageCallbackKHR',
819 'glGetDebugMessageLogKHR',
820 'glPushDebugGroupKHR',
821 'glPopDebugGroupKHR',
822 'glObjectLabelKHR',
823 'glGetObjectLabelKHR',
824 'glObjectPtrLabelKHR',
825 'glGetObjectPtrLabelKHR',
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700826 # GL_ARB_debug_output
827 'glDebugMessageControlARB',
828 'glDebugMessageInsertARB',
829 'glDebugMessageCallbackARB',
830 'glGetDebugMessageLogARB',
831 # GL_AMD_debug_output
832 'glDebugMessageEnableAMD',
833 'glDebugMessageInsertAMD',
834 'glDebugMessageCallbackAMD',
835 'glGetDebugMessageLogAMD',
José Fonseca71f5a352014-07-28 12:19:50 +0100836 # GL_EXT_debug_label
837 'glLabelObjectEXT',
838 'glGetObjectLabelEXT',
839 # GL_EXT_debug_marker
840 'glInsertEventMarkerEXT',
841 'glPushGroupMarkerEXT',
842 'glPopGroupMarkerEXT',
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -0700843 ]
844
José Fonseca54f304a2012-01-14 19:33:08 +0000845 def invokeFunction(self, function):
José Fonseca91492d22011-05-23 21:20:31 +0100846 if function.name in ('glLinkProgram', 'glLinkProgramARB'):
847 # These functions have been dispatched already
848 return
849
José Fonsecae0c55fd2015-01-26 22:46:13 +0000850 # Force glProgramBinary to fail. Per ARB_get_program_binary this
851 # should signal the app that it needs to recompile.
852 if function.name in ('glProgramBinary', 'glProgramBinaryOES'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100853 print(r' binaryFormat = 0xDEADDEAD;')
854 print(r' binary = &binaryFormat;')
855 print(r' length = sizeof binaryFormat;')
José Fonsecae0c55fd2015-01-26 22:46:13 +0000856
José Fonseca2cfa02c2013-06-10 08:05:29 +0100857 Tracer.invokeFunction(self, function)
858
859 def doInvokeFunction(self, function):
860 # Same as invokeFunction() but called both when trace is enabled or disabled.
861 #
862 # Used to modify the behavior of GL entry-points.
863
864 # Override GL extensions
865 if function.name in ('glGetString', 'glGetIntegerv', 'glGetStringi'):
866 Tracer.doInvokeFunction(self, function, prefix = 'gltrace::_', suffix = '_override')
867 return
868
José Fonseca71f5a352014-07-28 12:19:50 +0100869 # We implement GL_GREMEDY_*, etc., and not the driver
José Fonsecaf028a8f2012-02-15 23:33:35 +0000870 if function.name in self.marker_functions:
José Fonseca8f34d342011-07-15 20:16:40 +0100871 return
872
Peter Lohrmann9d9eb812013-07-12 16:15:25 -0400873 # We may be faking KHR_debug, so ensure the pointer queries result is
874 # always zeroed to prevent dereference of unitialized pointers
875 if function.name == 'glGetPointerv':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100876 print(' if (params &&')
877 print(' (pname == GL_DEBUG_CALLBACK_FUNCTION ||')
878 print(' pname == GL_DEBUG_CALLBACK_USER_PARAM)) {')
879 print(' *params = NULL;')
880 print(' }')
Peter Lohrmann9d9eb812013-07-12 16:15:25 -0400881
José Fonseca2cfa02c2013-06-10 08:05:29 +0100882 if function.name in self.getProcAddressFunctionNames:
José Fonseca631dbd12014-12-15 16:34:45 +0000883 nameArg = function.args[0].name
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100884 print(' if (strcmp("glNotifyMappedBufferRangeVMWX", (const char *)%s) == 0) {' % (nameArg,))
885 print(' _result = (%s)&glNotifyMappedBufferRangeVMWX;' % (function.type,))
José Fonsecaf028a8f2012-02-15 23:33:35 +0000886 for marker_function in self.marker_functions:
José Fonseca1b6c8752012-04-15 14:33:00 +0100887 if self.api.getFunctionByName(marker_function):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100888 print(' } else if (strcmp("%s", (const char *)%s) == 0) {' % (marker_function, nameArg))
889 print(' _result = (%s)&%s;' % (function.type, marker_function))
890 print(' } else {')
José Fonseca2cfa02c2013-06-10 08:05:29 +0100891 Tracer.doInvokeFunction(self, function)
892
893 # Replace function addresses with ours
894 # XXX: Doing this here instead of wrapRet means that the trace will
895 # contain the addresses of the wrapper functions, and not the real
896 # functions, but in practice this should make no difference.
897 if function.name in self.getProcAddressFunctionNames:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100898 print(' _result = _wrapProcAddress(%s, _result);' % (nameArg,))
José Fonseca2cfa02c2013-06-10 08:05:29 +0100899
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100900 print(' }')
José Fonseca1b3d3752011-07-15 10:15:19 +0100901 return
902
José Fonsecae0c55fd2015-01-26 22:46:13 +0000903 if function.name in ('glGetProgramBinary', 'glGetProgramBinaryOES'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100904 print(r' bufSize = 0;')
José Fonsecae0c55fd2015-01-26 22:46:13 +0000905
José Fonseca2cfa02c2013-06-10 08:05:29 +0100906 Tracer.doInvokeFunction(self, function)
José Fonseca91492d22011-05-23 21:20:31 +0100907
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300908 if function.name in ('glMapBufferRange', 'glMapBufferRangeEXT', 'glMapNamedBufferRange', 'glMapNamedBufferRangeEXT'):
909 print(r' if ((access & GL_MAP_COHERENT_BIT) && (access & GL_MAP_WRITE_BIT)) {')
910 print(r' gltrace::Context *_ctx = gltrace::getContext();')
911 if function.name in ('glMapBufferRange', 'glMapBufferRangeEXT'):
912 print(r' GLint buffer = getBufferName(target);')
Robert Tarasov6ccf5bb2020-01-10 12:31:25 -0800913 print(r' auto it = _ctx->sharedRes->bufferToShadowMemory.find(buffer);')
914 print(r' if (it != _ctx->sharedRes->bufferToShadowMemory.end()) {')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300915 print(r' _result = it->second->map(_ctx, _result, access, offset, length);')
916 print(r' } else {')
917 print(r' os::log("apitrace: error: %s: cannot find memory shadow\n", __FUNCTION__);')
918 print(r' }')
919 print(r' }')
920
921 # We should sync back readable coherent buffers only when a fence become signaled
922 # because in any other moment application cannot know if changes from operations on
923 # GPU are done and buffers are updated.
924 if function.name == 'glWaitSync':
925 print(r' gltrace::Context *_ctx = gltrace::getContext();')
Danylo Piliaiev76a24832020-02-07 16:28:53 +0200926 print(r' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300927 print(r' GLMemoryShadow::syncAllForReads(_ctx);')
928
929 if function.name == 'glClientWaitSync':
930 print(r' if (_result == GL_ALREADY_SIGNALED || _result == GL_CONDITION_SATISFIED) {')
931 print(r' gltrace::Context *_ctx = gltrace::getContext();')
Danylo Piliaiev76a24832020-02-07 16:28:53 +0200932 print(r' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300933 print(r' GLMemoryShadow::syncAllForReads(_ctx);')
934 print(r' }')
935
936 if function.name == 'glGetSynciv':
937 print(r' if (pname == GL_SYNC_STATUS && bufSize > 0 && values[0] == GL_SIGNALED) {')
938 print(r' gltrace::Context *_ctx = gltrace::getContext();')
Danylo Piliaiev76a24832020-02-07 16:28:53 +0200939 print(r' GLMemoryShadow::commitAllWrites(_ctx, trace::fakeMemcpy);')
Danylo Piliaiev9f18e5d2019-07-03 11:12:50 +0300940 print(r' GLMemoryShadow::syncAllForReads(_ctx);')
941 print(r' }')
942
José Fonsecae0c55fd2015-01-26 22:46:13 +0000943 if function.name == 'glGetProgramiv':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100944 print(r' if (params && pname == GL_PROGRAM_BINARY_LENGTH) {')
945 print(r' *params = 0;')
946 print(r' }')
José Fonsecae0c55fd2015-01-26 22:46:13 +0000947 if function.name in ('glGetProgramBinary', 'glGetProgramBinaryOES'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100948 print(r' if (length) {')
949 print(r' *length = 0;')
950 print(r' }')
José Fonsecae0c55fd2015-01-26 22:46:13 +0000951
José Fonseca54f304a2012-01-14 19:33:08 +0000952 def wrapRet(self, function, instance):
953 Tracer.wrapRet(self, function, instance)
José Fonseca1b3d3752011-07-15 10:15:19 +0100954
José Fonsecacdc322c2012-02-29 19:29:51 +0000955 # Keep track of buffer mappings
Jose Fonsecad2fb3402015-01-24 13:42:52 +0000956 if function.name in ('glMapBufferRange', 'glMapBufferRangeEXT'):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100957 print(' if (access & GL_MAP_WRITE_BIT) {')
958 print(' _checkBufferMapRange = true;')
959 print(' }')
José Fonseca669b1222011-02-20 09:05:10 +0000960
José Fonsecac9f12232011-03-25 20:07:42 +0000961 boolean_names = [
962 'GL_FALSE',
963 'GL_TRUE',
964 ]
965
966 def gl_boolean(self, value):
967 return self.boolean_names[int(bool(value))]
968
José Fonsecaa442a462014-11-12 21:16:22 +0000969 # Regular expression for the names of the functions that unpack from a
970 # pixel buffer object. See the ARB_pixel_buffer_object specification.
971 unpack_function_regex = re.compile(r'^gl(' + r'|'.join([
972 r'Bitmap',
973 r'PolygonStipple',
974 r'PixelMap[a-z]+v',
975 r'DrawPixels',
976 r'Color(Sub)?Table',
977 r'(Convolution|Separable)Filter[12]D',
978 r'(Compressed)?(Multi)?Tex(ture)?(Sub)?Image[1-4]D',
979 ]) + r')[0-9A-Z]*$')
José Fonsecae97bab92011-06-02 23:15:11 +0100980
José Fonseca54f304a2012-01-14 19:33:08 +0000981 def serializeArgValue(self, function, arg):
José Fonsecae97bab92011-06-02 23:15:11 +0100982 # Recognize offsets instead of blobs when a PBO is bound
José Fonsecaa442a462014-11-12 21:16:22 +0000983 if self.unpack_function_regex.match(function.name) \
José Fonsecae97bab92011-06-02 23:15:11 +0100984 and (isinstance(arg.type, stdapi.Blob) \
985 or (isinstance(arg.type, stdapi.Const) \
986 and isinstance(arg.type.type, stdapi.Blob))):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100987 print(' {')
988 print(' gltrace::Context *_ctx = gltrace::getContext();')
989 print(' GLint _unpack_buffer = 0;')
990 print(' if (_ctx->features.pixel_buffer_object)')
991 print(' _glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &_unpack_buffer);')
992 print(' if (_unpack_buffer) {')
993 print(' trace::localWriter.writePointer((uintptr_t)%s);' % arg.name)
994 print(' } else {')
José Fonseca54f304a2012-01-14 19:33:08 +0000995 Tracer.serializeArgValue(self, function, arg)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100996 print(' }')
997 print(' }')
José Fonsecae97bab92011-06-02 23:15:11 +0100998 return
999
Jose Fonseca0a676c52016-04-04 23:24:32 +01001000 # Recognize offsets instead of pointers when query buffer is bound
1001 if function.name.startswith('glGetQueryObject') and arg.output:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001002 print(r' gltrace::Context *_ctx = gltrace::getContext();')
1003 print(r' GLint _query_buffer = 0;')
1004 print(r' if (_ctx->features.query_buffer_object) {')
1005 print(r' _query_buffer = _glGetInteger(GL_QUERY_BUFFER_BINDING);')
1006 print(r' }')
1007 print(r' if (_query_buffer) {')
1008 print(r' trace::localWriter.writePointer((uintptr_t)%s);' % arg.name)
1009 print(r' } else {')
Jose Fonseca0a676c52016-04-04 23:24:32 +01001010 Tracer.serializeArgValue(self, function, arg)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001011 print(r' }')
Jose Fonseca0a676c52016-04-04 23:24:32 +01001012 return
1013
José Fonsecaa3f89ae2011-04-26 08:50:32 +01001014 # Several GL state functions take GLenum symbolic names as
1015 # integer/floats; so dump the symbolic name whenever possible
José Fonseca3bcb33c2011-05-27 20:14:31 +01001016 if function.name.startswith('gl') \
José Fonsecae3571092011-10-13 08:26:27 +01001017 and arg.type in (glapi.GLint, glapi.GLfloat, glapi.GLdouble) \
José Fonseca3bcb33c2011-05-27 20:14:31 +01001018 and arg.name == 'param':
José Fonsecaa3f89ae2011-04-26 08:50:32 +01001019 assert arg.index > 0
1020 assert function.args[arg.index - 1].name == 'pname'
1021 assert function.args[arg.index - 1].type == glapi.GLenum
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001022 print(' if (is_symbolic_pname(pname) && is_symbolic_param(%s)) {' % arg.name)
José Fonseca54f304a2012-01-14 19:33:08 +00001023 self.serializeValue(glapi.GLenum, arg.name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001024 print(' } else {')
José Fonseca54f304a2012-01-14 19:33:08 +00001025 Tracer.serializeArgValue(self, function, arg)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001026 print(' }')
José Fonsecaa3f89ae2011-04-26 08:50:32 +01001027 return
1028
José Fonseca54f304a2012-01-14 19:33:08 +00001029 Tracer.serializeArgValue(self, function, arg)
José Fonseca99221832011-03-22 22:15:46 +00001030
Jose Fonseca27b8d812016-05-13 07:09:09 -07001031 fake_function_names = [
1032 'glBindAttribLocation',
1033 'glBindAttribLocationARB',
1034 'glBindBuffer',
1035 'glBitmap',
1036 'glClientActiveTexture',
1037 'glDisableClientState',
1038 'glEnableClientState',
1039 'glEndList',
1040 'glNewList',
1041 'glScissor',
1042 'glStringMarkerGREMEDY',
1043 'glTexImage2D',
1044 'glViewport',
1045 ]
1046
José Fonseca4c938c22011-04-30 22:44:38 +01001047 def footer(self, api):
1048 Tracer.footer(self, api)
José Fonseca669b1222011-02-20 09:05:10 +00001049
Jose Fonseca27b8d812016-05-13 07:09:09 -07001050 # Generate helper functions to emit fake function calls into the trace
1051 for function in api.getAllFunctions():
1052 if function.name in self.fake_function_names:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001053 print(function.prototype('_fake_' + function.name))
1054 print(r'{')
Jose Fonseca27b8d812016-05-13 07:09:09 -07001055 self.fake_call(function, function.argNames())
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001056 print(r'}')
1057 print()
Jose Fonseca27b8d812016-05-13 07:09:09 -07001058
José Fonseca4c938c22011-04-30 22:44:38 +01001059 # A simple state tracker to track the pointer values
José Fonseca669b1222011-02-20 09:05:10 +00001060 # update the state
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001061 print('static void _trace_user_arrays(gltrace::Context *_ctx, GLuint count)')
1062 print('{')
1063 print(' glfeatures::Profile profile = _ctx->profile;')
1064 print(' bool es1 = profile.es() && profile.major == 1;')
1065 print()
José Fonseca8d1408b2014-02-03 19:57:18 +00001066
Jose Fonseca97d22a72016-05-10 04:47:08 -07001067 # Some apps, in particular Quake3, can tell the driver to lock more
1068 # vertices than those actually required for the draw call.
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001069 print(' count = std::max(count, _ctx->lockedArrayCount);')
1070 print()
Jose Fonseca97d22a72016-05-10 04:47:08 -07001071
José Fonseca8d1408b2014-02-03 19:57:18 +00001072 # Temporarily unbind the array buffer
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001073 print(' GLint _array_buffer = _glGetInteger(GL_ARRAY_BUFFER_BINDING);')
1074 print(' if (_array_buffer) {')
1075 print(' _fake_glBindBuffer(GL_ARRAY_BUFFER, 0);')
1076 print(' }')
1077 print()
José Fonseca1a2fdd22011-04-01 00:55:09 +01001078
José Fonseca99221832011-03-22 22:15:46 +00001079 for camelcase_name, uppercase_name in self.arrays:
Chia-I Wub3d218d2011-11-03 01:37:36 +08001080 # in which profile is the array available?
José Fonseca34ea6162015-01-08 23:45:43 +00001081 profile_check = 'profile.desktop()'
Chia-I Wub3d218d2011-11-03 01:37:36 +08001082 if camelcase_name in self.arrays_es1:
José Fonsecab0c59722015-01-05 20:45:41 +00001083 profile_check = '(' + profile_check + ' || es1)';
Chia-I Wub3d218d2011-11-03 01:37:36 +08001084
José Fonseca99221832011-03-22 22:15:46 +00001085 function_name = 'gl%sPointer' % camelcase_name
1086 enable_name = 'GL_%s_ARRAY' % uppercase_name
1087 binding_name = 'GL_%s_ARRAY_BUFFER_BINDING' % uppercase_name
José Fonseca1b6c8752012-04-15 14:33:00 +01001088 function = api.getFunctionByName(function_name)
José Fonseca99221832011-03-22 22:15:46 +00001089
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001090 print(' // %s' % function.prototype())
1091 print(' if (%s) {' % profile_check)
José Fonsecafb6744f2011-04-15 11:18:37 +01001092 self.array_trace_prolog(api, uppercase_name)
1093 self.array_prolog(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001094 print(' if (_glIsEnabled(%s)) {' % enable_name)
1095 print(' GLint _binding = _glGetInteger(%s);' % binding_name)
1096 print(' if (!_binding) {')
José Fonseca99221832011-03-22 22:15:46 +00001097
1098 # Get the arguments via glGet*
1099 for arg in function.args:
1100 arg_get_enum = 'GL_%s_ARRAY_%s' % (uppercase_name, arg.name.upper())
1101 arg_get_function, arg_type = TypeGetter().visit(arg.type)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001102 print(' %s %s = 0;' % (arg_type, arg.name))
1103 print(' _%s(%s, &%s);' % (arg_get_function, arg_get_enum, arg.name))
José Fonseca99221832011-03-22 22:15:46 +00001104
1105 arg_names = ', '.join([arg.name for arg in function.args[:-1]])
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001106 print(' size_t _size = _%s_size(%s, count);' % (function.name, arg_names))
José Fonseca99221832011-03-22 22:15:46 +00001107
1108 # Emit a fake function
José Fonsecafb6744f2011-04-15 11:18:37 +01001109 self.array_trace_intermezzo(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001110 print(' unsigned _call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,))
José Fonseca669b1222011-02-20 09:05:10 +00001111 for arg in function.args:
1112 assert not arg.output
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001113 print(' trace::localWriter.beginArg(%u);' % (arg.index,))
José Fonseca14c21bc2011-02-20 23:32:22 +00001114 if arg.name != 'pointer':
José Fonseca54f304a2012-01-14 19:33:08 +00001115 self.serializeValue(arg.type, arg.name)
José Fonseca14c21bc2011-02-20 23:32:22 +00001116 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001117 print(' trace::localWriter.writeBlob((const void *)%s, _size);' % (arg.name))
1118 print(' trace::localWriter.endArg();')
José Fonseca99221832011-03-22 22:15:46 +00001119
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001120 print(' trace::localWriter.endEnter();')
1121 print(' trace::localWriter.beginLeave(_call);')
1122 print(' trace::localWriter.endLeave();')
1123 print(' }')
1124 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +01001125 self.array_epilog(api, uppercase_name)
1126 self.array_trace_epilog(api, uppercase_name)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001127 print(' }')
1128 print()
José Fonseca1a2fdd22011-04-01 00:55:09 +01001129
José Fonseca1601c412011-05-10 10:38:19 +01001130 # Samething, but for glVertexAttribPointer*
1131 #
1132 # Some variants of glVertexAttribPointer alias conventional and generic attributes:
1133 # - glVertexAttribPointer: no
1134 # - glVertexAttribPointerARB: implementation dependent
1135 # - glVertexAttribPointerNV: yes
1136 #
1137 # This means that the implementations of these functions do not always
1138 # alias, and they need to be considered independently.
1139 #
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001140 print(' // ES1 does not support generic vertex attributes')
1141 print(' if (es1)')
1142 print(' return;')
1143 print()
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001144
1145 function_name = 'glVertexAttribPointer'
1146 function = api.getFunctionByName(function_name)
1147
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001148 print(' // %s' % function.prototype())
1149 print(' GLint _max_vertex_attribs = _glGetInteger(GL_MAX_VERTEX_ATTRIBS);')
1150 print(' for (GLint index = 0; index < _max_vertex_attribs; ++index) {')
1151 print(' GLint _enabled = 0;')
1152 print(' _glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &_enabled);')
1153 print(' if (_enabled) {')
1154 print(' GLint _binding = 0;')
1155 print(' _glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &_binding);')
1156 print(' if (!_binding) {')
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001157
1158 # Get the arguments via glGet*
1159 for arg in function.args[1:]:
1160 arg_get_enum = 'GL_VERTEX_ATTRIB_ARRAY_%s' % (arg.name.upper())
1161 arg_get_function, arg_type = TypeGetter('glGetVertexAttrib', False).visit(arg.type)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001162 print(' %s %s = 0;' % (arg_type, arg.name))
1163 print(' _%s(index, %s, &%s);' % (arg_get_function, arg_get_enum, arg.name))
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001164
1165 arg_names = ', '.join([arg.name for arg in function.args[1:-1]])
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001166 print(' size_t _size = _%s_size(%s, count);' % (function.name, arg_names))
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001167
1168 # Emit a fake function
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001169 print(' unsigned _call = trace::localWriter.beginEnter(&_%s_sig, true);' % (function.name,))
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001170 for arg in function.args:
1171 assert not arg.output
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001172 print(' trace::localWriter.beginArg(%u);' % (arg.index,))
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001173 if arg.name != 'pointer':
1174 self.serializeValue(arg.type, arg.name)
1175 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001176 print(' trace::localWriter.writeBlob((const void *)%s, _size);' % (arg.name))
1177 print(' trace::localWriter.endArg();')
Jose Fonsecaef45b1f2016-05-08 23:31:24 +01001178
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001179 print(' trace::localWriter.endEnter();')
1180 print(' trace::localWriter.beginLeave(_call);')
1181 print(' trace::localWriter.endLeave();')
1182 print(' }')
1183 print(' }')
1184 print(' }')
1185 print()
José Fonseca1a2fdd22011-04-01 00:55:09 +01001186
José Fonseca8d1408b2014-02-03 19:57:18 +00001187 # Restore the original array_buffer
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001188 print(' if (_array_buffer) {')
1189 print(' _fake_glBindBuffer(GL_ARRAY_BUFFER, _array_buffer);')
1190 print(' }')
1191 print()
José Fonseca8d1408b2014-02-03 19:57:18 +00001192
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001193 print('}')
1194 print()
José Fonseca669b1222011-02-20 09:05:10 +00001195
José Fonsecafb6744f2011-04-15 11:18:37 +01001196 #
1197 # Hooks for glTexCoordPointer, which is identical to the other array
1198 # pointers except the fact that it is indexed by glClientActiveTexture.
1199 #
1200
1201 def array_prolog(self, api, uppercase_name):
1202 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001203 print(' GLint max_units = 0;')
1204 print(' if (_ctx->profile.desktop())')
1205 print(' _glGetIntegerv(GL_MAX_TEXTURE_COORDS, &max_units);')
1206 print(' else')
1207 print(' _glGetIntegerv(GL_MAX_TEXTURE_UNITS, &max_units);')
1208 print(' GLint client_active_texture = GL_TEXTURE0;')
1209 print(' if (max_units > 0) {')
1210 print(' _glGetIntegerv(GL_CLIENT_ACTIVE_TEXTURE, &client_active_texture);')
1211 print(' }')
1212 print(' GLint unit = 0;')
1213 print(' do {')
1214 print(' GLint texture = GL_TEXTURE0 + unit;')
1215 print(' if (max_units > 0) {')
1216 print(' _glClientActiveTexture(texture);')
1217 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +01001218
1219 def array_trace_prolog(self, api, uppercase_name):
1220 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001221 print(' bool client_active_texture_dirty = false;')
José Fonsecafb6744f2011-04-15 11:18:37 +01001222
1223 def array_epilog(self, api, uppercase_name):
1224 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001225 print(' } while (++unit < max_units);')
José Fonsecafb6744f2011-04-15 11:18:37 +01001226 self.array_cleanup(api, uppercase_name)
1227
1228 def array_cleanup(self, api, uppercase_name):
1229 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001230 print(' if (max_units > 0) {')
1231 print(' _glClientActiveTexture(client_active_texture);')
1232 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +01001233
1234 def array_trace_intermezzo(self, api, uppercase_name):
1235 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001236 print(' if (texture != client_active_texture || client_active_texture_dirty) {')
1237 print(' client_active_texture_dirty = true;')
1238 print(' _fake_glClientActiveTexture(texture);')
1239 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +01001240
1241 def array_trace_epilog(self, api, uppercase_name):
1242 if uppercase_name == 'TEXTURE_COORD':
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001243 print(' if (client_active_texture_dirty) {')
1244 print(' _fake_glClientActiveTexture(client_active_texture);')
1245 print(' }')
José Fonsecafb6744f2011-04-15 11:18:37 +01001246
José Fonseca151c3702013-05-10 08:28:15 +01001247 def emitFakeTexture2D(self):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +01001248 print(r' _fake_glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels);')
José Fonseca669b1222011-02-20 09:05:10 +00001249