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