Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 1 | #!/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 | |
| 23 | import os,re,sys |
| 24 | import xml.etree.ElementTree as etree |
| 25 | from generator import * |
| 26 | from 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 |
| 61 | class 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() |
| 114 | class 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 Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 124 | self.intercepts = [] |
Mark Lobodzinski | 8c37583 | 2017-02-09 15:58:14 -0700 | [diff] [blame] | 125 | self.instance_extensions = [] |
| 126 | self.device_extensions = [] |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 127 | # Commands which are not autogenerated but still intercepted |
| 128 | self.no_autogen_list = [ |
Jamie Madill | 24aa974 | 2016-12-13 17:02:57 -0500 | [diff] [blame] | 129 | 'vkGetDeviceProcAddr', |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 130 | 'vkGetInstanceProcAddr', |
| 131 | 'vkCreateInstance', |
| 132 | 'vkDestroyInstance', |
| 133 | 'vkCreateDevice', |
| 134 | 'vkDestroyDevice', |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 135 | 'vkCreateComputePipelines', |
| 136 | 'vkCreateGraphicsPipelines', |
| 137 | 'vkCreateSwapchainKHR', |
Dustin Graves | 9a6eb05 | 2017-03-28 14:18:54 -0600 | [diff] [blame] | 138 | 'vkCreateSharedSwapchainsKHR', |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 139 | 'vkGetSwapchainImagesKHR', |
Chris Forbes | 0f507f2 | 2017-04-16 13:13:17 +1200 | [diff] [blame] | 140 | 'vkQueuePresentKHR', |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 141 | 'vkEnumerateInstanceLayerProperties', |
| 142 | 'vkEnumerateDeviceLayerProperties', |
| 143 | 'vkEnumerateInstanceExtensionProperties', |
Mark Lobodzinski | 71703a5 | 2017-03-03 08:40:16 -0700 | [diff] [blame] | 144 | 'vkCreateDescriptorUpdateTemplateKHR', |
| 145 | 'vkDestroyDescriptorUpdateTemplateKHR', |
| 146 | 'vkUpdateDescriptorSetWithTemplateKHR', |
| 147 | 'vkCmdPushDescriptorSetWithTemplateKHR', |
Mark Lobodzinski | a096c12 | 2017-03-16 11:54:35 -0600 | [diff] [blame] | 148 | 'vkDebugMarkerSetObjectTagEXT', |
| 149 | 'vkDebugMarkerSetObjectNameEXT', |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 150 | ] |
| 151 | # Commands shadowed by interface functions and are not implemented |
| 152 | self.interface_functions = [ |
| 153 | 'vkGetPhysicalDeviceDisplayPropertiesKHR', |
| 154 | 'vkGetPhysicalDeviceDisplayPlanePropertiesKHR', |
| 155 | 'vkGetDisplayPlaneSupportedDisplaysKHR', |
| 156 | 'vkGetDisplayModePropertiesKHR', |
Norbert Nopper | 1dec9a5 | 2016-11-25 07:55:13 +0100 | [diff] [blame] | 157 | 'vkGetDisplayPlaneCapabilitiesKHR', |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 158 | # DebugReport APIs are hooked, but handled separately in the source file |
| 159 | 'vkCreateDebugReportCallbackEXT', |
| 160 | 'vkDestroyDebugReportCallbackEXT', |
| 161 | 'vkDebugReportMessageEXT', |
| 162 | ] |
| 163 | self.headerVersion = None |
| 164 | # Internal state - accumulators for different inner block text |
| 165 | self.sections = dict([(section, []) for section in self.ALL_SECTIONS]) |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 166 | |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 167 | self.cmdMembers = [] |
| 168 | self.cmd_feature_protect = [] # Save ifdef's for each command |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 169 | self.cmd_info_data = [] # Save the cmdinfo data for wrapping the handles when processing is complete |
| 170 | self.structMembers = [] # List of StructMemberData records for all Vulkan structs |
| 171 | self.extension_structs = [] # List of all structs or sister-structs containing handles |
| 172 | # A sister-struct may contain no handles but shares <validextensionstructs> with one that does |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 173 | self.structTypes = dict() # Map of Vulkan struct typename to required VkStructureType |
| 174 | self.struct_member_dict = dict() |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 175 | # Named tuples to store struct and command data |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 176 | self.StructType = namedtuple('StructType', ['name', 'value']) |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 177 | self.CmdMemberData = namedtuple('CmdMemberData', ['name', 'members']) |
| 178 | self.CmdInfoData = namedtuple('CmdInfoData', ['name', 'cmdinfo']) |
| 179 | self.CmdExtraProtect = namedtuple('CmdExtraProtect', ['name', 'extra_protect']) |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 180 | |
| 181 | self.CommandParam = namedtuple('CommandParam', ['type', 'name', 'ispointer', 'isconst', 'iscount', 'len', 'extstructs', 'cdecl', 'islocal', 'iscreate', 'isdestroy', 'feature_protect']) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 182 | self.StructMemberData = namedtuple('StructMemberData', ['name', 'members']) |
| 183 | # |
| 184 | def incIndent(self, indent): |
| 185 | inc = ' ' * self.INDENT_SPACES |
| 186 | if indent: |
| 187 | return indent + inc |
| 188 | return inc |
| 189 | # |
| 190 | def decIndent(self, indent): |
| 191 | if indent and (len(indent) > self.INDENT_SPACES): |
| 192 | return indent[:-self.INDENT_SPACES] |
| 193 | return '' |
| 194 | # |
| 195 | # Override makeProtoName to drop the "vk" prefix |
| 196 | def makeProtoName(self, name, tail): |
| 197 | return self.genOpts.apientry + name[2:] + tail |
| 198 | # |
| 199 | # Check if the parameter passed in is a pointer to an array |
| 200 | def paramIsArray(self, param): |
| 201 | return param.attrib.get('len') is not None |
| 202 | # |
| 203 | def beginFile(self, genOpts): |
| 204 | OutputGenerator.beginFile(self, genOpts) |
| 205 | # User-supplied prefix text, if any (list of strings) |
| 206 | if (genOpts.prefixText): |
| 207 | for s in genOpts.prefixText: |
| 208 | write(s, file=self.outFile) |
| 209 | # Namespace |
| 210 | self.newline() |
| 211 | write('namespace unique_objects {', file = self.outFile) |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 212 | # Now that the data is all collected and complete, generate and output the wrapping/unwrapping routines |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 213 | def endFile(self): |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 214 | |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 215 | self.struct_member_dict = dict(self.structMembers) |
| 216 | |
| 217 | # Generate the list of APIs that might need to handle wrapped extension structs |
| 218 | self.GenerateCommandWrapExtensionList() |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 219 | # Write out wrapping/unwrapping functions |
| 220 | self.WrapCommands() |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 221 | # Build and write out pNext processing function |
| 222 | extension_proc = self.build_extension_processing_func() |
| 223 | self.newline() |
| 224 | write('// Unique Objects pNext extension handling function', file=self.outFile) |
| 225 | write('%s' % extension_proc, file=self.outFile) |
| 226 | |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 227 | # Actually write the interface to the output file. |
| 228 | if (self.emit): |
| 229 | self.newline() |
| 230 | if (self.featureExtraProtect != None): |
| 231 | write('#ifdef', self.featureExtraProtect, file=self.outFile) |
| 232 | # Write the unique_objects code to the file |
| 233 | if (self.sections['command']): |
| 234 | if (self.genOpts.protectProto): |
| 235 | write(self.genOpts.protectProto, |
| 236 | self.genOpts.protectProtoStr, file=self.outFile) |
| 237 | write('\n'.join(self.sections['command']), end=u'', file=self.outFile) |
| 238 | if (self.featureExtraProtect != None): |
| 239 | write('\n#endif //', self.featureExtraProtect, file=self.outFile) |
| 240 | else: |
| 241 | self.newline() |
| 242 | |
Mark Lobodzinski | 8c37583 | 2017-02-09 15:58:14 -0700 | [diff] [blame] | 243 | # Write out device extension white list |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 244 | self.newline() |
Mark Lobodzinski | 8c37583 | 2017-02-09 15:58:14 -0700 | [diff] [blame] | 245 | write('// Layer Device Extension Whitelist', file=self.outFile) |
| 246 | write('static const char *kUniqueObjectsSupportedDeviceExtensions =', file=self.outFile) |
| 247 | for line in self.device_extensions: |
| 248 | write('%s' % line, file=self.outFile) |
| 249 | write(';\n', file=self.outFile) |
| 250 | |
| 251 | # Write out instance extension white list |
| 252 | self.newline() |
| 253 | write('// Layer Instance Extension Whitelist', file=self.outFile) |
| 254 | write('static const char *kUniqueObjectsSupportedInstanceExtensions =', file=self.outFile) |
| 255 | for line in self.instance_extensions: |
| 256 | write('%s' % line, file=self.outFile) |
| 257 | write(';\n', file=self.outFile) |
| 258 | self.newline() |
| 259 | |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 260 | # Record intercepted procedures |
| 261 | write('// intercepts', file=self.outFile) |
| 262 | write('struct { const char* name; PFN_vkVoidFunction pFunc;} procmap[] = {', file=self.outFile) |
| 263 | write('\n'.join(self.intercepts), file=self.outFile) |
| 264 | write('};\n', file=self.outFile) |
| 265 | self.newline() |
| 266 | write('} // namespace unique_objects', file=self.outFile) |
| 267 | # Finish processing in superclass |
| 268 | OutputGenerator.endFile(self) |
| 269 | # |
| 270 | def beginFeature(self, interface, emit): |
| 271 | # Start processing in superclass |
| 272 | OutputGenerator.beginFeature(self, interface, emit) |
| 273 | self.headerVersion = None |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 274 | |
Mark Lobodzinski | 8c37583 | 2017-02-09 15:58:14 -0700 | [diff] [blame] | 275 | if self.featureName != 'VK_VERSION_1_0': |
| 276 | white_list_entry = [] |
| 277 | if (self.featureExtraProtect != None): |
| 278 | white_list_entry += [ '#ifdef %s' % self.featureExtraProtect ] |
| 279 | white_list_entry += [ '"%s"' % self.featureName ] |
| 280 | if (self.featureExtraProtect != None): |
| 281 | white_list_entry += [ '#endif' ] |
| 282 | featureType = interface.get('type') |
| 283 | if featureType == 'instance': |
| 284 | self.instance_extensions += white_list_entry |
| 285 | elif featureType == 'device': |
| 286 | self.device_extensions += white_list_entry |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 287 | # |
| 288 | def endFeature(self): |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 289 | # Finish processing in superclass |
| 290 | OutputGenerator.endFeature(self) |
| 291 | # |
| 292 | def genType(self, typeinfo, name): |
| 293 | OutputGenerator.genType(self, typeinfo, name) |
| 294 | typeElem = typeinfo.elem |
| 295 | # If the type is a struct type, traverse the imbedded <member> tags generating a structure. |
| 296 | # Otherwise, emit the tag text. |
| 297 | category = typeElem.get('category') |
| 298 | if (category == 'struct' or category == 'union'): |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 299 | self.genStruct(typeinfo, name) |
| 300 | # |
| 301 | # Append a definition to the specified section |
| 302 | def appendSection(self, section, text): |
| 303 | # self.sections[section].append('SECTION: ' + section + '\n') |
| 304 | self.sections[section].append(text) |
| 305 | # |
| 306 | # Check if the parameter passed in is a pointer |
| 307 | def paramIsPointer(self, param): |
| 308 | ispointer = False |
| 309 | for elem in param: |
| 310 | if ((elem.tag is not 'type') and (elem.tail is not None)) and '*' in elem.tail: |
| 311 | ispointer = True |
| 312 | return ispointer |
| 313 | # |
| 314 | # Get the category of a type |
| 315 | def getTypeCategory(self, typename): |
| 316 | types = self.registry.tree.findall("types/type") |
| 317 | for elem in types: |
| 318 | if (elem.find("name") is not None and elem.find('name').text == typename) or elem.attrib.get('name') == typename: |
| 319 | return elem.attrib.get('category') |
| 320 | # |
| 321 | # Check if a parent object is dispatchable or not |
| 322 | def isHandleTypeNonDispatchable(self, handletype): |
| 323 | handle = self.registry.tree.find("types/type/[name='" + handletype + "'][@category='handle']") |
| 324 | if handle is not None and handle.find('type').text == 'VK_DEFINE_NON_DISPATCHABLE_HANDLE': |
| 325 | return True |
| 326 | else: |
| 327 | return False |
| 328 | # |
| 329 | # Retrieve the type and name for a parameter |
| 330 | def getTypeNameTuple(self, param): |
| 331 | type = '' |
| 332 | name = '' |
| 333 | for elem in param: |
| 334 | if elem.tag == 'type': |
| 335 | type = noneStr(elem.text) |
| 336 | elif elem.tag == 'name': |
| 337 | name = noneStr(elem.text) |
| 338 | return (type, name) |
| 339 | # |
| 340 | # Retrieve the value of the len tag |
| 341 | def getLen(self, param): |
| 342 | result = None |
| 343 | len = param.attrib.get('len') |
| 344 | if len and len != 'null-terminated': |
| 345 | # For string arrays, 'len' can look like 'count,null-terminated', indicating that we |
| 346 | # have a null terminated array of strings. We strip the null-terminated from the |
| 347 | # 'len' field and only return the parameter specifying the string count |
| 348 | if 'null-terminated' in len: |
| 349 | result = len.split(',')[0] |
| 350 | else: |
| 351 | result = len |
| 352 | # Spec has now notation for len attributes, using :: instead of platform specific pointer symbol |
| 353 | result = str(result).replace('::', '->') |
| 354 | return result |
| 355 | # |
| 356 | # Generate a VkStructureType based on a structure typename |
| 357 | def genVkStructureType(self, typename): |
| 358 | # Add underscore between lowercase then uppercase |
| 359 | value = re.sub('([a-z0-9])([A-Z])', r'\1_\2', typename) |
| 360 | # Change to uppercase |
| 361 | value = value.upper() |
| 362 | # Add STRUCTURE_TYPE_ |
| 363 | return re.sub('VK_', 'VK_STRUCTURE_TYPE_', value) |
| 364 | # |
| 365 | # Struct parameter check generation. |
| 366 | # This is a special case of the <type> tag where the contents are interpreted as a set of |
| 367 | # <member> tags instead of freeform C type declarations. The <member> tags are just like |
| 368 | # <param> tags - they are a declaration of a struct or union member. Only simple member |
| 369 | # declarations are supported (no nested structs etc.) |
| 370 | def genStruct(self, typeinfo, typeName): |
| 371 | OutputGenerator.genStruct(self, typeinfo, typeName) |
| 372 | members = typeinfo.elem.findall('.//member') |
| 373 | # Iterate over members once to get length parameters for arrays |
| 374 | lens = set() |
| 375 | for member in members: |
| 376 | len = self.getLen(member) |
| 377 | if len: |
| 378 | lens.add(len) |
| 379 | # Generate member info |
| 380 | membersInfo = [] |
| 381 | for member in members: |
| 382 | # Get the member's type and name |
| 383 | info = self.getTypeNameTuple(member) |
| 384 | type = info[0] |
| 385 | name = info[1] |
| 386 | cdecl = self.makeCParamDecl(member, 0) |
| 387 | # Process VkStructureType |
| 388 | if type == 'VkStructureType': |
| 389 | # Extract the required struct type value from the comments |
| 390 | # embedded in the original text defining the 'typeinfo' element |
| 391 | rawXml = etree.tostring(typeinfo.elem).decode('ascii') |
| 392 | result = re.search(r'VK_STRUCTURE_TYPE_\w+', rawXml) |
| 393 | if result: |
| 394 | value = result.group(0) |
| 395 | else: |
| 396 | value = self.genVkStructureType(typeName) |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 397 | # Store the required type value |
| 398 | self.structTypes[typeName] = self.StructType(name=name, value=value) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 399 | # Store pointer/array/string info |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 400 | extstructs = member.attrib.get('validextensionstructs') if name == 'pNext' else None |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 401 | membersInfo.append(self.CommandParam(type=type, |
| 402 | name=name, |
| 403 | ispointer=self.paramIsPointer(member), |
| 404 | isconst=True if 'const' in cdecl else False, |
| 405 | iscount=True if name in lens else False, |
| 406 | len=self.getLen(member), |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 407 | extstructs=extstructs, |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 408 | cdecl=cdecl, |
| 409 | islocal=False, |
| 410 | iscreate=False, |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 411 | isdestroy=False, |
| 412 | feature_protect=self.featureExtraProtect)) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 413 | self.structMembers.append(self.StructMemberData(name=typeName, members=membersInfo)) |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 414 | |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 415 | # |
| 416 | # Insert a lock_guard line |
| 417 | def lock_guard(self, indent): |
| 418 | return '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % indent |
| 419 | # |
| 420 | # Determine if a struct has an NDO as a member or an embedded member |
| 421 | def struct_contains_ndo(self, struct_item): |
| 422 | struct_member_dict = dict(self.structMembers) |
| 423 | struct_members = struct_member_dict[struct_item] |
| 424 | |
| 425 | for member in struct_members: |
| 426 | if self.isHandleTypeNonDispatchable(member.type): |
| 427 | return True |
| 428 | elif member.type in struct_member_dict: |
| 429 | if self.struct_contains_ndo(member.type) == True: |
| 430 | return True |
| 431 | return False |
| 432 | # |
| 433 | # Return list of struct members which contain, or which sub-structures contain |
| 434 | # an NDO in a given list of parameters or members |
| 435 | def getParmeterStructsWithNdos(self, item_list): |
| 436 | struct_list = set() |
| 437 | for item in item_list: |
| 438 | paramtype = item.find('type') |
| 439 | typecategory = self.getTypeCategory(paramtype.text) |
| 440 | if typecategory == 'struct': |
| 441 | if self.struct_contains_ndo(paramtype.text) == True: |
| 442 | struct_list.add(item) |
| 443 | return struct_list |
| 444 | # |
| 445 | # Return list of non-dispatchable objects from a given list of parameters or members |
| 446 | def getNdosInParameterList(self, item_list, create_func): |
| 447 | ndo_list = set() |
| 448 | if create_func == True: |
| 449 | member_list = item_list[0:-1] |
| 450 | else: |
| 451 | member_list = item_list |
| 452 | for item in member_list: |
| 453 | if self.isHandleTypeNonDispatchable(paramtype.text): |
| 454 | ndo_list.add(item) |
| 455 | return ndo_list |
| 456 | # |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 457 | # Construct list of extension structs containing handles, or extension structs that share a <validextensionstructs> |
| 458 | # tag WITH an extension struct containing handles. All extension structs in any pNext chain will have to be copied. |
| 459 | # TODO: make this recursive -- structs buried three or more levels deep are not searched for extensions |
| 460 | def GenerateCommandWrapExtensionList(self): |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 461 | for struct in self.structMembers: |
| 462 | if (len(struct.members) > 1) and struct.members[1].extstructs is not None: |
| 463 | found = False; |
| 464 | for item in struct.members[1].extstructs.split(','): |
| 465 | if item != '' and self.struct_contains_ndo(item) == True: |
| 466 | found = True |
| 467 | if found == True: |
| 468 | for item in struct.members[1].extstructs.split(','): |
| 469 | if item != '' and item not in self.extension_structs: |
| 470 | self.extension_structs.append(item) |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 471 | # |
| 472 | # Returns True if a struct may have a pNext chain containing an NDO |
| 473 | def StructWithExtensions(self, struct_type): |
| 474 | if struct_type in self.struct_member_dict: |
| 475 | param_info = self.struct_member_dict[struct_type] |
| 476 | if (len(param_info) > 1) and param_info[1].extstructs is not None: |
| 477 | for item in param_info[1].extstructs.split(','): |
| 478 | if item in self.extension_structs: |
| 479 | return True |
| 480 | return False |
| 481 | # |
| 482 | # Generate pNext handling function |
| 483 | def build_extension_processing_func(self): |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 484 | # Construct helper functions to build and free pNext extension chains |
| 485 | pnext_proc = '' |
| 486 | pnext_proc += 'void *CreateUnwrappedExtensionStructs(layer_data *dev_data, const void *pNext) {\n' |
| 487 | pnext_proc += ' void *cur_pnext = const_cast<void *>(pNext);\n' |
| 488 | pnext_proc += ' void *head_pnext = NULL;\n' |
| 489 | pnext_proc += ' void *prev_ext_struct = NULL;\n' |
| 490 | pnext_proc += ' void *cur_ext_struct = NULL;\n\n' |
| 491 | pnext_proc += ' while (cur_pnext != NULL) {\n' |
| 492 | pnext_proc += ' GenericHeader *header = reinterpret_cast<GenericHeader *>(cur_pnext);\n\n' |
| 493 | pnext_proc += ' switch (header->sType) {\n' |
| 494 | for item in self.extension_structs: |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 495 | struct_info = self.struct_member_dict[item] |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 496 | if struct_info[0].feature_protect is not None: |
| 497 | pnext_proc += '#ifdef %s \n' % struct_info[0].feature_protect |
| 498 | pnext_proc += ' case %s: {\n' % self.structTypes[item].value |
| 499 | pnext_proc += ' safe_%s *safe_struct = reinterpret_cast<safe_%s *>(new safe_%s);\n' % (item, item, item) |
| 500 | pnext_proc += ' safe_struct->initialize(reinterpret_cast<const %s *>(cur_pnext));\n' % item |
| 501 | # Generate code to unwrap the handles |
| 502 | indent = ' ' |
| 503 | (tmp_decl, tmp_pre, tmp_post) = self.uniquify_members(struct_info, indent, 'safe_struct->', 0, False, False, False, False) |
| 504 | pnext_proc += tmp_pre |
| 505 | pnext_proc += ' cur_ext_struct = reinterpret_cast<void *>(safe_struct);\n' |
| 506 | pnext_proc += ' } break;\n' |
| 507 | if struct_info[0].feature_protect is not None: |
| 508 | pnext_proc += '#endif // %s \n' % struct_info[0].feature_protect |
| 509 | pnext_proc += '\n' |
| 510 | pnext_proc += ' default:\n' |
| 511 | pnext_proc += ' break;\n' |
| 512 | pnext_proc += ' }\n\n' |
| 513 | pnext_proc += ' // Save pointer to the first structure in the pNext chain\n' |
| 514 | pnext_proc += ' head_pnext = (head_pnext ? head_pnext : cur_ext_struct);\n\n' |
| 515 | pnext_proc += ' // For any extension structure but the first, link the last struct\'s pNext to the current ext struct\n' |
| 516 | pnext_proc += ' if (prev_ext_struct) {\n' |
| 517 | pnext_proc += ' (reinterpret_cast<GenericHeader *>(prev_ext_struct))->pNext = cur_ext_struct;\n' |
| 518 | pnext_proc += ' }\n' |
| 519 | pnext_proc += ' prev_ext_struct = cur_ext_struct;\n\n' |
| 520 | pnext_proc += ' // Process the next structure in the chain\n' |
| 521 | pnext_proc += ' cur_pnext = const_cast<void *>(header->pNext);\n' |
| 522 | pnext_proc += ' }\n' |
| 523 | pnext_proc += ' return head_pnext;\n' |
| 524 | pnext_proc += '}\n\n' |
| 525 | pnext_proc += '// Free a pNext extension chain\n' |
| 526 | pnext_proc += 'void FreeUnwrappedExtensionStructs(void *head) {\n' |
| 527 | pnext_proc += ' void * curr_ptr = head;\n' |
| 528 | pnext_proc += ' while (curr_ptr) {\n' |
| 529 | pnext_proc += ' GenericHeader *header = reinterpret_cast<GenericHeader *>(curr_ptr);\n' |
| 530 | pnext_proc += ' void *temp = curr_ptr;\n' |
| 531 | pnext_proc += ' curr_ptr = header->pNext;\n' |
| 532 | pnext_proc += ' free(temp);\n' |
| 533 | pnext_proc += ' }\n' |
| 534 | pnext_proc += '}\n' |
| 535 | return pnext_proc |
| 536 | |
| 537 | # |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 538 | # Generate source for creating a non-dispatchable object |
| 539 | def generate_create_ndo_code(self, indent, proto, params, cmd_info): |
| 540 | create_ndo_code = '' |
Mark Young | 3938987 | 2017-01-19 21:10:49 -0700 | [diff] [blame] | 541 | handle_type = params[-1].find('type') |
| 542 | if self.isHandleTypeNonDispatchable(handle_type.text): |
| 543 | # Check for special case where multiple handles are returned |
| 544 | ndo_array = False |
| 545 | if cmd_info[-1].len is not None: |
| 546 | ndo_array = True; |
| 547 | handle_name = params[-1].find('name') |
| 548 | create_ndo_code += '%sif (VK_SUCCESS == result) {\n' % (indent) |
| 549 | indent = self.incIndent(indent) |
| 550 | create_ndo_code += '%sstd::lock_guard<std::mutex> lock(global_lock);\n' % (indent) |
| 551 | ndo_dest = '*%s' % handle_name.text |
| 552 | if ndo_array == True: |
| 553 | create_ndo_code += '%sfor (uint32_t index0 = 0; index0 < %s; index0++) {\n' % (indent, cmd_info[-1].len) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 554 | indent = self.incIndent(indent) |
Mark Young | 3938987 | 2017-01-19 21:10:49 -0700 | [diff] [blame] | 555 | ndo_dest = '%s[index0]' % cmd_info[-1].name |
| 556 | create_ndo_code += '%suint64_t unique_id = global_unique_id++;\n' % (indent) |
| 557 | create_ndo_code += '%sdev_data->unique_id_mapping[unique_id] = reinterpret_cast<uint64_t &>(%s);\n' % (indent, ndo_dest) |
| 558 | create_ndo_code += '%s%s = reinterpret_cast<%s&>(unique_id);\n' % (indent, ndo_dest, handle_type.text) |
| 559 | if ndo_array == True: |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 560 | indent = self.decIndent(indent) |
Mark Young | 3938987 | 2017-01-19 21:10:49 -0700 | [diff] [blame] | 561 | create_ndo_code += '%s}\n' % indent |
| 562 | indent = self.decIndent(indent) |
| 563 | create_ndo_code += '%s}\n' % (indent) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 564 | return create_ndo_code |
| 565 | # |
| 566 | # Generate source for destroying a non-dispatchable object |
| 567 | def generate_destroy_ndo_code(self, indent, proto, cmd_info): |
| 568 | destroy_ndo_code = '' |
| 569 | ndo_array = False |
| 570 | if True in [destroy_txt in proto.text for destroy_txt in ['Destroy', 'Free']]: |
| 571 | # Check for special case where multiple handles are returned |
| 572 | if cmd_info[-1].len is not None: |
| 573 | ndo_array = True; |
| 574 | param = -1 |
| 575 | else: |
| 576 | param = -2 |
| 577 | if self.isHandleTypeNonDispatchable(cmd_info[param].type) == True: |
| 578 | if ndo_array == True: |
| 579 | # This API is freeing an array of handles. Remove them from the unique_id map. |
| 580 | destroy_ndo_code += '%sif ((VK_SUCCESS == result) && (%s)) {\n' % (indent, cmd_info[param].name) |
| 581 | indent = self.incIndent(indent) |
| 582 | destroy_ndo_code += '%sstd::unique_lock<std::mutex> lock(global_lock);\n' % (indent) |
| 583 | destroy_ndo_code += '%sfor (uint32_t index0 = 0; index0 < %s; index0++) {\n' % (indent, cmd_info[param].len) |
| 584 | indent = self.incIndent(indent) |
| 585 | destroy_ndo_code += '%s%s handle = %s[index0];\n' % (indent, cmd_info[param].type, cmd_info[param].name) |
| 586 | destroy_ndo_code += '%suint64_t unique_id = reinterpret_cast<uint64_t &>(handle);\n' % (indent) |
| 587 | destroy_ndo_code += '%sdev_data->unique_id_mapping.erase(unique_id);\n' % (indent) |
| 588 | indent = self.decIndent(indent); |
| 589 | destroy_ndo_code += '%s}\n' % indent |
| 590 | indent = self.decIndent(indent); |
| 591 | destroy_ndo_code += '%s}\n' % indent |
| 592 | else: |
| 593 | # Remove a single handle from the map |
| 594 | destroy_ndo_code += '%sstd::unique_lock<std::mutex> lock(global_lock);\n' % (indent) |
| 595 | destroy_ndo_code += '%suint64_t %s_id = reinterpret_cast<uint64_t &>(%s);\n' % (indent, cmd_info[param].name, cmd_info[param].name) |
| 596 | 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) |
| 597 | destroy_ndo_code += '%sdev_data->unique_id_mapping.erase(%s_id);\n' % (indent, cmd_info[param].name) |
| 598 | destroy_ndo_code += '%slock.unlock();\n' % (indent) |
| 599 | return ndo_array, destroy_ndo_code |
| 600 | |
| 601 | # |
| 602 | # Clean up local declarations |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 603 | def cleanUpLocalDeclarations(self, indent, prefix, name, len, index, process_pnext): |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 604 | cleanup = '%sif (local_%s%s)\n' % (indent, prefix, name) |
| 605 | if len is not None: |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 606 | if process_pnext: |
| 607 | cleanup += '%s for (uint32_t %s = 0; %s < %s%s; ++%s) {\n' % (indent, index, index, prefix, len, index) |
| 608 | cleanup += '%s FreeUnwrappedExtensionStructs(const_cast<void *>(local_%s%s[%s].pNext));\n' % (indent, prefix, name, index) |
| 609 | cleanup += '%s }\n' % indent |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 610 | cleanup += '%s delete[] local_%s%s;\n' % (indent, prefix, name) |
| 611 | else: |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 612 | if process_pnext: |
| 613 | cleanup += '%s FreeUnwrappedExtensionStructs(const_cast<void *>(local_%s%s->pNext));\n' % (indent, prefix, name) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 614 | cleanup += '%s delete local_%s%s;\n' % (indent, prefix, name) |
| 615 | return cleanup |
| 616 | # |
| 617 | # Output UO code for a single NDO (ndo_count is NULL) or a counted list of NDOs |
| 618 | def outputNDOs(self, ndo_type, ndo_name, ndo_count, prefix, index, indent, destroy_func, destroy_array, top_level): |
| 619 | decl_code = '' |
| 620 | pre_call_code = '' |
| 621 | post_call_code = '' |
| 622 | if ndo_count is not None: |
| 623 | if top_level == True: |
| 624 | decl_code += '%s%s *local_%s%s = NULL;\n' % (indent, ndo_type, prefix, ndo_name) |
| 625 | pre_call_code += '%s if (%s%s) {\n' % (indent, prefix, ndo_name) |
| 626 | indent = self.incIndent(indent) |
| 627 | if top_level == True: |
| 628 | pre_call_code += '%s local_%s%s = new %s[%s];\n' % (indent, prefix, ndo_name, ndo_type, ndo_count) |
| 629 | pre_call_code += '%s for (uint32_t %s = 0; %s < %s; ++%s) {\n' % (indent, index, index, ndo_count, index) |
| 630 | indent = self.incIndent(indent) |
| 631 | pre_call_code += '%s local_%s%s[%s] = (%s)dev_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(%s[%s])];\n' % (indent, prefix, ndo_name, index, ndo_type, ndo_name, index) |
| 632 | else: |
| 633 | pre_call_code += '%s for (uint32_t %s = 0; %s < %s; ++%s) {\n' % (indent, index, index, ndo_count, index) |
| 634 | indent = self.incIndent(indent) |
| 635 | pre_call_code += '%s %s%s[%s] = (%s)dev_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(%s%s[%s])];\n' % (indent, prefix, ndo_name, index, ndo_type, prefix, ndo_name, index) |
| 636 | indent = self.decIndent(indent) |
| 637 | pre_call_code += '%s }\n' % indent |
| 638 | indent = self.decIndent(indent) |
| 639 | pre_call_code += '%s }\n' % indent |
| 640 | if top_level == True: |
| 641 | post_call_code += '%sif (local_%s%s)\n' % (indent, prefix, ndo_name) |
| 642 | indent = self.incIndent(indent) |
| 643 | post_call_code += '%sdelete[] local_%s;\n' % (indent, ndo_name) |
| 644 | else: |
| 645 | if top_level == True: |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 646 | if (destroy_func == False) or (destroy_array == True): |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 647 | pre_call_code += '%s %s = (%s)dev_data->unique_id_mapping[reinterpret_cast<uint64_t &>(%s)];\n' % (indent, ndo_name, ndo_type, ndo_name) |
| 648 | else: |
| 649 | # Make temp copy of this var with the 'local' removed. It may be better to not pass in 'local_' |
| 650 | # as part of the string and explicitly print it |
| 651 | fix = str(prefix).strip('local_'); |
| 652 | pre_call_code += '%s if (%s%s) {\n' % (indent, fix, ndo_name) |
| 653 | indent = self.incIndent(indent) |
| 654 | pre_call_code += '%s %s%s = (%s)dev_data->unique_id_mapping[reinterpret_cast<const uint64_t &>(%s%s)];\n' % (indent, prefix, ndo_name, ndo_type, fix, ndo_name) |
| 655 | indent = self.decIndent(indent) |
| 656 | pre_call_code += '%s }\n' % indent |
| 657 | return decl_code, pre_call_code, post_call_code |
| 658 | # |
| 659 | # first_level_param indicates if elements are passed directly into the function else they're below a ptr/struct |
| 660 | # create_func means that this is API creates or allocates NDOs |
| 661 | # destroy_func indicates that this API destroys or frees NDOs |
| 662 | # destroy_array means that the destroy_func operated on an array of NDOs |
| 663 | def uniquify_members(self, members, indent, prefix, array_index, create_func, destroy_func, destroy_array, first_level_param): |
| 664 | decls = '' |
| 665 | pre_code = '' |
| 666 | post_code = '' |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 667 | index = 'index%s' % str(array_index) |
| 668 | array_index += 1 |
| 669 | # Process any NDOs in this structure and recurse for any sub-structs in this struct |
| 670 | for member in members: |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 671 | process_pnext = self.StructWithExtensions(member.type) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 672 | # Handle NDOs |
| 673 | if self.isHandleTypeNonDispatchable(member.type) == True: |
Jamie Madill | 24aa974 | 2016-12-13 17:02:57 -0500 | [diff] [blame] | 674 | count_name = member.len |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 675 | if (count_name is not None): |
| 676 | if first_level_param == False: |
| 677 | count_name = '%s%s' % (prefix, member.len) |
| 678 | |
| 679 | if (first_level_param == False) or (create_func == False): |
| 680 | (tmp_decl, tmp_pre, tmp_post) = self.outputNDOs(member.type, member.name, count_name, prefix, index, indent, destroy_func, destroy_array, first_level_param) |
| 681 | decls += tmp_decl |
| 682 | pre_code += tmp_pre |
| 683 | post_code += tmp_post |
| 684 | # Handle Structs that contain NDOs at some level |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 685 | elif member.type in self.struct_member_dict: |
| 686 | # Structs at first level will have an NDO, OR, we need a safe_struct for the pnext chain |
| 687 | if self.struct_contains_ndo(member.type) == True or process_pnext: |
| 688 | struct_info = self.struct_member_dict[member.type] |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 689 | # Struct Array |
| 690 | if member.len is not None: |
| 691 | # Update struct prefix |
| 692 | if first_level_param == True: |
| 693 | new_prefix = 'local_%s' % member.name |
| 694 | # Declare safe_VarType for struct |
| 695 | decls += '%ssafe_%s *%s = NULL;\n' % (indent, member.type, new_prefix) |
| 696 | else: |
| 697 | new_prefix = '%s%s' % (prefix, member.name) |
| 698 | pre_code += '%s if (%s%s) {\n' % (indent, prefix, member.name) |
| 699 | indent = self.incIndent(indent) |
| 700 | if first_level_param == True: |
| 701 | pre_code += '%s %s = new safe_%s[%s];\n' % (indent, new_prefix, member.type, member.len) |
| 702 | pre_code += '%s for (uint32_t %s = 0; %s < %s%s; ++%s) {\n' % (indent, index, index, prefix, member.len, index) |
| 703 | indent = self.incIndent(indent) |
| 704 | if first_level_param == True: |
| 705 | pre_code += '%s %s[%s].initialize(&%s[%s]);\n' % (indent, new_prefix, index, member.name, index) |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 706 | if process_pnext: |
| 707 | pre_code += '%s %s[%s].pNext = CreateUnwrappedExtensionStructs(dev_data, %s[%s].pNext);\n' % (indent, new_prefix, index, new_prefix, index) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 708 | local_prefix = '%s[%s].' % (new_prefix, index) |
| 709 | # Process sub-structs in this struct |
| 710 | (tmp_decl, tmp_pre, tmp_post) = self.uniquify_members(struct_info, indent, local_prefix, array_index, create_func, destroy_func, destroy_array, False) |
| 711 | decls += tmp_decl |
| 712 | pre_code += tmp_pre |
| 713 | post_code += tmp_post |
| 714 | indent = self.decIndent(indent) |
| 715 | pre_code += '%s }\n' % indent |
| 716 | indent = self.decIndent(indent) |
| 717 | pre_code += '%s }\n' % indent |
| 718 | if first_level_param == True: |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 719 | post_code += self.cleanUpLocalDeclarations(indent, prefix, member.name, member.len, index, process_pnext) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 720 | # Single Struct |
| 721 | else: |
| 722 | # Update struct prefix |
| 723 | if first_level_param == True: |
| 724 | new_prefix = 'local_%s->' % member.name |
| 725 | decls += '%ssafe_%s *local_%s%s = NULL;\n' % (indent, member.type, prefix, member.name) |
| 726 | else: |
| 727 | new_prefix = '%s%s->' % (prefix, member.name) |
| 728 | # Declare safe_VarType for struct |
| 729 | pre_code += '%s if (%s%s) {\n' % (indent, prefix, member.name) |
| 730 | indent = self.incIndent(indent) |
| 731 | if first_level_param == True: |
| 732 | pre_code += '%s local_%s%s = new safe_%s(%s);\n' % (indent, prefix, member.name, member.type, member.name) |
| 733 | # Process sub-structs in this struct |
| 734 | (tmp_decl, tmp_pre, tmp_post) = self.uniquify_members(struct_info, indent, new_prefix, array_index, create_func, destroy_func, destroy_array, False) |
| 735 | decls += tmp_decl |
| 736 | pre_code += tmp_pre |
| 737 | post_code += tmp_post |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 738 | if process_pnext: |
| 739 | pre_code += '%s local_%s%s->pNext = CreateUnwrappedExtensionStructs(dev_data, local_%s%s->pNext);\n' % (indent, prefix, member.name, prefix, member.name) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 740 | indent = self.decIndent(indent) |
| 741 | pre_code += '%s }\n' % indent |
| 742 | if first_level_param == True: |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 743 | post_code += self.cleanUpLocalDeclarations(indent, prefix, member.name, member.len, index, process_pnext) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 744 | return decls, pre_code, post_code |
| 745 | # |
| 746 | # For a particular API, generate the non-dispatchable-object wrapping/unwrapping code |
| 747 | def generate_wrapping_code(self, cmd): |
| 748 | indent = ' ' |
| 749 | proto = cmd.find('proto/name') |
| 750 | params = cmd.findall('param') |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 751 | |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 752 | if proto.text is not None: |
| 753 | cmd_member_dict = dict(self.cmdMembers) |
| 754 | cmd_info = cmd_member_dict[proto.text] |
| 755 | # Handle ndo create/allocate operations |
| 756 | if cmd_info[0].iscreate: |
| 757 | create_ndo_code = self.generate_create_ndo_code(indent, proto, params, cmd_info) |
| 758 | else: |
| 759 | create_ndo_code = '' |
| 760 | # Handle ndo destroy/free operations |
| 761 | if cmd_info[0].isdestroy: |
| 762 | (destroy_array, destroy_ndo_code) = self.generate_destroy_ndo_code(indent, proto, cmd_info) |
| 763 | else: |
| 764 | destroy_array = False |
| 765 | destroy_ndo_code = '' |
| 766 | paramdecl = '' |
| 767 | param_pre_code = '' |
| 768 | param_post_code = '' |
| 769 | create_func = True if create_ndo_code else False |
| 770 | destroy_func = True if destroy_ndo_code else False |
| 771 | (paramdecl, param_pre_code, param_post_code) = self.uniquify_members(cmd_info, indent, '', 0, create_func, destroy_func, destroy_array, True) |
| 772 | param_post_code += create_ndo_code |
| 773 | if destroy_ndo_code: |
| 774 | if destroy_array == True: |
| 775 | param_post_code += destroy_ndo_code |
| 776 | else: |
| 777 | param_pre_code += destroy_ndo_code |
| 778 | if param_pre_code: |
| 779 | if (not destroy_func) or (destroy_array): |
| 780 | param_pre_code = '%s{\n%s%s%s%s}\n' % (' ', indent, self.lock_guard(indent), param_pre_code, indent) |
| 781 | return paramdecl, param_pre_code, param_post_code |
| 782 | # |
| 783 | # Capture command parameter info needed to wrap NDOs as well as handling some boilerplate code |
| 784 | def genCmd(self, cmdinfo, cmdname): |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 785 | |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 786 | # Add struct-member type information to command parameter information |
| 787 | OutputGenerator.genCmd(self, cmdinfo, cmdname) |
| 788 | members = cmdinfo.elem.findall('.//param') |
| 789 | # Iterate over members once to get length parameters for arrays |
| 790 | lens = set() |
| 791 | for member in members: |
| 792 | len = self.getLen(member) |
| 793 | if len: |
| 794 | lens.add(len) |
| 795 | struct_member_dict = dict(self.structMembers) |
| 796 | # Generate member info |
| 797 | membersInfo = [] |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 798 | constains_extension_structs = False |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 799 | for member in members: |
| 800 | # Get type and name of member |
| 801 | info = self.getTypeNameTuple(member) |
| 802 | type = info[0] |
| 803 | name = info[1] |
| 804 | cdecl = self.makeCParamDecl(member, 0) |
| 805 | # Check for parameter name in lens set |
| 806 | iscount = True if name in lens else False |
| 807 | len = self.getLen(member) |
| 808 | isconst = True if 'const' in cdecl else False |
| 809 | ispointer = self.paramIsPointer(member) |
| 810 | # Mark param as local if it is an array of NDOs |
| 811 | islocal = False; |
| 812 | if self.isHandleTypeNonDispatchable(type) == True: |
| 813 | if (len is not None) and (isconst == True): |
| 814 | islocal = True |
| 815 | # Or if it's a struct that contains an NDO |
| 816 | elif type in struct_member_dict: |
| 817 | if self.struct_contains_ndo(type) == True: |
| 818 | islocal = True |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 819 | isdestroy = True if True in [destroy_txt in cmdname for destroy_txt in ['Destroy', 'Free']] else False |
Mark Young | 3938987 | 2017-01-19 21:10:49 -0700 | [diff] [blame] | 820 | iscreate = True if True in [create_txt in cmdname for create_txt in ['Create', 'Allocate', 'GetRandROutputDisplayEXT', 'RegisterDeviceEvent', 'RegisterDisplayEvent']] else False |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 821 | extstructs = member.attrib.get('validextensionstructs') if name == 'pNext' else None |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 822 | membersInfo.append(self.CommandParam(type=type, |
| 823 | name=name, |
| 824 | ispointer=ispointer, |
| 825 | isconst=isconst, |
| 826 | iscount=iscount, |
| 827 | len=len, |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 828 | extstructs=extstructs, |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 829 | cdecl=cdecl, |
| 830 | islocal=islocal, |
| 831 | iscreate=iscreate, |
Mark Lobodzinski | 8590463 | 2017-04-06 10:07:42 -0600 | [diff] [blame] | 832 | isdestroy=isdestroy, |
| 833 | feature_protect=self.featureExtraProtect)) |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 834 | self.cmdMembers.append(self.CmdMemberData(name=cmdname, members=membersInfo)) |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 835 | self.cmd_info_data.append(self.CmdInfoData(name=cmdname, cmdinfo=cmdinfo)) |
| 836 | self.cmd_feature_protect.append(self.CmdExtraProtect(name=cmdname, extra_protect=self.featureExtraProtect)) |
| 837 | # |
| 838 | # Create code to wrap NDOs as well as handling some boilerplate code |
| 839 | def WrapCommands(self): |
Mark Lobodzinski | a509c29 | 2016-10-11 14:33:07 -0600 | [diff] [blame] | 840 | cmd_member_dict = dict(self.cmdMembers) |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 841 | cmd_info_dict = dict(self.cmd_info_data) |
| 842 | cmd_protect_dict = dict(self.cmd_feature_protect) |
| 843 | |
| 844 | for api_call in self.cmdMembers: |
| 845 | cmdname = api_call.name |
| 846 | cmdinfo = cmd_info_dict[api_call.name] |
| 847 | if cmdname in self.interface_functions: |
| 848 | continue |
| 849 | if cmdname in self.no_autogen_list: |
| 850 | decls = self.makeCDecls(cmdinfo.elem) |
| 851 | self.appendSection('command', '') |
| 852 | self.appendSection('command', '// Declare only') |
| 853 | self.appendSection('command', decls[0]) |
| 854 | self.intercepts += [ ' {"%s", reinterpret_cast<PFN_vkVoidFunction>(%s)},' % (cmdname,cmdname[2:]) ] |
| 855 | continue |
| 856 | # Generate NDO wrapping/unwrapping code for all parameters |
| 857 | (api_decls, api_pre, api_post) = self.generate_wrapping_code(cmdinfo.elem) |
| 858 | # If API doesn't contain an NDO's, don't fool with it |
| 859 | if not api_decls and not api_pre and not api_post: |
| 860 | continue |
| 861 | feature_extra_protect = cmd_protect_dict[api_call.name] |
| 862 | if (feature_extra_protect != None): |
| 863 | self.appendSection('command', '') |
| 864 | self.appendSection('command', '#ifdef '+ feature_extra_protect) |
| 865 | self.intercepts += [ '#ifdef %s' % feature_extra_protect ] |
| 866 | # Add intercept to procmap |
| 867 | self.intercepts += [ ' {"%s", reinterpret_cast<PFN_vkVoidFunction>(%s)},' % (cmdname,cmdname[2:]) ] |
| 868 | decls = self.makeCDecls(cmdinfo.elem) |
| 869 | self.appendSection('command', '') |
| 870 | self.appendSection('command', decls[0][:-1]) |
| 871 | self.appendSection('command', '{') |
| 872 | # Setup common to call wrappers, first parameter is always dispatchable |
| 873 | dispatchable_type = cmdinfo.elem.find('param/type').text |
| 874 | dispatchable_name = cmdinfo.elem.find('param/name').text |
| 875 | # Generate local instance/pdev/device data lookup |
Chris Forbes | 5279a8c | 2017-05-02 16:26:23 -0700 | [diff] [blame] | 876 | if dispatchable_type in ["VkPhysicalDevice", "VkInstance"]: |
| 877 | self.appendSection('command', ' instance_layer_data *dev_data = GetLayerDataPtr(get_dispatch_key('+dispatchable_name+'), instance_layer_data_map);') |
| 878 | else: |
| 879 | self.appendSection('command', ' layer_data *dev_data = GetLayerDataPtr(get_dispatch_key('+dispatchable_name+'), layer_data_map);') |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 880 | # Handle return values, if any |
| 881 | resulttype = cmdinfo.elem.find('proto/type') |
| 882 | if (resulttype != None and resulttype.text == 'void'): |
| 883 | resulttype = None |
| 884 | if (resulttype != None): |
| 885 | assignresult = resulttype.text + ' result = ' |
| 886 | else: |
| 887 | assignresult = '' |
| 888 | # Pre-pend declarations and pre-api-call codegen |
| 889 | if api_decls: |
| 890 | self.appendSection('command', "\n".join(str(api_decls).rstrip().split("\n"))) |
| 891 | if api_pre: |
| 892 | self.appendSection('command', "\n".join(str(api_pre).rstrip().split("\n"))) |
| 893 | # Generate the API call itself |
| 894 | # Gather the parameter items |
| 895 | params = cmdinfo.elem.findall('param/name') |
| 896 | # Pull out the text for each of the parameters, separate them by commas in a list |
| 897 | paramstext = ', '.join([str(param.text) for param in params]) |
| 898 | # If any of these paramters has been replaced by a local var, fix up the list |
| 899 | params = cmd_member_dict[cmdname] |
| 900 | for param in params: |
Mark Lobodzinski | fbd4d18 | 2017-04-07 15:31:35 -0600 | [diff] [blame] | 901 | if param.islocal == True or self.StructWithExtensions(param.type): |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 902 | if param.ispointer == True: |
| 903 | paramstext = paramstext.replace(param.name, '(%s %s*)local_%s' % ('const', param.type, param.name)) |
| 904 | else: |
| 905 | paramstext = paramstext.replace(param.name, '(%s %s)local_%s' % ('const', param.type, param.name)) |
| 906 | # Use correct dispatch table |
Chris Forbes | 44c0530 | 2017-05-02 16:42:55 -0700 | [diff] [blame^] | 907 | API = cmdinfo.elem.attrib.get('name').replace('vk','dev_data->dispatch_table.',1) |
Mark Lobodzinski | 65c6cfa | 2017-04-06 15:22:07 -0600 | [diff] [blame] | 908 | # Put all this together for the final down-chain call |
| 909 | self.appendSection('command', ' ' + assignresult + API + '(' + paramstext + ');') |
| 910 | # And add the post-API-call codegen |
| 911 | self.appendSection('command', "\n".join(str(api_post).rstrip().split("\n"))) |
| 912 | # Handle the return result variable, if any |
| 913 | if (resulttype != None): |
| 914 | self.appendSection('command', ' return result;') |
| 915 | self.appendSection('command', '}') |
| 916 | if (feature_extra_protect != None): |
| 917 | self.appendSection('command', '#endif // '+ feature_extra_protect) |
| 918 | self.intercepts += [ '#endif' ] |