blob: 967790f443c26fff6aa70bea7bf513502fe75ce2 [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#pragma once
28#include "base_node.h"
29#include "query_state.h"
30#include "command_validation.h"
31#include "hash_vk_types.h"
32#include "subresource_adapter.h"
33#include "image_layout_map.h"
34#include "pipeline_state.h"
35#include "device_state.h"
36#include "descriptor_sets.h"
37#include "qfo_transfer.h"
38
Jeremy Gebben1ec89332021-08-05 13:51:49 -060039struct SUBPASS_INFO;
40class FRAMEBUFFER_STATE;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060041class RENDER_PASS_STATE;
42class CoreChecks;
43class ValidationStateTracker;
44
Tony-LunarG285dbdc2022-07-15 09:42:54 -060045#ifdef VK_USE_PLATFORM_METAL_EXT
46static bool GetMetalExport(const VkEventCreateInfo *info) {
47 bool retval = false;
48 auto export_metal_object_info = LvlFindInChain<VkExportMetalObjectCreateInfoEXT>(info->pNext);
49 while (export_metal_object_info) {
50 if (export_metal_object_info->exportObjectType == VK_EXPORT_METAL_OBJECT_TYPE_METAL_SHARED_EVENT_BIT_EXT) {
51 retval = true;
52 break;
53 }
54 export_metal_object_info = LvlFindInChain<VkExportMetalObjectCreateInfoEXT>(export_metal_object_info->pNext);
55 }
56 return retval;
57}
58#endif // VK_USE_PLATFORM_METAL_EXT
59
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060060class EVENT_STATE : public BASE_NODE {
61 public:
62 int write_in_use;
Tony-LunarG285dbdc2022-07-15 09:42:54 -060063#ifdef VK_USE_PLATFORM_METAL_EXT
64 const bool metal_event_export;
65#endif // VK_USE_PLATFORM_METAL_EXT
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060066 VkPipelineStageFlags2KHR stageMask = VkPipelineStageFlags2KHR(0);
67 VkEventCreateFlags flags;
Tony-LunarG285dbdc2022-07-15 09:42:54 -060068
69 EVENT_STATE(VkEvent event_, const VkEventCreateInfo *pCreateInfo)
70 : BASE_NODE(event_, kVulkanObjectTypeEvent),
71 write_in_use(0),
Tony-LunarGffb5b522022-06-15 15:49:27 -060072#ifdef VK_USE_PLATFORM_METAL_EXT
Tony-LunarG285dbdc2022-07-15 09:42:54 -060073 metal_event_export(GetMetalExport(pCreateInfo)),
74#endif // VK_USE_PLATFORM_METAL_EXT
75 flags(pCreateInfo->flags) {}
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060076
77 VkEvent event() const { return handle_.Cast<VkEvent>(); }
78};
79
80// Only CoreChecks uses this, but the state tracker stores it.
81constexpr static auto kInvalidLayout = image_layout_map::kInvalidLayout;
82using ImageSubresourceLayoutMap = image_layout_map::ImageSubresourceLayoutMap;
83typedef layer_data::unordered_map<VkEvent, VkPipelineStageFlags2KHR> EventToStageMap;
84
85// Track command pools and their command buffers
86class COMMAND_POOL_STATE : public BASE_NODE {
87 public:
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060088 ValidationStateTracker *dev_data;
Jeremy Gebben1ec89332021-08-05 13:51:49 -060089 const VkCommandPoolCreateFlags createFlags;
90 const uint32_t queueFamilyIndex;
91 const VkQueueFlags queue_flags;
92 const bool unprotected; // can't be used for protected memory
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060093 // Cmd buffers allocated from this pool
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060094 layer_data::unordered_map<VkCommandBuffer, CMD_BUFFER_STATE *> commandBuffers;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060095
Jeremy Gebbencd7fa282021-10-27 10:25:32 -060096 COMMAND_POOL_STATE(ValidationStateTracker *dev, VkCommandPool cp, const VkCommandPoolCreateInfo *pCreateInfo,
97 VkQueueFlags flags);
98 virtual ~COMMAND_POOL_STATE() { Destroy(); }
Jeremy Gebben159b3cc2021-06-03 09:09:03 -060099
100 VkCommandPool commandPool() const { return handle_.Cast<VkCommandPool>(); }
101
Jeremy Gebbencd7fa282021-10-27 10:25:32 -0600102 void Allocate(const VkCommandBufferAllocateInfo *create_info, const VkCommandBuffer *command_buffers);
103 void Free(uint32_t count, const VkCommandBuffer *command_buffers);
104 void Reset();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600105
Jeremy Gebbencd7fa282021-10-27 10:25:32 -0600106 void Destroy() override;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600107};
108
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600109// Autogenerated as part of the command_validation.h codegen
110const char *CommandTypeString(CMD_TYPE type);
111
112enum CB_STATE {
113 CB_NEW, // Newly created CB w/o any cmds
114 CB_RECORDING, // BeginCB has been called on this CB
115 CB_RECORDED, // EndCB has been called on this CB
116 CB_INVALID_COMPLETE, // had a complete recording, but was since invalidated
117 CB_INVALID_INCOMPLETE, // fouled before recording was completed
118};
119
120// CB Status -- used to track status of various bindings on cmd buffer objects
121typedef uint64_t CBStatusFlags;
122enum CBStatusFlagBits : uint64_t {
123 // clang-format off
124 CBSTATUS_NONE = 0x00000000, // No status is set
125 CBSTATUS_LINE_WIDTH_SET = 0x00000001, // Line width has been set
126 CBSTATUS_DEPTH_BIAS_SET = 0x00000002, // Depth bias has been set
127 CBSTATUS_BLEND_CONSTANTS_SET = 0x00000004, // Blend constants state has been set
128 CBSTATUS_DEPTH_BOUNDS_SET = 0x00000008, // Depth bounds state object has been set
129 CBSTATUS_STENCIL_READ_MASK_SET = 0x00000010, // Stencil read mask has been set
130 CBSTATUS_STENCIL_WRITE_MASK_SET = 0x00000020, // Stencil write mask has been set
131 CBSTATUS_STENCIL_REFERENCE_SET = 0x00000040, // Stencil reference has been set
132 CBSTATUS_VIEWPORT_SET = 0x00000080,
133 CBSTATUS_SCISSOR_SET = 0x00000100,
134 CBSTATUS_INDEX_BUFFER_BOUND = 0x00000200, // Index buffer has been set
135 CBSTATUS_EXCLUSIVE_SCISSOR_SET = 0x00000400,
136 CBSTATUS_SHADING_RATE_PALETTE_SET = 0x00000800,
137 CBSTATUS_LINE_STIPPLE_SET = 0x00001000,
138 CBSTATUS_VIEWPORT_W_SCALING_SET = 0x00002000,
139 CBSTATUS_CULL_MODE_SET = 0x00004000,
140 CBSTATUS_FRONT_FACE_SET = 0x00008000,
141 CBSTATUS_PRIMITIVE_TOPOLOGY_SET = 0x00010000,
142 CBSTATUS_VIEWPORT_WITH_COUNT_SET = 0x00020000,
143 CBSTATUS_SCISSOR_WITH_COUNT_SET = 0x00040000,
144 CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET = 0x00080000,
145 CBSTATUS_DEPTH_TEST_ENABLE_SET = 0x00100000,
146 CBSTATUS_DEPTH_WRITE_ENABLE_SET = 0x00200000,
147 CBSTATUS_DEPTH_COMPARE_OP_SET = 0x00400000,
148 CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET = 0x00800000,
149 CBSTATUS_STENCIL_TEST_ENABLE_SET = 0x01000000,
150 CBSTATUS_STENCIL_OP_SET = 0x02000000,
151 CBSTATUS_DISCARD_RECTANGLE_SET = 0x04000000,
152 CBSTATUS_SAMPLE_LOCATIONS_SET = 0x08000000,
153 CBSTATUS_COARSE_SAMPLE_ORDER_SET = 0x10000000,
154 CBSTATUS_PATCH_CONTROL_POINTS_SET = 0x20000000,
155 CBSTATUS_RASTERIZER_DISCARD_ENABLE_SET = 0x40000000,
156 CBSTATUS_DEPTH_BIAS_ENABLE_SET = 0x80000000,
157 CBSTATUS_LOGIC_OP_SET = 0x100000000,
158 CBSTATUS_PRIMITIVE_RESTART_ENABLE_SET = 0x200000000,
159 CBSTATUS_VERTEX_INPUT_SET = 0x400000000,
ziga-lunarg67b7c392022-03-26 01:45:34 +0100160 CBSTATUS_COLOR_WRITE_ENABLE_SET = 0x800000000,
161 CBSTATUS_ALL_STATE_SET = 0xFFFFFFDFF, // All state set (intentionally exclude index buffer)
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600162 // clang-format on
163};
164
165VkDynamicState ConvertToDynamicState(CBStatusFlagBits flag);
166CBStatusFlagBits ConvertToCBStatusFlagBits(VkDynamicState state);
167std::string DynamicStateString(CBStatusFlags input_value);
168
169struct BufferBinding {
170 std::shared_ptr<BUFFER_STATE> buffer_state;
171 VkDeviceSize size;
172 VkDeviceSize offset;
173 VkDeviceSize stride;
174
175 BufferBinding() : buffer_state(), size(0), offset(0), stride(0) {}
176 virtual ~BufferBinding() {}
177
178 virtual void reset() { *this = BufferBinding(); }
179};
180
181struct IndexBufferBinding : BufferBinding {
182 VkIndexType index_type;
183
184 IndexBufferBinding() : BufferBinding(), index_type(static_cast<VkIndexType>(0)) {}
185 virtual ~IndexBufferBinding() {}
186
187 virtual void reset() override { *this = IndexBufferBinding(); }
188};
189
190struct CBVertexBufferBindingInfo {
191 std::vector<BufferBinding> vertex_buffer_bindings;
192};
193
Jeremy Gebben4a8881f2022-01-10 17:04:30 -0700194typedef layer_data::unordered_map<const IMAGE_STATE *, std::shared_ptr<ImageSubresourceLayoutMap>> CommandBufferImageLayoutMap;
195
196typedef layer_data::unordered_map<const GlobalImageLayoutRangeMap *, std::shared_ptr<ImageSubresourceLayoutMap>>
197 CommandBufferAliasedLayoutMap;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600198
199class CMD_BUFFER_STATE : public REFCOUNTED_NODE {
200 public:
201 VkCommandBufferAllocateInfo createInfo = {};
202 VkCommandBufferBeginInfo beginInfo;
203 VkCommandBufferInheritanceInfo inheritanceInfo;
Jeremy Gebbencd7fa282021-10-27 10:25:32 -0600204 // since command buffers can only be destroyed by their command pool, this does not need to be a shared_ptr
205 const COMMAND_POOL_STATE *command_pool;
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600206 ValidationStateTracker *dev_data;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600207 bool hasDrawCmd;
208 bool hasTraceRaysCmd;
209 bool hasBuildAccelerationStructureCmd;
210 bool hasDispatchCmd;
211 bool unprotected; // can't be used for protected memory
ziga-lunarg0acda6f2022-04-25 22:41:52 +0200212 bool hasRenderPassInstance;
213 bool suspendsRenderPassInstance;
214 bool resumesRenderPassInstance;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600215
216 CB_STATE state; // Track cmd buffer update state
217 uint64_t commandCount; // Number of commands recorded. Currently only used with VK_KHR_performance_query
218 uint64_t submitCount; // Number of times CB has been submitted
Nathaniel Cesario62f03592022-07-11 09:19:20 -0600219 bool pipeline_bound = false; // True if CmdBindPipeline has been called on this command buffer, false otherwise
220 uint64_t commands_since_begin_rendering = 0; // Number of commands since the last CmdBeginRenderingCommand
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600221 typedef uint64_t ImageLayoutUpdateCount;
222 ImageLayoutUpdateCount image_layout_change_count; // The sequence number for changes to image layout (for cached validation)
223 CBStatusFlags status; // Track status of various bindings on cmd buffer
224 CBStatusFlags static_status; // All state bits provided by current graphics pipeline
225 // rather than dynamic state
226 CBStatusFlags dynamic_status; // dynamic state set up in pipeline
Tony-LunarG40b33882021-12-02 12:40:11 -0700227 std::string begin_rendering_func_name;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600228 // Currently storing "lastBound" objects on per-CB basis
229 // long-term may want to create caches of "lastBound" states and could have
230 // each individual CMD_NODE referencing its own "lastBound" state
231 // Store last bound state for Gfx & Compute pipeline bind points
232 std::array<LAST_BOUND_STATE, BindPoint_Count> lastBound; // index is LvlBindPoint.
233
John Zulauf5dea8842022-04-12 14:05:47 -0600234 // Use the casting boilerplate from BASE_NODE to implement the derived shared_from_this
235 std::shared_ptr<const CMD_BUFFER_STATE> shared_from_this() const { return SharedFromThisImpl(this); }
236 std::shared_ptr<CMD_BUFFER_STATE> shared_from_this() { return SharedFromThisImpl(this); }
237
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600238 struct CmdDrawDispatchInfo {
239 CMD_TYPE cmd_type;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600240 std::vector<std::pair<const uint32_t, DescriptorRequirement>> binding_infos;
241 VkFramebuffer framebuffer;
242 std::shared_ptr<std::vector<SUBPASS_INFO>> subpasses;
243 std::shared_ptr<std::vector<IMAGE_VIEW_STATE *>> attachments;
244 };
245 layer_data::unordered_map<VkDescriptorSet, std::vector<CmdDrawDispatchInfo>> validate_descriptorsets_in_queuesubmit;
246
247 // If VK_NV_inherited_viewport_scissor is enabled and VkCommandBufferInheritanceViewportScissorInfoNV::viewportScissor2D is
248 // true, then is the nonempty list of viewports passed in pViewportDepths. Otherwise, this is empty.
249 std::vector<VkViewport> inheritedViewportDepths;
250
251 // For each draw command D recorded to this command buffer, let
252 // * g_D be the graphics pipeline used
253 // * v_G be the viewportCount of g_D (0 if g_D disables rasterization or enables VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT)
254 // * s_G be the scissorCount of g_D (0 if g_D disables rasterization or enables VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT)
255 // Then this value is max(0, max(v_G for all D in cb), max(s_G for all D in cb))
256 uint32_t usedViewportScissorCount;
257 uint32_t pipelineStaticViewportCount; // v_G for currently-bound graphics pipeline.
258 uint32_t pipelineStaticScissorCount; // s_G for currently-bound graphics pipeline.
259
260 uint32_t viewportMask;
261 uint32_t viewportWithCountMask;
262 uint32_t viewportWithCountCount;
263 uint32_t scissorMask;
264 uint32_t scissorWithCountMask;
265 uint32_t scissorWithCountCount;
266
267 // Dynamic viewports set in this command buffer; if bit j of viewportMask is set then dynamicViewports[j] is valid, but the
268 // converse need not be true.
269 std::vector<VkViewport> dynamicViewports;
270
271 // Bits set when binding graphics pipeline defining corresponding static state, or executing any secondary command buffer.
272 // Bits unset by calling a corresponding vkCmdSet[State] cmd.
273 uint32_t trashedViewportMask;
274 uint32_t trashedScissorMask;
275 bool trashedViewportCount;
276 bool trashedScissorCount;
277
278 // True iff any draw command recorded to this command buffer consumes dynamic viewport/scissor with count state.
279 bool usedDynamicViewportCount;
280 bool usedDynamicScissorCount;
281
282 uint32_t initial_device_mask;
283 VkPrimitiveTopology primitiveTopology;
284
Nathaniel Cesario8d5c9d92022-07-25 12:59:16 -0600285 bool rasterization_disabled = false;
286
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600287 safe_VkRenderPassBeginInfo activeRenderPassBeginInfo;
288 std::shared_ptr<RENDER_PASS_STATE> activeRenderPass;
289 std::shared_ptr<std::vector<SUBPASS_INFO>> active_subpasses;
290 std::shared_ptr<std::vector<IMAGE_VIEW_STATE *>> active_attachments;
291 std::set<std::shared_ptr<IMAGE_VIEW_STATE>> attachments_view_states;
292
293 VkSubpassContents activeSubpassContents;
294 uint32_t active_render_pass_device_mask;
295 uint32_t activeSubpass;
296 std::shared_ptr<FRAMEBUFFER_STATE> activeFramebuffer;
297 layer_data::unordered_set<std::shared_ptr<FRAMEBUFFER_STATE>> framebuffers;
298 // Unified data structs to track objects bound to this command buffer as well as object
299 // dependencies that have been broken : either destroyed objects, or updated descriptor sets
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700300 layer_data::unordered_set<std::shared_ptr<BASE_NODE>> object_bindings;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600301 layer_data::unordered_map<VulkanTypedHandle, LogObjectList> broken_bindings;
302
303 QFOTransferBarrierSets<QFOBufferTransferBarrier> qfo_transfer_buffer_barriers;
304 QFOTransferBarrierSets<QFOImageTransferBarrier> qfo_transfer_image_barriers;
305
306 layer_data::unordered_set<VkEvent> waitedEvents;
307 std::vector<VkEvent> writeEventsBeforeWait;
308 std::vector<VkEvent> events;
309 layer_data::unordered_set<QueryObject> activeQueries;
310 layer_data::unordered_set<QueryObject> startedQueries;
311 layer_data::unordered_set<QueryObject> resetQueries;
ziga-lunarg96c7d822022-02-28 19:39:17 +0100312 layer_data::unordered_set<QueryObject> updatedQueries;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600313 CommandBufferImageLayoutMap image_layout_map;
Jeremy Gebben4a8881f2022-01-10 17:04:30 -0700314 CommandBufferAliasedLayoutMap aliased_image_layout_map; // storage for potentially aliased images
315
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600316 CBVertexBufferBindingInfo current_vertex_buffer_binding_info;
317 bool vertex_buffer_used; // Track for perf warning to make sure any bound vtx buffer used
318 VkCommandBuffer primaryCommandBuffer;
319 // If primary, the secondary command buffers we will call.
320 // If secondary, the primary command buffers we will be called by.
321 layer_data::unordered_set<CMD_BUFFER_STATE *> linkedCommandBuffers;
322 // Validation functions run at primary CB queue submit time
Jeremy Gebbene5361dd2021-11-18 14:23:56 -0700323 using QueueCallback = std::function<bool(const ValidationStateTracker &device_data, const class QUEUE_STATE &queue_state,
324 const CMD_BUFFER_STATE &cb_state)>;
325 std::vector<QueueCallback> queue_submit_functions;
Hans-Kristian Arntzen59c2c3f2021-06-14 11:40:12 +0200326 // Used by some layers to defer actions until vkCmdEndRenderPass time.
327 // Layers using this are responsible for inserting the callbacks into queue_submit_functions.
Jeremy Gebbene5361dd2021-11-18 14:23:56 -0700328 std::vector<QueueCallback> queue_submit_functions_after_render_pass;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600329 // Validation functions run when secondary CB is executed in primary
Jeremy Gebben057f9d52021-11-05 14:12:31 -0600330 std::vector<std::function<bool(const CMD_BUFFER_STATE &secondary, const CMD_BUFFER_STATE *primary, const FRAMEBUFFER_STATE *)>>
331 cmd_execute_commands_functions;
Jeremy Gebben332d4dd2022-01-01 12:40:02 -0700332 std::vector<std::function<bool(CMD_BUFFER_STATE &cb, bool do_validate, EventToStageMap *localEventToStageMap)>> eventUpdates;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600333 std::vector<std::function<bool(const ValidationStateTracker *device_data, bool do_validate, VkQueryPool &firstPerfQueryPool,
334 uint32_t perfQueryPass, QueryMap *localQueryToStateMap)>>
335 queryUpdates;
Jeremy Gebben87db52f2021-10-14 13:55:09 -0600336 layer_data::unordered_set<const cvdescriptorset::DescriptorSet *> validated_descriptor_sets;
337 layer_data::unordered_map<const cvdescriptorset::DescriptorSet *, cvdescriptorset::DescriptorSet::CachedValidation>
338 descriptorset_cache;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600339 // Contents valid only after an index buffer is bound (CBSTATUS_INDEX_BUFFER_BOUND set)
340 IndexBufferBinding index_buffer_binding;
341 bool performance_lock_acquired = false;
342 bool performance_lock_released = false;
343
344 // Cache of current insert label...
345 LoggingLabel debug_label;
346
347 std::vector<uint8_t> push_constant_data;
348 PushConstantRangesId push_constant_data_ranges;
349
350 std::map<VkShaderStageFlagBits, std::vector<uint8_t>>
351 push_constant_data_update; // vector's value is enum PushConstantByteState.
352 VkPipelineLayout push_constant_pipeline_layout_set;
353
354 // Used for Best Practices tracking
355 uint32_t small_indexed_draw_call_count;
356
357 bool transform_feedback_active{false};
ziga-lunarg40f5fbc2021-08-14 23:06:41 +0200358 bool conditional_rendering_active{false};
ziga-lunarg39eeaf22021-09-03 19:12:44 +0200359 bool conditional_rendering_inside_render_pass{false};
360 uint32_t conditional_rendering_subpass{0};
ziga-lunarg67b7c392022-03-26 01:45:34 +0100361 uint32_t dynamicColorWriteEnableAttachmentCount{0};
Jeremy Gebben332d4dd2022-01-01 12:40:02 -0700362 mutable ReadWriteLock lock;
363 ReadLockGuard ReadLock() const { return ReadLockGuard(lock); }
364 WriteLockGuard WriteLock() { return WriteLockGuard(lock); }
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600365
Jeremy Gebben3d22d582021-08-11 15:37:58 -0600366 CMD_BUFFER_STATE(ValidationStateTracker *, VkCommandBuffer cb, const VkCommandBufferAllocateInfo *pCreateInfo,
Jeremy Gebbencd7fa282021-10-27 10:25:32 -0600367 const COMMAND_POOL_STATE *cmd_pool);
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600368
Jeremy Gebben3d22d582021-08-11 15:37:58 -0600369 virtual ~CMD_BUFFER_STATE() { Destroy(); }
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600370
371 void Destroy() override;
372
373 VkCommandBuffer commandBuffer() const { return handle_.Cast<VkCommandBuffer>(); }
374
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600375 IMAGE_VIEW_STATE *GetActiveAttachmentImageViewState(uint32_t index);
376 const IMAGE_VIEW_STATE *GetActiveAttachmentImageViewState(uint32_t index) const;
377
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700378 void AddChild(std::shared_ptr<BASE_NODE> &base_node);
Jeremy Gebben9f537102021-10-05 16:37:12 -0600379 template <typename StateObject>
380 void AddChild(std::shared_ptr<StateObject> &child_node) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700381 auto base = std::static_pointer_cast<BASE_NODE>(child_node);
382 AddChild(base);
Jeremy Gebben9f537102021-10-05 16:37:12 -0600383 }
384
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700385 void RemoveChild(std::shared_ptr<BASE_NODE> &base_node);
386 template <typename StateObject>
387 void RemoveChild(std::shared_ptr<StateObject> &child_node) {
388 auto base = std::static_pointer_cast<BASE_NODE>(child_node);
389 RemoveChild(base);
390 }
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600391
Jeremy Gebben3d22d582021-08-11 15:37:58 -0600392 virtual void Reset();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600393
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600394 void IncrementResources();
395
396 void ResetPushConstantDataIfIncompatible(const PIPELINE_LAYOUT_STATE *pipeline_layout_state);
397
Jeremy Gebben6335df62021-11-01 10:50:13 -0600398 const ImageSubresourceLayoutMap *GetImageSubresourceLayoutMap(const IMAGE_STATE &image_state) const;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600399 ImageSubresourceLayoutMap *GetImageSubresourceLayoutMap(const IMAGE_STATE &image_state);
ziga-lunarg189ae5d2021-10-19 13:09:58 +0200400 const CommandBufferImageLayoutMap& GetImageSubresourceLayoutMap() const;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600401
402 const QFOTransferBarrierSets<QFOImageTransferBarrier> &GetQFOBarrierSets(const QFOImageTransferBarrier &type_tag) const {
403 return qfo_transfer_image_barriers;
404 }
405
406 const QFOTransferBarrierSets<QFOBufferTransferBarrier> &GetQFOBarrierSets(const QFOBufferTransferBarrier &type_tag) const {
407 return qfo_transfer_buffer_barriers;
408 }
409
410 QFOTransferBarrierSets<QFOImageTransferBarrier> &GetQFOBarrierSets(const QFOImageTransferBarrier &type_tag) {
411 return qfo_transfer_image_barriers;
412 }
413
414 QFOTransferBarrierSets<QFOBufferTransferBarrier> &GetQFOBarrierSets(const QFOBufferTransferBarrier &type_tag) {
415 return qfo_transfer_buffer_barriers;
416 }
417
418 PIPELINE_STATE *GetCurrentPipeline(VkPipelineBindPoint pipelineBindPoint) const {
419 const auto lv_bind_point = ConvertToLvlBindPoint(pipelineBindPoint);
420 return lastBound[lv_bind_point].pipeline_state;
421 }
422
423 void GetCurrentPipelineAndDesriptorSets(VkPipelineBindPoint pipelineBindPoint, const PIPELINE_STATE **rtn_pipe,
424 const std::vector<LAST_BOUND_STATE::PER_SET> **rtn_sets) const {
425 const auto lv_bind_point = ConvertToLvlBindPoint(pipelineBindPoint);
426 const auto &last_bound_it = lastBound[lv_bind_point];
427 if (!last_bound_it.IsUsing()) {
428 return;
429 }
430 *rtn_pipe = last_bound_it.pipeline_state;
431 *rtn_sets = &(last_bound_it.per_set);
432 }
433
434 VkQueueFlags GetQueueFlags() const {
435 return command_pool->queue_flags;
436 }
437
Jeremy Gebbenefead332021-06-18 08:22:44 -0600438 template <typename Barrier>
Jeremy Gebben7ba56152021-06-18 10:40:10 -0600439 inline bool IsReleaseOp(const Barrier &barrier) const {
440 return (IsTransferOp(barrier)) && (command_pool->queueFamilyIndex == barrier.srcQueueFamilyIndex);
441 }
442 template <typename Barrier>
443 inline bool IsAcquireOp(const Barrier &barrier) const {
444 return (IsTransferOp(barrier)) && (command_pool->queueFamilyIndex == barrier.dstQueueFamilyIndex);
Jeremy Gebbenefead332021-06-18 08:22:44 -0600445 }
446
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600447 void Begin(const VkCommandBufferBeginInfo *pBeginInfo);
448 void End(VkResult result);
449
450 void BeginQuery(const QueryObject &query_obj);
451 void EndQuery(const QueryObject &query_obj);
452 void EndQueries(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount);
453 void ResetQueryPool(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount);
454
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600455 void BeginRenderPass(CMD_TYPE cmd_type, const VkRenderPassBeginInfo *pRenderPassBegin, VkSubpassContents contents);
456 void NextSubpass(CMD_TYPE cmd_type, VkSubpassContents contents);
457 void EndRenderPass(CMD_TYPE cmd_type);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600458
Tony-LunarG40b33882021-12-02 12:40:11 -0700459 void BeginRendering(CMD_TYPE cmd_type, const VkRenderingInfo *pRenderingInfo);
amhagana448ea52021-11-02 14:09:14 -0400460
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600461 void ExecuteCommands(uint32_t commandBuffersCount, const VkCommandBuffer *pCommandBuffers);
462
463 void UpdateLastBoundDescriptorSets(VkPipelineBindPoint pipeline_bind_point, const PIPELINE_LAYOUT_STATE *pipeline_layout,
464 uint32_t first_set, uint32_t set_count, const VkDescriptorSet *pDescriptorSets,
Jeremy Gebben4d51c552022-01-06 21:27:15 -0700465 std::shared_ptr<cvdescriptorset::DescriptorSet> &push_descriptor_set,
466 uint32_t dynamic_offset_count, const uint32_t *p_dynamic_offsets);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600467
468 void PushDescriptorSetState(VkPipelineBindPoint pipelineBindPoint, PIPELINE_LAYOUT_STATE *pipeline_layout, uint32_t set,
469 uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pDescriptorWrites);
470
sfricke-samsung85584a72021-09-30 21:43:38 -0700471 void UpdateStateCmdDrawDispatchType(CMD_TYPE cmd_type, VkPipelineBindPoint bind_point);
472 void UpdateStateCmdDrawType(CMD_TYPE cmd_type, VkPipelineBindPoint bind_point);
473 void UpdateDrawState(CMD_TYPE cmd_type, const VkPipelineBindPoint bind_point);
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600474
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600475 virtual void RecordCmd(CMD_TYPE cmd_type);
476 void RecordStateCmd(CMD_TYPE cmd_type, CBStatusFlags state_bits);
ziga-lunarg67b7c392022-03-26 01:45:34 +0100477 void RecordColorWriteEnableStateCmd(CMD_TYPE cmd_type, CBStatusFlags state_bits, uint32_t attachment_count);
Jeremy Gebben9f537102021-10-05 16:37:12 -0600478 void RecordTransferCmd(CMD_TYPE cmd_type, std::shared_ptr<BINDABLE> &&buf1, std::shared_ptr<BINDABLE> &&buf2 = nullptr);
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -0600479 void RecordSetEvent(CMD_TYPE cmd_type, VkEvent event, VkPipelineStageFlags2KHR stageMask);
480 void RecordResetEvent(CMD_TYPE cmd_type, VkEvent event, VkPipelineStageFlags2KHR stageMask);
Jeremy Gebben332d4dd2022-01-01 12:40:02 -0700481 virtual void RecordWaitEvents(CMD_TYPE cmd_type, uint32_t eventCount, const VkEvent *pEvents,
482 VkPipelineStageFlags2KHR src_stage_mask);
Jeremy Gebbencefa7fd2021-08-17 13:44:47 -0600483 void RecordWriteTimestamp(CMD_TYPE cmd_type, VkPipelineStageFlags2KHR pipelineStage, VkQueryPool queryPool, uint32_t slot);
484
485 void RecordBarriers(uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
486 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
487 const VkImageMemoryBarrier *pImageMemoryBarriers);
488 void RecordBarriers(const VkDependencyInfoKHR &dep_info);
Jeremy Gebben10a0ed12021-08-17 09:54:29 -0600489
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600490 void SetImageViewLayout(const IMAGE_VIEW_STATE &view_state, VkImageLayout layout, VkImageLayout layoutStencil);
491 void SetImageViewInitialLayout(const IMAGE_VIEW_STATE &view_state, VkImageLayout layout);
492
493 void SetImageLayout(const IMAGE_STATE &image_state, const VkImageSubresourceRange &image_subresource_range,
494 VkImageLayout layout, VkImageLayout expected_layout = kInvalidLayout);
495 void SetImageLayout(const IMAGE_STATE &image_state, const VkImageSubresourceLayers &image_subresource_layers,
496 VkImageLayout layout);
497 void SetImageInitialLayout(VkImage image, const VkImageSubresourceRange &range, VkImageLayout layout);
498 void SetImageInitialLayout(const IMAGE_STATE &image_state, const VkImageSubresourceRange &range, VkImageLayout layout);
499 void SetImageInitialLayout(const IMAGE_STATE &image_state, const VkImageSubresourceLayers &layers, VkImageLayout layout);
500
Jeremy Gebben57642982021-09-14 14:14:55 -0600501 void Submit(uint32_t perf_submit_pass);
ziga-lunarg96c7d822022-02-28 19:39:17 +0100502 void Retire(uint32_t perf_submit_pass, const std::function<bool(const QueryObject &)> &is_query_updated_after);
Jeremy Gebben57642982021-09-14 14:14:55 -0600503
amhagana448ea52021-11-02 14:09:14 -0400504 uint32_t GetDynamicColorAttachmentCount() {
505 if (activeRenderPass) {
506 if (activeRenderPass->use_dynamic_rendering_inherited) {
507 return activeRenderPass->inheritance_rendering_info.colorAttachmentCount;
508 }
509 if (activeRenderPass->use_dynamic_rendering) {
Aaron Hagan92a44f82021-11-19 09:34:56 -0500510 return activeRenderPass->dynamic_rendering_begin_rendering_info.colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -0400511 }
512 }
513 return 0;
514 }
515 uint32_t GetDynamicColorAttachmentImageIndex(uint32_t index) { return index; }
516 uint32_t GetDynamicColorResolveAttachmentImageIndex(uint32_t index) { return index + GetDynamicColorAttachmentCount(); }
517 uint32_t GetDynamicDepthAttachmentImageIndex() { return 2 * GetDynamicColorAttachmentCount(); }
518 uint32_t GetDynamicDepthResolveAttachmentImageIndex() { return 2 * GetDynamicColorAttachmentCount() + 1; }
519 uint32_t GetDynamicStencilAttachmentImageIndex() { return 2 * GetDynamicColorAttachmentCount() + 2; }
520 uint32_t GetDynamicStencilResolveAttachmentImageIndex() { return 2 * GetDynamicColorAttachmentCount() + 3; }
521
Nathaniel Cesario8d5c9d92022-07-25 12:59:16 -0600522 bool RasterizationDisabled() const;
Nathaniel Cesario62f03592022-07-11 09:19:20 -0600523 inline void BindPipeline(LvlBindPoint bind_point, PIPELINE_STATE *pipe_state) {
524 lastBound[bind_point].pipeline_state = pipe_state;
525 pipeline_bound = true;
526 }
Nathaniel Cesario8d5c9d92022-07-25 12:59:16 -0600527
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600528 protected:
Jeremy Gebbenc9a24032021-11-02 11:36:08 -0600529 void NotifyInvalidate(const BASE_NODE::NodeList &invalid_nodes, bool unlink) override;
Jeremy Gebben1ec89332021-08-05 13:51:49 -0600530 void UpdateAttachmentsView(const VkRenderPassBeginInfo *pRenderPassBegin);
aitor-lunarga131fca2022-02-17 22:55:55 +0100531 void UnbindResources();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -0600532};
Jeremy Gebben7ba56152021-06-18 10:40:10 -0600533
534// specializations for barriers that cannot do queue family ownership transfers
535template <>
536inline bool CMD_BUFFER_STATE::IsReleaseOp(const VkMemoryBarrier &barrier) const {
537 return false;
538}
539template <>
540inline bool CMD_BUFFER_STATE::IsReleaseOp(const VkMemoryBarrier2KHR &barrier) const {
541 return false;
542}
543template <>
544inline bool CMD_BUFFER_STATE::IsReleaseOp(const VkSubpassDependency2 &barrier) const {
545 return false;
546}
547template <>
548inline bool CMD_BUFFER_STATE::IsAcquireOp(const VkMemoryBarrier &barrier) const {
549 return false;
550}
551template <>
552inline bool CMD_BUFFER_STATE::IsAcquireOp(const VkMemoryBarrier2KHR &barrier) const {
553 return false;
554}
555template <>
556inline bool CMD_BUFFER_STATE::IsAcquireOp(const VkSubpassDependency2 &barrier) const {
557 return false;
558}