blob: 4899c2bffe54275a5742d703015b4f865b4a56f1 [file] [log] [blame]
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001/* Copyright (c) 2015-2021 The Khronos Group Inc.
2 * Copyright (c) 2015-2021 Valve Corporation
3 * Copyright (c) 2015-2021 LunarG, Inc.
4 * Copyright (C) 2015-2021 Google Inc.
5 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Author: Courtney Goeltzenleuchter <courtneygo@google.com>
20 * Author: Tobin Ehlis <tobine@google.com>
21 * Author: Chris Forbes <chrisf@ijw.co.nz>
22 * Author: Mark Lobodzinski <mark@lunarg.com>
23 * Author: Dave Houlton <daveh@lunarg.com>
24 * Author: John Zulauf <jzulauf@lunarg.com>
25 * Author: Tobias Hector <tobias.hector@amd.com>
26 */
27#include "cmd_buffer_state.h"
28#include "render_pass_state.h"
29
30const char *CommandTypeString(CMD_TYPE type) {
31 // Autogenerated as part of the command_validation.h codegen
32 return kGeneratedCommandNameList[type];
33}
34
35VkDynamicState ConvertToDynamicState(CBStatusFlagBits flag) {
36 switch (flag) {
37 case CBSTATUS_LINE_WIDTH_SET:
38 return VK_DYNAMIC_STATE_LINE_WIDTH;
39 case CBSTATUS_DEPTH_BIAS_SET:
40 return VK_DYNAMIC_STATE_DEPTH_BIAS;
41 case CBSTATUS_BLEND_CONSTANTS_SET:
42 return VK_DYNAMIC_STATE_BLEND_CONSTANTS;
43 case CBSTATUS_DEPTH_BOUNDS_SET:
44 return VK_DYNAMIC_STATE_DEPTH_BOUNDS;
45 case CBSTATUS_STENCIL_READ_MASK_SET:
46 return VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK;
47 case CBSTATUS_STENCIL_WRITE_MASK_SET:
48 return VK_DYNAMIC_STATE_STENCIL_WRITE_MASK;
49 case CBSTATUS_STENCIL_REFERENCE_SET:
50 return VK_DYNAMIC_STATE_STENCIL_REFERENCE;
51 case CBSTATUS_VIEWPORT_SET:
52 return VK_DYNAMIC_STATE_VIEWPORT;
53 case CBSTATUS_SCISSOR_SET:
54 return VK_DYNAMIC_STATE_SCISSOR;
55 case CBSTATUS_EXCLUSIVE_SCISSOR_SET:
56 return VK_DYNAMIC_STATE_EXCLUSIVE_SCISSOR_NV;
57 case CBSTATUS_SHADING_RATE_PALETTE_SET:
58 return VK_DYNAMIC_STATE_VIEWPORT_SHADING_RATE_PALETTE_NV;
59 case CBSTATUS_LINE_STIPPLE_SET:
60 return VK_DYNAMIC_STATE_LINE_STIPPLE_EXT;
61 case CBSTATUS_VIEWPORT_W_SCALING_SET:
62 return VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV;
63 case CBSTATUS_CULL_MODE_SET:
64 return VK_DYNAMIC_STATE_CULL_MODE_EXT;
65 case CBSTATUS_FRONT_FACE_SET:
66 return VK_DYNAMIC_STATE_FRONT_FACE_EXT;
67 case CBSTATUS_PRIMITIVE_TOPOLOGY_SET:
68 return VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT;
69 case CBSTATUS_VIEWPORT_WITH_COUNT_SET:
70 return VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT;
71 case CBSTATUS_SCISSOR_WITH_COUNT_SET:
72 return VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT;
73 case CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET:
74 return VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT;
75 case CBSTATUS_DEPTH_TEST_ENABLE_SET:
76 return VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT;
77 case CBSTATUS_DEPTH_WRITE_ENABLE_SET:
78 return VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT;
79 case CBSTATUS_DEPTH_COMPARE_OP_SET:
80 return VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT;
81 case CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET:
82 return VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT;
83 case CBSTATUS_STENCIL_TEST_ENABLE_SET:
84 return VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT;
85 case CBSTATUS_STENCIL_OP_SET:
86 return VK_DYNAMIC_STATE_STENCIL_OP_EXT;
87 case CBSTATUS_DISCARD_RECTANGLE_SET:
88 return VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT;
89 case CBSTATUS_SAMPLE_LOCATIONS_SET:
90 return VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT;
91 case CBSTATUS_COARSE_SAMPLE_ORDER_SET:
92 return VK_DYNAMIC_STATE_VIEWPORT_COARSE_SAMPLE_ORDER_NV;
93 case CBSTATUS_PATCH_CONTROL_POINTS_SET:
94 return VK_DYNAMIC_STATE_PATCH_CONTROL_POINTS_EXT;
95 case CBSTATUS_RASTERIZER_DISCARD_ENABLE_SET:
96 return VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT;
97 case CBSTATUS_DEPTH_BIAS_ENABLE_SET:
98 return VK_DYNAMIC_STATE_DEPTH_BIAS_ENABLE_EXT;
99 case CBSTATUS_LOGIC_OP_SET:
100 return VK_DYNAMIC_STATE_LOGIC_OP_EXT;
101 case CBSTATUS_PRIMITIVE_RESTART_ENABLE_SET:
102 return VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE_EXT;
103 case CBSTATUS_VERTEX_INPUT_SET:
104 return VK_DYNAMIC_STATE_VERTEX_INPUT_EXT;
105 default:
106 // CBSTATUS_INDEX_BUFFER_BOUND is not in VkDynamicState
107 return VK_DYNAMIC_STATE_MAX_ENUM;
108 }
109 return VK_DYNAMIC_STATE_MAX_ENUM;
110}
111
112CBStatusFlagBits ConvertToCBStatusFlagBits(VkDynamicState state) {
113 switch (state) {
114 case VK_DYNAMIC_STATE_VIEWPORT:
115 return CBSTATUS_VIEWPORT_SET;
116 case VK_DYNAMIC_STATE_SCISSOR:
117 return CBSTATUS_SCISSOR_SET;
118 case VK_DYNAMIC_STATE_LINE_WIDTH:
119 return CBSTATUS_LINE_WIDTH_SET;
120 case VK_DYNAMIC_STATE_DEPTH_BIAS:
121 return CBSTATUS_DEPTH_BIAS_SET;
122 case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
123 return CBSTATUS_BLEND_CONSTANTS_SET;
124 case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
125 return CBSTATUS_DEPTH_BOUNDS_SET;
126 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
127 return CBSTATUS_STENCIL_READ_MASK_SET;
128 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
129 return CBSTATUS_STENCIL_WRITE_MASK_SET;
130 case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
131 return CBSTATUS_STENCIL_REFERENCE_SET;
132 case VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV:
133 return CBSTATUS_VIEWPORT_W_SCALING_SET;
134 case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT:
135 return CBSTATUS_DISCARD_RECTANGLE_SET;
136 case VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT:
137 return CBSTATUS_SAMPLE_LOCATIONS_SET;
138 case VK_DYNAMIC_STATE_VIEWPORT_SHADING_RATE_PALETTE_NV:
139 return CBSTATUS_SHADING_RATE_PALETTE_SET;
140 case VK_DYNAMIC_STATE_VIEWPORT_COARSE_SAMPLE_ORDER_NV:
141 return CBSTATUS_COARSE_SAMPLE_ORDER_SET;
142 case VK_DYNAMIC_STATE_EXCLUSIVE_SCISSOR_NV:
143 return CBSTATUS_EXCLUSIVE_SCISSOR_SET;
144 case VK_DYNAMIC_STATE_LINE_STIPPLE_EXT:
145 return CBSTATUS_LINE_STIPPLE_SET;
146 case VK_DYNAMIC_STATE_CULL_MODE_EXT:
147 return CBSTATUS_CULL_MODE_SET;
148 case VK_DYNAMIC_STATE_FRONT_FACE_EXT:
149 return CBSTATUS_FRONT_FACE_SET;
150 case VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT:
151 return CBSTATUS_PRIMITIVE_TOPOLOGY_SET;
152 case VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT:
153 return CBSTATUS_VIEWPORT_WITH_COUNT_SET;
154 case VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT:
155 return CBSTATUS_SCISSOR_WITH_COUNT_SET;
156 case VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT:
157 return CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET;
158 case VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT:
159 return CBSTATUS_DEPTH_TEST_ENABLE_SET;
160 case VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT:
161 return CBSTATUS_DEPTH_WRITE_ENABLE_SET;
162 case VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT:
163 return CBSTATUS_DEPTH_COMPARE_OP_SET;
164 case VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT:
165 return CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET;
166 case VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT:
167 return CBSTATUS_STENCIL_TEST_ENABLE_SET;
168 case VK_DYNAMIC_STATE_STENCIL_OP_EXT:
169 return CBSTATUS_STENCIL_OP_SET;
170 case VK_DYNAMIC_STATE_PATCH_CONTROL_POINTS_EXT:
171 return CBSTATUS_PATCH_CONTROL_POINTS_SET;
172 case VK_DYNAMIC_STATE_RASTERIZER_DISCARD_ENABLE_EXT:
173 return CBSTATUS_RASTERIZER_DISCARD_ENABLE_SET;
174 case VK_DYNAMIC_STATE_DEPTH_BIAS_ENABLE_EXT:
175 return CBSTATUS_DEPTH_BIAS_ENABLE_SET;
176 case VK_DYNAMIC_STATE_LOGIC_OP_EXT:
177 return CBSTATUS_LOGIC_OP_SET;
178 case VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE_EXT:
179 return CBSTATUS_PRIMITIVE_RESTART_ENABLE_SET;
180 case VK_DYNAMIC_STATE_VERTEX_INPUT_EXT:
181 return CBSTATUS_VERTEX_INPUT_SET;
182 default:
183 return CBSTATUS_NONE;
184 }
185 return CBSTATUS_NONE;
186}
187
188// Get the image viewstate for a given framebuffer attachment
189IMAGE_VIEW_STATE *CMD_BUFFER_STATE::GetActiveAttachmentImageViewState(uint32_t index) {
190 assert(active_attachments && index != VK_ATTACHMENT_UNUSED && (index < active_attachments->size()));
191 return active_attachments->at(index);
192}
193
194// Get the image viewstate for a given framebuffer attachment
195const IMAGE_VIEW_STATE *CMD_BUFFER_STATE::GetActiveAttachmentImageViewState(uint32_t index) const {
196 assert(active_attachments && index != VK_ATTACHMENT_UNUSED && (index < active_attachments->size()));
197 return active_attachments->at(index);
198}
199
200void CMD_BUFFER_STATE::AddChild(BASE_NODE *child_node) {
201 if (child_node->AddParent(this)) {
202 object_bindings.insert(child_node->Handle());
203 }
204}
205
206void CMD_BUFFER_STATE::RemoveChild(BASE_NODE *child_node) {
207 child_node->RemoveParent(this);
208 object_bindings.erase(child_node->Handle());
209}
210
211void CMD_BUFFER_STATE::Reset() {
212 ResetUse();
213 // Reset CB state (note that createInfo is not cleared)
214 memset(&beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
215 memset(&inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo));
216 hasDrawCmd = false;
217 hasTraceRaysCmd = false;
218 hasBuildAccelerationStructureCmd = false;
219 hasDispatchCmd = false;
220 state = CB_NEW;
221 commandCount = 0;
222 submitCount = 0;
223 image_layout_change_count = 1; // Start at 1. 0 is insert value for validation cache versions, s.t. new == dirty
224 status = 0;
225 static_status = 0;
226 inheritedViewportDepths.clear();
227 usedViewportScissorCount = 0;
228 pipelineStaticViewportCount = 0;
229 pipelineStaticScissorCount = 0;
230 viewportMask = 0;
231 viewportWithCountMask = 0;
232 viewportWithCountCount = 0;
233 scissorMask = 0;
234 scissorWithCountMask = 0;
235 scissorWithCountCount = 0;
236 trashedViewportMask = 0;
237 trashedScissorMask = 0;
238 trashedViewportCount = false;
239 trashedScissorCount = false;
240 usedDynamicViewportCount = false;
241 usedDynamicScissorCount = false;
242 primitiveTopology = VK_PRIMITIVE_TOPOLOGY_MAX_ENUM;
243
244 activeRenderPassBeginInfo = safe_VkRenderPassBeginInfo();
245 activeRenderPass = nullptr;
246 active_attachments = nullptr;
247 active_subpasses = nullptr;
248 attachments_view_states.clear();
249 activeSubpassContents = VK_SUBPASS_CONTENTS_INLINE;
250 activeSubpass = 0;
251 broken_bindings.clear();
252 waitedEvents.clear();
253 events.clear();
254 writeEventsBeforeWait.clear();
255 activeQueries.clear();
256 startedQueries.clear();
257 image_layout_map.clear();
258 current_vertex_buffer_binding_info.vertex_buffer_bindings.clear();
259 vertex_buffer_used = false;
260 primaryCommandBuffer = VK_NULL_HANDLE;
261
262 linkedCommandBuffers.clear();
263 // Remove reverse command buffer links.
264 Invalidate(true);
265
266 queue_submit_functions.clear();
267 cmd_execute_commands_functions.clear();
268 eventUpdates.clear();
269 queryUpdates.clear();
270
271 // Remove object bindings
272 for (const auto &obj : object_bindings) {
273 if (obj.node) {
274 obj.node->RemoveParent(this);
275 }
276 }
277 object_bindings.clear();
278
279 for (auto &item : lastBound) {
280 item.Reset();
281 }
282 // Remove this cmdBuffer's reference from each FrameBuffer's CB ref list
283 for (auto &framebuffer : framebuffers) {
284 framebuffer->RemoveParent(this);
285 }
286 framebuffers.clear();
287 activeFramebuffer = VK_NULL_HANDLE;
288 index_buffer_binding.reset();
289
290 qfo_transfer_image_barriers.Reset();
291 qfo_transfer_buffer_barriers.Reset();
292
293 // Clean up the label data
294 debug_label.Reset();
295 validate_descriptorsets_in_queuesubmit.clear();
296
297 // Best practices info
298 small_indexed_draw_call_count = 0;
299
300 transform_feedback_active = false;
301
302 // Remove object bindings
303 for (const auto &obj : object_bindings) {
304 BASE_NODE *base_obj = obj.node;
305 if (base_obj) RemoveChild(base_obj);
306 }
307 object_bindings.clear();
308}
309
310void CMD_BUFFER_STATE::Destroy() {
311 Reset();
312 BASE_NODE::Destroy();
313}
314
315void CMD_BUFFER_STATE::NotifyInvalidate(const LogObjectList &invalid_handles, bool unlink) {
316 if (state == CB_RECORDING) {
317 state = CB_INVALID_INCOMPLETE;
318 } else if (state == CB_RECORDED) {
319 state = CB_INVALID_COMPLETE;
320 }
321 assert(!invalid_handles.object_list.empty());
322 broken_bindings.emplace(invalid_handles.object_list[0], invalid_handles);
323
324 if (unlink) {
325 for (auto &obj : invalid_handles.object_list) {
326 object_bindings.erase(obj);
327 if (obj.type == kVulkanObjectTypeCommandBuffer) {
328 linkedCommandBuffers.erase(static_cast<CMD_BUFFER_STATE *>(obj.node));
329 }
330 }
331 }
332 BASE_NODE::NotifyInvalidate(invalid_handles, unlink);
333}
334
335// The const variant only need the image as it is the key for the map
336const ImageSubresourceLayoutMap *CMD_BUFFER_STATE::GetImageSubresourceLayoutMap(VkImage image) const {
337 auto it = image_layout_map.find(image);
338 if (it == image_layout_map.cend()) {
339 return nullptr;
340 }
341 return &it->second;
342}
343
344// The non-const variant only needs the image state, as the factory requires it to construct a new entry
345ImageSubresourceLayoutMap *CMD_BUFFER_STATE::GetImageSubresourceLayoutMap(const IMAGE_STATE &image_state) {
346 auto &layout_map = image_layout_map[image_state.image()];
347 if (!layout_map) {
348 // Was an empty slot... fill it in.
349 layout_map.emplace(image_state);
350 }
351 return &layout_map;
352}
353