blob: 97d7df00604137f017f5ed23aaa4a3bec6eeca59 [file] [log] [blame]
Mark Lobodzinskiff910992016-10-11 14:29:52 -06001#!/usr/bin/python3 -i
2#
3# Copyright (c) 2015-2016 The Khronos Group Inc.
4# Copyright (c) 2015-2016 Valve Corporation
5# Copyright (c) 2015-2016 LunarG, Inc.
6# Copyright (c) 2015-2016 Google Inc.
7#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20# Author: Mike Stroyan <stroyan@google.com>
21
22import os,re,sys
23from generator import *
24
25# ThreadGeneratorOptions - subclass of GeneratorOptions.
26#
27# Adds options used by ThreadOutputGenerator objects during threading
28# layer generation.
29#
30# Additional members
31# prefixText - list of strings to prefix generated header with
32# (usually a copyright statement + calling convention macros).
33# protectFile - True if multiple inclusion protection should be
34# generated (based on the filename) around the entire header.
35# protectFeature - True if #ifndef..#endif protection should be
36# generated around a feature interface in the header file.
37# genFuncPointers - True if function pointer typedefs should be
38# generated
39# protectProto - If conditional protection should be generated
40# around prototype declarations, set to either '#ifdef'
41# to require opt-in (#ifdef protectProtoStr) or '#ifndef'
42# to require opt-out (#ifndef protectProtoStr). Otherwise
43# set to None.
44# protectProtoStr - #ifdef/#ifndef symbol to use around prototype
45# declarations, if protectProto is set
46# apicall - string to use for the function declaration prefix,
47# such as APICALL on Windows.
48# apientry - string to use for the calling convention macro,
49# in typedefs, such as APIENTRY.
50# apientryp - string to use for the calling convention macro
51# in function pointer typedefs, such as APIENTRYP.
52# indentFuncProto - True if prototype declarations should put each
53# parameter on a separate line
54# indentFuncPointer - True if typedefed function pointers should put each
55# parameter on a separate line
56# alignFuncParam - if nonzero and parameters are being put on a
57# separate line, align parameter names at the specified column
58class ThreadGeneratorOptions(GeneratorOptions):
59 def __init__(self,
60 filename = None,
61 directory = '.',
62 apiname = None,
63 profile = None,
64 versions = '.*',
65 emitversions = '.*',
66 defaultExtensions = None,
67 addExtensions = None,
68 removeExtensions = None,
69 sortProcedure = regSortFeatures,
70 prefixText = "",
71 genFuncPointers = True,
72 protectFile = True,
73 protectFeature = True,
74 protectProto = None,
75 protectProtoStr = None,
76 apicall = '',
77 apientry = '',
78 apientryp = '',
79 indentFuncProto = True,
80 indentFuncPointer = False,
81 alignFuncParam = 0):
82 GeneratorOptions.__init__(self, filename, directory, apiname, profile,
83 versions, emitversions, defaultExtensions,
84 addExtensions, removeExtensions, sortProcedure)
85 self.prefixText = prefixText
86 self.genFuncPointers = genFuncPointers
87 self.protectFile = protectFile
88 self.protectFeature = protectFeature
89 self.protectProto = protectProto
90 self.protectProtoStr = protectProtoStr
91 self.apicall = apicall
92 self.apientry = apientry
93 self.apientryp = apientryp
94 self.indentFuncProto = indentFuncProto
95 self.indentFuncPointer = indentFuncPointer
96 self.alignFuncParam = alignFuncParam
97
98# ThreadOutputGenerator - subclass of OutputGenerator.
99# Generates Thread checking framework
100#
101# ---- methods ----
102# ThreadOutputGenerator(errFile, warnFile, diagFile) - args as for
103# OutputGenerator. Defines additional internal state.
104# ---- methods overriding base class ----
105# beginFile(genOpts)
106# endFile()
107# beginFeature(interface, emit)
108# endFeature()
109# genType(typeinfo,name)
110# genStruct(typeinfo,name)
111# genGroup(groupinfo,name)
112# genEnum(enuminfo, name)
113# genCmd(cmdinfo)
114class ThreadOutputGenerator(OutputGenerator):
115 """Generate specified API interfaces in a specific style, such as a C header"""
116 # This is an ordered list of sections in the header file.
117 TYPE_SECTIONS = ['include', 'define', 'basetype', 'handle', 'enum',
118 'group', 'bitmask', 'funcpointer', 'struct']
119 ALL_SECTIONS = TYPE_SECTIONS + ['command']
120 def __init__(self,
121 errFile = sys.stderr,
122 warnFile = sys.stderr,
123 diagFile = sys.stdout):
124 OutputGenerator.__init__(self, errFile, warnFile, diagFile)
125 # Internal state - accumulators for different inner block text
126 self.sections = dict([(section, []) for section in self.ALL_SECTIONS])
127 self.intercepts = []
128
129 # Check if the parameter passed in is a pointer to an array
130 def paramIsArray(self, param):
131 return param.attrib.get('len') is not None
132
133 # Check if the parameter passed in is a pointer
134 def paramIsPointer(self, param):
135 ispointer = False
136 for elem in param:
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600137 if ((elem.tag is not 'type') and (elem.tail is not None)) and '*' in elem.tail:
138 ispointer = True
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600139 return ispointer
Mark Lobodzinski60b77b32017-02-14 09:16:56 -0700140
141 # Check if an object is a non-dispatchable handle
142 def isHandleTypeNonDispatchable(self, handletype):
143 handle = self.registry.tree.find("types/type/[name='" + handletype + "'][@category='handle']")
144 if handle is not None and handle.find('type').text == 'VK_DEFINE_NON_DISPATCHABLE_HANDLE':
145 return True
146 else:
147 return False
148
149 # Check if an object is a dispatchable handle
150 def isHandleTypeDispatchable(self, handletype):
151 handle = self.registry.tree.find("types/type/[name='" + handletype + "'][@category='handle']")
152 if handle is not None and handle.find('type').text == 'VK_DEFINE_HANDLE':
153 return True
154 else:
155 return False
156
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600157 def makeThreadUseBlock(self, cmd, functionprefix):
158 """Generate C function pointer typedef for <command> Element"""
159 paramdecl = ''
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600160 # Find and add any parameters that are thread unsafe
161 params = cmd.findall('param')
162 for param in params:
163 paramname = param.find('name')
164 if False: # self.paramIsPointer(param):
165 paramdecl += ' // not watching use of pointer ' + paramname.text + '\n'
166 else:
167 externsync = param.attrib.get('externsync')
168 if externsync == 'true':
169 if self.paramIsArray(param):
170 paramdecl += ' for (uint32_t index=0;index<' + param.attrib.get('len') + ';index++) {\n'
171 paramdecl += ' ' + functionprefix + 'WriteObject(my_data, ' + paramname.text + '[index]);\n'
172 paramdecl += ' }\n'
173 else:
174 paramdecl += ' ' + functionprefix + 'WriteObject(my_data, ' + paramname.text + ');\n'
175 elif (param.attrib.get('externsync')):
176 if self.paramIsArray(param):
177 # Externsync can list pointers to arrays of members to synchronize
178 paramdecl += ' for (uint32_t index=0;index<' + param.attrib.get('len') + ';index++) {\n'
179 for member in externsync.split(","):
180 # Replace first empty [] in member name with index
181 element = member.replace('[]','[index]',1)
182 if '[]' in element:
183 # Replace any second empty [] in element name with
184 # inner array index based on mapping array names like
185 # "pSomeThings[]" to "someThingCount" array size.
186 # This could be more robust by mapping a param member
187 # name to a struct type and "len" attribute.
188 limit = element[0:element.find('s[]')] + 'Count'
189 dotp = limit.rfind('.p')
190 limit = limit[0:dotp+1] + limit[dotp+2:dotp+3].lower() + limit[dotp+3:]
191 paramdecl += ' for(uint32_t index2=0;index2<'+limit+';index2++)\n'
192 element = element.replace('[]','[index2]')
193 paramdecl += ' ' + functionprefix + 'WriteObject(my_data, ' + element + ');\n'
194 paramdecl += ' }\n'
195 else:
196 # externsync can list members to synchronize
197 for member in externsync.split(","):
198 member = str(member).replace("::", "->")
Mark Lobodzinski9c147802017-02-10 08:34:54 -0700199 member = str(member).replace(".", "->")
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600200 paramdecl += ' ' + functionprefix + 'WriteObject(my_data, ' + member + ');\n'
201 else:
202 paramtype = param.find('type')
203 if paramtype is not None:
204 paramtype = paramtype.text
205 else:
206 paramtype = 'None'
Mark Lobodzinski60b77b32017-02-14 09:16:56 -0700207 if (self.isHandleTypeDispatchable(paramtype) or self.isHandleTypeNonDispatchable(paramtype)) and paramtype != 'VkPhysicalDevice':
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600208 if self.paramIsArray(param) and ('pPipelines' != paramname.text):
Mark Lobodzinski9c147802017-02-10 08:34:54 -0700209 # Add pointer dereference for array counts that are pointer values
210 dereference = ''
211 for candidate in params:
212 if param.attrib.get('len') == candidate.find('name').text:
213 if self.paramIsPointer(candidate):
214 dereference = '*'
Mark Lobodzinski60b77b32017-02-14 09:16:56 -0700215 param_len = str(param.attrib.get('len')).replace("::", "->")
216 paramdecl += ' for (uint32_t index = 0; index < ' + dereference + param_len + '; index++) {\n'
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600217 paramdecl += ' ' + functionprefix + 'ReadObject(my_data, ' + paramname.text + '[index]);\n'
218 paramdecl += ' }\n'
219 elif not self.paramIsPointer(param):
220 # Pointer params are often being created.
221 # They are not being read from.
222 paramdecl += ' ' + functionprefix + 'ReadObject(my_data, ' + paramname.text + ');\n'
223 explicitexternsyncparams = cmd.findall("param[@externsync]")
224 if (explicitexternsyncparams is not None):
225 for param in explicitexternsyncparams:
226 externsyncattrib = param.attrib.get('externsync')
227 paramname = param.find('name')
228 paramdecl += ' // Host access to '
229 if externsyncattrib == 'true':
230 if self.paramIsArray(param):
231 paramdecl += 'each member of ' + paramname.text
232 elif self.paramIsPointer(param):
233 paramdecl += 'the object referenced by ' + paramname.text
234 else:
235 paramdecl += paramname.text
236 else:
237 paramdecl += externsyncattrib
238 paramdecl += ' must be externally synchronized\n'
239
240 # Find and add any "implicit" parameters that are thread unsafe
241 implicitexternsyncparams = cmd.find('implicitexternsyncparams')
242 if (implicitexternsyncparams is not None):
243 for elem in implicitexternsyncparams:
244 paramdecl += ' // '
245 paramdecl += elem.text
246 paramdecl += ' must be externally synchronized between host accesses\n'
247
248 if (paramdecl == ''):
249 return None
250 else:
251 return paramdecl
252 def beginFile(self, genOpts):
253 OutputGenerator.beginFile(self, genOpts)
254 # C-specific
255 #
256 # Multiple inclusion protection & C++ namespace.
257 if (genOpts.protectFile and self.genOpts.filename):
258 headerSym = '__' + re.sub('\.h', '_h_', os.path.basename(self.genOpts.filename))
259 write('#ifndef', headerSym, file=self.outFile)
260 write('#define', headerSym, '1', file=self.outFile)
261 self.newline()
262 write('namespace threading {', file=self.outFile)
263 self.newline()
264 #
265 # User-supplied prefix text, if any (list of strings)
266 if (genOpts.prefixText):
267 for s in genOpts.prefixText:
268 write(s, file=self.outFile)
269 def endFile(self):
270 # C-specific
271 # Finish C++ namespace and multiple inclusion protection
272 self.newline()
273 # record intercepted procedures
274 write('// intercepts', file=self.outFile)
275 write('struct { const char* name; PFN_vkVoidFunction pFunc;} procmap[] = {', file=self.outFile)
276 write('\n'.join(self.intercepts), file=self.outFile)
277 write('};\n', file=self.outFile)
278 self.newline()
279 write('} // namespace threading', file=self.outFile)
280 if (self.genOpts.protectFile and self.genOpts.filename):
281 self.newline()
282 write('#endif', file=self.outFile)
283 # Finish processing in superclass
284 OutputGenerator.endFile(self)
285 def beginFeature(self, interface, emit):
286 #write('// starting beginFeature', file=self.outFile)
287 # Start processing in superclass
288 OutputGenerator.beginFeature(self, interface, emit)
289 # C-specific
290 # Accumulate includes, defines, types, enums, function pointer typedefs,
291 # end function prototypes separately for this feature. They're only
292 # printed in endFeature().
293 self.sections = dict([(section, []) for section in self.ALL_SECTIONS])
294 #write('// ending beginFeature', file=self.outFile)
295 def endFeature(self):
296 # C-specific
297 # Actually write the interface to the output file.
298 #write('// starting endFeature', file=self.outFile)
299 if (self.emit):
300 self.newline()
301 if (self.genOpts.protectFeature):
302 write('#ifndef', self.featureName, file=self.outFile)
303 # If type declarations are needed by other features based on
304 # this one, it may be necessary to suppress the ExtraProtect,
305 # or move it below the 'for section...' loop.
306 #write('// endFeature looking at self.featureExtraProtect', file=self.outFile)
307 if (self.featureExtraProtect != None):
308 write('#ifdef', self.featureExtraProtect, file=self.outFile)
309 #write('#define', self.featureName, '1', file=self.outFile)
310 for section in self.TYPE_SECTIONS:
311 #write('// endFeature writing section'+section, file=self.outFile)
312 contents = self.sections[section]
313 if contents:
314 write('\n'.join(contents), file=self.outFile)
315 self.newline()
316 #write('// endFeature looking at self.sections[command]', file=self.outFile)
317 if (self.sections['command']):
Jamie Madill24aa9742016-12-13 17:02:57 -0500318 write('\n'.join(self.sections['command']), end=u'', file=self.outFile)
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600319 self.newline()
320 if (self.featureExtraProtect != None):
321 write('#endif /*', self.featureExtraProtect, '*/', file=self.outFile)
322 if (self.genOpts.protectFeature):
323 write('#endif /*', self.featureName, '*/', file=self.outFile)
324 # Finish processing in superclass
325 OutputGenerator.endFeature(self)
326 #write('// ending endFeature', file=self.outFile)
327 #
328 # Append a definition to the specified section
329 def appendSection(self, section, text):
330 # self.sections[section].append('SECTION: ' + section + '\n')
331 self.sections[section].append(text)
332 #
333 # Type generation
334 def genType(self, typeinfo, name):
335 pass
336 #
337 # Struct (e.g. C "struct" type) generation.
338 # This is a special case of the <type> tag where the contents are
339 # interpreted as a set of <member> tags instead of freeform C
340 # C type declarations. The <member> tags are just like <param>
341 # tags - they are a declaration of a struct or union member.
342 # Only simple member declarations are supported (no nested
343 # structs etc.)
344 def genStruct(self, typeinfo, typeName):
345 OutputGenerator.genStruct(self, typeinfo, typeName)
346 body = 'typedef ' + typeinfo.elem.get('category') + ' ' + typeName + ' {\n'
347 # paramdecl = self.makeCParamDecl(typeinfo.elem, self.genOpts.alignFuncParam)
348 for member in typeinfo.elem.findall('.//member'):
349 body += self.makeCParamDecl(member, self.genOpts.alignFuncParam)
350 body += ';\n'
351 body += '} ' + typeName + ';\n'
352 self.appendSection('struct', body)
353 #
354 # Group (e.g. C "enum" type) generation.
355 # These are concatenated together with other types.
356 def genGroup(self, groupinfo, groupName):
357 pass
358 # Enumerant generation
359 # <enum> tags may specify their values in several ways, but are usually
360 # just integers.
361 def genEnum(self, enuminfo, name):
362 pass
363 #
364 # Command generation
365 def genCmd(self, cmdinfo, name):
366 # Commands shadowed by interface functions and are not implemented
367 interface_functions = [
368 'vkEnumerateInstanceLayerProperties',
369 'vkEnumerateInstanceExtensionProperties',
370 'vkEnumerateDeviceLayerProperties',
371 ]
372 if name in interface_functions:
373 return
374 special_functions = [
375 'vkGetDeviceProcAddr',
376 'vkGetInstanceProcAddr',
377 'vkCreateDevice',
378 'vkDestroyDevice',
379 'vkCreateInstance',
380 'vkDestroyInstance',
381 'vkAllocateCommandBuffers',
382 'vkFreeCommandBuffers',
383 'vkCreateDebugReportCallbackEXT',
384 'vkDestroyDebugReportCallbackEXT',
Mark Lobodzinski3bd82ad2017-02-16 11:45:27 -0700385 'vkAllocateDescriptorSets',
Mark Lobodzinskidf47c5a2017-02-28 15:09:31 -0700386 'vkGetSwapchainImagesKHR',
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600387 ]
388 if name in special_functions:
389 decls = self.makeCDecls(cmdinfo.elem)
390 self.appendSection('command', '')
391 self.appendSection('command', '// declare only')
392 self.appendSection('command', decls[0])
393 self.intercepts += [ ' {"%s", reinterpret_cast<PFN_vkVoidFunction>(%s)},' % (name,name[2:]) ]
394 return
Mark Lobodzinski9c147802017-02-10 08:34:54 -0700395 if "QueuePresentKHR" in name or ("DebugMarker" in name and "EXT" in name):
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600396 self.appendSection('command', '// TODO - not wrapping EXT function ' + name)
397 return
398 # Determine first if this function needs to be intercepted
399 startthreadsafety = self.makeThreadUseBlock(cmdinfo.elem, 'start')
400 if startthreadsafety is None:
401 return
402 finishthreadsafety = self.makeThreadUseBlock(cmdinfo.elem, 'finish')
403 # record that the function will be intercepted
404 if (self.featureExtraProtect != None):
405 self.intercepts += [ '#ifdef %s' % self.featureExtraProtect ]
406 self.intercepts += [ ' {"%s", reinterpret_cast<PFN_vkVoidFunction>(%s)},' % (name,name[2:]) ]
407 if (self.featureExtraProtect != None):
408 self.intercepts += [ '#endif' ]
409
410 OutputGenerator.genCmd(self, cmdinfo, name)
411 #
412 decls = self.makeCDecls(cmdinfo.elem)
413 self.appendSection('command', '')
414 self.appendSection('command', decls[0][:-1])
415 self.appendSection('command', '{')
416 # setup common to call wrappers
417 # first parameter is always dispatchable
418 dispatchable_type = cmdinfo.elem.find('param/type').text
419 dispatchable_name = cmdinfo.elem.find('param/name').text
420 self.appendSection('command', ' dispatch_key key = get_dispatch_key('+dispatchable_name+');')
Tobin Ehlis8d6acde2017-02-08 07:40:40 -0700421 self.appendSection('command', ' layer_data *my_data = GetLayerDataPtr(key, layer_data_map);')
Mark Lobodzinskiff910992016-10-11 14:29:52 -0600422 if dispatchable_type in ["VkPhysicalDevice", "VkInstance"]:
423 self.appendSection('command', ' VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;')
424 else:
425 self.appendSection('command', ' VkLayerDispatchTable *pTable = my_data->device_dispatch_table;')
426 # Declare result variable, if any.
427 resulttype = cmdinfo.elem.find('proto/type')
428 if (resulttype != None and resulttype.text == 'void'):
429 resulttype = None
430 if (resulttype != None):
431 self.appendSection('command', ' ' + resulttype.text + ' result;')
432 assignresult = 'result = '
433 else:
434 assignresult = ''
435
436 self.appendSection('command', ' bool threadChecks = startMultiThread();')
437 self.appendSection('command', ' if (threadChecks) {')
438 self.appendSection('command', " "+"\n ".join(str(startthreadsafety).rstrip().split("\n")))
439 self.appendSection('command', ' }')
440 params = cmdinfo.elem.findall('param/name')
441 paramstext = ','.join([str(param.text) for param in params])
442 API = cmdinfo.elem.attrib.get('name').replace('vk','pTable->',1)
443 self.appendSection('command', ' ' + assignresult + API + '(' + paramstext + ');')
444 self.appendSection('command', ' if (threadChecks) {')
445 self.appendSection('command', " "+"\n ".join(str(finishthreadsafety).rstrip().split("\n")))
446 self.appendSection('command', ' } else {')
447 self.appendSection('command', ' finishMultiThread();')
448 self.appendSection('command', ' }')
449 # Return result variable, if any.
450 if (resulttype != None):
451 self.appendSection('command', ' return result;')
452 self.appendSection('command', '}')
453 #
454 # override makeProtoName to drop the "vk" prefix
455 def makeProtoName(self, name, tail):
456 return self.genOpts.apientry + name[2:] + tail