forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit_time_tracker.cpp
More file actions
210 lines (185 loc) · 9 KB
/
submit_time_tracker.cpp
File metadata and controls
210 lines (185 loc) · 9 KB
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
/* Copyright (c) 2026 The Khronos Group Inc.
* Copyright (c) 2026 Valve Corporation
* Copyright (c) 2026 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 "state_tracker/submit_time_tracker.h"
#include "state_tracker/semaphore_state.h"
#include "state_tracker/state_tracker.h"
#include "state_tracker/wsi_state.h"
#include "containers/container_utils.h"
#include "utils/convert_utils.h"
namespace vvl {
void SubmitTimeTracker::OnCreateTimelineSemaphore(VkSemaphore timeline, uint64_t initial_value) {
std::lock_guard lock(mutex_);
timeline_signals_[timeline] = initial_value;
}
void SubmitTimeTracker::OnDestroyTimelineSemaphore(VkSemaphore timeline) {
std::lock_guard lock(mutex_);
timeline_signals_.erase(timeline);
}
bool SubmitTimeTracker::ProcessSubmitInfo(const VkSubmitInfo& submit_info, VkQueue queue, const Location& submit_loc) const {
SubmitInfoConverter converter(submit_info);
return ProcessSubmitInfo(converter.submit_info2, queue, submit_loc);
}
bool SubmitTimeTracker::ProcessSubmitInfo(const VkSubmitInfo2& submit_info, VkQueue queue, const Location& submit_loc) const {
const auto wait_semaphores =
vvl::span<const VkSemaphoreSubmitInfo>(submit_info.pWaitSemaphoreInfos, submit_info.waitSemaphoreInfoCount);
const auto signal_semaphores =
vvl::span<const VkSemaphoreSubmitInfo>(submit_info.pSignalSemaphoreInfos, submit_info.signalSemaphoreInfoCount);
std::vector<std::shared_ptr<CommandBuffer>> command_buffers;
command_buffers.reserve(submit_info.commandBufferInfoCount);
for (const auto& cb_info : vvl::make_span(submit_info.pCommandBufferInfos, submit_info.commandBufferInfoCount)) {
// If Get returns null, store it to preserve original indexing
command_buffers.emplace_back(validator_.Get<CommandBuffer>(cb_info.commandBuffer));
}
std::lock_guard lock(mutex_);
SubmitTimeTracker& this_tracker = *const_cast<SubmitTimeTracker*>(this);
return this_tracker.ProcessBatch(std::move(command_buffers), wait_semaphores, signal_semaphores, queue, submit_loc);
}
bool SubmitTimeTracker::ProcessSignalSemaphore(const VkSemaphoreSignalInfo& signal_info) const {
std::lock_guard lock(mutex_);
SubmitTimeTracker& this_tracker = *const_cast<SubmitTimeTracker*>(this);
return this_tracker.ProcessSignal(signal_info.semaphore, signal_info.value);
}
// NOTE: when swapchain learns how to work with timeline semaphores, this function should be
// reworked to check for resolving timeline signals similar to ProcessBatch.
// Current version assumes only binary semaphores are allowed (the only option as of April 2026)
bool SubmitTimeTracker::ProcessPresent(const VkPresentInfoKHR& present_info, const Location& present_info_loc) const {
std::lock_guard lock(mutex_);
bool skip = false;
for (uint32_t i = 0; i < present_info.swapchainCount; i++) {
if (auto swapchain_state = validator_.Get<Swapchain>(present_info.pSwapchains[i])) {
const uint32_t image_index = present_info.pImageIndices[i];
if (image_index >= swapchain_state->images.size()) {
continue; // invalid image index, reported elsewhere
}
skip |= validator_.ProcessPresentBatch(*swapchain_state->images[image_index].image_state, present_info_loc);
}
}
return skip;
}
bool SubmitTimeTracker::ProcessBatch(std::vector<std::shared_ptr<CommandBuffer>>&& command_buffers,
vvl::span<const VkSemaphoreSubmitInfo> wait_semaphores,
vvl::span<const VkSemaphoreSubmitInfo> signal_semaphores, VkQueue queue,
const Location& submit_loc) {
bool skip = false;
std::vector<UnresolvedBatch>& unresolved_batches = unresolved_batches_[queue];
std::vector<VkSemaphoreSubmitInfo> unresolved_timeline_waits = GetUnresolvedTimelineWaits(wait_semaphores);
// Add wait-before-signal batches to unresolved list and return
const bool has_pending_waits = !unresolved_batches.empty() || !unresolved_timeline_waits.empty();
if (has_pending_waits) {
UnresolvedBatch batch(submit_loc);
batch.command_buffers = std::move(command_buffers);
batch.unresolved_timeline_waits = std::move(unresolved_timeline_waits);
batch.signals.assign(signal_semaphores.begin(), signal_semaphores.end());
unresolved_batches.emplace_back(std::move(batch));
return skip;
}
skip |= validator_.ProcessSubmissionBatch(*this, command_buffers, signal_semaphores, submit_loc);
const bool new_timeline_signals = RegisterTimelineSignals(signal_semaphores);
if (new_timeline_signals) {
skip |= PropagateTimelineSignals();
}
return skip;
}
bool SubmitTimeTracker::ProcessSignal(VkSemaphore timeline, uint64_t signal_value) {
bool skip = false;
const bool new_timeline_signal = UpdateTimelineValue(timeline, signal_value);
if (new_timeline_signal) {
skip |= PropagateTimelineSignals();
}
return skip;
}
std::vector<VkSemaphoreSubmitInfo> SubmitTimeTracker::GetUnresolvedTimelineWaits(
vvl::span<const VkSemaphoreSubmitInfo> wait_semaphores) {
std::vector<VkSemaphoreSubmitInfo> unresolved;
for (const auto& wait : wait_semaphores) {
const std::optional<uint64_t> current_value = GetTimelineValue(wait.semaphore);
if (!current_value.has_value()) {
// Invalid or external semaphores should not block this batch
continue;
}
if (wait.value > *current_value) {
unresolved.emplace_back(wait);
}
}
return unresolved;
}
bool SubmitTimeTracker::RegisterTimelineSignals(vvl::span<const VkSemaphoreSubmitInfo> signal_semaphores) {
bool new_timeline_signals = false;
for (const VkSemaphoreSubmitInfo& signal : signal_semaphores) {
new_timeline_signals |= UpdateTimelineValue(signal.semaphore, signal.value);
}
return new_timeline_signals;
}
bool SubmitTimeTracker::PropagateTimelineSignals() {
bool skip = false;
// The caller ensures we just registered new timeline signals
bool new_timeline_signals = true;
// Each iteration attempts to resolve pending batches using current timeline value.
// If a resolved batch generates new timeline signals, the loop runs again
while (new_timeline_signals) {
new_timeline_signals = false;
for (auto& [queue, batches] : unresolved_batches_) {
while (!batches.empty()) {
UnresolvedBatch& batch = batches.front();
if (!CanBeResolved(batch)) {
break;
}
const auto signals = vvl::span<const VkSemaphoreSubmitInfo>(batch.signals.data(), batch.signals.size());
skip |= validator_.ProcessSubmissionBatch(*this, batch.command_buffers, signals, batch.submit_loc_capture.Get());
new_timeline_signals |= RegisterTimelineSignals(batch.signals);
batches.erase(batches.begin());
}
}
}
return skip;
}
bool SubmitTimeTracker::CanBeResolved(const UnresolvedBatch& batch) const {
for (const VkSemaphoreSubmitInfo& wait : batch.unresolved_timeline_waits) {
const std::optional<uint64_t> current_value = GetTimelineValue(wait.semaphore);
if (!current_value.has_value()) {
// Invalid or external semaphores should not block this batch
continue;
}
if (wait.value > current_value) {
return false;
}
}
return true;
}
std::optional<uint64_t> SubmitTimeTracker::GetTimelineValue(VkSemaphore timeline) const {
auto semaphore_state = validator_.Get<Semaphore>(timeline);
if (!semaphore_state || semaphore_state->type != VK_SEMAPHORE_TYPE_TIMELINE ||
semaphore_state->Scope() != Semaphore::kInternal) {
// Used by the caller to detect invalid/non-timeline/external semaphores
return {};
}
const uint64_t current_value = vvl::FindExisting(timeline_signals_, timeline);
return current_value;
}
bool SubmitTimeTracker::UpdateTimelineValue(VkSemaphore timeline, uint64_t signal_value) {
auto semaphore_state = validator_.Get<Semaphore>(timeline);
if (!semaphore_state || semaphore_state->type != VK_SEMAPHORE_TYPE_TIMELINE) {
return false;
}
uint64_t& current_value = vvl::FindExisting(timeline_signals_, timeline);
if (signal_value <= current_value) {
return false; // non-increasing signal, the error should be reported elsewhere
}
current_value = signal_value;
return true;
}
} // namespace vvl