| 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 | |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 143 | # Note that the inline_custom_header_preamble template below contains three embedded template expansion identifiers. |
| 144 | # These get replaced with generated code sections, and are labeled: |
| 145 | # o COUNTER_CLASS_DEFINITIONS_TEMPLATE |
| 146 | # o COUNTER_CLASS_INSTANCES_TEMPLATE |
| 147 | # o COUNTER_CLASS_BODIES_TEMPLATE |
| Mark Lobodzinski | 706e52b | 2018-12-11 13:21:52 -0700 | [diff] [blame] | 148 | inline_custom_header_preamble = """ |
| 149 | #pragma once |
| 150 | |
| 151 | #include <condition_variable> |
| 152 | #include <mutex> |
| 153 | #include <vector> |
| 154 | #include <unordered_set> |
| 155 | #include <string> |
| 156 | |
| 157 | VK_DEFINE_NON_DISPATCHABLE_HANDLE(DISTINCT_NONDISPATCHABLE_PHONY_HANDLE) |
| 158 | // The following line must match the vulkan_core.h condition guarding VK_DEFINE_NON_DISPATCHABLE_HANDLE |
| 159 | #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(_M_X64) || defined(__ia64) || \ |
| 160 | defined(_M_IA64) || defined(__aarch64__) || defined(__powerpc64__) |
| 161 | // If pointers are 64-bit, then there can be separate counters for each |
| 162 | // NONDISPATCHABLE_HANDLE type. Otherwise they are all typedef uint64_t. |
| 163 | #define DISTINCT_NONDISPATCHABLE_HANDLES |
| 164 | // Make sure we catch any disagreement between us and the vulkan definition |
| 165 | static_assert(std::is_pointer<DISTINCT_NONDISPATCHABLE_PHONY_HANDLE>::value, |
| 166 | "Mismatched non-dispatchable handle handle, expected pointer type."); |
| 167 | #else |
| 168 | // Make sure we catch any disagreement between us and the vulkan definition |
| 169 | static_assert(std::is_same<uint64_t, DISTINCT_NONDISPATCHABLE_PHONY_HANDLE>::value, |
| 170 | "Mismatched non-dispatchable handle handle, expected uint64_t."); |
| 171 | #endif |
| 172 | |
| 173 | // Suppress unused warning on Linux |
| 174 | #if defined(__GNUC__) |
| 175 | #define DECORATE_UNUSED __attribute__((unused)) |
| 176 | #else |
| 177 | #define DECORATE_UNUSED |
| 178 | #endif |
| 179 | |
| 180 | // clang-format off |
| 181 | static const char DECORATE_UNUSED *kVUID_Threading_Info = "UNASSIGNED-Threading-Info"; |
| 182 | static const char DECORATE_UNUSED *kVUID_Threading_MultipleThreads = "UNASSIGNED-Threading-MultipleThreads"; |
| 183 | static const char DECORATE_UNUSED *kVUID_Threading_SingleThreadReuse = "UNASSIGNED-Threading-SingleThreadReuse"; |
| 184 | // clang-format on |
| 185 | |
| 186 | #undef DECORATE_UNUSED |
| 187 | |
| 188 | struct object_use_data { |
| 189 | loader_platform_thread_id thread; |
| 190 | int reader_count; |
| 191 | int writer_count; |
| 192 | }; |
| 193 | |
| 194 | template <typename T> |
| 195 | class counter { |
| 196 | public: |
| 197 | const char *typeName; |
| 198 | VkDebugReportObjectTypeEXT objectType; |
| 199 | debug_report_data **report_data; |
| 200 | std::unordered_map<T, object_use_data> uses; |
| 201 | std::mutex counter_lock; |
| 202 | std::condition_variable counter_condition; |
| 203 | |
| 204 | |
| 205 | void StartWrite(T object) { |
| 206 | if (object == VK_NULL_HANDLE) { |
| 207 | return; |
| 208 | } |
| 209 | bool skip = false; |
| 210 | loader_platform_thread_id tid = loader_platform_get_thread_id(); |
| 211 | std::unique_lock<std::mutex> lock(counter_lock); |
| 212 | if (uses.find(object) == uses.end()) { |
| 213 | // There is no current use of the object. Record writer thread. |
| 214 | struct object_use_data *use_data = &uses[object]; |
| 215 | use_data->reader_count = 0; |
| 216 | use_data->writer_count = 1; |
| 217 | use_data->thread = tid; |
| 218 | } else { |
| 219 | struct object_use_data *use_data = &uses[object]; |
| 220 | if (use_data->reader_count == 0) { |
| 221 | // There are no readers. Two writers just collided. |
| 222 | if (use_data->thread != tid) { |
| 223 | skip |= log_msg(*report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), |
| 224 | kVUID_Threading_MultipleThreads, |
| 225 | "THREADING ERROR : object of type %s is simultaneously used in " |
| 226 | "thread 0x%" PRIx64 " and thread 0x%" PRIx64, |
| 227 | typeName, (uint64_t)use_data->thread, (uint64_t)tid); |
| 228 | if (skip) { |
| 229 | // Wait for thread-safe access to object instead of skipping call. |
| 230 | while (uses.find(object) != uses.end()) { |
| 231 | counter_condition.wait(lock); |
| 232 | } |
| 233 | // There is now no current use of the object. Record writer thread. |
| 234 | struct object_use_data *new_use_data = &uses[object]; |
| 235 | new_use_data->thread = tid; |
| 236 | new_use_data->reader_count = 0; |
| 237 | new_use_data->writer_count = 1; |
| 238 | } else { |
| 239 | // Continue with an unsafe use of the object. |
| 240 | use_data->thread = tid; |
| 241 | use_data->writer_count += 1; |
| 242 | } |
| 243 | } else { |
| 244 | // This is either safe multiple use in one call, or recursive use. |
| 245 | // There is no way to make recursion safe. Just forge ahead. |
| 246 | use_data->writer_count += 1; |
| 247 | } |
| 248 | } else { |
| 249 | // There are readers. This writer collided with them. |
| 250 | if (use_data->thread != tid) { |
| 251 | skip |= log_msg(*report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), |
| 252 | kVUID_Threading_MultipleThreads, |
| 253 | "THREADING ERROR : object of type %s is simultaneously used in " |
| 254 | "thread 0x%" PRIx64 " and thread 0x%" PRIx64, |
| 255 | typeName, (uint64_t)use_data->thread, (uint64_t)tid); |
| 256 | if (skip) { |
| 257 | // Wait for thread-safe access to object instead of skipping call. |
| 258 | while (uses.find(object) != uses.end()) { |
| 259 | counter_condition.wait(lock); |
| 260 | } |
| 261 | // There is now no current use of the object. Record writer thread. |
| 262 | struct object_use_data *new_use_data = &uses[object]; |
| 263 | new_use_data->thread = tid; |
| 264 | new_use_data->reader_count = 0; |
| 265 | new_use_data->writer_count = 1; |
| 266 | } else { |
| 267 | // Continue with an unsafe use of the object. |
| 268 | use_data->thread = tid; |
| 269 | use_data->writer_count += 1; |
| 270 | } |
| 271 | } else { |
| 272 | // This is either safe multiple use in one call, or recursive use. |
| 273 | // There is no way to make recursion safe. Just forge ahead. |
| 274 | use_data->writer_count += 1; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void FinishWrite(T object) { |
| 281 | if (object == VK_NULL_HANDLE) { |
| 282 | return; |
| 283 | } |
| 284 | // Object is no longer in use |
| 285 | std::unique_lock<std::mutex> lock(counter_lock); |
| 286 | uses[object].writer_count -= 1; |
| 287 | if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) { |
| 288 | uses.erase(object); |
| 289 | } |
| 290 | // Notify any waiting threads that this object may be safe to use |
| 291 | lock.unlock(); |
| 292 | counter_condition.notify_all(); |
| 293 | } |
| 294 | |
| 295 | void StartRead(T object) { |
| 296 | if (object == VK_NULL_HANDLE) { |
| 297 | return; |
| 298 | } |
| 299 | bool skip = false; |
| 300 | loader_platform_thread_id tid = loader_platform_get_thread_id(); |
| 301 | std::unique_lock<std::mutex> lock(counter_lock); |
| 302 | if (uses.find(object) == uses.end()) { |
| 303 | // There is no current use of the object. Record reader count |
| 304 | struct object_use_data *use_data = &uses[object]; |
| 305 | use_data->reader_count = 1; |
| 306 | use_data->writer_count = 0; |
| 307 | use_data->thread = tid; |
| 308 | } else if (uses[object].writer_count > 0 && uses[object].thread != tid) { |
| 309 | // There is a writer of the object. |
| 310 | skip |= false; |
| 311 | log_msg(*report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, objectType, (uint64_t)(object), kVUID_Threading_MultipleThreads, |
| 312 | "THREADING ERROR : object of type %s is simultaneously used in " |
| 313 | "thread 0x%" PRIx64 " and thread 0x%" PRIx64, |
| 314 | typeName, (uint64_t)uses[object].thread, (uint64_t)tid); |
| 315 | if (skip) { |
| 316 | // Wait for thread-safe access to object instead of skipping call. |
| 317 | while (uses.find(object) != uses.end()) { |
| 318 | counter_condition.wait(lock); |
| 319 | } |
| 320 | // There is no current use of the object. Record reader count |
| 321 | struct object_use_data *use_data = &uses[object]; |
| 322 | use_data->reader_count = 1; |
| 323 | use_data->writer_count = 0; |
| 324 | use_data->thread = tid; |
| 325 | } else { |
| 326 | uses[object].reader_count += 1; |
| 327 | } |
| 328 | } else { |
| 329 | // There are other readers of the object. Increase reader count |
| 330 | uses[object].reader_count += 1; |
| 331 | } |
| 332 | } |
| 333 | void FinishRead(T object) { |
| 334 | if (object == VK_NULL_HANDLE) { |
| 335 | return; |
| 336 | } |
| 337 | std::unique_lock<std::mutex> lock(counter_lock); |
| 338 | uses[object].reader_count -= 1; |
| 339 | if ((uses[object].reader_count == 0) && (uses[object].writer_count == 0)) { |
| 340 | uses.erase(object); |
| 341 | } |
| 342 | // Notify any waiting threads that this object may be safe to use |
| 343 | lock.unlock(); |
| 344 | counter_condition.notify_all(); |
| 345 | } |
| 346 | counter(const char *name = "", VkDebugReportObjectTypeEXT type = VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, debug_report_data **rep_data = nullptr) { |
| 347 | typeName = name; |
| 348 | objectType = type; |
| 349 | report_data = rep_data; |
| 350 | } |
| 351 | }; |
| 352 | |
| 353 | |
| 354 | |
| 355 | class ThreadSafety : public ValidationObject { |
| 356 | public: |
| 357 | |
| 358 | // Override chassis read/write locks for this validation object |
| 359 | void write_lock() {} |
| 360 | void write_unlock() {} |
| 361 | |
| 362 | std::mutex command_pool_lock; |
| 363 | std::unordered_map<VkCommandBuffer, VkCommandPool> command_pool_map; |
| 364 | |
| 365 | counter<VkCommandBuffer> c_VkCommandBuffer; |
| 366 | counter<VkDevice> c_VkDevice; |
| 367 | counter<VkInstance> c_VkInstance; |
| 368 | counter<VkQueue> c_VkQueue; |
| 369 | #ifdef DISTINCT_NONDISPATCHABLE_HANDLES |
| 370 | |
| 371 | // Special entry to allow tracking of command pool Reset and Destroy |
| 372 | counter<VkCommandPool> c_VkCommandPoolContents; |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 373 | COUNTER_CLASS_DEFINITIONS_TEMPLATE |
| Mark Lobodzinski | 706e52b | 2018-12-11 13:21:52 -0700 | [diff] [blame] | 374 | |
| 375 | #else // DISTINCT_NONDISPATCHABLE_HANDLES |
| 376 | // Special entry to allow tracking of command pool Reset and Destroy |
| 377 | counter<uint64_t> c_VkCommandPoolContents; |
| 378 | |
| 379 | counter<uint64_t> c_uint64_t; |
| 380 | #endif // DISTINCT_NONDISPATCHABLE_HANDLES |
| 381 | |
| 382 | ThreadSafety() |
| 383 | : c_VkCommandBuffer("VkCommandBuffer", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, &report_data), |
| 384 | c_VkDevice("VkDevice", VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, &report_data), |
| 385 | c_VkInstance("VkInstance", VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT, &report_data), |
| 386 | c_VkQueue("VkQueue", VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT, &report_data), |
| 387 | c_VkCommandPoolContents("VkCommandPool", VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_POOL_EXT, &report_data), |
| 388 | |
| 389 | #ifdef DISTINCT_NONDISPATCHABLE_HANDLES |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 390 | COUNTER_CLASS_INSTANCES_TEMPLATE |
| 391 | |
| Mark Lobodzinski | 706e52b | 2018-12-11 13:21:52 -0700 | [diff] [blame] | 392 | |
| 393 | #else // DISTINCT_NONDISPATCHABLE_HANDLES |
| 394 | c_uint64_t("NON_DISPATCHABLE_HANDLE", VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, &report_data) |
| 395 | #endif // DISTINCT_NONDISPATCHABLE_HANDLES |
| 396 | {}; |
| 397 | |
| 398 | #define WRAPPER(type) \ |
| 399 | void StartWriteObject(type object) { \ |
| 400 | c_##type.StartWrite(object); \ |
| 401 | } \ |
| 402 | void FinishWriteObject(type object) { \ |
| 403 | c_##type.FinishWrite(object); \ |
| 404 | } \ |
| 405 | void StartReadObject(type object) { \ |
| 406 | c_##type.StartRead(object); \ |
| 407 | } \ |
| 408 | void FinishReadObject(type object) { \ |
| 409 | c_##type.FinishRead(object); \ |
| 410 | } |
| 411 | |
| 412 | WRAPPER(VkDevice) |
| 413 | WRAPPER(VkInstance) |
| 414 | WRAPPER(VkQueue) |
| 415 | #ifdef DISTINCT_NONDISPATCHABLE_HANDLES |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 416 | COUNTER_CLASS_BODIES_TEMPLATE |
| Mark Lobodzinski | 706e52b | 2018-12-11 13:21:52 -0700 | [diff] [blame] | 417 | |
| 418 | #else // DISTINCT_NONDISPATCHABLE_HANDLES |
| 419 | WRAPPER(uint64_t) |
| 420 | #endif // DISTINCT_NONDISPATCHABLE_HANDLES |
| 421 | |
| 422 | // VkCommandBuffer needs check for implicit use of command pool |
| 423 | void StartWriteObject(VkCommandBuffer object, bool lockPool = true) { |
| 424 | if (lockPool) { |
| 425 | std::unique_lock<std::mutex> lock(command_pool_lock); |
| 426 | VkCommandPool pool = command_pool_map[object]; |
| 427 | lock.unlock(); |
| 428 | StartWriteObject(pool); |
| 429 | } |
| 430 | c_VkCommandBuffer.StartWrite(object); |
| 431 | } |
| 432 | void FinishWriteObject(VkCommandBuffer object, bool lockPool = true) { |
| 433 | c_VkCommandBuffer.FinishWrite(object); |
| 434 | if (lockPool) { |
| 435 | std::unique_lock<std::mutex> lock(command_pool_lock); |
| 436 | VkCommandPool pool = command_pool_map[object]; |
| 437 | lock.unlock(); |
| 438 | FinishWriteObject(pool); |
| 439 | } |
| 440 | } |
| 441 | void StartReadObject(VkCommandBuffer object) { |
| 442 | std::unique_lock<std::mutex> lock(command_pool_lock); |
| 443 | VkCommandPool pool = command_pool_map[object]; |
| 444 | lock.unlock(); |
| 445 | // We set up a read guard against the "Contents" counter to catch conflict vs. vkResetCommandPool and vkDestroyCommandPool |
| 446 | // while *not* establishing a read guard against the command pool counter itself to avoid false postives for |
| 447 | // non-externally sync'd command buffers |
| 448 | c_VkCommandPoolContents.StartRead(pool); |
| 449 | c_VkCommandBuffer.StartRead(object); |
| 450 | } |
| 451 | void FinishReadObject(VkCommandBuffer object) { |
| 452 | c_VkCommandBuffer.FinishRead(object); |
| 453 | std::unique_lock<std::mutex> lock(command_pool_lock); |
| 454 | VkCommandPool pool = command_pool_map[object]; |
| 455 | lock.unlock(); |
| 456 | c_VkCommandPoolContents.FinishRead(pool); |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 457 | } """ |
| Mark Lobodzinski | 706e52b | 2018-12-11 13:21:52 -0700 | [diff] [blame] | 458 | |
| 459 | |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 460 | inline_custom_source_preamble = """ |
| 461 | void ThreadSafety::PreCallRecordAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo, |
| 462 | VkCommandBuffer *pCommandBuffers) { |
| 463 | StartReadObject(device); |
| 464 | StartWriteObject(pAllocateInfo->commandPool); |
| 465 | } |
| 466 | |
| 467 | void ThreadSafety::PostCallRecordAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pAllocateInfo, |
| 468 | VkCommandBuffer *pCommandBuffers) { |
| 469 | FinishReadObject(device); |
| 470 | FinishWriteObject(pAllocateInfo->commandPool); |
| 471 | |
| 472 | // Record mapping from command buffer to command pool |
| 473 | for (uint32_t index = 0; index < pAllocateInfo->commandBufferCount; index++) { |
| 474 | std::lock_guard<std::mutex> lock(command_pool_lock); |
| 475 | command_pool_map[pCommandBuffers[index]] = pAllocateInfo->commandPool; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | void ThreadSafety::PreCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo, |
| 480 | VkDescriptorSet *pDescriptorSets) { |
| 481 | StartReadObject(device); |
| 482 | StartWriteObject(pAllocateInfo->descriptorPool); |
| 483 | // Host access to pAllocateInfo::descriptorPool must be externally synchronized |
| 484 | } |
| 485 | |
| 486 | void ThreadSafety::PostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo, |
| 487 | VkDescriptorSet *pDescriptorSets) { |
| 488 | FinishReadObject(device); |
| 489 | FinishWriteObject(pAllocateInfo->descriptorPool); |
| 490 | // Host access to pAllocateInfo::descriptorPool must be externally synchronized |
| 491 | } |
| 492 | |
| 493 | void ThreadSafety::PreCallRecordFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount, |
| 494 | const VkCommandBuffer *pCommandBuffers) { |
| 495 | const bool lockCommandPool = false; // pool is already directly locked |
| 496 | StartReadObject(device); |
| 497 | StartWriteObject(commandPool); |
| 498 | for (uint32_t index = 0; index < commandBufferCount; index++) { |
| 499 | StartWriteObject(pCommandBuffers[index], lockCommandPool); |
| 500 | } |
| 501 | // The driver may immediately reuse command buffers in another thread. |
| 502 | // These updates need to be done before calling down to the driver. |
| 503 | for (uint32_t index = 0; index < commandBufferCount; index++) { |
| 504 | FinishWriteObject(pCommandBuffers[index], lockCommandPool); |
| 505 | std::lock_guard<std::mutex> lock(command_pool_lock); |
| 506 | command_pool_map.erase(pCommandBuffers[index]); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | void ThreadSafety::PostCallRecordFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount, |
| 511 | const VkCommandBuffer *pCommandBuffers) { |
| 512 | FinishReadObject(device); |
| 513 | FinishWriteObject(commandPool); |
| 514 | } |
| 515 | |
| 516 | void ThreadSafety::PreCallRecordResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags) { |
| 517 | StartReadObject(device); |
| 518 | StartWriteObject(commandPool); |
| 519 | // Check for any uses of non-externally sync'd command buffers (for example from vkCmdExecuteCommands) |
| 520 | c_VkCommandPoolContents.StartWrite(commandPool); |
| 521 | // Host access to commandPool must be externally synchronized |
| 522 | } |
| 523 | |
| 524 | void ThreadSafety::PostCallRecordResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags) { |
| 525 | FinishReadObject(device); |
| 526 | FinishWriteObject(commandPool); |
| 527 | c_VkCommandPoolContents.FinishWrite(commandPool); |
| 528 | // Host access to commandPool must be externally synchronized |
| 529 | } |
| 530 | |
| 531 | void ThreadSafety::PreCallRecordDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) { |
| 532 | StartReadObject(device); |
| 533 | StartWriteObject(commandPool); |
| 534 | // Check for any uses of non-externally sync'd command buffers (for example from vkCmdExecuteCommands) |
| 535 | c_VkCommandPoolContents.StartWrite(commandPool); |
| 536 | // Host access to commandPool must be externally synchronized |
| 537 | } |
| 538 | |
| 539 | void ThreadSafety::PostCallRecordDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks *pAllocator) { |
| 540 | FinishReadObject(device); |
| 541 | FinishWriteObject(commandPool); |
| 542 | c_VkCommandPoolContents.FinishWrite(commandPool); |
| 543 | } |
| 544 | |
| Mark Lobodzinski | 8925c05 | 2018-12-18 12:41:15 -0700 | [diff] [blame^] | 545 | // GetSwapchainImages can return a non-zero count with a NULL pSwapchainImages pointer. Let's avoid crashes by ignoring |
| 546 | // pSwapchainImages. |
| 547 | void ThreadSafety::PreCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount, |
| 548 | VkImage *pSwapchainImages) { |
| 549 | StartReadObject(device); |
| 550 | StartReadObject(swapchain); |
| 551 | } |
| 552 | |
| 553 | void ThreadSafety::PostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t *pSwapchainImageCount, |
| 554 | VkImage *pSwapchainImages) { |
| 555 | FinishReadObject(device); |
| 556 | FinishReadObject(swapchain); |
| 557 | } |
| 558 | |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 559 | """ |
| 560 | |
| 561 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 562 | # This is an ordered list of sections in the header file. |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 563 | ALL_SECTIONS = ['command'] |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 564 | def __init__(self, |
| 565 | errFile = sys.stderr, |
| 566 | warnFile = sys.stderr, |
| 567 | diagFile = sys.stdout): |
| 568 | OutputGenerator.__init__(self, errFile, warnFile, diagFile) |
| 569 | # Internal state - accumulators for different inner block text |
| 570 | self.sections = dict([(section, []) for section in self.ALL_SECTIONS]) |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 571 | self.non_dispatchable_types = set() |
| 572 | self.object_to_debug_report_type = { |
| 573 | 'VkInstance' : 'VK_DEBUG_REPORT_OBJECT_TYPE_INSTANCE_EXT', |
| 574 | 'VkPhysicalDevice' : 'VK_DEBUG_REPORT_OBJECT_TYPE_PHYSICAL_DEVICE_EXT', |
| 575 | 'VkDevice' : 'VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT', |
| 576 | 'VkQueue' : 'VK_DEBUG_REPORT_OBJECT_TYPE_QUEUE_EXT', |
| 577 | 'VkSemaphore' : 'VK_DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT', |
| 578 | 'VkCommandBuffer' : 'VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT', |
| 579 | 'VkFence' : 'VK_DEBUG_REPORT_OBJECT_TYPE_FENCE_EXT', |
| 580 | 'VkDeviceMemory' : 'VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT', |
| 581 | 'VkBuffer' : 'VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT', |
| 582 | 'VkImage' : 'VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT', |
| 583 | 'VkEvent' : 'VK_DEBUG_REPORT_OBJECT_TYPE_EVENT_EXT', |
| 584 | 'VkQueryPool' : 'VK_DEBUG_REPORT_OBJECT_TYPE_QUERY_POOL_EXT', |
| 585 | 'VkBufferView' : 'VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT', |
| 586 | 'VkImageView' : 'VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT', |
| 587 | 'VkShaderModule' : 'VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT', |
| 588 | 'VkPipelineCache' : 'VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_CACHE_EXT', |
| 589 | 'VkPipelineLayout' : 'VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_LAYOUT_EXT', |
| 590 | 'VkRenderPass' : 'VK_DEBUG_REPORT_OBJECT_TYPE_RENDER_PASS_EXT', |
| 591 | 'VkPipeline' : 'VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT', |
| 592 | 'VkDescriptorSetLayout' : 'VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT', |
| 593 | 'VkSampler' : 'VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_EXT', |
| 594 | 'VkDescriptorPool' : 'VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT', |
| 595 | 'VkDescriptorSet' : 'VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT', |
| 596 | 'VkFramebuffer' : 'VK_DEBUG_REPORT_OBJECT_TYPE_FRAMEBUFFER_EXT', |
| 597 | 'VkCommandPool' : 'VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_POOL_EXT', |
| 598 | 'VkSurfaceKHR' : 'VK_DEBUG_REPORT_OBJECT_TYPE_SURFACE_KHR_EXT', |
| 599 | 'VkSwapchainKHR' : 'VK_DEBUG_REPORT_OBJECT_TYPE_SWAPCHAIN_KHR_EXT', |
| 600 | 'VkDisplayKHR' : 'VK_DEBUG_REPORT_OBJECT_TYPE_DISPLAY_KHR_EXT', |
| 601 | 'VkDisplayModeKHR' : 'VK_DEBUG_REPORT_OBJECT_TYPE_DISPLAY_MODE_KHR_EXT', |
| 602 | 'VkObjectTableNVX' : 'VK_DEBUG_REPORT_OBJECT_TYPE_OBJECT_TABLE_NVX_EXT', |
| 603 | 'VkIndirectCommandsLayoutNVX' : 'VK_DEBUG_REPORT_OBJECT_TYPE_INDIRECT_COMMANDS_LAYOUT_NVX_EXT', |
| 604 | 'VkSamplerYcbcrConversion' : 'VK_DEBUG_REPORT_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION_EXT', |
| 605 | 'VkDescriptorUpdateTemplate' : 'VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE_EXT', |
| 606 | 'VkAccelerationStructureNV' : 'VK_DEBUG_REPORT_OBJECT_TYPE_ACCELERATION_STRUCTURE_NV_EXT', |
| 607 | 'VkDebugReportCallbackEXT' : 'VK_DEBUG_REPORT_OBJECT_TYPE_DEBUG_REPORT_EXT', |
| 608 | 'VkValidationCacheEXT' : 'VK_DEBUG_REPORT_OBJECT_TYPE_VALIDATION_CACHE_EXT' } |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 609 | |
| 610 | # Check if the parameter passed in is a pointer to an array |
| 611 | def paramIsArray(self, param): |
| 612 | return param.attrib.get('len') is not None |
| 613 | |
| 614 | # Check if the parameter passed in is a pointer |
| 615 | def paramIsPointer(self, param): |
| 616 | ispointer = False |
| 617 | for elem in param: |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 618 | if ((elem.tag is not 'type') and (elem.tail is not None)) and '*' in elem.tail: |
| 619 | ispointer = True |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 620 | return ispointer |
| Mark Lobodzinski | 60b77b3 | 2017-02-14 09:16:56 -0700 | [diff] [blame] | 621 | |
| 622 | # Check if an object is a non-dispatchable handle |
| 623 | def isHandleTypeNonDispatchable(self, handletype): |
| 624 | handle = self.registry.tree.find("types/type/[name='" + handletype + "'][@category='handle']") |
| 625 | if handle is not None and handle.find('type').text == 'VK_DEFINE_NON_DISPATCHABLE_HANDLE': |
| 626 | return True |
| 627 | else: |
| 628 | return False |
| 629 | |
| 630 | # Check if an object is a dispatchable handle |
| 631 | def isHandleTypeDispatchable(self, handletype): |
| 632 | handle = self.registry.tree.find("types/type/[name='" + handletype + "'][@category='handle']") |
| 633 | if handle is not None and handle.find('type').text == 'VK_DEFINE_HANDLE': |
| 634 | return True |
| 635 | else: |
| 636 | return False |
| 637 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 638 | def makeThreadUseBlock(self, cmd, functionprefix): |
| 639 | """Generate C function pointer typedef for <command> Element""" |
| 640 | paramdecl = '' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 641 | # Find and add any parameters that are thread unsafe |
| 642 | params = cmd.findall('param') |
| 643 | for param in params: |
| 644 | paramname = param.find('name') |
| 645 | if False: # self.paramIsPointer(param): |
| 646 | paramdecl += ' // not watching use of pointer ' + paramname.text + '\n' |
| 647 | else: |
| 648 | externsync = param.attrib.get('externsync') |
| 649 | if externsync == 'true': |
| 650 | if self.paramIsArray(param): |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 651 | 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] | 652 | paramdecl += ' ' + functionprefix + 'WriteObject(' + paramname.text + '[index]);\n' |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 653 | paramdecl += '}\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 654 | else: |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 655 | paramdecl += functionprefix + 'WriteObject(' + paramname.text + ');\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 656 | elif (param.attrib.get('externsync')): |
| 657 | if self.paramIsArray(param): |
| 658 | # Externsync can list pointers to arrays of members to synchronize |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 659 | paramdecl += 'for (uint32_t index=0;index<' + param.attrib.get('len') + ';index++) {\n' |
| 660 | second_indent = '' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 661 | for member in externsync.split(","): |
| 662 | # Replace first empty [] in member name with index |
| 663 | element = member.replace('[]','[index]',1) |
| 664 | if '[]' in element: |
| 665 | # Replace any second empty [] in element name with |
| 666 | # inner array index based on mapping array names like |
| 667 | # "pSomeThings[]" to "someThingCount" array size. |
| 668 | # This could be more robust by mapping a param member |
| 669 | # name to a struct type and "len" attribute. |
| 670 | limit = element[0:element.find('s[]')] + 'Count' |
| 671 | dotp = limit.rfind('.p') |
| 672 | 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] | 673 | paramdecl += ' for(uint32_t index2=0;index2<'+limit+';index2++)\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 674 | element = element.replace('[]','[index2]') |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 675 | second_indent = ' ' |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 676 | paramdecl += ' ' + second_indent + functionprefix + 'WriteObject(' + element + ');\n' |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 677 | paramdecl += '}\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 678 | else: |
| 679 | # externsync can list members to synchronize |
| 680 | for member in externsync.split(","): |
| 681 | member = str(member).replace("::", "->") |
| Mark Lobodzinski | 9c14780 | 2017-02-10 08:34:54 -0700 | [diff] [blame] | 682 | member = str(member).replace(".", "->") |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 683 | paramdecl += ' ' + functionprefix + 'WriteObject(' + member + ');\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 684 | else: |
| 685 | paramtype = param.find('type') |
| 686 | if paramtype is not None: |
| 687 | paramtype = paramtype.text |
| 688 | else: |
| 689 | paramtype = 'None' |
| Mark Lobodzinski | 60b77b3 | 2017-02-14 09:16:56 -0700 | [diff] [blame] | 690 | if (self.isHandleTypeDispatchable(paramtype) or self.isHandleTypeNonDispatchable(paramtype)) and paramtype != 'VkPhysicalDevice': |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 691 | if self.paramIsArray(param) and ('pPipelines' != paramname.text): |
| Mark Lobodzinski | 9c14780 | 2017-02-10 08:34:54 -0700 | [diff] [blame] | 692 | # Add pointer dereference for array counts that are pointer values |
| 693 | dereference = '' |
| 694 | for candidate in params: |
| 695 | if param.attrib.get('len') == candidate.find('name').text: |
| 696 | if self.paramIsPointer(candidate): |
| 697 | dereference = '*' |
| Mark Lobodzinski | 60b77b3 | 2017-02-14 09:16:56 -0700 | [diff] [blame] | 698 | param_len = str(param.attrib.get('len')).replace("::", "->") |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 699 | paramdecl += 'for (uint32_t index = 0; index < ' + dereference + param_len + '; index++) {\n' |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 700 | paramdecl += ' ' + functionprefix + 'ReadObject(' + paramname.text + '[index]);\n' |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 701 | paramdecl += '}\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 702 | elif not self.paramIsPointer(param): |
| 703 | # Pointer params are often being created. |
| 704 | # They are not being read from. |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 705 | paramdecl += functionprefix + 'ReadObject(' + paramname.text + ');\n' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 706 | explicitexternsyncparams = cmd.findall("param[@externsync]") |
| 707 | if (explicitexternsyncparams is not None): |
| 708 | for param in explicitexternsyncparams: |
| 709 | externsyncattrib = param.attrib.get('externsync') |
| 710 | paramname = param.find('name') |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 711 | paramdecl += '// Host access to ' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 712 | if externsyncattrib == 'true': |
| 713 | if self.paramIsArray(param): |
| 714 | paramdecl += 'each member of ' + paramname.text |
| 715 | elif self.paramIsPointer(param): |
| 716 | paramdecl += 'the object referenced by ' + paramname.text |
| 717 | else: |
| 718 | paramdecl += paramname.text |
| 719 | else: |
| 720 | paramdecl += externsyncattrib |
| 721 | paramdecl += ' must be externally synchronized\n' |
| 722 | |
| 723 | # Find and add any "implicit" parameters that are thread unsafe |
| 724 | implicitexternsyncparams = cmd.find('implicitexternsyncparams') |
| 725 | if (implicitexternsyncparams is not None): |
| 726 | for elem in implicitexternsyncparams: |
| Mark Lobodzinski | 716a4f9 | 2018-11-16 08:54:20 -0700 | [diff] [blame] | 727 | paramdecl += '// ' |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 728 | paramdecl += elem.text |
| 729 | paramdecl += ' must be externally synchronized between host accesses\n' |
| 730 | |
| 731 | if (paramdecl == ''): |
| 732 | return None |
| 733 | else: |
| 734 | return paramdecl |
| 735 | def beginFile(self, genOpts): |
| 736 | OutputGenerator.beginFile(self, genOpts) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 737 | # |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 738 | # TODO: LUGMAL -- remove this and add our copyright |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 739 | # User-supplied prefix text, if any (list of strings) |
| Mark Lobodzinski | 706e52b | 2018-12-11 13:21:52 -0700 | [diff] [blame] | 740 | write(self.inline_copyright_message, file=self.outFile) |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 741 | |
| 742 | self.header_file = (genOpts.filename == 'thread_safety.h') |
| 743 | self.source_file = (genOpts.filename == 'thread_safety.cpp') |
| 744 | |
| 745 | if not self.header_file and not self.source_file: |
| 746 | print("Error: Output Filenames have changed, update generator source.\n") |
| 747 | sys.exit(1) |
| 748 | |
| 749 | if self.source_file: |
| 750 | write('#include "chassis.h"', file=self.outFile) |
| Mark Lobodzinski | 706e52b | 2018-12-11 13:21:52 -0700 | [diff] [blame] | 751 | write('#include "thread_safety.h"', file=self.outFile) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 752 | self.newline() |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 753 | write(self.inline_custom_source_preamble, file=self.outFile) |
| Mark Lobodzinski | 706e52b | 2018-12-11 13:21:52 -0700 | [diff] [blame] | 754 | |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 755 | |
| 756 | def endFile(self): |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 757 | |
| 758 | # Create class definitions |
| 759 | counter_class_defs = '' |
| 760 | counter_class_instances = '' |
| 761 | counter_class_bodies = '' |
| 762 | |
| 763 | for obj in self.non_dispatchable_types: |
| 764 | counter_class_defs += ' counter<%s> c_%s;\n' % (obj, obj) |
| 765 | if obj in self.object_to_debug_report_type: |
| 766 | obj_type = self.object_to_debug_report_type[obj] |
| 767 | else: |
| 768 | obj_type = 'VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT' |
| 769 | counter_class_instances += ' c_%s("%s", %s, &report_data),\n' % (obj, obj, obj_type) |
| 770 | counter_class_bodies += 'WRAPPER(%s)\n' % obj |
| 771 | if self.header_file: |
| 772 | class_def = self.inline_custom_header_preamble.replace('COUNTER_CLASS_DEFINITIONS_TEMPLATE', counter_class_defs) |
| 773 | class_def = class_def.replace('COUNTER_CLASS_INSTANCES_TEMPLATE', counter_class_instances[:-2]) # Kill last comma |
| 774 | class_def = class_def.replace('COUNTER_CLASS_BODIES_TEMPLATE', counter_class_bodies) |
| 775 | write(class_def, file=self.outFile) |
| 776 | write('\n'.join(self.sections['command']), file=self.outFile) |
| Mark Lobodzinski | 706e52b | 2018-12-11 13:21:52 -0700 | [diff] [blame] | 777 | if self.header_file: |
| 778 | write('};', file=self.outFile) |
| 779 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 780 | # Finish processing in superclass |
| 781 | OutputGenerator.endFile(self) |
| Mark Lobodzinski | 706e52b | 2018-12-11 13:21:52 -0700 | [diff] [blame] | 782 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 783 | def beginFeature(self, interface, emit): |
| 784 | #write('// starting beginFeature', file=self.outFile) |
| 785 | # Start processing in superclass |
| 786 | OutputGenerator.beginFeature(self, interface, emit) |
| 787 | # C-specific |
| 788 | # Accumulate includes, defines, types, enums, function pointer typedefs, |
| 789 | # end function prototypes separately for this feature. They're only |
| 790 | # printed in endFeature(). |
| Mark Lobodzinski | 62f7156 | 2017-10-24 13:41:18 -0600 | [diff] [blame] | 791 | self.featureExtraProtect = GetFeatureProtect(interface) |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 792 | if (self.featureExtraProtect is not None): |
| 793 | self.appendSection('command', '\n#ifdef %s' % self.featureExtraProtect) |
| 794 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 795 | #write('// ending beginFeature', file=self.outFile) |
| 796 | def endFeature(self): |
| 797 | # C-specific |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 798 | if (self.emit): |
| MichaĆ Janiszewski | 3c3ce9e | 2018-10-30 23:25:21 +0100 | [diff] [blame] | 799 | if (self.featureExtraProtect is not None): |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 800 | self.appendSection('command', '#endif // %s' % self.featureExtraProtect) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 801 | # Finish processing in superclass |
| 802 | OutputGenerator.endFeature(self) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 803 | # |
| 804 | # Append a definition to the specified section |
| 805 | def appendSection(self, section, text): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 806 | self.sections[section].append(text) |
| 807 | # |
| 808 | # Type generation |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 809 | def genType(self, typeinfo, name, alias): |
| Mark Lobodzinski | 796454c | 2018-12-11 16:10:55 -0700 | [diff] [blame] | 810 | OutputGenerator.genType(self, typeinfo, name, alias) |
| 811 | type_elem = typeinfo.elem |
| 812 | category = type_elem.get('category') |
| 813 | if category == 'handle': |
| 814 | if self.isHandleTypeNonDispatchable(name): |
| 815 | self.non_dispatchable_types.add(name) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 816 | # |
| 817 | # Struct (e.g. C "struct" type) generation. |
| 818 | # This is a special case of the <type> tag where the contents are |
| 819 | # interpreted as a set of <member> tags instead of freeform C |
| 820 | # C type declarations. The <member> tags are just like <param> |
| 821 | # tags - they are a declaration of a struct or union member. |
| 822 | # Only simple member declarations are supported (no nested |
| 823 | # structs etc.) |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 824 | def genStruct(self, typeinfo, typeName, alias): |
| 825 | OutputGenerator.genStruct(self, typeinfo, typeName, alias) |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 826 | body = 'typedef ' + typeinfo.elem.get('category') + ' ' + typeName + ' {\n' |
| 827 | # paramdecl = self.makeCParamDecl(typeinfo.elem, self.genOpts.alignFuncParam) |
| 828 | for member in typeinfo.elem.findall('.//member'): |
| 829 | body += self.makeCParamDecl(member, self.genOpts.alignFuncParam) |
| 830 | body += ';\n' |
| 831 | body += '} ' + typeName + ';\n' |
| 832 | self.appendSection('struct', body) |
| 833 | # |
| 834 | # Group (e.g. C "enum" type) generation. |
| 835 | # These are concatenated together with other types. |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 836 | def genGroup(self, groupinfo, groupName, alias): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 837 | pass |
| 838 | # Enumerant generation |
| 839 | # <enum> tags may specify their values in several ways, but are usually |
| 840 | # just integers. |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 841 | def genEnum(self, enuminfo, name, alias): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 842 | pass |
| 843 | # |
| 844 | # Command generation |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 845 | def genCmd(self, cmdinfo, name, alias): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 846 | # Commands shadowed by interface functions and are not implemented |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 847 | special_functions = [ |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 848 | 'vkCreateDevice', |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 849 | 'vkCreateInstance', |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 850 | 'vkAllocateCommandBuffers', |
| 851 | 'vkFreeCommandBuffers', |
| John Zulauf | e28aa34 | 2018-10-24 12:18:39 -0600 | [diff] [blame] | 852 | 'vkResetCommandPool', |
| 853 | 'vkDestroyCommandPool', |
| Mark Lobodzinski | 3bd82ad | 2017-02-16 11:45:27 -0700 | [diff] [blame] | 854 | 'vkAllocateDescriptorSets', |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 855 | 'vkQueuePresentKHR', |
| Mark Lobodzinski | 8925c05 | 2018-12-18 12:41:15 -0700 | [diff] [blame^] | 856 | 'vkGetSwapchainImagesKHR', |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 857 | ] |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 858 | if name == 'vkQueuePresentKHR' or (name in special_functions and self.source_file): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 859 | return |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 860 | |
| 861 | if (("DebugMarker" in name or "DebugUtilsObject" in name) and "EXT" in name): |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 862 | self.appendSection('command', '// TODO - not wrapping EXT function ' + name) |
| 863 | return |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 864 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 865 | # Determine first if this function needs to be intercepted |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 866 | startthreadsafety = self.makeThreadUseBlock(cmdinfo.elem, 'Start') |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 867 | if startthreadsafety is None: |
| 868 | return |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 869 | finishthreadsafety = self.makeThreadUseBlock(cmdinfo.elem, 'Finish') |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 870 | |
| Mike Schuchardt | f375c7c | 2017-12-28 11:23:48 -0700 | [diff] [blame] | 871 | OutputGenerator.genCmd(self, cmdinfo, name, alias) |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 872 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 873 | # setup common to call wrappers |
| 874 | # first parameter is always dispatchable |
| 875 | dispatchable_type = cmdinfo.elem.find('param/type').text |
| 876 | dispatchable_name = cmdinfo.elem.find('param/name').text |
| Mark Lobodzinski | 1f2ba26 | 2018-12-04 14:15:47 -0700 | [diff] [blame] | 877 | |
| 878 | decls = self.makeCDecls(cmdinfo.elem) |
| 879 | |
| 880 | if self.source_file: |
| 881 | pre_decl = decls[0][:-1] |
| 882 | pre_decl = pre_decl.split("VKAPI_CALL ")[1] |
| 883 | pre_decl = 'void ThreadSafety::PreCallRecord' + pre_decl + ' {' |
| 884 | |
| 885 | # PreCallRecord |
| 886 | self.appendSection('command', '') |
| 887 | self.appendSection('command', pre_decl) |
| 888 | self.appendSection('command', " " + "\n ".join(str(startthreadsafety).rstrip().split("\n"))) |
| 889 | self.appendSection('command', '}') |
| 890 | |
| 891 | post_decl = pre_decl.replace('PreCallRecord', 'PostCallRecord') |
| 892 | |
| 893 | # PostCallRecord |
| 894 | self.appendSection('command', '') |
| 895 | self.appendSection('command', post_decl) |
| 896 | self.appendSection('command', " " + "\n ".join(str(finishthreadsafety).rstrip().split("\n"))) |
| 897 | self.appendSection('command', '}') |
| 898 | |
| 899 | if self.header_file: |
| 900 | pre_decl = decls[0][:-1] |
| 901 | pre_decl = pre_decl.split("VKAPI_CALL ")[1] |
| 902 | pre_decl = 'void PreCallRecord' + pre_decl + ';' |
| 903 | |
| 904 | # PreCallRecord |
| 905 | self.appendSection('command', '') |
| 906 | self.appendSection('command', pre_decl) |
| 907 | |
| 908 | post_decl = pre_decl.replace('PreCallRecord', 'PostCallRecord') |
| 909 | |
| 910 | # PostCallRecord |
| 911 | self.appendSection('command', '') |
| 912 | self.appendSection('command', post_decl) |
| 913 | |
| Mark Lobodzinski | ff91099 | 2016-10-11 14:29:52 -0600 | [diff] [blame] | 914 | # |
| 915 | # override makeProtoName to drop the "vk" prefix |
| 916 | def makeProtoName(self, name, tail): |
| 917 | return self.genOpts.apientry + name[2:] + tail |