Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 1 | /* 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 Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 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 Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 29 | #include "state_tracker.h" |
| 30 | #include "image_state.h" |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 31 | |
Jeremy Gebben | cd7fa28 | 2021-10-27 10:25:32 -0600 | [diff] [blame] | 32 | COMMAND_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 | |
| 41 | void 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 Gebben | 082a983 | 2021-10-28 13:40:11 -0600 | [diff] [blame] | 45 | dev_data->Add(std::move(new_cb)); |
Jeremy Gebben | cd7fa28 | 2021-10-27 10:25:32 -0600 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | void 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 Gebben | 082a983 | 2021-10-28 13:40:11 -0600 | [diff] [blame] | 53 | dev_data->Destroy<CMD_BUFFER_STATE>(iter->first); |
Jeremy Gebben | cd7fa28 | 2021-10-27 10:25:32 -0600 | [diff] [blame] | 54 | commandBuffers.erase(iter); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void COMMAND_POOL_STATE::Reset() { |
| 60 | for (auto &entry : commandBuffers) { |
Jeremy Gebben | 332d4dd | 2022-01-01 12:40:02 -0700 | [diff] [blame] | 61 | auto guard = entry.second->WriteLock(); |
Jeremy Gebben | cd7fa28 | 2021-10-27 10:25:32 -0600 | [diff] [blame] | 62 | entry.second->Reset(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void COMMAND_POOL_STATE::Destroy() { |
| 67 | for (auto &entry : commandBuffers) { |
Jeremy Gebben | 082a983 | 2021-10-28 13:40:11 -0600 | [diff] [blame] | 68 | dev_data->Destroy<CMD_BUFFER_STATE>(entry.first); |
Jeremy Gebben | cd7fa28 | 2021-10-27 10:25:32 -0600 | [diff] [blame] | 69 | } |
| 70 | commandBuffers.clear(); |
| 71 | BASE_NODE::Destroy(); |
| 72 | } |
| 73 | |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 74 | const char *CommandTypeString(CMD_TYPE type) { |
| 75 | // Autogenerated as part of the command_validation.h codegen |
| 76 | return kGeneratedCommandNameList[type]; |
| 77 | } |
| 78 | |
| 79 | VkDynamicState 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; |
| 149 | default: |
| 150 | // CBSTATUS_INDEX_BUFFER_BOUND is not in VkDynamicState |
| 151 | return VK_DYNAMIC_STATE_MAX_ENUM; |
| 152 | } |
| 153 | return VK_DYNAMIC_STATE_MAX_ENUM; |
| 154 | } |
| 155 | |
| 156 | CBStatusFlagBits ConvertToCBStatusFlagBits(VkDynamicState state) { |
| 157 | switch (state) { |
| 158 | case VK_DYNAMIC_STATE_VIEWPORT: |
| 159 | return CBSTATUS_VIEWPORT_SET; |
| 160 | case VK_DYNAMIC_STATE_SCISSOR: |
| 161 | return CBSTATUS_SCISSOR_SET; |
| 162 | case VK_DYNAMIC_STATE_LINE_WIDTH: |
| 163 | return CBSTATUS_LINE_WIDTH_SET; |
| 164 | case VK_DYNAMIC_STATE_DEPTH_BIAS: |
| 165 | return CBSTATUS_DEPTH_BIAS_SET; |
| 166 | case VK_DYNAMIC_STATE_BLEND_CONSTANTS: |
| 167 | return CBSTATUS_BLEND_CONSTANTS_SET; |
| 168 | case VK_DYNAMIC_STATE_DEPTH_BOUNDS: |
| 169 | return CBSTATUS_DEPTH_BOUNDS_SET; |
| 170 | case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK: |
| 171 | return CBSTATUS_STENCIL_READ_MASK_SET; |
| 172 | case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK: |
| 173 | return CBSTATUS_STENCIL_WRITE_MASK_SET; |
| 174 | case VK_DYNAMIC_STATE_STENCIL_REFERENCE: |
| 175 | return CBSTATUS_STENCIL_REFERENCE_SET; |
| 176 | case VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV: |
| 177 | return CBSTATUS_VIEWPORT_W_SCALING_SET; |
| 178 | case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT: |
| 179 | return CBSTATUS_DISCARD_RECTANGLE_SET; |
| 180 | case VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT: |
| 181 | return CBSTATUS_SAMPLE_LOCATIONS_SET; |
| 182 | case VK_DYNAMIC_STATE_VIEWPORT_SHADING_RATE_PALETTE_NV: |
| 183 | return CBSTATUS_SHADING_RATE_PALETTE_SET; |
| 184 | case VK_DYNAMIC_STATE_VIEWPORT_COARSE_SAMPLE_ORDER_NV: |
| 185 | return CBSTATUS_COARSE_SAMPLE_ORDER_SET; |
| 186 | case VK_DYNAMIC_STATE_EXCLUSIVE_SCISSOR_NV: |
| 187 | return CBSTATUS_EXCLUSIVE_SCISSOR_SET; |
| 188 | case VK_DYNAMIC_STATE_LINE_STIPPLE_EXT: |
| 189 | return CBSTATUS_LINE_STIPPLE_SET; |
| 190 | case VK_DYNAMIC_STATE_CULL_MODE_EXT: |
| 191 | return CBSTATUS_CULL_MODE_SET; |
| 192 | case VK_DYNAMIC_STATE_FRONT_FACE_EXT: |
| 193 | return CBSTATUS_FRONT_FACE_SET; |
| 194 | case VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT: |
| 195 | return CBSTATUS_PRIMITIVE_TOPOLOGY_SET; |
| 196 | case VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT: |
| 197 | return CBSTATUS_VIEWPORT_WITH_COUNT_SET; |
| 198 | case VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT: |
| 199 | return CBSTATUS_SCISSOR_WITH_COUNT_SET; |
| 200 | case VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT: |
| 201 | return CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET; |
| 202 | case VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT: |
| 203 | return CBSTATUS_DEPTH_TEST_ENABLE_SET; |
| 204 | case VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT: |
| 205 | return CBSTATUS_DEPTH_WRITE_ENABLE_SET; |
| 206 | case VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT: |
| 207 | return CBSTATUS_DEPTH_COMPARE_OP_SET; |
| 208 | case VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT: |
| 209 | return CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET; |
| 210 | case VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT: |
| 211 | return CBSTATUS_STENCIL_TEST_ENABLE_SET; |
| 212 | case VK_DYNAMIC_STATE_STENCIL_OP_EXT: |
| 213 | return CBSTATUS_STENCIL_OP_SET; |
| 214 | case VK_DYNAMIC_STATE_PATCH_CONTROL_POINTS_EXT: |
| 215 | return CBSTATUS_PATCH_CONTROL_POINTS_SET; |
| 216 | case VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT: |
| 217 | return CBSTATUS_RASTERIZER_DISCARD_ENABLE_SET; |
| 218 | case VK_DYNAMIC_STATE_DEPTH_BIAS_ENABLE_EXT: |
| 219 | return CBSTATUS_DEPTH_BIAS_ENABLE_SET; |
| 220 | case VK_DYNAMIC_STATE_LOGIC_OP_EXT: |
| 221 | return CBSTATUS_LOGIC_OP_SET; |
| 222 | case VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE_EXT: |
| 223 | return CBSTATUS_PRIMITIVE_RESTART_ENABLE_SET; |
| 224 | case VK_DYNAMIC_STATE_VERTEX_INPUT_EXT: |
| 225 | return CBSTATUS_VERTEX_INPUT_SET; |
| 226 | default: |
| 227 | return CBSTATUS_NONE; |
| 228 | } |
| 229 | return CBSTATUS_NONE; |
| 230 | } |
| 231 | |
Jeremy Gebben | cd7fa28 | 2021-10-27 10:25:32 -0600 | [diff] [blame] | 232 | CMD_BUFFER_STATE::CMD_BUFFER_STATE(ValidationStateTracker *dev, VkCommandBuffer cb, const VkCommandBufferAllocateInfo *pCreateInfo, |
| 233 | const COMMAND_POOL_STATE *pool) |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 234 | : REFCOUNTED_NODE(cb, kVulkanObjectTypeCommandBuffer), |
| 235 | createInfo(*pCreateInfo), |
| 236 | command_pool(pool), |
Jeremy Gebben | cd7fa28 | 2021-10-27 10:25:32 -0600 | [diff] [blame] | 237 | dev_data(dev), |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 238 | unprotected(pool->unprotected) { |
| 239 | Reset(); |
| 240 | } |
| 241 | |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 242 | // Get the image viewstate for a given framebuffer attachment |
| 243 | IMAGE_VIEW_STATE *CMD_BUFFER_STATE::GetActiveAttachmentImageViewState(uint32_t index) { |
| 244 | assert(active_attachments && index != VK_ATTACHMENT_UNUSED && (index < active_attachments->size())); |
| 245 | return active_attachments->at(index); |
| 246 | } |
| 247 | |
| 248 | // Get the image viewstate for a given framebuffer attachment |
| 249 | const IMAGE_VIEW_STATE *CMD_BUFFER_STATE::GetActiveAttachmentImageViewState(uint32_t index) const { |
ziga-lunarg | 452a5f9 | 2021-09-08 15:33:03 +0200 | [diff] [blame] | 250 | if (!active_attachments || index == VK_ATTACHMENT_UNUSED || (index >= active_attachments->size())) { |
| 251 | return nullptr; |
| 252 | } |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 253 | return active_attachments->at(index); |
| 254 | } |
| 255 | |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 256 | void CMD_BUFFER_STATE::AddChild(std::shared_ptr<BASE_NODE> &child_node) { |
Jeremy Gebben | c9a2403 | 2021-11-02 11:36:08 -0600 | [diff] [blame] | 257 | assert(child_node); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 258 | if (child_node->AddParent(this)) { |
Jeremy Gebben | c9a2403 | 2021-11-02 11:36:08 -0600 | [diff] [blame] | 259 | object_bindings.insert(child_node); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 263 | void CMD_BUFFER_STATE::RemoveChild(std::shared_ptr<BASE_NODE> &child_node) { |
Jeremy Gebben | c9a2403 | 2021-11-02 11:36:08 -0600 | [diff] [blame] | 264 | assert(child_node); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 265 | child_node->RemoveParent(this); |
Jeremy Gebben | c9a2403 | 2021-11-02 11:36:08 -0600 | [diff] [blame] | 266 | object_bindings.erase(child_node); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 267 | } |
| 268 | |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 269 | // Reset the command buffer state |
| 270 | // Maintain the createInfo and set state to CB_NEW, but clear all other state |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 271 | void CMD_BUFFER_STATE::Reset() { |
| 272 | ResetUse(); |
| 273 | // Reset CB state (note that createInfo is not cleared) |
| 274 | memset(&beginInfo, 0, sizeof(VkCommandBufferBeginInfo)); |
| 275 | memset(&inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo)); |
| 276 | hasDrawCmd = false; |
| 277 | hasTraceRaysCmd = false; |
| 278 | hasBuildAccelerationStructureCmd = false; |
| 279 | hasDispatchCmd = false; |
| 280 | state = CB_NEW; |
| 281 | commandCount = 0; |
| 282 | submitCount = 0; |
| 283 | image_layout_change_count = 1; // Start at 1. 0 is insert value for validation cache versions, s.t. new == dirty |
| 284 | status = 0; |
| 285 | static_status = 0; |
| 286 | inheritedViewportDepths.clear(); |
| 287 | usedViewportScissorCount = 0; |
| 288 | pipelineStaticViewportCount = 0; |
| 289 | pipelineStaticScissorCount = 0; |
| 290 | viewportMask = 0; |
| 291 | viewportWithCountMask = 0; |
| 292 | viewportWithCountCount = 0; |
| 293 | scissorMask = 0; |
| 294 | scissorWithCountMask = 0; |
| 295 | scissorWithCountCount = 0; |
| 296 | trashedViewportMask = 0; |
| 297 | trashedScissorMask = 0; |
| 298 | trashedViewportCount = false; |
| 299 | trashedScissorCount = false; |
| 300 | usedDynamicViewportCount = false; |
| 301 | usedDynamicScissorCount = false; |
| 302 | primitiveTopology = VK_PRIMITIVE_TOPOLOGY_MAX_ENUM; |
| 303 | |
| 304 | activeRenderPassBeginInfo = safe_VkRenderPassBeginInfo(); |
| 305 | activeRenderPass = nullptr; |
| 306 | active_attachments = nullptr; |
| 307 | active_subpasses = nullptr; |
| 308 | attachments_view_states.clear(); |
| 309 | activeSubpassContents = VK_SUBPASS_CONTENTS_INLINE; |
| 310 | activeSubpass = 0; |
| 311 | broken_bindings.clear(); |
| 312 | waitedEvents.clear(); |
| 313 | events.clear(); |
| 314 | writeEventsBeforeWait.clear(); |
| 315 | activeQueries.clear(); |
| 316 | startedQueries.clear(); |
| 317 | image_layout_map.clear(); |
Jeremy Gebben | 4a8881f | 2022-01-10 17:04:30 -0700 | [diff] [blame] | 318 | aliased_image_layout_map.clear(); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 319 | current_vertex_buffer_binding_info.vertex_buffer_bindings.clear(); |
| 320 | vertex_buffer_used = false; |
| 321 | primaryCommandBuffer = VK_NULL_HANDLE; |
| 322 | |
| 323 | linkedCommandBuffers.clear(); |
| 324 | // Remove reverse command buffer links. |
| 325 | Invalidate(true); |
| 326 | |
| 327 | queue_submit_functions.clear(); |
Hans-Kristian Arntzen | 59c2c3f | 2021-06-14 11:40:12 +0200 | [diff] [blame] | 328 | queue_submit_functions_after_render_pass.clear(); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 329 | cmd_execute_commands_functions.clear(); |
| 330 | eventUpdates.clear(); |
| 331 | queryUpdates.clear(); |
| 332 | |
| 333 | // Remove object bindings |
| 334 | for (const auto &obj : object_bindings) { |
Jeremy Gebben | c9a2403 | 2021-11-02 11:36:08 -0600 | [diff] [blame] | 335 | obj->RemoveParent(this); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 336 | } |
| 337 | object_bindings.clear(); |
| 338 | |
| 339 | for (auto &item : lastBound) { |
| 340 | item.Reset(); |
| 341 | } |
| 342 | // Remove this cmdBuffer's reference from each FrameBuffer's CB ref list |
| 343 | for (auto &framebuffer : framebuffers) { |
| 344 | framebuffer->RemoveParent(this); |
| 345 | } |
| 346 | framebuffers.clear(); |
| 347 | activeFramebuffer = VK_NULL_HANDLE; |
| 348 | index_buffer_binding.reset(); |
| 349 | |
| 350 | qfo_transfer_image_barriers.Reset(); |
| 351 | qfo_transfer_buffer_barriers.Reset(); |
| 352 | |
| 353 | // Clean up the label data |
| 354 | debug_label.Reset(); |
| 355 | validate_descriptorsets_in_queuesubmit.clear(); |
| 356 | |
| 357 | // Best practices info |
| 358 | small_indexed_draw_call_count = 0; |
| 359 | |
| 360 | transform_feedback_active = false; |
| 361 | |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 362 | // Clean up the label data |
| 363 | ResetCmdDebugUtilsLabel(dev_data->report_data, commandBuffer()); |
| 364 | |
| 365 | if (dev_data->command_buffer_reset_callback) { |
| 366 | (*dev_data->command_buffer_reset_callback)(commandBuffer()); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Track which resources are in-flight by atomically incrementing their "in_use" count |
| 371 | void CMD_BUFFER_STATE::IncrementResources() { |
| 372 | submitCount++; |
| 373 | |
| 374 | // TODO : We should be able to remove the NULL look-up checks from the code below as long as |
| 375 | // all the corresponding cases are verified to cause CB_INVALID state and the CB_INVALID state |
| 376 | // should then be flagged prior to calling this function |
| 377 | for (auto event : writeEventsBeforeWait) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 378 | auto event_state = dev_data->Get<EVENT_STATE>(event); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 379 | if (event_state) event_state->write_in_use++; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // Discussed in details in https://github.com/KhronosGroup/Vulkan-Docs/issues/1081 |
| 384 | // Internal discussion and CTS were written to prove that this is not called after an incompatible vkCmdBindPipeline |
| 385 | // "Binding a pipeline with a layout that is not compatible with the push constant layout does not disturb the push constant values" |
| 386 | // |
| 387 | // vkCmdBindDescriptorSet has nothing to do with push constants and don't need to call this after neither |
| 388 | // |
| 389 | // Part of this assumes apps at draw/dispath/traceRays/etc time will have it properly compatabile or else other VU will be triggered |
| 390 | void CMD_BUFFER_STATE::ResetPushConstantDataIfIncompatible(const PIPELINE_LAYOUT_STATE *pipeline_layout_state) { |
| 391 | if (pipeline_layout_state == nullptr) { |
| 392 | return; |
| 393 | } |
| 394 | if (push_constant_data_ranges == pipeline_layout_state->push_constant_ranges) { |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | push_constant_data_ranges = pipeline_layout_state->push_constant_ranges; |
| 399 | push_constant_data.clear(); |
| 400 | push_constant_data_update.clear(); |
| 401 | uint32_t size_needed = 0; |
| 402 | for (const auto &push_constant_range : *push_constant_data_ranges) { |
| 403 | auto size = push_constant_range.offset + push_constant_range.size; |
| 404 | size_needed = std::max(size_needed, size); |
| 405 | |
| 406 | auto stage_flags = push_constant_range.stageFlags; |
| 407 | uint32_t bit_shift = 0; |
| 408 | while (stage_flags) { |
| 409 | if (stage_flags & 1) { |
| 410 | VkShaderStageFlagBits flag = static_cast<VkShaderStageFlagBits>(1 << bit_shift); |
| 411 | const auto it = push_constant_data_update.find(flag); |
| 412 | |
| 413 | if (it != push_constant_data_update.end()) { |
| 414 | if (it->second.size() < push_constant_range.offset) { |
| 415 | it->second.resize(push_constant_range.offset, PC_Byte_Not_Set); |
| 416 | } |
| 417 | if (it->second.size() < size) { |
| 418 | it->second.resize(size, PC_Byte_Not_Updated); |
| 419 | } |
| 420 | } else { |
| 421 | std::vector<uint8_t> bytes; |
| 422 | bytes.resize(push_constant_range.offset, PC_Byte_Not_Set); |
| 423 | bytes.resize(size, PC_Byte_Not_Updated); |
| 424 | push_constant_data_update[flag] = bytes; |
| 425 | } |
| 426 | } |
| 427 | stage_flags = stage_flags >> 1; |
| 428 | ++bit_shift; |
| 429 | } |
| 430 | } |
| 431 | push_constant_data.resize(size_needed, 0); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void CMD_BUFFER_STATE::Destroy() { |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 435 | // Allow any derived class to clean up command buffer state |
| 436 | if (dev_data->command_buffer_reset_callback) { |
| 437 | (*dev_data->command_buffer_reset_callback)(commandBuffer()); |
| 438 | } |
| 439 | if (dev_data->command_buffer_free_callback) { |
| 440 | (*dev_data->command_buffer_free_callback)(commandBuffer()); |
| 441 | } |
| 442 | |
| 443 | // Remove the cb debug labels |
| 444 | EraseCmdDebugUtilsLabel(dev_data->report_data, commandBuffer()); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 445 | Reset(); |
| 446 | BASE_NODE::Destroy(); |
| 447 | } |
| 448 | |
Jeremy Gebben | c9a2403 | 2021-11-02 11:36:08 -0600 | [diff] [blame] | 449 | void CMD_BUFFER_STATE::NotifyInvalidate(const BASE_NODE::NodeList &invalid_nodes, bool unlink) { |
Jeremy Gebben | 332d4dd | 2022-01-01 12:40:02 -0700 | [diff] [blame] | 450 | { |
| 451 | auto guard = WriteLock(); |
| 452 | if (state == CB_RECORDING) { |
| 453 | state = CB_INVALID_INCOMPLETE; |
| 454 | } else if (state == CB_RECORDED) { |
| 455 | state = CB_INVALID_COMPLETE; |
| 456 | } |
| 457 | assert(!invalid_nodes.empty()); |
| 458 | LogObjectList log_list; |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 459 | for (auto &obj : invalid_nodes) { |
Jeremy Gebben | 332d4dd | 2022-01-01 12:40:02 -0700 | [diff] [blame] | 460 | log_list.object_list.emplace_back(obj->Handle()); |
| 461 | } |
| 462 | broken_bindings.emplace(invalid_nodes[0]->Handle(), log_list); |
| 463 | |
| 464 | if (unlink) { |
| 465 | for (auto &obj : invalid_nodes) { |
| 466 | object_bindings.erase(obj); |
| 467 | switch (obj->Type()) { |
| 468 | case kVulkanObjectTypeCommandBuffer: |
| 469 | linkedCommandBuffers.erase(static_cast<CMD_BUFFER_STATE *>(obj.get())); |
| 470 | break; |
| 471 | case kVulkanObjectTypeImage: |
| 472 | image_layout_map.erase(static_cast<IMAGE_STATE *>(obj.get())); |
| 473 | break; |
| 474 | default: |
| 475 | break; |
| 476 | } |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
Jeremy Gebben | c9a2403 | 2021-11-02 11:36:08 -0600 | [diff] [blame] | 480 | BASE_NODE::NotifyInvalidate(invalid_nodes, unlink); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 481 | } |
| 482 | |
ziga-lunarg | 189ae5d | 2021-10-19 13:09:58 +0200 | [diff] [blame] | 483 | const CommandBufferImageLayoutMap& CMD_BUFFER_STATE::GetImageSubresourceLayoutMap() const { return image_layout_map; } |
| 484 | |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 485 | // The const variant only need the image as it is the key for the map |
Jeremy Gebben | 6335df6 | 2021-11-01 10:50:13 -0600 | [diff] [blame] | 486 | const ImageSubresourceLayoutMap *CMD_BUFFER_STATE::GetImageSubresourceLayoutMap(const IMAGE_STATE &image_state) const { |
| 487 | auto it = image_layout_map.find(&image_state); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 488 | if (it == image_layout_map.cend()) { |
| 489 | return nullptr; |
| 490 | } |
Jeremy Gebben | 4a8881f | 2022-01-10 17:04:30 -0700 | [diff] [blame] | 491 | return it->second.get(); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // The non-const variant only needs the image state, as the factory requires it to construct a new entry |
| 495 | ImageSubresourceLayoutMap *CMD_BUFFER_STATE::GetImageSubresourceLayoutMap(const IMAGE_STATE &image_state) { |
Jeremy Gebben | 6335df6 | 2021-11-01 10:50:13 -0600 | [diff] [blame] | 496 | auto &layout_map = image_layout_map[&image_state]; |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 497 | if (!layout_map) { |
Jeremy Gebben | 4a8881f | 2022-01-10 17:04:30 -0700 | [diff] [blame] | 498 | // Make sure we don't create a nullptr keyed entry for a zombie Image |
| 499 | if (image_state.Destroyed() || !image_state.layout_range_map) { |
| 500 | return nullptr; |
| 501 | } |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 502 | // Was an empty slot... fill it in. |
Jeremy Gebben | 4a8881f | 2022-01-10 17:04:30 -0700 | [diff] [blame] | 503 | if (image_state.CanAlias()) { |
| 504 | // Aliasing images need to share the same local layout map. |
| 505 | // Since they use the same global layout state, use it as a key |
| 506 | // for the local state. We don't need a lock on the global range |
| 507 | // map to do a lookup based on its pointer. |
| 508 | const auto *global_layout_map = image_state.layout_range_map.get(); |
| 509 | auto iter = aliased_image_layout_map.find(global_layout_map); |
| 510 | if (iter != aliased_image_layout_map.end()) { |
| 511 | layout_map = iter->second; |
| 512 | } else { |
| 513 | layout_map = std::make_shared<ImageSubresourceLayoutMap>(image_state); |
| 514 | // Save the local layout map for the next aliased image. |
| 515 | // The global layout map pointer is only used as a key into the local lookup |
| 516 | // table so it doesn't need to be locked. |
| 517 | aliased_image_layout_map.emplace(global_layout_map, layout_map); |
| 518 | } |
| 519 | |
| 520 | } else { |
| 521 | layout_map = std::make_shared<ImageSubresourceLayoutMap>(image_state); |
| 522 | } |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 523 | } |
Jeremy Gebben | 4a8881f | 2022-01-10 17:04:30 -0700 | [diff] [blame] | 524 | return layout_map.get(); |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 525 | } |
| 526 | |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 527 | static bool SetQueryState(QueryObject object, QueryState value, QueryMap *localQueryToStateMap) { |
| 528 | (*localQueryToStateMap)[object] = value; |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | void CMD_BUFFER_STATE::BeginQuery(const QueryObject &query_obj) { |
| 533 | activeQueries.insert(query_obj); |
| 534 | startedQueries.insert(query_obj); |
| 535 | queryUpdates.emplace_back([query_obj](const ValidationStateTracker *device_data, bool do_validate, |
| 536 | VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass, QueryMap *localQueryToStateMap) { |
| 537 | SetQueryState(QueryObject(query_obj, perfQueryPass), QUERYSTATE_RUNNING, localQueryToStateMap); |
| 538 | return false; |
| 539 | }); |
ziga-lunarg | 96c7d82 | 2022-02-28 19:39:17 +0100 | [diff] [blame] | 540 | updatedQueries.insert(query_obj); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void CMD_BUFFER_STATE::EndQuery(const QueryObject &query_obj) { |
| 544 | activeQueries.erase(query_obj); |
| 545 | queryUpdates.emplace_back([query_obj](const ValidationStateTracker *device_data, bool do_validate, |
| 546 | VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass, QueryMap *localQueryToStateMap) { |
| 547 | return SetQueryState(QueryObject(query_obj, perfQueryPass), QUERYSTATE_ENDED, localQueryToStateMap); |
| 548 | }); |
ziga-lunarg | 96c7d82 | 2022-02-28 19:39:17 +0100 | [diff] [blame] | 549 | updatedQueries.insert(query_obj); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | static bool SetQueryStateMulti(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount, uint32_t perfPass, QueryState value, |
| 553 | QueryMap *localQueryToStateMap) { |
| 554 | for (uint32_t i = 0; i < queryCount; i++) { |
| 555 | QueryObject object = QueryObject(QueryObject(queryPool, firstQuery + i), perfPass); |
| 556 | (*localQueryToStateMap)[object] = value; |
| 557 | } |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | void CMD_BUFFER_STATE::EndQueries(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount) { |
| 562 | for (uint32_t slot = firstQuery; slot < (firstQuery + queryCount); slot++) { |
| 563 | QueryObject query = {queryPool, slot}; |
| 564 | activeQueries.erase(query); |
ziga-lunarg | 96c7d82 | 2022-02-28 19:39:17 +0100 | [diff] [blame] | 565 | updatedQueries.insert(query); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 566 | } |
| 567 | queryUpdates.emplace_back([queryPool, firstQuery, queryCount](const ValidationStateTracker *device_data, bool do_validate, |
| 568 | VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass, |
| 569 | QueryMap *localQueryToStateMap) { |
Lars-Ivar Hesselberg Simonsen | 1a5646f | 2021-10-25 15:06:16 +0200 | [diff] [blame] | 570 | return SetQueryStateMulti(queryPool, firstQuery, queryCount, perfQueryPass, QUERYSTATE_ENDED, localQueryToStateMap); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 571 | }); |
| 572 | } |
| 573 | |
| 574 | void CMD_BUFFER_STATE::ResetQueryPool(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount) { |
| 575 | for (uint32_t slot = firstQuery; slot < (firstQuery + queryCount); slot++) { |
| 576 | QueryObject query = {queryPool, slot}; |
| 577 | resetQueries.insert(query); |
ziga-lunarg | 96c7d82 | 2022-02-28 19:39:17 +0100 | [diff] [blame] | 578 | updatedQueries.insert(query); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | queryUpdates.emplace_back([queryPool, firstQuery, queryCount](const ValidationStateTracker *device_data, bool do_validate, |
| 582 | VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass, |
| 583 | QueryMap *localQueryToStateMap) { |
| 584 | return SetQueryStateMulti(queryPool, firstQuery, queryCount, perfQueryPass, QUERYSTATE_RESET, localQueryToStateMap); |
| 585 | }); |
| 586 | } |
| 587 | |
| 588 | void UpdateSubpassAttachments(const safe_VkSubpassDescription2 &subpass, std::vector<SUBPASS_INFO> &subpasses) { |
| 589 | for (uint32_t index = 0; index < subpass.inputAttachmentCount; ++index) { |
| 590 | const uint32_t attachment_index = subpass.pInputAttachments[index].attachment; |
| 591 | if (attachment_index != VK_ATTACHMENT_UNUSED) { |
| 592 | subpasses[attachment_index].used = true; |
| 593 | subpasses[attachment_index].usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 594 | subpasses[attachment_index].layout = subpass.pInputAttachments[index].layout; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | for (uint32_t index = 0; index < subpass.colorAttachmentCount; ++index) { |
| 599 | const uint32_t attachment_index = subpass.pColorAttachments[index].attachment; |
| 600 | if (attachment_index != VK_ATTACHMENT_UNUSED) { |
| 601 | subpasses[attachment_index].used = true; |
| 602 | subpasses[attachment_index].usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 603 | subpasses[attachment_index].layout = subpass.pColorAttachments[index].layout; |
| 604 | } |
| 605 | if (subpass.pResolveAttachments) { |
| 606 | const uint32_t attachment_index2 = subpass.pResolveAttachments[index].attachment; |
| 607 | if (attachment_index2 != VK_ATTACHMENT_UNUSED) { |
| 608 | subpasses[attachment_index2].used = true; |
| 609 | subpasses[attachment_index2].usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 610 | subpasses[attachment_index2].layout = subpass.pResolveAttachments[index].layout; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | if (subpass.pDepthStencilAttachment) { |
| 616 | const uint32_t attachment_index = subpass.pDepthStencilAttachment->attachment; |
| 617 | if (attachment_index != VK_ATTACHMENT_UNUSED) { |
| 618 | subpasses[attachment_index].used = true; |
| 619 | subpasses[attachment_index].usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 620 | subpasses[attachment_index].layout = subpass.pDepthStencilAttachment->layout; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | void CMD_BUFFER_STATE::UpdateAttachmentsView(const VkRenderPassBeginInfo *pRenderPassBegin) { |
| 626 | auto &attachments = *(active_attachments.get()); |
| 627 | const bool imageless = (activeFramebuffer->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) ? true : false; |
| 628 | const VkRenderPassAttachmentBeginInfo *attachment_info_struct = nullptr; |
| 629 | if (pRenderPassBegin) attachment_info_struct = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext); |
| 630 | |
| 631 | for (uint32_t i = 0; i < attachments.size(); ++i) { |
| 632 | if (imageless) { |
| 633 | if (attachment_info_struct && i < attachment_info_struct->attachmentCount) { |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 634 | auto res = attachments_view_states.insert(dev_data->Get<IMAGE_VIEW_STATE>(attachment_info_struct->pAttachments[i])); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 635 | attachments[i] = res.first->get(); |
| 636 | } |
| 637 | } else { |
| 638 | auto res = attachments_view_states.insert(activeFramebuffer->attachments_view_state[i]); |
| 639 | attachments[i] = res.first->get(); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
Jeremy Gebben | 10a0ed1 | 2021-08-17 09:54:29 -0600 | [diff] [blame] | 644 | void CMD_BUFFER_STATE::BeginRenderPass(CMD_TYPE cmd_type, const VkRenderPassBeginInfo *pRenderPassBegin, |
| 645 | const VkSubpassContents contents) { |
| 646 | RecordCmd(cmd_type); |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 647 | activeFramebuffer = dev_data->Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer); |
| 648 | activeRenderPass = dev_data->Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 649 | activeRenderPassBeginInfo = safe_VkRenderPassBeginInfo(pRenderPassBegin); |
| 650 | activeSubpass = 0; |
| 651 | activeSubpassContents = contents; |
| 652 | |
aitor-lunarg | a131fca | 2022-02-17 22:55:55 +0100 | [diff] [blame] | 653 | if (activeRenderPass) { |
| 654 | // Connect this RP to cmdBuffer |
| 655 | if (!dev_data->disabled[command_buffer_state]) { |
| 656 | AddChild(activeRenderPass); |
| 657 | } |
| 658 | |
| 659 | // Spec states that after BeginRenderPass all resources should be rebound |
| 660 | if (activeRenderPass->has_multiview_enabled) { |
| 661 | UnbindResources(); |
| 662 | } |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | auto chained_device_group_struct = LvlFindInChain<VkDeviceGroupRenderPassBeginInfo>(pRenderPassBegin->pNext); |
| 666 | if (chained_device_group_struct) { |
| 667 | active_render_pass_device_mask = chained_device_group_struct->deviceMask; |
| 668 | } else { |
| 669 | active_render_pass_device_mask = initial_device_mask; |
| 670 | } |
| 671 | |
| 672 | active_subpasses = nullptr; |
| 673 | active_attachments = nullptr; |
| 674 | |
| 675 | if (activeFramebuffer) { |
| 676 | framebuffers.insert(activeFramebuffer); |
| 677 | |
| 678 | // Set cb_state->active_subpasses |
| 679 | active_subpasses = std::make_shared<std::vector<SUBPASS_INFO>>(activeFramebuffer->createInfo.attachmentCount); |
| 680 | const auto &subpass = activeRenderPass->createInfo.pSubpasses[activeSubpass]; |
| 681 | UpdateSubpassAttachments(subpass, *active_subpasses); |
| 682 | |
| 683 | // Set cb_state->active_attachments & cb_state->attachments_view_states |
| 684 | active_attachments = std::make_shared<std::vector<IMAGE_VIEW_STATE *>>(activeFramebuffer->createInfo.attachmentCount); |
| 685 | UpdateAttachmentsView(pRenderPassBegin); |
| 686 | |
| 687 | // Connect this framebuffer and its children to this cmdBuffer |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 688 | AddChild(activeFramebuffer); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Jeremy Gebben | 10a0ed1 | 2021-08-17 09:54:29 -0600 | [diff] [blame] | 692 | void CMD_BUFFER_STATE::NextSubpass(CMD_TYPE cmd_type, VkSubpassContents contents) { |
| 693 | RecordCmd(cmd_type); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 694 | activeSubpass++; |
| 695 | activeSubpassContents = contents; |
| 696 | |
| 697 | // Update cb_state->active_subpasses |
aitor-lunarg | a131fca | 2022-02-17 22:55:55 +0100 | [diff] [blame] | 698 | if (activeRenderPass) { |
| 699 | if (activeFramebuffer) { |
| 700 | active_subpasses = nullptr; |
| 701 | active_subpasses = std::make_shared<std::vector<SUBPASS_INFO>>(activeFramebuffer->createInfo.attachmentCount); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 702 | |
ziga-lunarg | 31a3e77 | 2022-03-22 11:48:46 +0100 | [diff] [blame^] | 703 | if (activeSubpass < activeRenderPass->createInfo.subpassCount) { |
| 704 | const auto &subpass = activeRenderPass->createInfo.pSubpasses[activeSubpass]; |
| 705 | UpdateSubpassAttachments(subpass, *active_subpasses); |
| 706 | } |
aitor-lunarg | a131fca | 2022-02-17 22:55:55 +0100 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | // Spec states that after NextSubpass all resources should be rebound |
| 710 | if (activeRenderPass->has_multiview_enabled) { |
| 711 | UnbindResources(); |
| 712 | } |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
Jeremy Gebben | 10a0ed1 | 2021-08-17 09:54:29 -0600 | [diff] [blame] | 716 | void CMD_BUFFER_STATE::EndRenderPass(CMD_TYPE cmd_type) { |
| 717 | RecordCmd(cmd_type); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 718 | activeRenderPass = nullptr; |
| 719 | active_attachments = nullptr; |
| 720 | active_subpasses = nullptr; |
| 721 | activeSubpass = 0; |
| 722 | activeFramebuffer = VK_NULL_HANDLE; |
| 723 | } |
| 724 | |
Tony-LunarG | 40b3388 | 2021-12-02 12:40:11 -0700 | [diff] [blame] | 725 | void CMD_BUFFER_STATE::BeginRendering(CMD_TYPE cmd_type, const VkRenderingInfo *pRenderingInfo) { |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 726 | RecordCmd(cmd_type); |
Tony-LunarG | 40b3388 | 2021-12-02 12:40:11 -0700 | [diff] [blame] | 727 | begin_rendering_func_name = CommandTypeString(cmd_type); |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 728 | activeRenderPass = std::make_shared<RENDER_PASS_STATE>(pRenderingInfo); |
| 729 | |
| 730 | auto chained_device_group_struct = LvlFindInChain<VkDeviceGroupRenderPassBeginInfo>(pRenderingInfo->pNext); |
| 731 | if (chained_device_group_struct) { |
| 732 | active_render_pass_device_mask = chained_device_group_struct->deviceMask; |
| 733 | } else { |
| 734 | active_render_pass_device_mask = initial_device_mask; |
| 735 | } |
| 736 | |
| 737 | activeSubpassContents = ((pRenderingInfo->flags & VK_RENDERING_CONTENTS_SECONDARY_COMMAND_BUFFERS_BIT_KHR) ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS : VK_SUBPASS_CONTENTS_INLINE); |
| 738 | |
| 739 | active_attachments = nullptr; |
| 740 | uint32_t attachment_count = (pRenderingInfo->colorAttachmentCount + 2) * 2; |
| 741 | |
| 742 | // Set cb_state->active_attachments & cb_state->attachments_view_states |
| 743 | active_attachments = std::make_shared<std::vector<IMAGE_VIEW_STATE *>>(attachment_count); |
| 744 | auto &attachments = *(active_attachments.get()); |
| 745 | |
| 746 | for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; ++i) { |
| 747 | auto& colorAttachment = attachments[GetDynamicColorAttachmentImageIndex(i)]; |
| 748 | auto& colorResolveAttachment = attachments[GetDynamicColorResolveAttachmentImageIndex(i)]; |
| 749 | colorAttachment = nullptr; |
| 750 | colorResolveAttachment = nullptr; |
| 751 | |
| 752 | if (pRenderingInfo->pColorAttachments[i].imageView != VK_NULL_HANDLE) { |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 753 | auto res = |
| 754 | attachments_view_states.insert(dev_data->Get<IMAGE_VIEW_STATE>(pRenderingInfo->pColorAttachments[i].imageView)); |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 755 | colorAttachment = res.first->get(); |
| 756 | if (pRenderingInfo->pColorAttachments[i].resolveMode != VK_RESOLVE_MODE_NONE && |
| 757 | pRenderingInfo->pColorAttachments[i].resolveImageView != VK_NULL_HANDLE) { |
| 758 | colorResolveAttachment = res.first->get(); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | if (pRenderingInfo->pDepthAttachment && pRenderingInfo->pDepthAttachment->imageView != VK_NULL_HANDLE) { |
| 764 | auto& depthAttachment = attachments[GetDynamicDepthAttachmentImageIndex()]; |
| 765 | auto& depthResolveAttachment = attachments[GetDynamicDepthResolveAttachmentImageIndex()]; |
| 766 | depthAttachment = nullptr; |
| 767 | depthResolveAttachment = nullptr; |
| 768 | |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 769 | auto res = attachments_view_states.insert(dev_data->Get<IMAGE_VIEW_STATE>(pRenderingInfo->pDepthAttachment->imageView)); |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 770 | depthAttachment = res.first->get(); |
| 771 | if (pRenderingInfo->pDepthAttachment->resolveMode != VK_RESOLVE_MODE_NONE && |
| 772 | pRenderingInfo->pDepthAttachment->resolveImageView != VK_NULL_HANDLE) { |
| 773 | depthResolveAttachment = res.first->get(); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | if (pRenderingInfo->pStencilAttachment && pRenderingInfo->pStencilAttachment->imageView != VK_NULL_HANDLE) { |
| 778 | auto& stencilAttachment = attachments[GetDynamicStencilAttachmentImageIndex()]; |
| 779 | auto& stencilResolveAttachment = attachments[GetDynamicStencilResolveAttachmentImageIndex()]; |
| 780 | stencilAttachment = nullptr; |
| 781 | stencilResolveAttachment = nullptr; |
| 782 | |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 783 | auto res = attachments_view_states.insert(dev_data->Get<IMAGE_VIEW_STATE>(pRenderingInfo->pStencilAttachment->imageView)); |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 784 | stencilAttachment = res.first->get(); |
| 785 | if (pRenderingInfo->pStencilAttachment->resolveMode != VK_RESOLVE_MODE_NONE && |
| 786 | pRenderingInfo->pStencilAttachment->resolveImageView != VK_NULL_HANDLE) { |
| 787 | stencilResolveAttachment = res.first->get(); |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 792 | void CMD_BUFFER_STATE::Begin(const VkCommandBufferBeginInfo *pBeginInfo) { |
| 793 | if (CB_RECORDED == state || CB_INVALID_COMPLETE == state) { |
| 794 | Reset(); |
| 795 | } |
| 796 | // Set updated state here in case implicit reset occurs above |
| 797 | state = CB_RECORDING; |
| 798 | beginInfo = *pBeginInfo; |
| 799 | if (beginInfo.pInheritanceInfo && (createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY)) { |
| 800 | inheritanceInfo = *(beginInfo.pInheritanceInfo); |
| 801 | beginInfo.pInheritanceInfo = &inheritanceInfo; |
| 802 | // If we are a secondary command-buffer and inheriting. Update the items we should inherit. |
| 803 | if ((createInfo.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY) && |
| 804 | (beginInfo.flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) { |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 805 | if (beginInfo.pInheritanceInfo->renderPass) { |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 806 | activeRenderPass = dev_data->Get<RENDER_PASS_STATE>(beginInfo.pInheritanceInfo->renderPass); |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 807 | activeSubpass = beginInfo.pInheritanceInfo->subpass; |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 808 | |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 809 | if (beginInfo.pInheritanceInfo->framebuffer) { |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 810 | activeFramebuffer = dev_data->Get<FRAMEBUFFER_STATE>(beginInfo.pInheritanceInfo->framebuffer); |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 811 | active_subpasses = nullptr; |
| 812 | active_attachments = nullptr; |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 813 | |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 814 | if (activeFramebuffer) { |
| 815 | framebuffers.insert(activeFramebuffer); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 816 | |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 817 | // Set active_subpasses |
| 818 | active_subpasses = std::make_shared<std::vector<SUBPASS_INFO>>(activeFramebuffer->createInfo.attachmentCount); |
| 819 | const auto& subpass = activeRenderPass->createInfo.pSubpasses[activeSubpass]; |
| 820 | UpdateSubpassAttachments(subpass, *active_subpasses); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 821 | |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 822 | // Set active_attachments & attachments_view_states |
| 823 | active_attachments = |
| 824 | std::make_shared<std::vector<IMAGE_VIEW_STATE*>>(activeFramebuffer->createInfo.attachmentCount); |
| 825 | UpdateAttachmentsView(nullptr); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 826 | |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 827 | // Connect this framebuffer and its children to this cmdBuffer |
| 828 | if (!dev_data->disabled[command_buffer_state]) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 829 | AddChild(activeFramebuffer); |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 830 | } |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | } |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 834 | else |
| 835 | { |
Tony-LunarG | 40b3388 | 2021-12-02 12:40:11 -0700 | [diff] [blame] | 836 | auto inheritance_rendering_info = lvl_find_in_chain<VkCommandBufferInheritanceRenderingInfo>(beginInfo.pInheritanceInfo->pNext); |
amhagan | a448ea5 | 2021-11-02 14:09:14 -0400 | [diff] [blame] | 837 | if (inheritance_rendering_info) { |
| 838 | activeRenderPass = std::make_shared<RENDER_PASS_STATE>(inheritance_rendering_info); |
| 839 | } |
| 840 | } |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 841 | |
| 842 | // Check for VkCommandBufferInheritanceViewportScissorInfoNV (VK_NV_inherited_viewport_scissor) |
| 843 | auto p_inherited_viewport_scissor_info = |
| 844 | LvlFindInChain<VkCommandBufferInheritanceViewportScissorInfoNV>(beginInfo.pInheritanceInfo->pNext); |
| 845 | if (p_inherited_viewport_scissor_info != nullptr && p_inherited_viewport_scissor_info->viewportScissor2D) { |
| 846 | auto pViewportDepths = p_inherited_viewport_scissor_info->pViewportDepths; |
| 847 | inheritedViewportDepths.assign(pViewportDepths, |
| 848 | pViewportDepths + p_inherited_viewport_scissor_info->viewportDepthCount); |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | auto chained_device_group_struct = LvlFindInChain<VkDeviceGroupCommandBufferBeginInfo>(pBeginInfo->pNext); |
| 854 | if (chained_device_group_struct) { |
| 855 | initial_device_mask = chained_device_group_struct->deviceMask; |
| 856 | } else { |
| 857 | initial_device_mask = (1 << dev_data->physical_device_count) - 1; |
| 858 | } |
| 859 | performance_lock_acquired = dev_data->performance_lock_acquired; |
ziga-lunarg | 96c7d82 | 2022-02-28 19:39:17 +0100 | [diff] [blame] | 860 | updatedQueries.clear(); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | void CMD_BUFFER_STATE::End(VkResult result) { |
| 864 | // Cached validation is specific to a specific recording of a specific command buffer. |
Jeremy Gebben | 87db52f | 2021-10-14 13:55:09 -0600 | [diff] [blame] | 865 | descriptorset_cache.clear(); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 866 | validated_descriptor_sets.clear(); |
| 867 | if (VK_SUCCESS == result) { |
| 868 | state = CB_RECORDED; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | void CMD_BUFFER_STATE::ExecuteCommands(uint32_t commandBuffersCount, const VkCommandBuffer *pCommandBuffers) { |
Jeremy Gebben | 10a0ed1 | 2021-08-17 09:54:29 -0600 | [diff] [blame] | 873 | RecordCmd(CMD_EXECUTECOMMANDS); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 874 | for (uint32_t i = 0; i < commandBuffersCount; i++) { |
Jeremy Gebben | 332d4dd | 2022-01-01 12:40:02 -0700 | [diff] [blame] | 875 | auto sub_cb_state = dev_data->GetWrite<CMD_BUFFER_STATE>(pCommandBuffers[i]); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 876 | assert(sub_cb_state); |
| 877 | if (!(sub_cb_state->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) { |
| 878 | if (beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) { |
| 879 | // TODO: Because this is a state change, clearing the VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT needs to be moved |
| 880 | // from the validation step to the recording step |
| 881 | beginInfo.flags &= ~VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | // Propagate inital layout and current layout state to the primary cmd buffer |
| 886 | // NOTE: The update/population of the image_layout_map is done in CoreChecks, but for other classes derived from |
| 887 | // ValidationStateTracker these maps will be empty, so leaving the propagation in the the state tracker should be a no-op |
| 888 | // for those other classes. |
| 889 | for (const auto &sub_layout_map_entry : sub_cb_state->image_layout_map) { |
Jeremy Gebben | 6335df6 | 2021-11-01 10:50:13 -0600 | [diff] [blame] | 890 | const auto *image_state = sub_layout_map_entry.first; |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 891 | |
| 892 | auto *cb_subres_map = GetImageSubresourceLayoutMap(*image_state); |
Jeremy Gebben | 4a8881f | 2022-01-10 17:04:30 -0700 | [diff] [blame] | 893 | if (cb_subres_map) { |
| 894 | const auto &sub_cb_subres_map = sub_layout_map_entry.second; |
| 895 | cb_subres_map->UpdateFrom(*sub_cb_subres_map); |
| 896 | } |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | sub_cb_state->primaryCommandBuffer = commandBuffer(); |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 900 | linkedCommandBuffers.insert(sub_cb_state.get()); |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 901 | AddChild(sub_cb_state); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 902 | for (auto &function : sub_cb_state->queryUpdates) { |
| 903 | queryUpdates.push_back(function); |
| 904 | } |
| 905 | for (auto &function : sub_cb_state->queue_submit_functions) { |
| 906 | queue_submit_functions.push_back(function); |
| 907 | } |
| 908 | |
| 909 | // State is trashed after executing secondary command buffers. |
| 910 | // Importantly, this function runs after CoreChecks::PreCallValidateCmdExecuteCommands. |
| 911 | trashedViewportMask = ~uint32_t(0); |
| 912 | trashedScissorMask = ~uint32_t(0); |
| 913 | trashedViewportCount = true; |
| 914 | trashedScissorCount = true; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | void CMD_BUFFER_STATE::PushDescriptorSetState(VkPipelineBindPoint pipelineBindPoint, PIPELINE_LAYOUT_STATE *pipeline_layout, |
| 919 | uint32_t set, uint32_t descriptorWriteCount, |
| 920 | const VkWriteDescriptorSet *pDescriptorWrites) { |
| 921 | // Short circuit invalid updates |
| 922 | if (!pipeline_layout || (set >= pipeline_layout->set_layouts.size()) || !pipeline_layout->set_layouts[set] || |
| 923 | !pipeline_layout->set_layouts[set]->IsPushDescriptor()) { |
| 924 | return; |
| 925 | } |
| 926 | |
| 927 | // We need a descriptor set to update the bindings with, compatible with the passed layout |
| 928 | const auto &dsl = pipeline_layout->set_layouts[set]; |
| 929 | const auto lv_bind_point = ConvertToLvlBindPoint(pipelineBindPoint); |
| 930 | auto &last_bound = lastBound[lv_bind_point]; |
| 931 | auto &push_descriptor_set = last_bound.push_descriptor_set; |
| 932 | // If we are disturbing the current push_desriptor_set clear it |
| 933 | if (!push_descriptor_set || !CompatForSet(set, last_bound, pipeline_layout->compat_for_set)) { |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 934 | last_bound.UnbindAndResetPushDescriptorSet( |
| 935 | this, std::make_shared<cvdescriptorset::DescriptorSet>(VK_NULL_HANDLE, nullptr, dsl, 0, dev_data)); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 936 | } |
| 937 | |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 938 | UpdateLastBoundDescriptorSets(pipelineBindPoint, pipeline_layout, set, 1, nullptr, push_descriptor_set, 0, nullptr); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 939 | last_bound.pipeline_layout = pipeline_layout->layout(); |
| 940 | |
| 941 | // Now that we have either the new or extant push_descriptor set ... do the write updates against it |
| 942 | push_descriptor_set->PerformPushDescriptorsUpdate(dev_data, descriptorWriteCount, pDescriptorWrites); |
| 943 | } |
| 944 | |
| 945 | // Generic function to handle state update for all CmdDraw* and CmdDispatch* type functions |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 946 | void CMD_BUFFER_STATE::UpdateStateCmdDrawDispatchType(CMD_TYPE cmd_type, VkPipelineBindPoint bind_point) { |
| 947 | UpdateDrawState(cmd_type, bind_point); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 948 | hasDispatchCmd = true; |
| 949 | } |
| 950 | |
| 951 | // Generic function to handle state update for all CmdDraw* type functions |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 952 | void CMD_BUFFER_STATE::UpdateStateCmdDrawType(CMD_TYPE cmd_type, VkPipelineBindPoint bind_point) { |
| 953 | UpdateStateCmdDrawDispatchType(cmd_type, bind_point); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 954 | hasDrawCmd = true; |
| 955 | |
| 956 | // Update the consumed viewport/scissor count. |
| 957 | uint32_t &used = usedViewportScissorCount; |
| 958 | used = std::max(used, pipelineStaticViewportCount); |
| 959 | used = std::max(used, pipelineStaticScissorCount); |
| 960 | usedDynamicViewportCount |= !!(dynamic_status & CBSTATUS_VIEWPORT_WITH_COUNT_SET); // !! silences MSVC warn |
| 961 | usedDynamicScissorCount |= !!(dynamic_status & CBSTATUS_SCISSOR_WITH_COUNT_SET); |
| 962 | } |
| 963 | |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 964 | void CMD_BUFFER_STATE::UpdateDrawState(CMD_TYPE cmd_type, const VkPipelineBindPoint bind_point) { |
Jeremy Gebben | 10a0ed1 | 2021-08-17 09:54:29 -0600 | [diff] [blame] | 965 | RecordCmd(cmd_type); |
| 966 | |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 967 | const auto lv_bind_point = ConvertToLvlBindPoint(bind_point); |
| 968 | auto &state = lastBound[lv_bind_point]; |
| 969 | PIPELINE_STATE *pipe = state.pipeline_state; |
| 970 | if (VK_NULL_HANDLE != state.pipeline_layout) { |
| 971 | for (const auto &set_binding_pair : pipe->active_slots) { |
| 972 | uint32_t set_index = set_binding_pair.first; |
Charles Baker | a263fee | 2022-02-18 12:45:21 +1300 | [diff] [blame] | 973 | if (set_index >= state.per_set.size()) { |
| 974 | continue; |
| 975 | } |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 976 | // Pull the set node |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 977 | auto &descriptor_set = state.per_set[set_index].bound_descriptor_set; |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 978 | |
| 979 | // For the "bindless" style resource usage with many descriptors, need to optimize command <-> descriptor binding |
| 980 | |
| 981 | // TODO: If recreating the reduced_map here shows up in profilinging, need to find a way of sharing with the |
| 982 | // Validate pass. Though in the case of "many" descriptors, typically the descriptor count >> binding count |
| 983 | cvdescriptorset::PrefilterBindRequestMap reduced_map(*descriptor_set, set_binding_pair.second); |
| 984 | const auto &binding_req_map = reduced_map.FilteredMap(*this, *pipe); |
| 985 | |
| 986 | if (reduced_map.IsManyDescriptors()) { |
| 987 | // Only update validate binding tags if we meet the "many" criteria in the Prefilter class |
| 988 | descriptor_set->UpdateValidationCache(*this, *pipe, binding_req_map); |
| 989 | } |
| 990 | |
| 991 | // We can skip updating the state if "nothing" has changed since the last validation. |
| 992 | // See CoreChecks::ValidateCmdBufDrawState for more details. |
| 993 | bool descriptor_set_changed = |
| 994 | !reduced_map.IsManyDescriptors() || |
| 995 | // Update if descriptor set (or contents) has changed |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 996 | state.per_set[set_index].validated_set != descriptor_set.get() || |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 997 | state.per_set[set_index].validated_set_change_count != descriptor_set->GetChangeCount() || |
| 998 | (!dev_data->disabled[image_layout_validation] && |
| 999 | state.per_set[set_index].validated_set_image_layout_change_count != image_layout_change_count); |
| 1000 | bool need_update = descriptor_set_changed || |
| 1001 | // Update if previous bindingReqMap doesn't include new bindingReqMap |
| 1002 | !std::includes(state.per_set[set_index].validated_set_binding_req_map.begin(), |
| 1003 | state.per_set[set_index].validated_set_binding_req_map.end(), binding_req_map.begin(), |
| 1004 | binding_req_map.end()); |
| 1005 | |
| 1006 | if (need_update) { |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 1007 | if (!dev_data->disabled[command_buffer_state] && !descriptor_set->IsPushDescriptor()) { |
| 1008 | AddChild(descriptor_set); |
| 1009 | } |
| 1010 | |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1011 | // Bind this set and its active descriptor resources to the command buffer |
| 1012 | if (!descriptor_set_changed && reduced_map.IsManyDescriptors()) { |
| 1013 | // Only record the bindings that haven't already been recorded |
| 1014 | BindingReqMap delta_reqs; |
| 1015 | std::set_difference(binding_req_map.begin(), binding_req_map.end(), |
| 1016 | state.per_set[set_index].validated_set_binding_req_map.begin(), |
| 1017 | state.per_set[set_index].validated_set_binding_req_map.end(), |
| 1018 | layer_data::insert_iterator<BindingReqMap>(delta_reqs, delta_reqs.begin())); |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 1019 | descriptor_set->UpdateDrawState(dev_data, this, cmd_type, pipe, delta_reqs); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1020 | } else { |
sfricke-samsung | 85584a7 | 2021-09-30 21:43:38 -0700 | [diff] [blame] | 1021 | descriptor_set->UpdateDrawState(dev_data, this, cmd_type, pipe, binding_req_map); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1022 | } |
| 1023 | |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 1024 | state.per_set[set_index].validated_set = descriptor_set.get(); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1025 | state.per_set[set_index].validated_set_change_count = descriptor_set->GetChangeCount(); |
| 1026 | state.per_set[set_index].validated_set_image_layout_change_count = image_layout_change_count; |
| 1027 | if (reduced_map.IsManyDescriptors()) { |
| 1028 | // Check whether old == new before assigning, the equality check is much cheaper than |
| 1029 | // freeing and reallocating the map. |
| 1030 | if (state.per_set[set_index].validated_set_binding_req_map != set_binding_pair.second) { |
| 1031 | state.per_set[set_index].validated_set_binding_req_map = set_binding_pair.second; |
| 1032 | } |
| 1033 | } else { |
| 1034 | state.per_set[set_index].validated_set_binding_req_map = BindingReqMap(); |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | } |
ziga-lunarg | 4898e4f | 2021-10-03 13:57:48 +0200 | [diff] [blame] | 1039 | if (pipe && !pipe->vertex_binding_descriptions_.empty()) { |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1040 | vertex_buffer_used = true; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | // Update pipeline_layout bind points applying the "Pipeline Layout Compatibility" rules. |
| 1045 | // One of pDescriptorSets or push_descriptor_set should be nullptr, indicating whether this |
| 1046 | // is called for CmdBindDescriptorSets or CmdPushDescriptorSet. |
| 1047 | void CMD_BUFFER_STATE::UpdateLastBoundDescriptorSets(VkPipelineBindPoint pipeline_bind_point, |
| 1048 | const PIPELINE_LAYOUT_STATE *pipeline_layout, uint32_t first_set, |
| 1049 | uint32_t set_count, const VkDescriptorSet *pDescriptorSets, |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 1050 | std::shared_ptr<cvdescriptorset::DescriptorSet> &push_descriptor_set, |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1051 | uint32_t dynamic_offset_count, const uint32_t *p_dynamic_offsets) { |
| 1052 | assert((pDescriptorSets == nullptr) ^ (push_descriptor_set == nullptr)); |
| 1053 | // Defensive |
| 1054 | assert(pipeline_layout); |
| 1055 | if (!pipeline_layout) return; |
| 1056 | |
| 1057 | uint32_t required_size = first_set + set_count; |
| 1058 | const uint32_t last_binding_index = required_size - 1; |
| 1059 | assert(last_binding_index < pipeline_layout->compat_for_set.size()); |
| 1060 | |
| 1061 | // Some useful shorthand |
| 1062 | const auto lv_bind_point = ConvertToLvlBindPoint(pipeline_bind_point); |
| 1063 | auto &last_bound = lastBound[lv_bind_point]; |
Jeremy Gebben | 856b8c6 | 2021-12-01 15:20:07 -0700 | [diff] [blame] | 1064 | last_bound.pipeline_layout = pipeline_layout->layout(); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1065 | auto &pipe_compat_ids = pipeline_layout->compat_for_set; |
Jeremy Gebben | 856b8c6 | 2021-12-01 15:20:07 -0700 | [diff] [blame] | 1066 | // Resize binding arrays |
| 1067 | uint32_t last_set_index = first_set + set_count - 1; |
| 1068 | if (last_set_index >= last_bound.per_set.size()) { |
| 1069 | last_bound.per_set.resize(last_set_index + 1); |
| 1070 | } |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1071 | const uint32_t current_size = static_cast<uint32_t>(last_bound.per_set.size()); |
| 1072 | |
| 1073 | // We need this three times in this function, but nowhere else |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 1074 | auto push_descriptor_cleanup = [&last_bound](const std::shared_ptr<cvdescriptorset::DescriptorSet> &ds) -> bool { |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1075 | if (ds && ds->IsPushDescriptor()) { |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 1076 | assert(ds == last_bound.push_descriptor_set); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1077 | last_bound.push_descriptor_set = nullptr; |
| 1078 | return true; |
| 1079 | } |
| 1080 | return false; |
| 1081 | }; |
| 1082 | |
| 1083 | // Clean up the "disturbed" before and after the range to be set |
| 1084 | if (required_size < current_size) { |
| 1085 | if (last_bound.per_set[last_binding_index].compat_id_for_set != pipe_compat_ids[last_binding_index]) { |
| 1086 | // We're disturbing those after last, we'll shrink below, but first need to check for and cleanup the push_descriptor |
| 1087 | for (auto set_idx = required_size; set_idx < current_size; ++set_idx) { |
| 1088 | if (push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set)) break; |
| 1089 | } |
| 1090 | } else { |
| 1091 | // We're not disturbing past last, so leave the upper binding data alone. |
| 1092 | required_size = current_size; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | // We resize if we need more set entries or if those past "last" are disturbed |
| 1097 | if (required_size != current_size) { |
| 1098 | last_bound.per_set.resize(required_size); |
| 1099 | } |
| 1100 | |
| 1101 | // For any previously bound sets, need to set them to "invalid" if they were disturbed by this update |
| 1102 | for (uint32_t set_idx = 0; set_idx < first_set; ++set_idx) { |
| 1103 | if (last_bound.per_set[set_idx].compat_id_for_set != pipe_compat_ids[set_idx]) { |
| 1104 | push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set); |
| 1105 | last_bound.per_set[set_idx].bound_descriptor_set = nullptr; |
| 1106 | last_bound.per_set[set_idx].dynamicOffsets.clear(); |
| 1107 | last_bound.per_set[set_idx].compat_id_for_set = pipe_compat_ids[set_idx]; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | // Now update the bound sets with the input sets |
| 1112 | const uint32_t *input_dynamic_offsets = p_dynamic_offsets; // "read" pointer for dynamic offset data |
| 1113 | for (uint32_t input_idx = 0; input_idx < set_count; input_idx++) { |
| 1114 | auto set_idx = input_idx + first_set; // set_idx is index within layout, input_idx is index within input descriptor sets |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 1115 | auto descriptor_set = |
| 1116 | push_descriptor_set ? push_descriptor_set : dev_data->Get<cvdescriptorset::DescriptorSet>(pDescriptorSets[input_idx]); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1117 | |
| 1118 | // Record binding (or push) |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 1119 | if (descriptor_set != last_bound.push_descriptor_set) { |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1120 | // Only cleanup the push descriptors if they aren't the currently used set. |
| 1121 | push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set); |
| 1122 | } |
| 1123 | last_bound.per_set[set_idx].bound_descriptor_set = descriptor_set; |
| 1124 | last_bound.per_set[set_idx].compat_id_for_set = pipe_compat_ids[set_idx]; // compat ids are canonical *per* set index |
| 1125 | |
| 1126 | if (descriptor_set) { |
| 1127 | auto set_dynamic_descriptor_count = descriptor_set->GetDynamicDescriptorCount(); |
| 1128 | // TODO: Add logic for tracking push_descriptor offsets (here or in caller) |
| 1129 | if (set_dynamic_descriptor_count && input_dynamic_offsets) { |
| 1130 | const uint32_t *end_offset = input_dynamic_offsets + set_dynamic_descriptor_count; |
| 1131 | last_bound.per_set[set_idx].dynamicOffsets = std::vector<uint32_t>(input_dynamic_offsets, end_offset); |
| 1132 | input_dynamic_offsets = end_offset; |
| 1133 | assert(input_dynamic_offsets <= (p_dynamic_offsets + dynamic_offset_count)); |
| 1134 | } else { |
| 1135 | last_bound.per_set[set_idx].dynamicOffsets.clear(); |
| 1136 | } |
| 1137 | if (!descriptor_set->IsPushDescriptor()) { |
| 1138 | // Can't cache validation of push_descriptors |
Jeremy Gebben | 4d51c55 | 2022-01-06 21:27:15 -0700 | [diff] [blame] | 1139 | validated_descriptor_sets.insert(descriptor_set.get()); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | // Set image layout for given VkImageSubresourceRange struct |
| 1146 | void CMD_BUFFER_STATE::SetImageLayout(const IMAGE_STATE &image_state, const VkImageSubresourceRange &image_subresource_range, |
| 1147 | VkImageLayout layout, VkImageLayout expected_layout) { |
| 1148 | auto *subresource_map = GetImageSubresourceLayoutMap(image_state); |
Jeremy Gebben | 4a8881f | 2022-01-10 17:04:30 -0700 | [diff] [blame] | 1149 | if (subresource_map && subresource_map->SetSubresourceRangeLayout(*this, image_subresource_range, layout, expected_layout)) { |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1150 | image_layout_change_count++; // Change the version of this data to force revalidation |
| 1151 | } |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | // Set the initial image layout for all slices of an image view |
| 1155 | void CMD_BUFFER_STATE::SetImageViewInitialLayout(const IMAGE_VIEW_STATE &view_state, VkImageLayout layout) { |
| 1156 | if (dev_data->disabled[image_layout_validation]) { |
| 1157 | return; |
| 1158 | } |
| 1159 | IMAGE_STATE *image_state = view_state.image_state.get(); |
| 1160 | auto *subresource_map = GetImageSubresourceLayoutMap(*image_state); |
Jeremy Gebben | 4a8881f | 2022-01-10 17:04:30 -0700 | [diff] [blame] | 1161 | if (subresource_map) { |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1162 | subresource_map->SetSubresourceRangeInitialLayout(*this, layout, view_state); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | // Set the initial image layout for a passed non-normalized subresource range |
| 1167 | void CMD_BUFFER_STATE::SetImageInitialLayout(const IMAGE_STATE &image_state, const VkImageSubresourceRange &range, |
| 1168 | VkImageLayout layout) { |
| 1169 | auto *subresource_map = GetImageSubresourceLayoutMap(image_state); |
Jeremy Gebben | 4a8881f | 2022-01-10 17:04:30 -0700 | [diff] [blame] | 1170 | if (subresource_map) { |
| 1171 | subresource_map->SetSubresourceRangeInitialLayout(*this, image_state.NormalizeSubresourceRange(range), layout); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | void CMD_BUFFER_STATE::SetImageInitialLayout(VkImage image, const VkImageSubresourceRange &range, VkImageLayout layout) { |
Jeremy Gebben | f444939 | 2022-01-28 10:09:10 -0700 | [diff] [blame] | 1176 | auto image_state = dev_data->Get<IMAGE_STATE>(image); |
Jeremy Gebben | 1ec8933 | 2021-08-05 13:51:49 -0600 | [diff] [blame] | 1177 | if (!image_state) return; |
| 1178 | SetImageInitialLayout(*image_state, range, layout); |
| 1179 | } |
| 1180 | |
| 1181 | void CMD_BUFFER_STATE::SetImageInitialLayout(const IMAGE_STATE &image_state, const VkImageSubresourceLayers &layers, |
| 1182 | VkImageLayout layout) { |
| 1183 | SetImageInitialLayout(image_state, RangeFromLayers(layers), layout); |
| 1184 | } |
| 1185 | |
| 1186 | // Set image layout for all slices of an image view |
| 1187 | void CMD_BUFFER_STATE::SetImageViewLayout(const IMAGE_VIEW_STATE &view_state, VkImageLayout layout, VkImageLayout layoutStencil) { |
| 1188 | const IMAGE_STATE *image_state = view_state.image_state.get(); |
| 1189 | |
| 1190 | VkImageSubresourceRange sub_range = view_state.normalized_subresource_range; |
| 1191 | |
| 1192 | if (sub_range.aspectMask == (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT) && layoutStencil != kInvalidLayout) { |
| 1193 | sub_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; |
| 1194 | SetImageLayout(*image_state, sub_range, layout); |
| 1195 | sub_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1196 | SetImageLayout(*image_state, sub_range, layoutStencil); |
| 1197 | } else { |
| 1198 | SetImageLayout(*image_state, sub_range, layout); |
| 1199 | } |
| 1200 | } |
Jeremy Gebben | 10a0ed1 | 2021-08-17 09:54:29 -0600 | [diff] [blame] | 1201 | |
| 1202 | void CMD_BUFFER_STATE::RecordCmd(CMD_TYPE cmd_type) { commandCount++; } |
| 1203 | |
| 1204 | void CMD_BUFFER_STATE::RecordStateCmd(CMD_TYPE cmd_type, CBStatusFlags state_bits) { |
| 1205 | RecordCmd(cmd_type); |
| 1206 | status |= state_bits; |
| 1207 | static_status &= ~state_bits; |
| 1208 | } |
| 1209 | |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 1210 | void CMD_BUFFER_STATE::RecordTransferCmd(CMD_TYPE cmd_type, std::shared_ptr<BINDABLE> &&buf1, std::shared_ptr<BINDABLE> &&buf2) { |
Jeremy Gebben | 10a0ed1 | 2021-08-17 09:54:29 -0600 | [diff] [blame] | 1211 | RecordCmd(cmd_type); |
| 1212 | if (buf1) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1213 | AddChild(buf1); |
Jeremy Gebben | 10a0ed1 | 2021-08-17 09:54:29 -0600 | [diff] [blame] | 1214 | } |
| 1215 | if (buf2) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1216 | AddChild(buf2); |
Jeremy Gebben | 10a0ed1 | 2021-08-17 09:54:29 -0600 | [diff] [blame] | 1217 | } |
| 1218 | } |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1219 | |
| 1220 | static bool SetEventStageMask(VkEvent event, VkPipelineStageFlags2KHR stageMask, EventToStageMap *localEventToStageMap) { |
| 1221 | (*localEventToStageMap)[event] = stageMask; |
| 1222 | return false; |
| 1223 | } |
| 1224 | |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1225 | void CMD_BUFFER_STATE::RecordSetEvent(CMD_TYPE cmd_type, VkEvent event, VkPipelineStageFlags2KHR stageMask) { |
| 1226 | RecordCmd(cmd_type); |
| 1227 | if (!dev_data->disabled[command_buffer_state]) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 1228 | auto event_state = dev_data->Get<EVENT_STATE>(event); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1229 | if (event_state) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1230 | AddChild(event_state); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1231 | } |
| 1232 | } |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1233 | events.push_back(event); |
| 1234 | if (!waitedEvents.count(event)) { |
| 1235 | writeEventsBeforeWait.push_back(event); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1236 | } |
Jeremy Gebben | 332d4dd | 2022-01-01 12:40:02 -0700 | [diff] [blame] | 1237 | eventUpdates.emplace_back([event, stageMask](CMD_BUFFER_STATE &, bool do_validate, EventToStageMap *localEventToStageMap) { |
| 1238 | return SetEventStageMask(event, stageMask, localEventToStageMap); |
| 1239 | }); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1240 | } |
| 1241 | |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1242 | void CMD_BUFFER_STATE::RecordResetEvent(CMD_TYPE cmd_type, VkEvent event, VkPipelineStageFlags2KHR stageMask) { |
| 1243 | RecordCmd(cmd_type); |
| 1244 | if (!dev_data->disabled[command_buffer_state]) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 1245 | auto event_state = dev_data->Get<EVENT_STATE>(event); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1246 | if (event_state) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1247 | AddChild(event_state); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1248 | } |
| 1249 | } |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1250 | events.push_back(event); |
| 1251 | if (!waitedEvents.count(event)) { |
| 1252 | writeEventsBeforeWait.push_back(event); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1253 | } |
| 1254 | |
Jeremy Gebben | 332d4dd | 2022-01-01 12:40:02 -0700 | [diff] [blame] | 1255 | eventUpdates.emplace_back([event](CMD_BUFFER_STATE &, bool do_validate, EventToStageMap *localEventToStageMap) { |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1256 | return SetEventStageMask(event, VkPipelineStageFlags2KHR(0), localEventToStageMap); |
| 1257 | }); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1258 | } |
| 1259 | |
Jeremy Gebben | 332d4dd | 2022-01-01 12:40:02 -0700 | [diff] [blame] | 1260 | void CMD_BUFFER_STATE::RecordWaitEvents(CMD_TYPE cmd_type, uint32_t eventCount, const VkEvent *pEvents, |
| 1261 | VkPipelineStageFlags2KHR src_stage_mask) { |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1262 | RecordCmd(cmd_type); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1263 | for (uint32_t i = 0; i < eventCount; ++i) { |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1264 | if (!dev_data->disabled[command_buffer_state]) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 1265 | auto event_state = dev_data->Get<EVENT_STATE>(pEvents[i]); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1266 | if (event_state) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1267 | AddChild(event_state); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1268 | } |
| 1269 | } |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1270 | waitedEvents.insert(pEvents[i]); |
| 1271 | events.push_back(pEvents[i]); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1275 | void CMD_BUFFER_STATE::RecordBarriers(uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, |
| 1276 | uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers, |
| 1277 | uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) { |
| 1278 | if (dev_data->disabled[command_buffer_state]) return; |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1279 | |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1280 | for (uint32_t i = 0; i < bufferMemoryBarrierCount; i++) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 1281 | auto buffer_state = dev_data->Get<BUFFER_STATE>(pBufferMemoryBarriers[i].buffer); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1282 | if (buffer_state) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1283 | AddChild(buffer_state); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | for (uint32_t i = 0; i < imageMemoryBarrierCount; i++) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 1287 | auto image_state = dev_data->Get<IMAGE_STATE>(pImageMemoryBarriers[i].image); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1288 | if (image_state) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1289 | AddChild(image_state); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1294 | void CMD_BUFFER_STATE::RecordBarriers(const VkDependencyInfoKHR &dep_info) { |
| 1295 | if (dev_data->disabled[command_buffer_state]) return; |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1296 | |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1297 | for (uint32_t i = 0; i < dep_info.bufferMemoryBarrierCount; i++) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 1298 | auto buffer_state = dev_data->Get<BUFFER_STATE>(dep_info.pBufferMemoryBarriers[i].buffer); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1299 | if (buffer_state) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1300 | AddChild(buffer_state); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1301 | } |
| 1302 | } |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1303 | for (uint32_t i = 0; i < dep_info.imageMemoryBarrierCount; i++) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 1304 | auto image_state = dev_data->Get<IMAGE_STATE>(dep_info.pImageMemoryBarriers[i].image); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1305 | if (image_state) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1306 | AddChild(image_state); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1311 | void CMD_BUFFER_STATE::RecordWriteTimestamp(CMD_TYPE cmd_type, VkPipelineStageFlags2KHR pipelineStage, VkQueryPool queryPool, |
| 1312 | uint32_t slot) { |
| 1313 | RecordCmd(cmd_type); |
| 1314 | if (dev_data->disabled[query_validation]) return; |
| 1315 | |
| 1316 | if (!dev_data->disabled[command_buffer_state]) { |
Jeremy Gebben | b20a824 | 2021-11-05 15:14:43 -0600 | [diff] [blame] | 1317 | auto pool_state = dev_data->Get<QUERY_POOL_STATE>(queryPool); |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 1318 | AddChild(pool_state); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1319 | } |
| 1320 | QueryObject query = {queryPool, slot}; |
Jeremy Gebben | cefa7fd | 2021-08-17 13:44:47 -0600 | [diff] [blame] | 1321 | EndQuery(query); |
Jeremy Gebben | 29110d2 | 2021-08-17 13:30:50 -0600 | [diff] [blame] | 1322 | } |
Jeremy Gebben | 4af0aa8 | 2021-09-08 09:35:16 -0600 | [diff] [blame] | 1323 | |
Jeremy Gebben | 5764298 | 2021-09-14 14:14:55 -0600 | [diff] [blame] | 1324 | void CMD_BUFFER_STATE::Submit(uint32_t perf_submit_pass) { |
| 1325 | VkQueryPool first_pool = VK_NULL_HANDLE; |
| 1326 | EventToStageMap local_event_to_stage_map; |
| 1327 | QueryMap local_query_to_state_map; |
| 1328 | for (auto &function : queryUpdates) { |
| 1329 | function(nullptr, /*do_validate*/ false, first_pool, perf_submit_pass, &local_query_to_state_map); |
| 1330 | } |
Jeremy Gebben | 4af0aa8 | 2021-09-08 09:35:16 -0600 | [diff] [blame] | 1331 | |
Jeremy Gebben | 5764298 | 2021-09-14 14:14:55 -0600 | [diff] [blame] | 1332 | for (const auto &query_state_pair : local_query_to_state_map) { |
Jeremy Gebben | 1858ae9 | 2021-12-02 11:28:05 -0700 | [diff] [blame] | 1333 | auto query_pool_state = dev_data->Get<QUERY_POOL_STATE>(query_state_pair.first.pool); |
| 1334 | query_pool_state->SetQueryState(query_state_pair.first.query, query_state_pair.first.perf_pass, query_state_pair.second); |
Jeremy Gebben | 5764298 | 2021-09-14 14:14:55 -0600 | [diff] [blame] | 1335 | } |
Jeremy Gebben | 4af0aa8 | 2021-09-08 09:35:16 -0600 | [diff] [blame] | 1336 | |
Jeremy Gebben | 5764298 | 2021-09-14 14:14:55 -0600 | [diff] [blame] | 1337 | for (const auto &function : eventUpdates) { |
Jeremy Gebben | 332d4dd | 2022-01-01 12:40:02 -0700 | [diff] [blame] | 1338 | function(*this, /*do_validate*/ false, &local_event_to_stage_map); |
Jeremy Gebben | 5764298 | 2021-09-14 14:14:55 -0600 | [diff] [blame] | 1339 | } |
Jeremy Gebben | 4af0aa8 | 2021-09-08 09:35:16 -0600 | [diff] [blame] | 1340 | |
Jeremy Gebben | 5764298 | 2021-09-14 14:14:55 -0600 | [diff] [blame] | 1341 | for (const auto &eventStagePair : local_event_to_stage_map) { |
Jeremy Gebben | 9f53710 | 2021-10-05 16:37:12 -0600 | [diff] [blame] | 1342 | auto event_state = dev_data->Get<EVENT_STATE>(eventStagePair.first); |
| 1343 | event_state->stageMask = eventStagePair.second; |
Jeremy Gebben | 5764298 | 2021-09-14 14:14:55 -0600 | [diff] [blame] | 1344 | } |
| 1345 | } |
Jeremy Gebben | 4af0aa8 | 2021-09-08 09:35:16 -0600 | [diff] [blame] | 1346 | |
ziga-lunarg | 96c7d82 | 2022-02-28 19:39:17 +0100 | [diff] [blame] | 1347 | void CMD_BUFFER_STATE::Retire(uint32_t perf_submit_pass, const std::function<bool(const QueryObject &)>& is_query_updated_after) { |
Jeremy Gebben | 5764298 | 2021-09-14 14:14:55 -0600 | [diff] [blame] | 1348 | // First perform decrement on general case bound objects |
| 1349 | for (auto event : writeEventsBeforeWait) { |
Jeremy Gebben | d177d92 | 2021-10-28 13:42:10 -0600 | [diff] [blame] | 1350 | auto event_state = dev_data->Get<EVENT_STATE>(event); |
| 1351 | if (event_state) { |
| 1352 | event_state->write_in_use--; |
Jeremy Gebben | 5764298 | 2021-09-14 14:14:55 -0600 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | QueryMap local_query_to_state_map; |
| 1356 | VkQueryPool first_pool = VK_NULL_HANDLE; |
| 1357 | for (auto &function : queryUpdates) { |
| 1358 | function(nullptr, /*do_validate*/ false, first_pool, perf_submit_pass, &local_query_to_state_map); |
| 1359 | } |
| 1360 | |
| 1361 | for (const auto &query_state_pair : local_query_to_state_map) { |
ziga-lunarg | 96c7d82 | 2022-02-28 19:39:17 +0100 | [diff] [blame] | 1362 | if (query_state_pair.second == QUERYSTATE_ENDED && !is_query_updated_after(query_state_pair.first)) { |
Jeremy Gebben | 1858ae9 | 2021-12-02 11:28:05 -0700 | [diff] [blame] | 1363 | auto query_pool_state = dev_data->Get<QUERY_POOL_STATE>(query_state_pair.first.pool); |
| 1364 | query_pool_state->SetQueryState(query_state_pair.first.query, query_state_pair.first.perf_pass, QUERYSTATE_AVAILABLE); |
Jeremy Gebben | 4af0aa8 | 2021-09-08 09:35:16 -0600 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | } |
aitor-lunarg | a131fca | 2022-02-17 22:55:55 +0100 | [diff] [blame] | 1368 | |
| 1369 | void CMD_BUFFER_STATE::UnbindResources() { |
| 1370 | // Pipeline and descriptor sets |
| 1371 | lastBound[BindPoint_Graphics].Reset(); |
| 1372 | |
| 1373 | // Vertex and index buffers |
| 1374 | index_buffer_binding.reset(); |
Tony-LunarG | b477ffb | 2022-03-16 12:07:36 -0600 | [diff] [blame] | 1375 | status &= ~CBSTATUS_INDEX_BUFFER_BOUND; |
aitor-lunarg | a131fca | 2022-02-17 22:55:55 +0100 | [diff] [blame] | 1376 | vertex_buffer_used = false; |
| 1377 | current_vertex_buffer_binding_info.vertex_buffer_bindings.clear(); |
| 1378 | |
| 1379 | // Push constants |
| 1380 | push_constant_data.clear(); |
| 1381 | push_constant_data_ranges.reset(); |
| 1382 | push_constant_data_update.clear(); |
| 1383 | push_constant_pipeline_layout_set = VK_NULL_HANDLE; |
| 1384 | |
| 1385 | // Dynamic state |
| 1386 | dynamic_status = CBSTATUS_NONE; |
| 1387 | } |