| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -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: Mike Stroyan <stroyan@google.com> |
| Mark Lobodzinski | d3b439e | 2017-06-07 13:08:41 -0600 | [diff] [blame] | 21 | # Author: Mark Lobodzinski <mark@lunarg.com> |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 22 | |
| 23 | import os,re,sys |
| 24 | from generator import * |
| Mark Lobodzinski | 62f7156 | 2017-10-24 13:41:18 -0600 | [diff] [blame] | 25 | from common_codegen import * |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 26 | |
| 27 | # ThreadGeneratorOptions - subclass of GeneratorOptions. |
| 28 | # |
| 29 | # Adds options used by ThreadOutputGenerator objects during threading |
| 30 | # layer generation. |
| 31 | # |
| 32 | # Additional members |
| 33 | # prefixText - list of strings to prefix generated header with |
| 34 | # (usually a copyright statement + calling convention macros). |
| 35 | # protectFile - True if multiple inclusion protection should be |
| 36 | # generated (based on the filename) around the entire header. |
| 37 | # protectFeature - True if #ifndef..#endif protection should be |
| 38 | # generated around a feature interface in the header file. |
| 39 | # genFuncPointers - True if function pointer typedefs should be |
| 40 | # generated |
| 41 | # protectProto - If conditional protection should be generated |
| 42 | # around prototype declarations, set to either '#ifdef' |
| 43 | # to require opt-in (#ifdef protectProtoStr) or '#ifndef' |
| 44 | # to require opt-out (#ifndef protectProtoStr). Otherwise |
| 45 | # set to None. |
| 46 | # protectProtoStr - #ifdef/#ifndef symbol to use around prototype |
| 47 | # declarations, if protectProto is set |
| 48 | # apicall - string to use for the function declaration prefix, |
| 49 | # such as APICALL on Windows. |
| 50 | # apientry - string to use for the calling convention macro, |
| 51 | # in typedefs, such as APIENTRY. |
| 52 | # apientryp - string to use for the calling convention macro |
| 53 | # in function pointer typedefs, such as APIENTRYP. |
| 54 | # indentFuncProto - True if prototype declarations should put each |
| 55 | # parameter on a separate line |
| 56 | # indentFuncPointer - True if typedefed function pointers should put each |
| 57 | # parameter on a separate line |
| 58 | # alignFuncParam - if nonzero and parameters are being put on a |
| 59 | # separate line, align parameter names at the specified column |
| 60 | class ThreadGeneratorOptions(GeneratorOptions): |
| 61 | def __init__(self, |
| 62 | filename = None, |
| 63 | directory = '.', |
| 64 | apiname = None, |
| 65 | profile = None, |
| 66 | versions = '.*', |
| 67 | emitversions = '.*', |
| 68 | defaultExtensions = None, |
| 69 | addExtensions = None, |
| 70 | removeExtensions = None, |
| Mark Lobodzinski | 62f7156 | 2017-10-24 13:41:18 -0600 | [diff] [blame] | 71 | emitExtensions = None, |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 72 | sortProcedure = regSortFeatures, |
| 73 | prefixText = "", |
| 74 | genFuncPointers = True, |
| 75 | protectFile = True, |
| 76 | protectFeature = True, |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 77 | apicall = '', |
| 78 | apientry = '', |
| 79 | apientryp = '', |
| 80 | indentFuncProto = True, |
| 81 | indentFuncPointer = False, |
| Mark Lobodzinski | 62f7156 | 2017-10-24 13:41:18 -0600 | [diff] [blame] | 82 | alignFuncParam = 0, |
| 83 | expandEnumerants = True): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 84 | GeneratorOptions.__init__(self, filename, directory, apiname, profile, |
| 85 | versions, emitversions, defaultExtensions, |
| Mark Lobodzinski | 62f7156 | 2017-10-24 13:41:18 -0600 | [diff] [blame] | 86 | addExtensions, removeExtensions, emitExtensions, sortProcedure) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 87 | self.prefixText = prefixText |
| 88 | self.genFuncPointers = genFuncPointers |
| 89 | self.protectFile = protectFile |
| 90 | self.protectFeature = protectFeature |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 91 | self.apicall = apicall |
| 92 | self.apientry = apientry |
| 93 | self.apientryp = apientryp |
| 94 | self.indentFuncProto = indentFuncProto |
| 95 | self.indentFuncPointer = indentFuncPointer |
| 96 | self.alignFuncParam = alignFuncParam |
| Mark Lobodzinski | 62f7156 | 2017-10-24 13:41:18 -0600 | [diff] [blame] | 97 | self.expandEnumerants = expandEnumerants |
| 98 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 99 | |
| 100 | # ThreadOutputGenerator - subclass of OutputGenerator. |
| 101 | # Generates Thread checking framework |
| 102 | # |
| 103 | # ---- methods ---- |
| 104 | # ThreadOutputGenerator(errFile, warnFile, diagFile) - args as for |
| 105 | # OutputGenerator. Defines additional internal state. |
| 106 | # ---- methods overriding base class ---- |
| 107 | # beginFile(genOpts) |
| 108 | # endFile() |
| 109 | # beginFeature(interface, emit) |
| 110 | # endFeature() |
| 111 | # genType(typeinfo,name) |
| 112 | # genStruct(typeinfo,name) |
| 113 | # genGroup(groupinfo,name) |
| 114 | # genEnum(enuminfo, name) |
| 115 | # genCmd(cmdinfo) |
| 116 | class ThreadOutputGenerator(OutputGenerator): |
| 117 | """Generate specified API interfaces in a specific style, such as a C header""" |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 118 | |
| 119 | inline_copyright_message = """ |
| 120 | // This file is ***GENERATED***. Do Not Edit. |
| 121 | // See layer_chassis_dispatch_generator.py for modifications. |
| 122 | |
| 123 | /* Copyright (c) 2015-2018 The Khronos Group Inc. |
| 124 | * Copyright (c) 2015-2018 Valve Corporation |
| 125 | * Copyright (c) 2015-2018 LunarG, Inc. |
| 126 | * Copyright (c) 2015-2018 Google Inc. |
| 127 | * |
| 128 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 129 | * you may not use this file except in compliance with the License. |
| 130 | * You may obtain a copy of the License at |
| 131 | * |
| 132 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 133 | * |
| 134 | * Unless required by applicable law or agreed to in writing, software |
| 135 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 136 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 137 | * See the License for the specific language governing permissions and |
| 138 | * limitations under the License. |
| 139 | * |
| 140 | * Author: Mark Lobodzinski <mark@lunarg.com> |
| 141 | */""" |
| 142 | |
| 143 | inline_custom_source_preamble = """ |
| 144 | void ThreadSafety::PreCallRecordAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo, |
| 145 | VkCommandBuffer *pCommandBuffers) { |
| 146 | StartReadObject(device); |
| 147 | StartWriteObject(pAllocateInfo->commandPool); |
| 148 | } |
| 149 | |
| 150 | void ThreadSafety::PostCallRecordAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo, |
| 151 | VkCommandBuffer *pCommandBuffers) { |
| 152 | FinishReadObject(device); |
| 153 | FinishWriteObject(pAllocateInfo->commandPool); |
| 154 | |
| 155 | // Record mapping from command buffer to command pool |
| 156 | for (uint32_t index = 0; index < pAllocateInfo->commandBufferCount; index++) { |
| 157 | std::lock_guard<std::mutex> lock(command_pool_lock); |
| 158 | command_pool_map[pCommandBuffers[index]] = pAllocateInfo->commandPool; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void ThreadSafety::PreCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo, |
| 163 | VkDescriptorSet *pDescriptorSets) { |
| 164 | StartReadObject(device); |
| 165 | StartWriteObject(pAllocateInfo->descriptorPool); |
| 166 | // Host access to pAllocateInfo::descriptorPool must be externally synchronized |
| 167 | } |
| 168 | |
| 169 | void ThreadSafety::PostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo, |
| 170 | VkDescriptorSet *pDescriptorSets) { |
| 171 | FinishReadObject(device); |
| 172 | FinishWriteObject(pAllocateInfo->descriptorPool); |
| 173 | // Host access to pAllocateInfo::descriptorPool must be externally synchronized |
| 174 | } |
| 175 | |
| 176 | void ThreadSafety::PreCallRecordFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount, |
| 177 | const VkCommandBuffer *pCommandBuffers) { |
| 178 | const bool lockCommandPool = false; // pool is already directly locked |
| 179 | StartReadObject(device); |
| 180 | StartWriteObject(commandPool); |
| 181 | for (uint32_t index = 0; index < commandBufferCount; index++) { |
| 182 | StartWriteObject(pCommandBuffers[index], lockCommandPool); |
| 183 | } |
| 184 | // The driver may immediately reuse command buffers in another thread. |
| 185 | // These updates need to be done before calling down to the driver. |
| 186 | for (uint32_t index = 0; index < commandBufferCount; index++) { |
| 187 | FinishWriteObject(pCommandBuffers[index], lockCommandPool); |
| 188 | std::lock_guard<std::mutex> lock(command_pool_lock); |
| 189 | command_pool_map.erase(pCommandBuffers[index]); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void ThreadSafety::PostCallRecordFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount, |
| 194 | const VkCommandBuffer *pCommandBuffers) { |
| 195 | FinishReadObject(device); |
| 196 | FinishWriteObject(commandPool); |
| 197 | } |
| 198 | |
| 199 | void ThreadSafety::PreCallRecordResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags) { |
| 200 | StartReadObject(device); |
| 201 | StartWriteObject(commandPool); |
| 202 | // Check for any uses of non-externally sync'd command buffers (for example from vkCmdExecuteCommands) |
| 203 | c_VkCommandPoolContents.StartWrite(commandPool); |
| 204 | // Host access to commandPool must be externally synchronized |
| 205 | } |
| 206 | |
| 207 | void ThreadSafety::PostCallRecordResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags) { |
| 208 | FinishReadObject(device); |
| 209 | FinishWriteObject(commandPool); |
| 210 | c_VkCommandPoolContents.FinishWrite(commandPool); |
| 211 | // Host access to commandPool must be externally synchronized |
| 212 | } |
| 213 | |
| 214 | void ThreadSafety::PreCallRecordDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) { |
| 215 | StartReadObject(device); |
| 216 | StartWriteObject(commandPool); |
| 217 | // Check for any uses of non-externally sync'd command buffers (for example from vkCmdExecuteCommands) |
| 218 | c_VkCommandPoolContents.StartWrite(commandPool); |
| 219 | // Host access to commandPool must be externally synchronized |
| 220 | } |
| 221 | |
| 222 | void ThreadSafety::PostCallRecordDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) { |
| 223 | FinishReadObject(device); |
| 224 | FinishWriteObject(commandPool); |
| 225 | c_VkCommandPoolContents.FinishWrite(commandPool); |
| 226 | } |
| 227 | |
| 228 | """ |
| 229 | |
| 230 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 231 | # This is an ordered list of sections in the header file. |
| 232 | TYPE_SECTIONS = ['include', 'define', 'basetype', 'handle', 'enum', |
| 233 | 'group', 'bitmask', 'funcpointer', 'struct'] |
| 234 | ALL_SECTIONS = TYPE_SECTIONS + ['command'] |
| 235 | def __init__(self, |
| 236 | errFile = sys.stderr, |
| 237 | warnFile = sys.stderr, |
| 238 | diagFile = sys.stdout): |
| 239 | OutputGenerator.__init__(self, errFile, warnFile, diagFile) |
| 240 | # Internal state - accumulators for different inner block text |
| 241 | self.sections = dict([(section, []) for section in self.ALL_SECTIONS]) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 242 | |
| 243 | # Check if the parameter passed in is a pointer to an array |
| 244 | def paramIsArray(self, param): |
| 245 | return param.attrib.get('len') is not None |
| 246 | |
| 247 | # Check if the parameter passed in is a pointer |
| 248 | def paramIsPointer(self, param): |
| 249 | ispointer = False |
| 250 | for elem in param: |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 251 | if ((elem.tag is not 'type') and (elem.tail is not None)) and '*' in elem.tail: |
| 252 | ispointer = True |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 253 | return ispointer |
| Mark Lobodzinski | 60b77b3 | 2017-02-14 09:16:56 -0700 | [diff] [blame] | 254 | |
| 255 | # Check if an object is a non-dispatchable handle |
| 256 | def isHandleTypeNonDispatchable(self, handletype): |
| 257 | handle = self.registry.tree.find("types/type/[name='" + handletype + "'][@category='handle']") |
| 258 | if handle is not None and handle.find('type').text == 'VK_DEFINE_NON_DISPATCHABLE_HANDLE': |
| 259 | return True |
| 260 | else: |
| 261 | return False |
| 262 | |
| 263 | # Check if an object is a dispatchable handle |
| 264 | def isHandleTypeDispatchable(self, handletype): |
| 265 | handle = self.registry.tree.find("types/type/[name='" + handletype + "'][@category='handle']") |
| 266 | if handle is not None and handle.find('type').text == 'VK_DEFINE_HANDLE': |
| 267 | return True |
| 268 | else: |
| 269 | return False |
| 270 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 271 | def makeThreadUseBlock(self, cmd, functionprefix): |
| 272 | """Generate C function pointer typedef for <command> Element""" |
| 273 | paramdecl = '' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 274 | # Find and add any parameters that are thread unsafe |
| 275 | params = cmd.findall('param') |
| 276 | for param in params: |
| 277 | paramname = param.find('name') |
| 278 | if False: # self.paramIsPointer(param): |
| 279 | paramdecl += ' // not watching use of pointer ' + paramname.text + '\n' |
| 280 | else: |
| 281 | externsync = param.attrib.get('externsync') |
| 282 | if externsync == 'true': |
| 283 | if self.paramIsArray(param): |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 284 | paramdecl += 'for (uint32_t index=0;index<' + param.attrib.get('len') + ';index++) {\n' |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 285 | paramdecl += ' ' + functionprefix + 'WriteObject(' + paramname.text + '[index]);\n' |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 286 | paramdecl += '}\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 287 | else: |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 288 | paramdecl += functionprefix + 'WriteObject(' + paramname.text + ');\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 289 | elif (param.attrib.get('externsync')): |
| 290 | if self.paramIsArray(param): |
| 291 | # Externsync can list pointers to arrays of members to synchronize |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 292 | paramdecl += 'for (uint32_t index=0;index<' + param.attrib.get('len') + ';index++) {\n' |
| 293 | second_indent = '' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 294 | for member in externsync.split(","): |
| 295 | # Replace first empty [] in member name with index |
| 296 | element = member.replace('[]','[index]',1) |
| 297 | if '[]' in element: |
| 298 | # Replace any second empty [] in element name with |
| 299 | # inner array index based on mapping array names like |
| 300 | # "pSomeThings[]" to "someThingCount" array size. |
| 301 | # This could be more robust by mapping a param member |
| 302 | # name to a struct type and "len" attribute. |
| 303 | limit = element[0:element.find('s[]')] + 'Count' |
| 304 | dotp = limit.rfind('.p') |
| 305 | limit = limit[0:dotp+1] + limit[dotp+2:dotp+3].lower() + limit[dotp+3:] |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 306 | paramdecl += ' for(uint32_t index2=0;index2<'+limit+';index2++)\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 307 | element = element.replace('[]','[index2]') |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 308 | second_indent = ' ' |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 309 | paramdecl += ' ' + second_indent + functionprefix + 'WriteObject(' + element + ');\n' |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 310 | paramdecl += '}\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 311 | else: |
| 312 | # externsync can list members to synchronize |
| 313 | for member in externsync.split(","): |
| 314 | member = str(member).replace("::", "->") |
| Mark Lobodzinski | 9c14780 | 2017-02-10 08:34:54 -0700 | [diff] [blame] | 315 | member = str(member).replace(".", "->") |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 316 | paramdecl += ' ' + functionprefix + 'WriteObject(' + member + ');\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 317 | else: |
| 318 | paramtype = param.find('type') |
| 319 | if paramtype is not None: |
| 320 | paramtype = paramtype.text |
| 321 | else: |
| 322 | paramtype = 'None' |
| Mark Lobodzinski | 60b77b3 | 2017-02-14 09:16:56 -0700 | [diff] [blame] | 323 | if (self.isHandleTypeDispatchable(paramtype) or self.isHandleTypeNonDispatchable(paramtype)) and paramtype != 'VkPhysicalDevice': |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 324 | if self.paramIsArray(param) and ('pPipelines' != paramname.text): |
| Mark Lobodzinski | 9c14780 | 2017-02-10 08:34:54 -0700 | [diff] [blame] | 325 | # Add pointer dereference for array counts that are pointer values |
| 326 | dereference = '' |
| 327 | for candidate in params: |
| 328 | if param.attrib.get('len') == candidate.find('name').text: |
| 329 | if self.paramIsPointer(candidate): |
| 330 | dereference = '*' |
| Mark Lobodzinski | 60b77b3 | 2017-02-14 09:16:56 -0700 | [diff] [blame] | 331 | param_len = str(param.attrib.get('len')).replace("::", "->") |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 332 | paramdecl += 'for (uint32_t index = 0; index < ' + dereference + param_len + '; index++) {\n' |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 333 | paramdecl += ' ' + functionprefix + 'ReadObject(' + paramname.text + '[index]);\n' |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 334 | paramdecl += '}\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 335 | elif not self.paramIsPointer(param): |
| 336 | # Pointer params are often being created. |
| 337 | # They are not being read from. |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 338 | paramdecl += functionprefix + 'ReadObject(' + paramname.text + ');\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 339 | explicitexternsyncparams = cmd.findall("param[@externsync]") |
| 340 | if (explicitexternsyncparams is not None): |
| 341 | for param in explicitexternsyncparams: |
| 342 | externsyncattrib = param.attrib.get('externsync') |
| 343 | paramname = param.find('name') |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 344 | paramdecl += '// Host access to ' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 345 | if externsyncattrib == 'true': |
| 346 | if self.paramIsArray(param): |
| 347 | paramdecl += 'each member of ' + paramname.text |
| 348 | elif self.paramIsPointer(param): |
| 349 | paramdecl += 'the object referenced by ' + paramname.text |
| 350 | else: |
| 351 | paramdecl += paramname.text |
| 352 | else: |
| 353 | paramdecl += externsyncattrib |
| 354 | paramdecl += ' must be externally synchronized\n' |
| 355 | |
| 356 | # Find and add any "implicit" parameters that are thread unsafe |
| 357 | implicitexternsyncparams = cmd.find('implicitexternsyncparams') |
| 358 | if (implicitexternsyncparams is not None): |
| 359 | for elem in implicitexternsyncparams: |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 360 | paramdecl += '// ' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 361 | paramdecl += elem.text |
| 362 | paramdecl += ' must be externally synchronized between host accesses\n' |
| 363 | |
| 364 | if (paramdecl == ''): |
| 365 | return None |
| 366 | else: |
| 367 | return paramdecl |
| 368 | def beginFile(self, genOpts): |
| 369 | OutputGenerator.beginFile(self, genOpts) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 370 | # |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 371 | # TODO: LUGMAL -- remove this and add our copyright |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 372 | # User-supplied prefix text, if any (list of strings) |
| 373 | if (genOpts.prefixText): |
| 374 | for s in genOpts.prefixText: |
| 375 | write(s, file=self.outFile) |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 376 | |
| 377 | self.header_file = (genOpts.filename == 'thread_safety.h') |
| 378 | self.source_file = (genOpts.filename == 'thread_safety.cpp') |
| 379 | |
| 380 | if not self.header_file and not self.source_file: |
| 381 | print("Error: Output Filenames have changed, update generator source.\n") |
| 382 | sys.exit(1) |
| 383 | |
| 384 | if self.source_file: |
| 385 | write('#include "chassis.h"', file=self.outFile) |
| 386 | write('#include "thread_safety_validation.h"', file=self.outFile) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 387 | self.newline() |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 388 | write(self.inline_custom_source_preamble, file=self.outFile) |
| 389 | |
| 390 | def endFile(self): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 391 | # Finish processing in superclass |
| 392 | OutputGenerator.endFile(self) |
| 393 | def beginFeature(self, interface, emit): |
| 394 | #write('// starting beginFeature', file=self.outFile) |
| 395 | # Start processing in superclass |
| 396 | OutputGenerator.beginFeature(self, interface, emit) |
| 397 | # C-specific |
| 398 | # Accumulate includes, defines, types, enums, function pointer typedefs, |
| 399 | # end function prototypes separately for this feature. They're only |
| 400 | # printed in endFeature(). |
| Mark Lobodzinski | 62f7156 | 2017-10-24 13:41:18 -0600 | [diff] [blame] | 401 | self.featureExtraProtect = GetFeatureProtect(interface) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 402 | self.sections = dict([(section, []) for section in self.ALL_SECTIONS]) |
| 403 | #write('// ending beginFeature', file=self.outFile) |
| 404 | def endFeature(self): |
| 405 | # C-specific |
| 406 | # Actually write the interface to the output file. |
| 407 | #write('// starting endFeature', file=self.outFile) |
| 408 | if (self.emit): |
| 409 | self.newline() |
| 410 | if (self.genOpts.protectFeature): |
| 411 | write('#ifndef', self.featureName, file=self.outFile) |
| 412 | # If type declarations are needed by other features based on |
| 413 | # this one, it may be necessary to suppress the ExtraProtect, |
| 414 | # or move it below the 'for section...' loop. |
| 415 | #write('// endFeature looking at self.featureExtraProtect', file=self.outFile) |
| Michał Janiszewski | 3c3ce9e | 2018-10-30 23:25:21 +0100 | [diff] [blame] | 416 | if (self.featureExtraProtect is not None): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 417 | write('#ifdef', self.featureExtraProtect, file=self.outFile) |
| 418 | #write('#define', self.featureName, '1', file=self.outFile) |
| 419 | for section in self.TYPE_SECTIONS: |
| 420 | #write('// endFeature writing section'+section, file=self.outFile) |
| 421 | contents = self.sections[section] |
| 422 | if contents: |
| 423 | write('\n'.join(contents), file=self.outFile) |
| 424 | self.newline() |
| 425 | #write('// endFeature looking at self.sections[command]', file=self.outFile) |
| 426 | if (self.sections['command']): |
| Jamie Madill | 24aa974 | 2016-12-13 17:02:57 -0500 | [diff] [blame] | 427 | write('\n'.join(self.sections['command']), end=u'', file=self.outFile) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 428 | self.newline() |
| Michał Janiszewski | 3c3ce9e | 2018-10-30 23:25:21 +0100 | [diff] [blame] | 429 | if (self.featureExtraProtect is not None): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 430 | write('#endif /*', self.featureExtraProtect, '*/', file=self.outFile) |
| 431 | if (self.genOpts.protectFeature): |
| 432 | write('#endif /*', self.featureName, '*/', file=self.outFile) |
| 433 | # Finish processing in superclass |
| 434 | OutputGenerator.endFeature(self) |
| 435 | #write('// ending endFeature', file=self.outFile) |
| 436 | # |
| 437 | # Append a definition to the specified section |
| 438 | def appendSection(self, section, text): |
| 439 | # self.sections[section].append('SECTION: ' + section + '\n') |
| 440 | self.sections[section].append(text) |
| 441 | # |
| 442 | # Type generation |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 443 | def genType(self, typeinfo, name, alias): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 444 | pass |
| 445 | # |
| 446 | # Struct (e.g. C "struct" type) generation. |
| 447 | # This is a special case of the <type> tag where the contents are |
| 448 | # interpreted as a set of <member> tags instead of freeform C |
| 449 | # C type declarations. The <member> tags are just like <param> |
| 450 | # tags - they are a declaration of a struct or union member. |
| 451 | # Only simple member declarations are supported (no nested |
| 452 | # structs etc.) |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 453 | def genStruct(self, typeinfo, typeName, alias): |
| 454 | OutputGenerator.genStruct(self, typeinfo, typeName, alias) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 455 | body = 'typedef ' + typeinfo.elem.get('category') + ' ' + typeName + ' {\n' |
| 456 | # paramdecl = self.makeCParamDecl(typeinfo.elem, self.genOpts.alignFuncParam) |
| 457 | for member in typeinfo.elem.findall('.//member'): |
| 458 | body += self.makeCParamDecl(member, self.genOpts.alignFuncParam) |
| 459 | body += ';\n' |
| 460 | body += '} ' + typeName + ';\n' |
| 461 | self.appendSection('struct', body) |
| 462 | # |
| 463 | # Group (e.g. C "enum" type) generation. |
| 464 | # These are concatenated together with other types. |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 465 | def genGroup(self, groupinfo, groupName, alias): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 466 | pass |
| 467 | # Enumerant generation |
| 468 | # <enum> tags may specify their values in several ways, but are usually |
| 469 | # just integers. |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 470 | def genEnum(self, enuminfo, name, alias): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 471 | pass |
| 472 | # |
| 473 | # Command generation |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 474 | def genCmd(self, cmdinfo, name, alias): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 475 | # Commands shadowed by interface functions and are not implemented |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 476 | # TODO: Many of these no longer need to be manually written routines. Winnow list. |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 477 | special_functions = [ |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 478 | 'vkCreateDevice', |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 479 | 'vkCreateInstance', |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 480 | 'vkAllocateCommandBuffers', |
| 481 | 'vkFreeCommandBuffers', |
| John Zulauf | e28aa34 | 2018-10-24 12:18:39 -0600 | [diff] [blame] | 482 | 'vkResetCommandPool', |
| 483 | 'vkDestroyCommandPool', |
| Mark Lobodzinski | 3bd82ad | 2017-02-16 11:45:27 -0700 | [diff] [blame] | 484 | 'vkAllocateDescriptorSets', |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 485 | 'vkQueuePresentKHR', |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 486 | ] |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 487 | if name == 'vkQueuePresentKHR' or (name in special_functions and self.source_file): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 488 | return |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 489 | |
| 490 | if (("DebugMarker" in name or "DebugUtilsObject" in name) and "EXT" in name): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 491 | self.appendSection('command', '// TODO - not wrapping EXT function ' + name) |
| 492 | return |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 493 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 494 | # Determine first if this function needs to be intercepted |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 495 | startthreadsafety = self.makeThreadUseBlock(cmdinfo.elem, 'Start') |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 496 | if startthreadsafety is None: |
| 497 | return |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 498 | finishthreadsafety = self.makeThreadUseBlock(cmdinfo.elem, 'Finish') |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 499 | |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 500 | OutputGenerator.genCmd(self, cmdinfo, name, alias) |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 501 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 502 | # setup common to call wrappers |
| 503 | # first parameter is always dispatchable |
| 504 | dispatchable_type = cmdinfo.elem.find('param/type').text |
| 505 | dispatchable_name = cmdinfo.elem.find('param/name').text |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame^] | 506 | |
| 507 | decls = self.makeCDecls(cmdinfo.elem) |
| 508 | |
| 509 | if self.source_file: |
| 510 | pre_decl = decls[0][:-1] |
| 511 | pre_decl = pre_decl.split("VKAPI_CALL ")[1] |
| 512 | pre_decl = 'void ThreadSafety::PreCallRecord' + pre_decl + ' {' |
| 513 | |
| 514 | # PreCallRecord |
| 515 | self.appendSection('command', '') |
| 516 | self.appendSection('command', pre_decl) |
| 517 | self.appendSection('command', " " + "\n ".join(str(startthreadsafety).rstrip().split("\n"))) |
| 518 | self.appendSection('command', '}') |
| 519 | |
| 520 | post_decl = pre_decl.replace('PreCallRecord', 'PostCallRecord') |
| 521 | |
| 522 | # PostCallRecord |
| 523 | self.appendSection('command', '') |
| 524 | self.appendSection('command', post_decl) |
| 525 | self.appendSection('command', " " + "\n ".join(str(finishthreadsafety).rstrip().split("\n"))) |
| 526 | self.appendSection('command', '}') |
| 527 | |
| 528 | if self.header_file: |
| 529 | pre_decl = decls[0][:-1] |
| 530 | pre_decl = pre_decl.split("VKAPI_CALL ")[1] |
| 531 | pre_decl = 'void PreCallRecord' + pre_decl + ';' |
| 532 | |
| 533 | # PreCallRecord |
| 534 | self.appendSection('command', '') |
| 535 | self.appendSection('command', pre_decl) |
| 536 | |
| 537 | post_decl = pre_decl.replace('PreCallRecord', 'PostCallRecord') |
| 538 | |
| 539 | # PostCallRecord |
| 540 | self.appendSection('command', '') |
| 541 | self.appendSection('command', post_decl) |
| 542 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 543 | # |
| 544 | # override makeProtoName to drop the "vk" prefix |
| 545 | def makeProtoName(self, name, tail): |
| 546 | return self.genOpts.apientry + name[2:] + tail |