1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
|
/* Copyright (c) 2015-2025 The Khronos Group Inc.
* Copyright (c) 2015-2025 Valve Corporation
* Copyright (c) 2015-2025 LunarG, Inc.
* Copyright (c) 2015-2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "generated/dispatch_functions.h"
#include "thread_tracker/thread_safety_validation.h"
namespace threadsafety {
ReadLockGuard Device::ReadLock() const { return ReadLockGuard(validation_object_mutex, std::defer_lock); }
WriteLockGuard Device::WriteLock() { return WriteLockGuard(validation_object_mutex, std::defer_lock); }
void Device::PreCallRecordAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo,
VkCommandBuffer* pCommandBuffers, const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartWriteObject(pAllocateInfo->commandPool, record_obj.location);
}
void Device::PostCallRecordAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo,
VkCommandBuffer* pCommandBuffers, const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishWriteObject(pAllocateInfo->commandPool, record_obj.location);
// Record mapping from command buffer to command pool
if (pCommandBuffers) {
auto lock = WriteLockGuard(thread_safety_lock);
auto& pool_command_buffers = pool_command_buffers_map[pAllocateInfo->commandPool];
for (uint32_t index = 0; index < pAllocateInfo->commandBufferCount; index++) {
command_pool_map.insert_or_assign(pCommandBuffers[index], pAllocateInfo->commandPool);
CreateObject(pCommandBuffers[index]);
pool_command_buffers.insert(pCommandBuffers[index]);
}
}
}
void Device::PreCallRecordCreateDescriptorSetLayout(VkDevice device, const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkDescriptorSetLayout* pSetLayout,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
}
void Device::PostCallRecordCreateDescriptorSetLayout(VkDevice device, const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkDescriptorSetLayout* pSetLayout,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
if (record_obj.result == VK_SUCCESS) {
CreateObject(*pSetLayout);
// Check whether any binding uses read_only
bool read_only = (pCreateInfo->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_HOST_ONLY_POOL_BIT_EXT) != 0;
if (!read_only) {
if (const auto* flags_create_info =
vku::FindStructInPNextChain<VkDescriptorSetLayoutBindingFlagsCreateInfo>(pCreateInfo->pNext)) {
for (uint32_t i = 0; i < flags_create_info->bindingCount; ++i) {
// "Descriptor bindings created with this flag are also partially exempt from the external synchronization
// requirement in vkUpdateDescriptorSetWithTemplateKHR and vkUpdateDescriptorSets"
if (flags_create_info->pBindingFlags[i] &
(VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT)) {
read_only = true;
break;
}
}
}
}
dsl_read_only_map.insert_or_assign(*pSetLayout, read_only);
}
}
void Device::PreCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
VkDescriptorSet* pDescriptorSets, const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartWriteObject(pAllocateInfo->descriptorPool, record_obj.location);
// Host access to pAllocateInfo::descriptorPool must be externally synchronized
}
void Device::PostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo* pAllocateInfo,
VkDescriptorSet* pDescriptorSets, const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishWriteObject(pAllocateInfo->descriptorPool, record_obj.location);
// Host access to pAllocateInfo::descriptorPool must be externally synchronized
if (record_obj.result == VK_SUCCESS) {
auto lock = WriteLockGuard(thread_safety_lock);
auto& pool_descriptor_sets = pool_descriptor_sets_map[pAllocateInfo->descriptorPool];
for (uint32_t index0 = 0; index0 < pAllocateInfo->descriptorSetCount; index0++) {
CreateObject(pDescriptorSets[index0]);
pool_descriptor_sets.insert(pDescriptorSets[index0]);
auto iter = dsl_read_only_map.find(pAllocateInfo->pSetLayouts[index0]);
if (iter != dsl_read_only_map.end()) {
ds_read_only_map.insert_or_assign(pDescriptorSets[index0], iter->second);
} else {
assert(false && "descriptor set layout not found");
}
}
}
}
void Device::PreCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
const VkDescriptorSet* pDescriptorSets, const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartWriteObject(descriptorPool, record_obj.location);
if (pDescriptorSets) {
for (uint32_t index = 0; index < descriptorSetCount; index++) {
StartWriteObject(pDescriptorSets[index], record_obj.location);
}
}
// Host access to descriptorPool must be externally synchronized
// Host access to each member of pDescriptorSets must be externally synchronized
}
void Device::PostCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
const VkDescriptorSet* pDescriptorSets, const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishWriteObject(descriptorPool, record_obj.location);
if (pDescriptorSets) {
for (uint32_t index = 0; index < descriptorSetCount; index++) {
FinishWriteObject(pDescriptorSets[index], record_obj.location);
}
}
// Host access to descriptorPool must be externally synchronized
// Host access to each member of pDescriptorSets must be externally synchronized
// Host access to pAllocateInfo::descriptorPool must be externally synchronized
if (record_obj.result == VK_SUCCESS) {
auto lock = WriteLockGuard(thread_safety_lock);
auto& pool_descriptor_sets = pool_descriptor_sets_map[descriptorPool];
for (uint32_t index0 = 0; index0 < descriptorSetCount; index0++) {
auto descriptor_set = pDescriptorSets[index0];
DestroyObject(descriptor_set);
pool_descriptor_sets.erase(descriptor_set);
ds_read_only_map.erase(descriptor_set);
}
}
}
void Device::PreCallRecordDestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
const VkAllocationCallbacks* pAllocator, const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartWriteObject(descriptorPool, record_obj.location);
// Host access to descriptorPool must be externally synchronized
auto lock = ReadLockGuard(thread_safety_lock);
auto iterator = pool_descriptor_sets_map.find(descriptorPool);
// Possible to have no descriptor sets allocated from pool
if (iterator != pool_descriptor_sets_map.end()) {
for (auto descriptor_set : pool_descriptor_sets_map[descriptorPool]) {
StartWriteObject(descriptor_set, record_obj.location);
}
}
}
void Device::PostCallRecordDestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
const VkAllocationCallbacks* pAllocator, const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishWriteObject(descriptorPool, record_obj.location);
DestroyObject(descriptorPool);
// Host access to descriptorPool must be externally synchronized
{
auto lock = WriteLockGuard(thread_safety_lock);
// remove references to implicitly freed descriptor sets
for (auto descriptor_set : pool_descriptor_sets_map[descriptorPool]) {
FinishWriteObject(descriptor_set, record_obj.location);
DestroyObject(descriptor_set);
ds_read_only_map.erase(descriptor_set);
}
pool_descriptor_sets_map[descriptorPool].clear();
pool_descriptor_sets_map.erase(descriptorPool);
}
}
void Device::PreCallRecordResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, VkDescriptorPoolResetFlags flags,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartWriteObject(descriptorPool, record_obj.location);
// Host access to descriptorPool must be externally synchronized
// any sname:VkDescriptorSet objects allocated from pname:descriptorPool must be externally synchronized between host accesses
auto lock = ReadLockGuard(thread_safety_lock);
auto iterator = pool_descriptor_sets_map.find(descriptorPool);
// Possible to have no descriptor sets allocated from pool
if (iterator != pool_descriptor_sets_map.end()) {
for (auto descriptor_set : pool_descriptor_sets_map[descriptorPool]) {
StartWriteObject(descriptor_set, record_obj.location);
}
}
}
void Device::PostCallRecordResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool, VkDescriptorPoolResetFlags flags,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishWriteObject(descriptorPool, record_obj.location);
// Host access to descriptorPool must be externally synchronized
// any sname:VkDescriptorSet objects allocated from pname:descriptorPool must be externally synchronized between host accesses
if (record_obj.result == VK_SUCCESS) {
// remove references to implicitly freed descriptor sets
auto lock = WriteLockGuard(thread_safety_lock);
for (auto descriptor_set : pool_descriptor_sets_map[descriptorPool]) {
FinishWriteObject(descriptor_set, record_obj.location);
DestroyObject(descriptor_set);
ds_read_only_map.erase(descriptor_set);
}
pool_descriptor_sets_map[descriptorPool].clear();
}
}
bool Device::DsReadOnly(VkDescriptorSet set) const {
auto iter = ds_read_only_map.find(set);
if (iter != ds_read_only_map.end()) {
return iter->second;
}
return false;
}
void Device::PreCallRecordUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
const VkCopyDescriptorSet* pDescriptorCopies, const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
if (pDescriptorWrites) {
for (uint32_t index = 0; index < descriptorWriteCount; index++) {
auto dstSet = pDescriptorWrites[index].dstSet;
bool read_only = DsReadOnly(dstSet);
if (read_only) {
StartReadObject(dstSet, record_obj.location);
} else {
StartWriteObject(dstSet, record_obj.location);
}
}
}
if (pDescriptorCopies) {
for (uint32_t index = 0; index < descriptorCopyCount; index++) {
auto dstSet = pDescriptorCopies[index].dstSet;
bool read_only = DsReadOnly(dstSet);
if (read_only) {
StartReadObject(dstSet, record_obj.location);
} else {
StartWriteObject(dstSet, record_obj.location);
}
StartReadObject(pDescriptorCopies[index].srcSet, record_obj.location);
}
}
// Host access to pDescriptorWrites[].dstSet must be externally synchronized
// Host access to pDescriptorCopies[].dstSet must be externally synchronized
}
void Device::PostCallRecordUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
const VkWriteDescriptorSet* pDescriptorWrites, uint32_t descriptorCopyCount,
const VkCopyDescriptorSet* pDescriptorCopies, const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
if (pDescriptorWrites) {
for (uint32_t index = 0; index < descriptorWriteCount; index++) {
auto dstSet = pDescriptorWrites[index].dstSet;
bool read_only = DsReadOnly(dstSet);
if (read_only) {
FinishReadObject(dstSet, record_obj.location);
} else {
FinishWriteObject(dstSet, record_obj.location);
}
}
}
if (pDescriptorCopies) {
for (uint32_t index = 0; index < descriptorCopyCount; index++) {
auto dstSet = pDescriptorCopies[index].dstSet;
bool read_only = DsReadOnly(dstSet);
if (read_only) {
FinishReadObject(dstSet, record_obj.location);
} else {
FinishWriteObject(dstSet, record_obj.location);
}
FinishReadObject(pDescriptorCopies[index].srcSet, record_obj.location);
}
}
// Host access to pDescriptorWrites[].dstSet must be externally synchronized
// Host access to pDescriptorCopies[].dstSet must be externally synchronized
}
void Device::PreCallRecordUpdateDescriptorSetWithTemplate(VkDevice device, VkDescriptorSet descriptorSet,
VkDescriptorUpdateTemplate descriptorUpdateTemplate, const void* pData,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartReadObject(descriptorUpdateTemplate, record_obj.location);
const bool read_only = DsReadOnly(descriptorSet);
if (read_only) {
StartReadObject(descriptorSet, record_obj.location);
} else {
StartWriteObject(descriptorSet, record_obj.location);
}
// Host access to descriptorSet must be externally synchronized
}
void Device::PostCallRecordUpdateDescriptorSetWithTemplate(VkDevice device, VkDescriptorSet descriptorSet,
VkDescriptorUpdateTemplate descriptorUpdateTemplate, const void* pData,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishReadObject(descriptorUpdateTemplate, record_obj.location);
const bool read_only = DsReadOnly(descriptorSet);
if (read_only) {
FinishReadObject(descriptorSet, record_obj.location);
} else {
FinishWriteObject(descriptorSet, record_obj.location);
}
// Host access to descriptorSet must be externally synchronized
}
void Device::PreCallRecordUpdateDescriptorSetWithTemplateKHR(VkDevice device, VkDescriptorSet descriptorSet,
VkDescriptorUpdateTemplate descriptorUpdateTemplate, const void* pData,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartReadObject(descriptorUpdateTemplate, record_obj.location);
const bool read_only = DsReadOnly(descriptorSet);
if (read_only) {
StartReadObject(descriptorSet, record_obj.location);
} else {
StartWriteObject(descriptorSet, record_obj.location);
}
// Host access to descriptorSet must be externally synchronized
}
void Device::PostCallRecordUpdateDescriptorSetWithTemplateKHR(VkDevice device, VkDescriptorSet descriptorSet,
VkDescriptorUpdateTemplate descriptorUpdateTemplate,
const void* pData, const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishReadObject(descriptorUpdateTemplate, record_obj.location);
const bool read_only = DsReadOnly(descriptorSet);
if (read_only) {
FinishReadObject(descriptorSet, record_obj.location);
} else {
FinishWriteObject(descriptorSet, record_obj.location);
}
// Host access to descriptorSet must be externally synchronized
}
void Device::PreCallRecordFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount,
const VkCommandBuffer* pCommandBuffers, const RecordObject& record_obj) {
const bool lockCommandPool = false; // pool is already directly locked
StartReadObjectParentInstance(device, record_obj.location);
StartWriteObject(commandPool, record_obj.location);
if (pCommandBuffers) {
// Even though we're immediately "finishing" below, we still are testing for concurrency with any call in process
// so this isn't a no-op
// The driver may immediately reuse command buffers in another thread.
// These updates need to be done before calling down to the driver.
auto lock = WriteLockGuard(thread_safety_lock);
auto& pool_command_buffers = pool_command_buffers_map[commandPool];
for (uint32_t index = 0; index < commandBufferCount; index++) {
StartWriteObject(pCommandBuffers[index], record_obj.location, lockCommandPool);
FinishWriteObject(pCommandBuffers[index], record_obj.location, lockCommandPool);
DestroyObject(pCommandBuffers[index]);
pool_command_buffers.erase(pCommandBuffers[index]);
command_pool_map.erase(pCommandBuffers[index]);
}
}
}
void Device::PostCallRecordFreeCommandBuffers(VkDevice device, VkCommandPool commandPool, uint32_t commandBufferCount,
const VkCommandBuffer* pCommandBuffers, const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishWriteObject(commandPool, record_obj.location);
}
void Device::PreCallRecordCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
}
void Device::PostCallRecordCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkCommandPool* pCommandPool,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
if (record_obj.result == VK_SUCCESS) {
CreateObject(*pCommandPool);
c_VkCommandPoolContents.CreateObject(*pCommandPool);
}
}
void Device::PreCallRecordResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartWriteObject(commandPool, record_obj.location);
// Check for any uses of non-externally sync'd command buffers (for example from vkCmdExecuteCommands)
c_VkCommandPoolContents.StartWrite(commandPool, record_obj.location);
// Host access to commandPool must be externally synchronized
}
void Device::PostCallRecordResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishWriteObject(commandPool, record_obj.location);
c_VkCommandPoolContents.FinishWrite(commandPool, record_obj.location);
// Host access to commandPool must be externally synchronized
}
void Device::PreCallRecordDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks* pAllocator,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartWriteObject(commandPool, record_obj.location);
// Check for any uses of non-externally sync'd command buffers (for example from vkCmdExecuteCommands)
c_VkCommandPoolContents.StartWrite(commandPool, record_obj.location);
// Host access to commandPool must be externally synchronized
auto lock = WriteLockGuard(thread_safety_lock);
// The driver may immediately reuse command buffers in another thread.
// These updates need to be done before calling down to the driver.
// remove references to implicitly freed command pools
for (auto command_buffer : pool_command_buffers_map[commandPool]) {
DestroyObject(command_buffer);
}
pool_command_buffers_map[commandPool].clear();
pool_command_buffers_map.erase(commandPool);
}
void Device::PostCallRecordDestroyCommandPool(VkDevice device, VkCommandPool commandPool, const VkAllocationCallbacks* pAllocator,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishWriteObject(commandPool, record_obj.location);
DestroyObject(commandPool);
c_VkCommandPoolContents.FinishWrite(commandPool, record_obj.location);
c_VkCommandPoolContents.DestroyObject(commandPool);
}
// GetSwapchainImages can return a non-zero count with a NULL pSwapchainImages pointer. Let's avoid crashes by ignoring
// pSwapchainImages.
void Device::PreCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
VkImage* pSwapchainImages, const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartReadObject(swapchain, record_obj.location);
}
void Device::PostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
VkImage* pSwapchainImages, const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishReadObject(swapchain, record_obj.location);
if (pSwapchainImages != nullptr) {
auto lock = WriteLockGuard(thread_safety_lock);
auto& wrapped_swapchain_image_handles = swapchain_wrapped_image_handle_map[swapchain];
for (uint32_t i = static_cast<uint32_t>(wrapped_swapchain_image_handles.size()); i < *pSwapchainImageCount; i++) {
CreateObject(pSwapchainImages[i]);
wrapped_swapchain_image_handles.emplace_back(pSwapchainImages[i]);
}
}
}
void Device::PreCallRecordDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartWriteObject(swapchain, record_obj.location);
// Host access to swapchain must be externally synchronized
auto lock = ReadLockGuard(thread_safety_lock);
for (auto& image_handle : swapchain_wrapped_image_handle_map[swapchain]) {
StartWriteObject(image_handle, record_obj.location);
}
}
void Device::PostCallRecordDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishWriteObject(swapchain, record_obj.location);
DestroyObject(swapchain);
// Host access to swapchain must be externally synchronized
auto lock = WriteLockGuard(thread_safety_lock);
for (auto& image_handle : swapchain_wrapped_image_handle_map[swapchain]) {
FinishWriteObject(image_handle, record_obj.location);
DestroyObject(image_handle);
}
swapchain_wrapped_image_handle_map.erase(swapchain);
}
void Device::PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator, const RecordObject& record_obj) {
StartWriteObjectParentInstance(device, record_obj.location);
// Host access to device must be externally synchronized
}
void Device::PostCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator, const RecordObject& record_obj) {
FinishWriteObjectParentInstance(device, record_obj.location);
parent_instance->DestroyObject(device);
// Host access to device must be externally synchronized
auto lock = WriteLockGuard(thread_safety_lock);
for (auto& queue : device_queues_map[device]) {
DestroyObject(queue);
}
device_queues_map[device].clear();
}
void Device::PreCallRecordGetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
}
void Device::PostCallRecordGetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
CreateObject(*pQueue);
auto lock = WriteLockGuard(thread_safety_lock);
device_queues_map[device].insert(*pQueue);
}
void Device::PreCallRecordGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2* pQueueInfo, VkQueue* pQueue,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
}
void Device::PostCallRecordGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2* pQueueInfo, VkQueue* pQueue,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
CreateObject(*pQueue);
auto lock = WriteLockGuard(thread_safety_lock);
device_queues_map[device].insert(*pQueue);
}
void Instance::PostCallRecordGetPhysicalDeviceDisplayPropertiesKHR(VkPhysicalDevice physicalDevice, uint32_t* pPropertyCount,
VkDisplayPropertiesKHR* pProperties,
const RecordObject& record_obj) {
if ((record_obj.result != VK_SUCCESS) && (record_obj.result != VK_INCOMPLETE)) return;
if (pProperties) {
for (uint32_t i = 0; i < *pPropertyCount; ++i) {
CreateObject(pProperties[i].display);
}
}
}
void Instance::PostCallRecordGetPhysicalDeviceDisplayProperties2KHR(VkPhysicalDevice physicalDevice, uint32_t* pPropertyCount,
VkDisplayProperties2KHR* pProperties,
const RecordObject& record_obj) {
if ((record_obj.result != VK_SUCCESS) && (record_obj.result != VK_INCOMPLETE)) return;
if (pProperties) {
for (uint32_t i = 0; i < *pPropertyCount; ++i) {
CreateObject(pProperties[i].displayProperties.display);
}
}
}
void Instance::PostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice, uint32_t* pPropertyCount,
VkDisplayPlanePropertiesKHR* pProperties,
const RecordObject& record_obj) {
if ((record_obj.result != VK_SUCCESS) && (record_obj.result != VK_INCOMPLETE)) return;
if (pProperties) {
for (uint32_t i = 0; i < *pPropertyCount; ++i) {
CreateObject(pProperties[i].currentDisplay);
}
}
}
void Instance::PostCallRecordGetPhysicalDeviceDisplayPlaneProperties2KHR(VkPhysicalDevice physicalDevice, uint32_t* pPropertyCount,
VkDisplayPlaneProperties2KHR* pProperties,
const RecordObject& record_obj) {
if ((record_obj.result != VK_SUCCESS) && (record_obj.result != VK_INCOMPLETE)) return;
if (pProperties) {
for (uint32_t i = 0; i < *pPropertyCount; ++i) {
CreateObject(pProperties[i].displayPlaneProperties.currentDisplay);
}
}
}
void Instance::PreCallRecordGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
uint32_t* pDisplayCount, VkDisplayKHR* pDisplays,
const RecordObject& record_obj) {
// Nothing to do for this pre-call function
}
void Instance::PostCallRecordGetDisplayPlaneSupportedDisplaysKHR(VkPhysicalDevice physicalDevice, uint32_t planeIndex,
uint32_t* pDisplayCount, VkDisplayKHR* pDisplays,
const RecordObject& record_obj) {
if ((record_obj.result != VK_SUCCESS) && (record_obj.result != VK_INCOMPLETE)) return;
if (pDisplays) {
for (uint32_t index = 0; index < *pDisplayCount; index++) {
CreateObject(pDisplays[index]);
}
}
}
void Instance::PreCallRecordGetDisplayModePropertiesKHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display,
uint32_t* pPropertyCount, VkDisplayModePropertiesKHR* pProperties,
const RecordObject& record_obj) {
StartReadObject(display, record_obj.location);
}
void Instance::PostCallRecordGetDisplayModePropertiesKHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display,
uint32_t* pPropertyCount, VkDisplayModePropertiesKHR* pProperties,
const RecordObject& record_obj) {
FinishReadObject(display, record_obj.location);
if ((record_obj.result != VK_SUCCESS) && (record_obj.result != VK_INCOMPLETE)) return;
if (pProperties != nullptr) {
for (uint32_t index = 0; index < *pPropertyCount; index++) {
CreateObject(pProperties[index].displayMode);
}
}
}
void Instance::PreCallRecordGetDisplayModeProperties2KHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display,
uint32_t* pPropertyCount, VkDisplayModeProperties2KHR* pProperties,
const RecordObject& record_obj) {
StartReadObject(display, record_obj.location);
}
void Instance::PostCallRecordGetDisplayModeProperties2KHR(VkPhysicalDevice physicalDevice, VkDisplayKHR display,
uint32_t* pPropertyCount, VkDisplayModeProperties2KHR* pProperties,
const RecordObject& record_obj) {
FinishReadObject(display, record_obj.location);
if ((record_obj.result != VK_SUCCESS) && (record_obj.result != VK_INCOMPLETE)) return;
if (pProperties != nullptr) {
for (uint32_t index = 0; index < *pPropertyCount; index++) {
CreateObject(pProperties[index].displayModeProperties.displayMode);
}
}
}
void Instance::PreCallRecordGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
VkDisplayPlaneCapabilities2KHR* pCapabilities,
const RecordObject& record_obj) {
StartWriteObject(pDisplayPlaneInfo->mode, record_obj.location);
}
void Instance::PostCallRecordGetDisplayPlaneCapabilities2KHR(VkPhysicalDevice physicalDevice,
const VkDisplayPlaneInfo2KHR* pDisplayPlaneInfo,
VkDisplayPlaneCapabilities2KHR* pCapabilities,
const RecordObject& record_obj) {
FinishWriteObject(pDisplayPlaneInfo->mode, record_obj.location);
}
#ifdef VK_USE_PLATFORM_XLIB_XRANDR_EXT
void Instance::PostCallRecordGetRandROutputDisplayEXT(VkPhysicalDevice physicalDevice, Display* dpy, RROutput rrOutput,
VkDisplayKHR* pDisplay, const RecordObject& record_obj) {
if ((record_obj.result != VK_SUCCESS) || (pDisplay == nullptr)) return;
CreateObject(*pDisplay);
}
#endif // VK_USE_PLATFORM_XLIB_XRANDR_EXT
void Instance::PostCallRecordGetDrmDisplayEXT(VkPhysicalDevice physicalDevice, int32_t drmFd, uint32_t connectorId,
VkDisplayKHR* display, const RecordObject& record_obj) {
if ((record_obj.result != VK_SUCCESS) || (display == nullptr)) return;
CreateObject(*display);
}
void Device::PreCallRecordRegisterDisplayEventEXT(VkDevice device, VkDisplayKHR display,
const VkDisplayEventInfoEXT* pDisplayEventInfo,
const VkAllocationCallbacks* pAllocator, VkFence* pFence,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartReadObjectParentInstance(display, record_obj.location);
}
void Device::PostCallRecordRegisterDisplayEventEXT(VkDevice device, VkDisplayKHR display,
const VkDisplayEventInfoEXT* pDisplayEventInfo,
const VkAllocationCallbacks* pAllocator, VkFence* pFence,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
FinishReadObjectParentInstance(display, record_obj.location);
if (record_obj.result == VK_SUCCESS) {
CreateObject(*pFence);
}
}
void Device::PreCallRecordDeviceWaitIdle(VkDevice device, const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
auto lock = ReadLockGuard(thread_safety_lock);
const auto& queue_set = device_queues_map[device];
for (const auto& queue : queue_set) {
StartWriteObject(queue, record_obj.location);
}
}
void Device::PostCallRecordDeviceWaitIdle(VkDevice device, const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
auto lock = ReadLockGuard(thread_safety_lock);
const auto& queue_set = device_queues_map[device];
for (const auto& queue : queue_set) {
FinishWriteObject(queue, record_obj.location);
}
}
void Device::PreCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
VkPipelineCache pipelineCache, uint32_t createInfoCount,
const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
StartReadObject(deferredOperation, record_obj.location);
StartReadObject(pipelineCache, record_obj.location);
}
void Device::PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
VkPipelineCache pipelineCache, uint32_t createInfoCount,
const VkRayTracingPipelineCreateInfoKHR* pCreateInfos,
const VkAllocationCallbacks* pAllocator, VkPipeline* pPipelines,
const RecordObject& record_obj) {
auto unlock_objects = [this, device, deferredOperation, pipelineCache, record_obj]() {
this->FinishReadObjectParentInstance(device, record_obj.location);
this->FinishReadObject(deferredOperation, record_obj.location);
this->FinishReadObject(pipelineCache, record_obj.location);
};
auto register_objects = [this](const std::vector<VkPipeline>& pipelines) {
for (auto pipe : pipelines) {
if (!pipe) continue;
CreateObject(pipe);
}
};
// Fix check for deferred ray tracing pipeline creation
// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/5817
const bool is_operation_deferred = (deferredOperation != VK_NULL_HANDLE && record_obj.result == VK_OPERATION_DEFERRED_KHR);
if (is_operation_deferred) {
if (dispatch_device_->wrap_handles) {
deferredOperation = dispatch_device_->Unwrap(deferredOperation);
}
// Unlock objects once the deferred operation is complete
std::vector<std::function<void()>> post_completion_fns;
auto completion_find = dispatch_device_->deferred_operation_post_completion.pop(deferredOperation);
if (completion_find->first) {
post_completion_fns = std::move(completion_find->second);
}
post_completion_fns.emplace_back(unlock_objects);
dispatch_device_->deferred_operation_post_completion.insert(deferredOperation, std::move(post_completion_fns));
// We will only register the object once we know it was created successfully
std::vector<std::function<void(const std::vector<VkPipeline>&)>> post_check_fns;
auto check_find = dispatch_device_->deferred_operation_post_check.pop(deferredOperation);
if (check_find->first) {
post_check_fns = std::move(check_find->second);
}
post_check_fns.emplace_back(register_objects);
dispatch_device_->deferred_operation_post_check.insert(deferredOperation, std::move(post_check_fns));
} else {
unlock_objects();
if (pPipelines) {
for (uint32_t index = 0; index < createInfoCount; index++) {
if (!pPipelines[index]) continue;
CreateObject(pPipelines[index]);
}
}
}
}
void Device::PreCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, const RecordObject& record_obj) {
StartWriteObject(queue, record_obj.location);
uint32_t waitSemaphoreCount = pPresentInfo->waitSemaphoreCount;
if (pPresentInfo->pWaitSemaphores != nullptr) {
for (uint32_t index = 0; index < waitSemaphoreCount; index++) {
StartReadObject(pPresentInfo->pWaitSemaphores[index], record_obj.location);
}
}
if (pPresentInfo->pSwapchains != nullptr) {
for (uint32_t index = 0; index < pPresentInfo->swapchainCount; ++index) {
StartWriteObject(pPresentInfo->pSwapchains[index], record_obj.location);
}
}
if (const auto* present_fence_info = vku::FindStructInPNextChain<VkSwapchainPresentFenceInfoKHR>(pPresentInfo->pNext)) {
for (uint32_t index = 0; index < present_fence_info->swapchainCount; index++) {
StartWriteObject(present_fence_info->pFences[index], record_obj.location);
}
}
}
void Device::PostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo, const RecordObject& record_obj) {
FinishWriteObject(queue, record_obj.location);
uint32_t waitSemaphoreCount = pPresentInfo->waitSemaphoreCount;
if (pPresentInfo->pWaitSemaphores != nullptr) {
for (uint32_t index = 0; index < waitSemaphoreCount; index++) {
FinishReadObject(pPresentInfo->pWaitSemaphores[index], record_obj.location);
}
}
if (pPresentInfo->pSwapchains != nullptr) {
for (uint32_t index = 0; index < pPresentInfo->swapchainCount; ++index) {
FinishWriteObject(pPresentInfo->pSwapchains[index], record_obj.location);
}
}
if (const auto* present_fence_info = vku::FindStructInPNextChain<VkSwapchainPresentFenceInfoKHR>(pPresentInfo->pNext)) {
for (uint32_t index = 0; index < present_fence_info->swapchainCount; index++) {
FinishWriteObject(present_fence_info->pFences[index], record_obj.location);
}
}
}
void Device::PreCallRecordWaitForPresentKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t presentId, uint64_t timeout,
const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
// Do not track swapchain parameter for vkWaitForPresentKHR.
// vkWaitForPresentKHR has exception to external synchronization rules that swapchain
// passed to it can be used by the functions (except vkDestroySwapchainKHR) in other threads.
// NOTE: when we add support for tracking opposite side of the thread conflict (planned functionality),
// then it would be possible to account for vkWaitForPresentKHR + vkDestroySwapchainKHR combination.
// In that case we still need to consider if that's something that's worth to do.
// Code for checking that combination will interact with main thread safety detection code,
// so it should be simple and robust addition in order not to break main thread safety detection code
// in a subtle way (threading!). Ratio risk/value looks too high now.
}
void Device::PostCallRecordWaitForPresentKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t presentId, uint64_t timeout,
const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
}
void Device::PreCallRecordCreatePipelineBinariesKHR(VkDevice device, const VkPipelineBinaryCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkPipelineBinaryHandlesInfoKHR* pBinaries, const RecordObject& record_obj) {
StartReadObjectParentInstance(device, record_obj.location);
}
void Device::PostCallRecordCreatePipelineBinariesKHR(VkDevice device, const VkPipelineBinaryCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkPipelineBinaryHandlesInfoKHR* pBinaries, const RecordObject& record_obj) {
FinishReadObjectParentInstance(device, record_obj.location);
if (record_obj.result == VK_SUCCESS) {
for (uint32_t i = 0; i < pBinaries->pipelineBinaryCount; ++i) {
if (pBinaries->pPipelineBinaries != nullptr) {
CreateObject(pBinaries->pPipelineBinaries[i]);
}
}
}
}
} // namespace threadsafety
|