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 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
|
/*
* Copyright (c) 2019-2025 Valve Corporation
* Copyright (c) 2019-2025 LunarG, 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 "sync/sync_access_state.h"
#include "utils/sync_utils.h"
#include <vulkan/utility/vk_struct_helper.hpp>
static bool IsRead(SyncAccessIndex access) { return syncAccessReadMask[access]; }
ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
{{VK_PIPELINE_STAGE_2_NONE, SyncAccessFlags()},
{kColorAttachmentExecScope, kColorAttachmentAccessScope},
{kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
{kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
const UntaggedScopeOps scope;
for (const auto &barrier : barriers) {
ApplyBarrier(scope, barrier, layout_transition);
}
}
// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
// lazily, s.t. no previous access reports should need layout transitions.
void ResourceAccessState::ApplyBarriersImmediate(const SyncBarrier &barrier) {
assert(!HasPendingState()); // This should never be call in the middle of another barrier application
const UntaggedScopeOps scope;
ApplyBarrier(scope, barrier, false);
ApplyPendingBarriers(kInvalidTag); // There can't be any need for this tag
}
HazardResult ResourceAccessState::DetectHazard(const SyncAccessInfo &usage_info) const {
const auto &usage_stage = usage_info.stage_mask;
if (IsRead(usage_info.access_index)) {
if (IsRAWHazard(usage_info)) {
return HazardResult::HazardVsPriorWrite(this, usage_info, READ_AFTER_WRITE, *last_write);
}
} else {
// Write operation:
// Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
// If reads exists -- test only against them because either:
// * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
// * the read weren't hazards, and thus if the write is safe w.r.t. the reads, no hazard vs. last_write is possible if
// the current write happens after the reads, so just test the write against the reades
// Otherwise test against last_write
//
// Look for casus belli for WAR
if (last_reads.size()) {
for (const auto &read_access : last_reads) {
if (IsReadHazard(usage_stage, read_access)) {
return HazardResult::HazardVsPriorRead(this, usage_info, WRITE_AFTER_READ, read_access);
}
}
} else if (last_write.has_value() && last_write->IsWriteHazard(usage_info)) {
// Write-After-Write check -- if we have a previous write to test against
return HazardResult::HazardVsPriorWrite(this, usage_info, WRITE_AFTER_WRITE, *last_write);
}
}
return {};
}
HazardResult ResourceAccessState::DetectHazard(const SyncAccessInfo &usage_info, const OrderingBarrier &ordering, SyncFlags flags,
QueueId queue_id) const {
// The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
const VkPipelineStageFlagBits2 usage_stage = usage_info.stage_mask;
const SyncAccessIndex access_index = usage_info.access_index;
const bool input_attachment_ordering = ordering.access_scope[SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ];
if (IsRead(usage_info.access_index)) {
// Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
bool is_raw_hazard = IsRAWHazard(usage_info);
if (is_raw_hazard) {
// NOTE: we know last_write is non-zero
// See if the ordering rules save us from the simple RAW check above
// First check to see if the current usage is covered by the ordering rules
const bool usage_is_input_attachment = (access_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
const bool usage_is_ordered =
(input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
if (usage_is_ordered) {
// Check if the most recent write is ordered.
// Input attachment is ordered against load op but not against regular draws (requires subpass barrier).
bool most_recent_is_ordered =
last_write->IsOrdered(ordering, queue_id) && (!usage_is_input_attachment || last_write->IsLoadOp());
// If most recent write is not ordered then check if subsequent read is ordered
if (!most_recent_is_ordered) {
most_recent_is_ordered = (GetOrderedStages(queue_id, ordering, flags) != 0);
}
is_raw_hazard = !most_recent_is_ordered;
}
}
if (is_raw_hazard) {
return HazardResult::HazardVsPriorWrite(this, usage_info, READ_AFTER_WRITE, *last_write);
}
} else if (access_index == SyncAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
// For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
return DetectBarrierHazard(usage_info, queue_id, ordering.exec_scope, ordering.access_scope);
} else {
// Only check for WAW if there are no reads since last_write
const bool usage_write_is_ordered = (usage_info.access_bit & ordering.access_scope).any();
if (last_reads.size()) {
// Look for any WAR hazards outside the ordered set of stages
VkPipelineStageFlags2 ordered_stages = VK_PIPELINE_STAGE_2_NONE;
if (usage_write_is_ordered) {
// If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
ordered_stages = GetOrderedStages(queue_id, ordering, flags);
}
// If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
if ((ordered_stages & last_read_stages) != last_read_stages) {
for (const auto &read_access : last_reads) {
if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
if (IsReadHazard(usage_stage, read_access)) {
return HazardResult::HazardVsPriorRead(this, usage_info, WRITE_AFTER_READ, read_access);
}
}
}
} else if (last_write.has_value() && !(last_write->IsOrdered(ordering, queue_id) && usage_write_is_ordered)) {
bool ilt_ilt_hazard = false;
if ((access_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (last_write->IsIndex(SYNC_IMAGE_LAYOUT_TRANSITION))) {
// ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
// scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
ilt_ilt_hazard = !(last_write->Barriers() & ordering.access_scope).any();
}
if (ilt_ilt_hazard || last_write->IsWriteHazard(usage_info)) {
return HazardResult::HazardVsPriorWrite(this, usage_info, WRITE_AFTER_WRITE, *last_write);
}
}
}
return {};
}
HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, QueueId queue_id,
const ResourceUsageRange &tag_range) const {
HazardResult hazard;
using Size = FirstAccesses::size_type;
const auto &recorded_accesses = recorded_use.first_accesses_;
Size count = recorded_accesses.size();
if (count) {
// First access is only closed if the last is a write
bool do_write_last = recorded_use.first_access_closed_;
if (do_write_last) {
// Note: We know count > 0 so this is alway safe.
--count;
}
for (Size i = 0; i < count; ++i) {
const auto &first = recorded_accesses[i];
// Skip and quit logic
if (first.tag < tag_range.begin) continue;
if (first.tag >= tag_range.end) {
do_write_last = false; // ignore last since we know it can't be in tag_range
break;
}
const auto &first_ordering = GetOrderingRules(first.ordering_rule);
hazard = DetectHazard(*first.usage_info, first_ordering, 0, queue_id);
if (hazard.IsHazard()) {
hazard.AddRecordedAccess(first);
break;
}
}
if (do_write_last) {
// Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
const auto &last_access = recorded_accesses.back();
if (tag_range.includes(last_access.tag)) {
OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
if (last_access.usage_info->access_index == SyncAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
// Or in the layout first access scope as a barrier... IFF the usage is an ILT
// this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
// the barrier that applies them
barrier |= recorded_use.first_write_layout_ordering_;
}
// Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages
// in the active context
if (recorded_use.first_read_stages_) {
// we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
// reads in the active context are not "most recent" as all recorded context operations are *after* them
// This suppresses only RAW checks for stages present in the recorded context, but not those only present in the
// active context.
barrier.exec_scope |= recorded_use.first_read_stages_;
// if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
barrier.access_scope |= last_access.usage_info->access_bit;
}
hazard = DetectHazard(*last_access.usage_info, barrier, 0, queue_id);
if (hazard.IsHazard()) {
hazard.AddRecordedAccess(last_access);
}
}
}
}
return hazard;
}
// Asynchronous Hazards occur between subpasses with no connection through the DAG
HazardResult ResourceAccessState::DetectAsyncHazard(const SyncAccessInfo &usage_info, const ResourceUsageTag start_tag,
QueueId queue_id) const {
// Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
// subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
// the raster ordering rules.
if (IsRead(usage_info.access_index)) {
if (last_write.has_value() && last_write->IsQueue(queue_id) && (last_write->tag_ >= start_tag)) {
return HazardResult::HazardVsPriorWrite(this, usage_info, READ_RACING_WRITE, *last_write);
}
} else {
if (last_write.has_value() && last_write->IsQueue(queue_id) && (last_write->tag_ >= start_tag)) {
return HazardResult::HazardVsPriorWrite(this, usage_info, WRITE_RACING_WRITE, *last_write);
} else if (last_reads.size() > 0) {
// Any reads during the other subpass will conflict with this write, so we need to check them all.
for (const auto &read_access : last_reads) {
if (read_access.queue == queue_id && read_access.tag >= start_tag) {
return HazardResult::HazardVsPriorRead(this, usage_info, WRITE_RACING_READ, read_access);
}
}
}
}
return {};
}
HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
ResourceUsageTag start_tag, QueueId queue_id) const {
for (const auto &first : recorded_use.first_accesses_) {
// Skip and quit logic
if (first.tag < tag_range.begin) continue;
if (first.tag >= tag_range.end) break;
HazardResult hazard = DetectAsyncHazard(*first.usage_info, start_tag, queue_id);
if (hazard.IsHazard()) {
hazard.AddRecordedAccess(first);
return hazard;
}
}
return {};
}
HazardResult ResourceAccessState::DetectBarrierHazard(const SyncAccessInfo &usage_info, QueueId queue_id,
VkPipelineStageFlags2 src_exec_scope,
const SyncAccessFlags &src_access_scope) const {
// Only supporting image layout transitions for now
assert(usage_info.access_index == SyncAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
// only test for WAW if there no intervening read operations.
// See DetectHazard(SyncStagetAccessIndex) above for more details.
if (last_reads.size()) {
// Look at the reads if any
for (const auto &read_access : last_reads) {
if (read_access.IsReadBarrierHazard(queue_id, src_exec_scope, src_access_scope)) {
return HazardResult::HazardVsPriorRead(this, usage_info, WRITE_AFTER_READ, read_access);
}
}
} else if (last_write.has_value() && IsWriteBarrierHazard(queue_id, src_exec_scope, src_access_scope)) {
return HazardResult::HazardVsPriorWrite(this, usage_info, WRITE_AFTER_WRITE, *last_write);
}
return {};
}
HazardResult ResourceAccessState::DetectBarrierHazard(const SyncAccessInfo &usage_info, const ResourceAccessState &scope_state,
VkPipelineStageFlags2 src_exec_scope, const SyncAccessFlags &src_access_scope,
QueueId event_queue, ResourceUsageTag event_tag) const {
// Only supporting image layout transitions for now
assert(usage_info.access_index == SyncAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
if (last_write.has_value() && (last_write->tag_ >= event_tag)) {
// Any write after the event precludes the possibility of being in the first access scope for the layout transition
return HazardResult::HazardVsPriorWrite(this, usage_info, WRITE_AFTER_WRITE, *last_write);
} else {
// only test for WAW if there no intervening read operations.
// See DetectHazard(SyncStagetAccessIndex) above for more details.
if (last_reads.size()) {
// Look at the reads if any... if reads exist, they are either the reason the access is in the event
// first scope, or they are a hazard.
const ReadStates &scope_reads = scope_state.last_reads;
const ReadStates::size_type scope_read_count = scope_reads.size();
// Since the hasn't been a write:
// * The current read state is a superset of the scoped one
// * The stage order is the same.
assert(last_reads.size() >= scope_read_count);
for (ReadStates::size_type read_idx = 0; read_idx < scope_read_count; ++read_idx) {
const ReadState &scope_read = scope_reads[read_idx];
const ReadState ¤t_read = last_reads[read_idx];
assert(scope_read.stage == current_read.stage);
if (current_read.tag > event_tag) {
// The read is more recent than the set event scope, thus no barrier from the wait/ILT.
return HazardResult::HazardVsPriorRead(this, usage_info, WRITE_AFTER_READ, current_read);
} else {
// The read is in the events first synchronization scope, so we use a barrier hazard check
// If the read stage is not in the src sync scope
// *AND* not execution chained with an existing sync barrier (that's the or)
// then the barrier access is unsafe (R/W after R)
if (scope_read.IsReadBarrierHazard(event_queue, src_exec_scope, src_access_scope)) {
return HazardResult::HazardVsPriorRead(this, usage_info, WRITE_AFTER_READ, scope_read);
}
}
}
if (last_reads.size() > scope_read_count) {
const ReadState ¤t_read = last_reads[scope_read_count];
return HazardResult::HazardVsPriorRead(this, usage_info, WRITE_AFTER_READ, current_read);
}
} else if (last_write.has_value()) {
// if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
// The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
// So do a normal barrier hazard check
if (scope_state.IsWriteBarrierHazard(event_queue, src_exec_scope, src_access_scope)) {
return HazardResult::HazardVsPriorWrite(&scope_state, usage_info, WRITE_AFTER_WRITE, *scope_state.last_write);
}
}
}
return {};
}
void ResourceAccessState::MergePending(const ResourceAccessState &other) {
pending_layout_transition |= other.pending_layout_transition;
}
void ResourceAccessState::MergeReads(const ResourceAccessState &other) {
// Merge the read states
const auto pre_merge_count = last_reads.size();
const auto pre_merge_stages = last_read_stages;
for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
auto &other_read = other.last_reads[other_read_index];
if (pre_merge_stages & other_read.stage) {
// Merge in the barriers for read stages that exist in *both* this and other
// TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
// but we should wait on profiling data for that.
for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
auto &my_read = last_reads[my_read_index];
if (other_read.stage == my_read.stage) {
if (my_read.tag < other_read.tag) {
// Other is more recent, copy in the state
my_read.access_index = other_read.access_index;
my_read.tag = other_read.tag;
my_read.handle_index = other_read.handle_index;
my_read.queue = other_read.queue;
my_read.pending_dep_chain = other_read.pending_dep_chain;
// TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
// May require tracking more than one access per stage.
my_read.barriers = other_read.barriers;
my_read.sync_stages = other_read.sync_stages;
if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT) {
// Since I'm overwriting the fragement stage read, also update the input attachment info
// as this is the only stage that affects it.
input_attachment_read = other.input_attachment_read;
}
} else if (other_read.tag == my_read.tag) {
// The read tags match so merge the barriers
my_read.barriers |= other_read.barriers;
my_read.sync_stages |= other_read.sync_stages;
my_read.pending_dep_chain |= other_read.pending_dep_chain;
}
break;
}
}
} else {
// The other read stage doesn't exist in this, so add it.
last_reads.emplace_back(other_read);
last_read_stages |= other_read.stage;
if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT) {
input_attachment_read = other.input_attachment_read;
}
}
}
read_execution_barriers |= other.read_execution_barriers;
}
// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
void ResourceAccessState::Resolve(const ResourceAccessState &other) {
bool skip_first = false;
if (last_write.has_value()) {
if (other.last_write.has_value()) {
if (last_write->Tag() < other.last_write->Tag()) {
// NOTE: Both last and other have writes, and thus first access is "closed". We are selecting other's
// first_access state, but it and this can only differ if there are async hazards
// error state.
//
// If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
// operation
*this = other;
skip_first = true;
} else if (last_write->Tag() == other.last_write->Tag()) {
// In the *equals* case for write operations, we merged the write barriers and the read state (but without the
// dependency chaining logic or any stage expansion)
last_write->MergeBarriers(*other.last_write);
MergePending(other);
MergeReads(other);
} else {
// other write is before this write... in which case we keep this instead of other
// and can skip the "first_access" merge, since first_access has been closed since other write tag or before
skip_first = true;
}
} else {
// this has a write and other doesn't -- at best async read in other, which have been reported, and will be dropped
// Since this has a write first access is closed and shouldn't be updated by other
skip_first = true;
}
} else if (other.last_write.has_value()) { // && not this->last_write
// Other has write and this doesn't, thus keep it, See first access NOTE above
*this = other;
skip_first = true;
} else { // not this->last_write OR other.last_write
// Neither state has a write, just merge the reads
MergePending(other);
MergeReads(other);
}
// Merge first access information by making a copy of this first_access and reconstructing with a shuffle
// of the copy and other into this using the update first logic.
// NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
// of the other first_accesses... )
if (!skip_first && !(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
FirstAccesses firsts(std::move(first_accesses_));
ClearFirstUse();
auto a = firsts.begin();
auto a_end = firsts.end();
for (auto &b : other.first_accesses_) {
// TODO: Determine whether some tag offset will be needed for PHASE II
while ((a != a_end) && (a->tag < b.tag)) {
UpdateFirst(a->TagEx(), *a->usage_info, a->ordering_rule);
++a;
}
UpdateFirst(b.TagEx(), *b.usage_info, b.ordering_rule);
}
for (; a != a_end; ++a) {
UpdateFirst(a->TagEx(), *a->usage_info, a->ordering_rule);
}
}
}
void ResourceAccessState::Update(const SyncAccessInfo &usage_info, SyncOrdering ordering_rule, ResourceUsageTagEx tag_ex,
SyncFlags flags) {
const VkPipelineStageFlagBits2 usage_stage = usage_info.stage_mask;
if (IsRead(usage_info.access_index)) {
// Mulitple outstanding reads may be of interest and do dependency chains independently
// However, for purposes of barrier tracking, only one read per pipeline stage matters
if (usage_stage & last_read_stages) {
const auto not_usage_stage = ~usage_stage;
for (auto &read_access : last_reads) {
if (read_access.stage == usage_stage) {
// TODO: having Set here instead of constructor makes measurable performance difference.
// With MSVC compiler for doom capture using constructor results in: 4.8 fps -> 4.0 fps.
// When the entire system is more optimized there should be no sensitivity to such changes
// (more POD objects), and the Set method should be removed.
read_access.Set(usage_stage, usage_info.access_index, tag_ex);
} else if (read_access.barriers & usage_stage) {
// If the current access is barriered to this stage, mark it as "known to happen after"
read_access.sync_stages |= usage_stage;
} else {
// If the current access is *NOT* barriered to this stage it needs to be cleared.
// Note: this is possible because semaphores can *clear* effective barriers, so the assumption
// that sync_stages is a subset of barriers may not apply.
read_access.sync_stages &= not_usage_stage;
}
}
} else {
for (auto &read_access : last_reads) {
if (read_access.barriers & usage_stage) {
read_access.sync_stages |= usage_stage;
}
}
last_reads.emplace_back(usage_stage, usage_info.access_index, tag_ex);
last_read_stages |= usage_stage;
}
// Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT) {
// TODO Revisit re: multiple reads for a given stage
input_attachment_read = (usage_info.access_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
}
} else {
// Assume write
// TODO determine what to do with READ-WRITE operations if any
SetWrite(usage_info, tag_ex, flags);
}
UpdateFirst(tag_ex, usage_info, ordering_rule);
}
HazardResult HazardResult::HazardVsPriorWrite(const ResourceAccessState *access_state, const SyncAccessInfo &usage_info,
SyncHazard hazard, const WriteState &prior_write) {
HazardResult result;
result.state_.emplace(access_state, usage_info, hazard, prior_write.Access().access_index, prior_write.TagEx());
return result;
}
HazardResult HazardResult::HazardVsPriorRead(const ResourceAccessState *access_state, const SyncAccessInfo &usage_info,
SyncHazard hazard, const ReadState &prior_read) {
assert(prior_read.access_index != SYNC_ACCESS_INDEX_NONE);
HazardResult result;
result.state_.emplace(access_state, usage_info, hazard, prior_read.access_index, prior_read.TagEx());
return result;
}
void HazardResult::AddRecordedAccess(const ResourceFirstAccess &first_access) {
assert(state_.has_value());
state_->recorded_access = std::make_unique<const ResourceFirstAccess>(first_access);
}
bool HazardResult::IsWAWHazard() const {
assert(state_.has_value());
assert(state_->prior_access_index != SYNC_ACCESS_INDEX_NONE);
return (state_->hazard == WRITE_AFTER_WRITE) && (state_->prior_access_index == state_->access_index);
}
// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
// We can overwrite them as *this* write is now after them.
//
// Note: intentionally ignore pending barriers and chains (i.e. don't apply or clear them), let ApplyPendingBarriers handle them.
void ResourceAccessState::SetWrite(const SyncAccessInfo &usage_info, ResourceUsageTagEx tag_ex, SyncFlags flags) {
ClearRead();
if (last_write.has_value()) {
last_write->Set(usage_info, tag_ex, flags);
} else {
last_write.emplace(usage_info, tag_ex, flags);
}
}
void ResourceAccessState::ClearWrite() { last_write.reset(); }
void ResourceAccessState::ClearRead() {
last_reads.clear();
last_read_stages = VK_PIPELINE_STAGE_2_NONE;
read_execution_barriers = VK_PIPELINE_STAGE_2_NONE;
input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
}
void ResourceAccessState::ClearFirstUse() {
first_accesses_.clear();
first_read_stages_ = VK_PIPELINE_STAGE_2_NONE;
first_write_layout_ordering_ = OrderingBarrier();
first_access_closed_ = false;
}
void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
if (pending_layout_transition) {
// SetWrite clobbers the last_reads array, and thus we don't have to clear the read_state out.
const SyncAccessInfo &layout_usage_info = GetAccessInfo(SYNC_IMAGE_LAYOUT_TRANSITION);
const ResourceUsageTagEx tag_ex = ResourceUsageTagEx{tag, pending_layout_transition_handle_index};
SetWrite(layout_usage_info, tag_ex); // Side effect notes below
UpdateFirst(tag_ex, layout_usage_info, SyncOrdering::kNonAttachment);
TouchupFirstForLayoutTransition(tag, last_write->GetPendingLayoutOrdering());
last_write->ApplyPendingBarriers();
pending_layout_transition = false;
pending_layout_transition_handle_index = vvl::kNoIndex32;
} else {
// Apply the accumulate execution barriers (and thus update chaining information)
// for layout transition, last_reads is reset by SetWrite, so this will be skipped.
for (auto &read_access : last_reads) {
read_execution_barriers |= read_access.ApplyPendingBarriers();
}
// We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
if (last_write.has_value()) {
last_write->ApplyPendingBarriers();
}
}
}
// Assumes signal queue != wait queue
void ResourceAccessState::ApplySemaphore(const SemaphoreScope &signal, const SemaphoreScope wait) {
// Semaphores only guarantee the first scope of the signal is before the second scope of the wait.
// If any access isn't in the first scope, there are no guarantees, thus those barriers are cleared
assert(signal.queue != wait.queue);
for (auto &read_access : last_reads) {
if (read_access.ReadInQueueScopeOrChain(signal.queue, signal.exec_scope)) {
// Deflects WAR on wait queue
read_access.barriers = wait.exec_scope;
} else {
// Leave sync stages alone. Update method will clear unsynchronized stages on subsequent reads as needed.
read_access.barriers = VK_PIPELINE_STAGE_2_NONE;
}
}
if (WriteInQueueSourceScopeOrChain(signal.queue, signal.exec_scope, signal.valid_accesses)) {
assert(last_write.has_value());
// Will deflect RAW wait queue, WAW needs a chained barrier on wait queue
read_execution_barriers = wait.exec_scope;
last_write->barriers_ = wait.valid_accesses;
} else {
read_execution_barriers = VK_PIPELINE_STAGE_2_NONE;
if (last_write.has_value()) last_write->barriers_.reset();
}
if (last_write.has_value()) last_write->dependency_chain_ = read_execution_barriers;
}
// Read access predicate for queue wait
bool ResourceAccessState::WaitQueueTagPredicate::operator()(const ReadState &read_access) const {
return (read_access.queue == queue) && (read_access.tag <= tag) &&
(read_access.stage != VK_PIPELINE_STAGE_2_PRESENT_ENGINE_BIT_SYNCVAL);
}
bool ResourceAccessState::WaitQueueTagPredicate::operator()(const ResourceAccessState &access) const {
if (!access.last_write.has_value()) return false;
const auto &write_state = *access.last_write;
return write_state.IsQueue(queue) && (write_state.Tag() <= tag) &&
!write_state.IsIndex(SYNC_PRESENT_ENGINE_SYNCVAL_PRESENT_PRESENTED_SYNCVAL);
}
// Read access predicate for queue wait
bool ResourceAccessState::WaitTagPredicate::operator()(const ReadState &read_access) const {
return (read_access.tag <= tag) && (read_access.stage != VK_PIPELINE_STAGE_2_PRESENT_ENGINE_BIT_SYNCVAL);
}
bool ResourceAccessState::WaitTagPredicate::operator()(const ResourceAccessState &access) const {
if (!access.last_write.has_value()) return false;
const auto &write_state = *access.last_write;
return (write_state.Tag() <= tag) && !write_state.IsIndex(SYNC_PRESENT_ENGINE_SYNCVAL_PRESENT_PRESENTED_SYNCVAL);
}
// Present operations only matching only the *exactly* tagged present and acquire operations
bool ResourceAccessState::WaitAcquirePredicate::operator()(const ReadState &read_access) const {
return (read_access.tag == acquire_tag) && (read_access.stage == VK_PIPELINE_STAGE_2_PRESENT_ENGINE_BIT_SYNCVAL);
}
bool ResourceAccessState::WaitAcquirePredicate::operator()(const ResourceAccessState &access) const {
if (!access.last_write.has_value()) return false;
const auto &write_state = *access.last_write;
return (write_state.Tag() == present_tag) && write_state.IsIndex(SYNC_PRESENT_ENGINE_SYNCVAL_PRESENT_PRESENTED_SYNCVAL);
}
bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
if (!first_accesses_.size()) return false;
const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
return tag_range.intersects(first_access_range);
}
void ResourceAccessState::OffsetTag(ResourceUsageTag offset) {
if (last_write.has_value()) last_write->OffsetTag(offset);
for (auto &read_access : last_reads) {
read_access.tag += offset;
}
for (auto &first : first_accesses_) {
first.tag += offset;
}
}
static const SyncAccessFlags kAllSyncStageAccessBits = ~SyncAccessFlags(0);
ResourceAccessState::ResourceAccessState()
: last_write(),
last_read_stages(0),
read_execution_barriers(VK_PIPELINE_STAGE_2_NONE),
last_reads(),
input_attachment_read(false),
pending_layout_transition(false),
first_accesses_(),
first_read_stages_(VK_PIPELINE_STAGE_2_NONE),
first_write_layout_ordering_(),
first_access_closed_(false) {}
VkPipelineStageFlags2 ResourceAccessState::GetReadBarriers(SyncAccessIndex access_index) const {
for (const auto &read_access : last_reads) {
if (read_access.access_index == access_index) {
return read_access.barriers;
}
}
return VK_PIPELINE_STAGE_2_NONE;
}
void ResourceAccessState::SetQueueId(QueueId id) {
for (auto &read_access : last_reads) {
if (read_access.queue == kQueueIdInvalid) {
read_access.queue = id;
}
}
if (last_write.has_value()) last_write->SetQueueId(id);
}
bool ResourceAccessState::IsWriteBarrierHazard(QueueId queue_id, VkPipelineStageFlags2 src_exec_scope,
const SyncAccessFlags &src_access_scope) const {
return last_write.has_value() && last_write->IsWriteBarrierHazard(queue_id, src_exec_scope, src_access_scope);
}
bool ResourceAccessState::WriteInSourceScopeOrChain(VkPipelineStageFlags2 src_exec_scope, SyncAccessFlags src_access_scope) const {
return last_write.has_value() && last_write->WriteInSourceScopeOrChain(src_exec_scope, src_access_scope);
}
bool ResourceAccessState::WriteInQueueSourceScopeOrChain(QueueId queue, VkPipelineStageFlags2 src_exec_scope,
const SyncAccessFlags &src_access_scope) const {
return last_write.has_value() && last_write->WriteInQueueSourceScopeOrChain(queue, src_exec_scope, src_access_scope);
}
bool ResourceAccessState::WriteInEventScope(VkPipelineStageFlags2 src_exec_scope, const SyncAccessFlags &src_access_scope,
QueueId scope_queue, ResourceUsageTag scope_tag) const {
return last_write.has_value() && last_write->WriteInEventScope(src_exec_scope, src_access_scope, scope_queue, scope_tag);
}
// As ReadStates must be unique by stage, this is as good a sort as needed
bool operator<(const ReadState &lhs, const ReadState &rhs) { return lhs.stage < rhs.stage; }
void ResourceAccessState::Normalize() {
std::sort(last_reads.begin(), last_reads.end());
ClearFirstUse();
}
void ResourceAccessState::GatherReferencedTags(ResourceUsageTagSet &used) const {
if (last_write.has_value()) {
used.CachedInsert(last_write->Tag());
}
for (const auto &read_access : last_reads) {
used.CachedInsert(read_access.tag);
}
}
bool ResourceAccessState::IsRAWHazard(const SyncAccessInfo &usage_info) const {
assert(IsRead(usage_info.access_index));
// Only RAW vs. last_write if it doesn't happen-after any other read because either:
// * the previous reads are not hazards, and thus last_write must be visible and available to
// any reads that happen after.
// * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
// the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
return last_write.has_value() && (0 == (read_execution_barriers & usage_info.stage_mask)) &&
last_write->IsWriteHazard(usage_info);
}
VkPipelineStageFlags2 ResourceAccessState::GetOrderedStages(QueueId queue_id, const OrderingBarrier &ordering, SyncFlags flags) const {
// At apply queue submission order limits on the effect of ordering
VkPipelineStageFlags2 non_qso_stages = VK_PIPELINE_STAGE_2_NONE;
if (queue_id != kQueueIdInvalid) {
for (const auto &read_access : last_reads) {
if (read_access.queue != queue_id) {
non_qso_stages |= read_access.stage;
}
}
}
// Whether the stage are in the ordering scope only matters if the current write is ordered
const VkPipelineStageFlags2 read_stages_in_qso = last_read_stages & ~non_qso_stages;
VkPipelineStageFlags2 ordered_stages = read_stages_in_qso & ordering.exec_scope;
// Special input attachment handling as always (not encoded in exec_scop)
const bool input_attachment_ordering = ordering.access_scope[SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ];
if (input_attachment_ordering && input_attachment_read && (flags & SyncFlag::kStoreOp) != 0) {
// If we have an input attachment in last_reads and input attachments are ordered we all that stage
ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT;
}
return ordered_stages;
}
void ResourceAccessState::UpdateFirst(const ResourceUsageTagEx tag_ex, const SyncAccessInfo &usage_info,
SyncOrdering ordering_rule) {
// Only record until we record a write.
if (!first_access_closed_) {
const bool is_read = IsRead(usage_info.access_index);
const VkPipelineStageFlags2 usage_stage = is_read ? usage_info.stage_mask : 0U;
if (0 == (usage_stage & first_read_stages_)) {
// If this is a read we haven't seen or a write, record.
// We always need to know what stages were found prior to write
first_read_stages_ |= usage_stage;
if (0 == (read_execution_barriers & usage_stage)) {
// If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
first_accesses_.emplace_back(usage_info, tag_ex, ordering_rule);
first_access_closed_ = !is_read;
}
}
}
}
void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
// Only call this after recording an image layout transition
assert(first_accesses_.size());
if (first_accesses_.back().tag == tag) {
// If this layout transition is the the first write, add the additional ordering rules that guard the ILT
assert(first_accesses_.back().usage_info->access_index == SyncAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
first_write_layout_ordering_ = layout_ordering;
}
}
ReadState::ReadState(VkPipelineStageFlags2 stage, SyncAccessIndex access_index, ResourceUsageTagEx tag_ex) {
Set(stage, access_index, tag_ex);
}
void ReadState::Set(VkPipelineStageFlags2 stage, SyncAccessIndex access_index, ResourceUsageTagEx tag_ex) {
assert(access_index != SYNC_ACCESS_INDEX_NONE);
this->stage = stage;
this->access_index = access_index;
barriers = VK_PIPELINE_STAGE_2_NONE;
sync_stages = VK_PIPELINE_STAGE_2_NONE;
tag = tag_ex.tag;
handle_index = tag_ex.handle_index;
queue = kQueueIdInvalid;
pending_dep_chain = VK_PIPELINE_STAGE_2_NONE; // If this is a new read, we aren't applying a barrier set.
}
// Scope test including "queue submission order" effects. Specifically, accesses from a different queue are not
// considered to be in "queue submission order" with barriers, events, or semaphore signalling, but any barriers
// that have bee applied (via semaphore) to those accesses can be chained off of.
bool ReadState::ReadInQueueScopeOrChain(QueueId scope_queue, VkPipelineStageFlags2 exec_scope) const {
VkPipelineStageFlags2 effective_stages = barriers | ((scope_queue == queue) ? stage : VK_PIPELINE_STAGE_2_NONE);
// Special case. AS copy operations (e.g., vkCmdCopyAccelerationStructureKHR) can be synchronized using
// the ACCELERATION_STRUCTURE_COPY stage, but it's also valid to use ACCELERATION_STRUCTURE_BUILD stage.
// Internally, AS copy accesses are represented via ACCELERATION_STRUCTURE_COPY stage. The logic below
// ensures that ACCELERATION_STRUCTURE_COPY accesses can be protected by the barrier that specifies the
// ACCELERATION_STRUCTURE_BUILD state.
if (access_index == SYNC_ACCELERATION_STRUCTURE_COPY_ACCELERATION_STRUCTURE_READ) {
effective_stages |= VK_PIPELINE_STAGE_2_ACCELERATION_STRUCTURE_BUILD_BIT_KHR;
}
return (exec_scope & effective_stages) != 0;
}
VkPipelineStageFlags2 ReadState::ApplyPendingBarriers() {
barriers |= pending_dep_chain;
pending_dep_chain = VK_PIPELINE_STAGE_2_NONE;
return barriers;
}
WriteState::WriteState(const SyncAccessInfo &usage_info, ResourceUsageTagEx tag_ex, SyncFlags flags)
: access_(&usage_info),
barriers_(),
tag_(tag_ex.tag),
handle_index_(tag_ex.handle_index),
queue_(kQueueIdInvalid),
flags_(flags),
dependency_chain_(VK_PIPELINE_STAGE_2_NONE),
pending_layout_ordering_(),
pending_dep_chain_(VK_PIPELINE_STAGE_2_NONE),
pending_barriers_() {}
bool WriteState::IsWriteHazard(const SyncAccessInfo &usage_info) const { return !barriers_[usage_info.access_index]; }
bool WriteState::IsOrdered(const OrderingBarrier &ordering, QueueId queue_id) const {
assert(access_);
return (queue_ == queue_id) && ordering.access_scope[access_->access_index];
}
bool WriteState::IsWriteBarrierHazard(QueueId queue_id, VkPipelineStageFlags2 src_exec_scope,
const SyncAccessFlags &src_access_scope) const {
// Current implementation relies on TOP_OF_PIPE constant due to the fact that it's non-zero value
// and AND-ing with it can create execution dependency when necessary. One example, it allows the
// ALL_COMMANDS stage to guard all accesses even if NONE/TOP_OF_PIPE is used. When NONE constant is
// used, which has numerical value of zero, then AND-ing with it always results in 0 which means
// "no barrier", so it's not possible to use NONE internally in equivalent way to TOP_OF_PIPE.
// Here we replace NONE with TOP_OF_PIPE in the scenarios where they are equivalent according to the spec.
//
// If we update implementation to get rid of deprecated TOP_OF_PIPE/BOTTOM_OF_PIPE then we must
// invert the condition below and exchange TOP_OF_PIPE and NONE roles, so deprecated stages would
// not propagate into implementation internals.
if (src_exec_scope == VK_PIPELINE_STAGE_2_NONE && src_access_scope.none()) {
src_exec_scope = VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT;
}
// Special rules for sequential ILT's
if (IsIndex(SYNC_IMAGE_LAYOUT_TRANSITION)) {
if (queue_id == queue_) {
// In queue, they are implicitly ordered
return false;
} else {
// In dep chain means that the ILT is *available*
return !WriteInChain(src_exec_scope);
}
}
// In dep chain means that the write is *available*.
// Available writes are automatically made visible and can't cause hazards during transition.
if (WriteInChain(src_exec_scope)) {
return false;
}
// The write is not in chain (previous call), so need only to check if the write is in access scope.
return !WriteInScope(src_access_scope);
}
void WriteState::Set(const SyncAccessInfo &usage_info, ResourceUsageTagEx tag_ex, SyncFlags flags) {
access_ = &usage_info;
barriers_.reset();
dependency_chain_ = VK_PIPELINE_STAGE_2_NONE;
tag_ = tag_ex.tag;
handle_index_ = tag_ex.handle_index;
queue_ = kQueueIdInvalid;
flags_ = flags;
}
void WriteState::MergeBarriers(const WriteState &other) {
barriers_ |= other.barriers_;
dependency_chain_ |= other.dependency_chain_;
pending_barriers_ |= other.pending_barriers_;
pending_dep_chain_ |= other.pending_dep_chain_;
pending_layout_ordering_ |= other.pending_layout_ordering_;
}
void WriteState::UpdatePendingBarriers(const SyncBarrier &barrier) {
pending_barriers_ |= barrier.dst_access_scope;
pending_dep_chain_ |= barrier.dst_exec_scope.exec_scope;
}
void WriteState::ApplyPendingBarriers() {
dependency_chain_ |= pending_dep_chain_;
barriers_ |= pending_barriers_;
// Reset pending state
pending_dep_chain_ = VK_PIPELINE_STAGE_2_NONE;
pending_barriers_.reset();
pending_layout_ordering_ = OrderingBarrier();
}
void WriteState::UpdatePendingLayoutOrdering(const SyncBarrier &barrier) {
pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
}
void WriteState::SetQueueId(QueueId id) {
if (queue_ == kQueueIdInvalid) {
queue_ = id;
}
}
bool WriteState::WriteInChain(VkPipelineStageFlags2 src_exec_scope) const {
return 0 != (dependency_chain_ & src_exec_scope);
}
bool WriteState::WriteInScope(const SyncAccessFlags &src_access_scope) const {
assert(access_);
return src_access_scope[access_->access_index];
}
bool WriteState::WriteInSourceScopeOrChain(VkPipelineStageFlags2 src_exec_scope,
SyncAccessFlags src_access_scope) const {
assert(access_);
return WriteInChain(src_exec_scope) || WriteInScope(src_access_scope);
}
bool WriteState::WriteInQueueSourceScopeOrChain(QueueId queue, VkPipelineStageFlags2 src_exec_scope,
const SyncAccessFlags &src_access_scope) const {
assert(access_);
return WriteInChain(src_exec_scope) || ((queue == queue_) && WriteInScope(src_access_scope));
}
bool WriteState::WriteInEventScope(VkPipelineStageFlags2 src_exec_scope, const SyncAccessFlags &src_access_scope,
QueueId scope_queue, ResourceUsageTag scope_tag) const {
// The scope logic for events is, if we're asking, the resource usage was flagged as "in the first execution scope" at
// the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
// in order to know if it's in the excecution scope
assert(access_);
return (tag_ < scope_tag) && WriteInQueueSourceScopeOrChain(scope_queue, src_exec_scope, src_access_scope);
}
HazardResult::HazardState::HazardState(const ResourceAccessState *access_state_, const SyncAccessInfo &access_info_,
SyncHazard hazard_, SyncAccessIndex prior_access_index, ResourceUsageTagEx tag_ex)
: access_state(std::make_unique<const ResourceAccessState>(*access_state_)),
recorded_access(),
access_index(access_info_.access_index),
prior_access_index(prior_access_index),
tag(tag_ex.tag),
handle_index(tag_ex.handle_index),
hazard(hazard_) {
assert(prior_access_index != SYNC_ACCESS_INDEX_NONE);
// Touchup the hazard to reflect "present as release" semantics
// NOTE: For implementing QFO release/acquire semantics... touch up here as well
if (access_state->IsLastWriteOp(SYNC_PRESENT_ENGINE_SYNCVAL_PRESENT_PRESENTED_SYNCVAL)) {
if (hazard == SyncHazard::READ_AFTER_WRITE) {
hazard = SyncHazard::READ_AFTER_PRESENT;
} else if (hazard == SyncHazard::WRITE_AFTER_WRITE) {
hazard = SyncHazard::WRITE_AFTER_PRESENT;
}
} else if (access_index == SYNC_PRESENT_ENGINE_SYNCVAL_PRESENT_PRESENTED_SYNCVAL) {
if (hazard == SyncHazard::WRITE_AFTER_READ) {
hazard = SyncHazard::PRESENT_AFTER_READ;
} else if (hazard == SyncHazard::WRITE_AFTER_WRITE) {
hazard = SyncHazard::PRESENT_AFTER_WRITE;
}
}
}
static VkPipelineStageFlags2 RelatedPipelineStages(VkPipelineStageFlags2 stage_mask,
const vvl::unordered_map<VkPipelineStageFlags2, VkPipelineStageFlags2> &map) {
VkPipelineStageFlags2 unscanned = stage_mask;
VkPipelineStageFlags2 related = 0;
for (const auto &entry : map) {
const auto &stage = entry.first;
if (stage & unscanned) {
related = related | entry.second;
unscanned = unscanned & ~stage;
if (!unscanned) break;
}
}
return related;
}
static VkPipelineStageFlags2 WithEarlierPipelineStages(VkPipelineStageFlags2 stage_mask) {
return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyEarlierStages());
}
static VkPipelineStageFlags2 WithLaterPipelineStages(VkPipelineStageFlags2 stage_mask) {
return stage_mask | RelatedPipelineStages(stage_mask, syncLogicallyLaterStages());
}
template <typename Flags, typename Map>
static SyncAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
SyncAccessFlags scope;
for (const auto &bit_scope : map) {
if (flag_mask < bit_scope.first) break;
if (flag_mask & bit_scope.first) {
scope |= bit_scope.second;
}
}
return scope;
}
static VkAccessFlags2 ExpandAccessFlags(VkAccessFlags2 access_mask) {
VkAccessFlags2 expanded = access_mask;
if (VK_ACCESS_2_SHADER_READ_BIT & access_mask) {
expanded = expanded & ~VK_ACCESS_2_SHADER_READ_BIT;
expanded |= kShaderReadExpandBits;
}
if (VK_ACCESS_2_SHADER_WRITE_BIT & access_mask) {
expanded = expanded & ~VK_ACCESS_2_SHADER_WRITE_BIT;
expanded |= kShaderWriteExpandBits;
}
return expanded;
}
static SyncAccessFlags AccessScopeByStage(VkPipelineStageFlags2 stages) {
return AccessScopeImpl(stages, syncAccessMaskByStageBit());
}
static SyncAccessFlags AccessScopeByAccess(VkAccessFlags2 accesses) {
SyncAccessFlags sync_accesses = AccessScopeImpl(ExpandAccessFlags(accesses), syncAccessMaskByAccessBit());
// The above access expansion replaces SHADER_READ meta access with atomic accesses as defined by the specification.
// ACCELERATION_STRUCTURE_BUILD and MICROMAP_BUILD stages are special in a way that they use SHADER_READ access directly.
// It is an implementation detail of how SHADER_READ is used by the driver, and we cannot make assumption about specific
// atomic accesses. If we make such assumption then it can be a problem when after applying synchronization we won't be
// able to get full SHADER_READ access back, but only a subset of accesses, for example, only SHADER_STORAGE_READ.
// It would mean we made (incorrect) assumption how the driver represents SHADER_READ in the context of AS build.
//
// Handle special cases that use non-expanded meta accesses.
if (accesses & VK_ACCESS_2_SHADER_READ_BIT) {
sync_accesses |= SYNC_ACCELERATION_STRUCTURE_BUILD_SHADER_READ_BIT;
sync_accesses |= SYNC_MICROMAP_BUILD_EXT_SHADER_READ_BIT;
}
return sync_accesses;
}
static SyncAccessFlags AccessScope(const SyncAccessFlags &stage_scope, VkAccessFlags2 accesses) {
SyncAccessFlags access_scope = stage_scope & AccessScopeByAccess(accesses);
// Special case. AS copy operations (e.g., vkCmdCopyAccelerationStructureKHR) can be synchronized using
// the ACCELERATION_STRUCTURE_COPY stage, but it's also valid to use ACCELERATION_STRUCTURE_BUILD stage.
// Internally, AS copy accesses are represented via ACCELERATION_STRUCTURE_COPY stage. The logic below
// ensures that a barrier using ACCELERATION_STRUCTURE_BUILD stage can also protect accesses on
// ACCELERATION_STRUCTURE_COPY stage.
if (access_scope[SYNC_ACCELERATION_STRUCTURE_BUILD_ACCELERATION_STRUCTURE_READ]) {
access_scope.set(SYNC_ACCELERATION_STRUCTURE_COPY_ACCELERATION_STRUCTURE_READ);
}
if (access_scope[SYNC_ACCELERATION_STRUCTURE_BUILD_ACCELERATION_STRUCTURE_WRITE]) {
access_scope.set(SYNC_ACCELERATION_STRUCTURE_COPY_ACCELERATION_STRUCTURE_WRITE);
}
return access_scope;
}
SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2 mask_param,
const VkPipelineStageFlags2 disabled_feature_mask) {
const VkPipelineStageFlags2 expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags, disabled_feature_mask);
SyncExecScope result;
result.mask_param = mask_param;
result.exec_scope = WithEarlierPipelineStages(expanded_mask);
result.valid_accesses = AccessScopeByStage(expanded_mask);
// ALL_COMMANDS stage includes all accesses performed by the gpu, not only accesses defined by the stages
if (mask_param & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT) {
result.valid_accesses |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
}
return result;
}
SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2 mask_param) {
const VkPipelineStageFlags2 expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
SyncExecScope result;
result.mask_param = mask_param;
result.exec_scope = WithLaterPipelineStages(expanded_mask);
result.valid_accesses = AccessScopeByStage(expanded_mask);
// ALL_COMMANDS stage includes all accesses performed by the gpu, not only accesses defined by the stages
if (mask_param & VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT) {
result.valid_accesses |= SYNC_IMAGE_LAYOUT_TRANSITION_BIT;
}
return result;
}
SyncBarrier::SyncBarrier(const SyncExecScope &src_exec, const SyncExecScope &dst_exec)
: src_exec_scope(src_exec), dst_exec_scope(dst_exec) {}
SyncBarrier::SyncBarrier(const SyncExecScope &src_exec, const SyncExecScope &dst_exec, const SyncBarrier::AllAccess &)
: src_exec_scope(src_exec),
src_access_scope(src_exec.valid_accesses),
dst_exec_scope(dst_exec),
dst_access_scope(dst_exec.valid_accesses) {}
SyncBarrier::SyncBarrier(const SyncExecScope &src_exec, VkAccessFlags2 src_access_mask, const SyncExecScope &dst_exec,
VkAccessFlags2 dst_access_mask)
: src_exec_scope(src_exec),
src_access_scope(AccessScope(src_exec.valid_accesses, src_access_mask)),
dst_exec_scope(dst_exec),
dst_access_scope(AccessScope(dst_exec.valid_accesses, dst_access_mask)) {}
SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
const auto barrier = vku::FindStructInPNextChain<VkMemoryBarrier2>(subpass.pNext);
if (barrier) {
auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
src_exec_scope = src;
src_access_scope = AccessScope(src.valid_accesses, barrier->srcAccessMask);
auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
dst_exec_scope = dst;
dst_access_scope = AccessScope(dst.valid_accesses, barrier->dstAccessMask);
} else {
auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
src_exec_scope = src;
src_access_scope = AccessScope(src.valid_accesses, subpass.srcAccessMask);
auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
dst_exec_scope = dst;
dst_access_scope = AccessScope(dst.valid_accesses, subpass.dstAccessMask);
}
}
SyncBarrier::SyncBarrier(const std::vector<SyncBarrier> &barriers) {
// Merge each barrier
for (const SyncBarrier &barrier : barriers) {
// Note that after merge, only the exec_scope and access_scope fields are fully valid
// TODO: Do we need to update any of the other fields? Merging has limited application.
src_exec_scope.exec_scope |= barrier.src_exec_scope.exec_scope;
src_access_scope |= barrier.src_access_scope;
dst_exec_scope.exec_scope |= barrier.dst_exec_scope.exec_scope;
dst_access_scope |= barrier.dst_access_scope;
}
}
const char *string_SyncHazardVUID(SyncHazard hazard) {
switch (hazard) {
case SyncHazard::NONE:
return "SYNC-HAZARD-NONE";
break;
case SyncHazard::READ_AFTER_WRITE:
return "SYNC-HAZARD-READ-AFTER-WRITE";
break;
case SyncHazard::WRITE_AFTER_READ:
return "SYNC-HAZARD-WRITE-AFTER-READ";
break;
case SyncHazard::WRITE_AFTER_WRITE:
return "SYNC-HAZARD-WRITE-AFTER-WRITE";
break;
case SyncHazard::READ_RACING_WRITE:
return "SYNC-HAZARD-READ-RACING-WRITE";
break;
case SyncHazard::WRITE_RACING_WRITE:
return "SYNC-HAZARD-WRITE-RACING-WRITE";
break;
case SyncHazard::WRITE_RACING_READ:
return "SYNC-HAZARD-WRITE-RACING-READ";
break;
case SyncHazard::READ_AFTER_PRESENT:
return "SYNC-HAZARD-READ-AFTER-PRESENT";
break;
case SyncHazard::WRITE_AFTER_PRESENT:
return "SYNC-HAZARD-WRITE-AFTER-PRESENT";
break;
case SyncHazard::PRESENT_AFTER_WRITE:
return "SYNC-HAZARD-PRESENT-AFTER-WRITE";
break;
case SyncHazard::PRESENT_AFTER_READ:
return "SYNC-HAZARD-PRESENT-AFTER-READ";
break;
default:
assert(0);
}
return "SYNC-HAZARD-INVALID";
}
SyncHazardInfo GetSyncHazardInfo(SyncHazard hazard) {
switch (hazard) {
case SyncHazard::NONE:
return SyncHazardInfo{};
case SyncHazard::READ_AFTER_WRITE:
return SyncHazardInfo{false, true};
case SyncHazard::WRITE_AFTER_READ:
return SyncHazardInfo{true, false};
case SyncHazard::WRITE_AFTER_WRITE:
return SyncHazardInfo{true, true};
case SyncHazard::READ_RACING_WRITE:
return SyncHazardInfo{false, true, true};
case SyncHazard::WRITE_RACING_WRITE:
return SyncHazardInfo{true, true, true};
case SyncHazard::WRITE_RACING_READ:
return SyncHazardInfo{true, false, true};
case SyncHazard::READ_AFTER_PRESENT:
return SyncHazardInfo{false, true};
case SyncHazard::WRITE_AFTER_PRESENT:
return SyncHazardInfo{true, true};
case SyncHazard::PRESENT_AFTER_WRITE:
return SyncHazardInfo{true, true};
case SyncHazard::PRESENT_AFTER_READ:
return SyncHazardInfo{true, false};
default:
assert(false && "Unhandled SyncHazard value");
return SyncHazardInfo{};
}
}
|