blob: 289a5bbea3a6be2ac2a4d1ccd86c6c56cd404697 [file] [log] [blame]
Mark Lobodzinskia509c292016-10-11 14:33:07 -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: Tobin Ehlis <tobine@google.com>
21# Author: Mark Lobodzinski <mark@lunarg.com>
22
23import os,re,sys
24import xml.etree.ElementTree as etree
25from generator import *
26from collections import namedtuple
Mark Lobodzinski62f71562017-10-24 13:41:18 -060027from common_codegen import *
Mark Lobodzinskia509c292016-10-11 14:33:07 -060028
29# UniqueObjectsGeneratorOptions - subclass of GeneratorOptions.
30#
31# Adds options used by UniqueObjectsOutputGenerator objects during
32# unique objects layer generation.
33#
34# Additional members
35# prefixText - list of strings to prefix generated header with
36# (usually a copyright statement + calling convention macros).
37# protectFile - True if multiple inclusion protection should be
38# generated (based on the filename) around the entire header.
39# protectFeature - True if #ifndef..#endif protection should be
40# generated around a feature interface in the header file.
41# genFuncPointers - True if function pointer typedefs should be
42# generated
43# protectProto - If conditional protection should be generated
44# around prototype declarations, set to either '#ifdef'
45# to require opt-in (#ifdef protectProtoStr) or '#ifndef'
46# to require opt-out (#ifndef protectProtoStr). Otherwise
47# set to None.
48# protectProtoStr - #ifdef/#ifndef symbol to use around prototype
49# declarations, if protectProto is set
50# apicall - string to use for the function declaration prefix,
51# such as APICALL on Windows.
52# apientry - string to use for the calling convention macro,
53# in typedefs, such as APIENTRY.
54# apientryp - string to use for the calling convention macro
55# in function pointer typedefs, such as APIENTRYP.
56# indentFuncProto - True if prototype declarations should put each
57# parameter on a separate line
58# indentFuncPointer - True if typedefed function pointers should put each
59# parameter on a separate line
60# alignFuncParam - if nonzero and parameters are being put on a
61# separate line, align parameter names at the specified column
62class UniqueObjectsGeneratorOptions(GeneratorOptions):
63 def __init__(self,
64 filename = None,
65 directory = '.',
66 apiname = None,
67 profile = None,
68 versions = '.*',
69 emitversions = '.*',
70 defaultExtensions = None,
71 addExtensions = None,
72 removeExtensions = None,
Mark Lobodzinski62f71562017-10-24 13:41:18 -060073 emitExtensions = None,
Mark Lobodzinskia509c292016-10-11 14:33:07 -060074 sortProcedure = regSortFeatures,
75 prefixText = "",
76 genFuncPointers = True,
77 protectFile = True,
78 protectFeature = True,
Mark Lobodzinskia509c292016-10-11 14:33:07 -060079 apicall = '',
80 apientry = '',
81 apientryp = '',
82 indentFuncProto = True,
83 indentFuncPointer = False,
Mark Lobodzinski62f71562017-10-24 13:41:18 -060084 alignFuncParam = 0,
85 expandEnumerants = True):
Mark Lobodzinskia509c292016-10-11 14:33:07 -060086 GeneratorOptions.__init__(self, filename, directory, apiname, profile,
87 versions, emitversions, defaultExtensions,
Mark Lobodzinski62f71562017-10-24 13:41:18 -060088 addExtensions, removeExtensions, emitExtensions, sortProcedure)
Mark Lobodzinskia509c292016-10-11 14:33:07 -060089 self.prefixText = prefixText
90 self.genFuncPointers = genFuncPointers
91 self.protectFile = protectFile
92 self.protectFeature = protectFeature
Mark Lobodzinskia509c292016-10-11 14:33:07 -060093 self.apicall = apicall
94 self.apientry = apientry
95 self.apientryp = apientryp
96 self.indentFuncProto = indentFuncProto
97 self.indentFuncPointer = indentFuncPointer
Mark Lobodzinski62f71562017-10-24 13:41:18 -060098 self.alignFuncParam = alignFuncParam
99 self.expandEnumerants = expandEnumerants
100
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600101
102# UniqueObjectsOutputGenerator - subclass of OutputGenerator.
103# Generates unique objects layer non-dispatchable handle-wrapping code.
104#
105# ---- methods ----
106# UniqueObjectsOutputGenerator(errFile, warnFile, diagFile) - args as for OutputGenerator. Defines additional internal state.
107# ---- methods overriding base class ----
108# beginFile(genOpts)
109# endFile()
110# beginFeature(interface, emit)
111# endFeature()
112# genCmd(cmdinfo)
113# genStruct()
114# genType()
115class UniqueObjectsOutputGenerator(OutputGenerator):
116 """Generate UniqueObjects code based on XML element attributes"""
117 # This is an ordered list of sections in the header file.
118 ALL_SECTIONS = ['command']
119 def __init__(self,
120 errFile = sys.stderr,
121 warnFile = sys.stderr,
122 diagFile = sys.stdout):
123 OutputGenerator.__init__(self, errFile, warnFile, diagFile)
124 self.INDENT_SPACES = 4
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600125 self.intercepts = []
Mark Lobodzinski8c375832017-02-09 15:58:14 -0700126 self.instance_extensions = []
127 self.device_extensions = []
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600128 # Commands which are not autogenerated but still intercepted
129 self.no_autogen_list = [
Jamie Madill24aa9742016-12-13 17:02:57 -0500130 'vkGetDeviceProcAddr',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600131 'vkGetInstanceProcAddr',
132 'vkCreateInstance',
133 'vkDestroyInstance',
134 'vkCreateDevice',
135 'vkDestroyDevice',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600136 'vkCreateComputePipelines',
137 'vkCreateGraphicsPipelines',
138 'vkCreateSwapchainKHR',
Dustin Graves9a6eb052017-03-28 14:18:54 -0600139 'vkCreateSharedSwapchainsKHR',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600140 'vkGetSwapchainImagesKHR',
Mark Lobodzinski1ce83f42018-02-16 09:58:07 -0700141 'vkDestroySwapchainKHR',
Chris Forbes0f507f22017-04-16 13:13:17 +1200142 'vkQueuePresentKHR',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600143 'vkEnumerateInstanceLayerProperties',
144 'vkEnumerateDeviceLayerProperties',
145 'vkEnumerateInstanceExtensionProperties',
Mark Lobodzinskiab9a8752017-10-25 16:56:42 -0600146 'vkCreateDescriptorUpdateTemplate',
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700147 'vkCreateDescriptorUpdateTemplateKHR',
Mark Lobodzinskiab9a8752017-10-25 16:56:42 -0600148 'vkDestroyDescriptorUpdateTemplate',
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700149 'vkDestroyDescriptorUpdateTemplateKHR',
Mark Lobodzinskiab9a8752017-10-25 16:56:42 -0600150 'vkUpdateDescriptorSetWithTemplate',
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700151 'vkUpdateDescriptorSetWithTemplateKHR',
152 'vkCmdPushDescriptorSetWithTemplateKHR',
Mark Lobodzinskia096c122017-03-16 11:54:35 -0600153 'vkDebugMarkerSetObjectTagEXT',
154 'vkDebugMarkerSetObjectNameEXT',
Mark Youngabc2d6e2017-07-07 07:59:56 -0600155 'vkGetPhysicalDeviceDisplayProperties2KHR',
156 'vkGetPhysicalDeviceDisplayPlaneProperties2KHR',
157 'vkGetDisplayModeProperties2KHR',
Petr Krause91f7a12017-12-14 20:57:36 +0100158 'vkCreateRenderPass',
159 'vkDestroyRenderPass',
Mark Young6ba8abe2017-11-09 10:37:04 -0700160 'vkSetDebugUtilsObjectNameEXT',
161 'vkSetDebugUtilsObjectTagEXT',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600162 ]
163 # Commands shadowed by interface functions and are not implemented
164 self.interface_functions = [
165 'vkGetPhysicalDeviceDisplayPropertiesKHR',
166 'vkGetPhysicalDeviceDisplayPlanePropertiesKHR',
167 'vkGetDisplayPlaneSupportedDisplaysKHR',
168 'vkGetDisplayModePropertiesKHR',
Norbert Nopper1dec9a52016-11-25 07:55:13 +0100169 'vkGetDisplayPlaneCapabilitiesKHR',
Mark Young6ba8abe2017-11-09 10:37:04 -0700170 # VK_EXT_debug_report APIs are hooked, but handled separately in the source file
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600171 'vkCreateDebugReportCallbackEXT',
172 'vkDestroyDebugReportCallbackEXT',
173 'vkDebugReportMessageEXT',
Mark Young6ba8abe2017-11-09 10:37:04 -0700174 # VK_EXT_debug_utils APIs are hooked, but handled separately in the source file
175 'vkCreateDebugUtilsMessengerEXT',
176 'vkDestroyDebugUtilsMessengerEXT',
177 'vkSubmitDebugUtilsMessageEXT',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600178 ]
179 self.headerVersion = None
180 # Internal state - accumulators for different inner block text
181 self.sections = dict([(section, []) for section in self.ALL_SECTIONS])
Mark Lobodzinski85904632017-04-06 10:07:42 -0600182
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600183 self.cmdMembers = []
184 self.cmd_feature_protect = [] # Save ifdef's for each command
Mark Lobodzinski85904632017-04-06 10:07:42 -0600185 self.cmd_info_data = [] # Save the cmdinfo data for wrapping the handles when processing is complete
186 self.structMembers = [] # List of StructMemberData records for all Vulkan structs
187 self.extension_structs = [] # List of all structs or sister-structs containing handles
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600188 # A sister-struct may contain no handles but shares a structextends attribute with one that does
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600189 self.structTypes = dict() # Map of Vulkan struct typename to required VkStructureType
190 self.struct_member_dict = dict()
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600191 # Named tuples to store struct and command data
Mark Lobodzinski85904632017-04-06 10:07:42 -0600192 self.StructType = namedtuple('StructType', ['name', 'value'])
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600193 self.CmdMemberData = namedtuple('CmdMemberData', ['name', 'members'])
194 self.CmdInfoData = namedtuple('CmdInfoData', ['name', 'cmdinfo'])
195 self.CmdExtraProtect = namedtuple('CmdExtraProtect', ['name', 'extra_protect'])
Mark Lobodzinski85904632017-04-06 10:07:42 -0600196
197 self.CommandParam = namedtuple('CommandParam', ['type', 'name', 'ispointer', 'isconst', 'iscount', 'len', 'extstructs', 'cdecl', 'islocal', 'iscreate', 'isdestroy', 'feature_protect'])
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600198 self.StructMemberData = namedtuple('StructMemberData', ['name', 'members'])
199 #
200 def incIndent(self, indent):
201 inc = ' ' * self.INDENT_SPACES
202 if indent:
203 return indent + inc
204 return inc
205 #
206 def decIndent(self, indent):
207 if indent and (len(indent) > self.INDENT_SPACES):
208 return indent[:-self.INDENT_SPACES]
209 return ''
210 #
211 # Override makeProtoName to drop the "vk" prefix
212 def makeProtoName(self, name, tail):
213 return self.genOpts.apientry + name[2:] + tail
214 #
215 # Check if the parameter passed in is a pointer to an array
216 def paramIsArray(self, param):
217 return param.attrib.get('len') is not None
218 #
219 def beginFile(self, genOpts):
220 OutputGenerator.beginFile(self, genOpts)
221 # User-supplied prefix text, if any (list of strings)
222 if (genOpts.prefixText):
223 for s in genOpts.prefixText:
224 write(s, file=self.outFile)
225 # Namespace
226 self.newline()
227 write('namespace unique_objects {', file = self.outFile)
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600228 # Now that the data is all collected and complete, generate and output the wrapping/unwrapping routines
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600229 def endFile(self):
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600230
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600231 self.struct_member_dict = dict(self.structMembers)
232
233 # Generate the list of APIs that might need to handle wrapped extension structs
234 self.GenerateCommandWrapExtensionList()
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600235 # Write out wrapping/unwrapping functions
236 self.WrapCommands()
Mark Lobodzinski85904632017-04-06 10:07:42 -0600237 # Build and write out pNext processing function
238 extension_proc = self.build_extension_processing_func()
239 self.newline()
240 write('// Unique Objects pNext extension handling function', file=self.outFile)
241 write('%s' % extension_proc, file=self.outFile)
242
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600243 # Actually write the interface to the output file.
244 if (self.emit):
245 self.newline()
246 if (self.featureExtraProtect != None):
247 write('#ifdef', self.featureExtraProtect, file=self.outFile)
248 # Write the unique_objects code to the file
249 if (self.sections['command']):
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600250 write('\n'.join(self.sections['command']), end=u'', file=self.outFile)
251 if (self.featureExtraProtect != None):
252 write('\n#endif //', self.featureExtraProtect, file=self.outFile)
253 else:
254 self.newline()
255
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600256 # Record intercepted procedures
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600257 write('// Map of all APIs to be intercepted by this layer', file=self.outFile)
258 write('static const std::unordered_map<std::string, void*> name_to_funcptr_map = {', file=self.outFile)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600259 write('\n'.join(self.intercepts), file=self.outFile)
260 write('};\n', file=self.outFile)
261 self.newline()
262 write('} // namespace unique_objects', file=self.outFile)
263 # Finish processing in superclass
264 OutputGenerator.endFile(self)
265 #
266 def beginFeature(self, interface, emit):
267 # Start processing in superclass
268 OutputGenerator.beginFeature(self, interface, emit)
269 self.headerVersion = None
Mark Lobodzinski62f71562017-10-24 13:41:18 -0600270 self.featureExtraProtect = GetFeatureProtect(interface)
Mark Lobodzinski31964ca2017-09-18 14:15:09 -0600271 if self.featureName != 'VK_VERSION_1_0' and self.featureName != 'VK_VERSION_1_1':
Mark Lobodzinski8c375832017-02-09 15:58:14 -0700272 white_list_entry = []
273 if (self.featureExtraProtect != None):
274 white_list_entry += [ '#ifdef %s' % self.featureExtraProtect ]
275 white_list_entry += [ '"%s"' % self.featureName ]
276 if (self.featureExtraProtect != None):
277 white_list_entry += [ '#endif' ]
278 featureType = interface.get('type')
279 if featureType == 'instance':
280 self.instance_extensions += white_list_entry
281 elif featureType == 'device':
282 self.device_extensions += white_list_entry
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600283 #
284 def endFeature(self):
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600285 # Finish processing in superclass
286 OutputGenerator.endFeature(self)
287 #
288 def genType(self, typeinfo, name):
289 OutputGenerator.genType(self, typeinfo, name)
290 typeElem = typeinfo.elem
291 # If the type is a struct type, traverse the imbedded <member> tags generating a structure.
292 # Otherwise, emit the tag text.
293 category = typeElem.get('category')
294 if (category == 'struct' or category == 'union'):
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600295 self.genStruct(typeinfo, name)
296 #
297 # Append a definition to the specified section
298 def appendSection(self, section, text):
299 # self.sections[section].append('SECTION: ' + section + '\n')
300 self.sections[section].append(text)
301 #
302 # Check if the parameter passed in is a pointer
303 def paramIsPointer(self, param):
304 ispointer = False
305 for elem in param:
306 if ((elem.tag is not 'type') and (elem.tail is not None)) and '*' in elem.tail:
307 ispointer = True
308 return ispointer
309 #
310 # Get the category of a type
311 def getTypeCategory(self, typename):
312 types = self.registry.tree.findall("types/type")
313 for elem in types:
314 if (elem.find("name") is not None and elem.find('name').text == typename) or elem.attrib.get('name') == typename:
315 return elem.attrib.get('category')
316 #
317 # Check if a parent object is dispatchable or not
318 def isHandleTypeNonDispatchable(self, handletype):
319 handle = self.registry.tree.find("types/type/[name='" + handletype + "'][@category='handle']")
320 if handle is not None and handle.find('type').text == 'VK_DEFINE_NON_DISPATCHABLE_HANDLE':
321 return True
322 else:
323 return False
324 #
325 # Retrieve the type and name for a parameter
326 def getTypeNameTuple(self, param):
327 type = ''
328 name = ''
329 for elem in param:
330 if elem.tag == 'type':
331 type = noneStr(elem.text)
332 elif elem.tag == 'name':
333 name = noneStr(elem.text)
334 return (type, name)
335 #
336 # Retrieve the value of the len tag
337 def getLen(self, param):
338 result = None
339 len = param.attrib.get('len')
340 if len and len != 'null-terminated':
341 # For string arrays, 'len' can look like 'count,null-terminated', indicating that we
342 # have a null terminated array of strings. We strip the null-terminated from the
343 # 'len' field and only return the parameter specifying the string count
344 if 'null-terminated' in len:
345 result = len.split(',')[0]
346 else:
347 result = len
348 # Spec has now notation for len attributes, using :: instead of platform specific pointer symbol
349 result = str(result).replace('::', '->')
350 return result
351 #
352 # Generate a VkStructureType based on a structure typename
353 def genVkStructureType(self, typename):
354 # Add underscore between lowercase then uppercase
355 value = re.sub('([a-z0-9])([A-Z])', r'\1_\2', typename)
356 # Change to uppercase
357 value = value.upper()
358 # Add STRUCTURE_TYPE_
359 return re.sub('VK_', 'VK_STRUCTURE_TYPE_', value)
360 #
361 # Struct parameter check generation.
362 # This is a special case of the <type> tag where the contents are interpreted as a set of
363 # <member> tags instead of freeform C type declarations. The <member> tags are just like
364 # <param> tags - they are a declaration of a struct or union member. Only simple member
365 # declarations are supported (no nested structs etc.)
366 def genStruct(self, typeinfo, typeName):
367 OutputGenerator.genStruct(self, typeinfo, typeName)
368 members = typeinfo.elem.findall('.//member')
369 # Iterate over members once to get length parameters for arrays
370 lens = set()
371 for member in members:
372 len = self.getLen(member)
373 if len:
374 lens.add(len)
375 # Generate member info
376 membersInfo = []
377 for member in members:
378 # Get the member's type and name
379 info = self.getTypeNameTuple(member)
380 type = info[0]
381 name = info[1]
382 cdecl = self.makeCParamDecl(member, 0)
383 # Process VkStructureType
384 if type == 'VkStructureType':
385 # Extract the required struct type value from the comments
386 # embedded in the original text defining the 'typeinfo' element
387 rawXml = etree.tostring(typeinfo.elem).decode('ascii')
388 result = re.search(r'VK_STRUCTURE_TYPE_\w+', rawXml)
389 if result:
390 value = result.group(0)
391 else:
392 value = self.genVkStructureType(typeName)
Mark Lobodzinski85904632017-04-06 10:07:42 -0600393 # Store the required type value
394 self.structTypes[typeName] = self.StructType(name=name, value=value)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600395 # Store pointer/array/string info
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600396 extstructs = self.registry.validextensionstructs[typeName] if name == 'pNext' else None
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600397 membersInfo.append(self.CommandParam(type=type,
398 name=name,
399 ispointer=self.paramIsPointer(member),
400 isconst=True if 'const' in cdecl else False,
401 iscount=True if name in lens else False,
402 len=self.getLen(member),
Mark Lobodzinski85904632017-04-06 10:07:42 -0600403 extstructs=extstructs,
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600404 cdecl=cdecl,
405 islocal=False,
406 iscreate=False,
Mark Lobodzinski85904632017-04-06 10:07:42 -0600407 isdestroy=False,
408 feature_protect=self.featureExtraProtect))
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600409 self.structMembers.append(self.StructMemberData(name=typeName, members=membersInfo))
Mark Lobodzinski85904632017-04-06 10:07:42 -0600410
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600411 #
412 # Insert a lock_guard line
413 def lock_guard(self, indent):
414 return '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % indent
415 #
416 # Determine if a struct has an NDO as a member or an embedded member
417 def struct_contains_ndo(self, struct_item):
418 struct_member_dict = dict(self.structMembers)
419 struct_members = struct_member_dict[struct_item]
420
421 for member in struct_members:
422 if self.isHandleTypeNonDispatchable(member.type):
423 return True
424 elif member.type in struct_member_dict:
425 if self.struct_contains_ndo(member.type) == True:
426 return True
427 return False
428 #
429 # Return list of struct members which contain, or which sub-structures contain
430 # an NDO in a given list of parameters or members
431 def getParmeterStructsWithNdos(self, item_list):
432 struct_list = set()
433 for item in item_list:
434 paramtype = item.find('type')
435 typecategory = self.getTypeCategory(paramtype.text)
436 if typecategory == 'struct':
437 if self.struct_contains_ndo(paramtype.text) == True:
438 struct_list.add(item)
439 return struct_list
440 #
441 # Return list of non-dispatchable objects from a given list of parameters or members
442 def getNdosInParameterList(self, item_list, create_func):
443 ndo_list = set()
444 if create_func == True:
445 member_list = item_list[0:-1]
446 else:
447 member_list = item_list
448 for item in member_list:
449 if self.isHandleTypeNonDispatchable(paramtype.text):
450 ndo_list.add(item)
451 return ndo_list
452 #
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600453 # Construct list of extension structs containing handles, or extension structs that share a structextends attribute
454 # WITH an extension struct containing handles. All extension structs in any pNext chain will have to be copied.
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600455 # TODO: make this recursive -- structs buried three or more levels deep are not searched for extensions
456 def GenerateCommandWrapExtensionList(self):
Mark Lobodzinski85904632017-04-06 10:07:42 -0600457 for struct in self.structMembers:
458 if (len(struct.members) > 1) and struct.members[1].extstructs is not None:
459 found = False;
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600460 for item in struct.members[1].extstructs:
Mark Lobodzinski85904632017-04-06 10:07:42 -0600461 if item != '' and self.struct_contains_ndo(item) == True:
462 found = True
463 if found == True:
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600464 for item in struct.members[1].extstructs:
Mark Lobodzinski85904632017-04-06 10:07:42 -0600465 if item != '' and item not in self.extension_structs:
466 self.extension_structs.append(item)
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600467 #
468 # Returns True if a struct may have a pNext chain containing an NDO
469 def StructWithExtensions(self, struct_type):
470 if struct_type in self.struct_member_dict:
471 param_info = self.struct_member_dict[struct_type]
472 if (len(param_info) > 1) and param_info[1].extstructs is not None:
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600473 for item in param_info[1].extstructs:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600474 if item in self.extension_structs:
475 return True
476 return False
477 #
478 # Generate pNext handling function
479 def build_extension_processing_func(self):
Mark Lobodzinski85904632017-04-06 10:07:42 -0600480 # Construct helper functions to build and free pNext extension chains
481 pnext_proc = ''
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700482 pnext_proc += 'void *CreateUnwrappedExtensionStructs(const void *pNext) {\n'
Mark Lobodzinski85904632017-04-06 10:07:42 -0600483 pnext_proc += ' void *cur_pnext = const_cast<void *>(pNext);\n'
484 pnext_proc += ' void *head_pnext = NULL;\n'
485 pnext_proc += ' void *prev_ext_struct = NULL;\n'
486 pnext_proc += ' void *cur_ext_struct = NULL;\n\n'
487 pnext_proc += ' while (cur_pnext != NULL) {\n'
488 pnext_proc += ' GenericHeader *header = reinterpret_cast<GenericHeader *>(cur_pnext);\n\n'
489 pnext_proc += ' switch (header->sType) {\n'
490 for item in self.extension_structs:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600491 struct_info = self.struct_member_dict[item]
Mark Lobodzinski85904632017-04-06 10:07:42 -0600492 if struct_info[0].feature_protect is not None:
493 pnext_proc += '#ifdef %s \n' % struct_info[0].feature_protect
494 pnext_proc += ' case %s: {\n' % self.structTypes[item].value
Chris Forbes0f86c402017-05-02 18:36:39 -0700495 pnext_proc += ' safe_%s *safe_struct = new safe_%s;\n' % (item, item)
Mark Lobodzinski85904632017-04-06 10:07:42 -0600496 pnext_proc += ' safe_struct->initialize(reinterpret_cast<const %s *>(cur_pnext));\n' % item
497 # Generate code to unwrap the handles
498 indent = ' '
499 (tmp_decl, tmp_pre, tmp_post) = self.uniquify_members(struct_info, indent, 'safe_struct->', 0, False, False, False, False)
500 pnext_proc += tmp_pre
501 pnext_proc += ' cur_ext_struct = reinterpret_cast<void *>(safe_struct);\n'
502 pnext_proc += ' } break;\n'
503 if struct_info[0].feature_protect is not None:
504 pnext_proc += '#endif // %s \n' % struct_info[0].feature_protect
505 pnext_proc += '\n'
506 pnext_proc += ' default:\n'
507 pnext_proc += ' break;\n'
508 pnext_proc += ' }\n\n'
509 pnext_proc += ' // Save pointer to the first structure in the pNext chain\n'
510 pnext_proc += ' head_pnext = (head_pnext ? head_pnext : cur_ext_struct);\n\n'
511 pnext_proc += ' // For any extension structure but the first, link the last struct\'s pNext to the current ext struct\n'
512 pnext_proc += ' if (prev_ext_struct) {\n'
513 pnext_proc += ' (reinterpret_cast<GenericHeader *>(prev_ext_struct))->pNext = cur_ext_struct;\n'
514 pnext_proc += ' }\n'
515 pnext_proc += ' prev_ext_struct = cur_ext_struct;\n\n'
516 pnext_proc += ' // Process the next structure in the chain\n'
517 pnext_proc += ' cur_pnext = const_cast<void *>(header->pNext);\n'
518 pnext_proc += ' }\n'
519 pnext_proc += ' return head_pnext;\n'
520 pnext_proc += '}\n\n'
521 pnext_proc += '// Free a pNext extension chain\n'
522 pnext_proc += 'void FreeUnwrappedExtensionStructs(void *head) {\n'
523 pnext_proc += ' void * curr_ptr = head;\n'
524 pnext_proc += ' while (curr_ptr) {\n'
525 pnext_proc += ' GenericHeader *header = reinterpret_cast<GenericHeader *>(curr_ptr);\n'
526 pnext_proc += ' void *temp = curr_ptr;\n'
527 pnext_proc += ' curr_ptr = header->pNext;\n'
528 pnext_proc += ' free(temp);\n'
529 pnext_proc += ' }\n'
530 pnext_proc += '}\n'
531 return pnext_proc
532
533 #
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600534 # Generate source for creating a non-dispatchable object
535 def generate_create_ndo_code(self, indent, proto, params, cmd_info):
536 create_ndo_code = ''
Mark Young39389872017-01-19 21:10:49 -0700537 handle_type = params[-1].find('type')
538 if self.isHandleTypeNonDispatchable(handle_type.text):
539 # Check for special case where multiple handles are returned
540 ndo_array = False
541 if cmd_info[-1].len is not None:
542 ndo_array = True;
543 handle_name = params[-1].find('name')
544 create_ndo_code += '%sif (VK_SUCCESS == result) {\n' % (indent)
545 indent = self.incIndent(indent)
546 create_ndo_code += '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % (indent)
547 ndo_dest = '*%s' % handle_name.text
548 if ndo_array == True:
549 create_ndo_code += '%sfor (uint32_t index0 = 0; index0 < %s; index0++) {\n' % (indent, cmd_info[-1].len)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600550 indent = self.incIndent(indent)
Mark Young39389872017-01-19 21:10:49 -0700551 ndo_dest = '%s[index0]' % cmd_info[-1].name
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700552 create_ndo_code += '%s%s = WrapNew(%s);\n' % (indent, ndo_dest, ndo_dest)
Mark Young39389872017-01-19 21:10:49 -0700553 if ndo_array == True:
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600554 indent = self.decIndent(indent)
Mark Young39389872017-01-19 21:10:49 -0700555 create_ndo_code += '%s}\n' % indent
556 indent = self.decIndent(indent)
557 create_ndo_code += '%s}\n' % (indent)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600558 return create_ndo_code
559 #
560 # Generate source for destroying a non-dispatchable object
561 def generate_destroy_ndo_code(self, indent, proto, cmd_info):
562 destroy_ndo_code = ''
563 ndo_array = False
564 if True in [destroy_txt in proto.text for destroy_txt in ['Destroy', 'Free']]:
565 # Check for special case where multiple handles are returned
566 if cmd_info[-1].len is not None:
567 ndo_array = True;
568 param = -1
569 else:
570 param = -2
571 if self.isHandleTypeNonDispatchable(cmd_info[param].type) == True:
572 if ndo_array == True:
573 # This API is freeing an array of handles. Remove them from the unique_id map.
574 destroy_ndo_code += '%sif ((VK_SUCCESS == result) && (%s)) {\n' % (indent, cmd_info[param].name)
575 indent = self.incIndent(indent)
576 destroy_ndo_code += '%sstd::unique_lock<std::mutex> lock(global_lock);\n' % (indent)
577 destroy_ndo_code += '%sfor (uint32_t index0 = 0; index0 < %s; index0++) {\n' % (indent, cmd_info[param].len)
578 indent = self.incIndent(indent)
579 destroy_ndo_code += '%s%s handle = %s[index0];\n' % (indent, cmd_info[param].type, cmd_info[param].name)
580 destroy_ndo_code += '%suint64_t unique_id = reinterpret_cast<uint64_t &>(handle);\n' % (indent)
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700581 destroy_ndo_code += '%sunique_id_mapping.erase(unique_id);\n' % (indent)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600582 indent = self.decIndent(indent);
583 destroy_ndo_code += '%s}\n' % indent
584 indent = self.decIndent(indent);
585 destroy_ndo_code += '%s}\n' % indent
586 else:
587 # Remove a single handle from the map
588 destroy_ndo_code += '%sstd::unique_lock<std::mutex> lock(global_lock);\n' % (indent)
589 destroy_ndo_code += '%suint64_t %s_id = reinterpret_cast<uint64_t &>(%s);\n' % (indent, cmd_info[param].name, cmd_info[param].name)
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700590 destroy_ndo_code += '%s%s = (%s)unique_id_mapping[%s_id];\n' % (indent, cmd_info[param].name, cmd_info[param].type, cmd_info[param].name)
591 destroy_ndo_code += '%sunique_id_mapping.erase(%s_id);\n' % (indent, cmd_info[param].name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600592 destroy_ndo_code += '%slock.unlock();\n' % (indent)
593 return ndo_array, destroy_ndo_code
594
595 #
596 # Clean up local declarations
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600597 def cleanUpLocalDeclarations(self, indent, prefix, name, len, index, process_pnext):
Mark Young26095c42017-05-09 13:19:04 -0600598 cleanup = '%sif (local_%s%s) {\n' % (indent, prefix, name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600599 if len is not None:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600600 if process_pnext:
601 cleanup += '%s for (uint32_t %s = 0; %s < %s%s; ++%s) {\n' % (indent, index, index, prefix, len, index)
602 cleanup += '%s FreeUnwrappedExtensionStructs(const_cast<void *>(local_%s%s[%s].pNext));\n' % (indent, prefix, name, index)
603 cleanup += '%s }\n' % indent
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600604 cleanup += '%s delete[] local_%s%s;\n' % (indent, prefix, name)
605 else:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600606 if process_pnext:
607 cleanup += '%s FreeUnwrappedExtensionStructs(const_cast<void *>(local_%s%s->pNext));\n' % (indent, prefix, name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600608 cleanup += '%s delete local_%s%s;\n' % (indent, prefix, name)
Mark Young26095c42017-05-09 13:19:04 -0600609 cleanup += "%s}\n" % (indent)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600610 return cleanup
611 #
612 # Output UO code for a single NDO (ndo_count is NULL) or a counted list of NDOs
613 def outputNDOs(self, ndo_type, ndo_name, ndo_count, prefix, index, indent, destroy_func, destroy_array, top_level):
614 decl_code = ''
615 pre_call_code = ''
616 post_call_code = ''
617 if ndo_count is not None:
618 if top_level == True:
619 decl_code += '%s%s *local_%s%s = NULL;\n' % (indent, ndo_type, prefix, ndo_name)
620 pre_call_code += '%s if (%s%s) {\n' % (indent, prefix, ndo_name)
621 indent = self.incIndent(indent)
622 if top_level == True:
623 pre_call_code += '%s local_%s%s = new %s[%s];\n' % (indent, prefix, ndo_name, ndo_type, ndo_count)
624 pre_call_code += '%s for (uint32_t %s = 0; %s < %s; ++%s) {\n' % (indent, index, index, ndo_count, index)
625 indent = self.incIndent(indent)
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700626 pre_call_code += '%s local_%s%s[%s] = Unwrap(%s[%s]);\n' % (indent, prefix, ndo_name, index, ndo_name, index)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600627 else:
628 pre_call_code += '%s for (uint32_t %s = 0; %s < %s; ++%s) {\n' % (indent, index, index, ndo_count, index)
629 indent = self.incIndent(indent)
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700630 pre_call_code += '%s %s%s[%s] = Unwrap(%s%s[%s]);\n' % (indent, prefix, ndo_name, index, prefix, ndo_name, index)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600631 indent = self.decIndent(indent)
632 pre_call_code += '%s }\n' % indent
633 indent = self.decIndent(indent)
634 pre_call_code += '%s }\n' % indent
635 if top_level == True:
636 post_call_code += '%sif (local_%s%s)\n' % (indent, prefix, ndo_name)
637 indent = self.incIndent(indent)
638 post_call_code += '%sdelete[] local_%s;\n' % (indent, ndo_name)
639 else:
640 if top_level == True:
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600641 if (destroy_func == False) or (destroy_array == True):
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700642 pre_call_code += '%s %s = Unwrap(%s);\n' % (indent, ndo_name, ndo_name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600643 else:
644 # Make temp copy of this var with the 'local' removed. It may be better to not pass in 'local_'
645 # as part of the string and explicitly print it
646 fix = str(prefix).strip('local_');
647 pre_call_code += '%s if (%s%s) {\n' % (indent, fix, ndo_name)
648 indent = self.incIndent(indent)
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700649 pre_call_code += '%s %s%s = Unwrap(%s%s);\n' % (indent, prefix, ndo_name, fix, ndo_name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600650 indent = self.decIndent(indent)
651 pre_call_code += '%s }\n' % indent
652 return decl_code, pre_call_code, post_call_code
653 #
654 # first_level_param indicates if elements are passed directly into the function else they're below a ptr/struct
655 # create_func means that this is API creates or allocates NDOs
656 # destroy_func indicates that this API destroys or frees NDOs
657 # destroy_array means that the destroy_func operated on an array of NDOs
658 def uniquify_members(self, members, indent, prefix, array_index, create_func, destroy_func, destroy_array, first_level_param):
659 decls = ''
660 pre_code = ''
661 post_code = ''
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600662 index = 'index%s' % str(array_index)
663 array_index += 1
664 # Process any NDOs in this structure and recurse for any sub-structs in this struct
665 for member in members:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600666 process_pnext = self.StructWithExtensions(member.type)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600667 # Handle NDOs
668 if self.isHandleTypeNonDispatchable(member.type) == True:
Jamie Madill24aa9742016-12-13 17:02:57 -0500669 count_name = member.len
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600670 if (count_name is not None):
671 if first_level_param == False:
672 count_name = '%s%s' % (prefix, member.len)
673
674 if (first_level_param == False) or (create_func == False):
675 (tmp_decl, tmp_pre, tmp_post) = self.outputNDOs(member.type, member.name, count_name, prefix, index, indent, destroy_func, destroy_array, first_level_param)
676 decls += tmp_decl
677 pre_code += tmp_pre
678 post_code += tmp_post
679 # Handle Structs that contain NDOs at some level
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600680 elif member.type in self.struct_member_dict:
681 # Structs at first level will have an NDO, OR, we need a safe_struct for the pnext chain
682 if self.struct_contains_ndo(member.type) == True or process_pnext:
683 struct_info = self.struct_member_dict[member.type]
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600684 # Struct Array
685 if member.len is not None:
686 # Update struct prefix
687 if first_level_param == True:
688 new_prefix = 'local_%s' % member.name
689 # Declare safe_VarType for struct
690 decls += '%ssafe_%s *%s = NULL;\n' % (indent, member.type, new_prefix)
691 else:
692 new_prefix = '%s%s' % (prefix, member.name)
693 pre_code += '%s if (%s%s) {\n' % (indent, prefix, member.name)
694 indent = self.incIndent(indent)
695 if first_level_param == True:
696 pre_code += '%s %s = new safe_%s[%s];\n' % (indent, new_prefix, member.type, member.len)
697 pre_code += '%s for (uint32_t %s = 0; %s < %s%s; ++%s) {\n' % (indent, index, index, prefix, member.len, index)
698 indent = self.incIndent(indent)
699 if first_level_param == True:
700 pre_code += '%s %s[%s].initialize(&%s[%s]);\n' % (indent, new_prefix, index, member.name, index)
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600701 if process_pnext:
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700702 pre_code += '%s %s[%s].pNext = CreateUnwrappedExtensionStructs(%s[%s].pNext);\n' % (indent, new_prefix, index, new_prefix, index)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600703 local_prefix = '%s[%s].' % (new_prefix, index)
704 # Process sub-structs in this struct
705 (tmp_decl, tmp_pre, tmp_post) = self.uniquify_members(struct_info, indent, local_prefix, array_index, create_func, destroy_func, destroy_array, False)
706 decls += tmp_decl
707 pre_code += tmp_pre
708 post_code += tmp_post
709 indent = self.decIndent(indent)
710 pre_code += '%s }\n' % indent
711 indent = self.decIndent(indent)
712 pre_code += '%s }\n' % indent
713 if first_level_param == True:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600714 post_code += self.cleanUpLocalDeclarations(indent, prefix, member.name, member.len, index, process_pnext)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600715 # Single Struct
716 else:
717 # Update struct prefix
718 if first_level_param == True:
719 new_prefix = 'local_%s->' % member.name
720 decls += '%ssafe_%s *local_%s%s = NULL;\n' % (indent, member.type, prefix, member.name)
721 else:
722 new_prefix = '%s%s->' % (prefix, member.name)
723 # Declare safe_VarType for struct
724 pre_code += '%s if (%s%s) {\n' % (indent, prefix, member.name)
725 indent = self.incIndent(indent)
726 if first_level_param == True:
727 pre_code += '%s local_%s%s = new safe_%s(%s);\n' % (indent, prefix, member.name, member.type, member.name)
728 # Process sub-structs in this struct
729 (tmp_decl, tmp_pre, tmp_post) = self.uniquify_members(struct_info, indent, new_prefix, array_index, create_func, destroy_func, destroy_array, False)
730 decls += tmp_decl
731 pre_code += tmp_pre
732 post_code += tmp_post
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600733 if process_pnext:
Mark Lobodzinskic7eda922018-02-28 13:38:45 -0700734 pre_code += '%s local_%s%s->pNext = CreateUnwrappedExtensionStructs(local_%s%s->pNext);\n' % (indent, prefix, member.name, prefix, member.name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600735 indent = self.decIndent(indent)
736 pre_code += '%s }\n' % indent
737 if first_level_param == True:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600738 post_code += self.cleanUpLocalDeclarations(indent, prefix, member.name, member.len, index, process_pnext)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600739 return decls, pre_code, post_code
740 #
741 # For a particular API, generate the non-dispatchable-object wrapping/unwrapping code
742 def generate_wrapping_code(self, cmd):
743 indent = ' '
744 proto = cmd.find('proto/name')
745 params = cmd.findall('param')
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600746
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600747 if proto.text is not None:
748 cmd_member_dict = dict(self.cmdMembers)
749 cmd_info = cmd_member_dict[proto.text]
750 # Handle ndo create/allocate operations
751 if cmd_info[0].iscreate:
752 create_ndo_code = self.generate_create_ndo_code(indent, proto, params, cmd_info)
753 else:
754 create_ndo_code = ''
755 # Handle ndo destroy/free operations
756 if cmd_info[0].isdestroy:
757 (destroy_array, destroy_ndo_code) = self.generate_destroy_ndo_code(indent, proto, cmd_info)
758 else:
759 destroy_array = False
760 destroy_ndo_code = ''
761 paramdecl = ''
762 param_pre_code = ''
763 param_post_code = ''
764 create_func = True if create_ndo_code else False
765 destroy_func = True if destroy_ndo_code else False
766 (paramdecl, param_pre_code, param_post_code) = self.uniquify_members(cmd_info, indent, '', 0, create_func, destroy_func, destroy_array, True)
767 param_post_code += create_ndo_code
768 if destroy_ndo_code:
769 if destroy_array == True:
770 param_post_code += destroy_ndo_code
771 else:
772 param_pre_code += destroy_ndo_code
773 if param_pre_code:
774 if (not destroy_func) or (destroy_array):
775 param_pre_code = '%s{\n%s%s%s%s}\n' % (' ', indent, self.lock_guard(indent), param_pre_code, indent)
776 return paramdecl, param_pre_code, param_post_code
777 #
778 # Capture command parameter info needed to wrap NDOs as well as handling some boilerplate code
779 def genCmd(self, cmdinfo, cmdname):
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600780
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600781 # Add struct-member type information to command parameter information
782 OutputGenerator.genCmd(self, cmdinfo, cmdname)
783 members = cmdinfo.elem.findall('.//param')
784 # Iterate over members once to get length parameters for arrays
785 lens = set()
786 for member in members:
787 len = self.getLen(member)
788 if len:
789 lens.add(len)
790 struct_member_dict = dict(self.structMembers)
791 # Generate member info
792 membersInfo = []
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600793 constains_extension_structs = False
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600794 for member in members:
795 # Get type and name of member
796 info = self.getTypeNameTuple(member)
797 type = info[0]
798 name = info[1]
799 cdecl = self.makeCParamDecl(member, 0)
800 # Check for parameter name in lens set
801 iscount = True if name in lens else False
802 len = self.getLen(member)
803 isconst = True if 'const' in cdecl else False
804 ispointer = self.paramIsPointer(member)
805 # Mark param as local if it is an array of NDOs
806 islocal = False;
807 if self.isHandleTypeNonDispatchable(type) == True:
808 if (len is not None) and (isconst == True):
809 islocal = True
810 # Or if it's a struct that contains an NDO
811 elif type in struct_member_dict:
812 if self.struct_contains_ndo(type) == True:
813 islocal = True
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600814 isdestroy = True if True in [destroy_txt in cmdname for destroy_txt in ['Destroy', 'Free']] else False
Mark Young39389872017-01-19 21:10:49 -0700815 iscreate = True if True in [create_txt in cmdname for create_txt in ['Create', 'Allocate', 'GetRandROutputDisplayEXT', 'RegisterDeviceEvent', 'RegisterDisplayEvent']] else False
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600816 extstructs = self.registry.validextensionstructs[type] if name == 'pNext' else None
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600817 membersInfo.append(self.CommandParam(type=type,
818 name=name,
819 ispointer=ispointer,
820 isconst=isconst,
821 iscount=iscount,
822 len=len,
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600823 extstructs=extstructs,
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600824 cdecl=cdecl,
825 islocal=islocal,
826 iscreate=iscreate,
Mark Lobodzinski85904632017-04-06 10:07:42 -0600827 isdestroy=isdestroy,
828 feature_protect=self.featureExtraProtect))
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600829 self.cmdMembers.append(self.CmdMemberData(name=cmdname, members=membersInfo))
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600830 self.cmd_info_data.append(self.CmdInfoData(name=cmdname, cmdinfo=cmdinfo))
831 self.cmd_feature_protect.append(self.CmdExtraProtect(name=cmdname, extra_protect=self.featureExtraProtect))
832 #
833 # Create code to wrap NDOs as well as handling some boilerplate code
834 def WrapCommands(self):
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600835 cmd_member_dict = dict(self.cmdMembers)
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600836 cmd_info_dict = dict(self.cmd_info_data)
837 cmd_protect_dict = dict(self.cmd_feature_protect)
838
839 for api_call in self.cmdMembers:
840 cmdname = api_call.name
841 cmdinfo = cmd_info_dict[api_call.name]
842 if cmdname in self.interface_functions:
843 continue
844 if cmdname in self.no_autogen_list:
845 decls = self.makeCDecls(cmdinfo.elem)
846 self.appendSection('command', '')
847 self.appendSection('command', '// Declare only')
848 self.appendSection('command', decls[0])
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600849 self.intercepts += [ ' {"%s", (void *)%s},' % (cmdname,cmdname[2:]) ]
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600850 continue
851 # Generate NDO wrapping/unwrapping code for all parameters
852 (api_decls, api_pre, api_post) = self.generate_wrapping_code(cmdinfo.elem)
853 # If API doesn't contain an NDO's, don't fool with it
854 if not api_decls and not api_pre and not api_post:
855 continue
856 feature_extra_protect = cmd_protect_dict[api_call.name]
857 if (feature_extra_protect != None):
858 self.appendSection('command', '')
859 self.appendSection('command', '#ifdef '+ feature_extra_protect)
860 self.intercepts += [ '#ifdef %s' % feature_extra_protect ]
861 # Add intercept to procmap
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600862 self.intercepts += [ ' {"%s", (void*)%s},' % (cmdname,cmdname[2:]) ]
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600863 decls = self.makeCDecls(cmdinfo.elem)
864 self.appendSection('command', '')
865 self.appendSection('command', decls[0][:-1])
866 self.appendSection('command', '{')
867 # Setup common to call wrappers, first parameter is always dispatchable
868 dispatchable_type = cmdinfo.elem.find('param/type').text
869 dispatchable_name = cmdinfo.elem.find('param/name').text
870 # Generate local instance/pdev/device data lookup
Chris Forbes5279a8c2017-05-02 16:26:23 -0700871 if dispatchable_type in ["VkPhysicalDevice", "VkInstance"]:
872 self.appendSection('command', ' instance_layer_data *dev_data = GetLayerDataPtr(get_dispatch_key('+dispatchable_name+'), instance_layer_data_map);')
873 else:
874 self.appendSection('command', ' layer_data *dev_data = GetLayerDataPtr(get_dispatch_key('+dispatchable_name+'), layer_data_map);')
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600875 # Handle return values, if any
876 resulttype = cmdinfo.elem.find('proto/type')
877 if (resulttype != None and resulttype.text == 'void'):
878 resulttype = None
879 if (resulttype != None):
880 assignresult = resulttype.text + ' result = '
881 else:
882 assignresult = ''
883 # Pre-pend declarations and pre-api-call codegen
884 if api_decls:
885 self.appendSection('command', "\n".join(str(api_decls).rstrip().split("\n")))
886 if api_pre:
887 self.appendSection('command', "\n".join(str(api_pre).rstrip().split("\n")))
888 # Generate the API call itself
889 # Gather the parameter items
890 params = cmdinfo.elem.findall('param/name')
891 # Pull out the text for each of the parameters, separate them by commas in a list
892 paramstext = ', '.join([str(param.text) for param in params])
893 # If any of these paramters has been replaced by a local var, fix up the list
894 params = cmd_member_dict[cmdname]
895 for param in params:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600896 if param.islocal == True or self.StructWithExtensions(param.type):
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600897 if param.ispointer == True:
898 paramstext = paramstext.replace(param.name, '(%s %s*)local_%s' % ('const', param.type, param.name))
899 else:
900 paramstext = paramstext.replace(param.name, '(%s %s)local_%s' % ('const', param.type, param.name))
901 # Use correct dispatch table
Chris Forbes44c05302017-05-02 16:42:55 -0700902 API = cmdinfo.elem.attrib.get('name').replace('vk','dev_data->dispatch_table.',1)
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600903 # Put all this together for the final down-chain call
904 self.appendSection('command', ' ' + assignresult + API + '(' + paramstext + ');')
905 # And add the post-API-call codegen
906 self.appendSection('command', "\n".join(str(api_post).rstrip().split("\n")))
907 # Handle the return result variable, if any
908 if (resulttype != None):
909 self.appendSection('command', ' return result;')
910 self.appendSection('command', '}')
911 if (feature_extra_protect != None):
912 self.appendSection('command', '#endif // '+ feature_extra_protect)
913 self.intercepts += [ '#endif' ]