blob: 4778ac05c05955a4e732508b7ae46ee150e7db2e [file] [log] [blame]
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001/* Copyright (c) 2015-2021 The Khronos Group Inc.
2 * Copyright (c) 2015-2021 Valve Corporation
3 * Copyright (c) 2015-2021 LunarG, Inc.
4 * Copyright (C) 2015-2021 Google Inc.
5 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Author: Courtney Goeltzenleuchter <courtneygo@google.com>
20 * Author: Tobin Ehlis <tobine@google.com>
21 * Author: Chris Forbes <chrisf@ijw.co.nz>
22 * Author: Mark Lobodzinski <mark@lunarg.com>
23 * Author: Dave Houlton <daveh@lunarg.com>
24 * Author: John Zulauf <jzulauf@lunarg.com>
25 * Author: Tobias Hector <tobias.hector@amd.com>
26 */
27#include "cmd_buffer_state.h"
28#include "render_pass_state.h"
Jeremy Gebben1ec89332021-08-05 13:51:49 -060029#include "state_tracker.h"
30#include "image_state.h"
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060031
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060032COMMAND_POOL_STATE::COMMAND_POOL_STATE(ValidationStateTracker *dev, VkCommandPool cp, const VkCommandPoolCreateInfo *pCreateInfo,
33 VkQueueFlags flags)
34 : BASE_NODE(cp, kVulkanObjectTypeCommandPool),
35 dev_data(dev),
36 createFlags(pCreateInfo->flags),
37 queueFamilyIndex(pCreateInfo->queueFamilyIndex),
38 queue_flags(flags),
39 unprotected((pCreateInfo->flags & VK_COMMAND_POOL_CREATE_PROTECTED_BIT) == 0) {}
40
41void COMMAND_POOL_STATE::Allocate(const VkCommandBufferAllocateInfo *create_info, const VkCommandBuffer *command_buffers) {
42 for (uint32_t i = 0; i < create_info->commandBufferCount; i++) {
43 auto new_cb = dev_data->CreateCmdBufferState(command_buffers[i], create_info, this);
44 commandBuffers.emplace(command_buffers[i], new_cb.get());
Jeremy Gebben082a9832021-10-28 13:40:11 -060045 dev_data->Add(std::move(new_cb));
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060046 }
47}
48
49void COMMAND_POOL_STATE::Free(uint32_t count, const VkCommandBuffer *command_buffers) {
50 for (uint32_t i = 0; i < count; i++) {
51 auto iter = commandBuffers.find(command_buffers[i]);
52 if (iter != commandBuffers.end()) {
Jeremy Gebben082a9832021-10-28 13:40:11 -060053 dev_data->Destroy<CMD_BUFFER_STATE>(iter->first);
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060054 commandBuffers.erase(iter);
55 }
56 }
57}
58
59void COMMAND_POOL_STATE::Reset() {
60 for (auto &entry : commandBuffers) {
61 entry.second->Reset();
62 }
63}
64
65void COMMAND_POOL_STATE::Destroy() {
66 for (auto &entry : commandBuffers) {
Jeremy Gebben082a9832021-10-28 13:40:11 -060067 dev_data->Destroy<CMD_BUFFER_STATE>(entry.first);
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060068 }
69 commandBuffers.clear();
70 BASE_NODE::Destroy();
71}
72
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060073const char *CommandTypeString(CMD_TYPE type) {
74 // Autogenerated as part of the command_validation.h codegen
75 return kGeneratedCommandNameList[type];
76}
77
78VkDynamicState ConvertToDynamicState(CBStatusFlagBits flag) {
79 switch (flag) {
80 case CBSTATUS_LINE_WIDTH_SET:
81 return VK_DYNAMIC_STATE_LINE_WIDTH;
82 case CBSTATUS_DEPTH_BIAS_SET:
83 return VK_DYNAMIC_STATE_DEPTH_BIAS;
84 case CBSTATUS_BLEND_CONSTANTS_SET:
85 return VK_DYNAMIC_STATE_BLEND_CONSTANTS;
86 case CBSTATUS_DEPTH_BOUNDS_SET:
87 return VK_DYNAMIC_STATE_DEPTH_BOUNDS;
88 case CBSTATUS_STENCIL_READ_MASK_SET:
89 return VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK;
90 case CBSTATUS_STENCIL_WRITE_MASK_SET:
91 return VK_DYNAMIC_STATE_STENCIL_WRITE_MASK;
92 case CBSTATUS_STENCIL_REFERENCE_SET:
93 return VK_DYNAMIC_STATE_STENCIL_REFERENCE;
94 case CBSTATUS_VIEWPORT_SET:
95 return VK_DYNAMIC_STATE_VIEWPORT;
96 case CBSTATUS_SCISSOR_SET:
97 return VK_DYNAMIC_STATE_SCISSOR;
98 case CBSTATUS_EXCLUSIVE_SCISSOR_SET:
99 return VK_DYNAMIC_STATE_EXCLUSIVE_SCISSOR_NV;
100 case CBSTATUS_SHADING_RATE_PALETTE_SET:
101 return VK_DYNAMIC_STATE_VIEWPORT_SHADING_RATE_PALETTE_NV;
102 case CBSTATUS_LINE_STIPPLE_SET:
103 return VK_DYNAMIC_STATE_LINE_STIPPLE_EXT;
104 case CBSTATUS_VIEWPORT_W_SCALING_SET:
105 return VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV;
106 case CBSTATUS_CULL_MODE_SET:
107 return VK_DYNAMIC_STATE_CULL_MODE_EXT;
108 case CBSTATUS_FRONT_FACE_SET:
109 return VK_DYNAMIC_STATE_FRONT_FACE_EXT;
110 case CBSTATUS_PRIMITIVE_TOPOLOGY_SET:
111 return VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT;
112 case CBSTATUS_VIEWPORT_WITH_COUNT_SET:
113 return VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT;
114 case CBSTATUS_SCISSOR_WITH_COUNT_SET:
115 return VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT;
116 case CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET:
117 return VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT;
118 case CBSTATUS_DEPTH_TEST_ENABLE_SET:
119 return VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT;
120 case CBSTATUS_DEPTH_WRITE_ENABLE_SET:
121 return VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT;
122 case CBSTATUS_DEPTH_COMPARE_OP_SET:
123 return VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT;
124 case CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET:
125 return VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT;
126 case CBSTATUS_STENCIL_TEST_ENABLE_SET:
127 return VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT;
128 case CBSTATUS_STENCIL_OP_SET:
129 return VK_DYNAMIC_STATE_STENCIL_OP_EXT;
130 case CBSTATUS_DISCARD_RECTANGLE_SET:
131 return VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT;
132 case CBSTATUS_SAMPLE_LOCATIONS_SET:
133 return VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT;
134 case CBSTATUS_COARSE_SAMPLE_ORDER_SET:
135 return VK_DYNAMIC_STATE_VIEWPORT_COARSE_SAMPLE_ORDER_NV;
136 case CBSTATUS_PATCH_CONTROL_POINTS_SET:
137 return VK_DYNAMIC_STATE_PATCH_CONTROL_POINTS_EXT;
138 case CBSTATUS_RASTERIZER_DISCARD_ENABLE_SET:
139 return VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT;
140 case CBSTATUS_DEPTH_BIAS_ENABLE_SET:
141 return VK_DYNAMIC_STATE_DEPTH_BIAS_ENABLE_EXT;
142 case CBSTATUS_LOGIC_OP_SET:
143 return VK_DYNAMIC_STATE_LOGIC_OP_EXT;
144 case CBSTATUS_PRIMITIVE_RESTART_ENABLE_SET:
145 return VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE_EXT;
146 case CBSTATUS_VERTEX_INPUT_SET:
147 return VK_DYNAMIC_STATE_VERTEX_INPUT_EXT;
148 default:
149 // CBSTATUS_INDEX_BUFFER_BOUND is not in VkDynamicState
150 return VK_DYNAMIC_STATE_MAX_ENUM;
151 }
152 return VK_DYNAMIC_STATE_MAX_ENUM;
153}
154
155CBStatusFlagBits ConvertToCBStatusFlagBits(VkDynamicState state) {
156 switch (state) {
157 case VK_DYNAMIC_STATE_VIEWPORT:
158 return CBSTATUS_VIEWPORT_SET;
159 case VK_DYNAMIC_STATE_SCISSOR:
160 return CBSTATUS_SCISSOR_SET;
161 case VK_DYNAMIC_STATE_LINE_WIDTH:
162 return CBSTATUS_LINE_WIDTH_SET;
163 case VK_DYNAMIC_STATE_DEPTH_BIAS:
164 return CBSTATUS_DEPTH_BIAS_SET;
165 case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
166 return CBSTATUS_BLEND_CONSTANTS_SET;
167 case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
168 return CBSTATUS_DEPTH_BOUNDS_SET;
169 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
170 return CBSTATUS_STENCIL_READ_MASK_SET;
171 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
172 return CBSTATUS_STENCIL_WRITE_MASK_SET;
173 case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
174 return CBSTATUS_STENCIL_REFERENCE_SET;
175 case VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV:
176 return CBSTATUS_VIEWPORT_W_SCALING_SET;
177 case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT:
178 return CBSTATUS_DISCARD_RECTANGLE_SET;
179 case VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT:
180 return CBSTATUS_SAMPLE_LOCATIONS_SET;
181 case VK_DYNAMIC_STATE_VIEWPORT_SHADING_RATE_PALETTE_NV:
182 return CBSTATUS_SHADING_RATE_PALETTE_SET;
183 case VK_DYNAMIC_STATE_VIEWPORT_COARSE_SAMPLE_ORDER_NV:
184 return CBSTATUS_COARSE_SAMPLE_ORDER_SET;
185 case VK_DYNAMIC_STATE_EXCLUSIVE_SCISSOR_NV:
186 return CBSTATUS_EXCLUSIVE_SCISSOR_SET;
187 case VK_DYNAMIC_STATE_LINE_STIPPLE_EXT:
188 return CBSTATUS_LINE_STIPPLE_SET;
189 case VK_DYNAMIC_STATE_CULL_MODE_EXT:
190 return CBSTATUS_CULL_MODE_SET;
191 case VK_DYNAMIC_STATE_FRONT_FACE_EXT:
192 return CBSTATUS_FRONT_FACE_SET;
193 case VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT:
194 return CBSTATUS_PRIMITIVE_TOPOLOGY_SET;
195 case VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT:
196 return CBSTATUS_VIEWPORT_WITH_COUNT_SET;
197 case VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT:
198 return CBSTATUS_SCISSOR_WITH_COUNT_SET;
199 case VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT:
200 return CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET;
201 case VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT:
202 return CBSTATUS_DEPTH_TEST_ENABLE_SET;
203 case VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT:
204 return CBSTATUS_DEPTH_WRITE_ENABLE_SET;
205 case VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT:
206 return CBSTATUS_DEPTH_COMPARE_OP_SET;
207 case VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT:
208 return CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET;
209 case VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT:
210 return CBSTATUS_STENCIL_TEST_ENABLE_SET;
211 case VK_DYNAMIC_STATE_STENCIL_OP_EXT:
212 return CBSTATUS_STENCIL_OP_SET;
213 case VK_DYNAMIC_STATE_PATCH_CONTROL_POINTS_EXT:
214 return CBSTATUS_PATCH_CONTROL_POINTS_SET;
215 case VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT:
216 return CBSTATUS_RASTERIZER_DISCARD_ENABLE_SET;
217 case VK_DYNAMIC_STATE_DEPTH_BIAS_ENABLE_EXT:
218 return CBSTATUS_DEPTH_BIAS_ENABLE_SET;
219 case VK_DYNAMIC_STATE_LOGIC_OP_EXT:
220 return CBSTATUS_LOGIC_OP_SET;
221 case VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE_EXT:
222 return CBSTATUS_PRIMITIVE_RESTART_ENABLE_SET;
223 case VK_DYNAMIC_STATE_VERTEX_INPUT_EXT:
224 return CBSTATUS_VERTEX_INPUT_SET;
225 default:
226 return CBSTATUS_NONE;
227 }
228 return CBSTATUS_NONE;
229}
230
Jeremy Gebbencd7fa282021-10-27 10:25:32 -0600231CMD_BUFFER_STATE::CMD_BUFFER_STATE(ValidationStateTracker *dev, VkCommandBuffer cb, const VkCommandBufferAllocateInfo *pCreateInfo,
232 const COMMAND_POOL_STATE *pool)
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600233 : REFCOUNTED_NODE(cb, kVulkanObjectTypeCommandBuffer),
234 createInfo(*pCreateInfo),
235 command_pool(pool),
Jeremy Gebbencd7fa282021-10-27 10:25:32 -0600236 dev_data(dev),
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600237 unprotected(pool->unprotected) {
238 Reset();
239}
240
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600241// Get the image viewstate for a given framebuffer attachment
242IMAGE_VIEW_STATE *CMD_BUFFER_STATE::GetActiveAttachmentImageViewState(uint32_t index) {
243 assert(active_attachments && index != VK_ATTACHMENT_UNUSED && (index < active_attachments->size()));
244 return active_attachments->at(index);
245}
246
247// Get the image viewstate for a given framebuffer attachment
248const IMAGE_VIEW_STATE *CMD_BUFFER_STATE::GetActiveAttachmentImageViewState(uint32_t index) const {
ziga-lunarg452a5f92021-09-08 15:33:03 +0200249 if (!active_attachments || index == VK_ATTACHMENT_UNUSED || (index >= active_attachments->size())) {
250 return nullptr;
251 }
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600252 return active_attachments->at(index);
253}
254
255void CMD_BUFFER_STATE::AddChild(BASE_NODE *child_node) {
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600256 assert(child_node);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600257 if (child_node->AddParent(this)) {
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600258 object_bindings.insert(child_node);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600259 }
260}
261
262void CMD_BUFFER_STATE::RemoveChild(BASE_NODE *child_node) {
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600263 assert(child_node);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600264 child_node->RemoveParent(this);
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600265 object_bindings.erase(child_node);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600266}
267
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600268// Reset the command buffer state
269// Maintain the createInfo and set state to CB_NEW, but clear all other state
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600270void CMD_BUFFER_STATE::Reset() {
271 ResetUse();
272 // Reset CB state (note that createInfo is not cleared)
273 memset(&beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
274 memset(&inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo));
275 hasDrawCmd = false;
276 hasTraceRaysCmd = false;
277 hasBuildAccelerationStructureCmd = false;
278 hasDispatchCmd = false;
279 state = CB_NEW;
280 commandCount = 0;
281 submitCount = 0;
282 image_layout_change_count = 1; // Start at 1. 0 is insert value for validation cache versions, s.t. new == dirty
283 status = 0;
284 static_status = 0;
285 inheritedViewportDepths.clear();
286 usedViewportScissorCount = 0;
287 pipelineStaticViewportCount = 0;
288 pipelineStaticScissorCount = 0;
289 viewportMask = 0;
290 viewportWithCountMask = 0;
291 viewportWithCountCount = 0;
292 scissorMask = 0;
293 scissorWithCountMask = 0;
294 scissorWithCountCount = 0;
295 trashedViewportMask = 0;
296 trashedScissorMask = 0;
297 trashedViewportCount = false;
298 trashedScissorCount = false;
299 usedDynamicViewportCount = false;
300 usedDynamicScissorCount = false;
301 primitiveTopology = VK_PRIMITIVE_TOPOLOGY_MAX_ENUM;
302
303 activeRenderPassBeginInfo = safe_VkRenderPassBeginInfo();
304 activeRenderPass = nullptr;
305 active_attachments = nullptr;
306 active_subpasses = nullptr;
307 attachments_view_states.clear();
308 activeSubpassContents = VK_SUBPASS_CONTENTS_INLINE;
309 activeSubpass = 0;
310 broken_bindings.clear();
311 waitedEvents.clear();
312 events.clear();
313 writeEventsBeforeWait.clear();
314 activeQueries.clear();
315 startedQueries.clear();
316 image_layout_map.clear();
317 current_vertex_buffer_binding_info.vertex_buffer_bindings.clear();
318 vertex_buffer_used = false;
319 primaryCommandBuffer = VK_NULL_HANDLE;
320
321 linkedCommandBuffers.clear();
322 // Remove reverse command buffer links.
323 Invalidate(true);
324
325 queue_submit_functions.clear();
Hans-Kristian Arntzen59c2c3f2021-06-14 11:40:12 +0200326 queue_submit_functions_after_render_pass.clear();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600327 cmd_execute_commands_functions.clear();
328 eventUpdates.clear();
329 queryUpdates.clear();
330
331 // Remove object bindings
332 for (const auto &obj : object_bindings) {
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600333 obj->RemoveParent(this);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600334 }
335 object_bindings.clear();
336
337 for (auto &item : lastBound) {
338 item.Reset();
339 }
340 // Remove this cmdBuffer's reference from each FrameBuffer's CB ref list
341 for (auto &framebuffer : framebuffers) {
342 framebuffer->RemoveParent(this);
343 }
344 framebuffers.clear();
345 activeFramebuffer = VK_NULL_HANDLE;
346 index_buffer_binding.reset();
347
348 qfo_transfer_image_barriers.Reset();
349 qfo_transfer_buffer_barriers.Reset();
350
351 // Clean up the label data
352 debug_label.Reset();
353 validate_descriptorsets_in_queuesubmit.clear();
354
355 // Best practices info
356 small_indexed_draw_call_count = 0;
357
358 transform_feedback_active = false;
359
360 // Remove object bindings
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600361 for (auto *base_obj : object_bindings) {
362 RemoveChild(base_obj);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600363 }
364 object_bindings.clear();
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600365
366 // Clean up the label data
367 ResetCmdDebugUtilsLabel(dev_data->report_data, commandBuffer());
368
369 if (dev_data->command_buffer_reset_callback) {
370 (*dev_data->command_buffer_reset_callback)(commandBuffer());
371 }
372}
373
374// Track which resources are in-flight by atomically incrementing their "in_use" count
375void CMD_BUFFER_STATE::IncrementResources() {
376 submitCount++;
377
378 // TODO : We should be able to remove the NULL look-up checks from the code below as long as
379 // all the corresponding cases are verified to cause CB_INVALID state and the CB_INVALID state
380 // should then be flagged prior to calling this function
381 for (auto event : writeEventsBeforeWait) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600382 auto event_state = dev_data->Get<EVENT_STATE>(event);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600383 if (event_state) event_state->write_in_use++;
384 }
385}
386
387// Discussed in details in https://github.com/KhronosGroup/Vulkan-Docs/issues/1081
388// Internal discussion and CTS were written to prove that this is not called after an incompatible vkCmdBindPipeline
389// "Binding a pipeline with a layout that is not compatible with the push constant layout does not disturb the push constant values"
390//
391// vkCmdBindDescriptorSet has nothing to do with push constants and don't need to call this after neither
392//
393// Part of this assumes apps at draw/dispath/traceRays/etc time will have it properly compatabile or else other VU will be triggered
394void CMD_BUFFER_STATE::ResetPushConstantDataIfIncompatible(const PIPELINE_LAYOUT_STATE *pipeline_layout_state) {
395 if (pipeline_layout_state == nullptr) {
396 return;
397 }
398 if (push_constant_data_ranges == pipeline_layout_state->push_constant_ranges) {
399 return;
400 }
401
402 push_constant_data_ranges = pipeline_layout_state->push_constant_ranges;
403 push_constant_data.clear();
404 push_constant_data_update.clear();
405 uint32_t size_needed = 0;
406 for (const auto &push_constant_range : *push_constant_data_ranges) {
407 auto size = push_constant_range.offset + push_constant_range.size;
408 size_needed = std::max(size_needed, size);
409
410 auto stage_flags = push_constant_range.stageFlags;
411 uint32_t bit_shift = 0;
412 while (stage_flags) {
413 if (stage_flags & 1) {
414 VkShaderStageFlagBits flag = static_cast<VkShaderStageFlagBits>(1 << bit_shift);
415 const auto it = push_constant_data_update.find(flag);
416
417 if (it != push_constant_data_update.end()) {
418 if (it->second.size() < push_constant_range.offset) {
419 it->second.resize(push_constant_range.offset, PC_Byte_Not_Set);
420 }
421 if (it->second.size() < size) {
422 it->second.resize(size, PC_Byte_Not_Updated);
423 }
424 } else {
425 std::vector<uint8_t> bytes;
426 bytes.resize(push_constant_range.offset, PC_Byte_Not_Set);
427 bytes.resize(size, PC_Byte_Not_Updated);
428 push_constant_data_update[flag] = bytes;
429 }
430 }
431 stage_flags = stage_flags >> 1;
432 ++bit_shift;
433 }
434 }
435 push_constant_data.resize(size_needed, 0);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600436}
437
438void CMD_BUFFER_STATE::Destroy() {
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600439 // Allow any derived class to clean up command buffer state
440 if (dev_data->command_buffer_reset_callback) {
441 (*dev_data->command_buffer_reset_callback)(commandBuffer());
442 }
443 if (dev_data->command_buffer_free_callback) {
444 (*dev_data->command_buffer_free_callback)(commandBuffer());
445 }
446
447 // Remove the cb debug labels
448 EraseCmdDebugUtilsLabel(dev_data->report_data, commandBuffer());
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600449 Reset();
450 BASE_NODE::Destroy();
451}
452
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600453void CMD_BUFFER_STATE::NotifyInvalidate(const BASE_NODE::NodeList &invalid_nodes, bool unlink) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600454 if (state == CB_RECORDING) {
455 state = CB_INVALID_INCOMPLETE;
456 } else if (state == CB_RECORDED) {
457 state = CB_INVALID_COMPLETE;
458 }
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600459 assert(!invalid_nodes.empty());
460 LogObjectList log_list;
461 for (auto *obj : invalid_nodes) {
462 log_list.object_list.emplace_back(obj->Handle());
463 }
464 broken_bindings.emplace(invalid_nodes[0]->Handle(), log_list);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600465
466 if (unlink) {
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600467 for (auto *obj : invalid_nodes) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600468 object_bindings.erase(obj);
Jeremy Gebben6335df62021-11-01 10:50:13 -0600469 switch (obj->Type()) {
470 case kVulkanObjectTypeCommandBuffer:
471 linkedCommandBuffers.erase(static_cast<CMD_BUFFER_STATE *>(obj));
472 break;
473 case kVulkanObjectTypeImage:
474 image_layout_map.erase(static_cast<IMAGE_STATE *>(obj));
475 break;
476 default:
477 break;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600478 }
479 }
480 }
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600481 BASE_NODE::NotifyInvalidate(invalid_nodes, unlink);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600482}
483
ziga-lunarg189ae5d2021-10-19 13:09:58 +0200484const CommandBufferImageLayoutMap& CMD_BUFFER_STATE::GetImageSubresourceLayoutMap() const { return image_layout_map; }
485
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600486// The const variant only need the image as it is the key for the map
Jeremy Gebben6335df62021-11-01 10:50:13 -0600487const ImageSubresourceLayoutMap *CMD_BUFFER_STATE::GetImageSubresourceLayoutMap(const IMAGE_STATE &image_state) const {
488 auto it = image_layout_map.find(&image_state);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600489 if (it == image_layout_map.cend()) {
490 return nullptr;
491 }
492 return &it->second;
493}
494
495// The non-const variant only needs the image state, as the factory requires it to construct a new entry
496ImageSubresourceLayoutMap *CMD_BUFFER_STATE::GetImageSubresourceLayoutMap(const IMAGE_STATE &image_state) {
Jeremy Gebben6335df62021-11-01 10:50:13 -0600497 auto &layout_map = image_layout_map[&image_state];
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600498 if (!layout_map) {
499 // Was an empty slot... fill it in.
500 layout_map.emplace(image_state);
501 }
502 return &layout_map;
503}
504
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600505static bool SetQueryState(QueryObject object, QueryState value, QueryMap *localQueryToStateMap) {
506 (*localQueryToStateMap)[object] = value;
507 return false;
508}
509
510void CMD_BUFFER_STATE::BeginQuery(const QueryObject &query_obj) {
511 activeQueries.insert(query_obj);
512 startedQueries.insert(query_obj);
513 queryUpdates.emplace_back([query_obj](const ValidationStateTracker *device_data, bool do_validate,
514 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass, QueryMap *localQueryToStateMap) {
515 SetQueryState(QueryObject(query_obj, perfQueryPass), QUERYSTATE_RUNNING, localQueryToStateMap);
516 return false;
517 });
518}
519
520void CMD_BUFFER_STATE::EndQuery(const QueryObject &query_obj) {
521 activeQueries.erase(query_obj);
522 queryUpdates.emplace_back([query_obj](const ValidationStateTracker *device_data, bool do_validate,
523 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass, QueryMap *localQueryToStateMap) {
524 return SetQueryState(QueryObject(query_obj, perfQueryPass), QUERYSTATE_ENDED, localQueryToStateMap);
525 });
526}
527
528static bool SetQueryStateMulti(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount, uint32_t perfPass, QueryState value,
529 QueryMap *localQueryToStateMap) {
530 for (uint32_t i = 0; i < queryCount; i++) {
531 QueryObject object = QueryObject(QueryObject(queryPool, firstQuery + i), perfPass);
532 (*localQueryToStateMap)[object] = value;
533 }
534 return false;
535}
536
537void CMD_BUFFER_STATE::EndQueries(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount) {
538 for (uint32_t slot = firstQuery; slot < (firstQuery + queryCount); slot++) {
539 QueryObject query = {queryPool, slot};
540 activeQueries.erase(query);
541 }
542 queryUpdates.emplace_back([queryPool, firstQuery, queryCount](const ValidationStateTracker *device_data, bool do_validate,
543 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass,
544 QueryMap *localQueryToStateMap) {
Lars-Ivar Hesselberg Simonsen1a5646f2021-10-25 15:06:16 +0200545 return SetQueryStateMulti(queryPool, firstQuery, queryCount, perfQueryPass, QUERYSTATE_ENDED, localQueryToStateMap);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600546 });
547}
548
549void CMD_BUFFER_STATE::ResetQueryPool(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount) {
550 for (uint32_t slot = firstQuery; slot < (firstQuery + queryCount); slot++) {
551 QueryObject query = {queryPool, slot};
552 resetQueries.insert(query);
553 }
554
555 queryUpdates.emplace_back([queryPool, firstQuery, queryCount](const ValidationStateTracker *device_data, bool do_validate,
556 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass,
557 QueryMap *localQueryToStateMap) {
558 return SetQueryStateMulti(queryPool, firstQuery, queryCount, perfQueryPass, QUERYSTATE_RESET, localQueryToStateMap);
559 });
560}
561
562void UpdateSubpassAttachments(const safe_VkSubpassDescription2 &subpass, std::vector<SUBPASS_INFO> &subpasses) {
563 for (uint32_t index = 0; index < subpass.inputAttachmentCount; ++index) {
564 const uint32_t attachment_index = subpass.pInputAttachments[index].attachment;
565 if (attachment_index != VK_ATTACHMENT_UNUSED) {
566 subpasses[attachment_index].used = true;
567 subpasses[attachment_index].usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
568 subpasses[attachment_index].layout = subpass.pInputAttachments[index].layout;
569 }
570 }
571
572 for (uint32_t index = 0; index < subpass.colorAttachmentCount; ++index) {
573 const uint32_t attachment_index = subpass.pColorAttachments[index].attachment;
574 if (attachment_index != VK_ATTACHMENT_UNUSED) {
575 subpasses[attachment_index].used = true;
576 subpasses[attachment_index].usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
577 subpasses[attachment_index].layout = subpass.pColorAttachments[index].layout;
578 }
579 if (subpass.pResolveAttachments) {
580 const uint32_t attachment_index2 = subpass.pResolveAttachments[index].attachment;
581 if (attachment_index2 != VK_ATTACHMENT_UNUSED) {
582 subpasses[attachment_index2].used = true;
583 subpasses[attachment_index2].usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
584 subpasses[attachment_index2].layout = subpass.pResolveAttachments[index].layout;
585 }
586 }
587 }
588
589 if (subpass.pDepthStencilAttachment) {
590 const uint32_t attachment_index = subpass.pDepthStencilAttachment->attachment;
591 if (attachment_index != VK_ATTACHMENT_UNUSED) {
592 subpasses[attachment_index].used = true;
593 subpasses[attachment_index].usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
594 subpasses[attachment_index].layout = subpass.pDepthStencilAttachment->layout;
595 }
596 }
597}
598
599void CMD_BUFFER_STATE::UpdateAttachmentsView(const VkRenderPassBeginInfo *pRenderPassBegin) {
600 auto &attachments = *(active_attachments.get());
601 const bool imageless = (activeFramebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) ? true : false;
602 const VkRenderPassAttachmentBeginInfo *attachment_info_struct = nullptr;
603 if (pRenderPassBegin) attachment_info_struct = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
604
605 for (uint32_t i = 0; i < attachments.size(); ++i) {
606 if (imageless) {
607 if (attachment_info_struct && i < attachment_info_struct->attachmentCount) {
608 auto res =
609 attachments_view_states.insert(dev_data->GetShared<IMAGE_VIEW_STATE>(attachment_info_struct->pAttachments[i]));
610 attachments[i] = res.first->get();
611 }
612 } else {
613 auto res = attachments_view_states.insert(activeFramebuffer->attachments_view_state[i]);
614 attachments[i] = res.first->get();
615 }
616 }
617}
618
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600619void CMD_BUFFER_STATE::BeginRenderPass(CMD_TYPE cmd_type, const VkRenderPassBeginInfo *pRenderPassBegin,
620 const VkSubpassContents contents) {
621 RecordCmd(cmd_type);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600622 activeFramebuffer = dev_data->GetShared<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
623 activeRenderPass = dev_data->GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
624 activeRenderPassBeginInfo = safe_VkRenderPassBeginInfo(pRenderPassBegin);
625 activeSubpass = 0;
626 activeSubpassContents = contents;
627
628 // Connect this RP to cmdBuffer
629 if (!dev_data->disabled[command_buffer_state] && activeRenderPass) {
630 AddChild(activeRenderPass.get());
631 }
632
633 auto chained_device_group_struct = LvlFindInChain<VkDeviceGroupRenderPassBeginInfo>(pRenderPassBegin->pNext);
634 if (chained_device_group_struct) {
635 active_render_pass_device_mask = chained_device_group_struct->deviceMask;
636 } else {
637 active_render_pass_device_mask = initial_device_mask;
638 }
639
640 active_subpasses = nullptr;
641 active_attachments = nullptr;
642
643 if (activeFramebuffer) {
644 framebuffers.insert(activeFramebuffer);
645
646 // Set cb_state->active_subpasses
647 active_subpasses = std::make_shared<std::vector<SUBPASS_INFO>>(activeFramebuffer->createInfo.attachmentCount);
648 const auto &subpass = activeRenderPass->createInfo.pSubpasses[activeSubpass];
649 UpdateSubpassAttachments(subpass, *active_subpasses);
650
651 // Set cb_state->active_attachments & cb_state->attachments_view_states
652 active_attachments = std::make_shared<std::vector<IMAGE_VIEW_STATE *>>(activeFramebuffer->createInfo.attachmentCount);
653 UpdateAttachmentsView(pRenderPassBegin);
654
655 // Connect this framebuffer and its children to this cmdBuffer
656 AddChild(activeFramebuffer.get());
657 }
658}
659
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600660void CMD_BUFFER_STATE::NextSubpass(CMD_TYPE cmd_type, VkSubpassContents contents) {
661 RecordCmd(cmd_type);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600662 activeSubpass++;
663 activeSubpassContents = contents;
664
665 // Update cb_state->active_subpasses
666 if (activeRenderPass && activeFramebuffer) {
667 active_subpasses = nullptr;
668 active_subpasses = std::make_shared<std::vector<SUBPASS_INFO>>(activeFramebuffer->createInfo.attachmentCount);
669
670 const auto &subpass = activeRenderPass->createInfo.pSubpasses[activeSubpass];
671 UpdateSubpassAttachments(subpass, *active_subpasses);
672 }
673}
674
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600675void CMD_BUFFER_STATE::EndRenderPass(CMD_TYPE cmd_type) {
676 RecordCmd(cmd_type);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600677 activeRenderPass = nullptr;
678 active_attachments = nullptr;
679 active_subpasses = nullptr;
680 activeSubpass = 0;
681 activeFramebuffer = VK_NULL_HANDLE;
682}
683
amhagana448ea52021-11-02 14:09:14 -0400684void CMD_BUFFER_STATE::BeginRendering(CMD_TYPE cmd_type, const VkRenderingInfoKHR *pRenderingInfo) {
685 RecordCmd(cmd_type);
686 activeRenderPass = std::make_shared<RENDER_PASS_STATE>(pRenderingInfo);
687
688 auto chained_device_group_struct = LvlFindInChain<VkDeviceGroupRenderPassBeginInfo>(pRenderingInfo->pNext);
689 if (chained_device_group_struct) {
690 active_render_pass_device_mask = chained_device_group_struct->deviceMask;
691 } else {
692 active_render_pass_device_mask = initial_device_mask;
693 }
694
695 activeSubpassContents = ((pRenderingInfo->flags & VK_RENDERING_CONTENTS_SECONDARY_COMMAND_BUFFERS_BIT_KHR) ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS : VK_SUBPASS_CONTENTS_INLINE);
696
697 active_attachments = nullptr;
698 uint32_t attachment_count = (pRenderingInfo->colorAttachmentCount + 2) * 2;
699
700 // Set cb_state->active_attachments & cb_state->attachments_view_states
701 active_attachments = std::make_shared<std::vector<IMAGE_VIEW_STATE *>>(attachment_count);
702 auto &attachments = *(active_attachments.get());
703
704 for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; ++i) {
705 auto& colorAttachment = attachments[GetDynamicColorAttachmentImageIndex(i)];
706 auto& colorResolveAttachment = attachments[GetDynamicColorResolveAttachmentImageIndex(i)];
707 colorAttachment = nullptr;
708 colorResolveAttachment = nullptr;
709
710 if (pRenderingInfo->pColorAttachments[i].imageView != VK_NULL_HANDLE) {
711 auto res = attachments_view_states.insert(
712 dev_data->GetShared<IMAGE_VIEW_STATE>(pRenderingInfo->pColorAttachments[i].imageView));
713 colorAttachment = res.first->get();
714 if (pRenderingInfo->pColorAttachments[i].resolveMode != VK_RESOLVE_MODE_NONE &&
715 pRenderingInfo->pColorAttachments[i].resolveImageView != VK_NULL_HANDLE) {
716 colorResolveAttachment = res.first->get();
717 }
718 }
719 }
720
721 if (pRenderingInfo->pDepthAttachment && pRenderingInfo->pDepthAttachment->imageView != VK_NULL_HANDLE) {
722 auto& depthAttachment = attachments[GetDynamicDepthAttachmentImageIndex()];
723 auto& depthResolveAttachment = attachments[GetDynamicDepthResolveAttachmentImageIndex()];
724 depthAttachment = nullptr;
725 depthResolveAttachment = nullptr;
726
727 auto res =
728 attachments_view_states.insert(dev_data->GetShared<IMAGE_VIEW_STATE>(pRenderingInfo->pDepthAttachment->imageView));
729 depthAttachment = res.first->get();
730 if (pRenderingInfo->pDepthAttachment->resolveMode != VK_RESOLVE_MODE_NONE &&
731 pRenderingInfo->pDepthAttachment->resolveImageView != VK_NULL_HANDLE) {
732 depthResolveAttachment = res.first->get();
733 }
734 }
735
736 if (pRenderingInfo->pStencilAttachment && pRenderingInfo->pStencilAttachment->imageView != VK_NULL_HANDLE) {
737 auto& stencilAttachment = attachments[GetDynamicStencilAttachmentImageIndex()];
738 auto& stencilResolveAttachment = attachments[GetDynamicStencilResolveAttachmentImageIndex()];
739 stencilAttachment = nullptr;
740 stencilResolveAttachment = nullptr;
741
742 auto res =
743 attachments_view_states.insert(dev_data->GetShared<IMAGE_VIEW_STATE>(pRenderingInfo->pStencilAttachment->imageView));
744 stencilAttachment = res.first->get();
745 if (pRenderingInfo->pStencilAttachment->resolveMode != VK_RESOLVE_MODE_NONE &&
746 pRenderingInfo->pStencilAttachment->resolveImageView != VK_NULL_HANDLE) {
747 stencilResolveAttachment = res.first->get();
748 }
749 }
750}
751
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600752void CMD_BUFFER_STATE::Begin(const VkCommandBufferBeginInfo *pBeginInfo) {
753 if (CB_RECORDED == state || CB_INVALID_COMPLETE == state) {
754 Reset();
755 }
756 // Set updated state here in case implicit reset occurs above
757 state = CB_RECORDING;
758 beginInfo = *pBeginInfo;
759 if (beginInfo.pInheritanceInfo && (createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY)) {
760 inheritanceInfo = *(beginInfo.pInheritanceInfo);
761 beginInfo.pInheritanceInfo = &inheritanceInfo;
762 // If we are a secondary command-buffer and inheriting. Update the items we should inherit.
763 if ((createInfo.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY) &&
764 (beginInfo.flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
amhagana448ea52021-11-02 14:09:14 -0400765 if (beginInfo.pInheritanceInfo->renderPass) {
766 activeRenderPass = dev_data->GetShared<RENDER_PASS_STATE>(beginInfo.pInheritanceInfo->renderPass);
767 activeSubpass = beginInfo.pInheritanceInfo->subpass;
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600768
amhagana448ea52021-11-02 14:09:14 -0400769 if (beginInfo.pInheritanceInfo->framebuffer) {
770 activeFramebuffer = dev_data->GetShared<FRAMEBUFFER_STATE>(beginInfo.pInheritanceInfo->framebuffer);
771 active_subpasses = nullptr;
772 active_attachments = nullptr;
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600773
amhagana448ea52021-11-02 14:09:14 -0400774 if (activeFramebuffer) {
775 framebuffers.insert(activeFramebuffer);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600776
amhagana448ea52021-11-02 14:09:14 -0400777 // Set active_subpasses
778 active_subpasses = std::make_shared<std::vector<SUBPASS_INFO>>(activeFramebuffer->createInfo.attachmentCount);
779 const auto& subpass = activeRenderPass->createInfo.pSubpasses[activeSubpass];
780 UpdateSubpassAttachments(subpass, *active_subpasses);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600781
amhagana448ea52021-11-02 14:09:14 -0400782 // Set active_attachments & attachments_view_states
783 active_attachments =
784 std::make_shared<std::vector<IMAGE_VIEW_STATE*>>(activeFramebuffer->createInfo.attachmentCount);
785 UpdateAttachmentsView(nullptr);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600786
amhagana448ea52021-11-02 14:09:14 -0400787 // Connect this framebuffer and its children to this cmdBuffer
788 if (!dev_data->disabled[command_buffer_state]) {
789 AddChild(activeFramebuffer.get());
790 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600791 }
792 }
793 }
amhagana448ea52021-11-02 14:09:14 -0400794 else
795 {
796 auto inheritance_rendering_info = lvl_find_in_chain<VkCommandBufferInheritanceRenderingInfoKHR>(beginInfo.pInheritanceInfo->pNext);
797 if (inheritance_rendering_info) {
798 activeRenderPass = std::make_shared<RENDER_PASS_STATE>(inheritance_rendering_info);
799 }
800 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600801
802 // Check for VkCommandBufferInheritanceViewportScissorInfoNV (VK_NV_inherited_viewport_scissor)
803 auto p_inherited_viewport_scissor_info =
804 LvlFindInChain<VkCommandBufferInheritanceViewportScissorInfoNV>(beginInfo.pInheritanceInfo->pNext);
805 if (p_inherited_viewport_scissor_info != nullptr && p_inherited_viewport_scissor_info->viewportScissor2D) {
806 auto pViewportDepths = p_inherited_viewport_scissor_info->pViewportDepths;
807 inheritedViewportDepths.assign(pViewportDepths,
808 pViewportDepths + p_inherited_viewport_scissor_info->viewportDepthCount);
809 }
810 }
811 }
812
813 auto chained_device_group_struct = LvlFindInChain<VkDeviceGroupCommandBufferBeginInfo>(pBeginInfo->pNext);
814 if (chained_device_group_struct) {
815 initial_device_mask = chained_device_group_struct->deviceMask;
816 } else {
817 initial_device_mask = (1 << dev_data->physical_device_count) - 1;
818 }
819 performance_lock_acquired = dev_data->performance_lock_acquired;
820}
821
822void CMD_BUFFER_STATE::End(VkResult result) {
823 // Cached validation is specific to a specific recording of a specific command buffer.
Jeremy Gebben87db52f2021-10-14 13:55:09 -0600824 descriptorset_cache.clear();
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600825 validated_descriptor_sets.clear();
826 if (VK_SUCCESS == result) {
827 state = CB_RECORDED;
828 }
829}
830
831void CMD_BUFFER_STATE::ExecuteCommands(uint32_t commandBuffersCount, const VkCommandBuffer *pCommandBuffers) {
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600832 RecordCmd(CMD_EXECUTECOMMANDS);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600833 for (uint32_t i = 0; i < commandBuffersCount; i++) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600834 auto sub_cb_state = dev_data->Get<CMD_BUFFER_STATE>(pCommandBuffers[i]);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600835 assert(sub_cb_state);
836 if (!(sub_cb_state->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
837 if (beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
838 // TODO: Because this is a state change, clearing the VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT needs to be moved
839 // from the validation step to the recording step
840 beginInfo.flags &= ~VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
841 }
842 }
843
844 // Propagate inital layout and current layout state to the primary cmd buffer
845 // NOTE: The update/population of the image_layout_map is done in CoreChecks, but for other classes derived from
846 // ValidationStateTracker these maps will be empty, so leaving the propagation in the the state tracker should be a no-op
847 // for those other classes.
848 for (const auto &sub_layout_map_entry : sub_cb_state->image_layout_map) {
Jeremy Gebben6335df62021-11-01 10:50:13 -0600849 const auto *image_state = sub_layout_map_entry.first;
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600850
851 auto *cb_subres_map = GetImageSubresourceLayoutMap(*image_state);
852 const auto *sub_cb_subres_map = &sub_layout_map_entry.second;
853 assert(cb_subres_map && sub_cb_subres_map); // Non const get and map traversal should never be null
854 cb_subres_map->UpdateFrom(*sub_cb_subres_map);
855 }
856
857 sub_cb_state->primaryCommandBuffer = commandBuffer();
858 linkedCommandBuffers.insert(sub_cb_state);
859 AddChild(sub_cb_state);
860 for (auto &function : sub_cb_state->queryUpdates) {
861 queryUpdates.push_back(function);
862 }
863 for (auto &function : sub_cb_state->queue_submit_functions) {
864 queue_submit_functions.push_back(function);
865 }
866
867 // State is trashed after executing secondary command buffers.
868 // Importantly, this function runs after CoreChecks::PreCallValidateCmdExecuteCommands.
869 trashedViewportMask = ~uint32_t(0);
870 trashedScissorMask = ~uint32_t(0);
871 trashedViewportCount = true;
872 trashedScissorCount = true;
873 }
874}
875
876void CMD_BUFFER_STATE::PushDescriptorSetState(VkPipelineBindPoint pipelineBindPoint, PIPELINE_LAYOUT_STATE *pipeline_layout,
877 uint32_t set, uint32_t descriptorWriteCount,
878 const VkWriteDescriptorSet *pDescriptorWrites) {
879 // Short circuit invalid updates
880 if (!pipeline_layout || (set >= pipeline_layout->set_layouts.size()) || !pipeline_layout->set_layouts[set] ||
881 !pipeline_layout->set_layouts[set]->IsPushDescriptor()) {
882 return;
883 }
884
885 // We need a descriptor set to update the bindings with, compatible with the passed layout
886 const auto &dsl = pipeline_layout->set_layouts[set];
887 const auto lv_bind_point = ConvertToLvlBindPoint(pipelineBindPoint);
888 auto &last_bound = lastBound[lv_bind_point];
889 auto &push_descriptor_set = last_bound.push_descriptor_set;
890 // If we are disturbing the current push_desriptor_set clear it
891 if (!push_descriptor_set || !CompatForSet(set, last_bound, pipeline_layout->compat_for_set)) {
892 last_bound.UnbindAndResetPushDescriptorSet(this, new cvdescriptorset::DescriptorSet(0, nullptr, dsl, 0, dev_data));
893 }
894
895 UpdateLastBoundDescriptorSets(pipelineBindPoint, pipeline_layout, set, 1, nullptr, push_descriptor_set.get(), 0, nullptr);
896 last_bound.pipeline_layout = pipeline_layout->layout();
897
898 // Now that we have either the new or extant push_descriptor set ... do the write updates against it
899 push_descriptor_set->PerformPushDescriptorsUpdate(dev_data, descriptorWriteCount, pDescriptorWrites);
900}
901
902// Generic function to handle state update for all CmdDraw* and CmdDispatch* type functions
sfricke-samsung85584a72021-09-30 21:43:38 -0700903void CMD_BUFFER_STATE::UpdateStateCmdDrawDispatchType(CMD_TYPE cmd_type, VkPipelineBindPoint bind_point) {
904 UpdateDrawState(cmd_type, bind_point);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600905 hasDispatchCmd = true;
906}
907
908// Generic function to handle state update for all CmdDraw* type functions
sfricke-samsung85584a72021-09-30 21:43:38 -0700909void CMD_BUFFER_STATE::UpdateStateCmdDrawType(CMD_TYPE cmd_type, VkPipelineBindPoint bind_point) {
910 UpdateStateCmdDrawDispatchType(cmd_type, bind_point);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600911 hasDrawCmd = true;
912
913 // Update the consumed viewport/scissor count.
914 uint32_t &used = usedViewportScissorCount;
915 used = std::max(used, pipelineStaticViewportCount);
916 used = std::max(used, pipelineStaticScissorCount);
917 usedDynamicViewportCount |= !!(dynamic_status & CBSTATUS_VIEWPORT_WITH_COUNT_SET); // !! silences MSVC warn
918 usedDynamicScissorCount |= !!(dynamic_status & CBSTATUS_SCISSOR_WITH_COUNT_SET);
919}
920
sfricke-samsung85584a72021-09-30 21:43:38 -0700921void CMD_BUFFER_STATE::UpdateDrawState(CMD_TYPE cmd_type, const VkPipelineBindPoint bind_point) {
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600922 RecordCmd(cmd_type);
923
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600924 const auto lv_bind_point = ConvertToLvlBindPoint(bind_point);
925 auto &state = lastBound[lv_bind_point];
926 PIPELINE_STATE *pipe = state.pipeline_state;
927 if (VK_NULL_HANDLE != state.pipeline_layout) {
928 for (const auto &set_binding_pair : pipe->active_slots) {
929 uint32_t set_index = set_binding_pair.first;
930 // Pull the set node
931 cvdescriptorset::DescriptorSet *descriptor_set = state.per_set[set_index].bound_descriptor_set;
932
933 // For the "bindless" style resource usage with many descriptors, need to optimize command <-> descriptor binding
934
935 // TODO: If recreating the reduced_map here shows up in profilinging, need to find a way of sharing with the
936 // Validate pass. Though in the case of "many" descriptors, typically the descriptor count >> binding count
937 cvdescriptorset::PrefilterBindRequestMap reduced_map(*descriptor_set, set_binding_pair.second);
938 const auto &binding_req_map = reduced_map.FilteredMap(*this, *pipe);
939
940 if (reduced_map.IsManyDescriptors()) {
941 // Only update validate binding tags if we meet the "many" criteria in the Prefilter class
942 descriptor_set->UpdateValidationCache(*this, *pipe, binding_req_map);
943 }
944
945 // We can skip updating the state if "nothing" has changed since the last validation.
946 // See CoreChecks::ValidateCmdBufDrawState for more details.
947 bool descriptor_set_changed =
948 !reduced_map.IsManyDescriptors() ||
949 // Update if descriptor set (or contents) has changed
950 state.per_set[set_index].validated_set != descriptor_set ||
951 state.per_set[set_index].validated_set_change_count != descriptor_set->GetChangeCount() ||
952 (!dev_data->disabled[image_layout_validation] &&
953 state.per_set[set_index].validated_set_image_layout_change_count != image_layout_change_count);
954 bool need_update = descriptor_set_changed ||
955 // Update if previous bindingReqMap doesn't include new bindingReqMap
956 !std::includes(state.per_set[set_index].validated_set_binding_req_map.begin(),
957 state.per_set[set_index].validated_set_binding_req_map.end(), binding_req_map.begin(),
958 binding_req_map.end());
959
960 if (need_update) {
961 // Bind this set and its active descriptor resources to the command buffer
962 if (!descriptor_set_changed && reduced_map.IsManyDescriptors()) {
963 // Only record the bindings that haven't already been recorded
964 BindingReqMap delta_reqs;
965 std::set_difference(binding_req_map.begin(), binding_req_map.end(),
966 state.per_set[set_index].validated_set_binding_req_map.begin(),
967 state.per_set[set_index].validated_set_binding_req_map.end(),
968 layer_data::insert_iterator<BindingReqMap>(delta_reqs, delta_reqs.begin()));
sfricke-samsung85584a72021-09-30 21:43:38 -0700969 descriptor_set->UpdateDrawState(dev_data, this, cmd_type, pipe, delta_reqs);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600970 } else {
sfricke-samsung85584a72021-09-30 21:43:38 -0700971 descriptor_set->UpdateDrawState(dev_data, this, cmd_type, pipe, binding_req_map);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600972 }
973
974 state.per_set[set_index].validated_set = descriptor_set;
975 state.per_set[set_index].validated_set_change_count = descriptor_set->GetChangeCount();
976 state.per_set[set_index].validated_set_image_layout_change_count = image_layout_change_count;
977 if (reduced_map.IsManyDescriptors()) {
978 // Check whether old == new before assigning, the equality check is much cheaper than
979 // freeing and reallocating the map.
980 if (state.per_set[set_index].validated_set_binding_req_map != set_binding_pair.second) {
981 state.per_set[set_index].validated_set_binding_req_map = set_binding_pair.second;
982 }
983 } else {
984 state.per_set[set_index].validated_set_binding_req_map = BindingReqMap();
985 }
986 }
987 }
988 }
ziga-lunarg4898e4f2021-10-03 13:57:48 +0200989 if (pipe && !pipe->vertex_binding_descriptions_.empty()) {
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600990 vertex_buffer_used = true;
991 }
992}
993
994// Update pipeline_layout bind points applying the "Pipeline Layout Compatibility" rules.
995// One of pDescriptorSets or push_descriptor_set should be nullptr, indicating whether this
996// is called for CmdBindDescriptorSets or CmdPushDescriptorSet.
997void CMD_BUFFER_STATE::UpdateLastBoundDescriptorSets(VkPipelineBindPoint pipeline_bind_point,
998 const PIPELINE_LAYOUT_STATE *pipeline_layout, uint32_t first_set,
999 uint32_t set_count, const VkDescriptorSet *pDescriptorSets,
1000 cvdescriptorset::DescriptorSet *push_descriptor_set,
1001 uint32_t dynamic_offset_count, const uint32_t *p_dynamic_offsets) {
1002 assert((pDescriptorSets == nullptr) ^ (push_descriptor_set == nullptr));
1003 // Defensive
1004 assert(pipeline_layout);
1005 if (!pipeline_layout) return;
1006
1007 uint32_t required_size = first_set + set_count;
1008 const uint32_t last_binding_index = required_size - 1;
1009 assert(last_binding_index < pipeline_layout->compat_for_set.size());
1010
1011 // Some useful shorthand
1012 const auto lv_bind_point = ConvertToLvlBindPoint(pipeline_bind_point);
1013 auto &last_bound = lastBound[lv_bind_point];
1014 auto &pipe_compat_ids = pipeline_layout->compat_for_set;
1015 const uint32_t current_size = static_cast<uint32_t>(last_bound.per_set.size());
1016
1017 // We need this three times in this function, but nowhere else
1018 auto push_descriptor_cleanup = [&last_bound](const cvdescriptorset::DescriptorSet *ds) -> bool {
1019 if (ds && ds->IsPushDescriptor()) {
1020 assert(ds == last_bound.push_descriptor_set.get());
1021 last_bound.push_descriptor_set = nullptr;
1022 return true;
1023 }
1024 return false;
1025 };
1026
1027 // Clean up the "disturbed" before and after the range to be set
1028 if (required_size < current_size) {
1029 if (last_bound.per_set[last_binding_index].compat_id_for_set != pipe_compat_ids[last_binding_index]) {
1030 // We're disturbing those after last, we'll shrink below, but first need to check for and cleanup the push_descriptor
1031 for (auto set_idx = required_size; set_idx < current_size; ++set_idx) {
1032 if (push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set)) break;
1033 }
1034 } else {
1035 // We're not disturbing past last, so leave the upper binding data alone.
1036 required_size = current_size;
1037 }
1038 }
1039
1040 // We resize if we need more set entries or if those past "last" are disturbed
1041 if (required_size != current_size) {
1042 last_bound.per_set.resize(required_size);
1043 }
1044
1045 // For any previously bound sets, need to set them to "invalid" if they were disturbed by this update
1046 for (uint32_t set_idx = 0; set_idx < first_set; ++set_idx) {
1047 if (last_bound.per_set[set_idx].compat_id_for_set != pipe_compat_ids[set_idx]) {
1048 push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set);
1049 last_bound.per_set[set_idx].bound_descriptor_set = nullptr;
1050 last_bound.per_set[set_idx].dynamicOffsets.clear();
1051 last_bound.per_set[set_idx].compat_id_for_set = pipe_compat_ids[set_idx];
1052 }
1053 }
1054
1055 // Now update the bound sets with the input sets
1056 const uint32_t *input_dynamic_offsets = p_dynamic_offsets; // "read" pointer for dynamic offset data
1057 for (uint32_t input_idx = 0; input_idx < set_count; input_idx++) {
1058 auto set_idx = input_idx + first_set; // set_idx is index within layout, input_idx is index within input descriptor sets
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001059 auto descriptor_set = push_descriptor_set ? push_descriptor_set : dev_data->Get<cvdescriptorset::DescriptorSet>(pDescriptorSets[input_idx]);
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001060
1061 // Record binding (or push)
1062 if (descriptor_set != last_bound.push_descriptor_set.get()) {
1063 // Only cleanup the push descriptors if they aren't the currently used set.
1064 push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set);
1065 }
1066 last_bound.per_set[set_idx].bound_descriptor_set = descriptor_set;
1067 last_bound.per_set[set_idx].compat_id_for_set = pipe_compat_ids[set_idx]; // compat ids are canonical *per* set index
1068
1069 if (descriptor_set) {
1070 auto set_dynamic_descriptor_count = descriptor_set->GetDynamicDescriptorCount();
1071 // TODO: Add logic for tracking push_descriptor offsets (here or in caller)
1072 if (set_dynamic_descriptor_count && input_dynamic_offsets) {
1073 const uint32_t *end_offset = input_dynamic_offsets + set_dynamic_descriptor_count;
1074 last_bound.per_set[set_idx].dynamicOffsets = std::vector<uint32_t>(input_dynamic_offsets, end_offset);
1075 input_dynamic_offsets = end_offset;
1076 assert(input_dynamic_offsets <= (p_dynamic_offsets + dynamic_offset_count));
1077 } else {
1078 last_bound.per_set[set_idx].dynamicOffsets.clear();
1079 }
1080 if (!descriptor_set->IsPushDescriptor()) {
1081 // Can't cache validation of push_descriptors
1082 validated_descriptor_sets.insert(descriptor_set);
1083 }
1084 }
1085 }
1086}
1087
1088// Set image layout for given VkImageSubresourceRange struct
1089void CMD_BUFFER_STATE::SetImageLayout(const IMAGE_STATE &image_state, const VkImageSubresourceRange &image_subresource_range,
1090 VkImageLayout layout, VkImageLayout expected_layout) {
1091 auto *subresource_map = GetImageSubresourceLayoutMap(image_state);
1092 assert(subresource_map); // the non-const getter must return a valid pointer
1093 if (subresource_map->SetSubresourceRangeLayout(*this, image_subresource_range, layout, expected_layout)) {
1094 image_layout_change_count++; // Change the version of this data to force revalidation
1095 }
1096 for (const auto *alias_state : image_state.aliasing_images) {
1097 assert(alias_state);
1098 // The map state of the aliases should all be in sync, so no need to check the return value
1099 subresource_map = GetImageSubresourceLayoutMap(*alias_state);
1100 assert(subresource_map);
1101 subresource_map->SetSubresourceRangeLayout(*this, image_subresource_range, layout, expected_layout);
1102 }
1103}
1104
1105// Set the initial image layout for all slices of an image view
1106void CMD_BUFFER_STATE::SetImageViewInitialLayout(const IMAGE_VIEW_STATE &view_state, VkImageLayout layout) {
1107 if (dev_data->disabled[image_layout_validation]) {
1108 return;
1109 }
1110 IMAGE_STATE *image_state = view_state.image_state.get();
1111 auto *subresource_map = GetImageSubresourceLayoutMap(*image_state);
1112 subresource_map->SetSubresourceRangeInitialLayout(*this, layout, view_state);
1113 for (const auto *alias_state : image_state->aliasing_images) {
1114 assert(alias_state);
1115 subresource_map = GetImageSubresourceLayoutMap(*alias_state);
1116 subresource_map->SetSubresourceRangeInitialLayout(*this, layout, view_state);
1117 }
1118}
1119
1120// Set the initial image layout for a passed non-normalized subresource range
1121void CMD_BUFFER_STATE::SetImageInitialLayout(const IMAGE_STATE &image_state, const VkImageSubresourceRange &range,
1122 VkImageLayout layout) {
1123 auto *subresource_map = GetImageSubresourceLayoutMap(image_state);
1124 assert(subresource_map);
1125 subresource_map->SetSubresourceRangeInitialLayout(*this, image_state.NormalizeSubresourceRange(range), layout);
1126 for (const auto *alias_state : image_state.aliasing_images) {
1127 assert(alias_state);
1128 subresource_map = GetImageSubresourceLayoutMap(*alias_state);
1129 assert(subresource_map);
1130 subresource_map->SetSubresourceRangeInitialLayout(*this, alias_state->NormalizeSubresourceRange(range), layout);
1131 }
1132}
1133
1134void CMD_BUFFER_STATE::SetImageInitialLayout(VkImage image, const VkImageSubresourceRange &range, VkImageLayout layout) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001135 const auto image_state = dev_data->Get<IMAGE_STATE>(image);
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001136 if (!image_state) return;
1137 SetImageInitialLayout(*image_state, range, layout);
1138}
1139
1140void CMD_BUFFER_STATE::SetImageInitialLayout(const IMAGE_STATE &image_state, const VkImageSubresourceLayers &layers,
1141 VkImageLayout layout) {
1142 SetImageInitialLayout(image_state, RangeFromLayers(layers), layout);
1143}
1144
1145// Set image layout for all slices of an image view
1146void CMD_BUFFER_STATE::SetImageViewLayout(const IMAGE_VIEW_STATE &view_state, VkImageLayout layout, VkImageLayout layoutStencil) {
1147 const IMAGE_STATE *image_state = view_state.image_state.get();
1148
1149 VkImageSubresourceRange sub_range = view_state.normalized_subresource_range;
1150
1151 if (sub_range.aspectMask == (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT) && layoutStencil != kInvalidLayout) {
1152 sub_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1153 SetImageLayout(*image_state, sub_range, layout);
1154 sub_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1155 SetImageLayout(*image_state, sub_range, layoutStencil);
1156 } else {
1157 SetImageLayout(*image_state, sub_range, layout);
1158 }
1159}
Jeremy Gebben10a0ed12021-08-17 09:54:29 -06001160
1161void CMD_BUFFER_STATE::RecordCmd(CMD_TYPE cmd_type) { commandCount++; }
1162
1163void CMD_BUFFER_STATE::RecordStateCmd(CMD_TYPE cmd_type, CBStatusFlags state_bits) {
1164 RecordCmd(cmd_type);
1165 status |= state_bits;
1166 static_status &= ~state_bits;
1167}
1168
1169void CMD_BUFFER_STATE::RecordTransferCmd(CMD_TYPE cmd_type, BINDABLE *buf1, BINDABLE *buf2) {
1170 RecordCmd(cmd_type);
1171 if (buf1) {
1172 AddChild(buf1);
1173 }
1174 if (buf2) {
1175 AddChild(buf2);
1176 }
1177}
Jeremy Gebben29110d22021-08-17 13:30:50 -06001178
1179static bool SetEventStageMask(VkEvent event, VkPipelineStageFlags2KHR stageMask, EventToStageMap *localEventToStageMap) {
1180 (*localEventToStageMap)[event] = stageMask;
1181 return false;
1182}
1183
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001184void CMD_BUFFER_STATE::RecordSetEvent(CMD_TYPE cmd_type, VkEvent event, VkPipelineStageFlags2KHR stageMask) {
1185 RecordCmd(cmd_type);
1186 if (!dev_data->disabled[command_buffer_state]) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001187 auto event_state = dev_data->Get<EVENT_STATE>(event);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001188 if (event_state) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001189 AddChild(event_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001190 }
1191 }
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001192 events.push_back(event);
1193 if (!waitedEvents.count(event)) {
1194 writeEventsBeforeWait.push_back(event);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001195 }
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001196 eventUpdates.emplace_back(
Jeremy Gebben29110d22021-08-17 13:30:50 -06001197 [event, stageMask](const ValidationStateTracker *device_data, bool do_validate, EventToStageMap *localEventToStageMap) {
1198 return SetEventStageMask(event, stageMask, localEventToStageMap);
1199 });
1200}
1201
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001202void CMD_BUFFER_STATE::RecordResetEvent(CMD_TYPE cmd_type, VkEvent event, VkPipelineStageFlags2KHR stageMask) {
1203 RecordCmd(cmd_type);
1204 if (!dev_data->disabled[command_buffer_state]) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001205 auto event_state = dev_data->Get<EVENT_STATE>(event);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001206 if (event_state) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001207 AddChild(event_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001208 }
1209 }
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001210 events.push_back(event);
1211 if (!waitedEvents.count(event)) {
1212 writeEventsBeforeWait.push_back(event);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001213 }
1214
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001215 eventUpdates.emplace_back([event](const ValidationStateTracker *, bool do_validate, EventToStageMap *localEventToStageMap) {
1216 return SetEventStageMask(event, VkPipelineStageFlags2KHR(0), localEventToStageMap);
1217 });
Jeremy Gebben29110d22021-08-17 13:30:50 -06001218}
1219
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001220void CMD_BUFFER_STATE::RecordWaitEvents(CMD_TYPE cmd_type, uint32_t eventCount, const VkEvent *pEvents) {
1221 RecordCmd(cmd_type);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001222 for (uint32_t i = 0; i < eventCount; ++i) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001223 if (!dev_data->disabled[command_buffer_state]) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001224 auto event_state = dev_data->Get<EVENT_STATE>(pEvents[i]);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001225 if (event_state) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001226 AddChild(event_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001227 }
1228 }
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001229 waitedEvents.insert(pEvents[i]);
1230 events.push_back(pEvents[i]);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001231 }
1232}
1233
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001234void CMD_BUFFER_STATE::RecordBarriers(uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1235 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1236 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) {
1237 if (dev_data->disabled[command_buffer_state]) return;
Jeremy Gebben29110d22021-08-17 13:30:50 -06001238
Jeremy Gebben29110d22021-08-17 13:30:50 -06001239 for (uint32_t i = 0; i < bufferMemoryBarrierCount; i++) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001240 auto buffer_state = dev_data->Get<BUFFER_STATE>(pBufferMemoryBarriers[i].buffer);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001241 if (buffer_state) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001242 AddChild(buffer_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001243 }
1244 }
1245 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001246 auto image_state = dev_data->Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001247 if (image_state) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001248 AddChild(image_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001249 }
1250 }
1251}
1252
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001253void CMD_BUFFER_STATE::RecordBarriers(const VkDependencyInfoKHR &dep_info) {
1254 if (dev_data->disabled[command_buffer_state]) return;
Jeremy Gebben29110d22021-08-17 13:30:50 -06001255
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001256 for (uint32_t i = 0; i < dep_info.bufferMemoryBarrierCount; i++) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001257 auto buffer_state = dev_data->Get<BUFFER_STATE>(dep_info.pBufferMemoryBarriers[i].buffer);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001258 if (buffer_state) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001259 AddChild(buffer_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001260 }
1261 }
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001262 for (uint32_t i = 0; i < dep_info.imageMemoryBarrierCount; i++) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001263 auto image_state = dev_data->Get<IMAGE_STATE>(dep_info.pImageMemoryBarriers[i].image);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001264 if (image_state) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001265 AddChild(image_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001266 }
1267 }
1268}
1269
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001270void CMD_BUFFER_STATE::RecordWriteTimestamp(CMD_TYPE cmd_type, VkPipelineStageFlags2KHR pipelineStage, VkQueryPool queryPool,
1271 uint32_t slot) {
1272 RecordCmd(cmd_type);
1273 if (dev_data->disabled[query_validation]) return;
1274
1275 if (!dev_data->disabled[command_buffer_state]) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001276 auto pool_state = dev_data->Get<QUERY_POOL_STATE>(queryPool);
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001277 AddChild(pool_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001278 }
1279 QueryObject query = {queryPool, slot};
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001280 EndQuery(query);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001281}
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001282
Jeremy Gebben57642982021-09-14 14:14:55 -06001283void CMD_BUFFER_STATE::Submit(uint32_t perf_submit_pass) {
1284 VkQueryPool first_pool = VK_NULL_HANDLE;
1285 EventToStageMap local_event_to_stage_map;
1286 QueryMap local_query_to_state_map;
1287 for (auto &function : queryUpdates) {
1288 function(nullptr, /*do_validate*/ false, first_pool, perf_submit_pass, &local_query_to_state_map);
1289 }
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001290
Jeremy Gebben57642982021-09-14 14:14:55 -06001291 for (const auto &query_state_pair : local_query_to_state_map) {
Jeremy Gebben1858ae92021-12-02 11:28:05 -07001292 auto query_pool_state = dev_data->Get<QUERY_POOL_STATE>(query_state_pair.first.pool);
1293 query_pool_state->SetQueryState(query_state_pair.first.query, query_state_pair.first.perf_pass, query_state_pair.second);
Jeremy Gebben57642982021-09-14 14:14:55 -06001294 }
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001295
Jeremy Gebben57642982021-09-14 14:14:55 -06001296 for (const auto &function : eventUpdates) {
1297 function(nullptr, /*do_validate*/ false, &local_event_to_stage_map);
1298 }
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001299
Jeremy Gebben57642982021-09-14 14:14:55 -06001300 for (const auto &eventStagePair : local_event_to_stage_map) {
Jeremy Gebbend177d922021-10-28 13:42:10 -06001301 dev_data->Get<EVENT_STATE>(eventStagePair.first)->stageMask = eventStagePair.second;
Jeremy Gebben57642982021-09-14 14:14:55 -06001302 }
1303}
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001304
Jeremy Gebben57642982021-09-14 14:14:55 -06001305void CMD_BUFFER_STATE::Retire(uint32_t perf_submit_pass) {
1306 // First perform decrement on general case bound objects
1307 for (auto event : writeEventsBeforeWait) {
Jeremy Gebbend177d922021-10-28 13:42:10 -06001308 auto event_state = dev_data->Get<EVENT_STATE>(event);
1309 if (event_state) {
1310 event_state->write_in_use--;
Jeremy Gebben57642982021-09-14 14:14:55 -06001311 }
1312 }
1313 QueryMap local_query_to_state_map;
1314 VkQueryPool first_pool = VK_NULL_HANDLE;
1315 for (auto &function : queryUpdates) {
1316 function(nullptr, /*do_validate*/ false, first_pool, perf_submit_pass, &local_query_to_state_map);
1317 }
1318
1319 for (const auto &query_state_pair : local_query_to_state_map) {
1320 if (query_state_pair.second == QUERYSTATE_ENDED) {
Jeremy Gebben1858ae92021-12-02 11:28:05 -07001321 auto query_pool_state = dev_data->Get<QUERY_POOL_STATE>(query_state_pair.first.pool);
1322 query_pool_state->SetQueryState(query_state_pair.first.query, query_state_pair.first.perf_pass, QUERYSTATE_AVAILABLE);
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001323 }
1324 }
1325}