blob: ca45764e4715aa237f270d4072001900d21f6511 [file] [log] [blame]
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001/* Copyright (c) 2015-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2022 Valve Corporation
3 * Copyright (c) 2015-2022 LunarG, Inc.
4 * Copyright (C) 2015-2022 Google Inc.
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06005 * 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) {
Jeremy Gebben332d4dd2022-01-01 12:40:02 -070061 auto guard = entry.second->WriteLock();
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060062 entry.second->Reset();
63 }
64}
65
66void COMMAND_POOL_STATE::Destroy() {
67 for (auto &entry : commandBuffers) {
Jeremy Gebben082a9832021-10-28 13:40:11 -060068 dev_data->Destroy<CMD_BUFFER_STATE>(entry.first);
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060069 }
70 commandBuffers.clear();
71 BASE_NODE::Destroy();
72}
73
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060074const char *CommandTypeString(CMD_TYPE type) {
75 // Autogenerated as part of the command_validation.h codegen
76 return kGeneratedCommandNameList[type];
77}
78
79VkDynamicState ConvertToDynamicState(CBStatusFlagBits flag) {
80 switch (flag) {
81 case CBSTATUS_LINE_WIDTH_SET:
82 return VK_DYNAMIC_STATE_LINE_WIDTH;
83 case CBSTATUS_DEPTH_BIAS_SET:
84 return VK_DYNAMIC_STATE_DEPTH_BIAS;
85 case CBSTATUS_BLEND_CONSTANTS_SET:
86 return VK_DYNAMIC_STATE_BLEND_CONSTANTS;
87 case CBSTATUS_DEPTH_BOUNDS_SET:
88 return VK_DYNAMIC_STATE_DEPTH_BOUNDS;
89 case CBSTATUS_STENCIL_READ_MASK_SET:
90 return VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK;
91 case CBSTATUS_STENCIL_WRITE_MASK_SET:
92 return VK_DYNAMIC_STATE_STENCIL_WRITE_MASK;
93 case CBSTATUS_STENCIL_REFERENCE_SET:
94 return VK_DYNAMIC_STATE_STENCIL_REFERENCE;
95 case CBSTATUS_VIEWPORT_SET:
96 return VK_DYNAMIC_STATE_VIEWPORT;
97 case CBSTATUS_SCISSOR_SET:
98 return VK_DYNAMIC_STATE_SCISSOR;
99 case CBSTATUS_EXCLUSIVE_SCISSOR_SET:
100 return VK_DYNAMIC_STATE_EXCLUSIVE_SCISSOR_NV;
101 case CBSTATUS_SHADING_RATE_PALETTE_SET:
102 return VK_DYNAMIC_STATE_VIEWPORT_SHADING_RATE_PALETTE_NV;
103 case CBSTATUS_LINE_STIPPLE_SET:
104 return VK_DYNAMIC_STATE_LINE_STIPPLE_EXT;
105 case CBSTATUS_VIEWPORT_W_SCALING_SET:
106 return VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV;
107 case CBSTATUS_CULL_MODE_SET:
108 return VK_DYNAMIC_STATE_CULL_MODE_EXT;
109 case CBSTATUS_FRONT_FACE_SET:
110 return VK_DYNAMIC_STATE_FRONT_FACE_EXT;
111 case CBSTATUS_PRIMITIVE_TOPOLOGY_SET:
112 return VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT;
113 case CBSTATUS_VIEWPORT_WITH_COUNT_SET:
114 return VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT;
115 case CBSTATUS_SCISSOR_WITH_COUNT_SET:
116 return VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT;
117 case CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET:
118 return VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT;
119 case CBSTATUS_DEPTH_TEST_ENABLE_SET:
120 return VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT;
121 case CBSTATUS_DEPTH_WRITE_ENABLE_SET:
122 return VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT;
123 case CBSTATUS_DEPTH_COMPARE_OP_SET:
124 return VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT;
125 case CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET:
126 return VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT;
127 case CBSTATUS_STENCIL_TEST_ENABLE_SET:
128 return VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT;
129 case CBSTATUS_STENCIL_OP_SET:
130 return VK_DYNAMIC_STATE_STENCIL_OP_EXT;
131 case CBSTATUS_DISCARD_RECTANGLE_SET:
132 return VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT;
133 case CBSTATUS_SAMPLE_LOCATIONS_SET:
134 return VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT;
135 case CBSTATUS_COARSE_SAMPLE_ORDER_SET:
136 return VK_DYNAMIC_STATE_VIEWPORT_COARSE_SAMPLE_ORDER_NV;
137 case CBSTATUS_PATCH_CONTROL_POINTS_SET:
138 return VK_DYNAMIC_STATE_PATCH_CONTROL_POINTS_EXT;
139 case CBSTATUS_RASTERIZER_DISCARD_ENABLE_SET:
140 return VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT;
141 case CBSTATUS_DEPTH_BIAS_ENABLE_SET:
142 return VK_DYNAMIC_STATE_DEPTH_BIAS_ENABLE_EXT;
143 case CBSTATUS_LOGIC_OP_SET:
144 return VK_DYNAMIC_STATE_LOGIC_OP_EXT;
145 case CBSTATUS_PRIMITIVE_RESTART_ENABLE_SET:
146 return VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE_EXT;
147 case CBSTATUS_VERTEX_INPUT_SET:
148 return VK_DYNAMIC_STATE_VERTEX_INPUT_EXT;
ziga-lunarg67b7c392022-03-26 01:45:34 +0100149 case CBSTATUS_COLOR_WRITE_ENABLE_SET:
150 return VK_DYNAMIC_STATE_COLOR_WRITE_ENABLE_EXT;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600151 default:
152 // CBSTATUS_INDEX_BUFFER_BOUND is not in VkDynamicState
153 return VK_DYNAMIC_STATE_MAX_ENUM;
154 }
155 return VK_DYNAMIC_STATE_MAX_ENUM;
156}
157
158CBStatusFlagBits ConvertToCBStatusFlagBits(VkDynamicState state) {
159 switch (state) {
160 case VK_DYNAMIC_STATE_VIEWPORT:
161 return CBSTATUS_VIEWPORT_SET;
162 case VK_DYNAMIC_STATE_SCISSOR:
163 return CBSTATUS_SCISSOR_SET;
164 case VK_DYNAMIC_STATE_LINE_WIDTH:
165 return CBSTATUS_LINE_WIDTH_SET;
166 case VK_DYNAMIC_STATE_DEPTH_BIAS:
167 return CBSTATUS_DEPTH_BIAS_SET;
168 case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
169 return CBSTATUS_BLEND_CONSTANTS_SET;
170 case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
171 return CBSTATUS_DEPTH_BOUNDS_SET;
172 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
173 return CBSTATUS_STENCIL_READ_MASK_SET;
174 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
175 return CBSTATUS_STENCIL_WRITE_MASK_SET;
176 case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
177 return CBSTATUS_STENCIL_REFERENCE_SET;
178 case VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV:
179 return CBSTATUS_VIEWPORT_W_SCALING_SET;
180 case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT:
181 return CBSTATUS_DISCARD_RECTANGLE_SET;
182 case VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT:
183 return CBSTATUS_SAMPLE_LOCATIONS_SET;
184 case VK_DYNAMIC_STATE_VIEWPORT_SHADING_RATE_PALETTE_NV:
185 return CBSTATUS_SHADING_RATE_PALETTE_SET;
186 case VK_DYNAMIC_STATE_VIEWPORT_COARSE_SAMPLE_ORDER_NV:
187 return CBSTATUS_COARSE_SAMPLE_ORDER_SET;
188 case VK_DYNAMIC_STATE_EXCLUSIVE_SCISSOR_NV:
189 return CBSTATUS_EXCLUSIVE_SCISSOR_SET;
190 case VK_DYNAMIC_STATE_LINE_STIPPLE_EXT:
191 return CBSTATUS_LINE_STIPPLE_SET;
192 case VK_DYNAMIC_STATE_CULL_MODE_EXT:
193 return CBSTATUS_CULL_MODE_SET;
194 case VK_DYNAMIC_STATE_FRONT_FACE_EXT:
195 return CBSTATUS_FRONT_FACE_SET;
196 case VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT:
197 return CBSTATUS_PRIMITIVE_TOPOLOGY_SET;
198 case VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT:
199 return CBSTATUS_VIEWPORT_WITH_COUNT_SET;
200 case VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT:
201 return CBSTATUS_SCISSOR_WITH_COUNT_SET;
202 case VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT:
203 return CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET;
204 case VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT:
205 return CBSTATUS_DEPTH_TEST_ENABLE_SET;
206 case VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT:
207 return CBSTATUS_DEPTH_WRITE_ENABLE_SET;
208 case VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT:
209 return CBSTATUS_DEPTH_COMPARE_OP_SET;
210 case VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT:
211 return CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET;
212 case VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT:
213 return CBSTATUS_STENCIL_TEST_ENABLE_SET;
214 case VK_DYNAMIC_STATE_STENCIL_OP_EXT:
215 return CBSTATUS_STENCIL_OP_SET;
216 case VK_DYNAMIC_STATE_PATCH_CONTROL_POINTS_EXT:
217 return CBSTATUS_PATCH_CONTROL_POINTS_SET;
218 case VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT:
219 return CBSTATUS_RASTERIZER_DISCARD_ENABLE_SET;
220 case VK_DYNAMIC_STATE_DEPTH_BIAS_ENABLE_EXT:
221 return CBSTATUS_DEPTH_BIAS_ENABLE_SET;
222 case VK_DYNAMIC_STATE_LOGIC_OP_EXT:
223 return CBSTATUS_LOGIC_OP_SET;
224 case VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE_EXT:
225 return CBSTATUS_PRIMITIVE_RESTART_ENABLE_SET;
226 case VK_DYNAMIC_STATE_VERTEX_INPUT_EXT:
227 return CBSTATUS_VERTEX_INPUT_SET;
ziga-lunarg67b7c392022-03-26 01:45:34 +0100228 case VK_DYNAMIC_STATE_COLOR_WRITE_ENABLE_EXT:
229 return CBSTATUS_COLOR_WRITE_ENABLE_SET;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600230 default:
231 return CBSTATUS_NONE;
232 }
233 return CBSTATUS_NONE;
234}
235
Jeremy Gebbencd7fa282021-10-27 10:25:32 -0600236CMD_BUFFER_STATE::CMD_BUFFER_STATE(ValidationStateTracker *dev, VkCommandBuffer cb, const VkCommandBufferAllocateInfo *pCreateInfo,
237 const COMMAND_POOL_STATE *pool)
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600238 : REFCOUNTED_NODE(cb, kVulkanObjectTypeCommandBuffer),
239 createInfo(*pCreateInfo),
240 command_pool(pool),
Jeremy Gebbencd7fa282021-10-27 10:25:32 -0600241 dev_data(dev),
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600242 unprotected(pool->unprotected) {
243 Reset();
244}
245
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600246// Get the image viewstate for a given framebuffer attachment
247IMAGE_VIEW_STATE *CMD_BUFFER_STATE::GetActiveAttachmentImageViewState(uint32_t index) {
248 assert(active_attachments && index != VK_ATTACHMENT_UNUSED && (index < active_attachments->size()));
249 return active_attachments->at(index);
250}
251
252// Get the image viewstate for a given framebuffer attachment
253const IMAGE_VIEW_STATE *CMD_BUFFER_STATE::GetActiveAttachmentImageViewState(uint32_t index) const {
ziga-lunarg452a5f92021-09-08 15:33:03 +0200254 if (!active_attachments || index == VK_ATTACHMENT_UNUSED || (index >= active_attachments->size())) {
255 return nullptr;
256 }
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600257 return active_attachments->at(index);
258}
259
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700260void CMD_BUFFER_STATE::AddChild(std::shared_ptr<BASE_NODE> &child_node) {
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600261 assert(child_node);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600262 if (child_node->AddParent(this)) {
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600263 object_bindings.insert(child_node);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600264 }
265}
266
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700267void CMD_BUFFER_STATE::RemoveChild(std::shared_ptr<BASE_NODE> &child_node) {
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600268 assert(child_node);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600269 child_node->RemoveParent(this);
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600270 object_bindings.erase(child_node);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600271}
272
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600273// Reset the command buffer state
274// Maintain the createInfo and set state to CB_NEW, but clear all other state
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600275void CMD_BUFFER_STATE::Reset() {
Jeremy Gebbened5fab52022-08-01 09:49:27 -0600276 assert(!InUse());
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600277 // Reset CB state (note that createInfo is not cleared)
278 memset(&beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
279 memset(&inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo));
280 hasDrawCmd = false;
281 hasTraceRaysCmd = false;
282 hasBuildAccelerationStructureCmd = false;
283 hasDispatchCmd = false;
ziga-lunarg0acda6f2022-04-25 22:41:52 +0200284 hasRenderPassInstance = false;
285 suspendsRenderPassInstance = false;
286 resumesRenderPassInstance = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600287 state = CB_NEW;
288 commandCount = 0;
289 submitCount = 0;
290 image_layout_change_count = 1; // Start at 1. 0 is insert value for validation cache versions, s.t. new == dirty
291 status = 0;
292 static_status = 0;
293 inheritedViewportDepths.clear();
294 usedViewportScissorCount = 0;
295 pipelineStaticViewportCount = 0;
296 pipelineStaticScissorCount = 0;
297 viewportMask = 0;
298 viewportWithCountMask = 0;
299 viewportWithCountCount = 0;
300 scissorMask = 0;
301 scissorWithCountMask = 0;
302 scissorWithCountCount = 0;
303 trashedViewportMask = 0;
304 trashedScissorMask = 0;
305 trashedViewportCount = false;
306 trashedScissorCount = false;
307 usedDynamicViewportCount = false;
308 usedDynamicScissorCount = false;
309 primitiveTopology = VK_PRIMITIVE_TOPOLOGY_MAX_ENUM;
ziga-lunarg67b7c392022-03-26 01:45:34 +0100310 dynamicColorWriteEnableAttachmentCount = 0;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600311
312 activeRenderPassBeginInfo = safe_VkRenderPassBeginInfo();
313 activeRenderPass = nullptr;
314 active_attachments = nullptr;
315 active_subpasses = nullptr;
316 attachments_view_states.clear();
317 activeSubpassContents = VK_SUBPASS_CONTENTS_INLINE;
318 activeSubpass = 0;
319 broken_bindings.clear();
320 waitedEvents.clear();
321 events.clear();
322 writeEventsBeforeWait.clear();
323 activeQueries.clear();
324 startedQueries.clear();
325 image_layout_map.clear();
Jeremy Gebben4a8881f2022-01-10 17:04:30 -0700326 aliased_image_layout_map.clear();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600327 current_vertex_buffer_binding_info.vertex_buffer_bindings.clear();
328 vertex_buffer_used = false;
329 primaryCommandBuffer = VK_NULL_HANDLE;
330
331 linkedCommandBuffers.clear();
332 // Remove reverse command buffer links.
333 Invalidate(true);
334
335 queue_submit_functions.clear();
Hans-Kristian Arntzen59c2c3f2021-06-14 11:40:12 +0200336 queue_submit_functions_after_render_pass.clear();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600337 cmd_execute_commands_functions.clear();
338 eventUpdates.clear();
339 queryUpdates.clear();
340
341 // Remove object bindings
342 for (const auto &obj : object_bindings) {
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600343 obj->RemoveParent(this);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600344 }
345 object_bindings.clear();
346
347 for (auto &item : lastBound) {
348 item.Reset();
349 }
350 // Remove this cmdBuffer's reference from each FrameBuffer's CB ref list
351 for (auto &framebuffer : framebuffers) {
352 framebuffer->RemoveParent(this);
353 }
354 framebuffers.clear();
355 activeFramebuffer = VK_NULL_HANDLE;
356 index_buffer_binding.reset();
357
358 qfo_transfer_image_barriers.Reset();
359 qfo_transfer_buffer_barriers.Reset();
360
361 // Clean up the label data
362 debug_label.Reset();
363 validate_descriptorsets_in_queuesubmit.clear();
364
365 // Best practices info
366 small_indexed_draw_call_count = 0;
367
368 transform_feedback_active = false;
369
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600370 // Clean up the label data
371 ResetCmdDebugUtilsLabel(dev_data->report_data, commandBuffer());
372
373 if (dev_data->command_buffer_reset_callback) {
374 (*dev_data->command_buffer_reset_callback)(commandBuffer());
375 }
376}
377
378// Track which resources are in-flight by atomically incrementing their "in_use" count
379void CMD_BUFFER_STATE::IncrementResources() {
380 submitCount++;
381
382 // TODO : We should be able to remove the NULL look-up checks from the code below as long as
383 // all the corresponding cases are verified to cause CB_INVALID state and the CB_INVALID state
384 // should then be flagged prior to calling this function
385 for (auto event : writeEventsBeforeWait) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -0600386 auto event_state = dev_data->Get<EVENT_STATE>(event);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600387 if (event_state) event_state->write_in_use++;
388 }
389}
390
391// Discussed in details in https://github.com/KhronosGroup/Vulkan-Docs/issues/1081
392// Internal discussion and CTS were written to prove that this is not called after an incompatible vkCmdBindPipeline
393// "Binding a pipeline with a layout that is not compatible with the push constant layout does not disturb the push constant values"
394//
395// vkCmdBindDescriptorSet has nothing to do with push constants and don't need to call this after neither
396//
397// Part of this assumes apps at draw/dispath/traceRays/etc time will have it properly compatabile or else other VU will be triggered
398void CMD_BUFFER_STATE::ResetPushConstantDataIfIncompatible(const PIPELINE_LAYOUT_STATE *pipeline_layout_state) {
399 if (pipeline_layout_state == nullptr) {
400 return;
401 }
402 if (push_constant_data_ranges == pipeline_layout_state->push_constant_ranges) {
403 return;
404 }
405
406 push_constant_data_ranges = pipeline_layout_state->push_constant_ranges;
407 push_constant_data.clear();
408 push_constant_data_update.clear();
409 uint32_t size_needed = 0;
410 for (const auto &push_constant_range : *push_constant_data_ranges) {
411 auto size = push_constant_range.offset + push_constant_range.size;
412 size_needed = std::max(size_needed, size);
413
414 auto stage_flags = push_constant_range.stageFlags;
415 uint32_t bit_shift = 0;
416 while (stage_flags) {
417 if (stage_flags & 1) {
418 VkShaderStageFlagBits flag = static_cast<VkShaderStageFlagBits>(1 << bit_shift);
419 const auto it = push_constant_data_update.find(flag);
420
421 if (it != push_constant_data_update.end()) {
422 if (it->second.size() < push_constant_range.offset) {
423 it->second.resize(push_constant_range.offset, PC_Byte_Not_Set);
424 }
425 if (it->second.size() < size) {
426 it->second.resize(size, PC_Byte_Not_Updated);
427 }
428 } else {
429 std::vector<uint8_t> bytes;
430 bytes.resize(push_constant_range.offset, PC_Byte_Not_Set);
431 bytes.resize(size, PC_Byte_Not_Updated);
432 push_constant_data_update[flag] = bytes;
433 }
434 }
435 stage_flags = stage_flags >> 1;
436 ++bit_shift;
437 }
438 }
439 push_constant_data.resize(size_needed, 0);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600440}
441
442void CMD_BUFFER_STATE::Destroy() {
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600443 // Allow any derived class to clean up command buffer state
444 if (dev_data->command_buffer_reset_callback) {
445 (*dev_data->command_buffer_reset_callback)(commandBuffer());
446 }
447 if (dev_data->command_buffer_free_callback) {
448 (*dev_data->command_buffer_free_callback)(commandBuffer());
449 }
450
451 // Remove the cb debug labels
452 EraseCmdDebugUtilsLabel(dev_data->report_data, commandBuffer());
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600453 Reset();
454 BASE_NODE::Destroy();
455}
456
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600457void CMD_BUFFER_STATE::NotifyInvalidate(const BASE_NODE::NodeList &invalid_nodes, bool unlink) {
Jeremy Gebben332d4dd2022-01-01 12:40:02 -0700458 {
459 auto guard = WriteLock();
460 if (state == CB_RECORDING) {
461 state = CB_INVALID_INCOMPLETE;
462 } else if (state == CB_RECORDED) {
463 state = CB_INVALID_COMPLETE;
464 }
465 assert(!invalid_nodes.empty());
466 LogObjectList log_list;
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700467 for (auto &obj : invalid_nodes) {
Jeremy Gebben332d4dd2022-01-01 12:40:02 -0700468 log_list.object_list.emplace_back(obj->Handle());
469 }
470 broken_bindings.emplace(invalid_nodes[0]->Handle(), log_list);
471
472 if (unlink) {
473 for (auto &obj : invalid_nodes) {
474 object_bindings.erase(obj);
475 switch (obj->Type()) {
476 case kVulkanObjectTypeCommandBuffer:
477 linkedCommandBuffers.erase(static_cast<CMD_BUFFER_STATE *>(obj.get()));
478 break;
479 case kVulkanObjectTypeImage:
480 image_layout_map.erase(static_cast<IMAGE_STATE *>(obj.get()));
481 break;
482 default:
483 break;
484 }
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600485 }
486 }
487 }
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600488 BASE_NODE::NotifyInvalidate(invalid_nodes, unlink);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600489}
490
ziga-lunarg189ae5d2021-10-19 13:09:58 +0200491const CommandBufferImageLayoutMap& CMD_BUFFER_STATE::GetImageSubresourceLayoutMap() const { return image_layout_map; }
492
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600493// The const variant only need the image as it is the key for the map
Jeremy Gebben6335df62021-11-01 10:50:13 -0600494const ImageSubresourceLayoutMap *CMD_BUFFER_STATE::GetImageSubresourceLayoutMap(const IMAGE_STATE &image_state) const {
495 auto it = image_layout_map.find(&image_state);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600496 if (it == image_layout_map.cend()) {
497 return nullptr;
498 }
Jeremy Gebben4a8881f2022-01-10 17:04:30 -0700499 return it->second.get();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600500}
501
502// The non-const variant only needs the image state, as the factory requires it to construct a new entry
503ImageSubresourceLayoutMap *CMD_BUFFER_STATE::GetImageSubresourceLayoutMap(const IMAGE_STATE &image_state) {
Jeremy Gebben6335df62021-11-01 10:50:13 -0600504 auto &layout_map = image_layout_map[&image_state];
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600505 if (!layout_map) {
Jeremy Gebben4a8881f2022-01-10 17:04:30 -0700506 // Make sure we don't create a nullptr keyed entry for a zombie Image
507 if (image_state.Destroyed() || !image_state.layout_range_map) {
508 return nullptr;
509 }
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600510 // Was an empty slot... fill it in.
Jeremy Gebben4a8881f2022-01-10 17:04:30 -0700511 if (image_state.CanAlias()) {
512 // Aliasing images need to share the same local layout map.
513 // Since they use the same global layout state, use it as a key
514 // for the local state. We don't need a lock on the global range
515 // map to do a lookup based on its pointer.
516 const auto *global_layout_map = image_state.layout_range_map.get();
517 auto iter = aliased_image_layout_map.find(global_layout_map);
518 if (iter != aliased_image_layout_map.end()) {
519 layout_map = iter->second;
520 } else {
521 layout_map = std::make_shared<ImageSubresourceLayoutMap>(image_state);
522 // Save the local layout map for the next aliased image.
523 // The global layout map pointer is only used as a key into the local lookup
524 // table so it doesn't need to be locked.
525 aliased_image_layout_map.emplace(global_layout_map, layout_map);
526 }
527
528 } else {
529 layout_map = std::make_shared<ImageSubresourceLayoutMap>(image_state);
530 }
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600531 }
Jeremy Gebben4a8881f2022-01-10 17:04:30 -0700532 return layout_map.get();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600533}
534
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600535static bool SetQueryState(QueryObject object, QueryState value, QueryMap *localQueryToStateMap) {
536 (*localQueryToStateMap)[object] = value;
537 return false;
538}
539
540void CMD_BUFFER_STATE::BeginQuery(const QueryObject &query_obj) {
541 activeQueries.insert(query_obj);
542 startedQueries.insert(query_obj);
543 queryUpdates.emplace_back([query_obj](const ValidationStateTracker *device_data, bool do_validate,
544 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass, QueryMap *localQueryToStateMap) {
545 SetQueryState(QueryObject(query_obj, perfQueryPass), QUERYSTATE_RUNNING, localQueryToStateMap);
546 return false;
547 });
ziga-lunarg96c7d822022-02-28 19:39:17 +0100548 updatedQueries.insert(query_obj);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600549}
550
551void CMD_BUFFER_STATE::EndQuery(const QueryObject &query_obj) {
552 activeQueries.erase(query_obj);
553 queryUpdates.emplace_back([query_obj](const ValidationStateTracker *device_data, bool do_validate,
554 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass, QueryMap *localQueryToStateMap) {
555 return SetQueryState(QueryObject(query_obj, perfQueryPass), QUERYSTATE_ENDED, localQueryToStateMap);
556 });
ziga-lunarg96c7d822022-02-28 19:39:17 +0100557 updatedQueries.insert(query_obj);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600558}
559
560static bool SetQueryStateMulti(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount, uint32_t perfPass, QueryState value,
561 QueryMap *localQueryToStateMap) {
562 for (uint32_t i = 0; i < queryCount; i++) {
563 QueryObject object = QueryObject(QueryObject(queryPool, firstQuery + i), perfPass);
564 (*localQueryToStateMap)[object] = value;
565 }
566 return false;
567}
568
569void CMD_BUFFER_STATE::EndQueries(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount) {
570 for (uint32_t slot = firstQuery; slot < (firstQuery + queryCount); slot++) {
571 QueryObject query = {queryPool, slot};
572 activeQueries.erase(query);
ziga-lunarg96c7d822022-02-28 19:39:17 +0100573 updatedQueries.insert(query);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600574 }
575 queryUpdates.emplace_back([queryPool, firstQuery, queryCount](const ValidationStateTracker *device_data, bool do_validate,
576 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass,
577 QueryMap *localQueryToStateMap) {
Lars-Ivar Hesselberg Simonsen1a5646f2021-10-25 15:06:16 +0200578 return SetQueryStateMulti(queryPool, firstQuery, queryCount, perfQueryPass, QUERYSTATE_ENDED, localQueryToStateMap);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600579 });
580}
581
582void CMD_BUFFER_STATE::ResetQueryPool(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount) {
583 for (uint32_t slot = firstQuery; slot < (firstQuery + queryCount); slot++) {
584 QueryObject query = {queryPool, slot};
585 resetQueries.insert(query);
ziga-lunarg96c7d822022-02-28 19:39:17 +0100586 updatedQueries.insert(query);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600587 }
588
589 queryUpdates.emplace_back([queryPool, firstQuery, queryCount](const ValidationStateTracker *device_data, bool do_validate,
590 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass,
591 QueryMap *localQueryToStateMap) {
592 return SetQueryStateMulti(queryPool, firstQuery, queryCount, perfQueryPass, QUERYSTATE_RESET, localQueryToStateMap);
593 });
594}
595
596void UpdateSubpassAttachments(const safe_VkSubpassDescription2 &subpass, std::vector<SUBPASS_INFO> &subpasses) {
597 for (uint32_t index = 0; index < subpass.inputAttachmentCount; ++index) {
598 const uint32_t attachment_index = subpass.pInputAttachments[index].attachment;
599 if (attachment_index != VK_ATTACHMENT_UNUSED) {
600 subpasses[attachment_index].used = true;
601 subpasses[attachment_index].usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
602 subpasses[attachment_index].layout = subpass.pInputAttachments[index].layout;
603 }
604 }
605
606 for (uint32_t index = 0; index < subpass.colorAttachmentCount; ++index) {
607 const uint32_t attachment_index = subpass.pColorAttachments[index].attachment;
608 if (attachment_index != VK_ATTACHMENT_UNUSED) {
609 subpasses[attachment_index].used = true;
610 subpasses[attachment_index].usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
611 subpasses[attachment_index].layout = subpass.pColorAttachments[index].layout;
612 }
613 if (subpass.pResolveAttachments) {
614 const uint32_t attachment_index2 = subpass.pResolveAttachments[index].attachment;
615 if (attachment_index2 != VK_ATTACHMENT_UNUSED) {
616 subpasses[attachment_index2].used = true;
617 subpasses[attachment_index2].usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
618 subpasses[attachment_index2].layout = subpass.pResolveAttachments[index].layout;
619 }
620 }
621 }
622
623 if (subpass.pDepthStencilAttachment) {
624 const uint32_t attachment_index = subpass.pDepthStencilAttachment->attachment;
625 if (attachment_index != VK_ATTACHMENT_UNUSED) {
626 subpasses[attachment_index].used = true;
627 subpasses[attachment_index].usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
628 subpasses[attachment_index].layout = subpass.pDepthStencilAttachment->layout;
629 }
630 }
631}
632
633void CMD_BUFFER_STATE::UpdateAttachmentsView(const VkRenderPassBeginInfo *pRenderPassBegin) {
634 auto &attachments = *(active_attachments.get());
635 const bool imageless = (activeFramebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) ? true : false;
636 const VkRenderPassAttachmentBeginInfo *attachment_info_struct = nullptr;
637 if (pRenderPassBegin) attachment_info_struct = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
638
639 for (uint32_t i = 0; i < attachments.size(); ++i) {
640 if (imageless) {
641 if (attachment_info_struct && i < attachment_info_struct->attachmentCount) {
Jeremy Gebben9f537102021-10-05 16:37:12 -0600642 auto res = attachments_view_states.insert(dev_data->Get<IMAGE_VIEW_STATE>(attachment_info_struct->pAttachments[i]));
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600643 attachments[i] = res.first->get();
644 }
645 } else {
646 auto res = attachments_view_states.insert(activeFramebuffer->attachments_view_state[i]);
647 attachments[i] = res.first->get();
648 }
649 }
650}
651
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600652void CMD_BUFFER_STATE::BeginRenderPass(CMD_TYPE cmd_type, const VkRenderPassBeginInfo *pRenderPassBegin,
653 const VkSubpassContents contents) {
654 RecordCmd(cmd_type);
Jeremy Gebben9f537102021-10-05 16:37:12 -0600655 activeFramebuffer = dev_data->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
656 activeRenderPass = dev_data->Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600657 activeRenderPassBeginInfo = safe_VkRenderPassBeginInfo(pRenderPassBegin);
658 activeSubpass = 0;
659 activeSubpassContents = contents;
660
aitor-lunarga131fca2022-02-17 22:55:55 +0100661 if (activeRenderPass) {
662 // Connect this RP to cmdBuffer
663 if (!dev_data->disabled[command_buffer_state]) {
664 AddChild(activeRenderPass);
665 }
666
667 // Spec states that after BeginRenderPass all resources should be rebound
668 if (activeRenderPass->has_multiview_enabled) {
669 UnbindResources();
670 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600671 }
672
673 auto chained_device_group_struct = LvlFindInChain<VkDeviceGroupRenderPassBeginInfo>(pRenderPassBegin->pNext);
674 if (chained_device_group_struct) {
675 active_render_pass_device_mask = chained_device_group_struct->deviceMask;
676 } else {
677 active_render_pass_device_mask = initial_device_mask;
678 }
679
680 active_subpasses = nullptr;
681 active_attachments = nullptr;
682
683 if (activeFramebuffer) {
684 framebuffers.insert(activeFramebuffer);
685
686 // Set cb_state->active_subpasses
687 active_subpasses = std::make_shared<std::vector<SUBPASS_INFO>>(activeFramebuffer->createInfo.attachmentCount);
688 const auto &subpass = activeRenderPass->createInfo.pSubpasses[activeSubpass];
689 UpdateSubpassAttachments(subpass, *active_subpasses);
690
691 // Set cb_state->active_attachments & cb_state->attachments_view_states
692 active_attachments = std::make_shared<std::vector<IMAGE_VIEW_STATE *>>(activeFramebuffer->createInfo.attachmentCount);
693 UpdateAttachmentsView(pRenderPassBegin);
694
695 // Connect this framebuffer and its children to this cmdBuffer
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700696 AddChild(activeFramebuffer);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600697 }
698}
699
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600700void CMD_BUFFER_STATE::NextSubpass(CMD_TYPE cmd_type, VkSubpassContents contents) {
701 RecordCmd(cmd_type);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600702 activeSubpass++;
703 activeSubpassContents = contents;
704
705 // Update cb_state->active_subpasses
aitor-lunarga131fca2022-02-17 22:55:55 +0100706 if (activeRenderPass) {
707 if (activeFramebuffer) {
708 active_subpasses = nullptr;
709 active_subpasses = std::make_shared<std::vector<SUBPASS_INFO>>(activeFramebuffer->createInfo.attachmentCount);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600710
ziga-lunarg31a3e772022-03-22 11:48:46 +0100711 if (activeSubpass < activeRenderPass->createInfo.subpassCount) {
712 const auto &subpass = activeRenderPass->createInfo.pSubpasses[activeSubpass];
713 UpdateSubpassAttachments(subpass, *active_subpasses);
714 }
aitor-lunarga131fca2022-02-17 22:55:55 +0100715 }
716
717 // Spec states that after NextSubpass all resources should be rebound
718 if (activeRenderPass->has_multiview_enabled) {
719 UnbindResources();
720 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600721 }
722}
723
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600724void CMD_BUFFER_STATE::EndRenderPass(CMD_TYPE cmd_type) {
725 RecordCmd(cmd_type);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600726 activeRenderPass = nullptr;
727 active_attachments = nullptr;
728 active_subpasses = nullptr;
729 activeSubpass = 0;
730 activeFramebuffer = VK_NULL_HANDLE;
731}
732
Tony-LunarG40b33882021-12-02 12:40:11 -0700733void CMD_BUFFER_STATE::BeginRendering(CMD_TYPE cmd_type, const VkRenderingInfo *pRenderingInfo) {
amhagana448ea52021-11-02 14:09:14 -0400734 RecordCmd(cmd_type);
Tony-LunarG40b33882021-12-02 12:40:11 -0700735 begin_rendering_func_name = CommandTypeString(cmd_type);
amhagana448ea52021-11-02 14:09:14 -0400736 activeRenderPass = std::make_shared<RENDER_PASS_STATE>(pRenderingInfo);
737
738 auto chained_device_group_struct = LvlFindInChain<VkDeviceGroupRenderPassBeginInfo>(pRenderingInfo->pNext);
739 if (chained_device_group_struct) {
740 active_render_pass_device_mask = chained_device_group_struct->deviceMask;
741 } else {
742 active_render_pass_device_mask = initial_device_mask;
743 }
744
745 activeSubpassContents = ((pRenderingInfo->flags & VK_RENDERING_CONTENTS_SECONDARY_COMMAND_BUFFERS_BIT_KHR) ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS : VK_SUBPASS_CONTENTS_INLINE);
ziga-lunarg143bdbf2022-05-04 19:04:58 +0200746 if (!hasRenderPassInstance && pRenderingInfo->flags & VK_RENDERING_RESUMING_BIT) {
ziga-lunarg0acda6f2022-04-25 22:41:52 +0200747 resumesRenderPassInstance = true;
748 }
ziga-lunarg143bdbf2022-05-04 19:04:58 +0200749 suspendsRenderPassInstance = (pRenderingInfo->flags & VK_RENDERING_SUSPENDING_BIT) > 0;
750 hasRenderPassInstance = true;
amhagana448ea52021-11-02 14:09:14 -0400751
752 active_attachments = nullptr;
753 uint32_t attachment_count = (pRenderingInfo->colorAttachmentCount + 2) * 2;
754
755 // Set cb_state->active_attachments & cb_state->attachments_view_states
756 active_attachments = std::make_shared<std::vector<IMAGE_VIEW_STATE *>>(attachment_count);
757 auto &attachments = *(active_attachments.get());
758
759 for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; ++i) {
760 auto& colorAttachment = attachments[GetDynamicColorAttachmentImageIndex(i)];
761 auto& colorResolveAttachment = attachments[GetDynamicColorResolveAttachmentImageIndex(i)];
762 colorAttachment = nullptr;
763 colorResolveAttachment = nullptr;
764
765 if (pRenderingInfo->pColorAttachments[i].imageView != VK_NULL_HANDLE) {
Jeremy Gebben9f537102021-10-05 16:37:12 -0600766 auto res =
767 attachments_view_states.insert(dev_data->Get<IMAGE_VIEW_STATE>(pRenderingInfo->pColorAttachments[i].imageView));
amhagana448ea52021-11-02 14:09:14 -0400768 colorAttachment = res.first->get();
769 if (pRenderingInfo->pColorAttachments[i].resolveMode != VK_RESOLVE_MODE_NONE &&
770 pRenderingInfo->pColorAttachments[i].resolveImageView != VK_NULL_HANDLE) {
771 colorResolveAttachment = res.first->get();
772 }
773 }
774 }
775
776 if (pRenderingInfo->pDepthAttachment && pRenderingInfo->pDepthAttachment->imageView != VK_NULL_HANDLE) {
777 auto& depthAttachment = attachments[GetDynamicDepthAttachmentImageIndex()];
778 auto& depthResolveAttachment = attachments[GetDynamicDepthResolveAttachmentImageIndex()];
779 depthAttachment = nullptr;
780 depthResolveAttachment = nullptr;
781
Jeremy Gebben9f537102021-10-05 16:37:12 -0600782 auto res = attachments_view_states.insert(dev_data->Get<IMAGE_VIEW_STATE>(pRenderingInfo->pDepthAttachment->imageView));
amhagana448ea52021-11-02 14:09:14 -0400783 depthAttachment = res.first->get();
784 if (pRenderingInfo->pDepthAttachment->resolveMode != VK_RESOLVE_MODE_NONE &&
785 pRenderingInfo->pDepthAttachment->resolveImageView != VK_NULL_HANDLE) {
786 depthResolveAttachment = res.first->get();
787 }
788 }
789
790 if (pRenderingInfo->pStencilAttachment && pRenderingInfo->pStencilAttachment->imageView != VK_NULL_HANDLE) {
791 auto& stencilAttachment = attachments[GetDynamicStencilAttachmentImageIndex()];
792 auto& stencilResolveAttachment = attachments[GetDynamicStencilResolveAttachmentImageIndex()];
793 stencilAttachment = nullptr;
794 stencilResolveAttachment = nullptr;
795
Jeremy Gebben9f537102021-10-05 16:37:12 -0600796 auto res = attachments_view_states.insert(dev_data->Get<IMAGE_VIEW_STATE>(pRenderingInfo->pStencilAttachment->imageView));
amhagana448ea52021-11-02 14:09:14 -0400797 stencilAttachment = res.first->get();
798 if (pRenderingInfo->pStencilAttachment->resolveMode != VK_RESOLVE_MODE_NONE &&
799 pRenderingInfo->pStencilAttachment->resolveImageView != VK_NULL_HANDLE) {
800 stencilResolveAttachment = res.first->get();
801 }
802 }
803}
804
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600805void CMD_BUFFER_STATE::Begin(const VkCommandBufferBeginInfo *pBeginInfo) {
806 if (CB_RECORDED == state || CB_INVALID_COMPLETE == state) {
807 Reset();
808 }
809 // Set updated state here in case implicit reset occurs above
810 state = CB_RECORDING;
811 beginInfo = *pBeginInfo;
812 if (beginInfo.pInheritanceInfo && (createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY)) {
813 inheritanceInfo = *(beginInfo.pInheritanceInfo);
814 beginInfo.pInheritanceInfo = &inheritanceInfo;
815 // If we are a secondary command-buffer and inheriting. Update the items we should inherit.
816 if ((createInfo.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY) &&
817 (beginInfo.flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
amhagana448ea52021-11-02 14:09:14 -0400818 if (beginInfo.pInheritanceInfo->renderPass) {
Jeremy Gebben9f537102021-10-05 16:37:12 -0600819 activeRenderPass = dev_data->Get<RENDER_PASS_STATE>(beginInfo.pInheritanceInfo->renderPass);
amhagana448ea52021-11-02 14:09:14 -0400820 activeSubpass = beginInfo.pInheritanceInfo->subpass;
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600821
amhagana448ea52021-11-02 14:09:14 -0400822 if (beginInfo.pInheritanceInfo->framebuffer) {
Jeremy Gebben9f537102021-10-05 16:37:12 -0600823 activeFramebuffer = dev_data->Get<FRAMEBUFFER_STATE>(beginInfo.pInheritanceInfo->framebuffer);
amhagana448ea52021-11-02 14:09:14 -0400824 active_subpasses = nullptr;
825 active_attachments = nullptr;
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600826
amhagana448ea52021-11-02 14:09:14 -0400827 if (activeFramebuffer) {
828 framebuffers.insert(activeFramebuffer);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600829
amhagana448ea52021-11-02 14:09:14 -0400830 // Set active_subpasses
831 active_subpasses = std::make_shared<std::vector<SUBPASS_INFO>>(activeFramebuffer->createInfo.attachmentCount);
832 const auto& subpass = activeRenderPass->createInfo.pSubpasses[activeSubpass];
833 UpdateSubpassAttachments(subpass, *active_subpasses);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600834
amhagana448ea52021-11-02 14:09:14 -0400835 // Set active_attachments & attachments_view_states
836 active_attachments =
837 std::make_shared<std::vector<IMAGE_VIEW_STATE*>>(activeFramebuffer->createInfo.attachmentCount);
838 UpdateAttachmentsView(nullptr);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600839
amhagana448ea52021-11-02 14:09:14 -0400840 // Connect this framebuffer and its children to this cmdBuffer
841 if (!dev_data->disabled[command_buffer_state]) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700842 AddChild(activeFramebuffer);
amhagana448ea52021-11-02 14:09:14 -0400843 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600844 }
845 }
846 }
amhagana448ea52021-11-02 14:09:14 -0400847 else
848 {
Tony-LunarG40b33882021-12-02 12:40:11 -0700849 auto inheritance_rendering_info = lvl_find_in_chain<VkCommandBufferInheritanceRenderingInfo>(beginInfo.pInheritanceInfo->pNext);
amhagana448ea52021-11-02 14:09:14 -0400850 if (inheritance_rendering_info) {
851 activeRenderPass = std::make_shared<RENDER_PASS_STATE>(inheritance_rendering_info);
852 }
853 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600854
855 // Check for VkCommandBufferInheritanceViewportScissorInfoNV (VK_NV_inherited_viewport_scissor)
856 auto p_inherited_viewport_scissor_info =
857 LvlFindInChain<VkCommandBufferInheritanceViewportScissorInfoNV>(beginInfo.pInheritanceInfo->pNext);
858 if (p_inherited_viewport_scissor_info != nullptr && p_inherited_viewport_scissor_info->viewportScissor2D) {
859 auto pViewportDepths = p_inherited_viewport_scissor_info->pViewportDepths;
860 inheritedViewportDepths.assign(pViewportDepths,
861 pViewportDepths + p_inherited_viewport_scissor_info->viewportDepthCount);
862 }
863 }
864 }
865
866 auto chained_device_group_struct = LvlFindInChain<VkDeviceGroupCommandBufferBeginInfo>(pBeginInfo->pNext);
867 if (chained_device_group_struct) {
868 initial_device_mask = chained_device_group_struct->deviceMask;
869 } else {
870 initial_device_mask = (1 << dev_data->physical_device_count) - 1;
871 }
872 performance_lock_acquired = dev_data->performance_lock_acquired;
ziga-lunarg96c7d822022-02-28 19:39:17 +0100873 updatedQueries.clear();
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600874}
875
876void CMD_BUFFER_STATE::End(VkResult result) {
877 // Cached validation is specific to a specific recording of a specific command buffer.
Jeremy Gebben87db52f2021-10-14 13:55:09 -0600878 descriptorset_cache.clear();
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600879 validated_descriptor_sets.clear();
880 if (VK_SUCCESS == result) {
881 state = CB_RECORDED;
882 }
883}
884
885void CMD_BUFFER_STATE::ExecuteCommands(uint32_t commandBuffersCount, const VkCommandBuffer *pCommandBuffers) {
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600886 RecordCmd(CMD_EXECUTECOMMANDS);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600887 for (uint32_t i = 0; i < commandBuffersCount; i++) {
Jeremy Gebben332d4dd2022-01-01 12:40:02 -0700888 auto sub_cb_state = dev_data->GetWrite<CMD_BUFFER_STATE>(pCommandBuffers[i]);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600889 assert(sub_cb_state);
890 if (!(sub_cb_state->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
891 if (beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
892 // TODO: Because this is a state change, clearing the VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT needs to be moved
893 // from the validation step to the recording step
894 beginInfo.flags &= ~VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
895 }
896 }
897
898 // Propagate inital layout and current layout state to the primary cmd buffer
899 // NOTE: The update/population of the image_layout_map is done in CoreChecks, but for other classes derived from
900 // ValidationStateTracker these maps will be empty, so leaving the propagation in the the state tracker should be a no-op
901 // for those other classes.
902 for (const auto &sub_layout_map_entry : sub_cb_state->image_layout_map) {
Jeremy Gebben6335df62021-11-01 10:50:13 -0600903 const auto *image_state = sub_layout_map_entry.first;
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600904
905 auto *cb_subres_map = GetImageSubresourceLayoutMap(*image_state);
Jeremy Gebben4a8881f2022-01-10 17:04:30 -0700906 if (cb_subres_map) {
907 const auto &sub_cb_subres_map = sub_layout_map_entry.second;
908 cb_subres_map->UpdateFrom(*sub_cb_subres_map);
909 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600910 }
911
912 sub_cb_state->primaryCommandBuffer = commandBuffer();
Jeremy Gebben9f537102021-10-05 16:37:12 -0600913 linkedCommandBuffers.insert(sub_cb_state.get());
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700914 AddChild(sub_cb_state);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600915 for (auto &function : sub_cb_state->queryUpdates) {
916 queryUpdates.push_back(function);
917 }
aitor-lunarge8c5f8b2022-03-28 20:29:42 +0200918 for (auto &function : sub_cb_state->eventUpdates) {
919 eventUpdates.push_back(function);
920 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600921 for (auto &function : sub_cb_state->queue_submit_functions) {
922 queue_submit_functions.push_back(function);
923 }
924
925 // State is trashed after executing secondary command buffers.
926 // Importantly, this function runs after CoreChecks::PreCallValidateCmdExecuteCommands.
927 trashedViewportMask = ~uint32_t(0);
928 trashedScissorMask = ~uint32_t(0);
929 trashedViewportCount = true;
930 trashedScissorCount = true;
931 }
932}
933
934void CMD_BUFFER_STATE::PushDescriptorSetState(VkPipelineBindPoint pipelineBindPoint, PIPELINE_LAYOUT_STATE *pipeline_layout,
935 uint32_t set, uint32_t descriptorWriteCount,
936 const VkWriteDescriptorSet *pDescriptorWrites) {
937 // Short circuit invalid updates
938 if (!pipeline_layout || (set >= pipeline_layout->set_layouts.size()) || !pipeline_layout->set_layouts[set] ||
939 !pipeline_layout->set_layouts[set]->IsPushDescriptor()) {
940 return;
941 }
942
943 // We need a descriptor set to update the bindings with, compatible with the passed layout
944 const auto &dsl = pipeline_layout->set_layouts[set];
945 const auto lv_bind_point = ConvertToLvlBindPoint(pipelineBindPoint);
946 auto &last_bound = lastBound[lv_bind_point];
947 auto &push_descriptor_set = last_bound.push_descriptor_set;
948 // If we are disturbing the current push_desriptor_set clear it
949 if (!push_descriptor_set || !CompatForSet(set, last_bound, pipeline_layout->compat_for_set)) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -0700950 last_bound.UnbindAndResetPushDescriptorSet(
951 this, std::make_shared<cvdescriptorset::DescriptorSet>(VK_NULL_HANDLE, nullptr, dsl, 0, dev_data));
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600952 }
953
Jeremy Gebben4d51c552022-01-06 21:27:15 -0700954 UpdateLastBoundDescriptorSets(pipelineBindPoint, pipeline_layout, set, 1, nullptr, push_descriptor_set, 0, nullptr);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600955 last_bound.pipeline_layout = pipeline_layout->layout();
956
957 // Now that we have either the new or extant push_descriptor set ... do the write updates against it
958 push_descriptor_set->PerformPushDescriptorsUpdate(dev_data, descriptorWriteCount, pDescriptorWrites);
959}
960
961// Generic function to handle state update for all CmdDraw* and CmdDispatch* type functions
sfricke-samsung85584a72021-09-30 21:43:38 -0700962void CMD_BUFFER_STATE::UpdateStateCmdDrawDispatchType(CMD_TYPE cmd_type, VkPipelineBindPoint bind_point) {
963 UpdateDrawState(cmd_type, bind_point);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600964 hasDispatchCmd = true;
965}
966
967// Generic function to handle state update for all CmdDraw* type functions
sfricke-samsung85584a72021-09-30 21:43:38 -0700968void CMD_BUFFER_STATE::UpdateStateCmdDrawType(CMD_TYPE cmd_type, VkPipelineBindPoint bind_point) {
969 UpdateStateCmdDrawDispatchType(cmd_type, bind_point);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600970 hasDrawCmd = true;
971
972 // Update the consumed viewport/scissor count.
973 uint32_t &used = usedViewportScissorCount;
974 used = std::max(used, pipelineStaticViewportCount);
975 used = std::max(used, pipelineStaticScissorCount);
976 usedDynamicViewportCount |= !!(dynamic_status & CBSTATUS_VIEWPORT_WITH_COUNT_SET); // !! silences MSVC warn
977 usedDynamicScissorCount |= !!(dynamic_status & CBSTATUS_SCISSOR_WITH_COUNT_SET);
978}
979
sfricke-samsung85584a72021-09-30 21:43:38 -0700980void CMD_BUFFER_STATE::UpdateDrawState(CMD_TYPE cmd_type, const VkPipelineBindPoint bind_point) {
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600981 RecordCmd(cmd_type);
982
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600983 const auto lv_bind_point = ConvertToLvlBindPoint(bind_point);
984 auto &state = lastBound[lv_bind_point];
985 PIPELINE_STATE *pipe = state.pipeline_state;
986 if (VK_NULL_HANDLE != state.pipeline_layout) {
987 for (const auto &set_binding_pair : pipe->active_slots) {
988 uint32_t set_index = set_binding_pair.first;
Charles Bakera263fee2022-02-18 12:45:21 +1300989 if (set_index >= state.per_set.size()) {
990 continue;
991 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600992 // Pull the set node
Jeremy Gebben4d51c552022-01-06 21:27:15 -0700993 auto &descriptor_set = state.per_set[set_index].bound_descriptor_set;
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600994
995 // For the "bindless" style resource usage with many descriptors, need to optimize command <-> descriptor binding
996
997 // TODO: If recreating the reduced_map here shows up in profilinging, need to find a way of sharing with the
998 // Validate pass. Though in the case of "many" descriptors, typically the descriptor count >> binding count
999 cvdescriptorset::PrefilterBindRequestMap reduced_map(*descriptor_set, set_binding_pair.second);
1000 const auto &binding_req_map = reduced_map.FilteredMap(*this, *pipe);
1001
1002 if (reduced_map.IsManyDescriptors()) {
1003 // Only update validate binding tags if we meet the "many" criteria in the Prefilter class
1004 descriptor_set->UpdateValidationCache(*this, *pipe, binding_req_map);
1005 }
1006
1007 // We can skip updating the state if "nothing" has changed since the last validation.
1008 // See CoreChecks::ValidateCmdBufDrawState for more details.
1009 bool descriptor_set_changed =
1010 !reduced_map.IsManyDescriptors() ||
1011 // Update if descriptor set (or contents) has changed
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001012 state.per_set[set_index].validated_set != descriptor_set.get() ||
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001013 state.per_set[set_index].validated_set_change_count != descriptor_set->GetChangeCount() ||
1014 (!dev_data->disabled[image_layout_validation] &&
1015 state.per_set[set_index].validated_set_image_layout_change_count != image_layout_change_count);
1016 bool need_update = descriptor_set_changed ||
1017 // Update if previous bindingReqMap doesn't include new bindingReqMap
1018 !std::includes(state.per_set[set_index].validated_set_binding_req_map.begin(),
1019 state.per_set[set_index].validated_set_binding_req_map.end(), binding_req_map.begin(),
1020 binding_req_map.end());
1021
1022 if (need_update) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001023 if (!dev_data->disabled[command_buffer_state] && !descriptor_set->IsPushDescriptor()) {
1024 AddChild(descriptor_set);
1025 }
1026
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001027 // Bind this set and its active descriptor resources to the command buffer
1028 if (!descriptor_set_changed && reduced_map.IsManyDescriptors()) {
1029 // Only record the bindings that haven't already been recorded
1030 BindingReqMap delta_reqs;
1031 std::set_difference(binding_req_map.begin(), binding_req_map.end(),
1032 state.per_set[set_index].validated_set_binding_req_map.begin(),
1033 state.per_set[set_index].validated_set_binding_req_map.end(),
1034 layer_data::insert_iterator<BindingReqMap>(delta_reqs, delta_reqs.begin()));
sfricke-samsung85584a72021-09-30 21:43:38 -07001035 descriptor_set->UpdateDrawState(dev_data, this, cmd_type, pipe, delta_reqs);
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001036 } else {
sfricke-samsung85584a72021-09-30 21:43:38 -07001037 descriptor_set->UpdateDrawState(dev_data, this, cmd_type, pipe, binding_req_map);
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001038 }
1039
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001040 state.per_set[set_index].validated_set = descriptor_set.get();
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001041 state.per_set[set_index].validated_set_change_count = descriptor_set->GetChangeCount();
1042 state.per_set[set_index].validated_set_image_layout_change_count = image_layout_change_count;
1043 if (reduced_map.IsManyDescriptors()) {
1044 // Check whether old == new before assigning, the equality check is much cheaper than
1045 // freeing and reallocating the map.
1046 if (state.per_set[set_index].validated_set_binding_req_map != set_binding_pair.second) {
1047 state.per_set[set_index].validated_set_binding_req_map = set_binding_pair.second;
1048 }
1049 } else {
1050 state.per_set[set_index].validated_set_binding_req_map = BindingReqMap();
1051 }
1052 }
1053 }
1054 }
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001055 if (pipe && pipe->vertex_input_state && !pipe->vertex_input_state->binding_descriptions.empty()) {
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001056 vertex_buffer_used = true;
1057 }
1058}
1059
1060// Update pipeline_layout bind points applying the "Pipeline Layout Compatibility" rules.
1061// One of pDescriptorSets or push_descriptor_set should be nullptr, indicating whether this
1062// is called for CmdBindDescriptorSets or CmdPushDescriptorSet.
1063void CMD_BUFFER_STATE::UpdateLastBoundDescriptorSets(VkPipelineBindPoint pipeline_bind_point,
1064 const PIPELINE_LAYOUT_STATE *pipeline_layout, uint32_t first_set,
1065 uint32_t set_count, const VkDescriptorSet *pDescriptorSets,
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001066 std::shared_ptr<cvdescriptorset::DescriptorSet> &push_descriptor_set,
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001067 uint32_t dynamic_offset_count, const uint32_t *p_dynamic_offsets) {
1068 assert((pDescriptorSets == nullptr) ^ (push_descriptor_set == nullptr));
1069 // Defensive
1070 assert(pipeline_layout);
1071 if (!pipeline_layout) return;
1072
1073 uint32_t required_size = first_set + set_count;
1074 const uint32_t last_binding_index = required_size - 1;
1075 assert(last_binding_index < pipeline_layout->compat_for_set.size());
1076
1077 // Some useful shorthand
1078 const auto lv_bind_point = ConvertToLvlBindPoint(pipeline_bind_point);
1079 auto &last_bound = lastBound[lv_bind_point];
Jeremy Gebben856b8c62021-12-01 15:20:07 -07001080 last_bound.pipeline_layout = pipeline_layout->layout();
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001081 auto &pipe_compat_ids = pipeline_layout->compat_for_set;
Jeremy Gebben856b8c62021-12-01 15:20:07 -07001082 // Resize binding arrays
1083 uint32_t last_set_index = first_set + set_count - 1;
1084 if (last_set_index >= last_bound.per_set.size()) {
1085 last_bound.per_set.resize(last_set_index + 1);
1086 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001087 const uint32_t current_size = static_cast<uint32_t>(last_bound.per_set.size());
1088
1089 // We need this three times in this function, but nowhere else
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001090 auto push_descriptor_cleanup = [&last_bound](const std::shared_ptr<cvdescriptorset::DescriptorSet> &ds) -> bool {
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001091 if (ds && ds->IsPushDescriptor()) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001092 assert(ds == last_bound.push_descriptor_set);
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001093 last_bound.push_descriptor_set = nullptr;
1094 return true;
1095 }
1096 return false;
1097 };
1098
1099 // Clean up the "disturbed" before and after the range to be set
1100 if (required_size < current_size) {
1101 if (last_bound.per_set[last_binding_index].compat_id_for_set != pipe_compat_ids[last_binding_index]) {
1102 // We're disturbing those after last, we'll shrink below, but first need to check for and cleanup the push_descriptor
1103 for (auto set_idx = required_size; set_idx < current_size; ++set_idx) {
1104 if (push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set)) break;
1105 }
1106 } else {
1107 // We're not disturbing past last, so leave the upper binding data alone.
1108 required_size = current_size;
1109 }
1110 }
1111
1112 // We resize if we need more set entries or if those past "last" are disturbed
1113 if (required_size != current_size) {
1114 last_bound.per_set.resize(required_size);
1115 }
1116
1117 // For any previously bound sets, need to set them to "invalid" if they were disturbed by this update
1118 for (uint32_t set_idx = 0; set_idx < first_set; ++set_idx) {
1119 if (last_bound.per_set[set_idx].compat_id_for_set != pipe_compat_ids[set_idx]) {
1120 push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set);
1121 last_bound.per_set[set_idx].bound_descriptor_set = nullptr;
1122 last_bound.per_set[set_idx].dynamicOffsets.clear();
1123 last_bound.per_set[set_idx].compat_id_for_set = pipe_compat_ids[set_idx];
1124 }
1125 }
1126
1127 // Now update the bound sets with the input sets
1128 const uint32_t *input_dynamic_offsets = p_dynamic_offsets; // "read" pointer for dynamic offset data
1129 for (uint32_t input_idx = 0; input_idx < set_count; input_idx++) {
1130 auto set_idx = input_idx + first_set; // set_idx is index within layout, input_idx is index within input descriptor sets
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001131 auto descriptor_set =
1132 push_descriptor_set ? push_descriptor_set : dev_data->Get<cvdescriptorset::DescriptorSet>(pDescriptorSets[input_idx]);
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001133
1134 // Record binding (or push)
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001135 if (descriptor_set != last_bound.push_descriptor_set) {
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001136 // Only cleanup the push descriptors if they aren't the currently used set.
1137 push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set);
1138 }
1139 last_bound.per_set[set_idx].bound_descriptor_set = descriptor_set;
1140 last_bound.per_set[set_idx].compat_id_for_set = pipe_compat_ids[set_idx]; // compat ids are canonical *per* set index
1141
1142 if (descriptor_set) {
1143 auto set_dynamic_descriptor_count = descriptor_set->GetDynamicDescriptorCount();
1144 // TODO: Add logic for tracking push_descriptor offsets (here or in caller)
1145 if (set_dynamic_descriptor_count && input_dynamic_offsets) {
1146 const uint32_t *end_offset = input_dynamic_offsets + set_dynamic_descriptor_count;
1147 last_bound.per_set[set_idx].dynamicOffsets = std::vector<uint32_t>(input_dynamic_offsets, end_offset);
1148 input_dynamic_offsets = end_offset;
1149 assert(input_dynamic_offsets <= (p_dynamic_offsets + dynamic_offset_count));
1150 } else {
1151 last_bound.per_set[set_idx].dynamicOffsets.clear();
1152 }
1153 if (!descriptor_set->IsPushDescriptor()) {
1154 // Can't cache validation of push_descriptors
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001155 validated_descriptor_sets.insert(descriptor_set.get());
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001156 }
1157 }
1158 }
1159}
1160
1161// Set image layout for given VkImageSubresourceRange struct
1162void CMD_BUFFER_STATE::SetImageLayout(const IMAGE_STATE &image_state, const VkImageSubresourceRange &image_subresource_range,
1163 VkImageLayout layout, VkImageLayout expected_layout) {
1164 auto *subresource_map = GetImageSubresourceLayoutMap(image_state);
Jeremy Gebben4a8881f2022-01-10 17:04:30 -07001165 if (subresource_map && subresource_map->SetSubresourceRangeLayout(*this, image_subresource_range, layout, expected_layout)) {
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001166 image_layout_change_count++; // Change the version of this data to force revalidation
1167 }
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001168}
1169
1170// Set the initial image layout for all slices of an image view
1171void CMD_BUFFER_STATE::SetImageViewInitialLayout(const IMAGE_VIEW_STATE &view_state, VkImageLayout layout) {
1172 if (dev_data->disabled[image_layout_validation]) {
1173 return;
1174 }
1175 IMAGE_STATE *image_state = view_state.image_state.get();
1176 auto *subresource_map = GetImageSubresourceLayoutMap(*image_state);
Jeremy Gebben4a8881f2022-01-10 17:04:30 -07001177 if (subresource_map) {
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001178 subresource_map->SetSubresourceRangeInitialLayout(*this, layout, view_state);
1179 }
1180}
1181
1182// Set the initial image layout for a passed non-normalized subresource range
1183void CMD_BUFFER_STATE::SetImageInitialLayout(const IMAGE_STATE &image_state, const VkImageSubresourceRange &range,
1184 VkImageLayout layout) {
1185 auto *subresource_map = GetImageSubresourceLayoutMap(image_state);
Jeremy Gebben4a8881f2022-01-10 17:04:30 -07001186 if (subresource_map) {
1187 subresource_map->SetSubresourceRangeInitialLayout(*this, image_state.NormalizeSubresourceRange(range), layout);
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001188 }
1189}
1190
1191void CMD_BUFFER_STATE::SetImageInitialLayout(VkImage image, const VkImageSubresourceRange &range, VkImageLayout layout) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07001192 auto image_state = dev_data->Get<IMAGE_STATE>(image);
Jeremy Gebben1ec89332021-08-05 13:51:49 -06001193 if (!image_state) return;
1194 SetImageInitialLayout(*image_state, range, layout);
1195}
1196
1197void CMD_BUFFER_STATE::SetImageInitialLayout(const IMAGE_STATE &image_state, const VkImageSubresourceLayers &layers,
1198 VkImageLayout layout) {
1199 SetImageInitialLayout(image_state, RangeFromLayers(layers), layout);
1200}
1201
1202// Set image layout for all slices of an image view
1203void CMD_BUFFER_STATE::SetImageViewLayout(const IMAGE_VIEW_STATE &view_state, VkImageLayout layout, VkImageLayout layoutStencil) {
1204 const IMAGE_STATE *image_state = view_state.image_state.get();
1205
1206 VkImageSubresourceRange sub_range = view_state.normalized_subresource_range;
1207
1208 if (sub_range.aspectMask == (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT) && layoutStencil != kInvalidLayout) {
1209 sub_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1210 SetImageLayout(*image_state, sub_range, layout);
1211 sub_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1212 SetImageLayout(*image_state, sub_range, layoutStencil);
1213 } else {
1214 SetImageLayout(*image_state, sub_range, layout);
1215 }
1216}
Jeremy Gebben10a0ed12021-08-17 09:54:29 -06001217
1218void CMD_BUFFER_STATE::RecordCmd(CMD_TYPE cmd_type) { commandCount++; }
1219
1220void CMD_BUFFER_STATE::RecordStateCmd(CMD_TYPE cmd_type, CBStatusFlags state_bits) {
1221 RecordCmd(cmd_type);
1222 status |= state_bits;
1223 static_status &= ~state_bits;
1224}
1225
ziga-lunarg67b7c392022-03-26 01:45:34 +01001226void CMD_BUFFER_STATE::RecordColorWriteEnableStateCmd(CMD_TYPE cmd_type, CBStatusFlags state_bits, uint32_t attachment_count) {
1227 RecordStateCmd(cmd_type, state_bits);
1228 dynamicColorWriteEnableAttachmentCount = std::max(dynamicColorWriteEnableAttachmentCount, attachment_count);
1229}
1230
Jeremy Gebben9f537102021-10-05 16:37:12 -06001231void CMD_BUFFER_STATE::RecordTransferCmd(CMD_TYPE cmd_type, std::shared_ptr<BINDABLE> &&buf1, std::shared_ptr<BINDABLE> &&buf2) {
Jeremy Gebben10a0ed12021-08-17 09:54:29 -06001232 RecordCmd(cmd_type);
1233 if (buf1) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001234 AddChild(buf1);
Jeremy Gebben10a0ed12021-08-17 09:54:29 -06001235 }
1236 if (buf2) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001237 AddChild(buf2);
Jeremy Gebben10a0ed12021-08-17 09:54:29 -06001238 }
1239}
Jeremy Gebben29110d22021-08-17 13:30:50 -06001240
1241static bool SetEventStageMask(VkEvent event, VkPipelineStageFlags2KHR stageMask, EventToStageMap *localEventToStageMap) {
1242 (*localEventToStageMap)[event] = stageMask;
1243 return false;
1244}
1245
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001246void CMD_BUFFER_STATE::RecordSetEvent(CMD_TYPE cmd_type, VkEvent event, VkPipelineStageFlags2KHR stageMask) {
1247 RecordCmd(cmd_type);
1248 if (!dev_data->disabled[command_buffer_state]) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001249 auto event_state = dev_data->Get<EVENT_STATE>(event);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001250 if (event_state) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001251 AddChild(event_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001252 }
1253 }
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001254 events.push_back(event);
1255 if (!waitedEvents.count(event)) {
1256 writeEventsBeforeWait.push_back(event);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001257 }
Jeremy Gebben332d4dd2022-01-01 12:40:02 -07001258 eventUpdates.emplace_back([event, stageMask](CMD_BUFFER_STATE &, bool do_validate, EventToStageMap *localEventToStageMap) {
1259 return SetEventStageMask(event, stageMask, localEventToStageMap);
1260 });
Jeremy Gebben29110d22021-08-17 13:30:50 -06001261}
1262
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001263void CMD_BUFFER_STATE::RecordResetEvent(CMD_TYPE cmd_type, VkEvent event, VkPipelineStageFlags2KHR stageMask) {
1264 RecordCmd(cmd_type);
1265 if (!dev_data->disabled[command_buffer_state]) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001266 auto event_state = dev_data->Get<EVENT_STATE>(event);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001267 if (event_state) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001268 AddChild(event_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001269 }
1270 }
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001271 events.push_back(event);
1272 if (!waitedEvents.count(event)) {
1273 writeEventsBeforeWait.push_back(event);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001274 }
1275
Jeremy Gebben332d4dd2022-01-01 12:40:02 -07001276 eventUpdates.emplace_back([event](CMD_BUFFER_STATE &, bool do_validate, EventToStageMap *localEventToStageMap) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001277 return SetEventStageMask(event, VkPipelineStageFlags2KHR(0), localEventToStageMap);
1278 });
Jeremy Gebben29110d22021-08-17 13:30:50 -06001279}
1280
Jeremy Gebben332d4dd2022-01-01 12:40:02 -07001281void CMD_BUFFER_STATE::RecordWaitEvents(CMD_TYPE cmd_type, uint32_t eventCount, const VkEvent *pEvents,
1282 VkPipelineStageFlags2KHR src_stage_mask) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001283 RecordCmd(cmd_type);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001284 for (uint32_t i = 0; i < eventCount; ++i) {
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001285 if (!dev_data->disabled[command_buffer_state]) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001286 auto event_state = dev_data->Get<EVENT_STATE>(pEvents[i]);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001287 if (event_state) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001288 AddChild(event_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001289 }
1290 }
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001291 waitedEvents.insert(pEvents[i]);
1292 events.push_back(pEvents[i]);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001293 }
1294}
1295
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001296void CMD_BUFFER_STATE::RecordBarriers(uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1297 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1298 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) {
1299 if (dev_data->disabled[command_buffer_state]) return;
Jeremy Gebben29110d22021-08-17 13:30:50 -06001300
Jeremy Gebben29110d22021-08-17 13:30:50 -06001301 for (uint32_t i = 0; i < bufferMemoryBarrierCount; i++) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001302 auto buffer_state = dev_data->Get<BUFFER_STATE>(pBufferMemoryBarriers[i].buffer);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001303 if (buffer_state) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001304 AddChild(buffer_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001305 }
1306 }
1307 for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001308 auto image_state = dev_data->Get<IMAGE_STATE>(pImageMemoryBarriers[i].image);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001309 if (image_state) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001310 AddChild(image_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001311 }
1312 }
1313}
1314
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001315void CMD_BUFFER_STATE::RecordBarriers(const VkDependencyInfoKHR &dep_info) {
1316 if (dev_data->disabled[command_buffer_state]) return;
Jeremy Gebben29110d22021-08-17 13:30:50 -06001317
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001318 for (uint32_t i = 0; i < dep_info.bufferMemoryBarrierCount; i++) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001319 auto buffer_state = dev_data->Get<BUFFER_STATE>(dep_info.pBufferMemoryBarriers[i].buffer);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001320 if (buffer_state) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001321 AddChild(buffer_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001322 }
1323 }
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001324 for (uint32_t i = 0; i < dep_info.imageMemoryBarrierCount; i++) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001325 auto image_state = dev_data->Get<IMAGE_STATE>(dep_info.pImageMemoryBarriers[i].image);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001326 if (image_state) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001327 AddChild(image_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001328 }
1329 }
1330}
1331
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001332void CMD_BUFFER_STATE::RecordWriteTimestamp(CMD_TYPE cmd_type, VkPipelineStageFlags2KHR pipelineStage, VkQueryPool queryPool,
1333 uint32_t slot) {
1334 RecordCmd(cmd_type);
1335 if (dev_data->disabled[query_validation]) return;
1336
1337 if (!dev_data->disabled[command_buffer_state]) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06001338 auto pool_state = dev_data->Get<QUERY_POOL_STATE>(queryPool);
Jeremy Gebben610d3a62022-01-01 12:53:17 -07001339 AddChild(pool_state);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001340 }
1341 QueryObject query = {queryPool, slot};
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -06001342 EndQuery(query);
Jeremy Gebben29110d22021-08-17 13:30:50 -06001343}
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001344
Jeremy Gebben57642982021-09-14 14:14:55 -06001345void CMD_BUFFER_STATE::Submit(uint32_t perf_submit_pass) {
1346 VkQueryPool first_pool = VK_NULL_HANDLE;
1347 EventToStageMap local_event_to_stage_map;
1348 QueryMap local_query_to_state_map;
1349 for (auto &function : queryUpdates) {
1350 function(nullptr, /*do_validate*/ false, first_pool, perf_submit_pass, &local_query_to_state_map);
1351 }
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001352
Jeremy Gebben57642982021-09-14 14:14:55 -06001353 for (const auto &query_state_pair : local_query_to_state_map) {
Jeremy Gebben1858ae92021-12-02 11:28:05 -07001354 auto query_pool_state = dev_data->Get<QUERY_POOL_STATE>(query_state_pair.first.pool);
1355 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 -06001356 }
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001357
Jeremy Gebben57642982021-09-14 14:14:55 -06001358 for (const auto &function : eventUpdates) {
Jeremy Gebben332d4dd2022-01-01 12:40:02 -07001359 function(*this, /*do_validate*/ false, &local_event_to_stage_map);
Jeremy Gebben57642982021-09-14 14:14:55 -06001360 }
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001361
Jeremy Gebben57642982021-09-14 14:14:55 -06001362 for (const auto &eventStagePair : local_event_to_stage_map) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06001363 auto event_state = dev_data->Get<EVENT_STATE>(eventStagePair.first);
1364 event_state->stageMask = eventStagePair.second;
Jeremy Gebben57642982021-09-14 14:14:55 -06001365 }
1366}
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001367
ziga-lunarg96c7d822022-02-28 19:39:17 +01001368void CMD_BUFFER_STATE::Retire(uint32_t perf_submit_pass, const std::function<bool(const QueryObject &)>& is_query_updated_after) {
Jeremy Gebben57642982021-09-14 14:14:55 -06001369 // First perform decrement on general case bound objects
1370 for (auto event : writeEventsBeforeWait) {
Jeremy Gebbend177d922021-10-28 13:42:10 -06001371 auto event_state = dev_data->Get<EVENT_STATE>(event);
1372 if (event_state) {
1373 event_state->write_in_use--;
Jeremy Gebben57642982021-09-14 14:14:55 -06001374 }
1375 }
1376 QueryMap local_query_to_state_map;
1377 VkQueryPool first_pool = VK_NULL_HANDLE;
1378 for (auto &function : queryUpdates) {
1379 function(nullptr, /*do_validate*/ false, first_pool, perf_submit_pass, &local_query_to_state_map);
1380 }
1381
1382 for (const auto &query_state_pair : local_query_to_state_map) {
ziga-lunarg96c7d822022-02-28 19:39:17 +01001383 if (query_state_pair.second == QUERYSTATE_ENDED && !is_query_updated_after(query_state_pair.first)) {
Jeremy Gebben1858ae92021-12-02 11:28:05 -07001384 auto query_pool_state = dev_data->Get<QUERY_POOL_STATE>(query_state_pair.first.pool);
1385 query_pool_state->SetQueryState(query_state_pair.first.query, query_state_pair.first.perf_pass, QUERYSTATE_AVAILABLE);
Jeremy Gebben4af0aa82021-09-08 09:35:16 -06001386 }
1387 }
1388}
aitor-lunarga131fca2022-02-17 22:55:55 +01001389
1390void CMD_BUFFER_STATE::UnbindResources() {
aitor-lunarga131fca2022-02-17 22:55:55 +01001391 // Vertex and index buffers
1392 index_buffer_binding.reset();
1393 vertex_buffer_used = false;
1394 current_vertex_buffer_binding_info.vertex_buffer_bindings.clear();
1395
1396 // Push constants
1397 push_constant_data.clear();
1398 push_constant_data_ranges.reset();
1399 push_constant_data_update.clear();
1400 push_constant_pipeline_layout_set = VK_NULL_HANDLE;
1401
Aitor Camacho8ef96de2022-06-13 19:13:03 +02001402 // Reset status of cb to force rebinding of all resources
1403 // Index buffer included
1404 status = CBSTATUS_NONE;
1405
1406 // Pipeline and descriptor sets
1407 lastBound[BindPoint_Graphics].Reset();
Nathaniel Cesario8d5c9d92022-07-25 12:59:16 -06001408}
1409
1410bool CMD_BUFFER_STATE::RasterizationDisabled() const {
1411 auto pipeline = lastBound[BindPoint_Graphics].pipeline_state;
1412 if (pipeline) {
1413 if (pipeline->IsDynamic(VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT)) {
1414 return rasterization_disabled;
1415 } else {
1416 return pipeline->RasterizationDisabled();
1417 }
1418 }
1419
1420 return false;
1421}