blob: fe7cc9c70d85fddd6ff134762c3c2061709420ca [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
27
28# UniqueObjectsGeneratorOptions - subclass of GeneratorOptions.
29#
30# Adds options used by UniqueObjectsOutputGenerator objects during
31# unique objects layer generation.
32#
33# Additional members
34# prefixText - list of strings to prefix generated header with
35# (usually a copyright statement + calling convention macros).
36# protectFile - True if multiple inclusion protection should be
37# generated (based on the filename) around the entire header.
38# protectFeature - True if #ifndef..#endif protection should be
39# generated around a feature interface in the header file.
40# genFuncPointers - True if function pointer typedefs should be
41# generated
42# protectProto - If conditional protection should be generated
43# around prototype declarations, set to either '#ifdef'
44# to require opt-in (#ifdef protectProtoStr) or '#ifndef'
45# to require opt-out (#ifndef protectProtoStr). Otherwise
46# set to None.
47# protectProtoStr - #ifdef/#ifndef symbol to use around prototype
48# declarations, if protectProto is set
49# apicall - string to use for the function declaration prefix,
50# such as APICALL on Windows.
51# apientry - string to use for the calling convention macro,
52# in typedefs, such as APIENTRY.
53# apientryp - string to use for the calling convention macro
54# in function pointer typedefs, such as APIENTRYP.
55# indentFuncProto - True if prototype declarations should put each
56# parameter on a separate line
57# indentFuncPointer - True if typedefed function pointers should put each
58# parameter on a separate line
59# alignFuncParam - if nonzero and parameters are being put on a
60# separate line, align parameter names at the specified column
61class UniqueObjectsGeneratorOptions(GeneratorOptions):
62 def __init__(self,
63 filename = None,
64 directory = '.',
65 apiname = None,
66 profile = None,
67 versions = '.*',
68 emitversions = '.*',
69 defaultExtensions = None,
70 addExtensions = None,
71 removeExtensions = None,
72 sortProcedure = regSortFeatures,
73 prefixText = "",
74 genFuncPointers = True,
75 protectFile = True,
76 protectFeature = True,
77 protectProto = None,
78 protectProtoStr = None,
79 apicall = '',
80 apientry = '',
81 apientryp = '',
82 indentFuncProto = True,
83 indentFuncPointer = False,
84 alignFuncParam = 0):
85 GeneratorOptions.__init__(self, filename, directory, apiname, profile,
86 versions, emitversions, defaultExtensions,
87 addExtensions, removeExtensions, sortProcedure)
88 self.prefixText = prefixText
89 self.genFuncPointers = genFuncPointers
90 self.protectFile = protectFile
91 self.protectFeature = protectFeature
92 self.protectProto = protectProto
93 self.protectProtoStr = protectProtoStr
94 self.apicall = apicall
95 self.apientry = apientry
96 self.apientryp = apientryp
97 self.indentFuncProto = indentFuncProto
98 self.indentFuncPointer = indentFuncPointer
99 self.alignFuncParam = alignFuncParam
100
101# UniqueObjectsOutputGenerator - subclass of OutputGenerator.
102# Generates unique objects layer non-dispatchable handle-wrapping code.
103#
104# ---- methods ----
105# UniqueObjectsOutputGenerator(errFile, warnFile, diagFile) - args as for OutputGenerator. Defines additional internal state.
106# ---- methods overriding base class ----
107# beginFile(genOpts)
108# endFile()
109# beginFeature(interface, emit)
110# endFeature()
111# genCmd(cmdinfo)
112# genStruct()
113# genType()
114class UniqueObjectsOutputGenerator(OutputGenerator):
115 """Generate UniqueObjects code based on XML element attributes"""
116 # This is an ordered list of sections in the header file.
117 ALL_SECTIONS = ['command']
118 def __init__(self,
119 errFile = sys.stderr,
120 warnFile = sys.stderr,
121 diagFile = sys.stdout):
122 OutputGenerator.__init__(self, errFile, warnFile, diagFile)
123 self.INDENT_SPACES = 4
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600124 self.intercepts = []
Mark Lobodzinski8c375832017-02-09 15:58:14 -0700125 self.instance_extensions = []
126 self.device_extensions = []
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600127 # Commands which are not autogenerated but still intercepted
128 self.no_autogen_list = [
Jamie Madill24aa9742016-12-13 17:02:57 -0500129 'vkGetDeviceProcAddr',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600130 'vkGetInstanceProcAddr',
131 'vkCreateInstance',
132 'vkDestroyInstance',
133 'vkCreateDevice',
134 'vkDestroyDevice',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600135 'vkCreateComputePipelines',
136 'vkCreateGraphicsPipelines',
137 'vkCreateSwapchainKHR',
Dustin Graves9a6eb052017-03-28 14:18:54 -0600138 'vkCreateSharedSwapchainsKHR',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600139 'vkGetSwapchainImagesKHR',
Mark Lobodzinski1ce83f42018-02-16 09:58:07 -0700140 'vkDestroySwapchainKHR',
Chris Forbes0f507f22017-04-16 13:13:17 +1200141 'vkQueuePresentKHR',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600142 'vkEnumerateInstanceLayerProperties',
143 'vkEnumerateDeviceLayerProperties',
144 'vkEnumerateInstanceExtensionProperties',
Mark Lobodzinski71703a52017-03-03 08:40:16 -0700145 'vkCreateDescriptorUpdateTemplateKHR',
146 'vkDestroyDescriptorUpdateTemplateKHR',
147 'vkUpdateDescriptorSetWithTemplateKHR',
148 'vkCmdPushDescriptorSetWithTemplateKHR',
Mark Lobodzinskia096c122017-03-16 11:54:35 -0600149 'vkDebugMarkerSetObjectTagEXT',
150 'vkDebugMarkerSetObjectNameEXT',
Mark Youngabc2d6e2017-07-07 07:59:56 -0600151 'vkGetPhysicalDeviceDisplayProperties2KHR',
152 'vkGetPhysicalDeviceDisplayPlaneProperties2KHR',
153 'vkGetDisplayModeProperties2KHR',
Petr Krause91f7a12017-12-14 20:57:36 +0100154 'vkCreateRenderPass',
155 'vkDestroyRenderPass',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600156 ]
157 # Commands shadowed by interface functions and are not implemented
158 self.interface_functions = [
159 'vkGetPhysicalDeviceDisplayPropertiesKHR',
160 'vkGetPhysicalDeviceDisplayPlanePropertiesKHR',
161 'vkGetDisplayPlaneSupportedDisplaysKHR',
162 'vkGetDisplayModePropertiesKHR',
Norbert Nopper1dec9a52016-11-25 07:55:13 +0100163 'vkGetDisplayPlaneCapabilitiesKHR',
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600164 # DebugReport APIs are hooked, but handled separately in the source file
165 'vkCreateDebugReportCallbackEXT',
166 'vkDestroyDebugReportCallbackEXT',
167 'vkDebugReportMessageEXT',
168 ]
169 self.headerVersion = None
170 # Internal state - accumulators for different inner block text
171 self.sections = dict([(section, []) for section in self.ALL_SECTIONS])
Mark Lobodzinski85904632017-04-06 10:07:42 -0600172
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600173 self.cmdMembers = []
174 self.cmd_feature_protect = [] # Save ifdef's for each command
Mark Lobodzinski85904632017-04-06 10:07:42 -0600175 self.cmd_info_data = [] # Save the cmdinfo data for wrapping the handles when processing is complete
176 self.structMembers = [] # List of StructMemberData records for all Vulkan structs
177 self.extension_structs = [] # List of all structs or sister-structs containing handles
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600178 # A sister-struct may contain no handles but shares a structextends attribute with one that does
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600179 self.structTypes = dict() # Map of Vulkan struct typename to required VkStructureType
180 self.struct_member_dict = dict()
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600181 # Named tuples to store struct and command data
Mark Lobodzinski85904632017-04-06 10:07:42 -0600182 self.StructType = namedtuple('StructType', ['name', 'value'])
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600183 self.CmdMemberData = namedtuple('CmdMemberData', ['name', 'members'])
184 self.CmdInfoData = namedtuple('CmdInfoData', ['name', 'cmdinfo'])
185 self.CmdExtraProtect = namedtuple('CmdExtraProtect', ['name', 'extra_protect'])
Mark Lobodzinski85904632017-04-06 10:07:42 -0600186
187 self.CommandParam = namedtuple('CommandParam', ['type', 'name', 'ispointer', 'isconst', 'iscount', 'len', 'extstructs', 'cdecl', 'islocal', 'iscreate', 'isdestroy', 'feature_protect'])
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600188 self.StructMemberData = namedtuple('StructMemberData', ['name', 'members'])
189 #
190 def incIndent(self, indent):
191 inc = ' ' * self.INDENT_SPACES
192 if indent:
193 return indent + inc
194 return inc
195 #
196 def decIndent(self, indent):
197 if indent and (len(indent) > self.INDENT_SPACES):
198 return indent[:-self.INDENT_SPACES]
199 return ''
200 #
201 # Override makeProtoName to drop the "vk" prefix
202 def makeProtoName(self, name, tail):
203 return self.genOpts.apientry + name[2:] + tail
204 #
205 # Check if the parameter passed in is a pointer to an array
206 def paramIsArray(self, param):
207 return param.attrib.get('len') is not None
208 #
209 def beginFile(self, genOpts):
210 OutputGenerator.beginFile(self, genOpts)
211 # User-supplied prefix text, if any (list of strings)
212 if (genOpts.prefixText):
213 for s in genOpts.prefixText:
214 write(s, file=self.outFile)
215 # Namespace
216 self.newline()
217 write('namespace unique_objects {', file = self.outFile)
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600218 # Now that the data is all collected and complete, generate and output the wrapping/unwrapping routines
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600219 def endFile(self):
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600220
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600221 self.struct_member_dict = dict(self.structMembers)
222
223 # Generate the list of APIs that might need to handle wrapped extension structs
224 self.GenerateCommandWrapExtensionList()
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600225 # Write out wrapping/unwrapping functions
226 self.WrapCommands()
Mark Lobodzinski85904632017-04-06 10:07:42 -0600227 # Build and write out pNext processing function
228 extension_proc = self.build_extension_processing_func()
229 self.newline()
230 write('// Unique Objects pNext extension handling function', file=self.outFile)
231 write('%s' % extension_proc, file=self.outFile)
232
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600233 # Actually write the interface to the output file.
234 if (self.emit):
235 self.newline()
236 if (self.featureExtraProtect != None):
237 write('#ifdef', self.featureExtraProtect, file=self.outFile)
238 # Write the unique_objects code to the file
239 if (self.sections['command']):
240 if (self.genOpts.protectProto):
241 write(self.genOpts.protectProto,
242 self.genOpts.protectProtoStr, file=self.outFile)
243 write('\n'.join(self.sections['command']), end=u'', file=self.outFile)
244 if (self.featureExtraProtect != None):
245 write('\n#endif //', self.featureExtraProtect, file=self.outFile)
246 else:
247 self.newline()
248
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600249 # Record intercepted procedures
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600250 write('// Map of all APIs to be intercepted by this layer', file=self.outFile)
251 write('static const std::unordered_map<std::string, void*> name_to_funcptr_map = {', file=self.outFile)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600252 write('\n'.join(self.intercepts), file=self.outFile)
253 write('};\n', file=self.outFile)
254 self.newline()
255 write('} // namespace unique_objects', file=self.outFile)
256 # Finish processing in superclass
257 OutputGenerator.endFile(self)
258 #
259 def beginFeature(self, interface, emit):
260 # Start processing in superclass
261 OutputGenerator.beginFeature(self, interface, emit)
262 self.headerVersion = None
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600263
Mark Lobodzinski8c375832017-02-09 15:58:14 -0700264 if self.featureName != 'VK_VERSION_1_0':
265 white_list_entry = []
266 if (self.featureExtraProtect != None):
267 white_list_entry += [ '#ifdef %s' % self.featureExtraProtect ]
268 white_list_entry += [ '"%s"' % self.featureName ]
269 if (self.featureExtraProtect != None):
270 white_list_entry += [ '#endif' ]
271 featureType = interface.get('type')
272 if featureType == 'instance':
273 self.instance_extensions += white_list_entry
274 elif featureType == 'device':
275 self.device_extensions += white_list_entry
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600276 #
277 def endFeature(self):
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600278 # Finish processing in superclass
279 OutputGenerator.endFeature(self)
280 #
281 def genType(self, typeinfo, name):
282 OutputGenerator.genType(self, typeinfo, name)
283 typeElem = typeinfo.elem
284 # If the type is a struct type, traverse the imbedded <member> tags generating a structure.
285 # Otherwise, emit the tag text.
286 category = typeElem.get('category')
287 if (category == 'struct' or category == 'union'):
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600288 self.genStruct(typeinfo, name)
289 #
290 # Append a definition to the specified section
291 def appendSection(self, section, text):
292 # self.sections[section].append('SECTION: ' + section + '\n')
293 self.sections[section].append(text)
294 #
295 # Check if the parameter passed in is a pointer
296 def paramIsPointer(self, param):
297 ispointer = False
298 for elem in param:
299 if ((elem.tag is not 'type') and (elem.tail is not None)) and '*' in elem.tail:
300 ispointer = True
301 return ispointer
302 #
303 # Get the category of a type
304 def getTypeCategory(self, typename):
305 types = self.registry.tree.findall("types/type")
306 for elem in types:
307 if (elem.find("name") is not None and elem.find('name').text == typename) or elem.attrib.get('name') == typename:
308 return elem.attrib.get('category')
309 #
310 # Check if a parent object is dispatchable or not
311 def isHandleTypeNonDispatchable(self, handletype):
312 handle = self.registry.tree.find("types/type/[name='" + handletype + "'][@category='handle']")
313 if handle is not None and handle.find('type').text == 'VK_DEFINE_NON_DISPATCHABLE_HANDLE':
314 return True
315 else:
316 return False
317 #
318 # Retrieve the type and name for a parameter
319 def getTypeNameTuple(self, param):
320 type = ''
321 name = ''
322 for elem in param:
323 if elem.tag == 'type':
324 type = noneStr(elem.text)
325 elif elem.tag == 'name':
326 name = noneStr(elem.text)
327 return (type, name)
328 #
329 # Retrieve the value of the len tag
330 def getLen(self, param):
331 result = None
332 len = param.attrib.get('len')
333 if len and len != 'null-terminated':
334 # For string arrays, 'len' can look like 'count,null-terminated', indicating that we
335 # have a null terminated array of strings. We strip the null-terminated from the
336 # 'len' field and only return the parameter specifying the string count
337 if 'null-terminated' in len:
338 result = len.split(',')[0]
339 else:
340 result = len
341 # Spec has now notation for len attributes, using :: instead of platform specific pointer symbol
342 result = str(result).replace('::', '->')
343 return result
344 #
345 # Generate a VkStructureType based on a structure typename
346 def genVkStructureType(self, typename):
347 # Add underscore between lowercase then uppercase
348 value = re.sub('([a-z0-9])([A-Z])', r'\1_\2', typename)
349 # Change to uppercase
350 value = value.upper()
351 # Add STRUCTURE_TYPE_
352 return re.sub('VK_', 'VK_STRUCTURE_TYPE_', value)
353 #
354 # Struct parameter check generation.
355 # This is a special case of the <type> tag where the contents are interpreted as a set of
356 # <member> tags instead of freeform C type declarations. The <member> tags are just like
357 # <param> tags - they are a declaration of a struct or union member. Only simple member
358 # declarations are supported (no nested structs etc.)
359 def genStruct(self, typeinfo, typeName):
360 OutputGenerator.genStruct(self, typeinfo, typeName)
361 members = typeinfo.elem.findall('.//member')
362 # Iterate over members once to get length parameters for arrays
363 lens = set()
364 for member in members:
365 len = self.getLen(member)
366 if len:
367 lens.add(len)
368 # Generate member info
369 membersInfo = []
370 for member in members:
371 # Get the member's type and name
372 info = self.getTypeNameTuple(member)
373 type = info[0]
374 name = info[1]
375 cdecl = self.makeCParamDecl(member, 0)
376 # Process VkStructureType
377 if type == 'VkStructureType':
378 # Extract the required struct type value from the comments
379 # embedded in the original text defining the 'typeinfo' element
380 rawXml = etree.tostring(typeinfo.elem).decode('ascii')
381 result = re.search(r'VK_STRUCTURE_TYPE_\w+', rawXml)
382 if result:
383 value = result.group(0)
384 else:
385 value = self.genVkStructureType(typeName)
Mark Lobodzinski85904632017-04-06 10:07:42 -0600386 # Store the required type value
387 self.structTypes[typeName] = self.StructType(name=name, value=value)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600388 # Store pointer/array/string info
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600389 extstructs = self.registry.validextensionstructs[typeName] if name == 'pNext' else None
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600390 membersInfo.append(self.CommandParam(type=type,
391 name=name,
392 ispointer=self.paramIsPointer(member),
393 isconst=True if 'const' in cdecl else False,
394 iscount=True if name in lens else False,
395 len=self.getLen(member),
Mark Lobodzinski85904632017-04-06 10:07:42 -0600396 extstructs=extstructs,
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600397 cdecl=cdecl,
398 islocal=False,
399 iscreate=False,
Mark Lobodzinski85904632017-04-06 10:07:42 -0600400 isdestroy=False,
401 feature_protect=self.featureExtraProtect))
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600402 self.structMembers.append(self.StructMemberData(name=typeName, members=membersInfo))
Mark Lobodzinski85904632017-04-06 10:07:42 -0600403
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600404 #
405 # Insert a lock_guard line
406 def lock_guard(self, indent):
407 return '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % indent
408 #
409 # Determine if a struct has an NDO as a member or an embedded member
410 def struct_contains_ndo(self, struct_item):
411 struct_member_dict = dict(self.structMembers)
412 struct_members = struct_member_dict[struct_item]
413
414 for member in struct_members:
415 if self.isHandleTypeNonDispatchable(member.type):
416 return True
417 elif member.type in struct_member_dict:
418 if self.struct_contains_ndo(member.type) == True:
419 return True
420 return False
421 #
422 # Return list of struct members which contain, or which sub-structures contain
423 # an NDO in a given list of parameters or members
424 def getParmeterStructsWithNdos(self, item_list):
425 struct_list = set()
426 for item in item_list:
427 paramtype = item.find('type')
428 typecategory = self.getTypeCategory(paramtype.text)
429 if typecategory == 'struct':
430 if self.struct_contains_ndo(paramtype.text) == True:
431 struct_list.add(item)
432 return struct_list
433 #
434 # Return list of non-dispatchable objects from a given list of parameters or members
435 def getNdosInParameterList(self, item_list, create_func):
436 ndo_list = set()
437 if create_func == True:
438 member_list = item_list[0:-1]
439 else:
440 member_list = item_list
441 for item in member_list:
442 if self.isHandleTypeNonDispatchable(paramtype.text):
443 ndo_list.add(item)
444 return ndo_list
445 #
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600446 # Construct list of extension structs containing handles, or extension structs that share a structextends attribute
447 # 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 -0600448 # TODO: make this recursive -- structs buried three or more levels deep are not searched for extensions
449 def GenerateCommandWrapExtensionList(self):
Mark Lobodzinski85904632017-04-06 10:07:42 -0600450 for struct in self.structMembers:
451 if (len(struct.members) > 1) and struct.members[1].extstructs is not None:
452 found = False;
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600453 for item in struct.members[1].extstructs:
Mark Lobodzinski85904632017-04-06 10:07:42 -0600454 if item != '' and self.struct_contains_ndo(item) == True:
455 found = True
456 if found == True:
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600457 for item in struct.members[1].extstructs:
Mark Lobodzinski85904632017-04-06 10:07:42 -0600458 if item != '' and item not in self.extension_structs:
459 self.extension_structs.append(item)
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600460 #
461 # Returns True if a struct may have a pNext chain containing an NDO
462 def StructWithExtensions(self, struct_type):
463 if struct_type in self.struct_member_dict:
464 param_info = self.struct_member_dict[struct_type]
465 if (len(param_info) > 1) and param_info[1].extstructs is not None:
Mike Schuchardta40d0b02017-07-23 12:47:47 -0600466 for item in param_info[1].extstructs:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600467 if item in self.extension_structs:
468 return True
469 return False
470 #
471 # Generate pNext handling function
472 def build_extension_processing_func(self):
Mark Lobodzinski85904632017-04-06 10:07:42 -0600473 # Construct helper functions to build and free pNext extension chains
474 pnext_proc = ''
475 pnext_proc += 'void *CreateUnwrappedExtensionStructs(layer_data *dev_data, const void *pNext) {\n'
476 pnext_proc += ' void *cur_pnext = const_cast<void *>(pNext);\n'
477 pnext_proc += ' void *head_pnext = NULL;\n'
478 pnext_proc += ' void *prev_ext_struct = NULL;\n'
479 pnext_proc += ' void *cur_ext_struct = NULL;\n\n'
480 pnext_proc += ' while (cur_pnext != NULL) {\n'
481 pnext_proc += ' GenericHeader *header = reinterpret_cast<GenericHeader *>(cur_pnext);\n\n'
482 pnext_proc += ' switch (header->sType) {\n'
483 for item in self.extension_structs:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600484 struct_info = self.struct_member_dict[item]
Mark Lobodzinski85904632017-04-06 10:07:42 -0600485 if struct_info[0].feature_protect is not None:
486 pnext_proc += '#ifdef %s \n' % struct_info[0].feature_protect
487 pnext_proc += ' case %s: {\n' % self.structTypes[item].value
Chris Forbes0f86c402017-05-02 18:36:39 -0700488 pnext_proc += ' safe_%s *safe_struct = new safe_%s;\n' % (item, item)
Mark Lobodzinski85904632017-04-06 10:07:42 -0600489 pnext_proc += ' safe_struct->initialize(reinterpret_cast<const %s *>(cur_pnext));\n' % item
490 # Generate code to unwrap the handles
491 indent = ' '
492 (tmp_decl, tmp_pre, tmp_post) = self.uniquify_members(struct_info, indent, 'safe_struct->', 0, False, False, False, False)
493 pnext_proc += tmp_pre
494 pnext_proc += ' cur_ext_struct = reinterpret_cast<void *>(safe_struct);\n'
495 pnext_proc += ' } break;\n'
496 if struct_info[0].feature_protect is not None:
497 pnext_proc += '#endif // %s \n' % struct_info[0].feature_protect
498 pnext_proc += '\n'
499 pnext_proc += ' default:\n'
500 pnext_proc += ' break;\n'
501 pnext_proc += ' }\n\n'
502 pnext_proc += ' // Save pointer to the first structure in the pNext chain\n'
503 pnext_proc += ' head_pnext = (head_pnext ? head_pnext : cur_ext_struct);\n\n'
504 pnext_proc += ' // For any extension structure but the first, link the last struct\'s pNext to the current ext struct\n'
505 pnext_proc += ' if (prev_ext_struct) {\n'
506 pnext_proc += ' (reinterpret_cast<GenericHeader *>(prev_ext_struct))->pNext = cur_ext_struct;\n'
507 pnext_proc += ' }\n'
508 pnext_proc += ' prev_ext_struct = cur_ext_struct;\n\n'
509 pnext_proc += ' // Process the next structure in the chain\n'
510 pnext_proc += ' cur_pnext = const_cast<void *>(header->pNext);\n'
511 pnext_proc += ' }\n'
512 pnext_proc += ' return head_pnext;\n'
513 pnext_proc += '}\n\n'
514 pnext_proc += '// Free a pNext extension chain\n'
515 pnext_proc += 'void FreeUnwrappedExtensionStructs(void *head) {\n'
516 pnext_proc += ' void * curr_ptr = head;\n'
517 pnext_proc += ' while (curr_ptr) {\n'
518 pnext_proc += ' GenericHeader *header = reinterpret_cast<GenericHeader *>(curr_ptr);\n'
519 pnext_proc += ' void *temp = curr_ptr;\n'
520 pnext_proc += ' curr_ptr = header->pNext;\n'
521 pnext_proc += ' free(temp);\n'
522 pnext_proc += ' }\n'
523 pnext_proc += '}\n'
524 return pnext_proc
525
526 #
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600527 # Generate source for creating a non-dispatchable object
528 def generate_create_ndo_code(self, indent, proto, params, cmd_info):
529 create_ndo_code = ''
Mark Young39389872017-01-19 21:10:49 -0700530 handle_type = params[-1].find('type')
531 if self.isHandleTypeNonDispatchable(handle_type.text):
532 # Check for special case where multiple handles are returned
533 ndo_array = False
534 if cmd_info[-1].len is not None:
535 ndo_array = True;
536 handle_name = params[-1].find('name')
537 create_ndo_code += '%sif (VK_SUCCESS == result) {\n' % (indent)
538 indent = self.incIndent(indent)
539 create_ndo_code += '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % (indent)
540 ndo_dest = '*%s' % handle_name.text
541 if ndo_array == True:
542 create_ndo_code += '%sfor (uint32_t index0 = 0; index0 < %s; index0++) {\n' % (indent, cmd_info[-1].len)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600543 indent = self.incIndent(indent)
Mark Young39389872017-01-19 21:10:49 -0700544 ndo_dest = '%s[index0]' % cmd_info[-1].name
Chris Forbesd73a1a02017-05-02 18:25:30 -0700545 create_ndo_code += '%s%s = WrapNew(dev_data, %s);\n' % (indent, ndo_dest, ndo_dest)
Mark Young39389872017-01-19 21:10:49 -0700546 if ndo_array == True:
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600547 indent = self.decIndent(indent)
Mark Young39389872017-01-19 21:10:49 -0700548 create_ndo_code += '%s}\n' % indent
549 indent = self.decIndent(indent)
550 create_ndo_code += '%s}\n' % (indent)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600551 return create_ndo_code
552 #
553 # Generate source for destroying a non-dispatchable object
554 def generate_destroy_ndo_code(self, indent, proto, cmd_info):
555 destroy_ndo_code = ''
556 ndo_array = False
557 if True in [destroy_txt in proto.text for destroy_txt in ['Destroy', 'Free']]:
558 # Check for special case where multiple handles are returned
559 if cmd_info[-1].len is not None:
560 ndo_array = True;
561 param = -1
562 else:
563 param = -2
564 if self.isHandleTypeNonDispatchable(cmd_info[param].type) == True:
565 if ndo_array == True:
566 # This API is freeing an array of handles. Remove them from the unique_id map.
567 destroy_ndo_code += '%sif ((VK_SUCCESS == result) && (%s)) {\n' % (indent, cmd_info[param].name)
568 indent = self.incIndent(indent)
569 destroy_ndo_code += '%sstd::unique_lock<std::mutex> lock(global_lock);\n' % (indent)
570 destroy_ndo_code += '%sfor (uint32_t index0 = 0; index0 < %s; index0++) {\n' % (indent, cmd_info[param].len)
571 indent = self.incIndent(indent)
572 destroy_ndo_code += '%s%s handle = %s[index0];\n' % (indent, cmd_info[param].type, cmd_info[param].name)
573 destroy_ndo_code += '%suint64_t unique_id = reinterpret_cast<uint64_t &>(handle);\n' % (indent)
574 destroy_ndo_code += '%sdev_data->unique_id_mapping.erase(unique_id);\n' % (indent)
575 indent = self.decIndent(indent);
576 destroy_ndo_code += '%s}\n' % indent
577 indent = self.decIndent(indent);
578 destroy_ndo_code += '%s}\n' % indent
579 else:
580 # Remove a single handle from the map
581 destroy_ndo_code += '%sstd::unique_lock<std::mutex> lock(global_lock);\n' % (indent)
582 destroy_ndo_code += '%suint64_t %s_id = reinterpret_cast<uint64_t &>(%s);\n' % (indent, cmd_info[param].name, cmd_info[param].name)
583 destroy_ndo_code += '%s%s = (%s)dev_data->unique_id_mapping[%s_id];\n' % (indent, cmd_info[param].name, cmd_info[param].type, cmd_info[param].name)
584 destroy_ndo_code += '%sdev_data->unique_id_mapping.erase(%s_id);\n' % (indent, cmd_info[param].name)
585 destroy_ndo_code += '%slock.unlock();\n' % (indent)
586 return ndo_array, destroy_ndo_code
587
588 #
589 # Clean up local declarations
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600590 def cleanUpLocalDeclarations(self, indent, prefix, name, len, index, process_pnext):
Mark Young26095c42017-05-09 13:19:04 -0600591 cleanup = '%sif (local_%s%s) {\n' % (indent, prefix, name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600592 if len is not None:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600593 if process_pnext:
594 cleanup += '%s for (uint32_t %s = 0; %s < %s%s; ++%s) {\n' % (indent, index, index, prefix, len, index)
595 cleanup += '%s FreeUnwrappedExtensionStructs(const_cast<void *>(local_%s%s[%s].pNext));\n' % (indent, prefix, name, index)
596 cleanup += '%s }\n' % indent
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600597 cleanup += '%s delete[] local_%s%s;\n' % (indent, prefix, name)
598 else:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600599 if process_pnext:
600 cleanup += '%s FreeUnwrappedExtensionStructs(const_cast<void *>(local_%s%s->pNext));\n' % (indent, prefix, name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600601 cleanup += '%s delete local_%s%s;\n' % (indent, prefix, name)
Mark Young26095c42017-05-09 13:19:04 -0600602 cleanup += "%s}\n" % (indent)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600603 return cleanup
604 #
605 # Output UO code for a single NDO (ndo_count is NULL) or a counted list of NDOs
606 def outputNDOs(self, ndo_type, ndo_name, ndo_count, prefix, index, indent, destroy_func, destroy_array, top_level):
607 decl_code = ''
608 pre_call_code = ''
609 post_call_code = ''
610 if ndo_count is not None:
611 if top_level == True:
612 decl_code += '%s%s *local_%s%s = NULL;\n' % (indent, ndo_type, prefix, ndo_name)
613 pre_call_code += '%s if (%s%s) {\n' % (indent, prefix, ndo_name)
614 indent = self.incIndent(indent)
615 if top_level == True:
616 pre_call_code += '%s local_%s%s = new %s[%s];\n' % (indent, prefix, ndo_name, ndo_type, ndo_count)
617 pre_call_code += '%s for (uint32_t %s = 0; %s < %s; ++%s) {\n' % (indent, index, index, ndo_count, index)
618 indent = self.incIndent(indent)
Chris Forbesb9889702017-05-02 18:35:12 -0700619 pre_call_code += '%s local_%s%s[%s] = Unwrap(dev_data, %s[%s]);\n' % (indent, prefix, ndo_name, index, ndo_name, index)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600620 else:
621 pre_call_code += '%s for (uint32_t %s = 0; %s < %s; ++%s) {\n' % (indent, index, index, ndo_count, index)
622 indent = self.incIndent(indent)
Chris Forbesb9889702017-05-02 18:35:12 -0700623 pre_call_code += '%s %s%s[%s] = Unwrap(dev_data, %s%s[%s]);\n' % (indent, prefix, ndo_name, index, prefix, ndo_name, index)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600624 indent = self.decIndent(indent)
625 pre_call_code += '%s }\n' % indent
626 indent = self.decIndent(indent)
627 pre_call_code += '%s }\n' % indent
628 if top_level == True:
629 post_call_code += '%sif (local_%s%s)\n' % (indent, prefix, ndo_name)
630 indent = self.incIndent(indent)
631 post_call_code += '%sdelete[] local_%s;\n' % (indent, ndo_name)
632 else:
633 if top_level == True:
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600634 if (destroy_func == False) or (destroy_array == True):
Chris Forbesb9889702017-05-02 18:35:12 -0700635 pre_call_code += '%s %s = Unwrap(dev_data, %s);\n' % (indent, ndo_name, ndo_name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600636 else:
637 # Make temp copy of this var with the 'local' removed. It may be better to not pass in 'local_'
638 # as part of the string and explicitly print it
639 fix = str(prefix).strip('local_');
640 pre_call_code += '%s if (%s%s) {\n' % (indent, fix, ndo_name)
641 indent = self.incIndent(indent)
Chris Forbesb9889702017-05-02 18:35:12 -0700642 pre_call_code += '%s %s%s = Unwrap(dev_data, %s%s);\n' % (indent, prefix, ndo_name, fix, ndo_name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600643 indent = self.decIndent(indent)
644 pre_call_code += '%s }\n' % indent
645 return decl_code, pre_call_code, post_call_code
646 #
647 # first_level_param indicates if elements are passed directly into the function else they're below a ptr/struct
648 # create_func means that this is API creates or allocates NDOs
649 # destroy_func indicates that this API destroys or frees NDOs
650 # destroy_array means that the destroy_func operated on an array of NDOs
651 def uniquify_members(self, members, indent, prefix, array_index, create_func, destroy_func, destroy_array, first_level_param):
652 decls = ''
653 pre_code = ''
654 post_code = ''
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600655 index = 'index%s' % str(array_index)
656 array_index += 1
657 # Process any NDOs in this structure and recurse for any sub-structs in this struct
658 for member in members:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600659 process_pnext = self.StructWithExtensions(member.type)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600660 # Handle NDOs
661 if self.isHandleTypeNonDispatchable(member.type) == True:
Jamie Madill24aa9742016-12-13 17:02:57 -0500662 count_name = member.len
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600663 if (count_name is not None):
664 if first_level_param == False:
665 count_name = '%s%s' % (prefix, member.len)
666
667 if (first_level_param == False) or (create_func == False):
668 (tmp_decl, tmp_pre, tmp_post) = self.outputNDOs(member.type, member.name, count_name, prefix, index, indent, destroy_func, destroy_array, first_level_param)
669 decls += tmp_decl
670 pre_code += tmp_pre
671 post_code += tmp_post
672 # Handle Structs that contain NDOs at some level
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600673 elif member.type in self.struct_member_dict:
674 # Structs at first level will have an NDO, OR, we need a safe_struct for the pnext chain
675 if self.struct_contains_ndo(member.type) == True or process_pnext:
676 struct_info = self.struct_member_dict[member.type]
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600677 # Struct Array
678 if member.len is not None:
679 # Update struct prefix
680 if first_level_param == True:
681 new_prefix = 'local_%s' % member.name
682 # Declare safe_VarType for struct
683 decls += '%ssafe_%s *%s = NULL;\n' % (indent, member.type, new_prefix)
684 else:
685 new_prefix = '%s%s' % (prefix, member.name)
686 pre_code += '%s if (%s%s) {\n' % (indent, prefix, member.name)
687 indent = self.incIndent(indent)
688 if first_level_param == True:
689 pre_code += '%s %s = new safe_%s[%s];\n' % (indent, new_prefix, member.type, member.len)
690 pre_code += '%s for (uint32_t %s = 0; %s < %s%s; ++%s) {\n' % (indent, index, index, prefix, member.len, index)
691 indent = self.incIndent(indent)
692 if first_level_param == True:
693 pre_code += '%s %s[%s].initialize(&%s[%s]);\n' % (indent, new_prefix, index, member.name, index)
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600694 if process_pnext:
695 pre_code += '%s %s[%s].pNext = CreateUnwrappedExtensionStructs(dev_data, %s[%s].pNext);\n' % (indent, new_prefix, index, new_prefix, index)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600696 local_prefix = '%s[%s].' % (new_prefix, index)
697 # Process sub-structs in this struct
698 (tmp_decl, tmp_pre, tmp_post) = self.uniquify_members(struct_info, indent, local_prefix, array_index, create_func, destroy_func, destroy_array, False)
699 decls += tmp_decl
700 pre_code += tmp_pre
701 post_code += tmp_post
702 indent = self.decIndent(indent)
703 pre_code += '%s }\n' % indent
704 indent = self.decIndent(indent)
705 pre_code += '%s }\n' % indent
706 if first_level_param == True:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600707 post_code += self.cleanUpLocalDeclarations(indent, prefix, member.name, member.len, index, process_pnext)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600708 # Single Struct
709 else:
710 # Update struct prefix
711 if first_level_param == True:
712 new_prefix = 'local_%s->' % member.name
713 decls += '%ssafe_%s *local_%s%s = NULL;\n' % (indent, member.type, prefix, member.name)
714 else:
715 new_prefix = '%s%s->' % (prefix, member.name)
716 # Declare safe_VarType for struct
717 pre_code += '%s if (%s%s) {\n' % (indent, prefix, member.name)
718 indent = self.incIndent(indent)
719 if first_level_param == True:
720 pre_code += '%s local_%s%s = new safe_%s(%s);\n' % (indent, prefix, member.name, member.type, member.name)
721 # Process sub-structs in this struct
722 (tmp_decl, tmp_pre, tmp_post) = self.uniquify_members(struct_info, indent, new_prefix, array_index, create_func, destroy_func, destroy_array, False)
723 decls += tmp_decl
724 pre_code += tmp_pre
725 post_code += tmp_post
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600726 if process_pnext:
727 pre_code += '%s local_%s%s->pNext = CreateUnwrappedExtensionStructs(dev_data, local_%s%s->pNext);\n' % (indent, prefix, member.name, prefix, member.name)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600728 indent = self.decIndent(indent)
729 pre_code += '%s }\n' % indent
730 if first_level_param == True:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600731 post_code += self.cleanUpLocalDeclarations(indent, prefix, member.name, member.len, index, process_pnext)
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600732 return decls, pre_code, post_code
733 #
734 # For a particular API, generate the non-dispatchable-object wrapping/unwrapping code
735 def generate_wrapping_code(self, cmd):
736 indent = ' '
737 proto = cmd.find('proto/name')
738 params = cmd.findall('param')
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600739
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600740 if proto.text is not None:
741 cmd_member_dict = dict(self.cmdMembers)
742 cmd_info = cmd_member_dict[proto.text]
743 # Handle ndo create/allocate operations
744 if cmd_info[0].iscreate:
745 create_ndo_code = self.generate_create_ndo_code(indent, proto, params, cmd_info)
746 else:
747 create_ndo_code = ''
748 # Handle ndo destroy/free operations
749 if cmd_info[0].isdestroy:
750 (destroy_array, destroy_ndo_code) = self.generate_destroy_ndo_code(indent, proto, cmd_info)
751 else:
752 destroy_array = False
753 destroy_ndo_code = ''
754 paramdecl = ''
755 param_pre_code = ''
756 param_post_code = ''
757 create_func = True if create_ndo_code else False
758 destroy_func = True if destroy_ndo_code else False
759 (paramdecl, param_pre_code, param_post_code) = self.uniquify_members(cmd_info, indent, '', 0, create_func, destroy_func, destroy_array, True)
760 param_post_code += create_ndo_code
761 if destroy_ndo_code:
762 if destroy_array == True:
763 param_post_code += destroy_ndo_code
764 else:
765 param_pre_code += destroy_ndo_code
766 if param_pre_code:
767 if (not destroy_func) or (destroy_array):
768 param_pre_code = '%s{\n%s%s%s%s}\n' % (' ', indent, self.lock_guard(indent), param_pre_code, indent)
769 return paramdecl, param_pre_code, param_post_code
770 #
771 # Capture command parameter info needed to wrap NDOs as well as handling some boilerplate code
772 def genCmd(self, cmdinfo, cmdname):
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600773
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600774 # Add struct-member type information to command parameter information
775 OutputGenerator.genCmd(self, cmdinfo, cmdname)
776 members = cmdinfo.elem.findall('.//param')
777 # Iterate over members once to get length parameters for arrays
778 lens = set()
779 for member in members:
780 len = self.getLen(member)
781 if len:
782 lens.add(len)
783 struct_member_dict = dict(self.structMembers)
784 # Generate member info
785 membersInfo = []
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600786 constains_extension_structs = False
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600787 for member in members:
788 # Get type and name of member
789 info = self.getTypeNameTuple(member)
790 type = info[0]
791 name = info[1]
792 cdecl = self.makeCParamDecl(member, 0)
793 # Check for parameter name in lens set
794 iscount = True if name in lens else False
795 len = self.getLen(member)
796 isconst = True if 'const' in cdecl else False
797 ispointer = self.paramIsPointer(member)
798 # Mark param as local if it is an array of NDOs
799 islocal = False;
800 if self.isHandleTypeNonDispatchable(type) == True:
801 if (len is not None) and (isconst == True):
802 islocal = True
803 # Or if it's a struct that contains an NDO
804 elif type in struct_member_dict:
805 if self.struct_contains_ndo(type) == True:
806 islocal = True
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600807 isdestroy = True if True in [destroy_txt in cmdname for destroy_txt in ['Destroy', 'Free']] else False
Mark Young39389872017-01-19 21:10:49 -0700808 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 -0600809 extstructs = self.registry.validextensionstructs[type] if name == 'pNext' else None
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600810 membersInfo.append(self.CommandParam(type=type,
811 name=name,
812 ispointer=ispointer,
813 isconst=isconst,
814 iscount=iscount,
815 len=len,
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600816 extstructs=extstructs,
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600817 cdecl=cdecl,
818 islocal=islocal,
819 iscreate=iscreate,
Mark Lobodzinski85904632017-04-06 10:07:42 -0600820 isdestroy=isdestroy,
821 feature_protect=self.featureExtraProtect))
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600822 self.cmdMembers.append(self.CmdMemberData(name=cmdname, members=membersInfo))
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600823 self.cmd_info_data.append(self.CmdInfoData(name=cmdname, cmdinfo=cmdinfo))
824 self.cmd_feature_protect.append(self.CmdExtraProtect(name=cmdname, extra_protect=self.featureExtraProtect))
825 #
826 # Create code to wrap NDOs as well as handling some boilerplate code
827 def WrapCommands(self):
Mark Lobodzinskia509c292016-10-11 14:33:07 -0600828 cmd_member_dict = dict(self.cmdMembers)
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600829 cmd_info_dict = dict(self.cmd_info_data)
830 cmd_protect_dict = dict(self.cmd_feature_protect)
831
832 for api_call in self.cmdMembers:
833 cmdname = api_call.name
834 cmdinfo = cmd_info_dict[api_call.name]
835 if cmdname in self.interface_functions:
836 continue
837 if cmdname in self.no_autogen_list:
838 decls = self.makeCDecls(cmdinfo.elem)
839 self.appendSection('command', '')
840 self.appendSection('command', '// Declare only')
841 self.appendSection('command', decls[0])
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600842 self.intercepts += [ ' {"%s", (void *)%s},' % (cmdname,cmdname[2:]) ]
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600843 continue
844 # Generate NDO wrapping/unwrapping code for all parameters
845 (api_decls, api_pre, api_post) = self.generate_wrapping_code(cmdinfo.elem)
846 # If API doesn't contain an NDO's, don't fool with it
847 if not api_decls and not api_pre and not api_post:
848 continue
849 feature_extra_protect = cmd_protect_dict[api_call.name]
850 if (feature_extra_protect != None):
851 self.appendSection('command', '')
852 self.appendSection('command', '#ifdef '+ feature_extra_protect)
853 self.intercepts += [ '#ifdef %s' % feature_extra_protect ]
854 # Add intercept to procmap
Mark Lobodzinski38686e92017-06-07 16:04:50 -0600855 self.intercepts += [ ' {"%s", (void*)%s},' % (cmdname,cmdname[2:]) ]
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600856 decls = self.makeCDecls(cmdinfo.elem)
857 self.appendSection('command', '')
858 self.appendSection('command', decls[0][:-1])
859 self.appendSection('command', '{')
860 # Setup common to call wrappers, first parameter is always dispatchable
861 dispatchable_type = cmdinfo.elem.find('param/type').text
862 dispatchable_name = cmdinfo.elem.find('param/name').text
863 # Generate local instance/pdev/device data lookup
Chris Forbes5279a8c2017-05-02 16:26:23 -0700864 if dispatchable_type in ["VkPhysicalDevice", "VkInstance"]:
865 self.appendSection('command', ' instance_layer_data *dev_data = GetLayerDataPtr(get_dispatch_key('+dispatchable_name+'), instance_layer_data_map);')
866 else:
867 self.appendSection('command', ' layer_data *dev_data = GetLayerDataPtr(get_dispatch_key('+dispatchable_name+'), layer_data_map);')
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600868 # Handle return values, if any
869 resulttype = cmdinfo.elem.find('proto/type')
870 if (resulttype != None and resulttype.text == 'void'):
871 resulttype = None
872 if (resulttype != None):
873 assignresult = resulttype.text + ' result = '
874 else:
875 assignresult = ''
876 # Pre-pend declarations and pre-api-call codegen
877 if api_decls:
878 self.appendSection('command', "\n".join(str(api_decls).rstrip().split("\n")))
879 if api_pre:
880 self.appendSection('command', "\n".join(str(api_pre).rstrip().split("\n")))
881 # Generate the API call itself
882 # Gather the parameter items
883 params = cmdinfo.elem.findall('param/name')
884 # Pull out the text for each of the parameters, separate them by commas in a list
885 paramstext = ', '.join([str(param.text) for param in params])
886 # If any of these paramters has been replaced by a local var, fix up the list
887 params = cmd_member_dict[cmdname]
888 for param in params:
Mark Lobodzinskifbd4d182017-04-07 15:31:35 -0600889 if param.islocal == True or self.StructWithExtensions(param.type):
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600890 if param.ispointer == True:
891 paramstext = paramstext.replace(param.name, '(%s %s*)local_%s' % ('const', param.type, param.name))
892 else:
893 paramstext = paramstext.replace(param.name, '(%s %s)local_%s' % ('const', param.type, param.name))
894 # Use correct dispatch table
Chris Forbes44c05302017-05-02 16:42:55 -0700895 API = cmdinfo.elem.attrib.get('name').replace('vk','dev_data->dispatch_table.',1)
Mark Lobodzinski65c6cfa2017-04-06 15:22:07 -0600896 # Put all this together for the final down-chain call
897 self.appendSection('command', ' ' + assignresult + API + '(' + paramstext + ');')
898 # And add the post-API-call codegen
899 self.appendSection('command', "\n".join(str(api_post).rstrip().split("\n")))
900 # Handle the return result variable, if any
901 if (resulttype != None):
902 self.appendSection('command', ' return result;')
903 self.appendSection('command', '}')
904 if (feature_extra_protect != None):
905 self.appendSection('command', '#endif // '+ feature_extra_protect)
906 self.intercepts += [ '#endif' ]