blob: 4f5fe1811345a830ed85d78752fce2d0580ff005 [file] [log] [blame]
Tony-LunarG73719992020-01-15 10:20:28 -07001/* Copyright (c) 2015-2020 The Khronos Group Inc.
2 * Copyright (c) 2015-2020 Valve Corporation
3 * Copyright (c) 2015-2020 LunarG, Inc.
4 * Copyright (C) 2015-2020 Google Inc.
Tobias Hector6663c9b2020-11-05 10:18:02 +00005 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
locke-lunargd556cc32019-09-17 01:21:23 -06006 *
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: Mark Lobodzinski <mark@lunarg.com>
20 * Author: Dave Houlton <daveh@lunarg.com>
21 * Shannon McPherson <shannon@lunarg.com>
Tobias Hector6663c9b2020-11-05 10:18:02 +000022 * Author: Tobias Hector <tobias.hector@amd.com>
locke-lunargd556cc32019-09-17 01:21:23 -060023 */
24
locke-lunargd556cc32019-09-17 01:21:23 -060025#include <cmath>
26#include <set>
locke-lunargd556cc32019-09-17 01:21:23 -060027
28#include "vk_enum_string_helper.h"
29#include "vk_format_utils.h"
30#include "vk_layer_data.h"
31#include "vk_layer_utils.h"
32#include "vk_layer_logging.h"
33#include "vk_typemap_helper.h"
34
35#include "chassis.h"
36#include "state_tracker.h"
37#include "shader_validation.h"
38
John Zulauf890b50b2020-06-17 15:18:19 -060039const char *CommandTypeString(CMD_TYPE type) {
40 // Autogenerated as part of the vk_validation_error_message.h codegen
Mark Lobodzinski677dc0b2020-11-12 15:28:09 -070041 return kGeneratedCommandNameList[type];
John Zulauf890b50b2020-06-17 15:18:19 -060042}
43
locke-lunarg4189aa22020-10-21 00:23:48 -060044VkDynamicState ConvertToDynamicState(CBStatusFlagBits flag) {
45 switch (flag) {
46 case CBSTATUS_LINE_WIDTH_SET:
47 return VK_DYNAMIC_STATE_LINE_WIDTH;
48 case CBSTATUS_DEPTH_BIAS_SET:
49 return VK_DYNAMIC_STATE_DEPTH_BIAS;
50 case CBSTATUS_BLEND_CONSTANTS_SET:
51 return VK_DYNAMIC_STATE_BLEND_CONSTANTS;
52 case CBSTATUS_DEPTH_BOUNDS_SET:
53 return VK_DYNAMIC_STATE_DEPTH_BOUNDS;
54 case CBSTATUS_STENCIL_READ_MASK_SET:
55 return VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK;
56 case CBSTATUS_STENCIL_WRITE_MASK_SET:
57 return VK_DYNAMIC_STATE_STENCIL_WRITE_MASK;
58 case CBSTATUS_STENCIL_REFERENCE_SET:
59 return VK_DYNAMIC_STATE_STENCIL_REFERENCE;
60 case CBSTATUS_VIEWPORT_SET:
61 return VK_DYNAMIC_STATE_VIEWPORT;
62 case CBSTATUS_SCISSOR_SET:
63 return VK_DYNAMIC_STATE_SCISSOR;
64 case CBSTATUS_EXCLUSIVE_SCISSOR_SET:
65 return VK_DYNAMIC_STATE_EXCLUSIVE_SCISSOR_NV;
66 case CBSTATUS_SHADING_RATE_PALETTE_SET:
67 return VK_DYNAMIC_STATE_VIEWPORT_SHADING_RATE_PALETTE_NV;
68 case CBSTATUS_LINE_STIPPLE_SET:
69 return VK_DYNAMIC_STATE_LINE_STIPPLE_EXT;
70 case CBSTATUS_VIEWPORT_W_SCALING_SET:
71 return VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV;
72 case CBSTATUS_CULL_MODE_SET:
73 return VK_DYNAMIC_STATE_CULL_MODE_EXT;
74 case CBSTATUS_FRONT_FACE_SET:
75 return VK_DYNAMIC_STATE_FRONT_FACE_EXT;
76 case CBSTATUS_PRIMITIVE_TOPOLOGY_SET:
77 return VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT;
78 case CBSTATUS_VIEWPORT_WITH_COUNT_SET:
79 return VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT;
80 case CBSTATUS_SCISSOR_WITH_COUNT_SET:
81 return VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT;
82 case CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET:
83 return VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT;
84 case CBSTATUS_DEPTH_TEST_ENABLE_SET:
85 return VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT;
86 case CBSTATUS_DEPTH_WRITE_ENABLE_SET:
87 return VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT;
88 case CBSTATUS_DEPTH_COMPARE_OP_SET:
89 return VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT;
90 case CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET:
91 return VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT;
92 case CBSTATUS_STENCIL_TEST_ENABLE_SET:
93 return VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT;
94 case CBSTATUS_STENCIL_OP_SET:
95 return VK_DYNAMIC_STATE_STENCIL_OP_EXT;
96 case CBSTATUS_DISCARD_RECTANGLE_SET:
97 return VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT;
98 case CBSTATUS_SAMPLE_LOCATIONS_SET:
99 return VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT;
100 case CBSTATUS_COARSE_SAMPLE_ORDER_SET:
101 return VK_DYNAMIC_STATE_VIEWPORT_COARSE_SAMPLE_ORDER_NV;
102 default:
103 // CBSTATUS_INDEX_BUFFER_BOUND is not in VkDynamicState
104 return VK_DYNAMIC_STATE_MAX_ENUM;
105 }
106 return VK_DYNAMIC_STATE_MAX_ENUM;
107}
108
109CBStatusFlagBits ConvertToCBStatusFlagBits(VkDynamicState state) {
110 switch (state) {
111 case VK_DYNAMIC_STATE_VIEWPORT:
112 return CBSTATUS_VIEWPORT_SET;
113 case VK_DYNAMIC_STATE_SCISSOR:
114 return CBSTATUS_SCISSOR_SET;
115 case VK_DYNAMIC_STATE_LINE_WIDTH:
116 return CBSTATUS_LINE_WIDTH_SET;
117 case VK_DYNAMIC_STATE_DEPTH_BIAS:
118 return CBSTATUS_DEPTH_BIAS_SET;
119 case VK_DYNAMIC_STATE_BLEND_CONSTANTS:
120 return CBSTATUS_BLEND_CONSTANTS_SET;
121 case VK_DYNAMIC_STATE_DEPTH_BOUNDS:
122 return CBSTATUS_DEPTH_BOUNDS_SET;
123 case VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK:
124 return CBSTATUS_STENCIL_READ_MASK_SET;
125 case VK_DYNAMIC_STATE_STENCIL_WRITE_MASK:
126 return CBSTATUS_STENCIL_WRITE_MASK_SET;
127 case VK_DYNAMIC_STATE_STENCIL_REFERENCE:
128 return CBSTATUS_STENCIL_REFERENCE_SET;
129 case VK_DYNAMIC_STATE_VIEWPORT_W_SCALING_NV:
130 return CBSTATUS_VIEWPORT_W_SCALING_SET;
131 case VK_DYNAMIC_STATE_DISCARD_RECTANGLE_EXT:
132 return CBSTATUS_DISCARD_RECTANGLE_SET;
133 case VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT:
134 return CBSTATUS_SAMPLE_LOCATIONS_SET;
135 case VK_DYNAMIC_STATE_VIEWPORT_SHADING_RATE_PALETTE_NV:
136 return CBSTATUS_SHADING_RATE_PALETTE_SET;
137 case VK_DYNAMIC_STATE_VIEWPORT_COARSE_SAMPLE_ORDER_NV:
138 return CBSTATUS_COARSE_SAMPLE_ORDER_SET;
139 case VK_DYNAMIC_STATE_EXCLUSIVE_SCISSOR_NV:
140 return CBSTATUS_EXCLUSIVE_SCISSOR_SET;
141 case VK_DYNAMIC_STATE_LINE_STIPPLE_EXT:
142 return CBSTATUS_LINE_STIPPLE_SET;
143 case VK_DYNAMIC_STATE_CULL_MODE_EXT:
144 return CBSTATUS_CULL_MODE_SET;
145 case VK_DYNAMIC_STATE_FRONT_FACE_EXT:
146 return CBSTATUS_FRONT_FACE_SET;
147 case VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY_EXT:
148 return CBSTATUS_PRIMITIVE_TOPOLOGY_SET;
149 case VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT:
150 return CBSTATUS_VIEWPORT_WITH_COUNT_SET;
151 case VK_DYNAMIC_STATE_SCISSOR_WITH_COUNT_EXT:
152 return CBSTATUS_SCISSOR_WITH_COUNT_SET;
153 case VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE_EXT:
154 return CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET;
155 case VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE_EXT:
156 return CBSTATUS_DEPTH_TEST_ENABLE_SET;
157 case VK_DYNAMIC_STATE_DEPTH_WRITE_ENABLE_EXT:
158 return CBSTATUS_DEPTH_WRITE_ENABLE_SET;
159 case VK_DYNAMIC_STATE_DEPTH_COMPARE_OP_EXT:
160 return CBSTATUS_DEPTH_COMPARE_OP_SET;
161 case VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT:
162 return CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET;
163 case VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT:
164 return CBSTATUS_STENCIL_TEST_ENABLE_SET;
165 case VK_DYNAMIC_STATE_STENCIL_OP_EXT:
166 return CBSTATUS_STENCIL_OP_SET;
167 default:
168 return CBSTATUS_NONE;
169 }
170 return CBSTATUS_NONE;
171}
172
Mark Lobodzinskib4ab6ac2020-04-02 13:12:06 -0600173void ValidationStateTracker::InitDeviceValidationObject(bool add_obj, ValidationObject *inst_obj, ValidationObject *dev_obj) {
174 if (add_obj) {
175 instance_state = reinterpret_cast<ValidationStateTracker *>(GetValidationObject(inst_obj->object_dispatch, container_type));
176 // Call base class
177 ValidationObject::InitDeviceValidationObject(add_obj, inst_obj, dev_obj);
178 }
179}
180
John Zulauf5c5e88d2019-12-26 11:22:02 -0700181uint32_t ResolveRemainingLevels(const VkImageSubresourceRange *range, uint32_t mip_levels) {
182 // Return correct number of mip levels taking into account VK_REMAINING_MIP_LEVELS
183 uint32_t mip_level_count = range->levelCount;
184 if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
185 mip_level_count = mip_levels - range->baseMipLevel;
186 }
187 return mip_level_count;
188}
189
190uint32_t ResolveRemainingLayers(const VkImageSubresourceRange *range, uint32_t layers) {
191 // Return correct number of layers taking into account VK_REMAINING_ARRAY_LAYERS
192 uint32_t array_layer_count = range->layerCount;
193 if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
194 array_layer_count = layers - range->baseArrayLayer;
195 }
196 return array_layer_count;
197}
198
199VkImageSubresourceRange NormalizeSubresourceRange(const VkImageCreateInfo &image_create_info,
200 const VkImageSubresourceRange &range) {
201 VkImageSubresourceRange norm = range;
202 norm.levelCount = ResolveRemainingLevels(&range, image_create_info.mipLevels);
203
204 // Special case for 3D images with VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT_KHR flag bit, where <extent.depth> and
205 // <arrayLayers> can potentially alias.
206 uint32_t layer_limit = (0 != (image_create_info.flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT_KHR))
207 ? image_create_info.extent.depth
208 : image_create_info.arrayLayers;
209 norm.layerCount = ResolveRemainingLayers(&range, layer_limit);
210
211 // For multiplanar formats, IMAGE_ASPECT_COLOR is equivalent to adding the aspect of the individual planes
212 VkImageAspectFlags &aspect_mask = norm.aspectMask;
213 if (FormatIsMultiplane(image_create_info.format)) {
214 if (aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) {
215 aspect_mask &= ~VK_IMAGE_ASPECT_COLOR_BIT;
216 aspect_mask |= (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT);
217 if (FormatPlaneCount(image_create_info.format) > 2) {
218 aspect_mask |= VK_IMAGE_ASPECT_PLANE_2_BIT;
219 }
220 }
221 }
222 return norm;
223}
224
225VkImageSubresourceRange NormalizeSubresourceRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &range) {
226 const VkImageCreateInfo &image_create_info = image_state.createInfo;
227 return NormalizeSubresourceRange(image_create_info, range);
228}
229
John Zulauf2bc1fde2020-04-24 15:09:51 -0600230// NOTE: Beware the lifespan of the rp_begin when holding the return. If the rp_begin isn't a "safe" copy, "IMAGELESS"
231// attachments won't persist past the API entry point exit.
232std::pair<uint32_t, const VkImageView *> GetFramebufferAttachments(const VkRenderPassBeginInfo &rp_begin,
233 const FRAMEBUFFER_STATE &fb_state) {
234 const VkImageView *attachments = fb_state.createInfo.pAttachments;
235 uint32_t count = fb_state.createInfo.attachmentCount;
236 if (fb_state.createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) {
237 const auto *framebuffer_attachments = lvl_find_in_chain<VkRenderPassAttachmentBeginInfo>(rp_begin.pNext);
238 if (framebuffer_attachments) {
239 attachments = framebuffer_attachments->pAttachments;
240 count = framebuffer_attachments->attachmentCount;
241 }
242 }
243 return std::make_pair(count, attachments);
244}
245
246std::vector<const IMAGE_VIEW_STATE *> ValidationStateTracker::GetAttachmentViews(const VkRenderPassBeginInfo &rp_begin,
247 const FRAMEBUFFER_STATE &fb_state) const {
248 std::vector<const IMAGE_VIEW_STATE *> views;
249
250 const auto count_attachment = GetFramebufferAttachments(rp_begin, fb_state);
251 const auto attachment_count = count_attachment.first;
252 const auto *attachments = count_attachment.second;
253 views.resize(attachment_count, nullptr);
254 for (uint32_t i = 0; i < attachment_count; i++) {
255 if (attachments[i] != VK_NULL_HANDLE) {
256 views[i] = Get<IMAGE_VIEW_STATE>(attachments[i]);
257 }
258 }
259 return views;
260}
261
262std::vector<const IMAGE_VIEW_STATE *> ValidationStateTracker::GetCurrentAttachmentViews(const CMD_BUFFER_STATE &cb_state) const {
263 // Only valid *after* RecordBeginRenderPass and *before* RecordEndRenderpass as it relies on cb_state for the renderpass info.
264 std::vector<const IMAGE_VIEW_STATE *> views;
265
locke-lunargaecf2152020-05-12 17:15:41 -0600266 const auto *rp_state = cb_state.activeRenderPass.get();
John Zulauf2bc1fde2020-04-24 15:09:51 -0600267 if (!rp_state) return views;
268 const auto &rp_begin = *cb_state.activeRenderPassBeginInfo.ptr();
269 const auto *fb_state = Get<FRAMEBUFFER_STATE>(rp_begin.framebuffer);
270 if (!fb_state) return views;
271
272 return GetAttachmentViews(rp_begin, *fb_state);
273}
274
locke-lunarg3e127c72020-06-09 17:45:28 -0600275PIPELINE_STATE *GetCurrentPipelineFromCommandBuffer(const CMD_BUFFER_STATE &cmd, VkPipelineBindPoint pipelineBindPoint) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -0600276 const auto lv_bind_point = ConvertToLvlBindPoint(pipelineBindPoint);
277 return cmd.lastBound[lv_bind_point].pipeline_state;
locke-lunarg3e127c72020-06-09 17:45:28 -0600278}
279
280void GetCurrentPipelineAndDesriptorSetsFromCommandBuffer(const CMD_BUFFER_STATE &cmd, VkPipelineBindPoint pipelineBindPoint,
281 const PIPELINE_STATE **rtn_pipe,
282 const std::vector<LAST_BOUND_STATE::PER_SET> **rtn_sets) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -0600283 const auto lv_bind_point = ConvertToLvlBindPoint(pipelineBindPoint);
284 const auto &last_bound_it = cmd.lastBound[lv_bind_point];
285 if (!last_bound_it.IsUsing()) {
locke-lunarg3e127c72020-06-09 17:45:28 -0600286 return;
287 }
locke-lunargb8d7a7a2020-10-25 16:01:52 -0600288 *rtn_pipe = last_bound_it.pipeline_state;
289 *rtn_sets = &(last_bound_it.per_set);
locke-lunarg3e127c72020-06-09 17:45:28 -0600290}
291
locke-lunargd556cc32019-09-17 01:21:23 -0600292#ifdef VK_USE_PLATFORM_ANDROID_KHR
293// Android-specific validation that uses types defined only with VK_USE_PLATFORM_ANDROID_KHR
294// This could also move into a seperate core_validation_android.cpp file... ?
295
296void ValidationStateTracker::RecordCreateImageANDROID(const VkImageCreateInfo *create_info, IMAGE_STATE *is_node) {
297 const VkExternalMemoryImageCreateInfo *emici = lvl_find_in_chain<VkExternalMemoryImageCreateInfo>(create_info->pNext);
298 if (emici && (emici->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)) {
Spencer Fricke224c9852020-04-06 07:47:29 -0700299 is_node->external_ahb = true;
locke-lunargd556cc32019-09-17 01:21:23 -0600300 }
301 const VkExternalFormatANDROID *ext_fmt_android = lvl_find_in_chain<VkExternalFormatANDROID>(create_info->pNext);
302 if (ext_fmt_android && (0 != ext_fmt_android->externalFormat)) {
303 is_node->has_ahb_format = true;
304 is_node->ahb_format = ext_fmt_android->externalFormat;
Spencer Fricke6bba8c72020-04-06 07:41:21 -0700305 // VUID 01894 will catch if not found in map
306 auto it = ahb_ext_formats_map.find(ext_fmt_android->externalFormat);
307 if (it != ahb_ext_formats_map.end()) {
308 is_node->format_features = it->second;
309 }
locke-lunargd556cc32019-09-17 01:21:23 -0600310 }
311}
312
sfricke-samsung013f1ef2020-05-14 22:56:20 -0700313void ValidationStateTracker::RecordCreateBufferANDROID(const VkBufferCreateInfo *create_info, BUFFER_STATE *bs_node) {
314 const VkExternalMemoryBufferCreateInfo *embci = lvl_find_in_chain<VkExternalMemoryBufferCreateInfo>(create_info->pNext);
315 if (embci && (embci->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)) {
316 bs_node->external_ahb = true;
317 }
318}
319
locke-lunargd556cc32019-09-17 01:21:23 -0600320void ValidationStateTracker::RecordCreateSamplerYcbcrConversionANDROID(const VkSamplerYcbcrConversionCreateInfo *create_info,
sfricke-samsungbe3584f2020-04-22 14:58:06 -0700321 VkSamplerYcbcrConversion ycbcr_conversion,
322 SAMPLER_YCBCR_CONVERSION_STATE *ycbcr_state) {
locke-lunargd556cc32019-09-17 01:21:23 -0600323 const VkExternalFormatANDROID *ext_format_android = lvl_find_in_chain<VkExternalFormatANDROID>(create_info->pNext);
324 if (ext_format_android && (0 != ext_format_android->externalFormat)) {
325 ycbcr_conversion_ahb_fmt_map.emplace(ycbcr_conversion, ext_format_android->externalFormat);
sfricke-samsungbe3584f2020-04-22 14:58:06 -0700326 // VUID 01894 will catch if not found in map
327 auto it = ahb_ext_formats_map.find(ext_format_android->externalFormat);
328 if (it != ahb_ext_formats_map.end()) {
329 ycbcr_state->format_features = it->second;
330 }
locke-lunargd556cc32019-09-17 01:21:23 -0600331 }
332};
333
334void ValidationStateTracker::RecordDestroySamplerYcbcrConversionANDROID(VkSamplerYcbcrConversion ycbcr_conversion) {
335 ycbcr_conversion_ahb_fmt_map.erase(ycbcr_conversion);
336};
337
Spencer Fricke6bba8c72020-04-06 07:41:21 -0700338void ValidationStateTracker::PostCallRecordGetAndroidHardwareBufferPropertiesANDROID(
339 VkDevice device, const struct AHardwareBuffer *buffer, VkAndroidHardwareBufferPropertiesANDROID *pProperties, VkResult result) {
340 if (VK_SUCCESS != result) return;
341 auto ahb_format_props = lvl_find_in_chain<VkAndroidHardwareBufferFormatPropertiesANDROID>(pProperties->pNext);
342 if (ahb_format_props) {
343 ahb_ext_formats_map.insert({ahb_format_props->externalFormat, ahb_format_props->formatFeatures});
344 }
345}
346
locke-lunargd556cc32019-09-17 01:21:23 -0600347#else
348
349void ValidationStateTracker::RecordCreateImageANDROID(const VkImageCreateInfo *create_info, IMAGE_STATE *is_node) {}
350
sfricke-samsung013f1ef2020-05-14 22:56:20 -0700351void ValidationStateTracker::RecordCreateBufferANDROID(const VkBufferCreateInfo *create_info, BUFFER_STATE *bs_node) {}
352
locke-lunargd556cc32019-09-17 01:21:23 -0600353void ValidationStateTracker::RecordCreateSamplerYcbcrConversionANDROID(const VkSamplerYcbcrConversionCreateInfo *create_info,
sfricke-samsungbe3584f2020-04-22 14:58:06 -0700354 VkSamplerYcbcrConversion ycbcr_conversion,
355 SAMPLER_YCBCR_CONVERSION_STATE *ycbcr_state){};
locke-lunargd556cc32019-09-17 01:21:23 -0600356
357void ValidationStateTracker::RecordDestroySamplerYcbcrConversionANDROID(VkSamplerYcbcrConversion ycbcr_conversion){};
358
359#endif // VK_USE_PLATFORM_ANDROID_KHR
360
Mark Lobodzinskid3ec86f2020-03-18 11:23:04 -0600361std::shared_ptr<cvdescriptorset::DescriptorSetLayout const> GetDslFromPipelineLayout(PIPELINE_LAYOUT_STATE const *layout_data,
362 uint32_t set) {
363 std::shared_ptr<cvdescriptorset::DescriptorSetLayout const> dsl = nullptr;
364 if (layout_data && (set < layout_data->set_layouts.size())) {
365 dsl = layout_data->set_layouts[set];
366 }
367 return dsl;
368}
369
Petr Kraus44f1c482020-04-25 20:09:25 +0200370void AddImageStateProps(IMAGE_STATE &image_state, const VkDevice device, const VkPhysicalDevice physical_device) {
371 // Add feature support according to Image Format Features (vkspec.html#resources-image-format-features)
372 // if format is AHB external format then the features are already set
373 if (image_state.has_ahb_format == false) {
374 const VkImageTiling image_tiling = image_state.createInfo.tiling;
375 const VkFormat image_format = image_state.createInfo.format;
376 if (image_tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
377 VkImageDrmFormatModifierPropertiesEXT drm_format_properties = {
378 VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT, nullptr};
379 DispatchGetImageDrmFormatModifierPropertiesEXT(device, image_state.image, &drm_format_properties);
380
381 VkFormatProperties2 format_properties_2 = {VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, nullptr};
382 VkDrmFormatModifierPropertiesListEXT drm_properties_list = {VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT,
383 nullptr};
384 format_properties_2.pNext = (void *)&drm_properties_list;
385 DispatchGetPhysicalDeviceFormatProperties2(physical_device, image_format, &format_properties_2);
Lionel Landwerlin09351a72020-06-22 18:15:59 +0300386 std::vector<VkDrmFormatModifierPropertiesEXT> drm_properties;
387 drm_properties.resize(drm_properties_list.drmFormatModifierCount);
388 drm_properties_list.pDrmFormatModifierProperties = &drm_properties[0];
389 DispatchGetPhysicalDeviceFormatProperties2(physical_device, image_format, &format_properties_2);
Petr Kraus44f1c482020-04-25 20:09:25 +0200390
391 for (uint32_t i = 0; i < drm_properties_list.drmFormatModifierCount; i++) {
Lionel Landwerlin94f1ce32020-07-02 17:39:31 +0300392 if (drm_properties_list.pDrmFormatModifierProperties[i].drmFormatModifier ==
393 drm_format_properties.drmFormatModifier) {
394 image_state.format_features =
Petr Kraus44f1c482020-04-25 20:09:25 +0200395 drm_properties_list.pDrmFormatModifierProperties[i].drmFormatModifierTilingFeatures;
Lionel Landwerlin94f1ce32020-07-02 17:39:31 +0300396 break;
Petr Kraus44f1c482020-04-25 20:09:25 +0200397 }
398 }
399 } else {
400 VkFormatProperties format_properties;
401 DispatchGetPhysicalDeviceFormatProperties(physical_device, image_format, &format_properties);
402 image_state.format_features = (image_tiling == VK_IMAGE_TILING_LINEAR) ? format_properties.linearTilingFeatures
403 : format_properties.optimalTilingFeatures;
404 }
405 }
406}
407
locke-lunargd556cc32019-09-17 01:21:23 -0600408void ValidationStateTracker::PostCallRecordCreateImage(VkDevice device, const VkImageCreateInfo *pCreateInfo,
409 const VkAllocationCallbacks *pAllocator, VkImage *pImage, VkResult result) {
410 if (VK_SUCCESS != result) return;
locke-lunarg296a3c92020-03-25 01:04:29 -0600411 auto is_node = std::make_shared<IMAGE_STATE>(device, *pImage, pCreateInfo);
sfricke-samsung71bc6572020-04-29 15:49:43 -0700412 is_node->disjoint = ((pCreateInfo->flags & VK_IMAGE_CREATE_DISJOINT_BIT) != 0);
locke-lunargd556cc32019-09-17 01:21:23 -0600413 if (device_extensions.vk_android_external_memory_android_hardware_buffer) {
414 RecordCreateImageANDROID(pCreateInfo, is_node.get());
415 }
416 const auto swapchain_info = lvl_find_in_chain<VkImageSwapchainCreateInfoKHR>(pCreateInfo->pNext);
417 if (swapchain_info) {
418 is_node->create_from_swapchain = swapchain_info->swapchain;
419 }
420
locke-lunargd556cc32019-09-17 01:21:23 -0600421 // Record the memory requirements in case they won't be queried
sfricke-samsung013f1ef2020-05-14 22:56:20 -0700422 // External AHB memory can't be queried until after memory is bound
Spencer Fricke224c9852020-04-06 07:47:29 -0700423 if (is_node->external_ahb == false) {
sfricke-samsung71bc6572020-04-29 15:49:43 -0700424 if (is_node->disjoint == false) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -0700425 DispatchGetImageMemoryRequirements(device, *pImage, &is_node->requirements);
426 } else {
427 uint32_t plane_count = FormatPlaneCount(pCreateInfo->format);
428 VkImagePlaneMemoryRequirementsInfo image_plane_req = {VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO, nullptr};
429 VkMemoryRequirements2 mem_reqs2 = {VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2, nullptr};
430 VkImageMemoryRequirementsInfo2 mem_req_info2 = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2};
431 mem_req_info2.pNext = &image_plane_req;
432 mem_req_info2.image = *pImage;
433
434 assert(plane_count != 0); // assumes each format has at least first plane
435 image_plane_req.planeAspect = VK_IMAGE_ASPECT_PLANE_0_BIT;
436 DispatchGetImageMemoryRequirements2(device, &mem_req_info2, &mem_reqs2);
437 is_node->plane0_requirements = mem_reqs2.memoryRequirements;
438
439 if (plane_count >= 2) {
440 image_plane_req.planeAspect = VK_IMAGE_ASPECT_PLANE_1_BIT;
441 DispatchGetImageMemoryRequirements2(device, &mem_req_info2, &mem_reqs2);
442 is_node->plane1_requirements = mem_reqs2.memoryRequirements;
443 }
444 if (plane_count >= 3) {
445 image_plane_req.planeAspect = VK_IMAGE_ASPECT_PLANE_2_BIT;
446 DispatchGetImageMemoryRequirements2(device, &mem_req_info2, &mem_reqs2);
447 is_node->plane2_requirements = mem_reqs2.memoryRequirements;
448 }
449 }
locke-lunargd556cc32019-09-17 01:21:23 -0600450 }
Spencer Fricke6bba8c72020-04-06 07:41:21 -0700451
Petr Kraus44f1c482020-04-25 20:09:25 +0200452 AddImageStateProps(*is_node, device, physical_device);
Spencer Fricke6bba8c72020-04-06 07:41:21 -0700453
sfricke-samsungedce77a2020-07-03 22:35:13 -0700454 is_node->unprotected = ((pCreateInfo->flags & VK_IMAGE_CREATE_PROTECTED_BIT) == 0);
455
locke-lunargd556cc32019-09-17 01:21:23 -0600456 imageMap.insert(std::make_pair(*pImage, std::move(is_node)));
457}
458
459void ValidationStateTracker::PreCallRecordDestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator) {
460 if (!image) return;
461 IMAGE_STATE *image_state = GetImageState(image);
462 const VulkanTypedHandle obj_struct(image, kVulkanObjectTypeImage);
463 InvalidateCommandBuffers(image_state->cb_bindings, obj_struct);
464 // Clean up memory mapping, bindings and range references for image
465 for (auto mem_binding : image_state->GetBoundMemory()) {
locke-lunargcf04d582019-11-26 00:31:50 -0700466 RemoveImageMemoryRange(image, mem_binding);
locke-lunargd556cc32019-09-17 01:21:23 -0600467 }
468 if (image_state->bind_swapchain) {
469 auto swapchain = GetSwapchainState(image_state->bind_swapchain);
470 if (swapchain) {
locke-lunargb3584732019-10-28 20:18:36 -0600471 swapchain->images[image_state->bind_swapchain_imageIndex].bound_images.erase(image_state->image);
locke-lunargd556cc32019-09-17 01:21:23 -0600472 }
473 }
474 RemoveAliasingImage(image_state);
475 ClearMemoryObjectBindings(obj_struct);
Jeff Bolze7fc67b2019-10-04 12:29:31 -0500476 image_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -0600477 // Remove image from imageMap
478 imageMap.erase(image);
479}
480
481void ValidationStateTracker::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image,
482 VkImageLayout imageLayout, const VkClearColorValue *pColor,
483 uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
484 auto cb_node = GetCBState(commandBuffer);
485 auto image_state = GetImageState(image);
486 if (cb_node && image_state) {
487 AddCommandBufferBindingImage(cb_node, image_state);
488 }
489}
490
491void ValidationStateTracker::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
492 VkImageLayout imageLayout,
493 const VkClearDepthStencilValue *pDepthStencil,
494 uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
495 auto cb_node = GetCBState(commandBuffer);
496 auto image_state = GetImageState(image);
497 if (cb_node && image_state) {
498 AddCommandBufferBindingImage(cb_node, image_state);
499 }
500}
501
502void ValidationStateTracker::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage,
503 VkImageLayout srcImageLayout, VkImage dstImage, VkImageLayout dstImageLayout,
504 uint32_t regionCount, const VkImageCopy *pRegions) {
505 auto cb_node = GetCBState(commandBuffer);
506 auto src_image_state = GetImageState(srcImage);
507 auto dst_image_state = GetImageState(dstImage);
508
509 // Update bindings between images and cmd buffer
510 AddCommandBufferBindingImage(cb_node, src_image_state);
511 AddCommandBufferBindingImage(cb_node, dst_image_state);
512}
513
Jeff Leger178b1e52020-10-05 12:22:23 -0400514void ValidationStateTracker::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
515 const VkCopyImageInfo2KHR *pCopyImageInfo) {
516 auto cb_node = GetCBState(commandBuffer);
517 auto src_image_state = GetImageState(pCopyImageInfo->srcImage);
518 auto dst_image_state = GetImageState(pCopyImageInfo->dstImage);
519
520 // Update bindings between images and cmd buffer
521 AddCommandBufferBindingImage(cb_node, src_image_state);
522 AddCommandBufferBindingImage(cb_node, dst_image_state);
523}
524
locke-lunargd556cc32019-09-17 01:21:23 -0600525void ValidationStateTracker::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage,
526 VkImageLayout srcImageLayout, VkImage dstImage,
527 VkImageLayout dstImageLayout, uint32_t regionCount,
528 const VkImageResolve *pRegions) {
529 auto cb_node = GetCBState(commandBuffer);
530 auto src_image_state = GetImageState(srcImage);
531 auto dst_image_state = GetImageState(dstImage);
532
533 // Update bindings between images and cmd buffer
534 AddCommandBufferBindingImage(cb_node, src_image_state);
535 AddCommandBufferBindingImage(cb_node, dst_image_state);
536}
537
Jeff Leger178b1e52020-10-05 12:22:23 -0400538void ValidationStateTracker::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
539 const VkResolveImageInfo2KHR *pResolveImageInfo) {
540 auto cb_node = GetCBState(commandBuffer);
541 auto src_image_state = GetImageState(pResolveImageInfo->srcImage);
542 auto dst_image_state = GetImageState(pResolveImageInfo->dstImage);
543
544 // Update bindings between images and cmd buffer
545 AddCommandBufferBindingImage(cb_node, src_image_state);
546 AddCommandBufferBindingImage(cb_node, dst_image_state);
547}
548
locke-lunargd556cc32019-09-17 01:21:23 -0600549void ValidationStateTracker::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage,
550 VkImageLayout srcImageLayout, VkImage dstImage, VkImageLayout dstImageLayout,
551 uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) {
552 auto cb_node = GetCBState(commandBuffer);
553 auto src_image_state = GetImageState(srcImage);
554 auto dst_image_state = GetImageState(dstImage);
555
556 // Update bindings between images and cmd buffer
557 AddCommandBufferBindingImage(cb_node, src_image_state);
558 AddCommandBufferBindingImage(cb_node, dst_image_state);
559}
560
Jeff Leger178b1e52020-10-05 12:22:23 -0400561void ValidationStateTracker::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
562 const VkBlitImageInfo2KHR *pBlitImageInfo) {
563 auto cb_node = GetCBState(commandBuffer);
564 auto src_image_state = GetImageState(pBlitImageInfo->srcImage);
565 auto dst_image_state = GetImageState(pBlitImageInfo->dstImage);
566
567 // Update bindings between images and cmd buffer
568 AddCommandBufferBindingImage(cb_node, src_image_state);
569 AddCommandBufferBindingImage(cb_node, dst_image_state);
570}
571
locke-lunargd556cc32019-09-17 01:21:23 -0600572void ValidationStateTracker::PostCallRecordCreateBuffer(VkDevice device, const VkBufferCreateInfo *pCreateInfo,
573 const VkAllocationCallbacks *pAllocator, VkBuffer *pBuffer,
574 VkResult result) {
575 if (result != VK_SUCCESS) return;
576 // TODO : This doesn't create deep copy of pQueueFamilyIndices so need to fix that if/when we want that data to be valid
Jeff Bolze7fc67b2019-10-04 12:29:31 -0500577 auto buffer_state = std::make_shared<BUFFER_STATE>(*pBuffer, pCreateInfo);
locke-lunargd556cc32019-09-17 01:21:23 -0600578
sfricke-samsung013f1ef2020-05-14 22:56:20 -0700579 if (device_extensions.vk_android_external_memory_android_hardware_buffer) {
580 RecordCreateBufferANDROID(pCreateInfo, buffer_state.get());
581 }
locke-lunargd556cc32019-09-17 01:21:23 -0600582 // Get a set of requirements in the case the app does not
sfricke-samsungad90e722020-07-08 20:54:24 -0700583 DispatchGetBufferMemoryRequirements(device, *pBuffer, &buffer_state->requirements);
locke-lunargd556cc32019-09-17 01:21:23 -0600584
sfricke-samsungedce77a2020-07-03 22:35:13 -0700585 buffer_state->unprotected = ((pCreateInfo->flags & VK_BUFFER_CREATE_PROTECTED_BIT) == 0);
586
locke-lunargd556cc32019-09-17 01:21:23 -0600587 bufferMap.insert(std::make_pair(*pBuffer, std::move(buffer_state)));
588}
589
590void ValidationStateTracker::PostCallRecordCreateBufferView(VkDevice device, const VkBufferViewCreateInfo *pCreateInfo,
591 const VkAllocationCallbacks *pAllocator, VkBufferView *pView,
592 VkResult result) {
593 if (result != VK_SUCCESS) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -0500594 auto buffer_state = GetBufferShared(pCreateInfo->buffer);
locke-lunarg25b6c352020-08-06 17:44:18 -0600595 auto buffer_view_state = std::make_shared<BUFFER_VIEW_STATE>(buffer_state, *pView, pCreateInfo);
596
597 VkFormatProperties format_properties;
598 DispatchGetPhysicalDeviceFormatProperties(physical_device, pCreateInfo->format, &format_properties);
599 buffer_view_state->format_features = format_properties.bufferFeatures;
600
601 bufferViewMap.insert(std::make_pair(*pView, std::move(buffer_view_state)));
locke-lunargd556cc32019-09-17 01:21:23 -0600602}
603
604void ValidationStateTracker::PostCallRecordCreateImageView(VkDevice device, const VkImageViewCreateInfo *pCreateInfo,
605 const VkAllocationCallbacks *pAllocator, VkImageView *pView,
606 VkResult result) {
607 if (result != VK_SUCCESS) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -0500608 auto image_state = GetImageShared(pCreateInfo->image);
Spencer Fricke6bba8c72020-04-06 07:41:21 -0700609 auto image_view_state = std::make_shared<IMAGE_VIEW_STATE>(image_state, *pView, pCreateInfo);
610
611 // Add feature support according to Image View Format Features (vkspec.html#resources-image-view-format-features)
612 const VkImageTiling image_tiling = image_state->createInfo.tiling;
613 const VkFormat image_view_format = pCreateInfo->format;
614 if (image_state->has_ahb_format == true) {
615 // The ImageView uses same Image's format feature since they share same AHB
616 image_view_state->format_features = image_state->format_features;
617 } else if (image_tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
618 // Parameter validation should catch if this is used without VK_EXT_image_drm_format_modifier
619 assert(device_extensions.vk_ext_image_drm_format_modifier);
620 VkImageDrmFormatModifierPropertiesEXT drm_format_properties = {VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT,
621 nullptr};
622 DispatchGetImageDrmFormatModifierPropertiesEXT(device, image_state->image, &drm_format_properties);
623
624 VkFormatProperties2 format_properties_2 = {VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, nullptr};
625 VkDrmFormatModifierPropertiesListEXT drm_properties_list = {VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT,
626 nullptr};
627 format_properties_2.pNext = (void *)&drm_properties_list;
628 DispatchGetPhysicalDeviceFormatProperties2(physical_device, image_view_format, &format_properties_2);
629
630 for (uint32_t i = 0; i < drm_properties_list.drmFormatModifierCount; i++) {
Lionel Landwerlin94f1ce32020-07-02 17:39:31 +0300631 if (drm_properties_list.pDrmFormatModifierProperties[i].drmFormatModifier == drm_format_properties.drmFormatModifier) {
Spencer Fricke6bba8c72020-04-06 07:41:21 -0700632 image_view_state->format_features |=
633 drm_properties_list.pDrmFormatModifierProperties[i].drmFormatModifierTilingFeatures;
Lionel Landwerlin94f1ce32020-07-02 17:39:31 +0300634 break;
Spencer Fricke6bba8c72020-04-06 07:41:21 -0700635 }
636 }
637 } else {
638 VkFormatProperties format_properties;
639 DispatchGetPhysicalDeviceFormatProperties(physical_device, image_view_format, &format_properties);
640 image_view_state->format_features = (image_tiling == VK_IMAGE_TILING_LINEAR) ? format_properties.linearTilingFeatures
641 : format_properties.optimalTilingFeatures;
642 }
643
locke-lunarg9939d4b2020-10-26 20:11:08 -0600644 // filter_cubic_props is used in CmdDraw validation. But it takes a lot of performance if it does in CmdDraw.
645 image_view_state->filter_cubic_props = lvl_init_struct<VkFilterCubicImageViewImageFormatPropertiesEXT>();
646 if (IsExtEnabled(device_extensions.vk_ext_filter_cubic)) {
647 auto imageview_format_info = lvl_init_struct<VkPhysicalDeviceImageViewImageFormatInfoEXT>();
648 imageview_format_info.imageViewType = pCreateInfo->viewType;
649 auto image_format_info = lvl_init_struct<VkPhysicalDeviceImageFormatInfo2>(&imageview_format_info);
650 image_format_info.type = image_state->createInfo.imageType;
651 image_format_info.format = image_state->createInfo.format;
652 image_format_info.tiling = image_state->createInfo.tiling;
653 image_format_info.usage = image_state->createInfo.usage;
654 image_format_info.flags = image_state->createInfo.flags;
655
656 auto image_format_properties = lvl_init_struct<VkImageFormatProperties2>(&image_view_state->filter_cubic_props);
657
658 DispatchGetPhysicalDeviceImageFormatProperties2(physical_device, &image_format_info, &image_format_properties);
659 }
Spencer Fricke6bba8c72020-04-06 07:41:21 -0700660 imageViewMap.insert(std::make_pair(*pView, std::move(image_view_state)));
locke-lunargd556cc32019-09-17 01:21:23 -0600661}
662
663void ValidationStateTracker::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
664 uint32_t regionCount, const VkBufferCopy *pRegions) {
665 auto cb_node = GetCBState(commandBuffer);
666 auto src_buffer_state = GetBufferState(srcBuffer);
667 auto dst_buffer_state = GetBufferState(dstBuffer);
668
669 // Update bindings between buffers and cmd buffer
670 AddCommandBufferBindingBuffer(cb_node, src_buffer_state);
671 AddCommandBufferBindingBuffer(cb_node, dst_buffer_state);
672}
673
Jeff Leger178b1e52020-10-05 12:22:23 -0400674void ValidationStateTracker::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
675 const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
676 auto cb_node = GetCBState(commandBuffer);
677 auto src_buffer_state = GetBufferState(pCopyBufferInfos->srcBuffer);
678 auto dst_buffer_state = GetBufferState(pCopyBufferInfos->dstBuffer);
679
680 // Update bindings between buffers and cmd buffer
681 AddCommandBufferBindingBuffer(cb_node, src_buffer_state);
682 AddCommandBufferBindingBuffer(cb_node, dst_buffer_state);
683}
684
locke-lunargd556cc32019-09-17 01:21:23 -0600685void ValidationStateTracker::PreCallRecordDestroyImageView(VkDevice device, VkImageView imageView,
686 const VkAllocationCallbacks *pAllocator) {
687 IMAGE_VIEW_STATE *image_view_state = GetImageViewState(imageView);
688 if (!image_view_state) return;
689 const VulkanTypedHandle obj_struct(imageView, kVulkanObjectTypeImageView);
690
691 // Any bound cmd buffers are now invalid
692 InvalidateCommandBuffers(image_view_state->cb_bindings, obj_struct);
Jeff Bolze7fc67b2019-10-04 12:29:31 -0500693 image_view_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -0600694 imageViewMap.erase(imageView);
695}
696
697void ValidationStateTracker::PreCallRecordDestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *pAllocator) {
698 if (!buffer) return;
699 auto buffer_state = GetBufferState(buffer);
700 const VulkanTypedHandle obj_struct(buffer, kVulkanObjectTypeBuffer);
701
702 InvalidateCommandBuffers(buffer_state->cb_bindings, obj_struct);
703 for (auto mem_binding : buffer_state->GetBoundMemory()) {
locke-lunargcf04d582019-11-26 00:31:50 -0700704 RemoveBufferMemoryRange(buffer, mem_binding);
locke-lunargd556cc32019-09-17 01:21:23 -0600705 }
706 ClearMemoryObjectBindings(obj_struct);
Jeff Bolze7fc67b2019-10-04 12:29:31 -0500707 buffer_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -0600708 bufferMap.erase(buffer_state->buffer);
709}
710
711void ValidationStateTracker::PreCallRecordDestroyBufferView(VkDevice device, VkBufferView bufferView,
712 const VkAllocationCallbacks *pAllocator) {
713 if (!bufferView) return;
714 auto buffer_view_state = GetBufferViewState(bufferView);
715 const VulkanTypedHandle obj_struct(bufferView, kVulkanObjectTypeBufferView);
716
717 // Any bound cmd buffers are now invalid
718 InvalidateCommandBuffers(buffer_view_state->cb_bindings, obj_struct);
Jeff Bolze7fc67b2019-10-04 12:29:31 -0500719 buffer_view_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -0600720 bufferViewMap.erase(bufferView);
721}
722
723void ValidationStateTracker::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
724 VkDeviceSize size, uint32_t data) {
725 auto cb_node = GetCBState(commandBuffer);
726 auto buffer_state = GetBufferState(dstBuffer);
727 // Update bindings between buffer and cmd buffer
728 AddCommandBufferBindingBuffer(cb_node, buffer_state);
729}
730
731void ValidationStateTracker::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
732 VkImageLayout srcImageLayout, VkBuffer dstBuffer,
733 uint32_t regionCount, const VkBufferImageCopy *pRegions) {
734 auto cb_node = GetCBState(commandBuffer);
735 auto src_image_state = GetImageState(srcImage);
736 auto dst_buffer_state = GetBufferState(dstBuffer);
737
738 // Update bindings between buffer/image and cmd buffer
739 AddCommandBufferBindingImage(cb_node, src_image_state);
740 AddCommandBufferBindingBuffer(cb_node, dst_buffer_state);
741}
742
Jeff Leger178b1e52020-10-05 12:22:23 -0400743void ValidationStateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
744 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
745 auto cb_node = GetCBState(commandBuffer);
746 auto src_image_state = GetImageState(pCopyImageToBufferInfo->srcImage);
747 auto dst_buffer_state = GetBufferState(pCopyImageToBufferInfo->dstBuffer);
748
749 // Update bindings between buffer/image and cmd buffer
750 AddCommandBufferBindingImage(cb_node, src_image_state);
751 AddCommandBufferBindingBuffer(cb_node, dst_buffer_state);
752}
753
locke-lunargd556cc32019-09-17 01:21:23 -0600754void ValidationStateTracker::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
755 VkImageLayout dstImageLayout, uint32_t regionCount,
756 const VkBufferImageCopy *pRegions) {
757 auto cb_node = GetCBState(commandBuffer);
758 auto src_buffer_state = GetBufferState(srcBuffer);
759 auto dst_image_state = GetImageState(dstImage);
760
761 AddCommandBufferBindingBuffer(cb_node, src_buffer_state);
762 AddCommandBufferBindingImage(cb_node, dst_image_state);
763}
764
Jeff Leger178b1e52020-10-05 12:22:23 -0400765void ValidationStateTracker::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
766 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
767 auto cb_node = GetCBState(commandBuffer);
768 auto src_buffer_state = GetBufferState(pCopyBufferToImageInfo->srcBuffer);
769 auto dst_image_state = GetImageState(pCopyBufferToImageInfo->dstImage);
770
771 AddCommandBufferBindingBuffer(cb_node, src_buffer_state);
772 AddCommandBufferBindingImage(cb_node, dst_image_state);
773}
774
locke-lunargd556cc32019-09-17 01:21:23 -0600775// Get the image viewstate for a given framebuffer attachment
locke-lunargfc78e932020-11-19 17:06:24 -0700776IMAGE_VIEW_STATE *ValidationStateTracker::GetActiveAttachmentImageViewState(const CMD_BUFFER_STATE *cb, uint32_t index,
777 const CMD_BUFFER_STATE *primary_cb) {
778 if (primary_cb) {
779 assert(primary_cb->active_attachments && index != VK_ATTACHMENT_UNUSED && (index < primary_cb->active_attachments->size()));
780 return primary_cb->active_attachments->at(index);
Lionel Landwerlin484d10f2020-04-24 01:34:47 +0300781 }
locke-lunargfc78e932020-11-19 17:06:24 -0700782 assert(cb->active_attachments && index != VK_ATTACHMENT_UNUSED && (index < cb->active_attachments->size()));
783 return cb->active_attachments->at(index);
locke-lunargd556cc32019-09-17 01:21:23 -0600784}
785
786// Get the image viewstate for a given framebuffer attachment
locke-lunargfc78e932020-11-19 17:06:24 -0700787const IMAGE_VIEW_STATE *ValidationStateTracker::GetActiveAttachmentImageViewState(const CMD_BUFFER_STATE *cb, uint32_t index,
788 const CMD_BUFFER_STATE *primary_cb) const {
789 if (primary_cb) {
790 assert(primary_cb->active_attachments && index != VK_ATTACHMENT_UNUSED && (index < primary_cb->active_attachments->size()));
791 return primary_cb->active_attachments->at(index);
Lionel Landwerlin484d10f2020-04-24 01:34:47 +0300792 }
locke-lunargfc78e932020-11-19 17:06:24 -0700793 assert(cb->active_attachments && index != VK_ATTACHMENT_UNUSED && (index < cb->active_attachments->size()));
794 return cb->active_attachments->at(index);
locke-lunargd556cc32019-09-17 01:21:23 -0600795}
796
797void ValidationStateTracker::AddAliasingImage(IMAGE_STATE *image_state) {
locke-lunargd556cc32019-09-17 01:21:23 -0600798 std::unordered_set<VkImage> *bound_images = nullptr;
799
locke-lunargb3584732019-10-28 20:18:36 -0600800 if (image_state->bind_swapchain) {
801 auto swapchain_state = GetSwapchainState(image_state->bind_swapchain);
locke-lunargd556cc32019-09-17 01:21:23 -0600802 if (swapchain_state) {
locke-lunargb3584732019-10-28 20:18:36 -0600803 bound_images = &swapchain_state->images[image_state->bind_swapchain_imageIndex].bound_images;
locke-lunargd556cc32019-09-17 01:21:23 -0600804 }
805 } else {
locke-lunargcf04d582019-11-26 00:31:50 -0700806 if (image_state->binding.mem_state) {
807 bound_images = &image_state->binding.mem_state->bound_images;
locke-lunargd556cc32019-09-17 01:21:23 -0600808 }
809 }
810
811 if (bound_images) {
812 for (const auto &handle : *bound_images) {
813 if (handle != image_state->image) {
814 auto is = GetImageState(handle);
815 if (is && is->IsCompatibleAliasing(image_state)) {
816 auto inserted = is->aliasing_images.emplace(image_state->image);
817 if (inserted.second) {
818 image_state->aliasing_images.emplace(handle);
819 }
820 }
821 }
822 }
823 }
824}
825
826void ValidationStateTracker::RemoveAliasingImage(IMAGE_STATE *image_state) {
827 for (const auto &image : image_state->aliasing_images) {
828 auto is = GetImageState(image);
829 if (is) {
830 is->aliasing_images.erase(image_state->image);
831 }
832 }
833 image_state->aliasing_images.clear();
834}
835
836void ValidationStateTracker::RemoveAliasingImages(const std::unordered_set<VkImage> &bound_images) {
837 // This is one way clear. Because the bound_images include cross references, the one way clear loop could clear the whole
838 // reference. It doesn't need two ways clear.
839 for (const auto &handle : bound_images) {
840 auto is = GetImageState(handle);
841 if (is) {
842 is->aliasing_images.clear();
843 }
844 }
845}
846
Jeff Bolz310775c2019-10-09 00:46:33 -0500847const EVENT_STATE *ValidationStateTracker::GetEventState(VkEvent event) const {
848 auto it = eventMap.find(event);
849 if (it == eventMap.end()) {
850 return nullptr;
851 }
852 return &it->second;
853}
854
locke-lunargd556cc32019-09-17 01:21:23 -0600855EVENT_STATE *ValidationStateTracker::GetEventState(VkEvent event) {
856 auto it = eventMap.find(event);
857 if (it == eventMap.end()) {
858 return nullptr;
859 }
860 return &it->second;
861}
862
863const QUEUE_STATE *ValidationStateTracker::GetQueueState(VkQueue queue) const {
864 auto it = queueMap.find(queue);
865 if (it == queueMap.cend()) {
866 return nullptr;
867 }
868 return &it->second;
869}
870
871QUEUE_STATE *ValidationStateTracker::GetQueueState(VkQueue queue) {
872 auto it = queueMap.find(queue);
873 if (it == queueMap.end()) {
874 return nullptr;
875 }
876 return &it->second;
877}
878
879const PHYSICAL_DEVICE_STATE *ValidationStateTracker::GetPhysicalDeviceState(VkPhysicalDevice phys) const {
880 auto *phys_dev_map = ((physical_device_map.size() > 0) ? &physical_device_map : &instance_state->physical_device_map);
881 auto it = phys_dev_map->find(phys);
882 if (it == phys_dev_map->end()) {
883 return nullptr;
884 }
885 return &it->second;
886}
887
888PHYSICAL_DEVICE_STATE *ValidationStateTracker::GetPhysicalDeviceState(VkPhysicalDevice phys) {
889 auto *phys_dev_map = ((physical_device_map.size() > 0) ? &physical_device_map : &instance_state->physical_device_map);
890 auto it = phys_dev_map->find(phys);
891 if (it == phys_dev_map->end()) {
892 return nullptr;
893 }
894 return &it->second;
895}
896
897PHYSICAL_DEVICE_STATE *ValidationStateTracker::GetPhysicalDeviceState() { return physical_device_state; }
898const PHYSICAL_DEVICE_STATE *ValidationStateTracker::GetPhysicalDeviceState() const { return physical_device_state; }
899
900// Return ptr to memory binding for given handle of specified type
901template <typename State, typename Result>
902static Result GetObjectMemBindingImpl(State state, const VulkanTypedHandle &typed_handle) {
903 switch (typed_handle.type) {
904 case kVulkanObjectTypeImage:
905 return state->GetImageState(typed_handle.Cast<VkImage>());
906 case kVulkanObjectTypeBuffer:
907 return state->GetBufferState(typed_handle.Cast<VkBuffer>());
908 case kVulkanObjectTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700909 return state->GetAccelerationStructureStateNV(typed_handle.Cast<VkAccelerationStructureNV>());
locke-lunargd556cc32019-09-17 01:21:23 -0600910 default:
911 break;
912 }
913 return nullptr;
914}
915
916const BINDABLE *ValidationStateTracker::GetObjectMemBinding(const VulkanTypedHandle &typed_handle) const {
917 return GetObjectMemBindingImpl<const ValidationStateTracker *, const BINDABLE *>(this, typed_handle);
918}
919
920BINDABLE *ValidationStateTracker::GetObjectMemBinding(const VulkanTypedHandle &typed_handle) {
921 return GetObjectMemBindingImpl<ValidationStateTracker *, BINDABLE *>(this, typed_handle);
922}
923
924void ValidationStateTracker::AddMemObjInfo(void *object, const VkDeviceMemory mem, const VkMemoryAllocateInfo *pAllocateInfo) {
925 assert(object != NULL);
926
John Zulauf79952712020-04-07 11:25:54 -0600927 auto fake_address = fake_memory.Alloc(pAllocateInfo->allocationSize);
928 memObjMap[mem] = std::make_shared<DEVICE_MEMORY_STATE>(object, mem, pAllocateInfo, fake_address);
Jeff Bolze7fc67b2019-10-04 12:29:31 -0500929 auto mem_info = memObjMap[mem].get();
locke-lunargd556cc32019-09-17 01:21:23 -0600930
931 auto dedicated = lvl_find_in_chain<VkMemoryDedicatedAllocateInfoKHR>(pAllocateInfo->pNext);
932 if (dedicated) {
933 mem_info->is_dedicated = true;
934 mem_info->dedicated_buffer = dedicated->buffer;
935 mem_info->dedicated_image = dedicated->image;
936 }
937 auto export_info = lvl_find_in_chain<VkExportMemoryAllocateInfo>(pAllocateInfo->pNext);
938 if (export_info) {
939 mem_info->is_export = true;
940 mem_info->export_handle_type_flags = export_info->handleTypes;
941 }
sfricke-samsung23068272020-06-21 14:49:51 -0700942
Mark Lobodzinski7bda6ed2020-07-24 09:57:38 -0600943 auto alloc_flags = lvl_find_in_chain<VkMemoryAllocateFlagsInfo>(pAllocateInfo->pNext);
944 if (alloc_flags) {
945 auto dev_mask = alloc_flags->deviceMask;
946 if ((dev_mask != 0) && (dev_mask & (dev_mask - 1))) {
947 mem_info->multi_instance = true;
948 }
949 }
950 auto heap_index = phys_dev_mem_props.memoryTypes[mem_info->alloc_info.memoryTypeIndex].heapIndex;
Tony-LunarGb399fb02020-08-06 14:20:59 -0600951 mem_info->multi_instance |= (((phys_dev_mem_props.memoryHeaps[heap_index].flags & VK_MEMORY_HEAP_MULTI_INSTANCE_BIT) != 0) &&
952 physical_device_count > 1);
Mark Lobodzinski7bda6ed2020-07-24 09:57:38 -0600953
sfricke-samsung23068272020-06-21 14:49:51 -0700954 // Assumes validation already for only a single import operation in the pNext
955#ifdef VK_USE_PLATFORM_WIN32_KHR
956 auto win32_import = lvl_find_in_chain<VkImportMemoryWin32HandleInfoKHR>(pAllocateInfo->pNext);
957 if (win32_import) {
958 mem_info->is_import = true;
959 mem_info->import_handle_type_flags = win32_import->handleType;
960 }
961#endif
962 auto fd_import = lvl_find_in_chain<VkImportMemoryFdInfoKHR>(pAllocateInfo->pNext);
963 if (fd_import) {
964 mem_info->is_import = true;
965 mem_info->import_handle_type_flags = fd_import->handleType;
966 }
967 auto host_pointer_import = lvl_find_in_chain<VkImportMemoryHostPointerInfoEXT>(pAllocateInfo->pNext);
968 if (host_pointer_import) {
969 mem_info->is_import = true;
970 mem_info->import_handle_type_flags = host_pointer_import->handleType;
971 }
972#ifdef VK_USE_PLATFORM_ANDROID_KHR
973 // AHB Import doesn't have handle in the pNext struct
974 // It should be assumed that all imported AHB can only have the same, single handleType
975 auto ahb_import = lvl_find_in_chain<VkImportAndroidHardwareBufferInfoANDROID>(pAllocateInfo->pNext);
976 if ((ahb_import) && (ahb_import->buffer != nullptr)) {
977 mem_info->is_import_ahb = true;
978 mem_info->is_import = true;
979 mem_info->import_handle_type_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
980 }
sfricke-samsung98acb882020-11-03 19:16:10 -0800981#endif // VK_USE_PLATFORM_ANDROID_KHR
sfricke-samsungedce77a2020-07-03 22:35:13 -0700982
983 const VkMemoryType memory_type = phys_dev_mem_props.memoryTypes[pAllocateInfo->memoryTypeIndex];
984 mem_info->unprotected = ((memory_type.propertyFlags & VK_MEMORY_PROPERTY_PROTECTED_BIT) == 0);
locke-lunargd556cc32019-09-17 01:21:23 -0600985}
986
987// Create binding link between given sampler and command buffer node
988void ValidationStateTracker::AddCommandBufferBindingSampler(CMD_BUFFER_STATE *cb_node, SAMPLER_STATE *sampler_state) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -0600989 if (disabled[command_buffer_state]) {
locke-lunargd556cc32019-09-17 01:21:23 -0600990 return;
991 }
Jeff Bolzadbfa852019-10-04 13:53:30 -0500992 AddCommandBufferBinding(sampler_state->cb_bindings,
993 VulkanTypedHandle(sampler_state->sampler, kVulkanObjectTypeSampler, sampler_state), cb_node);
locke-lunargd556cc32019-09-17 01:21:23 -0600994}
995
996// Create binding link between given image node and command buffer node
997void ValidationStateTracker::AddCommandBufferBindingImage(CMD_BUFFER_STATE *cb_node, IMAGE_STATE *image_state) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -0600998 if (disabled[command_buffer_state]) {
locke-lunargd556cc32019-09-17 01:21:23 -0600999 return;
1000 }
1001 // Skip validation if this image was created through WSI
1002 if (image_state->create_from_swapchain == VK_NULL_HANDLE) {
1003 // First update cb binding for image
Jeff Bolzadbfa852019-10-04 13:53:30 -05001004 if (AddCommandBufferBinding(image_state->cb_bindings,
1005 VulkanTypedHandle(image_state->image, kVulkanObjectTypeImage, image_state), cb_node)) {
locke-lunargd556cc32019-09-17 01:21:23 -06001006 // Now update CB binding in MemObj mini CB list
1007 for (auto mem_binding : image_state->GetBoundMemory()) {
locke-lunargcf04d582019-11-26 00:31:50 -07001008 // Now update CBInfo's Mem reference list
1009 AddCommandBufferBinding(mem_binding->cb_bindings,
1010 VulkanTypedHandle(mem_binding->mem, kVulkanObjectTypeDeviceMemory, mem_binding), cb_node);
locke-lunargd556cc32019-09-17 01:21:23 -06001011 }
1012 }
1013 }
1014}
1015
1016// Create binding link between given image view node and its image with command buffer node
1017void ValidationStateTracker::AddCommandBufferBindingImageView(CMD_BUFFER_STATE *cb_node, IMAGE_VIEW_STATE *view_state) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06001018 if (disabled[command_buffer_state]) {
locke-lunargd556cc32019-09-17 01:21:23 -06001019 return;
1020 }
1021 // First add bindings for imageView
Jeff Bolzadbfa852019-10-04 13:53:30 -05001022 if (AddCommandBufferBinding(view_state->cb_bindings,
1023 VulkanTypedHandle(view_state->image_view, kVulkanObjectTypeImageView, view_state), cb_node)) {
locke-lunargd556cc32019-09-17 01:21:23 -06001024 // Only need to continue if this is a new item
Jeff Bolzadbfa852019-10-04 13:53:30 -05001025 auto image_state = view_state->image_state.get();
locke-lunargd556cc32019-09-17 01:21:23 -06001026 // Add bindings for image within imageView
1027 if (image_state) {
1028 AddCommandBufferBindingImage(cb_node, image_state);
1029 }
1030 }
1031}
1032
1033// Create binding link between given buffer node and command buffer node
1034void ValidationStateTracker::AddCommandBufferBindingBuffer(CMD_BUFFER_STATE *cb_node, BUFFER_STATE *buffer_state) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06001035 if (disabled[command_buffer_state]) {
locke-lunargd556cc32019-09-17 01:21:23 -06001036 return;
1037 }
1038 // First update cb binding for buffer
Jeff Bolzadbfa852019-10-04 13:53:30 -05001039 if (AddCommandBufferBinding(buffer_state->cb_bindings,
1040 VulkanTypedHandle(buffer_state->buffer, kVulkanObjectTypeBuffer, buffer_state), cb_node)) {
locke-lunargd556cc32019-09-17 01:21:23 -06001041 // Now update CB binding in MemObj mini CB list
1042 for (auto mem_binding : buffer_state->GetBoundMemory()) {
locke-lunargcf04d582019-11-26 00:31:50 -07001043 // Now update CBInfo's Mem reference list
1044 AddCommandBufferBinding(mem_binding->cb_bindings,
1045 VulkanTypedHandle(mem_binding->mem, kVulkanObjectTypeDeviceMemory, mem_binding), cb_node);
locke-lunargd556cc32019-09-17 01:21:23 -06001046 }
1047 }
1048}
1049
1050// Create binding link between given buffer view node and its buffer with command buffer node
1051void ValidationStateTracker::AddCommandBufferBindingBufferView(CMD_BUFFER_STATE *cb_node, BUFFER_VIEW_STATE *view_state) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06001052 if (disabled[command_buffer_state]) {
locke-lunargd556cc32019-09-17 01:21:23 -06001053 return;
1054 }
1055 // First add bindings for bufferView
Jeff Bolzadbfa852019-10-04 13:53:30 -05001056 if (AddCommandBufferBinding(view_state->cb_bindings,
1057 VulkanTypedHandle(view_state->buffer_view, kVulkanObjectTypeBufferView, view_state), cb_node)) {
1058 auto buffer_state = view_state->buffer_state.get();
locke-lunargd556cc32019-09-17 01:21:23 -06001059 // Add bindings for buffer within bufferView
1060 if (buffer_state) {
1061 AddCommandBufferBindingBuffer(cb_node, buffer_state);
1062 }
1063 }
1064}
1065
1066// Create binding link between given acceleration structure and command buffer node
1067void ValidationStateTracker::AddCommandBufferBindingAccelerationStructure(CMD_BUFFER_STATE *cb_node,
1068 ACCELERATION_STRUCTURE_STATE *as_state) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06001069 if (disabled[command_buffer_state]) {
locke-lunargd556cc32019-09-17 01:21:23 -06001070 return;
1071 }
Jeff Bolzadbfa852019-10-04 13:53:30 -05001072 if (AddCommandBufferBinding(
1073 as_state->cb_bindings,
1074 VulkanTypedHandle(as_state->acceleration_structure, kVulkanObjectTypeAccelerationStructureNV, as_state), cb_node)) {
locke-lunargd556cc32019-09-17 01:21:23 -06001075 // Now update CB binding in MemObj mini CB list
1076 for (auto mem_binding : as_state->GetBoundMemory()) {
locke-lunargcf04d582019-11-26 00:31:50 -07001077 // Now update CBInfo's Mem reference list
1078 AddCommandBufferBinding(mem_binding->cb_bindings,
1079 VulkanTypedHandle(mem_binding->mem, kVulkanObjectTypeDeviceMemory, mem_binding), cb_node);
locke-lunargd556cc32019-09-17 01:21:23 -06001080 }
1081 }
1082}
1083
sourav parmarcd5fb182020-07-17 12:58:44 -07001084// Create binding link between given acceleration structure and command buffer node
1085void ValidationStateTracker::AddCommandBufferBindingAccelerationStructure(CMD_BUFFER_STATE *cb_node,
1086 ACCELERATION_STRUCTURE_STATE_KHR *as_state) {
1087 if (disabled[command_buffer_state]) {
1088 return;
1089 }
1090 if (AddCommandBufferBinding(
1091 as_state->cb_bindings,
1092 VulkanTypedHandle(as_state->acceleration_structure, kVulkanObjectTypeAccelerationStructureKHR, as_state), cb_node)) {
1093 // Now update CB binding in MemObj mini CB list
1094 for (auto mem_binding : as_state->GetBoundMemory()) {
1095 // Now update CBInfo's Mem reference list
1096 AddCommandBufferBinding(mem_binding->cb_bindings,
1097 VulkanTypedHandle(mem_binding->mem, kVulkanObjectTypeDeviceMemory, mem_binding), cb_node);
1098 }
1099 }
1100}
1101
locke-lunargd556cc32019-09-17 01:21:23 -06001102// Clear a single object binding from given memory object
locke-lunarg5f59e782019-12-19 10:32:23 -07001103void ValidationStateTracker::ClearMemoryObjectBinding(const VulkanTypedHandle &typed_handle, DEVICE_MEMORY_STATE *mem_info) {
locke-lunargd556cc32019-09-17 01:21:23 -06001104 // This obj is bound to a memory object. Remove the reference to this object in that memory object's list
1105 if (mem_info) {
1106 mem_info->obj_bindings.erase(typed_handle);
1107 }
1108}
1109
1110// ClearMemoryObjectBindings clears the binding of objects to memory
1111// For the given object it pulls the memory bindings and makes sure that the bindings
1112// no longer refer to the object being cleared. This occurs when objects are destroyed.
1113void ValidationStateTracker::ClearMemoryObjectBindings(const VulkanTypedHandle &typed_handle) {
1114 BINDABLE *mem_binding = GetObjectMemBinding(typed_handle);
1115 if (mem_binding) {
1116 if (!mem_binding->sparse) {
locke-lunarg5f59e782019-12-19 10:32:23 -07001117 ClearMemoryObjectBinding(typed_handle, mem_binding->binding.mem_state.get());
locke-lunargd556cc32019-09-17 01:21:23 -06001118 } else { // Sparse, clear all bindings
1119 for (auto &sparse_mem_binding : mem_binding->sparse_bindings) {
locke-lunarg5f59e782019-12-19 10:32:23 -07001120 ClearMemoryObjectBinding(typed_handle, sparse_mem_binding.mem_state.get());
locke-lunargd556cc32019-09-17 01:21:23 -06001121 }
1122 }
1123 }
1124}
1125
1126// SetMemBinding is used to establish immutable, non-sparse binding between a single image/buffer object and memory object.
1127// Corresponding valid usage checks are in ValidateSetMemBinding().
1128void ValidationStateTracker::SetMemBinding(VkDeviceMemory mem, BINDABLE *mem_binding, VkDeviceSize memory_offset,
1129 const VulkanTypedHandle &typed_handle) {
1130 assert(mem_binding);
locke-lunargd556cc32019-09-17 01:21:23 -06001131
1132 if (mem != VK_NULL_HANDLE) {
locke-lunargcf04d582019-11-26 00:31:50 -07001133 mem_binding->binding.mem_state = GetShared<DEVICE_MEMORY_STATE>(mem);
1134 if (mem_binding->binding.mem_state) {
locke-lunarg5f59e782019-12-19 10:32:23 -07001135 mem_binding->binding.offset = memory_offset;
1136 mem_binding->binding.size = mem_binding->requirements.size;
locke-lunargcf04d582019-11-26 00:31:50 -07001137 mem_binding->binding.mem_state->obj_bindings.insert(typed_handle);
locke-lunargd556cc32019-09-17 01:21:23 -06001138 // For image objects, make sure default memory state is correctly set
1139 // TODO : What's the best/correct way to handle this?
1140 if (kVulkanObjectTypeImage == typed_handle.type) {
1141 auto const image_state = reinterpret_cast<const IMAGE_STATE *>(mem_binding);
1142 if (image_state) {
1143 VkImageCreateInfo ici = image_state->createInfo;
1144 if (ici.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
1145 // TODO:: More memory state transition stuff.
1146 }
1147 }
1148 }
locke-lunargcf04d582019-11-26 00:31:50 -07001149 mem_binding->UpdateBoundMemorySet(); // force recreation of cached set
locke-lunargd556cc32019-09-17 01:21:23 -06001150 }
1151 }
1152}
1153
1154// For NULL mem case, clear any previous binding Else...
1155// Make sure given object is in its object map
1156// IF a previous binding existed, update binding
1157// Add reference from objectInfo to memoryInfo
1158// Add reference off of object's binding info
1159// Return VK_TRUE if addition is successful, VK_FALSE otherwise
locke-lunargcf04d582019-11-26 00:31:50 -07001160bool ValidationStateTracker::SetSparseMemBinding(const VkDeviceMemory mem, const VkDeviceSize mem_offset,
1161 const VkDeviceSize mem_size, const VulkanTypedHandle &typed_handle) {
locke-lunargd556cc32019-09-17 01:21:23 -06001162 bool skip = VK_FALSE;
1163 // Handle NULL case separately, just clear previous binding & decrement reference
locke-lunargcf04d582019-11-26 00:31:50 -07001164 if (mem == VK_NULL_HANDLE) {
locke-lunargd556cc32019-09-17 01:21:23 -06001165 // TODO : This should cause the range of the resource to be unbound according to spec
1166 } else {
1167 BINDABLE *mem_binding = GetObjectMemBinding(typed_handle);
1168 assert(mem_binding);
1169 if (mem_binding) { // Invalid handles are reported by object tracker, but Get returns NULL for them, so avoid SEGV here
1170 assert(mem_binding->sparse);
locke-lunargcf04d582019-11-26 00:31:50 -07001171 MEM_BINDING binding = {GetShared<DEVICE_MEMORY_STATE>(mem), mem_offset, mem_size};
1172 if (binding.mem_state) {
1173 binding.mem_state->obj_bindings.insert(typed_handle);
locke-lunargd556cc32019-09-17 01:21:23 -06001174 // Need to set mem binding for this object
1175 mem_binding->sparse_bindings.insert(binding);
1176 mem_binding->UpdateBoundMemorySet();
1177 }
1178 }
1179 }
1180 return skip;
1181}
1182
locke-lunarg540b2252020-08-03 13:23:36 -06001183void ValidationStateTracker::UpdateDrawState(CMD_BUFFER_STATE *cb_state, CMD_TYPE cmd_type, const VkPipelineBindPoint bind_point,
1184 const char *function) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001185 const auto lv_bind_point = ConvertToLvlBindPoint(bind_point);
1186 auto &state = cb_state->lastBound[lv_bind_point];
locke-lunargd556cc32019-09-17 01:21:23 -06001187 PIPELINE_STATE *pPipe = state.pipeline_state;
1188 if (VK_NULL_HANDLE != state.pipeline_layout) {
1189 for (const auto &set_binding_pair : pPipe->active_slots) {
1190 uint32_t setIndex = set_binding_pair.first;
1191 // Pull the set node
1192 cvdescriptorset::DescriptorSet *descriptor_set = state.per_set[setIndex].bound_descriptor_set;
locke-lunargd556cc32019-09-17 01:21:23 -06001193
Tony-LunarG77822802020-05-28 16:35:46 -06001194 // For the "bindless" style resource usage with many descriptors, need to optimize command <-> descriptor binding
locke-lunargd556cc32019-09-17 01:21:23 -06001195
Tony-LunarG77822802020-05-28 16:35:46 -06001196 // TODO: If recreating the reduced_map here shows up in profilinging, need to find a way of sharing with the
1197 // Validate pass. Though in the case of "many" descriptors, typically the descriptor count >> binding count
1198 cvdescriptorset::PrefilterBindRequestMap reduced_map(*descriptor_set, set_binding_pair.second);
1199 const auto &binding_req_map = reduced_map.FilteredMap(*cb_state, *pPipe);
1200
1201 if (reduced_map.IsManyDescriptors()) {
1202 // Only update validate binding tags if we meet the "many" criteria in the Prefilter class
1203 descriptor_set->UpdateValidationCache(*cb_state, *pPipe, binding_req_map);
1204 }
1205
1206 // We can skip updating the state if "nothing" has changed since the last validation.
1207 // See CoreChecks::ValidateCmdBufDrawState for more details.
1208 bool descriptor_set_changed =
1209 !reduced_map.IsManyDescriptors() ||
1210 // Update if descriptor set (or contents) has changed
1211 state.per_set[setIndex].validated_set != descriptor_set ||
1212 state.per_set[setIndex].validated_set_change_count != descriptor_set->GetChangeCount() ||
1213 (!disabled[image_layout_validation] &&
1214 state.per_set[setIndex].validated_set_image_layout_change_count != cb_state->image_layout_change_count);
1215 bool need_update = descriptor_set_changed ||
1216 // Update if previous bindingReqMap doesn't include new bindingReqMap
1217 !std::includes(state.per_set[setIndex].validated_set_binding_req_map.begin(),
1218 state.per_set[setIndex].validated_set_binding_req_map.end(), binding_req_map.begin(),
1219 binding_req_map.end());
1220
1221 if (need_update) {
1222 // Bind this set and its active descriptor resources to the command buffer
1223 if (!descriptor_set_changed && reduced_map.IsManyDescriptors()) {
1224 // Only record the bindings that haven't already been recorded
1225 BindingReqMap delta_reqs;
1226 std::set_difference(binding_req_map.begin(), binding_req_map.end(),
1227 state.per_set[setIndex].validated_set_binding_req_map.begin(),
1228 state.per_set[setIndex].validated_set_binding_req_map.end(),
1229 std::inserter(delta_reqs, delta_reqs.begin()));
locke-lunarg540b2252020-08-03 13:23:36 -06001230 descriptor_set->UpdateDrawState(this, cb_state, cmd_type, pPipe, delta_reqs, function);
Tony-LunarG77822802020-05-28 16:35:46 -06001231 } else {
locke-lunarg540b2252020-08-03 13:23:36 -06001232 descriptor_set->UpdateDrawState(this, cb_state, cmd_type, pPipe, binding_req_map, function);
locke-lunargd556cc32019-09-17 01:21:23 -06001233 }
1234
Tony-LunarG77822802020-05-28 16:35:46 -06001235 state.per_set[setIndex].validated_set = descriptor_set;
1236 state.per_set[setIndex].validated_set_change_count = descriptor_set->GetChangeCount();
1237 state.per_set[setIndex].validated_set_image_layout_change_count = cb_state->image_layout_change_count;
1238 if (reduced_map.IsManyDescriptors()) {
1239 // Check whether old == new before assigning, the equality check is much cheaper than
1240 // freeing and reallocating the map.
1241 if (state.per_set[setIndex].validated_set_binding_req_map != set_binding_pair.second) {
1242 state.per_set[setIndex].validated_set_binding_req_map = set_binding_pair.second;
Jeff Bolz56308942019-10-06 22:05:23 -05001243 }
Tony-LunarG77822802020-05-28 16:35:46 -06001244 } else {
1245 state.per_set[setIndex].validated_set_binding_req_map = BindingReqMap();
locke-lunargd556cc32019-09-17 01:21:23 -06001246 }
1247 }
1248 }
1249 }
1250 if (!pPipe->vertex_binding_descriptions_.empty()) {
1251 cb_state->vertex_buffer_used = true;
1252 }
1253}
1254
1255// Remove set from setMap and delete the set
1256void ValidationStateTracker::FreeDescriptorSet(cvdescriptorset::DescriptorSet *descriptor_set) {
Jeff Bolze7fc67b2019-10-04 12:29:31 -05001257 descriptor_set->destroyed = true;
Jeff Bolzadbfa852019-10-04 13:53:30 -05001258 const VulkanTypedHandle obj_struct(descriptor_set->GetSet(), kVulkanObjectTypeDescriptorSet);
Jeff Bolz41a1ced2019-10-11 11:40:49 -05001259 // Any bound cmd buffers are now invalid
Jeff Bolzadbfa852019-10-04 13:53:30 -05001260 InvalidateCommandBuffers(descriptor_set->cb_bindings, obj_struct);
Jeff Bolz41a1ced2019-10-11 11:40:49 -05001261
locke-lunargd556cc32019-09-17 01:21:23 -06001262 setMap.erase(descriptor_set->GetSet());
1263}
1264
1265// Free all DS Pools including their Sets & related sub-structs
1266// NOTE : Calls to this function should be wrapped in mutex
1267void ValidationStateTracker::DeleteDescriptorSetPools() {
1268 for (auto ii = descriptorPoolMap.begin(); ii != descriptorPoolMap.end();) {
1269 // Remove this pools' sets from setMap and delete them
1270 for (auto ds : ii->second->sets) {
1271 FreeDescriptorSet(ds);
1272 }
1273 ii->second->sets.clear();
1274 ii = descriptorPoolMap.erase(ii);
1275 }
1276}
1277
1278// For given object struct return a ptr of BASE_NODE type for its wrapping struct
1279BASE_NODE *ValidationStateTracker::GetStateStructPtrFromObject(const VulkanTypedHandle &object_struct) {
Jeff Bolzadbfa852019-10-04 13:53:30 -05001280 if (object_struct.node) {
1281#ifdef _DEBUG
1282 // assert that lookup would find the same object
1283 VulkanTypedHandle other = object_struct;
1284 other.node = nullptr;
1285 assert(object_struct.node == GetStateStructPtrFromObject(other));
1286#endif
1287 return object_struct.node;
1288 }
locke-lunargd556cc32019-09-17 01:21:23 -06001289 BASE_NODE *base_ptr = nullptr;
1290 switch (object_struct.type) {
1291 case kVulkanObjectTypeDescriptorSet: {
1292 base_ptr = GetSetNode(object_struct.Cast<VkDescriptorSet>());
1293 break;
1294 }
1295 case kVulkanObjectTypeSampler: {
1296 base_ptr = GetSamplerState(object_struct.Cast<VkSampler>());
1297 break;
1298 }
1299 case kVulkanObjectTypeQueryPool: {
1300 base_ptr = GetQueryPoolState(object_struct.Cast<VkQueryPool>());
1301 break;
1302 }
1303 case kVulkanObjectTypePipeline: {
1304 base_ptr = GetPipelineState(object_struct.Cast<VkPipeline>());
1305 break;
1306 }
1307 case kVulkanObjectTypeBuffer: {
1308 base_ptr = GetBufferState(object_struct.Cast<VkBuffer>());
1309 break;
1310 }
1311 case kVulkanObjectTypeBufferView: {
1312 base_ptr = GetBufferViewState(object_struct.Cast<VkBufferView>());
1313 break;
1314 }
1315 case kVulkanObjectTypeImage: {
1316 base_ptr = GetImageState(object_struct.Cast<VkImage>());
1317 break;
1318 }
1319 case kVulkanObjectTypeImageView: {
1320 base_ptr = GetImageViewState(object_struct.Cast<VkImageView>());
1321 break;
1322 }
1323 case kVulkanObjectTypeEvent: {
1324 base_ptr = GetEventState(object_struct.Cast<VkEvent>());
1325 break;
1326 }
1327 case kVulkanObjectTypeDescriptorPool: {
1328 base_ptr = GetDescriptorPoolState(object_struct.Cast<VkDescriptorPool>());
1329 break;
1330 }
1331 case kVulkanObjectTypeCommandPool: {
1332 base_ptr = GetCommandPoolState(object_struct.Cast<VkCommandPool>());
1333 break;
1334 }
1335 case kVulkanObjectTypeFramebuffer: {
1336 base_ptr = GetFramebufferState(object_struct.Cast<VkFramebuffer>());
1337 break;
1338 }
1339 case kVulkanObjectTypeRenderPass: {
1340 base_ptr = GetRenderPassState(object_struct.Cast<VkRenderPass>());
1341 break;
1342 }
1343 case kVulkanObjectTypeDeviceMemory: {
1344 base_ptr = GetDevMemState(object_struct.Cast<VkDeviceMemory>());
1345 break;
1346 }
1347 case kVulkanObjectTypeAccelerationStructureNV: {
sourav parmarcd5fb182020-07-17 12:58:44 -07001348 base_ptr = GetAccelerationStructureStateNV(object_struct.Cast<VkAccelerationStructureNV>());
1349 break;
1350 }
1351 case kVulkanObjectTypeAccelerationStructureKHR: {
1352 base_ptr = GetAccelerationStructureStateKHR(object_struct.Cast<VkAccelerationStructureKHR>());
locke-lunargd556cc32019-09-17 01:21:23 -06001353 break;
1354 }
Jeff Bolzadbfa852019-10-04 13:53:30 -05001355 case kVulkanObjectTypeUnknown:
1356 // This can happen if an element of the object_bindings vector has been
1357 // zeroed out, after an object is destroyed.
1358 break;
locke-lunargd556cc32019-09-17 01:21:23 -06001359 default:
1360 // TODO : Any other objects to be handled here?
1361 assert(0);
1362 break;
1363 }
1364 return base_ptr;
1365}
1366
sfricke-samsungbf1a2ed2020-06-14 23:31:00 -07001367// Gets union of all features defined by Potential Format Features
1368// except, does not handle the external format case for AHB as that only can be used for sampled images
sfricke-samsungbe3584f2020-04-22 14:58:06 -07001369VkFormatFeatureFlags ValidationStateTracker::GetPotentialFormatFeatures(VkFormat format) const {
1370 VkFormatFeatureFlags format_features = 0;
1371
1372 if (format != VK_FORMAT_UNDEFINED) {
1373 VkFormatProperties format_properties;
1374 DispatchGetPhysicalDeviceFormatProperties(physical_device, format, &format_properties);
1375 format_features |= format_properties.linearTilingFeatures;
1376 format_features |= format_properties.optimalTilingFeatures;
1377 if (device_extensions.vk_ext_image_drm_format_modifier) {
1378 // VK_KHR_get_physical_device_properties2 is required in this case
1379 VkFormatProperties2 format_properties_2 = {VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2};
1380 VkDrmFormatModifierPropertiesListEXT drm_properties_list = {VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT,
1381 nullptr};
1382 format_properties_2.pNext = (void *)&drm_properties_list;
1383 DispatchGetPhysicalDeviceFormatProperties2(physical_device, format, &format_properties_2);
1384 for (uint32_t i = 0; i < drm_properties_list.drmFormatModifierCount; i++) {
1385 format_features |= drm_properties_list.pDrmFormatModifierProperties[i].drmFormatModifierTilingFeatures;
1386 }
1387 }
1388 }
1389
1390 return format_features;
1391}
1392
locke-lunargd556cc32019-09-17 01:21:23 -06001393// Tie the VulkanTypedHandle to the cmd buffer which includes:
1394// Add object_binding to cmd buffer
1395// Add cb_binding to object
Jeff Bolzadbfa852019-10-04 13:53:30 -05001396bool ValidationStateTracker::AddCommandBufferBinding(small_unordered_map<CMD_BUFFER_STATE *, int, 8> &cb_bindings,
locke-lunargd556cc32019-09-17 01:21:23 -06001397 const VulkanTypedHandle &obj, CMD_BUFFER_STATE *cb_node) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06001398 if (disabled[command_buffer_state]) {
Jeff Bolzadbfa852019-10-04 13:53:30 -05001399 return false;
locke-lunargd556cc32019-09-17 01:21:23 -06001400 }
Jeff Bolzadbfa852019-10-04 13:53:30 -05001401 // Insert the cb_binding with a default 'index' of -1. Then push the obj into the object_bindings
1402 // vector, and update cb_bindings[cb_node] with the index of that element of the vector.
1403 auto inserted = cb_bindings.insert({cb_node, -1});
1404 if (inserted.second) {
1405 cb_node->object_bindings.push_back(obj);
1406 inserted.first->second = (int)cb_node->object_bindings.size() - 1;
1407 return true;
1408 }
1409 return false;
locke-lunargd556cc32019-09-17 01:21:23 -06001410}
1411
1412// For a given object, if cb_node is in that objects cb_bindings, remove cb_node
1413void ValidationStateTracker::RemoveCommandBufferBinding(VulkanTypedHandle const &object, CMD_BUFFER_STATE *cb_node) {
1414 BASE_NODE *base_obj = GetStateStructPtrFromObject(object);
1415 if (base_obj) base_obj->cb_bindings.erase(cb_node);
1416}
1417
1418// Reset the command buffer state
1419// Maintain the createInfo and set state to CB_NEW, but clear all other state
1420void ValidationStateTracker::ResetCommandBufferState(const VkCommandBuffer cb) {
1421 CMD_BUFFER_STATE *pCB = GetCBState(cb);
1422 if (pCB) {
1423 pCB->in_use.store(0);
1424 // Reset CB state (note that createInfo is not cleared)
1425 pCB->commandBuffer = cb;
1426 memset(&pCB->beginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1427 memset(&pCB->inheritanceInfo, 0, sizeof(VkCommandBufferInheritanceInfo));
1428 pCB->hasDrawCmd = false;
1429 pCB->hasTraceRaysCmd = false;
1430 pCB->hasBuildAccelerationStructureCmd = false;
1431 pCB->hasDispatchCmd = false;
1432 pCB->state = CB_NEW;
Lionel Landwerlinc7420912019-05-23 00:33:42 +01001433 pCB->commandCount = 0;
locke-lunargd556cc32019-09-17 01:21:23 -06001434 pCB->submitCount = 0;
1435 pCB->image_layout_change_count = 1; // Start at 1. 0 is insert value for validation cache versions, s.t. new == dirty
1436 pCB->status = 0;
1437 pCB->static_status = 0;
1438 pCB->viewportMask = 0;
Piers Daniell39842ee2020-07-10 16:42:33 -06001439 pCB->viewportWithCountMask = 0;
Tobias Hector6663c9b2020-11-05 10:18:02 +00001440 pCB->viewportWithCountCount = 0;
locke-lunargd556cc32019-09-17 01:21:23 -06001441 pCB->scissorMask = 0;
Piers Daniell39842ee2020-07-10 16:42:33 -06001442 pCB->scissorWithCountMask = 0;
1443 pCB->primitiveTopology = VK_PRIMITIVE_TOPOLOGY_MAX_ENUM;
locke-lunargd556cc32019-09-17 01:21:23 -06001444
1445 for (auto &item : pCB->lastBound) {
locke-lunargb8d7a7a2020-10-25 16:01:52 -06001446 item.reset();
locke-lunargd556cc32019-09-17 01:21:23 -06001447 }
1448
Tony-LunarG61e7c0c2020-03-03 16:09:11 -07001449 pCB->activeRenderPassBeginInfo = safe_VkRenderPassBeginInfo();
locke-lunargd556cc32019-09-17 01:21:23 -06001450 pCB->activeRenderPass = nullptr;
locke-lunargfc78e932020-11-19 17:06:24 -07001451 pCB->active_attachments = nullptr;
1452 pCB->active_subpasses = nullptr;
1453 pCB->attachments_view_states.clear();
locke-lunargd556cc32019-09-17 01:21:23 -06001454 pCB->activeSubpassContents = VK_SUBPASS_CONTENTS_INLINE;
1455 pCB->activeSubpass = 0;
1456 pCB->broken_bindings.clear();
1457 pCB->waitedEvents.clear();
1458 pCB->events.clear();
1459 pCB->writeEventsBeforeWait.clear();
locke-lunargd556cc32019-09-17 01:21:23 -06001460 pCB->activeQueries.clear();
1461 pCB->startedQueries.clear();
1462 pCB->image_layout_map.clear();
locke-lunargd556cc32019-09-17 01:21:23 -06001463 pCB->current_vertex_buffer_binding_info.vertex_buffer_bindings.clear();
1464 pCB->vertex_buffer_used = false;
1465 pCB->primaryCommandBuffer = VK_NULL_HANDLE;
1466 // If secondary, invalidate any primary command buffer that may call us.
1467 if (pCB->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Jeff Bolzadbfa852019-10-04 13:53:30 -05001468 InvalidateLinkedCommandBuffers(pCB->linkedCommandBuffers, VulkanTypedHandle(cb, kVulkanObjectTypeCommandBuffer));
locke-lunargd556cc32019-09-17 01:21:23 -06001469 }
1470
1471 // Remove reverse command buffer links.
1472 for (auto pSubCB : pCB->linkedCommandBuffers) {
1473 pSubCB->linkedCommandBuffers.erase(pCB);
1474 }
1475 pCB->linkedCommandBuffers.clear();
locke-lunargd556cc32019-09-17 01:21:23 -06001476 pCB->queue_submit_functions.clear();
1477 pCB->cmd_execute_commands_functions.clear();
1478 pCB->eventUpdates.clear();
1479 pCB->queryUpdates.clear();
1480
1481 // Remove object bindings
1482 for (const auto &obj : pCB->object_bindings) {
1483 RemoveCommandBufferBinding(obj, pCB);
1484 }
1485 pCB->object_bindings.clear();
1486 // Remove this cmdBuffer's reference from each FrameBuffer's CB ref list
1487 for (auto framebuffer : pCB->framebuffers) {
locke-lunargaecf2152020-05-12 17:15:41 -06001488 framebuffer->cb_bindings.erase(pCB);
locke-lunargd556cc32019-09-17 01:21:23 -06001489 }
1490 pCB->framebuffers.clear();
1491 pCB->activeFramebuffer = VK_NULL_HANDLE;
Ricardo Garcia05ae4382020-12-01 17:44:12 +01001492 pCB->index_buffer_binding.reset();
locke-lunargd556cc32019-09-17 01:21:23 -06001493
1494 pCB->qfo_transfer_image_barriers.Reset();
1495 pCB->qfo_transfer_buffer_barriers.Reset();
1496
1497 // Clean up the label data
1498 ResetCmdDebugUtilsLabel(report_data, pCB->commandBuffer);
1499 pCB->debug_label.Reset();
locke-gb3ce08f2019-09-30 12:30:56 -06001500 pCB->validate_descriptorsets_in_queuesubmit.clear();
Attilio Provenzano02859b22020-02-27 14:17:28 +00001501
1502 // Best practices info
1503 pCB->small_indexed_draw_call_count = 0;
Jeremy Hayes9bda85a2020-05-21 16:36:17 -06001504
1505 pCB->transform_feedback_active = false;
locke-lunargd556cc32019-09-17 01:21:23 -06001506 }
1507 if (command_buffer_reset_callback) {
1508 (*command_buffer_reset_callback)(cb);
1509 }
1510}
1511
1512void ValidationStateTracker::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
1513 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice,
1514 VkResult result) {
1515 if (VK_SUCCESS != result) return;
1516
1517 const VkPhysicalDeviceFeatures *enabled_features_found = pCreateInfo->pEnabledFeatures;
1518 if (nullptr == enabled_features_found) {
1519 const auto *features2 = lvl_find_in_chain<VkPhysicalDeviceFeatures2KHR>(pCreateInfo->pNext);
1520 if (features2) {
1521 enabled_features_found = &(features2->features);
1522 }
1523 }
1524
1525 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
1526 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, this->container_type);
1527 ValidationStateTracker *state_tracker = static_cast<ValidationStateTracker *>(validation_data);
1528
1529 if (nullptr == enabled_features_found) {
1530 state_tracker->enabled_features.core = {};
1531 } else {
1532 state_tracker->enabled_features.core = *enabled_features_found;
1533 }
1534
1535 // Make sure that queue_family_properties are obtained for this device's physical_device, even if the app has not
1536 // previously set them through an explicit API call.
1537 uint32_t count;
1538 auto pd_state = GetPhysicalDeviceState(gpu);
1539 DispatchGetPhysicalDeviceQueueFamilyProperties(gpu, &count, nullptr);
1540 pd_state->queue_family_properties.resize(std::max(static_cast<uint32_t>(pd_state->queue_family_properties.size()), count));
1541 DispatchGetPhysicalDeviceQueueFamilyProperties(gpu, &count, &pd_state->queue_family_properties[0]);
1542 // Save local link to this device's physical device state
1543 state_tracker->physical_device_state = pd_state;
1544
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001545 const auto *vulkan_12_features = lvl_find_in_chain<VkPhysicalDeviceVulkan12Features>(pCreateInfo->pNext);
1546 if (vulkan_12_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001547 state_tracker->enabled_features.core12 = *vulkan_12_features;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001548 } else {
sfricke-samsung27c70722020-05-02 08:42:39 -07001549 // Set Extension Feature Aliases to false as there is no struct to check
1550 state_tracker->enabled_features.core12.drawIndirectCount = VK_FALSE;
1551 state_tracker->enabled_features.core12.samplerMirrorClampToEdge = VK_FALSE;
1552 state_tracker->enabled_features.core12.descriptorIndexing = VK_FALSE;
1553 state_tracker->enabled_features.core12.samplerFilterMinmax = VK_FALSE;
1554 state_tracker->enabled_features.core12.shaderOutputLayer = VK_FALSE;
1555 state_tracker->enabled_features.core12.shaderOutputViewportIndex = VK_FALSE;
1556
1557 // These structs are only allowed in pNext chain if there is no VkPhysicalDeviceVulkan12Features
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001558
1559 const auto *eight_bit_storage_features = lvl_find_in_chain<VkPhysicalDevice8BitStorageFeatures>(pCreateInfo->pNext);
1560 if (eight_bit_storage_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001561 state_tracker->enabled_features.core12.storageBuffer8BitAccess = eight_bit_storage_features->storageBuffer8BitAccess;
1562 state_tracker->enabled_features.core12.uniformAndStorageBuffer8BitAccess =
1563 eight_bit_storage_features->uniformAndStorageBuffer8BitAccess;
1564 state_tracker->enabled_features.core12.storagePushConstant8 = eight_bit_storage_features->storagePushConstant8;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001565 }
1566
1567 const auto *float16_int8_features = lvl_find_in_chain<VkPhysicalDeviceShaderFloat16Int8Features>(pCreateInfo->pNext);
1568 if (float16_int8_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001569 state_tracker->enabled_features.core12.shaderFloat16 = float16_int8_features->shaderFloat16;
1570 state_tracker->enabled_features.core12.shaderInt8 = float16_int8_features->shaderInt8;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001571 }
1572
1573 const auto *descriptor_indexing_features =
1574 lvl_find_in_chain<VkPhysicalDeviceDescriptorIndexingFeatures>(pCreateInfo->pNext);
1575 if (descriptor_indexing_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001576 state_tracker->enabled_features.core12.shaderInputAttachmentArrayDynamicIndexing =
1577 descriptor_indexing_features->shaderInputAttachmentArrayDynamicIndexing;
1578 state_tracker->enabled_features.core12.shaderUniformTexelBufferArrayDynamicIndexing =
1579 descriptor_indexing_features->shaderUniformTexelBufferArrayDynamicIndexing;
1580 state_tracker->enabled_features.core12.shaderStorageTexelBufferArrayDynamicIndexing =
1581 descriptor_indexing_features->shaderStorageTexelBufferArrayDynamicIndexing;
1582 state_tracker->enabled_features.core12.shaderUniformBufferArrayNonUniformIndexing =
1583 descriptor_indexing_features->shaderUniformBufferArrayNonUniformIndexing;
1584 state_tracker->enabled_features.core12.shaderSampledImageArrayNonUniformIndexing =
1585 descriptor_indexing_features->shaderSampledImageArrayNonUniformIndexing;
1586 state_tracker->enabled_features.core12.shaderStorageBufferArrayNonUniformIndexing =
1587 descriptor_indexing_features->shaderStorageBufferArrayNonUniformIndexing;
1588 state_tracker->enabled_features.core12.shaderStorageImageArrayNonUniformIndexing =
1589 descriptor_indexing_features->shaderStorageImageArrayNonUniformIndexing;
1590 state_tracker->enabled_features.core12.shaderInputAttachmentArrayNonUniformIndexing =
1591 descriptor_indexing_features->shaderInputAttachmentArrayNonUniformIndexing;
1592 state_tracker->enabled_features.core12.shaderUniformTexelBufferArrayNonUniformIndexing =
1593 descriptor_indexing_features->shaderUniformTexelBufferArrayNonUniformIndexing;
1594 state_tracker->enabled_features.core12.shaderStorageTexelBufferArrayNonUniformIndexing =
1595 descriptor_indexing_features->shaderStorageTexelBufferArrayNonUniformIndexing;
1596 state_tracker->enabled_features.core12.descriptorBindingUniformBufferUpdateAfterBind =
1597 descriptor_indexing_features->descriptorBindingUniformBufferUpdateAfterBind;
1598 state_tracker->enabled_features.core12.descriptorBindingSampledImageUpdateAfterBind =
1599 descriptor_indexing_features->descriptorBindingSampledImageUpdateAfterBind;
1600 state_tracker->enabled_features.core12.descriptorBindingStorageImageUpdateAfterBind =
1601 descriptor_indexing_features->descriptorBindingStorageImageUpdateAfterBind;
1602 state_tracker->enabled_features.core12.descriptorBindingStorageBufferUpdateAfterBind =
1603 descriptor_indexing_features->descriptorBindingStorageBufferUpdateAfterBind;
1604 state_tracker->enabled_features.core12.descriptorBindingUniformTexelBufferUpdateAfterBind =
1605 descriptor_indexing_features->descriptorBindingUniformTexelBufferUpdateAfterBind;
1606 state_tracker->enabled_features.core12.descriptorBindingStorageTexelBufferUpdateAfterBind =
1607 descriptor_indexing_features->descriptorBindingStorageTexelBufferUpdateAfterBind;
1608 state_tracker->enabled_features.core12.descriptorBindingUpdateUnusedWhilePending =
1609 descriptor_indexing_features->descriptorBindingUpdateUnusedWhilePending;
1610 state_tracker->enabled_features.core12.descriptorBindingPartiallyBound =
1611 descriptor_indexing_features->descriptorBindingPartiallyBound;
1612 state_tracker->enabled_features.core12.descriptorBindingVariableDescriptorCount =
1613 descriptor_indexing_features->descriptorBindingVariableDescriptorCount;
1614 state_tracker->enabled_features.core12.runtimeDescriptorArray = descriptor_indexing_features->runtimeDescriptorArray;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001615 }
1616
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001617 const auto *scalar_block_layout_features = lvl_find_in_chain<VkPhysicalDeviceScalarBlockLayoutFeatures>(pCreateInfo->pNext);
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001618 if (scalar_block_layout_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001619 state_tracker->enabled_features.core12.scalarBlockLayout = scalar_block_layout_features->scalarBlockLayout;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001620 }
1621
1622 const auto *imageless_framebuffer_features =
1623 lvl_find_in_chain<VkPhysicalDeviceImagelessFramebufferFeatures>(pCreateInfo->pNext);
1624 if (imageless_framebuffer_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001625 state_tracker->enabled_features.core12.imagelessFramebuffer = imageless_framebuffer_features->imagelessFramebuffer;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001626 }
1627
1628 const auto *uniform_buffer_standard_layout_features =
1629 lvl_find_in_chain<VkPhysicalDeviceUniformBufferStandardLayoutFeatures>(pCreateInfo->pNext);
1630 if (uniform_buffer_standard_layout_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001631 state_tracker->enabled_features.core12.uniformBufferStandardLayout =
1632 uniform_buffer_standard_layout_features->uniformBufferStandardLayout;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001633 }
1634
1635 const auto *subgroup_extended_types_features =
1636 lvl_find_in_chain<VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures>(pCreateInfo->pNext);
1637 if (subgroup_extended_types_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001638 state_tracker->enabled_features.core12.shaderSubgroupExtendedTypes =
1639 subgroup_extended_types_features->shaderSubgroupExtendedTypes;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001640 }
1641
1642 const auto *separate_depth_stencil_layouts_features =
1643 lvl_find_in_chain<VkPhysicalDeviceSeparateDepthStencilLayoutsFeatures>(pCreateInfo->pNext);
1644 if (separate_depth_stencil_layouts_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001645 state_tracker->enabled_features.core12.separateDepthStencilLayouts =
1646 separate_depth_stencil_layouts_features->separateDepthStencilLayouts;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001647 }
1648
1649 const auto *host_query_reset_features = lvl_find_in_chain<VkPhysicalDeviceHostQueryResetFeatures>(pCreateInfo->pNext);
1650 if (host_query_reset_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001651 state_tracker->enabled_features.core12.hostQueryReset = host_query_reset_features->hostQueryReset;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001652 }
1653
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001654 const auto *timeline_semaphore_features = lvl_find_in_chain<VkPhysicalDeviceTimelineSemaphoreFeatures>(pCreateInfo->pNext);
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001655 if (timeline_semaphore_features) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001656 state_tracker->enabled_features.core12.timelineSemaphore = timeline_semaphore_features->timelineSemaphore;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001657 }
1658
1659 const auto *buffer_device_address = lvl_find_in_chain<VkPhysicalDeviceBufferDeviceAddressFeatures>(pCreateInfo->pNext);
1660 if (buffer_device_address) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001661 state_tracker->enabled_features.core12.bufferDeviceAddress = buffer_device_address->bufferDeviceAddress;
1662 state_tracker->enabled_features.core12.bufferDeviceAddressCaptureReplay =
1663 buffer_device_address->bufferDeviceAddressCaptureReplay;
1664 state_tracker->enabled_features.core12.bufferDeviceAddressMultiDevice =
1665 buffer_device_address->bufferDeviceAddressMultiDevice;
1666 }
1667 }
1668
1669 const auto *vulkan_11_features = lvl_find_in_chain<VkPhysicalDeviceVulkan11Features>(pCreateInfo->pNext);
1670 if (vulkan_11_features) {
1671 state_tracker->enabled_features.core11 = *vulkan_11_features;
1672 } else {
1673 // These structs are only allowed in pNext chain if there is no kPhysicalDeviceVulkan11Features
1674
1675 const auto *sixteen_bit_storage_features = lvl_find_in_chain<VkPhysicalDevice16BitStorageFeatures>(pCreateInfo->pNext);
1676 if (sixteen_bit_storage_features) {
1677 state_tracker->enabled_features.core11.storageBuffer16BitAccess =
1678 sixteen_bit_storage_features->storageBuffer16BitAccess;
1679 state_tracker->enabled_features.core11.uniformAndStorageBuffer16BitAccess =
1680 sixteen_bit_storage_features->uniformAndStorageBuffer16BitAccess;
1681 state_tracker->enabled_features.core11.storagePushConstant16 = sixteen_bit_storage_features->storagePushConstant16;
1682 state_tracker->enabled_features.core11.storageInputOutput16 = sixteen_bit_storage_features->storageInputOutput16;
1683 }
1684
1685 const auto *multiview_features = lvl_find_in_chain<VkPhysicalDeviceMultiviewFeatures>(pCreateInfo->pNext);
1686 if (multiview_features) {
1687 state_tracker->enabled_features.core11.multiview = multiview_features->multiview;
1688 state_tracker->enabled_features.core11.multiviewGeometryShader = multiview_features->multiviewGeometryShader;
1689 state_tracker->enabled_features.core11.multiviewTessellationShader = multiview_features->multiviewTessellationShader;
1690 }
1691
1692 const auto *variable_pointers_features = lvl_find_in_chain<VkPhysicalDeviceVariablePointersFeatures>(pCreateInfo->pNext);
1693 if (variable_pointers_features) {
1694 state_tracker->enabled_features.core11.variablePointersStorageBuffer =
1695 variable_pointers_features->variablePointersStorageBuffer;
1696 state_tracker->enabled_features.core11.variablePointers = variable_pointers_features->variablePointers;
1697 }
1698
1699 const auto *protected_memory_features = lvl_find_in_chain<VkPhysicalDeviceProtectedMemoryFeatures>(pCreateInfo->pNext);
1700 if (protected_memory_features) {
1701 state_tracker->enabled_features.core11.protectedMemory = protected_memory_features->protectedMemory;
1702 }
1703
1704 const auto *ycbcr_conversion_features =
1705 lvl_find_in_chain<VkPhysicalDeviceSamplerYcbcrConversionFeatures>(pCreateInfo->pNext);
1706 if (ycbcr_conversion_features) {
1707 state_tracker->enabled_features.core11.samplerYcbcrConversion = ycbcr_conversion_features->samplerYcbcrConversion;
1708 }
1709
1710 const auto *shader_draw_parameters_features =
1711 lvl_find_in_chain<VkPhysicalDeviceShaderDrawParametersFeatures>(pCreateInfo->pNext);
1712 if (shader_draw_parameters_features) {
1713 state_tracker->enabled_features.core11.shaderDrawParameters = shader_draw_parameters_features->shaderDrawParameters;
Tony-LunarGb036c2f2019-12-05 14:38:25 -07001714 }
1715 }
1716
locke-lunargd556cc32019-09-17 01:21:23 -06001717 const auto *device_group_ci = lvl_find_in_chain<VkDeviceGroupDeviceCreateInfo>(pCreateInfo->pNext);
Tony-LunarGca4891a2020-08-10 15:46:46 -06001718 if (device_group_ci) {
1719 state_tracker->physical_device_count = device_group_ci->physicalDeviceCount;
1720 state_tracker->device_group_create_info = *device_group_ci;
1721 } else {
1722 state_tracker->physical_device_count = 1;
1723 }
locke-lunargd556cc32019-09-17 01:21:23 -06001724
locke-lunargd556cc32019-09-17 01:21:23 -06001725 const auto *exclusive_scissor_features = lvl_find_in_chain<VkPhysicalDeviceExclusiveScissorFeaturesNV>(pCreateInfo->pNext);
1726 if (exclusive_scissor_features) {
1727 state_tracker->enabled_features.exclusive_scissor = *exclusive_scissor_features;
1728 }
1729
1730 const auto *shading_rate_image_features = lvl_find_in_chain<VkPhysicalDeviceShadingRateImageFeaturesNV>(pCreateInfo->pNext);
1731 if (shading_rate_image_features) {
1732 state_tracker->enabled_features.shading_rate_image = *shading_rate_image_features;
1733 }
1734
1735 const auto *mesh_shader_features = lvl_find_in_chain<VkPhysicalDeviceMeshShaderFeaturesNV>(pCreateInfo->pNext);
1736 if (mesh_shader_features) {
1737 state_tracker->enabled_features.mesh_shader = *mesh_shader_features;
1738 }
1739
1740 const auto *inline_uniform_block_features =
1741 lvl_find_in_chain<VkPhysicalDeviceInlineUniformBlockFeaturesEXT>(pCreateInfo->pNext);
1742 if (inline_uniform_block_features) {
1743 state_tracker->enabled_features.inline_uniform_block = *inline_uniform_block_features;
1744 }
1745
1746 const auto *transform_feedback_features = lvl_find_in_chain<VkPhysicalDeviceTransformFeedbackFeaturesEXT>(pCreateInfo->pNext);
1747 if (transform_feedback_features) {
1748 state_tracker->enabled_features.transform_feedback_features = *transform_feedback_features;
1749 }
1750
locke-lunargd556cc32019-09-17 01:21:23 -06001751 const auto *vtx_attrib_div_features = lvl_find_in_chain<VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT>(pCreateInfo->pNext);
1752 if (vtx_attrib_div_features) {
1753 state_tracker->enabled_features.vtx_attrib_divisor_features = *vtx_attrib_div_features;
1754 }
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001755
Jeff Bolz4563f2a2019-12-10 13:30:30 -06001756 const auto *buffer_device_address_ext = lvl_find_in_chain<VkPhysicalDeviceBufferDeviceAddressFeaturesEXT>(pCreateInfo->pNext);
1757 if (buffer_device_address_ext) {
Jeff Bolz33fc6722020-03-31 12:58:16 -05001758 state_tracker->enabled_features.buffer_device_address_ext = *buffer_device_address_ext;
locke-lunargd556cc32019-09-17 01:21:23 -06001759 }
1760
1761 const auto *cooperative_matrix_features = lvl_find_in_chain<VkPhysicalDeviceCooperativeMatrixFeaturesNV>(pCreateInfo->pNext);
1762 if (cooperative_matrix_features) {
1763 state_tracker->enabled_features.cooperative_matrix_features = *cooperative_matrix_features;
1764 }
1765
locke-lunargd556cc32019-09-17 01:21:23 -06001766 const auto *compute_shader_derivatives_features =
1767 lvl_find_in_chain<VkPhysicalDeviceComputeShaderDerivativesFeaturesNV>(pCreateInfo->pNext);
1768 if (compute_shader_derivatives_features) {
1769 state_tracker->enabled_features.compute_shader_derivatives_features = *compute_shader_derivatives_features;
1770 }
1771
1772 const auto *fragment_shader_barycentric_features =
1773 lvl_find_in_chain<VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV>(pCreateInfo->pNext);
1774 if (fragment_shader_barycentric_features) {
1775 state_tracker->enabled_features.fragment_shader_barycentric_features = *fragment_shader_barycentric_features;
1776 }
1777
1778 const auto *shader_image_footprint_features =
1779 lvl_find_in_chain<VkPhysicalDeviceShaderImageFootprintFeaturesNV>(pCreateInfo->pNext);
1780 if (shader_image_footprint_features) {
1781 state_tracker->enabled_features.shader_image_footprint_features = *shader_image_footprint_features;
1782 }
1783
1784 const auto *fragment_shader_interlock_features =
1785 lvl_find_in_chain<VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT>(pCreateInfo->pNext);
1786 if (fragment_shader_interlock_features) {
1787 state_tracker->enabled_features.fragment_shader_interlock_features = *fragment_shader_interlock_features;
1788 }
1789
1790 const auto *demote_to_helper_invocation_features =
1791 lvl_find_in_chain<VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT>(pCreateInfo->pNext);
1792 if (demote_to_helper_invocation_features) {
1793 state_tracker->enabled_features.demote_to_helper_invocation_features = *demote_to_helper_invocation_features;
1794 }
1795
1796 const auto *texel_buffer_alignment_features =
1797 lvl_find_in_chain<VkPhysicalDeviceTexelBufferAlignmentFeaturesEXT>(pCreateInfo->pNext);
1798 if (texel_buffer_alignment_features) {
1799 state_tracker->enabled_features.texel_buffer_alignment_features = *texel_buffer_alignment_features;
1800 }
1801
locke-lunargd556cc32019-09-17 01:21:23 -06001802 const auto *pipeline_exe_props_features =
1803 lvl_find_in_chain<VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR>(pCreateInfo->pNext);
1804 if (pipeline_exe_props_features) {
1805 state_tracker->enabled_features.pipeline_exe_props_features = *pipeline_exe_props_features;
1806 }
1807
Jeff Bolz82f854d2019-09-17 14:56:47 -05001808 const auto *dedicated_allocation_image_aliasing_features =
1809 lvl_find_in_chain<VkPhysicalDeviceDedicatedAllocationImageAliasingFeaturesNV>(pCreateInfo->pNext);
1810 if (dedicated_allocation_image_aliasing_features) {
1811 state_tracker->enabled_features.dedicated_allocation_image_aliasing_features =
1812 *dedicated_allocation_image_aliasing_features;
1813 }
1814
Lionel Landwerlinc7420912019-05-23 00:33:42 +01001815 const auto *performance_query_features = lvl_find_in_chain<VkPhysicalDevicePerformanceQueryFeaturesKHR>(pCreateInfo->pNext);
1816 if (performance_query_features) {
1817 state_tracker->enabled_features.performance_query_features = *performance_query_features;
1818 }
1819
Tobias Hector782bcde2019-11-28 16:19:42 +00001820 const auto *device_coherent_memory_features = lvl_find_in_chain<VkPhysicalDeviceCoherentMemoryFeaturesAMD>(pCreateInfo->pNext);
1821 if (device_coherent_memory_features) {
1822 state_tracker->enabled_features.device_coherent_memory_features = *device_coherent_memory_features;
1823 }
1824
sfricke-samsungcead0802020-01-30 22:20:10 -08001825 const auto *ycbcr_image_array_features = lvl_find_in_chain<VkPhysicalDeviceYcbcrImageArraysFeaturesEXT>(pCreateInfo->pNext);
1826 if (ycbcr_image_array_features) {
1827 state_tracker->enabled_features.ycbcr_image_array_features = *ycbcr_image_array_features;
1828 }
1829
sourav parmarcd5fb182020-07-17 12:58:44 -07001830 const auto *ray_query_features = lvl_find_in_chain<VkPhysicalDeviceRayQueryFeaturesKHR>(pCreateInfo->pNext);
1831 if (ray_query_features) {
1832 state_tracker->enabled_features.ray_query_features = *ray_query_features;
1833 }
1834
1835 const auto *ray_tracing_pipeline_features =
1836 lvl_find_in_chain<VkPhysicalDeviceRayTracingPipelineFeaturesKHR>(pCreateInfo->pNext);
1837 if (ray_tracing_pipeline_features) {
1838 state_tracker->enabled_features.ray_tracing_pipeline_features = *ray_tracing_pipeline_features;
1839 }
1840
1841 const auto *ray_tracing_acceleration_structure_features =
1842 lvl_find_in_chain<VkPhysicalDeviceAccelerationStructureFeaturesKHR>(pCreateInfo->pNext);
1843 if (ray_tracing_acceleration_structure_features) {
1844 state_tracker->enabled_features.ray_tracing_acceleration_structure_features = *ray_tracing_acceleration_structure_features;
Jeff Bolz443c2ca2020-03-19 12:11:51 -05001845 }
1846
Jeff Bolz165818a2020-05-08 11:19:03 -05001847 const auto *robustness2_features = lvl_find_in_chain<VkPhysicalDeviceRobustness2FeaturesEXT>(pCreateInfo->pNext);
1848 if (robustness2_features) {
1849 state_tracker->enabled_features.robustness2_features = *robustness2_features;
1850 }
1851
janharaldfredriksen-arm3b793772020-05-12 18:55:53 +02001852 const auto *fragment_density_map_features =
1853 lvl_find_in_chain<VkPhysicalDeviceFragmentDensityMapFeaturesEXT>(pCreateInfo->pNext);
1854 if (fragment_density_map_features) {
1855 state_tracker->enabled_features.fragment_density_map_features = *fragment_density_map_features;
1856 }
1857
janharaldfredriksen-arm36e17572020-07-07 13:59:28 +02001858 const auto *fragment_density_map_features2 =
1859 lvl_find_in_chain<VkPhysicalDeviceFragmentDensityMap2FeaturesEXT>(pCreateInfo->pNext);
1860 if (fragment_density_map_features2) {
1861 state_tracker->enabled_features.fragment_density_map2_features = *fragment_density_map_features2;
1862 }
1863
sfricke-samsung0c4a06f2020-06-27 01:24:32 -07001864 const auto *astc_decode_features = lvl_find_in_chain<VkPhysicalDeviceASTCDecodeFeaturesEXT>(pCreateInfo->pNext);
1865 if (astc_decode_features) {
1866 state_tracker->enabled_features.astc_decode_features = *astc_decode_features;
1867 }
1868
Tony-LunarG7337b312020-04-15 16:40:25 -06001869 const auto *custom_border_color_features = lvl_find_in_chain<VkPhysicalDeviceCustomBorderColorFeaturesEXT>(pCreateInfo->pNext);
1870 if (custom_border_color_features) {
1871 state_tracker->enabled_features.custom_border_color_features = *custom_border_color_features;
1872 }
1873
sfricke-samsungfd661d62020-05-16 00:57:27 -07001874 const auto *pipeline_creation_cache_control_features =
1875 lvl_find_in_chain<VkPhysicalDevicePipelineCreationCacheControlFeaturesEXT>(pCreateInfo->pNext);
1876 if (pipeline_creation_cache_control_features) {
1877 state_tracker->enabled_features.pipeline_creation_cache_control_features = *pipeline_creation_cache_control_features;
1878 }
1879
Tobias Hector6663c9b2020-11-05 10:18:02 +00001880 const auto *fragment_shading_rate_features =
1881 lvl_find_in_chain<VkPhysicalDeviceFragmentShadingRateFeaturesKHR>(pCreateInfo->pNext);
1882 if (fragment_shading_rate_features) {
1883 state_tracker->enabled_features.fragment_shading_rate_features = *fragment_shading_rate_features;
1884 }
1885
Piers Daniell39842ee2020-07-10 16:42:33 -06001886 const auto *extended_dynamic_state_features =
1887 lvl_find_in_chain<VkPhysicalDeviceExtendedDynamicStateFeaturesEXT>(pCreateInfo->pNext);
1888 if (extended_dynamic_state_features) {
1889 state_tracker->enabled_features.extended_dynamic_state_features = *extended_dynamic_state_features;
1890 }
1891
locke-lunarg3fa463a2020-10-23 16:39:04 -06001892 const auto *multiview_features = lvl_find_in_chain<VkPhysicalDeviceMultiviewFeatures>(pCreateInfo->pNext);
1893 if (multiview_features) {
1894 state_tracker->enabled_features.multiview_features = *multiview_features;
1895 }
1896
Nathaniel Cesariob3f2d702020-11-09 09:20:49 -07001897 const auto *portability_features = lvl_find_in_chain<VkPhysicalDevicePortabilitySubsetFeaturesKHR>(pCreateInfo->pNext);
1898 if (portability_features) {
1899 state_tracker->enabled_features.portability_subset_features = *portability_features;
1900 }
1901
locke-lunargd556cc32019-09-17 01:21:23 -06001902 // Store physical device properties and physical device mem limits into CoreChecks structs
1903 DispatchGetPhysicalDeviceMemoryProperties(gpu, &state_tracker->phys_dev_mem_props);
1904 DispatchGetPhysicalDeviceProperties(gpu, &state_tracker->phys_dev_props);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001905 GetPhysicalDeviceExtProperties(gpu, state_tracker->device_extensions.vk_feature_version_1_2,
1906 &state_tracker->phys_dev_props_core11);
1907 GetPhysicalDeviceExtProperties(gpu, state_tracker->device_extensions.vk_feature_version_1_2,
1908 &state_tracker->phys_dev_props_core12);
locke-lunargd556cc32019-09-17 01:21:23 -06001909
1910 const auto &dev_ext = state_tracker->device_extensions;
1911 auto *phys_dev_props = &state_tracker->phys_dev_ext_props;
1912
1913 if (dev_ext.vk_khr_push_descriptor) {
1914 // Get the needed push_descriptor limits
1915 VkPhysicalDevicePushDescriptorPropertiesKHR push_descriptor_prop;
1916 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_khr_push_descriptor, &push_descriptor_prop);
1917 phys_dev_props->max_push_descriptors = push_descriptor_prop.maxPushDescriptors;
1918 }
1919
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001920 if (!state_tracker->device_extensions.vk_feature_version_1_2 && dev_ext.vk_ext_descriptor_indexing) {
1921 VkPhysicalDeviceDescriptorIndexingPropertiesEXT descriptor_indexing_prop;
1922 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_ext_descriptor_indexing, &descriptor_indexing_prop);
1923 state_tracker->phys_dev_props_core12.maxUpdateAfterBindDescriptorsInAllPools =
1924 descriptor_indexing_prop.maxUpdateAfterBindDescriptorsInAllPools;
1925 state_tracker->phys_dev_props_core12.shaderUniformBufferArrayNonUniformIndexingNative =
1926 descriptor_indexing_prop.shaderUniformBufferArrayNonUniformIndexingNative;
1927 state_tracker->phys_dev_props_core12.shaderSampledImageArrayNonUniformIndexingNative =
1928 descriptor_indexing_prop.shaderSampledImageArrayNonUniformIndexingNative;
1929 state_tracker->phys_dev_props_core12.shaderStorageBufferArrayNonUniformIndexingNative =
1930 descriptor_indexing_prop.shaderStorageBufferArrayNonUniformIndexingNative;
1931 state_tracker->phys_dev_props_core12.shaderStorageImageArrayNonUniformIndexingNative =
1932 descriptor_indexing_prop.shaderStorageImageArrayNonUniformIndexingNative;
1933 state_tracker->phys_dev_props_core12.shaderInputAttachmentArrayNonUniformIndexingNative =
1934 descriptor_indexing_prop.shaderInputAttachmentArrayNonUniformIndexingNative;
1935 state_tracker->phys_dev_props_core12.robustBufferAccessUpdateAfterBind =
1936 descriptor_indexing_prop.robustBufferAccessUpdateAfterBind;
1937 state_tracker->phys_dev_props_core12.quadDivergentImplicitLod = descriptor_indexing_prop.quadDivergentImplicitLod;
1938 state_tracker->phys_dev_props_core12.maxPerStageDescriptorUpdateAfterBindSamplers =
1939 descriptor_indexing_prop.maxPerStageDescriptorUpdateAfterBindSamplers;
1940 state_tracker->phys_dev_props_core12.maxPerStageDescriptorUpdateAfterBindUniformBuffers =
1941 descriptor_indexing_prop.maxPerStageDescriptorUpdateAfterBindUniformBuffers;
1942 state_tracker->phys_dev_props_core12.maxPerStageDescriptorUpdateAfterBindStorageBuffers =
1943 descriptor_indexing_prop.maxPerStageDescriptorUpdateAfterBindStorageBuffers;
1944 state_tracker->phys_dev_props_core12.maxPerStageDescriptorUpdateAfterBindSampledImages =
1945 descriptor_indexing_prop.maxPerStageDescriptorUpdateAfterBindSampledImages;
1946 state_tracker->phys_dev_props_core12.maxPerStageDescriptorUpdateAfterBindStorageImages =
1947 descriptor_indexing_prop.maxPerStageDescriptorUpdateAfterBindStorageImages;
1948 state_tracker->phys_dev_props_core12.maxPerStageDescriptorUpdateAfterBindInputAttachments =
1949 descriptor_indexing_prop.maxPerStageDescriptorUpdateAfterBindInputAttachments;
1950 state_tracker->phys_dev_props_core12.maxPerStageUpdateAfterBindResources =
1951 descriptor_indexing_prop.maxPerStageUpdateAfterBindResources;
1952 state_tracker->phys_dev_props_core12.maxDescriptorSetUpdateAfterBindSamplers =
1953 descriptor_indexing_prop.maxDescriptorSetUpdateAfterBindSamplers;
1954 state_tracker->phys_dev_props_core12.maxDescriptorSetUpdateAfterBindUniformBuffers =
1955 descriptor_indexing_prop.maxDescriptorSetUpdateAfterBindUniformBuffers;
1956 state_tracker->phys_dev_props_core12.maxDescriptorSetUpdateAfterBindUniformBuffersDynamic =
1957 descriptor_indexing_prop.maxDescriptorSetUpdateAfterBindUniformBuffersDynamic;
1958 state_tracker->phys_dev_props_core12.maxDescriptorSetUpdateAfterBindStorageBuffers =
1959 descriptor_indexing_prop.maxDescriptorSetUpdateAfterBindStorageBuffers;
1960 state_tracker->phys_dev_props_core12.maxDescriptorSetUpdateAfterBindStorageBuffersDynamic =
1961 descriptor_indexing_prop.maxDescriptorSetUpdateAfterBindStorageBuffersDynamic;
1962 state_tracker->phys_dev_props_core12.maxDescriptorSetUpdateAfterBindSampledImages =
1963 descriptor_indexing_prop.maxDescriptorSetUpdateAfterBindSampledImages;
1964 state_tracker->phys_dev_props_core12.maxDescriptorSetUpdateAfterBindStorageImages =
1965 descriptor_indexing_prop.maxDescriptorSetUpdateAfterBindStorageImages;
1966 state_tracker->phys_dev_props_core12.maxDescriptorSetUpdateAfterBindInputAttachments =
1967 descriptor_indexing_prop.maxDescriptorSetUpdateAfterBindInputAttachments;
1968 }
1969
locke-lunargd556cc32019-09-17 01:21:23 -06001970 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_nv_shading_rate_image, &phys_dev_props->shading_rate_image_props);
1971 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_nv_mesh_shader, &phys_dev_props->mesh_shader_props);
1972 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_ext_inline_uniform_block, &phys_dev_props->inline_uniform_block_props);
1973 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_ext_vertex_attribute_divisor, &phys_dev_props->vtx_attrib_divisor_props);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001974
1975 if (!state_tracker->device_extensions.vk_feature_version_1_2 && dev_ext.vk_khr_depth_stencil_resolve) {
1976 VkPhysicalDeviceDepthStencilResolvePropertiesKHR depth_stencil_resolve_props;
1977 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_khr_depth_stencil_resolve, &depth_stencil_resolve_props);
1978 state_tracker->phys_dev_props_core12.supportedDepthResolveModes = depth_stencil_resolve_props.supportedDepthResolveModes;
1979 state_tracker->phys_dev_props_core12.supportedStencilResolveModes =
1980 depth_stencil_resolve_props.supportedStencilResolveModes;
1981 state_tracker->phys_dev_props_core12.independentResolveNone = depth_stencil_resolve_props.independentResolveNone;
1982 state_tracker->phys_dev_props_core12.independentResolve = depth_stencil_resolve_props.independentResolve;
1983 }
1984
locke-lunargd556cc32019-09-17 01:21:23 -06001985 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_ext_transform_feedback, &phys_dev_props->transform_feedback_props);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05001986 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_nv_ray_tracing, &phys_dev_props->ray_tracing_propsNV);
sourav parmarcd5fb182020-07-17 12:58:44 -07001987 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_khr_ray_tracing_pipeline, &phys_dev_props->ray_tracing_propsKHR);
1988 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_khr_acceleration_structure, &phys_dev_props->acc_structure_props);
locke-lunargd556cc32019-09-17 01:21:23 -06001989 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_ext_texel_buffer_alignment, &phys_dev_props->texel_buffer_alignment_props);
1990 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_ext_fragment_density_map, &phys_dev_props->fragment_density_map_props);
janharaldfredriksen-arm36e17572020-07-07 13:59:28 +02001991 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_ext_fragment_density_map_2, &phys_dev_props->fragment_density_map2_props);
Lionel Landwerlinc7420912019-05-23 00:33:42 +01001992 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_khr_performance_query, &phys_dev_props->performance_query_props);
sfricke-samsung8f658d42020-05-03 20:12:24 -07001993 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_ext_sample_locations, &phys_dev_props->sample_locations_props);
Tony-LunarG7337b312020-04-15 16:40:25 -06001994 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_ext_custom_border_color, &phys_dev_props->custom_border_color_props);
locke-lunarg3fa463a2020-10-23 16:39:04 -06001995 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_khr_multiview, &phys_dev_props->multiview_props);
Nathaniel Cesario3291c912020-11-17 16:54:41 -07001996 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_khr_portability_subset, &phys_dev_props->portability_props);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001997
1998 if (!state_tracker->device_extensions.vk_feature_version_1_2 && dev_ext.vk_khr_timeline_semaphore) {
1999 VkPhysicalDeviceTimelineSemaphorePropertiesKHR timeline_semaphore_props;
2000 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_khr_timeline_semaphore, &timeline_semaphore_props);
2001 state_tracker->phys_dev_props_core12.maxTimelineSemaphoreValueDifference =
2002 timeline_semaphore_props.maxTimelineSemaphoreValueDifference;
2003 }
2004
2005 if (!state_tracker->device_extensions.vk_feature_version_1_2 && dev_ext.vk_khr_shader_float_controls) {
2006 VkPhysicalDeviceFloatControlsPropertiesKHR float_controls_props;
2007 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_khr_shader_float_controls, &float_controls_props);
2008 state_tracker->phys_dev_props_core12.denormBehaviorIndependence = float_controls_props.denormBehaviorIndependence;
2009 state_tracker->phys_dev_props_core12.roundingModeIndependence = float_controls_props.roundingModeIndependence;
2010 state_tracker->phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16 =
2011 float_controls_props.shaderSignedZeroInfNanPreserveFloat16;
2012 state_tracker->phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32 =
2013 float_controls_props.shaderSignedZeroInfNanPreserveFloat32;
2014 state_tracker->phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64 =
2015 float_controls_props.shaderSignedZeroInfNanPreserveFloat64;
2016 state_tracker->phys_dev_props_core12.shaderDenormPreserveFloat16 = float_controls_props.shaderDenormPreserveFloat16;
2017 state_tracker->phys_dev_props_core12.shaderDenormPreserveFloat32 = float_controls_props.shaderDenormPreserveFloat32;
2018 state_tracker->phys_dev_props_core12.shaderDenormPreserveFloat64 = float_controls_props.shaderDenormPreserveFloat64;
2019 state_tracker->phys_dev_props_core12.shaderDenormFlushToZeroFloat16 = float_controls_props.shaderDenormFlushToZeroFloat16;
2020 state_tracker->phys_dev_props_core12.shaderDenormFlushToZeroFloat32 = float_controls_props.shaderDenormFlushToZeroFloat32;
2021 state_tracker->phys_dev_props_core12.shaderDenormFlushToZeroFloat64 = float_controls_props.shaderDenormFlushToZeroFloat64;
2022 state_tracker->phys_dev_props_core12.shaderRoundingModeRTEFloat16 = float_controls_props.shaderRoundingModeRTEFloat16;
2023 state_tracker->phys_dev_props_core12.shaderRoundingModeRTEFloat32 = float_controls_props.shaderRoundingModeRTEFloat32;
2024 state_tracker->phys_dev_props_core12.shaderRoundingModeRTEFloat64 = float_controls_props.shaderRoundingModeRTEFloat64;
2025 state_tracker->phys_dev_props_core12.shaderRoundingModeRTZFloat16 = float_controls_props.shaderRoundingModeRTZFloat16;
2026 state_tracker->phys_dev_props_core12.shaderRoundingModeRTZFloat32 = float_controls_props.shaderRoundingModeRTZFloat32;
2027 state_tracker->phys_dev_props_core12.shaderRoundingModeRTZFloat64 = float_controls_props.shaderRoundingModeRTZFloat64;
2028 }
Mark Lobodzinskie4a2b7f2019-12-20 12:51:30 -07002029
locke-lunargd556cc32019-09-17 01:21:23 -06002030 if (state_tracker->device_extensions.vk_nv_cooperative_matrix) {
2031 // Get the needed cooperative_matrix properties
2032 auto cooperative_matrix_props = lvl_init_struct<VkPhysicalDeviceCooperativeMatrixPropertiesNV>();
2033 auto prop2 = lvl_init_struct<VkPhysicalDeviceProperties2KHR>(&cooperative_matrix_props);
2034 instance_dispatch_table.GetPhysicalDeviceProperties2KHR(gpu, &prop2);
2035 state_tracker->phys_dev_ext_props.cooperative_matrix_props = cooperative_matrix_props;
2036
2037 uint32_t numCooperativeMatrixProperties = 0;
2038 instance_dispatch_table.GetPhysicalDeviceCooperativeMatrixPropertiesNV(gpu, &numCooperativeMatrixProperties, NULL);
2039 state_tracker->cooperative_matrix_properties.resize(numCooperativeMatrixProperties,
2040 lvl_init_struct<VkCooperativeMatrixPropertiesNV>());
2041
2042 instance_dispatch_table.GetPhysicalDeviceCooperativeMatrixPropertiesNV(gpu, &numCooperativeMatrixProperties,
2043 state_tracker->cooperative_matrix_properties.data());
2044 }
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002045 if (!state_tracker->device_extensions.vk_feature_version_1_2 && state_tracker->api_version >= VK_API_VERSION_1_1) {
locke-lunargd556cc32019-09-17 01:21:23 -06002046 // Get the needed subgroup limits
2047 auto subgroup_prop = lvl_init_struct<VkPhysicalDeviceSubgroupProperties>();
2048 auto prop2 = lvl_init_struct<VkPhysicalDeviceProperties2KHR>(&subgroup_prop);
2049 instance_dispatch_table.GetPhysicalDeviceProperties2(gpu, &prop2);
2050
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002051 state_tracker->phys_dev_props_core11.subgroupSize = subgroup_prop.subgroupSize;
2052 state_tracker->phys_dev_props_core11.subgroupSupportedStages = subgroup_prop.supportedStages;
2053 state_tracker->phys_dev_props_core11.subgroupSupportedOperations = subgroup_prop.supportedOperations;
2054 state_tracker->phys_dev_props_core11.subgroupQuadOperationsInAllStages = subgroup_prop.quadOperationsInAllStages;
locke-lunargd556cc32019-09-17 01:21:23 -06002055 }
2056
Tobias Hector6663c9b2020-11-05 10:18:02 +00002057 GetPhysicalDeviceExtProperties(gpu, dev_ext.vk_khr_fragment_shading_rate, &phys_dev_props->fragment_shading_rate_props);
2058
locke-lunargd556cc32019-09-17 01:21:23 -06002059 // Store queue family data
2060 if (pCreateInfo->pQueueCreateInfos != nullptr) {
2061 for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; ++i) {
sfricke-samsung590ae1e2020-04-25 01:18:05 -07002062 const VkDeviceQueueCreateInfo &queue_create_info = pCreateInfo->pQueueCreateInfos[i];
locke-lunargd556cc32019-09-17 01:21:23 -06002063 state_tracker->queue_family_index_map.insert(
sfricke-samsung590ae1e2020-04-25 01:18:05 -07002064 std::make_pair(queue_create_info.queueFamilyIndex, queue_create_info.queueCount));
2065 state_tracker->queue_family_create_flags_map.insert(
2066 std::make_pair(queue_create_info.queueFamilyIndex, queue_create_info.flags));
locke-lunargd556cc32019-09-17 01:21:23 -06002067 }
2068 }
2069}
2070
2071void ValidationStateTracker::PreCallRecordDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator) {
2072 if (!device) return;
2073
locke-lunargd556cc32019-09-17 01:21:23 -06002074 // Reset all command buffers before destroying them, to unlink object_bindings.
2075 for (auto &commandBuffer : commandBufferMap) {
2076 ResetCommandBufferState(commandBuffer.first);
2077 }
Jeff Bolzadbfa852019-10-04 13:53:30 -05002078 pipelineMap.clear();
2079 renderPassMap.clear();
locke-lunargd556cc32019-09-17 01:21:23 -06002080 commandBufferMap.clear();
2081
2082 // This will also delete all sets in the pool & remove them from setMap
2083 DeleteDescriptorSetPools();
2084 // All sets should be removed
2085 assert(setMap.empty());
2086 descriptorSetLayoutMap.clear();
2087 imageViewMap.clear();
2088 imageMap.clear();
2089 bufferViewMap.clear();
2090 bufferMap.clear();
2091 // Queues persist until device is destroyed
2092 queueMap.clear();
2093}
2094
2095// Loop through bound objects and increment their in_use counts.
2096void ValidationStateTracker::IncrementBoundObjects(CMD_BUFFER_STATE const *cb_node) {
2097 for (auto obj : cb_node->object_bindings) {
2098 auto base_obj = GetStateStructPtrFromObject(obj);
2099 if (base_obj) {
2100 base_obj->in_use.fetch_add(1);
2101 }
2102 }
2103}
2104
2105// Track which resources are in-flight by atomically incrementing their "in_use" count
2106void ValidationStateTracker::IncrementResources(CMD_BUFFER_STATE *cb_node) {
2107 cb_node->submitCount++;
2108 cb_node->in_use.fetch_add(1);
2109
2110 // First Increment for all "generic" objects bound to cmd buffer, followed by special-case objects below
2111 IncrementBoundObjects(cb_node);
2112 // TODO : We should be able to remove the NULL look-up checks from the code below as long as
2113 // all the corresponding cases are verified to cause CB_INVALID state and the CB_INVALID state
2114 // should then be flagged prior to calling this function
2115 for (auto event : cb_node->writeEventsBeforeWait) {
2116 auto event_state = GetEventState(event);
2117 if (event_state) event_state->write_in_use++;
2118 }
2119}
2120
2121// Decrement in-use count for objects bound to command buffer
2122void ValidationStateTracker::DecrementBoundResources(CMD_BUFFER_STATE const *cb_node) {
2123 BASE_NODE *base_obj = nullptr;
2124 for (auto obj : cb_node->object_bindings) {
2125 base_obj = GetStateStructPtrFromObject(obj);
2126 if (base_obj) {
2127 base_obj->in_use.fetch_sub(1);
2128 }
2129 }
2130}
2131
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002132void ValidationStateTracker::RetireWorkOnQueue(QUEUE_STATE *pQueue, uint64_t seq) {
locke-lunargd556cc32019-09-17 01:21:23 -06002133 std::unordered_map<VkQueue, uint64_t> otherQueueSeqs;
Marshall Drew-Brook03847582020-11-06 15:10:45 -08002134 std::unordered_map<VkSemaphore, uint64_t> timelineSemaphoreCounters;
locke-lunargd556cc32019-09-17 01:21:23 -06002135
2136 // Roll this queue forward, one submission at a time.
2137 while (pQueue->seq < seq) {
2138 auto &submission = pQueue->submissions.front();
2139
2140 for (auto &wait : submission.waitSemaphores) {
2141 auto pSemaphore = GetSemaphoreState(wait.semaphore);
2142 if (pSemaphore) {
2143 pSemaphore->in_use.fetch_sub(1);
2144 }
Marshall Drew-Brook03847582020-11-06 15:10:45 -08002145 if (wait.type == VK_SEMAPHORE_TYPE_TIMELINE_KHR) {
2146 auto &lastCounter = timelineSemaphoreCounters[wait.semaphore];
2147 lastCounter = std::max(lastCounter, wait.payload);
2148 } else {
2149 auto &lastSeq = otherQueueSeqs[wait.queue];
2150 lastSeq = std::max(lastSeq, wait.seq);
2151 }
locke-lunargd556cc32019-09-17 01:21:23 -06002152 }
2153
Juan A. Suarez Romero9cef8852020-03-10 12:19:42 +01002154 for (auto &signal : submission.signalSemaphores) {
2155 auto pSemaphore = GetSemaphoreState(signal.semaphore);
locke-lunargd556cc32019-09-17 01:21:23 -06002156 if (pSemaphore) {
2157 pSemaphore->in_use.fetch_sub(1);
Juan A. Suarez Romero9cef8852020-03-10 12:19:42 +01002158 if (pSemaphore->type == VK_SEMAPHORE_TYPE_TIMELINE_KHR && pSemaphore->payload < signal.payload) {
2159 pSemaphore->payload = signal.payload;
2160 }
locke-lunargd556cc32019-09-17 01:21:23 -06002161 }
2162 }
2163
2164 for (auto &semaphore : submission.externalSemaphores) {
2165 auto pSemaphore = GetSemaphoreState(semaphore);
2166 if (pSemaphore) {
2167 pSemaphore->in_use.fetch_sub(1);
2168 }
2169 }
2170
2171 for (auto cb : submission.cbs) {
2172 auto cb_node = GetCBState(cb);
2173 if (!cb_node) {
2174 continue;
2175 }
2176 // First perform decrement on general case bound objects
2177 DecrementBoundResources(cb_node);
2178 for (auto event : cb_node->writeEventsBeforeWait) {
2179 auto eventNode = eventMap.find(event);
2180 if (eventNode != eventMap.end()) {
2181 eventNode->second.write_in_use--;
2182 }
2183 }
Jeff Bolz310775c2019-10-09 00:46:33 -05002184 QueryMap localQueryToStateMap;
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02002185 VkQueryPool first_pool = VK_NULL_HANDLE;
Jeff Bolz310775c2019-10-09 00:46:33 -05002186 for (auto &function : cb_node->queryUpdates) {
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02002187 function(nullptr, /*do_validate*/ false, first_pool, submission.perf_submit_pass, &localQueryToStateMap);
Jeff Bolz310775c2019-10-09 00:46:33 -05002188 }
2189
2190 for (auto queryStatePair : localQueryToStateMap) {
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002191 if (queryStatePair.second == QUERYSTATE_ENDED) {
2192 queryToStateMap[queryStatePair.first] = QUERYSTATE_AVAILABLE;
2193 }
locke-lunargd556cc32019-09-17 01:21:23 -06002194 }
locke-lunargd556cc32019-09-17 01:21:23 -06002195 cb_node->in_use.fetch_sub(1);
2196 }
2197
2198 auto pFence = GetFenceState(submission.fence);
2199 if (pFence && pFence->scope == kSyncScopeInternal) {
2200 pFence->state = FENCE_RETIRED;
2201 }
2202
2203 pQueue->submissions.pop_front();
2204 pQueue->seq++;
2205 }
2206
2207 // Roll other queues forward to the highest seq we saw a wait for
2208 for (auto qs : otherQueueSeqs) {
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002209 RetireWorkOnQueue(GetQueueState(qs.first), qs.second);
locke-lunargd556cc32019-09-17 01:21:23 -06002210 }
Marshall Drew-Brook03847582020-11-06 15:10:45 -08002211 for (auto sc : timelineSemaphoreCounters) {
2212 RetireTimelineSemaphore(sc.first, sc.second);
2213 }
locke-lunargd556cc32019-09-17 01:21:23 -06002214}
2215
2216// Submit a fence to a queue, delimiting previous fences and previous untracked
2217// work by it.
2218static void SubmitFence(QUEUE_STATE *pQueue, FENCE_STATE *pFence, uint64_t submitCount) {
2219 pFence->state = FENCE_INFLIGHT;
2220 pFence->signaler.first = pQueue->queue;
2221 pFence->signaler.second = pQueue->seq + pQueue->submissions.size() + submitCount;
2222}
2223
2224void ValidationStateTracker::PostCallRecordQueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits,
2225 VkFence fence, VkResult result) {
Mark Lobodzinski09379db2020-05-07 08:22:01 -06002226 if (result != VK_SUCCESS) return;
locke-lunargd556cc32019-09-17 01:21:23 -06002227 uint64_t early_retire_seq = 0;
2228 auto pQueue = GetQueueState(queue);
2229 auto pFence = GetFenceState(fence);
2230
2231 if (pFence) {
2232 if (pFence->scope == kSyncScopeInternal) {
2233 // Mark fence in use
2234 SubmitFence(pQueue, pFence, std::max(1u, submitCount));
2235 if (!submitCount) {
2236 // If no submissions, but just dropping a fence on the end of the queue,
2237 // record an empty submission with just the fence, so we can determine
2238 // its completion.
2239 pQueue->submissions.emplace_back(std::vector<VkCommandBuffer>(), std::vector<SEMAPHORE_WAIT>(),
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002240 std::vector<SEMAPHORE_SIGNAL>(), std::vector<VkSemaphore>(), fence, 0);
locke-lunargd556cc32019-09-17 01:21:23 -06002241 }
2242 } else {
2243 // Retire work up until this fence early, we will not see the wait that corresponds to this signal
2244 early_retire_seq = pQueue->seq + pQueue->submissions.size();
2245 }
2246 }
2247
2248 // Now process each individual submit
2249 for (uint32_t submit_idx = 0; submit_idx < submitCount; submit_idx++) {
2250 std::vector<VkCommandBuffer> cbs;
2251 const VkSubmitInfo *submit = &pSubmits[submit_idx];
Jeremy Gebben404e6832020-09-29 14:58:07 -06002252 std::vector<SEMAPHORE_WAIT> semaphore_waits;
2253 std::vector<SEMAPHORE_SIGNAL> semaphore_signals;
2254 std::vector<VkSemaphore> semaphore_externals;
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002255 const uint64_t next_seq = pQueue->seq + pQueue->submissions.size() + 1;
Juan A. Suarez Romerof3024162019-10-31 17:57:50 +00002256 auto *timeline_semaphore_submit = lvl_find_in_chain<VkTimelineSemaphoreSubmitInfoKHR>(submit->pNext);
locke-lunargd556cc32019-09-17 01:21:23 -06002257 for (uint32_t i = 0; i < submit->waitSemaphoreCount; ++i) {
2258 VkSemaphore semaphore = submit->pWaitSemaphores[i];
2259 auto pSemaphore = GetSemaphoreState(semaphore);
2260 if (pSemaphore) {
2261 if (pSemaphore->scope == kSyncScopeInternal) {
Juan A. Suarez Romero9cef8852020-03-10 12:19:42 +01002262 SEMAPHORE_WAIT wait;
2263 wait.semaphore = semaphore;
Marshall Drew-Brook03847582020-11-06 15:10:45 -08002264 wait.type = pSemaphore->type;
Juan A. Suarez Romero9cef8852020-03-10 12:19:42 +01002265 if (pSemaphore->type == VK_SEMAPHORE_TYPE_BINARY_KHR) {
2266 if (pSemaphore->signaler.first != VK_NULL_HANDLE) {
2267 wait.queue = pSemaphore->signaler.first;
2268 wait.seq = pSemaphore->signaler.second;
2269 semaphore_waits.push_back(wait);
2270 pSemaphore->in_use.fetch_add(1);
2271 }
2272 pSemaphore->signaler.first = VK_NULL_HANDLE;
2273 pSemaphore->signaled = false;
2274 } else if (pSemaphore->payload < timeline_semaphore_submit->pWaitSemaphoreValues[i]) {
2275 wait.queue = queue;
2276 wait.seq = next_seq;
2277 wait.payload = timeline_semaphore_submit->pWaitSemaphoreValues[i];
2278 semaphore_waits.push_back(wait);
locke-lunargd556cc32019-09-17 01:21:23 -06002279 pSemaphore->in_use.fetch_add(1);
2280 }
locke-lunargd556cc32019-09-17 01:21:23 -06002281 } else {
2282 semaphore_externals.push_back(semaphore);
2283 pSemaphore->in_use.fetch_add(1);
2284 if (pSemaphore->scope == kSyncScopeExternalTemporary) {
2285 pSemaphore->scope = kSyncScopeInternal;
2286 }
2287 }
2288 }
2289 }
2290 for (uint32_t i = 0; i < submit->signalSemaphoreCount; ++i) {
2291 VkSemaphore semaphore = submit->pSignalSemaphores[i];
2292 auto pSemaphore = GetSemaphoreState(semaphore);
2293 if (pSemaphore) {
2294 if (pSemaphore->scope == kSyncScopeInternal) {
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002295 SEMAPHORE_SIGNAL signal;
2296 signal.semaphore = semaphore;
2297 signal.seq = next_seq;
Juan A. Suarez Romerof3024162019-10-31 17:57:50 +00002298 if (pSemaphore->type == VK_SEMAPHORE_TYPE_BINARY_KHR) {
2299 pSemaphore->signaler.first = queue;
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002300 pSemaphore->signaler.second = next_seq;
Juan A. Suarez Romerof3024162019-10-31 17:57:50 +00002301 pSemaphore->signaled = true;
2302 } else {
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002303 signal.payload = timeline_semaphore_submit->pSignalSemaphoreValues[i];
Juan A. Suarez Romerof3024162019-10-31 17:57:50 +00002304 }
locke-lunargd556cc32019-09-17 01:21:23 -06002305 pSemaphore->in_use.fetch_add(1);
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002306 semaphore_signals.push_back(signal);
locke-lunargd556cc32019-09-17 01:21:23 -06002307 } else {
2308 // Retire work up until this submit early, we will not see the wait that corresponds to this signal
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002309 early_retire_seq = std::max(early_retire_seq, next_seq);
locke-lunargd556cc32019-09-17 01:21:23 -06002310 }
2311 }
2312 }
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02002313 const auto perf_submit = lvl_find_in_chain<VkPerformanceQuerySubmitInfoKHR>(submit->pNext);
2314 uint32_t perf_pass = perf_submit ? perf_submit->counterPassIndex : 0;
2315
locke-lunargd556cc32019-09-17 01:21:23 -06002316 for (uint32_t i = 0; i < submit->commandBufferCount; i++) {
2317 auto cb_node = GetCBState(submit->pCommandBuffers[i]);
2318 if (cb_node) {
2319 cbs.push_back(submit->pCommandBuffers[i]);
2320 for (auto secondaryCmdBuffer : cb_node->linkedCommandBuffers) {
2321 cbs.push_back(secondaryCmdBuffer->commandBuffer);
2322 IncrementResources(secondaryCmdBuffer);
2323 }
2324 IncrementResources(cb_node);
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002325
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02002326 VkQueryPool first_pool = VK_NULL_HANDLE;
2327 EventToStageMap localEventToStageMap;
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002328 QueryMap localQueryToStateMap;
2329 for (auto &function : cb_node->queryUpdates) {
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02002330 function(nullptr, /*do_validate*/ false, first_pool, perf_pass, &localQueryToStateMap);
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002331 }
2332
2333 for (auto queryStatePair : localQueryToStateMap) {
2334 queryToStateMap[queryStatePair.first] = queryStatePair.second;
2335 }
2336
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002337 for (auto &function : cb_node->eventUpdates) {
2338 function(nullptr, /*do_validate*/ false, &localEventToStageMap);
2339 }
2340
2341 for (auto eventStagePair : localEventToStageMap) {
2342 eventMap[eventStagePair.first].stageMask = eventStagePair.second;
2343 }
locke-lunargd556cc32019-09-17 01:21:23 -06002344 }
2345 }
Lionel Landwerlinc7420912019-05-23 00:33:42 +01002346
locke-lunargd556cc32019-09-17 01:21:23 -06002347 pQueue->submissions.emplace_back(cbs, semaphore_waits, semaphore_signals, semaphore_externals,
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02002348 submit_idx == submitCount - 1 ? fence : (VkFence)VK_NULL_HANDLE, perf_pass);
locke-lunargd556cc32019-09-17 01:21:23 -06002349 }
2350
2351 if (early_retire_seq) {
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002352 RetireWorkOnQueue(pQueue, early_retire_seq);
locke-lunargd556cc32019-09-17 01:21:23 -06002353 }
2354}
2355
2356void ValidationStateTracker::PostCallRecordAllocateMemory(VkDevice device, const VkMemoryAllocateInfo *pAllocateInfo,
2357 const VkAllocationCallbacks *pAllocator, VkDeviceMemory *pMemory,
2358 VkResult result) {
2359 if (VK_SUCCESS == result) {
2360 AddMemObjInfo(device, *pMemory, pAllocateInfo);
2361 }
2362 return;
2363}
2364
2365void ValidationStateTracker::PreCallRecordFreeMemory(VkDevice device, VkDeviceMemory mem, const VkAllocationCallbacks *pAllocator) {
2366 if (!mem) return;
2367 DEVICE_MEMORY_STATE *mem_info = GetDevMemState(mem);
2368 const VulkanTypedHandle obj_struct(mem, kVulkanObjectTypeDeviceMemory);
2369
2370 // Clear mem binding for any bound objects
2371 for (const auto &obj : mem_info->obj_bindings) {
2372 BINDABLE *bindable_state = nullptr;
2373 switch (obj.type) {
2374 case kVulkanObjectTypeImage:
2375 bindable_state = GetImageState(obj.Cast<VkImage>());
2376 break;
2377 case kVulkanObjectTypeBuffer:
2378 bindable_state = GetBufferState(obj.Cast<VkBuffer>());
2379 break;
2380 case kVulkanObjectTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -07002381 bindable_state = GetAccelerationStructureStateNV(obj.Cast<VkAccelerationStructureNV>());
locke-lunargd556cc32019-09-17 01:21:23 -06002382 break;
2383
2384 default:
2385 // Should only have acceleration structure, buffer, or image objects bound to memory
2386 assert(0);
2387 }
2388
2389 if (bindable_state) {
Jeff Bolz41e29052020-03-29 22:33:55 -05002390 // Remove any sparse bindings bound to the resource that use this memory.
2391 for (auto it = bindable_state->sparse_bindings.begin(); it != bindable_state->sparse_bindings.end();) {
2392 auto nextit = it;
2393 nextit++;
2394
2395 auto &sparse_mem_binding = *it;
2396 if (sparse_mem_binding.mem_state.get() == mem_info) {
2397 bindable_state->sparse_bindings.erase(it);
2398 }
2399
2400 it = nextit;
2401 }
locke-lunargd556cc32019-09-17 01:21:23 -06002402 bindable_state->UpdateBoundMemorySet();
2403 }
2404 }
2405 // Any bound cmd buffers are now invalid
2406 InvalidateCommandBuffers(mem_info->cb_bindings, obj_struct);
2407 RemoveAliasingImages(mem_info->bound_images);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002408 mem_info->destroyed = true;
John Zulauf79952712020-04-07 11:25:54 -06002409 fake_memory.Free(mem_info->fake_base_address);
locke-lunargd556cc32019-09-17 01:21:23 -06002410 memObjMap.erase(mem);
2411}
2412
2413void ValidationStateTracker::PostCallRecordQueueBindSparse(VkQueue queue, uint32_t bindInfoCount, const VkBindSparseInfo *pBindInfo,
2414 VkFence fence, VkResult result) {
2415 if (result != VK_SUCCESS) return;
2416 uint64_t early_retire_seq = 0;
2417 auto pFence = GetFenceState(fence);
2418 auto pQueue = GetQueueState(queue);
2419
2420 if (pFence) {
2421 if (pFence->scope == kSyncScopeInternal) {
2422 SubmitFence(pQueue, pFence, std::max(1u, bindInfoCount));
2423 if (!bindInfoCount) {
2424 // No work to do, just dropping a fence in the queue by itself.
2425 pQueue->submissions.emplace_back(std::vector<VkCommandBuffer>(), std::vector<SEMAPHORE_WAIT>(),
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002426 std::vector<SEMAPHORE_SIGNAL>(), std::vector<VkSemaphore>(), fence, 0);
locke-lunargd556cc32019-09-17 01:21:23 -06002427 }
2428 } else {
2429 // Retire work up until this fence early, we will not see the wait that corresponds to this signal
2430 early_retire_seq = pQueue->seq + pQueue->submissions.size();
2431 }
2432 }
2433
2434 for (uint32_t bindIdx = 0; bindIdx < bindInfoCount; ++bindIdx) {
2435 const VkBindSparseInfo &bindInfo = pBindInfo[bindIdx];
2436 // Track objects tied to memory
2437 for (uint32_t j = 0; j < bindInfo.bufferBindCount; j++) {
2438 for (uint32_t k = 0; k < bindInfo.pBufferBinds[j].bindCount; k++) {
2439 auto sparse_binding = bindInfo.pBufferBinds[j].pBinds[k];
locke-lunargcf04d582019-11-26 00:31:50 -07002440 SetSparseMemBinding(sparse_binding.memory, sparse_binding.memoryOffset, sparse_binding.size,
locke-lunargd556cc32019-09-17 01:21:23 -06002441 VulkanTypedHandle(bindInfo.pBufferBinds[j].buffer, kVulkanObjectTypeBuffer));
2442 }
2443 }
2444 for (uint32_t j = 0; j < bindInfo.imageOpaqueBindCount; j++) {
2445 for (uint32_t k = 0; k < bindInfo.pImageOpaqueBinds[j].bindCount; k++) {
2446 auto sparse_binding = bindInfo.pImageOpaqueBinds[j].pBinds[k];
locke-lunargcf04d582019-11-26 00:31:50 -07002447 SetSparseMemBinding(sparse_binding.memory, sparse_binding.memoryOffset, sparse_binding.size,
locke-lunargd556cc32019-09-17 01:21:23 -06002448 VulkanTypedHandle(bindInfo.pImageOpaqueBinds[j].image, kVulkanObjectTypeImage));
2449 }
2450 }
2451 for (uint32_t j = 0; j < bindInfo.imageBindCount; j++) {
2452 for (uint32_t k = 0; k < bindInfo.pImageBinds[j].bindCount; k++) {
2453 auto sparse_binding = bindInfo.pImageBinds[j].pBinds[k];
2454 // TODO: This size is broken for non-opaque bindings, need to update to comprehend full sparse binding data
2455 VkDeviceSize size = sparse_binding.extent.depth * sparse_binding.extent.height * sparse_binding.extent.width * 4;
locke-lunargcf04d582019-11-26 00:31:50 -07002456 SetSparseMemBinding(sparse_binding.memory, sparse_binding.memoryOffset, size,
locke-lunargd556cc32019-09-17 01:21:23 -06002457 VulkanTypedHandle(bindInfo.pImageBinds[j].image, kVulkanObjectTypeImage));
2458 }
2459 }
2460
2461 std::vector<SEMAPHORE_WAIT> semaphore_waits;
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002462 std::vector<SEMAPHORE_SIGNAL> semaphore_signals;
locke-lunargd556cc32019-09-17 01:21:23 -06002463 std::vector<VkSemaphore> semaphore_externals;
2464 for (uint32_t i = 0; i < bindInfo.waitSemaphoreCount; ++i) {
2465 VkSemaphore semaphore = bindInfo.pWaitSemaphores[i];
2466 auto pSemaphore = GetSemaphoreState(semaphore);
2467 if (pSemaphore) {
2468 if (pSemaphore->scope == kSyncScopeInternal) {
2469 if (pSemaphore->signaler.first != VK_NULL_HANDLE) {
Marshall Drew-Brook03847582020-11-06 15:10:45 -08002470 semaphore_waits.push_back(
2471 {semaphore, pSemaphore->type, pSemaphore->signaler.first, pSemaphore->signaler.second});
locke-lunargd556cc32019-09-17 01:21:23 -06002472 pSemaphore->in_use.fetch_add(1);
2473 }
2474 pSemaphore->signaler.first = VK_NULL_HANDLE;
2475 pSemaphore->signaled = false;
2476 } else {
2477 semaphore_externals.push_back(semaphore);
2478 pSemaphore->in_use.fetch_add(1);
2479 if (pSemaphore->scope == kSyncScopeExternalTemporary) {
2480 pSemaphore->scope = kSyncScopeInternal;
2481 }
2482 }
2483 }
2484 }
2485 for (uint32_t i = 0; i < bindInfo.signalSemaphoreCount; ++i) {
2486 VkSemaphore semaphore = bindInfo.pSignalSemaphores[i];
2487 auto pSemaphore = GetSemaphoreState(semaphore);
2488 if (pSemaphore) {
2489 if (pSemaphore->scope == kSyncScopeInternal) {
2490 pSemaphore->signaler.first = queue;
2491 pSemaphore->signaler.second = pQueue->seq + pQueue->submissions.size() + 1;
2492 pSemaphore->signaled = true;
2493 pSemaphore->in_use.fetch_add(1);
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002494
2495 SEMAPHORE_SIGNAL signal;
2496 signal.semaphore = semaphore;
2497 signal.seq = pSemaphore->signaler.second;
2498 semaphore_signals.push_back(signal);
locke-lunargd556cc32019-09-17 01:21:23 -06002499 } else {
2500 // Retire work up until this submit early, we will not see the wait that corresponds to this signal
2501 early_retire_seq = std::max(early_retire_seq, pQueue->seq + pQueue->submissions.size() + 1);
2502 }
2503 }
2504 }
2505
2506 pQueue->submissions.emplace_back(std::vector<VkCommandBuffer>(), semaphore_waits, semaphore_signals, semaphore_externals,
Lionel Landwerlinc7420912019-05-23 00:33:42 +01002507 bindIdx == bindInfoCount - 1 ? fence : (VkFence)VK_NULL_HANDLE, 0);
locke-lunargd556cc32019-09-17 01:21:23 -06002508 }
2509
2510 if (early_retire_seq) {
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002511 RetireWorkOnQueue(pQueue, early_retire_seq);
locke-lunargd556cc32019-09-17 01:21:23 -06002512 }
2513}
2514
2515void ValidationStateTracker::PostCallRecordCreateSemaphore(VkDevice device, const VkSemaphoreCreateInfo *pCreateInfo,
2516 const VkAllocationCallbacks *pAllocator, VkSemaphore *pSemaphore,
2517 VkResult result) {
2518 if (VK_SUCCESS != result) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002519 auto semaphore_state = std::make_shared<SEMAPHORE_STATE>();
locke-lunargd556cc32019-09-17 01:21:23 -06002520 semaphore_state->signaler.first = VK_NULL_HANDLE;
2521 semaphore_state->signaler.second = 0;
2522 semaphore_state->signaled = false;
2523 semaphore_state->scope = kSyncScopeInternal;
Juan A. Suarez Romerof3024162019-10-31 17:57:50 +00002524 semaphore_state->type = VK_SEMAPHORE_TYPE_BINARY_KHR;
2525 semaphore_state->payload = 0;
2526 auto semaphore_type_create_info = lvl_find_in_chain<VkSemaphoreTypeCreateInfoKHR>(pCreateInfo->pNext);
2527 if (semaphore_type_create_info) {
2528 semaphore_state->type = semaphore_type_create_info->semaphoreType;
2529 semaphore_state->payload = semaphore_type_create_info->initialValue;
2530 }
locke-lunargd556cc32019-09-17 01:21:23 -06002531 semaphoreMap[*pSemaphore] = std::move(semaphore_state);
2532}
2533
2534void ValidationStateTracker::RecordImportSemaphoreState(VkSemaphore semaphore, VkExternalSemaphoreHandleTypeFlagBitsKHR handle_type,
2535 VkSemaphoreImportFlagsKHR flags) {
2536 SEMAPHORE_STATE *sema_node = GetSemaphoreState(semaphore);
2537 if (sema_node && sema_node->scope != kSyncScopeExternalPermanent) {
2538 if ((handle_type == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR || flags & VK_SEMAPHORE_IMPORT_TEMPORARY_BIT_KHR) &&
2539 sema_node->scope == kSyncScopeInternal) {
2540 sema_node->scope = kSyncScopeExternalTemporary;
2541 } else {
2542 sema_node->scope = kSyncScopeExternalPermanent;
2543 }
2544 }
2545}
2546
Juan A. Suarez Romerof3024162019-10-31 17:57:50 +00002547void ValidationStateTracker::PostCallRecordSignalSemaphoreKHR(VkDevice device, const VkSemaphoreSignalInfoKHR *pSignalInfo,
2548 VkResult result) {
2549 auto *pSemaphore = GetSemaphoreState(pSignalInfo->semaphore);
2550 pSemaphore->payload = pSignalInfo->value;
2551}
2552
locke-lunargd556cc32019-09-17 01:21:23 -06002553void ValidationStateTracker::RecordMappedMemory(VkDeviceMemory mem, VkDeviceSize offset, VkDeviceSize size, void **ppData) {
2554 auto mem_info = GetDevMemState(mem);
2555 if (mem_info) {
2556 mem_info->mapped_range.offset = offset;
2557 mem_info->mapped_range.size = size;
2558 mem_info->p_driver_data = *ppData;
2559 }
2560}
2561
2562void ValidationStateTracker::RetireFence(VkFence fence) {
2563 auto pFence = GetFenceState(fence);
2564 if (pFence && pFence->scope == kSyncScopeInternal) {
2565 if (pFence->signaler.first != VK_NULL_HANDLE) {
2566 // Fence signaller is a queue -- use this as proof that prior operations on that queue have completed.
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002567 RetireWorkOnQueue(GetQueueState(pFence->signaler.first), pFence->signaler.second);
locke-lunargd556cc32019-09-17 01:21:23 -06002568 } else {
2569 // Fence signaller is the WSI. We're not tracking what the WSI op actually /was/ in CV yet, but we need to mark
2570 // the fence as retired.
2571 pFence->state = FENCE_RETIRED;
2572 }
2573 }
2574}
2575
2576void ValidationStateTracker::PostCallRecordWaitForFences(VkDevice device, uint32_t fenceCount, const VkFence *pFences,
2577 VkBool32 waitAll, uint64_t timeout, VkResult result) {
2578 if (VK_SUCCESS != result) return;
2579
2580 // When we know that all fences are complete we can clean/remove their CBs
2581 if ((VK_TRUE == waitAll) || (1 == fenceCount)) {
2582 for (uint32_t i = 0; i < fenceCount; i++) {
2583 RetireFence(pFences[i]);
2584 }
2585 }
2586 // NOTE : Alternate case not handled here is when some fences have completed. In
2587 // this case for app to guarantee which fences completed it will have to call
2588 // vkGetFenceStatus() at which point we'll clean/remove their CBs if complete.
2589}
2590
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002591void ValidationStateTracker::RetireTimelineSemaphore(VkSemaphore semaphore, uint64_t until_payload) {
2592 auto pSemaphore = GetSemaphoreState(semaphore);
2593 if (pSemaphore) {
2594 for (auto &pair : queueMap) {
2595 QUEUE_STATE &queueState = pair.second;
Tony-LunarG47d5e272020-04-07 15:35:55 -06002596 uint64_t max_seq = 0;
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002597 for (const auto &submission : queueState.submissions) {
2598 for (const auto &signalSemaphore : submission.signalSemaphores) {
2599 if (signalSemaphore.semaphore == semaphore && signalSemaphore.payload <= until_payload) {
Tony-LunarG47d5e272020-04-07 15:35:55 -06002600 if (signalSemaphore.seq > max_seq) {
2601 max_seq = signalSemaphore.seq;
2602 }
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002603 }
2604 }
2605 }
Tony-LunarG47d5e272020-04-07 15:35:55 -06002606 if (max_seq) {
2607 RetireWorkOnQueue(&queueState, max_seq);
2608 }
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002609 }
2610 }
2611}
2612
John Zulauff89de662020-04-13 18:57:34 -06002613void ValidationStateTracker::RecordWaitSemaphores(VkDevice device, const VkSemaphoreWaitInfo *pWaitInfo, uint64_t timeout,
2614 VkResult result) {
Jakub Okoński04feb3b2020-02-01 18:31:01 +01002615 if (VK_SUCCESS != result) return;
2616
2617 for (uint32_t i = 0; i < pWaitInfo->semaphoreCount; i++) {
2618 RetireTimelineSemaphore(pWaitInfo->pSemaphores[i], pWaitInfo->pValues[i]);
2619 }
2620}
2621
John Zulauff89de662020-04-13 18:57:34 -06002622void ValidationStateTracker::PostCallRecordWaitSemaphores(VkDevice device, const VkSemaphoreWaitInfo *pWaitInfo, uint64_t timeout,
2623 VkResult result) {
2624 RecordWaitSemaphores(device, pWaitInfo, timeout, result);
2625}
2626
2627void ValidationStateTracker::PostCallRecordWaitSemaphoresKHR(VkDevice device, const VkSemaphoreWaitInfo *pWaitInfo,
2628 uint64_t timeout, VkResult result) {
2629 RecordWaitSemaphores(device, pWaitInfo, timeout, result);
2630}
2631
Adrian Coca Lorentec7d76102020-09-28 13:58:16 +02002632void ValidationStateTracker::RecordGetSemaphoreCounterValue(VkDevice device, VkSemaphore semaphore, uint64_t *pValue,
2633 VkResult result) {
2634 if (VK_SUCCESS != result) return;
2635
2636 RetireTimelineSemaphore(semaphore, *pValue);
2637}
2638
2639void ValidationStateTracker::PostCallRecordGetSemaphoreCounterValue(VkDevice device, VkSemaphore semaphore, uint64_t *pValue,
2640 VkResult result) {
2641 RecordGetSemaphoreCounterValue(device, semaphore, pValue, result);
2642}
2643void ValidationStateTracker::PostCallRecordGetSemaphoreCounterValueKHR(VkDevice device, VkSemaphore semaphore, uint64_t *pValue,
2644 VkResult result) {
2645 RecordGetSemaphoreCounterValue(device, semaphore, pValue, result);
2646}
2647
locke-lunargd556cc32019-09-17 01:21:23 -06002648void ValidationStateTracker::PostCallRecordGetFenceStatus(VkDevice device, VkFence fence, VkResult result) {
2649 if (VK_SUCCESS != result) return;
2650 RetireFence(fence);
2651}
2652
2653void ValidationStateTracker::RecordGetDeviceQueueState(uint32_t queue_family_index, VkQueue queue) {
2654 // Add queue to tracking set only if it is new
2655 auto queue_is_new = queues.emplace(queue);
2656 if (queue_is_new.second == true) {
2657 QUEUE_STATE *queue_state = &queueMap[queue];
2658 queue_state->queue = queue;
2659 queue_state->queueFamilyIndex = queue_family_index;
2660 queue_state->seq = 0;
2661 }
2662}
2663
2664void ValidationStateTracker::PostCallRecordGetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex,
2665 VkQueue *pQueue) {
2666 RecordGetDeviceQueueState(queueFamilyIndex, *pQueue);
2667}
2668
2669void ValidationStateTracker::PostCallRecordGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2 *pQueueInfo, VkQueue *pQueue) {
2670 RecordGetDeviceQueueState(pQueueInfo->queueFamilyIndex, *pQueue);
2671}
2672
2673void ValidationStateTracker::PostCallRecordQueueWaitIdle(VkQueue queue, VkResult result) {
2674 if (VK_SUCCESS != result) return;
2675 QUEUE_STATE *queue_state = GetQueueState(queue);
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002676 RetireWorkOnQueue(queue_state, queue_state->seq + queue_state->submissions.size());
locke-lunargd556cc32019-09-17 01:21:23 -06002677}
2678
2679void ValidationStateTracker::PostCallRecordDeviceWaitIdle(VkDevice device, VkResult result) {
2680 if (VK_SUCCESS != result) return;
2681 for (auto &queue : queueMap) {
Jeff Bolz8041c5b2019-10-20 22:14:20 -05002682 RetireWorkOnQueue(&queue.second, queue.second.seq + queue.second.submissions.size());
locke-lunargd556cc32019-09-17 01:21:23 -06002683 }
2684}
2685
2686void ValidationStateTracker::PreCallRecordDestroyFence(VkDevice device, VkFence fence, const VkAllocationCallbacks *pAllocator) {
2687 if (!fence) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002688 auto fence_state = GetFenceState(fence);
2689 fence_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06002690 fenceMap.erase(fence);
2691}
2692
2693void ValidationStateTracker::PreCallRecordDestroySemaphore(VkDevice device, VkSemaphore semaphore,
2694 const VkAllocationCallbacks *pAllocator) {
2695 if (!semaphore) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002696 auto semaphore_state = GetSemaphoreState(semaphore);
2697 semaphore_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06002698 semaphoreMap.erase(semaphore);
2699}
2700
2701void ValidationStateTracker::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
2702 if (!event) return;
2703 EVENT_STATE *event_state = GetEventState(event);
2704 const VulkanTypedHandle obj_struct(event, kVulkanObjectTypeEvent);
2705 InvalidateCommandBuffers(event_state->cb_bindings, obj_struct);
2706 eventMap.erase(event);
2707}
2708
2709void ValidationStateTracker::PreCallRecordDestroyQueryPool(VkDevice device, VkQueryPool queryPool,
2710 const VkAllocationCallbacks *pAllocator) {
2711 if (!queryPool) return;
2712 QUERY_POOL_STATE *qp_state = GetQueryPoolState(queryPool);
2713 const VulkanTypedHandle obj_struct(queryPool, kVulkanObjectTypeQueryPool);
2714 InvalidateCommandBuffers(qp_state->cb_bindings, obj_struct);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002715 qp_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06002716 queryPoolMap.erase(queryPool);
2717}
2718
2719// Object with given handle is being bound to memory w/ given mem_info struct.
2720// Track the newly bound memory range with given memoryOffset
2721// Also scan any previous ranges, track aliased ranges with new range, and flag an error if a linear
2722// and non-linear range incorrectly overlap.
locke-lunargd556cc32019-09-17 01:21:23 -06002723void ValidationStateTracker::InsertMemoryRange(const VulkanTypedHandle &typed_handle, DEVICE_MEMORY_STATE *mem_info,
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002724 VkDeviceSize memoryOffset) {
locke-lunargd556cc32019-09-17 01:21:23 -06002725 if (typed_handle.type == kVulkanObjectTypeImage) {
2726 mem_info->bound_images.insert(typed_handle.Cast<VkImage>());
2727 } else if (typed_handle.type == kVulkanObjectTypeBuffer) {
locke-lunarg37c410a2020-02-17 17:34:13 -07002728 mem_info->bound_buffers.insert(typed_handle.Cast<VkBuffer>());
locke-lunargd556cc32019-09-17 01:21:23 -06002729 } else if (typed_handle.type == kVulkanObjectTypeAccelerationStructureNV) {
locke-lunarg37c410a2020-02-17 17:34:13 -07002730 mem_info->bound_acceleration_structures.insert(typed_handle.Cast<VkAccelerationStructureNV>());
locke-lunargd556cc32019-09-17 01:21:23 -06002731 } else {
2732 // Unsupported object type
2733 assert(false);
2734 }
2735}
2736
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002737void ValidationStateTracker::InsertImageMemoryRange(VkImage image, DEVICE_MEMORY_STATE *mem_info, VkDeviceSize mem_offset) {
2738 InsertMemoryRange(VulkanTypedHandle(image, kVulkanObjectTypeImage), mem_info, mem_offset);
locke-lunargd556cc32019-09-17 01:21:23 -06002739}
2740
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002741void ValidationStateTracker::InsertBufferMemoryRange(VkBuffer buffer, DEVICE_MEMORY_STATE *mem_info, VkDeviceSize mem_offset) {
2742 InsertMemoryRange(VulkanTypedHandle(buffer, kVulkanObjectTypeBuffer), mem_info, mem_offset);
locke-lunargd556cc32019-09-17 01:21:23 -06002743}
2744
2745void ValidationStateTracker::InsertAccelerationStructureMemoryRange(VkAccelerationStructureNV as, DEVICE_MEMORY_STATE *mem_info,
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002746 VkDeviceSize mem_offset) {
2747 InsertMemoryRange(VulkanTypedHandle(as, kVulkanObjectTypeAccelerationStructureNV), mem_info, mem_offset);
locke-lunargd556cc32019-09-17 01:21:23 -06002748}
2749
2750// This function will remove the handle-to-index mapping from the appropriate map.
2751static void RemoveMemoryRange(const VulkanTypedHandle &typed_handle, DEVICE_MEMORY_STATE *mem_info) {
2752 if (typed_handle.type == kVulkanObjectTypeImage) {
2753 mem_info->bound_images.erase(typed_handle.Cast<VkImage>());
2754 } else if (typed_handle.type == kVulkanObjectTypeBuffer) {
locke-lunarg37c410a2020-02-17 17:34:13 -07002755 mem_info->bound_buffers.erase(typed_handle.Cast<VkBuffer>());
locke-lunargd556cc32019-09-17 01:21:23 -06002756 } else if (typed_handle.type == kVulkanObjectTypeAccelerationStructureNV) {
locke-lunarg37c410a2020-02-17 17:34:13 -07002757 mem_info->bound_acceleration_structures.erase(typed_handle.Cast<VkAccelerationStructureNV>());
locke-lunargd556cc32019-09-17 01:21:23 -06002758 } else {
2759 // Unsupported object type
2760 assert(false);
2761 }
2762}
2763
2764void ValidationStateTracker::RemoveBufferMemoryRange(VkBuffer buffer, DEVICE_MEMORY_STATE *mem_info) {
2765 RemoveMemoryRange(VulkanTypedHandle(buffer, kVulkanObjectTypeBuffer), mem_info);
2766}
2767
2768void ValidationStateTracker::RemoveImageMemoryRange(VkImage image, DEVICE_MEMORY_STATE *mem_info) {
2769 RemoveMemoryRange(VulkanTypedHandle(image, kVulkanObjectTypeImage), mem_info);
2770}
2771
locke-lunargd556cc32019-09-17 01:21:23 -06002772void ValidationStateTracker::UpdateBindBufferMemoryState(VkBuffer buffer, VkDeviceMemory mem, VkDeviceSize memoryOffset) {
2773 BUFFER_STATE *buffer_state = GetBufferState(buffer);
2774 if (buffer_state) {
2775 // Track bound memory range information
2776 auto mem_info = GetDevMemState(mem);
2777 if (mem_info) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002778 InsertBufferMemoryRange(buffer, mem_info, memoryOffset);
locke-lunargd556cc32019-09-17 01:21:23 -06002779 }
2780 // Track objects tied to memory
2781 SetMemBinding(mem, buffer_state, memoryOffset, VulkanTypedHandle(buffer, kVulkanObjectTypeBuffer));
2782 }
2783}
2784
2785void ValidationStateTracker::PostCallRecordBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory mem,
2786 VkDeviceSize memoryOffset, VkResult result) {
2787 if (VK_SUCCESS != result) return;
2788 UpdateBindBufferMemoryState(buffer, mem, memoryOffset);
2789}
2790
2791void ValidationStateTracker::PostCallRecordBindBufferMemory2(VkDevice device, uint32_t bindInfoCount,
2792 const VkBindBufferMemoryInfoKHR *pBindInfos, VkResult result) {
2793 for (uint32_t i = 0; i < bindInfoCount; i++) {
2794 UpdateBindBufferMemoryState(pBindInfos[i].buffer, pBindInfos[i].memory, pBindInfos[i].memoryOffset);
2795 }
2796}
2797
2798void ValidationStateTracker::PostCallRecordBindBufferMemory2KHR(VkDevice device, uint32_t bindInfoCount,
2799 const VkBindBufferMemoryInfoKHR *pBindInfos, VkResult result) {
2800 for (uint32_t i = 0; i < bindInfoCount; i++) {
2801 UpdateBindBufferMemoryState(pBindInfos[i].buffer, pBindInfos[i].memory, pBindInfos[i].memoryOffset);
2802 }
2803}
2804
Spencer Fricke6c127102020-04-16 06:25:20 -07002805void ValidationStateTracker::RecordGetBufferMemoryRequirementsState(VkBuffer buffer) {
locke-lunargd556cc32019-09-17 01:21:23 -06002806 BUFFER_STATE *buffer_state = GetBufferState(buffer);
2807 if (buffer_state) {
locke-lunargd556cc32019-09-17 01:21:23 -06002808 buffer_state->memory_requirements_checked = true;
2809 }
2810}
2811
2812void ValidationStateTracker::PostCallRecordGetBufferMemoryRequirements(VkDevice device, VkBuffer buffer,
2813 VkMemoryRequirements *pMemoryRequirements) {
Spencer Fricke6c127102020-04-16 06:25:20 -07002814 RecordGetBufferMemoryRequirementsState(buffer);
locke-lunargd556cc32019-09-17 01:21:23 -06002815}
2816
2817void ValidationStateTracker::PostCallRecordGetBufferMemoryRequirements2(VkDevice device,
2818 const VkBufferMemoryRequirementsInfo2KHR *pInfo,
2819 VkMemoryRequirements2KHR *pMemoryRequirements) {
Spencer Fricke6c127102020-04-16 06:25:20 -07002820 RecordGetBufferMemoryRequirementsState(pInfo->buffer);
locke-lunargd556cc32019-09-17 01:21:23 -06002821}
2822
2823void ValidationStateTracker::PostCallRecordGetBufferMemoryRequirements2KHR(VkDevice device,
2824 const VkBufferMemoryRequirementsInfo2KHR *pInfo,
2825 VkMemoryRequirements2KHR *pMemoryRequirements) {
Spencer Fricke6c127102020-04-16 06:25:20 -07002826 RecordGetBufferMemoryRequirementsState(pInfo->buffer);
locke-lunargd556cc32019-09-17 01:21:23 -06002827}
2828
Spencer Fricke6c127102020-04-16 06:25:20 -07002829void ValidationStateTracker::RecordGetImageMemoryRequirementsState(VkImage image, const VkImageMemoryRequirementsInfo2 *pInfo) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002830 const VkImagePlaneMemoryRequirementsInfo *plane_info =
2831 (pInfo == nullptr) ? nullptr : lvl_find_in_chain<VkImagePlaneMemoryRequirementsInfo>(pInfo->pNext);
locke-lunargd556cc32019-09-17 01:21:23 -06002832 IMAGE_STATE *image_state = GetImageState(image);
2833 if (image_state) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002834 if (plane_info != nullptr) {
2835 // Multi-plane image
2836 image_state->memory_requirements_checked = false; // Each image plane needs to be checked itself
2837 if (plane_info->planeAspect == VK_IMAGE_ASPECT_PLANE_0_BIT) {
2838 image_state->plane0_memory_requirements_checked = true;
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002839 } else if (plane_info->planeAspect == VK_IMAGE_ASPECT_PLANE_1_BIT) {
2840 image_state->plane1_memory_requirements_checked = true;
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002841 } else if (plane_info->planeAspect == VK_IMAGE_ASPECT_PLANE_2_BIT) {
2842 image_state->plane2_memory_requirements_checked = true;
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002843 }
2844 } else {
2845 // Single Plane image
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07002846 image_state->memory_requirements_checked = true;
2847 }
locke-lunargd556cc32019-09-17 01:21:23 -06002848 }
2849}
2850
2851void ValidationStateTracker::PostCallRecordGetImageMemoryRequirements(VkDevice device, VkImage image,
2852 VkMemoryRequirements *pMemoryRequirements) {
Spencer Fricke6c127102020-04-16 06:25:20 -07002853 RecordGetImageMemoryRequirementsState(image, nullptr);
locke-lunargd556cc32019-09-17 01:21:23 -06002854}
2855
2856void ValidationStateTracker::PostCallRecordGetImageMemoryRequirements2(VkDevice device, const VkImageMemoryRequirementsInfo2 *pInfo,
2857 VkMemoryRequirements2 *pMemoryRequirements) {
Spencer Fricke6c127102020-04-16 06:25:20 -07002858 RecordGetImageMemoryRequirementsState(pInfo->image, pInfo);
locke-lunargd556cc32019-09-17 01:21:23 -06002859}
2860
2861void ValidationStateTracker::PostCallRecordGetImageMemoryRequirements2KHR(VkDevice device,
2862 const VkImageMemoryRequirementsInfo2 *pInfo,
2863 VkMemoryRequirements2 *pMemoryRequirements) {
Spencer Fricke6c127102020-04-16 06:25:20 -07002864 RecordGetImageMemoryRequirementsState(pInfo->image, pInfo);
locke-lunargd556cc32019-09-17 01:21:23 -06002865}
2866
2867static void RecordGetImageSparseMemoryRequirementsState(IMAGE_STATE *image_state,
2868 VkSparseImageMemoryRequirements *sparse_image_memory_requirements) {
2869 image_state->sparse_requirements.emplace_back(*sparse_image_memory_requirements);
2870 if (sparse_image_memory_requirements->formatProperties.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) {
2871 image_state->sparse_metadata_required = true;
2872 }
2873}
2874
2875void ValidationStateTracker::PostCallRecordGetImageSparseMemoryRequirements(
2876 VkDevice device, VkImage image, uint32_t *pSparseMemoryRequirementCount,
2877 VkSparseImageMemoryRequirements *pSparseMemoryRequirements) {
2878 auto image_state = GetImageState(image);
2879 image_state->get_sparse_reqs_called = true;
2880 if (!pSparseMemoryRequirements) return;
2881 for (uint32_t i = 0; i < *pSparseMemoryRequirementCount; i++) {
2882 RecordGetImageSparseMemoryRequirementsState(image_state, &pSparseMemoryRequirements[i]);
2883 }
2884}
2885
2886void ValidationStateTracker::PostCallRecordGetImageSparseMemoryRequirements2(
2887 VkDevice device, const VkImageSparseMemoryRequirementsInfo2KHR *pInfo, uint32_t *pSparseMemoryRequirementCount,
2888 VkSparseImageMemoryRequirements2KHR *pSparseMemoryRequirements) {
2889 auto image_state = GetImageState(pInfo->image);
2890 image_state->get_sparse_reqs_called = true;
2891 if (!pSparseMemoryRequirements) return;
2892 for (uint32_t i = 0; i < *pSparseMemoryRequirementCount; i++) {
2893 assert(!pSparseMemoryRequirements[i].pNext); // TODO: If an extension is ever added here we need to handle it
2894 RecordGetImageSparseMemoryRequirementsState(image_state, &pSparseMemoryRequirements[i].memoryRequirements);
2895 }
2896}
2897
2898void ValidationStateTracker::PostCallRecordGetImageSparseMemoryRequirements2KHR(
2899 VkDevice device, const VkImageSparseMemoryRequirementsInfo2KHR *pInfo, uint32_t *pSparseMemoryRequirementCount,
2900 VkSparseImageMemoryRequirements2KHR *pSparseMemoryRequirements) {
2901 auto image_state = GetImageState(pInfo->image);
2902 image_state->get_sparse_reqs_called = true;
2903 if (!pSparseMemoryRequirements) return;
2904 for (uint32_t i = 0; i < *pSparseMemoryRequirementCount; i++) {
2905 assert(!pSparseMemoryRequirements[i].pNext); // TODO: If an extension is ever added here we need to handle it
2906 RecordGetImageSparseMemoryRequirementsState(image_state, &pSparseMemoryRequirements[i].memoryRequirements);
2907 }
2908}
2909
2910void ValidationStateTracker::PreCallRecordDestroyShaderModule(VkDevice device, VkShaderModule shaderModule,
2911 const VkAllocationCallbacks *pAllocator) {
2912 if (!shaderModule) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002913 auto shader_module_state = GetShaderModuleState(shaderModule);
2914 shader_module_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06002915 shaderModuleMap.erase(shaderModule);
2916}
2917
2918void ValidationStateTracker::PreCallRecordDestroyPipeline(VkDevice device, VkPipeline pipeline,
2919 const VkAllocationCallbacks *pAllocator) {
2920 if (!pipeline) return;
2921 PIPELINE_STATE *pipeline_state = GetPipelineState(pipeline);
2922 const VulkanTypedHandle obj_struct(pipeline, kVulkanObjectTypePipeline);
2923 // Any bound cmd buffers are now invalid
2924 InvalidateCommandBuffers(pipeline_state->cb_bindings, obj_struct);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002925 pipeline_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06002926 pipelineMap.erase(pipeline);
2927}
2928
2929void ValidationStateTracker::PreCallRecordDestroyPipelineLayout(VkDevice device, VkPipelineLayout pipelineLayout,
2930 const VkAllocationCallbacks *pAllocator) {
2931 if (!pipelineLayout) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002932 auto pipeline_layout_state = GetPipelineLayout(pipelineLayout);
2933 pipeline_layout_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06002934 pipelineLayoutMap.erase(pipelineLayout);
2935}
2936
2937void ValidationStateTracker::PreCallRecordDestroySampler(VkDevice device, VkSampler sampler,
2938 const VkAllocationCallbacks *pAllocator) {
2939 if (!sampler) return;
2940 SAMPLER_STATE *sampler_state = GetSamplerState(sampler);
2941 const VulkanTypedHandle obj_struct(sampler, kVulkanObjectTypeSampler);
2942 // Any bound cmd buffers are now invalid
2943 if (sampler_state) {
2944 InvalidateCommandBuffers(sampler_state->cb_bindings, obj_struct);
Yuly Novikov424cdd52020-05-26 16:45:12 -04002945
2946 if (sampler_state->createInfo.borderColor == VK_BORDER_COLOR_INT_CUSTOM_EXT ||
2947 sampler_state->createInfo.borderColor == VK_BORDER_COLOR_FLOAT_CUSTOM_EXT) {
2948 custom_border_color_sampler_count--;
2949 }
2950
2951 sampler_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06002952 }
2953 samplerMap.erase(sampler);
2954}
2955
2956void ValidationStateTracker::PreCallRecordDestroyDescriptorSetLayout(VkDevice device, VkDescriptorSetLayout descriptorSetLayout,
2957 const VkAllocationCallbacks *pAllocator) {
2958 if (!descriptorSetLayout) return;
2959 auto layout_it = descriptorSetLayoutMap.find(descriptorSetLayout);
2960 if (layout_it != descriptorSetLayoutMap.end()) {
Jeff Bolz6ae39612019-10-11 20:57:36 -05002961 layout_it->second.get()->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06002962 descriptorSetLayoutMap.erase(layout_it);
2963 }
2964}
2965
2966void ValidationStateTracker::PreCallRecordDestroyDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
2967 const VkAllocationCallbacks *pAllocator) {
2968 if (!descriptorPool) return;
2969 DESCRIPTOR_POOL_STATE *desc_pool_state = GetDescriptorPoolState(descriptorPool);
2970 const VulkanTypedHandle obj_struct(descriptorPool, kVulkanObjectTypeDescriptorPool);
2971 if (desc_pool_state) {
2972 // Any bound cmd buffers are now invalid
2973 InvalidateCommandBuffers(desc_pool_state->cb_bindings, obj_struct);
2974 // Free sets that were in this pool
2975 for (auto ds : desc_pool_state->sets) {
2976 FreeDescriptorSet(ds);
2977 }
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002978 desc_pool_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06002979 descriptorPoolMap.erase(descriptorPool);
2980 }
2981}
2982
2983// Free all command buffers in given list, removing all references/links to them using ResetCommandBufferState
2984void ValidationStateTracker::FreeCommandBufferStates(COMMAND_POOL_STATE *pool_state, const uint32_t command_buffer_count,
2985 const VkCommandBuffer *command_buffers) {
2986 for (uint32_t i = 0; i < command_buffer_count; i++) {
John Zulaufd1f85d42020-04-15 12:23:15 -06002987 // Allow any derived class to clean up command buffer state
2988 if (command_buffer_free_callback) {
2989 (*command_buffer_free_callback)(command_buffers[i]);
2990 }
2991
locke-lunargd556cc32019-09-17 01:21:23 -06002992 auto cb_state = GetCBState(command_buffers[i]);
2993 // Remove references to command buffer's state and delete
2994 if (cb_state) {
2995 // reset prior to delete, removing various references to it.
2996 // TODO: fix this, it's insane.
2997 ResetCommandBufferState(cb_state->commandBuffer);
2998 // Remove the cb_state's references from COMMAND_POOL_STATEs
2999 pool_state->commandBuffers.erase(command_buffers[i]);
3000 // Remove the cb debug labels
3001 EraseCmdDebugUtilsLabel(report_data, cb_state->commandBuffer);
3002 // Remove CBState from CB map
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003003 cb_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06003004 commandBufferMap.erase(cb_state->commandBuffer);
3005 }
3006 }
3007}
3008
3009void ValidationStateTracker::PreCallRecordFreeCommandBuffers(VkDevice device, VkCommandPool commandPool,
3010 uint32_t commandBufferCount, const VkCommandBuffer *pCommandBuffers) {
3011 auto pPool = GetCommandPoolState(commandPool);
3012 FreeCommandBufferStates(pPool, commandBufferCount, pCommandBuffers);
3013}
3014
3015void ValidationStateTracker::PostCallRecordCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo *pCreateInfo,
3016 const VkAllocationCallbacks *pAllocator, VkCommandPool *pCommandPool,
3017 VkResult result) {
3018 if (VK_SUCCESS != result) return;
sfricke-samsungc1543372020-08-18 22:37:27 -07003019 VkCommandPool command_pool = *pCommandPool;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003020 auto cmd_pool_state = std::make_shared<COMMAND_POOL_STATE>();
sfricke-samsungc1543372020-08-18 22:37:27 -07003021 cmd_pool_state->commandPool = command_pool;
locke-lunargd556cc32019-09-17 01:21:23 -06003022 cmd_pool_state->createFlags = pCreateInfo->flags;
3023 cmd_pool_state->queueFamilyIndex = pCreateInfo->queueFamilyIndex;
sfricke-samsung0c45edc2020-07-01 22:19:53 -07003024 cmd_pool_state->unprotected = ((pCreateInfo->flags & VK_COMMAND_POOL_CREATE_PROTECTED_BIT) == 0);
sfricke-samsungc1543372020-08-18 22:37:27 -07003025 commandPoolMap[command_pool] = std::move(cmd_pool_state);
locke-lunargd556cc32019-09-17 01:21:23 -06003026}
3027
3028void ValidationStateTracker::PostCallRecordCreateQueryPool(VkDevice device, const VkQueryPoolCreateInfo *pCreateInfo,
3029 const VkAllocationCallbacks *pAllocator, VkQueryPool *pQueryPool,
3030 VkResult result) {
3031 if (VK_SUCCESS != result) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003032 auto query_pool_state = std::make_shared<QUERY_POOL_STATE>();
locke-lunargd556cc32019-09-17 01:21:23 -06003033 query_pool_state->createInfo = *pCreateInfo;
Lionel Landwerlinc7420912019-05-23 00:33:42 +01003034 query_pool_state->pool = *pQueryPool;
3035 if (pCreateInfo->queryType == VK_QUERY_TYPE_PERFORMANCE_QUERY_KHR) {
3036 const auto *perf = lvl_find_in_chain<VkQueryPoolPerformanceCreateInfoKHR>(pCreateInfo->pNext);
Mark Lobodzinski7e948e42020-09-09 10:23:36 -06003037 query_pool_state->perf_counter_index_count = perf->counterIndexCount;
Lionel Landwerlinc7420912019-05-23 00:33:42 +01003038
Mark Lobodzinski7e948e42020-09-09 10:23:36 -06003039 const QUEUE_FAMILY_PERF_COUNTERS &counters = *physical_device_state->perf_counters[perf->queueFamilyIndex];
Lionel Landwerlinc7420912019-05-23 00:33:42 +01003040 for (uint32_t i = 0; i < perf->counterIndexCount; i++) {
3041 const auto &counter = counters.counters[perf->pCounterIndices[i]];
3042 switch (counter.scope) {
3043 case VK_QUERY_SCOPE_COMMAND_BUFFER_KHR:
3044 query_pool_state->has_perf_scope_command_buffer = true;
3045 break;
3046 case VK_QUERY_SCOPE_RENDER_PASS_KHR:
3047 query_pool_state->has_perf_scope_render_pass = true;
3048 break;
3049 default:
3050 break;
3051 }
3052 }
3053
3054 DispatchGetPhysicalDeviceQueueFamilyPerformanceQueryPassesKHR(physical_device_state->phys_device, perf,
3055 &query_pool_state->n_performance_passes);
3056 }
3057
locke-lunargd556cc32019-09-17 01:21:23 -06003058 queryPoolMap[*pQueryPool] = std::move(query_pool_state);
3059
3060 QueryObject query_obj{*pQueryPool, 0u};
3061 for (uint32_t i = 0; i < pCreateInfo->queryCount; ++i) {
3062 query_obj.query = i;
3063 queryToStateMap[query_obj] = QUERYSTATE_UNKNOWN;
3064 }
3065}
3066
3067void ValidationStateTracker::PreCallRecordDestroyCommandPool(VkDevice device, VkCommandPool commandPool,
3068 const VkAllocationCallbacks *pAllocator) {
3069 if (!commandPool) return;
3070 COMMAND_POOL_STATE *cp_state = GetCommandPoolState(commandPool);
3071 // Remove cmdpool from cmdpoolmap, after freeing layer data for the command buffers
3072 // "When a pool is destroyed, all command buffers allocated from the pool are freed."
3073 if (cp_state) {
3074 // Create a vector, as FreeCommandBufferStates deletes from cp_state->commandBuffers during iteration.
3075 std::vector<VkCommandBuffer> cb_vec{cp_state->commandBuffers.begin(), cp_state->commandBuffers.end()};
3076 FreeCommandBufferStates(cp_state, static_cast<uint32_t>(cb_vec.size()), cb_vec.data());
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003077 cp_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06003078 commandPoolMap.erase(commandPool);
3079 }
3080}
3081
3082void ValidationStateTracker::PostCallRecordResetCommandPool(VkDevice device, VkCommandPool commandPool,
3083 VkCommandPoolResetFlags flags, VkResult result) {
3084 if (VK_SUCCESS != result) return;
3085 // Reset all of the CBs allocated from this pool
3086 auto command_pool_state = GetCommandPoolState(commandPool);
3087 for (auto cmdBuffer : command_pool_state->commandBuffers) {
3088 ResetCommandBufferState(cmdBuffer);
3089 }
3090}
3091
3092void ValidationStateTracker::PostCallRecordResetFences(VkDevice device, uint32_t fenceCount, const VkFence *pFences,
3093 VkResult result) {
3094 for (uint32_t i = 0; i < fenceCount; ++i) {
3095 auto pFence = GetFenceState(pFences[i]);
3096 if (pFence) {
3097 if (pFence->scope == kSyncScopeInternal) {
3098 pFence->state = FENCE_UNSIGNALED;
3099 } else if (pFence->scope == kSyncScopeExternalTemporary) {
3100 pFence->scope = kSyncScopeInternal;
3101 }
3102 }
3103 }
3104}
3105
Jeff Bolzadbfa852019-10-04 13:53:30 -05003106// For given cb_nodes, invalidate them and track object causing invalidation.
3107// InvalidateCommandBuffers and InvalidateLinkedCommandBuffers are essentially
3108// the same, except one takes a map and one takes a set, and InvalidateCommandBuffers
3109// can also unlink objects from command buffers.
3110void ValidationStateTracker::InvalidateCommandBuffers(small_unordered_map<CMD_BUFFER_STATE *, int, 8> &cb_nodes,
3111 const VulkanTypedHandle &obj, bool unlink) {
3112 for (const auto &cb_node_pair : cb_nodes) {
3113 auto &cb_node = cb_node_pair.first;
3114 if (cb_node->state == CB_RECORDING) {
3115 cb_node->state = CB_INVALID_INCOMPLETE;
3116 } else if (cb_node->state == CB_RECORDED) {
3117 cb_node->state = CB_INVALID_COMPLETE;
3118 }
3119 cb_node->broken_bindings.push_back(obj);
3120
3121 // if secondary, then propagate the invalidation to the primaries that will call us.
3122 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
3123 InvalidateLinkedCommandBuffers(cb_node->linkedCommandBuffers, obj);
3124 }
3125 if (unlink) {
3126 int index = cb_node_pair.second;
3127 assert(cb_node->object_bindings[index] == obj);
3128 cb_node->object_bindings[index] = VulkanTypedHandle();
3129 }
3130 }
3131 if (unlink) {
3132 cb_nodes.clear();
3133 }
3134}
3135
3136void ValidationStateTracker::InvalidateLinkedCommandBuffers(std::unordered_set<CMD_BUFFER_STATE *> &cb_nodes,
3137 const VulkanTypedHandle &obj) {
locke-lunargd556cc32019-09-17 01:21:23 -06003138 for (auto cb_node : cb_nodes) {
3139 if (cb_node->state == CB_RECORDING) {
3140 cb_node->state = CB_INVALID_INCOMPLETE;
3141 } else if (cb_node->state == CB_RECORDED) {
3142 cb_node->state = CB_INVALID_COMPLETE;
3143 }
3144 cb_node->broken_bindings.push_back(obj);
3145
3146 // if secondary, then propagate the invalidation to the primaries that will call us.
3147 if (cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
Jeff Bolzadbfa852019-10-04 13:53:30 -05003148 InvalidateLinkedCommandBuffers(cb_node->linkedCommandBuffers, obj);
locke-lunargd556cc32019-09-17 01:21:23 -06003149 }
3150 }
3151}
3152
3153void ValidationStateTracker::PreCallRecordDestroyFramebuffer(VkDevice device, VkFramebuffer framebuffer,
3154 const VkAllocationCallbacks *pAllocator) {
3155 if (!framebuffer) return;
3156 FRAMEBUFFER_STATE *framebuffer_state = GetFramebufferState(framebuffer);
3157 const VulkanTypedHandle obj_struct(framebuffer, kVulkanObjectTypeFramebuffer);
3158 InvalidateCommandBuffers(framebuffer_state->cb_bindings, obj_struct);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003159 framebuffer_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06003160 frameBufferMap.erase(framebuffer);
3161}
3162
3163void ValidationStateTracker::PreCallRecordDestroyRenderPass(VkDevice device, VkRenderPass renderPass,
3164 const VkAllocationCallbacks *pAllocator) {
3165 if (!renderPass) return;
3166 RENDER_PASS_STATE *rp_state = GetRenderPassState(renderPass);
3167 const VulkanTypedHandle obj_struct(renderPass, kVulkanObjectTypeRenderPass);
3168 InvalidateCommandBuffers(rp_state->cb_bindings, obj_struct);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003169 rp_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06003170 renderPassMap.erase(renderPass);
3171}
3172
3173void ValidationStateTracker::PostCallRecordCreateFence(VkDevice device, const VkFenceCreateInfo *pCreateInfo,
3174 const VkAllocationCallbacks *pAllocator, VkFence *pFence, VkResult result) {
3175 if (VK_SUCCESS != result) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003176 auto fence_state = std::make_shared<FENCE_STATE>();
locke-lunargd556cc32019-09-17 01:21:23 -06003177 fence_state->fence = *pFence;
3178 fence_state->createInfo = *pCreateInfo;
3179 fence_state->state = (pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) ? FENCE_RETIRED : FENCE_UNSIGNALED;
3180 fenceMap[*pFence] = std::move(fence_state);
3181}
3182
3183bool ValidationStateTracker::PreCallValidateCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
3184 const VkGraphicsPipelineCreateInfo *pCreateInfos,
3185 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003186 void *cgpl_state_data) const {
locke-lunargd556cc32019-09-17 01:21:23 -06003187 // Set up the state that CoreChecks, gpu_validation and later StateTracker Record will use.
3188 create_graphics_pipeline_api_state *cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state *>(cgpl_state_data);
3189 cgpl_state->pCreateInfos = pCreateInfos; // GPU validation can alter this, so we have to set a default value for the Chassis
3190 cgpl_state->pipe_state.reserve(count);
3191 for (uint32_t i = 0; i < count; i++) {
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003192 cgpl_state->pipe_state.push_back(std::make_shared<PIPELINE_STATE>());
Jeff Bolz6ae39612019-10-11 20:57:36 -05003193 (cgpl_state->pipe_state)[i]->initGraphicsPipeline(this, &pCreateInfos[i], GetRenderPassShared(pCreateInfos[i].renderPass));
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003194 (cgpl_state->pipe_state)[i]->pipeline_layout = GetPipelineLayoutShared(pCreateInfos[i].layout);
locke-lunargd556cc32019-09-17 01:21:23 -06003195 }
3196 return false;
3197}
3198
3199void ValidationStateTracker::PostCallRecordCreateGraphicsPipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
3200 const VkGraphicsPipelineCreateInfo *pCreateInfos,
3201 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
3202 VkResult result, void *cgpl_state_data) {
3203 create_graphics_pipeline_api_state *cgpl_state = reinterpret_cast<create_graphics_pipeline_api_state *>(cgpl_state_data);
3204 // This API may create pipelines regardless of the return value
3205 for (uint32_t i = 0; i < count; i++) {
3206 if (pPipelines[i] != VK_NULL_HANDLE) {
3207 (cgpl_state->pipe_state)[i]->pipeline = pPipelines[i];
3208 pipelineMap[pPipelines[i]] = std::move((cgpl_state->pipe_state)[i]);
3209 }
3210 }
3211 cgpl_state->pipe_state.clear();
3212}
3213
3214bool ValidationStateTracker::PreCallValidateCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
3215 const VkComputePipelineCreateInfo *pCreateInfos,
3216 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003217 void *ccpl_state_data) const {
locke-lunargd556cc32019-09-17 01:21:23 -06003218 auto *ccpl_state = reinterpret_cast<create_compute_pipeline_api_state *>(ccpl_state_data);
3219 ccpl_state->pCreateInfos = pCreateInfos; // GPU validation can alter this, so we have to set a default value for the Chassis
3220 ccpl_state->pipe_state.reserve(count);
3221 for (uint32_t i = 0; i < count; i++) {
3222 // Create and initialize internal tracking data structure
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003223 ccpl_state->pipe_state.push_back(std::make_shared<PIPELINE_STATE>());
locke-lunargd556cc32019-09-17 01:21:23 -06003224 ccpl_state->pipe_state.back()->initComputePipeline(this, &pCreateInfos[i]);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003225 ccpl_state->pipe_state.back()->pipeline_layout = GetPipelineLayoutShared(pCreateInfos[i].layout);
locke-lunargd556cc32019-09-17 01:21:23 -06003226 }
3227 return false;
3228}
3229
3230void ValidationStateTracker::PostCallRecordCreateComputePipelines(VkDevice device, VkPipelineCache pipelineCache, uint32_t count,
3231 const VkComputePipelineCreateInfo *pCreateInfos,
3232 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines,
3233 VkResult result, void *ccpl_state_data) {
3234 create_compute_pipeline_api_state *ccpl_state = reinterpret_cast<create_compute_pipeline_api_state *>(ccpl_state_data);
3235
3236 // This API may create pipelines regardless of the return value
3237 for (uint32_t i = 0; i < count; i++) {
3238 if (pPipelines[i] != VK_NULL_HANDLE) {
3239 (ccpl_state->pipe_state)[i]->pipeline = pPipelines[i];
3240 pipelineMap[pPipelines[i]] = std::move((ccpl_state->pipe_state)[i]);
3241 }
3242 }
3243 ccpl_state->pipe_state.clear();
3244}
3245
3246bool ValidationStateTracker::PreCallValidateCreateRayTracingPipelinesNV(VkDevice device, VkPipelineCache pipelineCache,
3247 uint32_t count,
3248 const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
3249 const VkAllocationCallbacks *pAllocator,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003250 VkPipeline *pPipelines, void *crtpl_state_data) const {
locke-lunargd556cc32019-09-17 01:21:23 -06003251 auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_api_state *>(crtpl_state_data);
3252 crtpl_state->pipe_state.reserve(count);
3253 for (uint32_t i = 0; i < count; i++) {
3254 // Create and initialize internal tracking data structure
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003255 crtpl_state->pipe_state.push_back(std::make_shared<PIPELINE_STATE>());
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003256 crtpl_state->pipe_state.back()->initRayTracingPipeline(this, &pCreateInfos[i]);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003257 crtpl_state->pipe_state.back()->pipeline_layout = GetPipelineLayoutShared(pCreateInfos[i].layout);
locke-lunargd556cc32019-09-17 01:21:23 -06003258 }
3259 return false;
3260}
3261
3262void ValidationStateTracker::PostCallRecordCreateRayTracingPipelinesNV(
3263 VkDevice device, VkPipelineCache pipelineCache, uint32_t count, const VkRayTracingPipelineCreateInfoNV *pCreateInfos,
3264 const VkAllocationCallbacks *pAllocator, VkPipeline *pPipelines, VkResult result, void *crtpl_state_data) {
3265 auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_api_state *>(crtpl_state_data);
3266 // This API may create pipelines regardless of the return value
3267 for (uint32_t i = 0; i < count; i++) {
3268 if (pPipelines[i] != VK_NULL_HANDLE) {
3269 (crtpl_state->pipe_state)[i]->pipeline = pPipelines[i];
3270 pipelineMap[pPipelines[i]] = std::move((crtpl_state->pipe_state)[i]);
3271 }
3272 }
3273 crtpl_state->pipe_state.clear();
3274}
3275
sourav parmarcd5fb182020-07-17 12:58:44 -07003276bool ValidationStateTracker::PreCallValidateCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
3277 VkPipelineCache pipelineCache, uint32_t count,
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003278 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
3279 const VkAllocationCallbacks *pAllocator,
3280 VkPipeline *pPipelines, void *crtpl_state_data) const {
3281 auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data);
3282 crtpl_state->pipe_state.reserve(count);
3283 for (uint32_t i = 0; i < count; i++) {
3284 // Create and initialize internal tracking data structure
3285 crtpl_state->pipe_state.push_back(std::make_shared<PIPELINE_STATE>());
3286 crtpl_state->pipe_state.back()->initRayTracingPipeline(this, &pCreateInfos[i]);
3287 crtpl_state->pipe_state.back()->pipeline_layout = GetPipelineLayoutShared(pCreateInfos[i].layout);
3288 }
3289 return false;
3290}
3291
sourav parmarcd5fb182020-07-17 12:58:44 -07003292void ValidationStateTracker::PostCallRecordCreateRayTracingPipelinesKHR(VkDevice device, VkDeferredOperationKHR deferredOperation,
3293 VkPipelineCache pipelineCache, uint32_t count,
3294 const VkRayTracingPipelineCreateInfoKHR *pCreateInfos,
3295 const VkAllocationCallbacks *pAllocator,
3296 VkPipeline *pPipelines, VkResult result,
3297 void *crtpl_state_data) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003298 auto *crtpl_state = reinterpret_cast<create_ray_tracing_pipeline_khr_api_state *>(crtpl_state_data);
3299 // This API may create pipelines regardless of the return value
3300 for (uint32_t i = 0; i < count; i++) {
3301 if (pPipelines[i] != VK_NULL_HANDLE) {
3302 (crtpl_state->pipe_state)[i]->pipeline = pPipelines[i];
3303 pipelineMap[pPipelines[i]] = std::move((crtpl_state->pipe_state)[i]);
3304 }
3305 }
3306 crtpl_state->pipe_state.clear();
3307}
3308
locke-lunargd556cc32019-09-17 01:21:23 -06003309void ValidationStateTracker::PostCallRecordCreateSampler(VkDevice device, const VkSamplerCreateInfo *pCreateInfo,
3310 const VkAllocationCallbacks *pAllocator, VkSampler *pSampler,
3311 VkResult result) {
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003312 samplerMap[*pSampler] = std::make_shared<SAMPLER_STATE>(pSampler, pCreateInfo);
Tony-LunarG7337b312020-04-15 16:40:25 -06003313 if (pCreateInfo->borderColor == VK_BORDER_COLOR_INT_CUSTOM_EXT || pCreateInfo->borderColor == VK_BORDER_COLOR_FLOAT_CUSTOM_EXT)
3314 custom_border_color_sampler_count++;
locke-lunargd556cc32019-09-17 01:21:23 -06003315}
3316
3317void ValidationStateTracker::PostCallRecordCreateDescriptorSetLayout(VkDevice device,
3318 const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
3319 const VkAllocationCallbacks *pAllocator,
3320 VkDescriptorSetLayout *pSetLayout, VkResult result) {
3321 if (VK_SUCCESS != result) return;
3322 descriptorSetLayoutMap[*pSetLayout] = std::make_shared<cvdescriptorset::DescriptorSetLayout>(pCreateInfo, *pSetLayout);
3323}
3324
3325// For repeatable sorting, not very useful for "memory in range" search
3326struct PushConstantRangeCompare {
3327 bool operator()(const VkPushConstantRange *lhs, const VkPushConstantRange *rhs) const {
3328 if (lhs->offset == rhs->offset) {
3329 if (lhs->size == rhs->size) {
3330 // The comparison is arbitrary, but avoids false aliasing by comparing all fields.
3331 return lhs->stageFlags < rhs->stageFlags;
3332 }
3333 // If the offsets are the same then sorting by the end of range is useful for validation
3334 return lhs->size < rhs->size;
3335 }
3336 return lhs->offset < rhs->offset;
3337 }
3338};
3339
3340static PushConstantRangesDict push_constant_ranges_dict;
3341
3342PushConstantRangesId GetCanonicalId(const VkPipelineLayoutCreateInfo *info) {
3343 if (!info->pPushConstantRanges) {
3344 // Hand back the empty entry (creating as needed)...
3345 return push_constant_ranges_dict.look_up(PushConstantRanges());
3346 }
3347
3348 // Sort the input ranges to ensure equivalent ranges map to the same id
3349 std::set<const VkPushConstantRange *, PushConstantRangeCompare> sorted;
3350 for (uint32_t i = 0; i < info->pushConstantRangeCount; i++) {
3351 sorted.insert(info->pPushConstantRanges + i);
3352 }
3353
Jeremy Hayesf34b9fb2019-12-06 09:37:03 -07003354 PushConstantRanges ranges;
3355 ranges.reserve(sorted.size());
locke-lunargd556cc32019-09-17 01:21:23 -06003356 for (const auto range : sorted) {
3357 ranges.emplace_back(*range);
3358 }
3359 return push_constant_ranges_dict.look_up(std::move(ranges));
3360}
3361
3362// Dictionary of canoncial form of the pipeline set layout of descriptor set layouts
3363static PipelineLayoutSetLayoutsDict pipeline_layout_set_layouts_dict;
3364
3365// Dictionary of canonical form of the "compatible for set" records
3366static PipelineLayoutCompatDict pipeline_layout_compat_dict;
3367
3368static PipelineLayoutCompatId GetCanonicalId(const uint32_t set_index, const PushConstantRangesId pcr_id,
3369 const PipelineLayoutSetLayoutsId set_layouts_id) {
3370 return pipeline_layout_compat_dict.look_up(PipelineLayoutCompatDef(set_index, pcr_id, set_layouts_id));
3371}
3372
3373void ValidationStateTracker::PostCallRecordCreatePipelineLayout(VkDevice device, const VkPipelineLayoutCreateInfo *pCreateInfo,
3374 const VkAllocationCallbacks *pAllocator,
3375 VkPipelineLayout *pPipelineLayout, VkResult result) {
3376 if (VK_SUCCESS != result) return;
3377
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003378 auto pipeline_layout_state = std::make_shared<PIPELINE_LAYOUT_STATE>();
locke-lunargd556cc32019-09-17 01:21:23 -06003379 pipeline_layout_state->layout = *pPipelineLayout;
3380 pipeline_layout_state->set_layouts.resize(pCreateInfo->setLayoutCount);
3381 PipelineLayoutSetLayoutsDef set_layouts(pCreateInfo->setLayoutCount);
3382 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; ++i) {
Jeff Bolz6ae39612019-10-11 20:57:36 -05003383 pipeline_layout_state->set_layouts[i] = GetDescriptorSetLayoutShared(pCreateInfo->pSetLayouts[i]);
locke-lunargd556cc32019-09-17 01:21:23 -06003384 set_layouts[i] = pipeline_layout_state->set_layouts[i]->GetLayoutId();
3385 }
3386
3387 // Get canonical form IDs for the "compatible for set" contents
3388 pipeline_layout_state->push_constant_ranges = GetCanonicalId(pCreateInfo);
3389 auto set_layouts_id = pipeline_layout_set_layouts_dict.look_up(set_layouts);
3390 pipeline_layout_state->compat_for_set.reserve(pCreateInfo->setLayoutCount);
3391
3392 // Create table of "compatible for set N" cannonical forms for trivial accept validation
3393 for (uint32_t i = 0; i < pCreateInfo->setLayoutCount; ++i) {
3394 pipeline_layout_state->compat_for_set.emplace_back(
3395 GetCanonicalId(i, pipeline_layout_state->push_constant_ranges, set_layouts_id));
3396 }
3397 pipelineLayoutMap[*pPipelineLayout] = std::move(pipeline_layout_state);
3398}
3399
3400void ValidationStateTracker::PostCallRecordCreateDescriptorPool(VkDevice device, const VkDescriptorPoolCreateInfo *pCreateInfo,
3401 const VkAllocationCallbacks *pAllocator,
3402 VkDescriptorPool *pDescriptorPool, VkResult result) {
3403 if (VK_SUCCESS != result) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003404 descriptorPoolMap[*pDescriptorPool] = std::make_shared<DESCRIPTOR_POOL_STATE>(*pDescriptorPool, pCreateInfo);
locke-lunargd556cc32019-09-17 01:21:23 -06003405}
3406
3407void ValidationStateTracker::PostCallRecordResetDescriptorPool(VkDevice device, VkDescriptorPool descriptorPool,
3408 VkDescriptorPoolResetFlags flags, VkResult result) {
3409 if (VK_SUCCESS != result) return;
3410 DESCRIPTOR_POOL_STATE *pPool = GetDescriptorPoolState(descriptorPool);
3411 // TODO: validate flags
3412 // For every set off of this pool, clear it, remove from setMap, and free cvdescriptorset::DescriptorSet
3413 for (auto ds : pPool->sets) {
3414 FreeDescriptorSet(ds);
3415 }
3416 pPool->sets.clear();
3417 // Reset available count for each type and available sets for this pool
3418 for (auto it = pPool->availableDescriptorTypeCount.begin(); it != pPool->availableDescriptorTypeCount.end(); ++it) {
3419 pPool->availableDescriptorTypeCount[it->first] = pPool->maxDescriptorTypeCount[it->first];
3420 }
3421 pPool->availableSets = pPool->maxSets;
3422}
3423
3424bool ValidationStateTracker::PreCallValidateAllocateDescriptorSets(VkDevice device,
3425 const VkDescriptorSetAllocateInfo *pAllocateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003426 VkDescriptorSet *pDescriptorSets, void *ads_state_data) const {
locke-lunargd556cc32019-09-17 01:21:23 -06003427 // Always update common data
3428 cvdescriptorset::AllocateDescriptorSetsData *ads_state =
3429 reinterpret_cast<cvdescriptorset::AllocateDescriptorSetsData *>(ads_state_data);
3430 UpdateAllocateDescriptorSetsData(pAllocateInfo, ads_state);
3431
3432 return false;
3433}
3434
3435// Allocation state was good and call down chain was made so update state based on allocating descriptor sets
3436void ValidationStateTracker::PostCallRecordAllocateDescriptorSets(VkDevice device, const VkDescriptorSetAllocateInfo *pAllocateInfo,
3437 VkDescriptorSet *pDescriptorSets, VkResult result,
3438 void *ads_state_data) {
3439 if (VK_SUCCESS != result) return;
3440 // All the updates are contained in a single cvdescriptorset function
3441 cvdescriptorset::AllocateDescriptorSetsData *ads_state =
3442 reinterpret_cast<cvdescriptorset::AllocateDescriptorSetsData *>(ads_state_data);
3443 PerformAllocateDescriptorSets(pAllocateInfo, pDescriptorSets, ads_state);
3444}
3445
3446void ValidationStateTracker::PreCallRecordFreeDescriptorSets(VkDevice device, VkDescriptorPool descriptorPool, uint32_t count,
3447 const VkDescriptorSet *pDescriptorSets) {
3448 DESCRIPTOR_POOL_STATE *pool_state = GetDescriptorPoolState(descriptorPool);
3449 // Update available descriptor sets in pool
3450 pool_state->availableSets += count;
3451
3452 // For each freed descriptor add its resources back into the pool as available and remove from pool and setMap
3453 for (uint32_t i = 0; i < count; ++i) {
3454 if (pDescriptorSets[i] != VK_NULL_HANDLE) {
3455 auto descriptor_set = setMap[pDescriptorSets[i]].get();
3456 uint32_t type_index = 0, descriptor_count = 0;
3457 for (uint32_t j = 0; j < descriptor_set->GetBindingCount(); ++j) {
3458 type_index = static_cast<uint32_t>(descriptor_set->GetTypeFromIndex(j));
3459 descriptor_count = descriptor_set->GetDescriptorCountFromIndex(j);
3460 pool_state->availableDescriptorTypeCount[type_index] += descriptor_count;
3461 }
3462 FreeDescriptorSet(descriptor_set);
3463 pool_state->sets.erase(descriptor_set);
3464 }
3465 }
3466}
3467
3468void ValidationStateTracker::PreCallRecordUpdateDescriptorSets(VkDevice device, uint32_t descriptorWriteCount,
3469 const VkWriteDescriptorSet *pDescriptorWrites,
3470 uint32_t descriptorCopyCount,
3471 const VkCopyDescriptorSet *pDescriptorCopies) {
3472 cvdescriptorset::PerformUpdateDescriptorSets(this, descriptorWriteCount, pDescriptorWrites, descriptorCopyCount,
3473 pDescriptorCopies);
3474}
3475
3476void ValidationStateTracker::PostCallRecordAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *pCreateInfo,
3477 VkCommandBuffer *pCommandBuffer, VkResult result) {
3478 if (VK_SUCCESS != result) return;
Jeff Bolz6835fda2019-10-06 00:15:34 -05003479 auto pPool = GetCommandPoolShared(pCreateInfo->commandPool);
locke-lunargd556cc32019-09-17 01:21:23 -06003480 if (pPool) {
3481 for (uint32_t i = 0; i < pCreateInfo->commandBufferCount; i++) {
3482 // Add command buffer to its commandPool map
3483 pPool->commandBuffers.insert(pCommandBuffer[i]);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003484 auto pCB = std::make_shared<CMD_BUFFER_STATE>();
locke-lunargd556cc32019-09-17 01:21:23 -06003485 pCB->createInfo = *pCreateInfo;
Jeff Bolz6835fda2019-10-06 00:15:34 -05003486 pCB->command_pool = pPool;
sfricke-samsung0c45edc2020-07-01 22:19:53 -07003487 pCB->unprotected = pPool->unprotected;
locke-lunargd556cc32019-09-17 01:21:23 -06003488 // Add command buffer to map
3489 commandBufferMap[pCommandBuffer[i]] = std::move(pCB);
3490 ResetCommandBufferState(pCommandBuffer[i]);
3491 }
3492 }
3493}
3494
3495// Add bindings between the given cmd buffer & framebuffer and the framebuffer's children
3496void ValidationStateTracker::AddFramebufferBinding(CMD_BUFFER_STATE *cb_state, FRAMEBUFFER_STATE *fb_state) {
Jeff Bolzadbfa852019-10-04 13:53:30 -05003497 AddCommandBufferBinding(fb_state->cb_bindings, VulkanTypedHandle(fb_state->framebuffer, kVulkanObjectTypeFramebuffer, fb_state),
locke-lunargd556cc32019-09-17 01:21:23 -06003498 cb_state);
Mark Lobodzinski544b3dd2019-12-03 14:44:54 -07003499 // If imageless fb, skip fb binding
locke-lunargfc78e932020-11-19 17:06:24 -07003500 if (!fb_state || fb_state->createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT_KHR) return;
locke-lunargd556cc32019-09-17 01:21:23 -06003501 const uint32_t attachmentCount = fb_state->createInfo.attachmentCount;
3502 for (uint32_t attachment = 0; attachment < attachmentCount; ++attachment) {
locke-lunargfc78e932020-11-19 17:06:24 -07003503 auto view_state = GetActiveAttachmentImageViewState(cb_state, attachment);
locke-lunargd556cc32019-09-17 01:21:23 -06003504 if (view_state) {
3505 AddCommandBufferBindingImageView(cb_state, view_state);
3506 }
3507 }
3508}
3509
locke-lunargfc78e932020-11-19 17:06:24 -07003510void UpdateSubpassAttachments(const safe_VkSubpassDescription2 &subpass, std::vector<SUBPASS_INFO> &subpasses) {
3511 for (uint32_t index = 0; index < subpass.inputAttachmentCount; ++index) {
3512 const uint32_t attachment_index = subpass.pInputAttachments[index].attachment;
3513 if (attachment_index != VK_ATTACHMENT_UNUSED) {
3514 subpasses[attachment_index].used = true;
3515 subpasses[attachment_index].usage = VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
3516 subpasses[attachment_index].layout = subpass.pInputAttachments[index].layout;
3517 }
3518 }
3519
3520 for (uint32_t index = 0; index < subpass.colorAttachmentCount; ++index) {
3521 const uint32_t attachment_index = subpass.pColorAttachments[index].attachment;
3522 if (attachment_index != VK_ATTACHMENT_UNUSED) {
3523 subpasses[attachment_index].used = true;
3524 subpasses[attachment_index].usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
3525 subpasses[attachment_index].layout = subpass.pColorAttachments[index].layout;
3526 }
3527 if (subpass.pResolveAttachments) {
3528 const uint32_t attachment_index2 = subpass.pResolveAttachments[index].attachment;
3529 if (attachment_index2 != VK_ATTACHMENT_UNUSED) {
3530 subpasses[attachment_index2].used = true;
3531 subpasses[attachment_index2].usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
3532 subpasses[attachment_index2].layout = subpass.pResolveAttachments[index].layout;
3533 }
3534 }
3535 }
3536
3537 if (subpass.pDepthStencilAttachment) {
3538 const uint32_t attachment_index = subpass.pDepthStencilAttachment->attachment;
3539 if (attachment_index != VK_ATTACHMENT_UNUSED) {
3540 subpasses[attachment_index].used = true;
3541 subpasses[attachment_index].usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
3542 subpasses[attachment_index].layout = subpass.pDepthStencilAttachment->layout;
3543 }
3544 }
3545}
3546
3547void UpdateAttachmentsView(ValidationStateTracker &tracker, CMD_BUFFER_STATE &cb_state, const FRAMEBUFFER_STATE &framebuffer,
3548 const VkRenderPassBeginInfo *pRenderPassBegin) {
3549 auto &attachments = *(cb_state.active_attachments.get());
3550 const bool imageless = (framebuffer.createInfo.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) ? true : false;
3551 const VkRenderPassAttachmentBeginInfo *attachment_info_struct = nullptr;
3552 if (pRenderPassBegin) attachment_info_struct = lvl_find_in_chain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
3553
3554 for (uint32_t i = 0; i < attachments.size(); ++i) {
3555 if (imageless) {
3556 if (attachment_info_struct && i < attachment_info_struct->attachmentCount) {
3557 auto res = cb_state.attachments_view_states.insert(
3558 tracker.GetShared<IMAGE_VIEW_STATE>(attachment_info_struct->pAttachments[i]));
3559 attachments[i] = res.first->get();
3560 }
3561 } else {
3562 auto res = cb_state.attachments_view_states.insert(framebuffer.attachments_view_state[i]);
3563 attachments[i] = res.first->get();
3564 }
3565 }
3566}
3567
locke-lunargd556cc32019-09-17 01:21:23 -06003568void ValidationStateTracker::PreCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer,
3569 const VkCommandBufferBeginInfo *pBeginInfo) {
3570 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3571 if (!cb_state) return;
locke-lunargfc78e932020-11-19 17:06:24 -07003572
locke-lunargd556cc32019-09-17 01:21:23 -06003573 if (CB_RECORDED == cb_state->state || CB_INVALID_COMPLETE == cb_state->state) {
3574 ResetCommandBufferState(commandBuffer);
3575 }
3576 // Set updated state here in case implicit reset occurs above
3577 cb_state->state = CB_RECORDING;
3578 cb_state->beginInfo = *pBeginInfo;
3579 if (cb_state->beginInfo.pInheritanceInfo) {
3580 cb_state->inheritanceInfo = *(cb_state->beginInfo.pInheritanceInfo);
3581 cb_state->beginInfo.pInheritanceInfo = &cb_state->inheritanceInfo;
3582 // If we are a secondary command-buffer and inheriting. Update the items we should inherit.
3583 if ((cb_state->createInfo.level != VK_COMMAND_BUFFER_LEVEL_PRIMARY) &&
3584 (cb_state->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) {
locke-lunargaecf2152020-05-12 17:15:41 -06003585 cb_state->activeRenderPass = GetShared<RENDER_PASS_STATE>(cb_state->beginInfo.pInheritanceInfo->renderPass);
locke-lunargd556cc32019-09-17 01:21:23 -06003586 cb_state->activeSubpass = cb_state->beginInfo.pInheritanceInfo->subpass;
locke-lunargfc78e932020-11-19 17:06:24 -07003587
locke-lunargaecf2152020-05-12 17:15:41 -06003588 if (cb_state->beginInfo.pInheritanceInfo->framebuffer) {
3589 cb_state->activeFramebuffer = GetShared<FRAMEBUFFER_STATE>(cb_state->beginInfo.pInheritanceInfo->framebuffer);
locke-lunargfc78e932020-11-19 17:06:24 -07003590 cb_state->active_subpasses = nullptr;
3591 cb_state->active_attachments = nullptr;
3592
3593 if (cb_state->activeFramebuffer) {
3594 cb_state->framebuffers.insert(cb_state->activeFramebuffer);
3595
3596 // Set cb_state->active_subpasses
3597 cb_state->active_subpasses =
3598 std::make_shared<std::vector<SUBPASS_INFO>>(cb_state->activeFramebuffer->createInfo.attachmentCount);
3599 const auto &subpass = cb_state->activeRenderPass->createInfo.pSubpasses[cb_state->activeSubpass];
3600 UpdateSubpassAttachments(subpass, *cb_state->active_subpasses);
3601
3602 // Set cb_state->active_attachments & cb_state->attachments_view_states
3603 cb_state->active_attachments =
3604 std::make_shared<std::vector<IMAGE_VIEW_STATE *>>(cb_state->activeFramebuffer->createInfo.attachmentCount);
3605 UpdateAttachmentsView(*this, *cb_state, *cb_state->activeFramebuffer, nullptr);
3606
3607 // Connect this framebuffer and its children to this cmdBuffer
3608 AddFramebufferBinding(cb_state, cb_state->activeFramebuffer.get());
3609 }
locke-lunargaecf2152020-05-12 17:15:41 -06003610 }
locke-lunargd556cc32019-09-17 01:21:23 -06003611 }
3612 }
3613
3614 auto chained_device_group_struct = lvl_find_in_chain<VkDeviceGroupCommandBufferBeginInfo>(pBeginInfo->pNext);
3615 if (chained_device_group_struct) {
3616 cb_state->initial_device_mask = chained_device_group_struct->deviceMask;
3617 } else {
3618 cb_state->initial_device_mask = (1 << physical_device_count) - 1;
3619 }
Lionel Landwerlinc7420912019-05-23 00:33:42 +01003620
3621 cb_state->performance_lock_acquired = performance_lock_acquired;
locke-lunargd556cc32019-09-17 01:21:23 -06003622}
3623
3624void ValidationStateTracker::PostCallRecordEndCommandBuffer(VkCommandBuffer commandBuffer, VkResult result) {
3625 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3626 if (!cb_state) return;
3627 // Cached validation is specific to a specific recording of a specific command buffer.
3628 for (auto descriptor_set : cb_state->validated_descriptor_sets) {
3629 descriptor_set->ClearCachedValidation(cb_state);
3630 }
3631 cb_state->validated_descriptor_sets.clear();
3632 if (VK_SUCCESS == result) {
3633 cb_state->state = CB_RECORDED;
3634 }
3635}
3636
3637void ValidationStateTracker::PostCallRecordResetCommandBuffer(VkCommandBuffer commandBuffer, VkCommandBufferResetFlags flags,
3638 VkResult result) {
3639 if (VK_SUCCESS == result) {
3640 ResetCommandBufferState(commandBuffer);
3641 }
3642}
3643
3644CBStatusFlags MakeStaticStateMask(VkPipelineDynamicStateCreateInfo const *ds) {
3645 // initially assume everything is static state
3646 CBStatusFlags flags = CBSTATUS_ALL_STATE_SET;
3647
3648 if (ds) {
3649 for (uint32_t i = 0; i < ds->dynamicStateCount; i++) {
locke-lunarg4189aa22020-10-21 00:23:48 -06003650 flags &= ~ConvertToCBStatusFlagBits(ds->pDynamicStates[i]);
locke-lunargd556cc32019-09-17 01:21:23 -06003651 }
3652 }
locke-lunargd556cc32019-09-17 01:21:23 -06003653 return flags;
3654}
3655
3656// Validation cache:
3657// CV is the bottommost implementor of this extension. Don't pass calls down.
3658// utility function to set collective state for pipeline
3659void SetPipelineState(PIPELINE_STATE *pPipe) {
3660 // If any attachment used by this pipeline has blendEnable, set top-level blendEnable
3661 if (pPipe->graphicsPipelineCI.pColorBlendState) {
3662 for (size_t i = 0; i < pPipe->attachments.size(); ++i) {
3663 if (VK_TRUE == pPipe->attachments[i].blendEnable) {
3664 if (((pPipe->attachments[i].dstAlphaBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) &&
3665 (pPipe->attachments[i].dstAlphaBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA)) ||
3666 ((pPipe->attachments[i].dstColorBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) &&
3667 (pPipe->attachments[i].dstColorBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA)) ||
3668 ((pPipe->attachments[i].srcAlphaBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) &&
3669 (pPipe->attachments[i].srcAlphaBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA)) ||
3670 ((pPipe->attachments[i].srcColorBlendFactor >= VK_BLEND_FACTOR_CONSTANT_COLOR) &&
3671 (pPipe->attachments[i].srcColorBlendFactor <= VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA))) {
3672 pPipe->blendConstantsEnabled = true;
3673 }
3674 }
3675 }
3676 }
sfricke-samsung8f658d42020-05-03 20:12:24 -07003677 // Check if sample location is enabled
3678 if (pPipe->graphicsPipelineCI.pMultisampleState) {
3679 const VkPipelineSampleLocationsStateCreateInfoEXT *sample_location_state =
3680 lvl_find_in_chain<VkPipelineSampleLocationsStateCreateInfoEXT>(pPipe->graphicsPipelineCI.pMultisampleState->pNext);
3681 if (sample_location_state != nullptr) {
3682 pPipe->sample_location_enabled = sample_location_state->sampleLocationsEnable;
3683 }
3684 }
locke-lunargd556cc32019-09-17 01:21:23 -06003685}
3686
locke-lunargb8be8222020-10-20 00:34:37 -06003687void UpdateSamplerDescriptorsUsedByImage(LAST_BOUND_STATE &last_bound_state) {
3688 if (!last_bound_state.pipeline_state) return;
3689 if (last_bound_state.per_set.empty()) return;
3690
3691 for (auto &slot : last_bound_state.pipeline_state->active_slots) {
3692 for (auto &req : slot.second) {
3693 for (auto &samplers : req.second.samplers_used_by_image) {
3694 for (auto &sampler : samplers) {
3695 if (sampler.first.sampler_slot.first < last_bound_state.per_set.size() &&
3696 last_bound_state.per_set[sampler.first.sampler_slot.first].bound_descriptor_set) {
3697 sampler.second = last_bound_state.per_set[sampler.first.sampler_slot.first]
locke-lunargb8d7a7a2020-10-25 16:01:52 -06003698 .bound_descriptor_set->GetDescriptorFromBinding(sampler.first.sampler_slot.second,
3699 sampler.first.sampler_index);
locke-lunargb8be8222020-10-20 00:34:37 -06003700 }
3701 }
3702 }
3703 }
3704 }
3705}
3706
locke-lunargd556cc32019-09-17 01:21:23 -06003707void ValidationStateTracker::PreCallRecordCmdBindPipeline(VkCommandBuffer commandBuffer, VkPipelineBindPoint pipelineBindPoint,
3708 VkPipeline pipeline) {
3709 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3710 assert(cb_state);
3711
3712 auto pipe_state = GetPipelineState(pipeline);
3713 if (VK_PIPELINE_BIND_POINT_GRAPHICS == pipelineBindPoint) {
3714 cb_state->status &= ~cb_state->static_status;
3715 cb_state->static_status = MakeStaticStateMask(pipe_state->graphicsPipelineCI.ptr()->pDynamicState);
3716 cb_state->status |= cb_state->static_status;
locke-lunarg4189aa22020-10-21 00:23:48 -06003717 cb_state->dynamic_status = CBSTATUS_ALL_STATE_SET & (~cb_state->static_status);
locke-lunargd556cc32019-09-17 01:21:23 -06003718 }
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003719 ResetCommandBufferPushConstantDataIfIncompatible(cb_state, pipe_state->pipeline_layout->layout);
locke-lunargb8d7a7a2020-10-25 16:01:52 -06003720 const auto lv_bind_point = ConvertToLvlBindPoint(pipelineBindPoint);
3721 cb_state->lastBound[lv_bind_point].pipeline_state = pipe_state;
locke-lunargd556cc32019-09-17 01:21:23 -06003722 SetPipelineState(pipe_state);
Jeff Bolzadbfa852019-10-04 13:53:30 -05003723 AddCommandBufferBinding(pipe_state->cb_bindings, VulkanTypedHandle(pipeline, kVulkanObjectTypePipeline), cb_state);
locke-lunargb8be8222020-10-20 00:34:37 -06003724
3725 for (auto &slot : pipe_state->active_slots) {
3726 for (auto &req : slot.second) {
3727 for (auto &sampler : req.second.samplers_used_by_image) {
3728 for (auto &des : sampler) {
3729 des.second = nullptr;
3730 }
3731 }
3732 }
3733 }
locke-lunargb8d7a7a2020-10-25 16:01:52 -06003734 UpdateSamplerDescriptorsUsedByImage(cb_state->lastBound[lv_bind_point]);
locke-lunargd556cc32019-09-17 01:21:23 -06003735}
3736
3737void ValidationStateTracker::PreCallRecordCmdSetViewport(VkCommandBuffer commandBuffer, uint32_t firstViewport,
3738 uint32_t viewportCount, const VkViewport *pViewports) {
3739 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3740 cb_state->viewportMask |= ((1u << viewportCount) - 1u) << firstViewport;
3741 cb_state->status |= CBSTATUS_VIEWPORT_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06003742 cb_state->static_status &= ~CBSTATUS_VIEWPORT_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06003743}
3744
3745void ValidationStateTracker::PreCallRecordCmdSetExclusiveScissorNV(VkCommandBuffer commandBuffer, uint32_t firstExclusiveScissor,
3746 uint32_t exclusiveScissorCount,
3747 const VkRect2D *pExclusiveScissors) {
3748 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3749 // TODO: We don't have VUIDs for validating that all exclusive scissors have been set.
3750 // cb_state->exclusiveScissorMask |= ((1u << exclusiveScissorCount) - 1u) << firstExclusiveScissor;
3751 cb_state->status |= CBSTATUS_EXCLUSIVE_SCISSOR_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06003752 cb_state->static_status &= ~CBSTATUS_EXCLUSIVE_SCISSOR_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06003753}
3754
3755void ValidationStateTracker::PreCallRecordCmdBindShadingRateImageNV(VkCommandBuffer commandBuffer, VkImageView imageView,
3756 VkImageLayout imageLayout) {
3757 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3758
3759 if (imageView != VK_NULL_HANDLE) {
3760 auto view_state = GetImageViewState(imageView);
3761 AddCommandBufferBindingImageView(cb_state, view_state);
3762 }
3763}
3764
3765void ValidationStateTracker::PreCallRecordCmdSetViewportShadingRatePaletteNV(VkCommandBuffer commandBuffer, uint32_t firstViewport,
3766 uint32_t viewportCount,
3767 const VkShadingRatePaletteNV *pShadingRatePalettes) {
3768 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3769 // TODO: We don't have VUIDs for validating that all shading rate palettes have been set.
3770 // cb_state->shadingRatePaletteMask |= ((1u << viewportCount) - 1u) << firstViewport;
3771 cb_state->status |= CBSTATUS_SHADING_RATE_PALETTE_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06003772 cb_state->static_status &= ~CBSTATUS_SHADING_RATE_PALETTE_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06003773}
3774
3775void ValidationStateTracker::PostCallRecordCreateAccelerationStructureNV(VkDevice device,
3776 const VkAccelerationStructureCreateInfoNV *pCreateInfo,
3777 const VkAllocationCallbacks *pAllocator,
3778 VkAccelerationStructureNV *pAccelerationStructure,
3779 VkResult result) {
3780 if (VK_SUCCESS != result) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003781 auto as_state = std::make_shared<ACCELERATION_STRUCTURE_STATE>(*pAccelerationStructure, pCreateInfo);
locke-lunargd556cc32019-09-17 01:21:23 -06003782
3783 // Query the requirements in case the application doesn't (to avoid bind/validation time query)
3784 VkAccelerationStructureMemoryRequirementsInfoNV as_memory_requirements_info = {};
3785 as_memory_requirements_info.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_INFO_NV;
3786 as_memory_requirements_info.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_OBJECT_NV;
3787 as_memory_requirements_info.accelerationStructure = as_state->acceleration_structure;
3788 DispatchGetAccelerationStructureMemoryRequirementsNV(device, &as_memory_requirements_info, &as_state->memory_requirements);
3789
3790 VkAccelerationStructureMemoryRequirementsInfoNV scratch_memory_req_info = {};
3791 scratch_memory_req_info.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_INFO_NV;
3792 scratch_memory_req_info.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_BUILD_SCRATCH_NV;
3793 scratch_memory_req_info.accelerationStructure = as_state->acceleration_structure;
3794 DispatchGetAccelerationStructureMemoryRequirementsNV(device, &scratch_memory_req_info,
3795 &as_state->build_scratch_memory_requirements);
3796
3797 VkAccelerationStructureMemoryRequirementsInfoNV update_memory_req_info = {};
3798 update_memory_req_info.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_INFO_NV;
3799 update_memory_req_info.type = VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_UPDATE_SCRATCH_NV;
3800 update_memory_req_info.accelerationStructure = as_state->acceleration_structure;
3801 DispatchGetAccelerationStructureMemoryRequirementsNV(device, &update_memory_req_info,
3802 &as_state->update_scratch_memory_requirements);
Mark Lobodzinski17dc4602020-05-29 07:48:40 -06003803 as_state->allocator = pAllocator;
locke-lunargd556cc32019-09-17 01:21:23 -06003804 accelerationStructureMap[*pAccelerationStructure] = std::move(as_state);
3805}
3806
Jeff Bolz95176d02020-04-01 00:36:16 -05003807void ValidationStateTracker::PostCallRecordCreateAccelerationStructureKHR(VkDevice device,
3808 const VkAccelerationStructureCreateInfoKHR *pCreateInfo,
3809 const VkAllocationCallbacks *pAllocator,
3810 VkAccelerationStructureKHR *pAccelerationStructure,
3811 VkResult result) {
3812 if (VK_SUCCESS != result) return;
sourav parmarcd5fb182020-07-17 12:58:44 -07003813 auto as_state = std::make_shared<ACCELERATION_STRUCTURE_STATE_KHR>(*pAccelerationStructure, pCreateInfo);
Mark Lobodzinski17dc4602020-05-29 07:48:40 -06003814 as_state->allocator = pAllocator;
sourav parmarcd5fb182020-07-17 12:58:44 -07003815 accelerationStructureMap_khr[*pAccelerationStructure] = std::move(as_state);
Jeff Bolz95176d02020-04-01 00:36:16 -05003816}
3817
sourav parmarcd5fb182020-07-17 12:58:44 -07003818void ValidationStateTracker::PostCallRecordCmdBuildAccelerationStructuresKHR(
3819 VkCommandBuffer commandBuffer, uint32_t infoCount, const VkAccelerationStructureBuildGeometryInfoKHR *pInfos,
3820 const VkAccelerationStructureBuildRangeInfoKHR *const *ppBuildRangeInfos) {
3821 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3822 if (cb_state == nullptr) {
3823 return;
3824 }
3825 for (uint32_t i = 0; i < infoCount; ++i) {
3826 ACCELERATION_STRUCTURE_STATE_KHR *dst_as_state = GetAccelerationStructureStateKHR(pInfos[i].dstAccelerationStructure);
3827 ACCELERATION_STRUCTURE_STATE_KHR *src_as_state = GetAccelerationStructureStateKHR(pInfos[i].srcAccelerationStructure);
3828 if (dst_as_state != nullptr) {
3829 dst_as_state->built = true;
3830 dst_as_state->build_info_khr.initialize(&pInfos[i]);
3831 AddCommandBufferBindingAccelerationStructure(cb_state, dst_as_state);
3832 }
3833 if (src_as_state != nullptr) {
3834 AddCommandBufferBindingAccelerationStructure(cb_state, src_as_state);
3835 }
3836 }
3837 cb_state->hasBuildAccelerationStructureCmd = true;
3838}
3839
3840void ValidationStateTracker::PostCallRecordCmdBuildAccelerationStructuresIndirectKHR(
3841 VkCommandBuffer commandBuffer, uint32_t infoCount, const VkAccelerationStructureBuildGeometryInfoKHR *pInfos,
3842 const VkDeviceAddress *pIndirectDeviceAddresses, const uint32_t *pIndirectStrides,
3843 const uint32_t *const *ppMaxPrimitiveCounts) {
3844 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3845 if (cb_state == nullptr) {
3846 return;
3847 }
3848 for (uint32_t i = 0; i < infoCount; ++i) {
3849 ACCELERATION_STRUCTURE_STATE_KHR *dst_as_state = GetAccelerationStructureStateKHR(pInfos[i].dstAccelerationStructure);
3850 ACCELERATION_STRUCTURE_STATE_KHR *src_as_state = GetAccelerationStructureStateKHR(pInfos[i].srcAccelerationStructure);
3851 if (dst_as_state != nullptr) {
3852 dst_as_state->built = true;
3853 dst_as_state->build_info_khr.initialize(&pInfos[i]);
3854 AddCommandBufferBindingAccelerationStructure(cb_state, dst_as_state);
3855 }
3856 if (src_as_state != nullptr) {
3857 AddCommandBufferBindingAccelerationStructure(cb_state, src_as_state);
3858 }
3859 }
3860 cb_state->hasBuildAccelerationStructureCmd = true;
3861}
locke-lunargd556cc32019-09-17 01:21:23 -06003862void ValidationStateTracker::PostCallRecordGetAccelerationStructureMemoryRequirementsNV(
3863 VkDevice device, const VkAccelerationStructureMemoryRequirementsInfoNV *pInfo, VkMemoryRequirements2KHR *pMemoryRequirements) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003864 ACCELERATION_STRUCTURE_STATE *as_state = GetAccelerationStructureStateNV(pInfo->accelerationStructure);
locke-lunargd556cc32019-09-17 01:21:23 -06003865 if (as_state != nullptr) {
3866 if (pInfo->type == VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_OBJECT_NV) {
3867 as_state->memory_requirements = *pMemoryRequirements;
3868 as_state->memory_requirements_checked = true;
3869 } else if (pInfo->type == VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_BUILD_SCRATCH_NV) {
3870 as_state->build_scratch_memory_requirements = *pMemoryRequirements;
3871 as_state->build_scratch_memory_requirements_checked = true;
3872 } else if (pInfo->type == VK_ACCELERATION_STRUCTURE_MEMORY_REQUIREMENTS_TYPE_UPDATE_SCRATCH_NV) {
3873 as_state->update_scratch_memory_requirements = *pMemoryRequirements;
3874 as_state->update_scratch_memory_requirements_checked = true;
3875 }
3876 }
3877}
3878
sourav parmarcd5fb182020-07-17 12:58:44 -07003879void ValidationStateTracker::PostCallRecordBindAccelerationStructureMemoryNV(
3880 VkDevice device, uint32_t bindInfoCount, const VkBindAccelerationStructureMemoryInfoNV *pBindInfos, VkResult result) {
locke-lunargd556cc32019-09-17 01:21:23 -06003881 if (VK_SUCCESS != result) return;
3882 for (uint32_t i = 0; i < bindInfoCount; i++) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003883 const VkBindAccelerationStructureMemoryInfoNV &info = pBindInfos[i];
locke-lunargd556cc32019-09-17 01:21:23 -06003884
sourav parmarcd5fb182020-07-17 12:58:44 -07003885 ACCELERATION_STRUCTURE_STATE *as_state = GetAccelerationStructureStateNV(info.accelerationStructure);
locke-lunargd556cc32019-09-17 01:21:23 -06003886 if (as_state) {
3887 // Track bound memory range information
3888 auto mem_info = GetDevMemState(info.memory);
3889 if (mem_info) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07003890 InsertAccelerationStructureMemoryRange(info.accelerationStructure, mem_info, info.memoryOffset);
locke-lunargd556cc32019-09-17 01:21:23 -06003891 }
3892 // Track objects tied to memory
3893 SetMemBinding(info.memory, as_state, info.memoryOffset,
sourav parmarcd5fb182020-07-17 12:58:44 -07003894 VulkanTypedHandle(info.accelerationStructure, kVulkanObjectTypeAccelerationStructureNV));
locke-lunargd556cc32019-09-17 01:21:23 -06003895
3896 // GPU validation of top level acceleration structure building needs acceleration structure handles.
Jeff Bolz95176d02020-04-01 00:36:16 -05003897 // XXX TODO: Query device address for KHR extension
sourav parmarcd5fb182020-07-17 12:58:44 -07003898 if (enabled[gpu_validation]) {
locke-lunargd556cc32019-09-17 01:21:23 -06003899 DispatchGetAccelerationStructureHandleNV(device, info.accelerationStructure, 8, &as_state->opaque_handle);
3900 }
3901 }
3902 }
3903}
3904
3905void ValidationStateTracker::PostCallRecordCmdBuildAccelerationStructureNV(
3906 VkCommandBuffer commandBuffer, const VkAccelerationStructureInfoNV *pInfo, VkBuffer instanceData, VkDeviceSize instanceOffset,
3907 VkBool32 update, VkAccelerationStructureNV dst, VkAccelerationStructureNV src, VkBuffer scratch, VkDeviceSize scratchOffset) {
3908 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3909 if (cb_state == nullptr) {
3910 return;
3911 }
3912
sourav parmarcd5fb182020-07-17 12:58:44 -07003913 ACCELERATION_STRUCTURE_STATE *dst_as_state = GetAccelerationStructureStateNV(dst);
3914 ACCELERATION_STRUCTURE_STATE *src_as_state = GetAccelerationStructureStateNV(src);
locke-lunargd556cc32019-09-17 01:21:23 -06003915 if (dst_as_state != nullptr) {
3916 dst_as_state->built = true;
3917 dst_as_state->build_info.initialize(pInfo);
3918 AddCommandBufferBindingAccelerationStructure(cb_state, dst_as_state);
3919 }
3920 if (src_as_state != nullptr) {
3921 AddCommandBufferBindingAccelerationStructure(cb_state, src_as_state);
3922 }
3923 cb_state->hasBuildAccelerationStructureCmd = true;
3924}
3925
3926void ValidationStateTracker::PostCallRecordCmdCopyAccelerationStructureNV(VkCommandBuffer commandBuffer,
3927 VkAccelerationStructureNV dst,
3928 VkAccelerationStructureNV src,
3929 VkCopyAccelerationStructureModeNV mode) {
3930 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3931 if (cb_state) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003932 ACCELERATION_STRUCTURE_STATE *src_as_state = GetAccelerationStructureStateNV(src);
3933 ACCELERATION_STRUCTURE_STATE *dst_as_state = GetAccelerationStructureStateNV(dst);
locke-lunargd556cc32019-09-17 01:21:23 -06003934 if (dst_as_state != nullptr && src_as_state != nullptr) {
3935 dst_as_state->built = true;
3936 dst_as_state->build_info = src_as_state->build_info;
3937 AddCommandBufferBindingAccelerationStructure(cb_state, dst_as_state);
3938 AddCommandBufferBindingAccelerationStructure(cb_state, src_as_state);
3939 }
3940 }
3941}
3942
Jeff Bolz95176d02020-04-01 00:36:16 -05003943void ValidationStateTracker::PreCallRecordDestroyAccelerationStructureKHR(VkDevice device,
3944 VkAccelerationStructureKHR accelerationStructure,
3945 const VkAllocationCallbacks *pAllocator) {
locke-lunargd556cc32019-09-17 01:21:23 -06003946 if (!accelerationStructure) return;
sourav parmarcd5fb182020-07-17 12:58:44 -07003947 auto *as_state = GetAccelerationStructureStateKHR(accelerationStructure);
locke-lunargd556cc32019-09-17 01:21:23 -06003948 if (as_state) {
Jeff Bolz95176d02020-04-01 00:36:16 -05003949 const VulkanTypedHandle obj_struct(accelerationStructure, kVulkanObjectTypeAccelerationStructureKHR);
locke-lunargd556cc32019-09-17 01:21:23 -06003950 InvalidateCommandBuffers(as_state->cb_bindings, obj_struct);
3951 for (auto mem_binding : as_state->GetBoundMemory()) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003952 RemoveMemoryRange(obj_struct, mem_binding);
locke-lunargd556cc32019-09-17 01:21:23 -06003953 }
3954 ClearMemoryObjectBindings(obj_struct);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05003955 as_state->destroyed = true;
sourav parmarcd5fb182020-07-17 12:58:44 -07003956 accelerationStructureMap_khr.erase(accelerationStructure);
locke-lunargd556cc32019-09-17 01:21:23 -06003957 }
3958}
3959
Jeff Bolz95176d02020-04-01 00:36:16 -05003960void ValidationStateTracker::PreCallRecordDestroyAccelerationStructureNV(VkDevice device,
3961 VkAccelerationStructureNV accelerationStructure,
3962 const VkAllocationCallbacks *pAllocator) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003963 if (!accelerationStructure) return;
3964 auto *as_state = GetAccelerationStructureStateNV(accelerationStructure);
3965 if (as_state) {
3966 const VulkanTypedHandle obj_struct(accelerationStructure, kVulkanObjectTypeAccelerationStructureNV);
3967 InvalidateCommandBuffers(as_state->cb_bindings, obj_struct);
3968 for (auto mem_binding : as_state->GetBoundMemory()) {
3969 RemoveMemoryRange(obj_struct, mem_binding);
3970 }
3971 ClearMemoryObjectBindings(obj_struct);
3972 as_state->destroyed = true;
3973 accelerationStructureMap.erase(accelerationStructure);
3974 }
Jeff Bolz95176d02020-04-01 00:36:16 -05003975}
3976
Chris Mayer9ded5eb2019-09-19 16:33:26 +02003977void ValidationStateTracker::PreCallRecordCmdSetViewportWScalingNV(VkCommandBuffer commandBuffer, uint32_t firstViewport,
3978 uint32_t viewportCount,
3979 const VkViewportWScalingNV *pViewportWScalings) {
3980 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3981 cb_state->status |= CBSTATUS_VIEWPORT_W_SCALING_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06003982 cb_state->static_status &= ~CBSTATUS_VIEWPORT_W_SCALING_SET;
Chris Mayer9ded5eb2019-09-19 16:33:26 +02003983}
3984
locke-lunargd556cc32019-09-17 01:21:23 -06003985void ValidationStateTracker::PreCallRecordCmdSetLineWidth(VkCommandBuffer commandBuffer, float lineWidth) {
3986 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3987 cb_state->status |= CBSTATUS_LINE_WIDTH_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06003988 cb_state->static_status &= ~CBSTATUS_LINE_WIDTH_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06003989}
3990
3991void ValidationStateTracker::PreCallRecordCmdSetLineStippleEXT(VkCommandBuffer commandBuffer, uint32_t lineStippleFactor,
3992 uint16_t lineStipplePattern) {
3993 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
3994 cb_state->status |= CBSTATUS_LINE_STIPPLE_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06003995 cb_state->static_status &= ~CBSTATUS_LINE_STIPPLE_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06003996}
3997
3998void ValidationStateTracker::PreCallRecordCmdSetDepthBias(VkCommandBuffer commandBuffer, float depthBiasConstantFactor,
3999 float depthBiasClamp, float depthBiasSlopeFactor) {
4000 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4001 cb_state->status |= CBSTATUS_DEPTH_BIAS_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06004002 cb_state->static_status &= ~CBSTATUS_DEPTH_BIAS_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06004003}
4004
Lionel Landwerlinc7420912019-05-23 00:33:42 +01004005void ValidationStateTracker::PreCallRecordCmdSetScissor(VkCommandBuffer commandBuffer, uint32_t firstScissor, uint32_t scissorCount,
4006 const VkRect2D *pScissors) {
4007 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4008 cb_state->scissorMask |= ((1u << scissorCount) - 1u) << firstScissor;
4009 cb_state->status |= CBSTATUS_SCISSOR_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06004010 cb_state->static_status &= ~CBSTATUS_SCISSOR_SET;
Lionel Landwerlinc7420912019-05-23 00:33:42 +01004011}
4012
locke-lunargd556cc32019-09-17 01:21:23 -06004013void ValidationStateTracker::PreCallRecordCmdSetBlendConstants(VkCommandBuffer commandBuffer, const float blendConstants[4]) {
4014 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4015 cb_state->status |= CBSTATUS_BLEND_CONSTANTS_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06004016 cb_state->static_status &= ~CBSTATUS_BLEND_CONSTANTS_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06004017}
4018
4019void ValidationStateTracker::PreCallRecordCmdSetDepthBounds(VkCommandBuffer commandBuffer, float minDepthBounds,
4020 float maxDepthBounds) {
4021 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4022 cb_state->status |= CBSTATUS_DEPTH_BOUNDS_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06004023 cb_state->static_status &= ~CBSTATUS_DEPTH_BOUNDS_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06004024}
4025
4026void ValidationStateTracker::PreCallRecordCmdSetStencilCompareMask(VkCommandBuffer commandBuffer, VkStencilFaceFlags faceMask,
4027 uint32_t compareMask) {
4028 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4029 cb_state->status |= CBSTATUS_STENCIL_READ_MASK_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06004030 cb_state->static_status &= ~CBSTATUS_STENCIL_READ_MASK_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06004031}
4032
4033void ValidationStateTracker::PreCallRecordCmdSetStencilWriteMask(VkCommandBuffer commandBuffer, VkStencilFaceFlags faceMask,
4034 uint32_t writeMask) {
4035 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4036 cb_state->status |= CBSTATUS_STENCIL_WRITE_MASK_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06004037 cb_state->static_status &= ~CBSTATUS_STENCIL_WRITE_MASK_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06004038}
4039
4040void ValidationStateTracker::PreCallRecordCmdSetStencilReference(VkCommandBuffer commandBuffer, VkStencilFaceFlags faceMask,
4041 uint32_t reference) {
4042 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4043 cb_state->status |= CBSTATUS_STENCIL_REFERENCE_SET;
Piers Daniell39842ee2020-07-10 16:42:33 -06004044 cb_state->static_status &= ~CBSTATUS_STENCIL_REFERENCE_SET;
locke-lunargd556cc32019-09-17 01:21:23 -06004045}
4046
4047// Update pipeline_layout bind points applying the "Pipeline Layout Compatibility" rules.
4048// One of pDescriptorSets or push_descriptor_set should be nullptr, indicating whether this
4049// is called for CmdBindDescriptorSets or CmdPushDescriptorSet.
4050void ValidationStateTracker::UpdateLastBoundDescriptorSets(CMD_BUFFER_STATE *cb_state, VkPipelineBindPoint pipeline_bind_point,
4051 const PIPELINE_LAYOUT_STATE *pipeline_layout, uint32_t first_set,
4052 uint32_t set_count, const VkDescriptorSet *pDescriptorSets,
4053 cvdescriptorset::DescriptorSet *push_descriptor_set,
4054 uint32_t dynamic_offset_count, const uint32_t *p_dynamic_offsets) {
4055 assert((pDescriptorSets == nullptr) ^ (push_descriptor_set == nullptr));
4056 // Defensive
4057 assert(pipeline_layout);
4058 if (!pipeline_layout) return;
4059
4060 uint32_t required_size = first_set + set_count;
4061 const uint32_t last_binding_index = required_size - 1;
4062 assert(last_binding_index < pipeline_layout->compat_for_set.size());
4063
4064 // Some useful shorthand
locke-lunargb8d7a7a2020-10-25 16:01:52 -06004065 const auto lv_bind_point = ConvertToLvlBindPoint(pipeline_bind_point);
4066 auto &last_bound = cb_state->lastBound[lv_bind_point];
locke-lunargd556cc32019-09-17 01:21:23 -06004067 auto &pipe_compat_ids = pipeline_layout->compat_for_set;
4068 const uint32_t current_size = static_cast<uint32_t>(last_bound.per_set.size());
4069
4070 // We need this three times in this function, but nowhere else
4071 auto push_descriptor_cleanup = [&last_bound](const cvdescriptorset::DescriptorSet *ds) -> bool {
4072 if (ds && ds->IsPushDescriptor()) {
4073 assert(ds == last_bound.push_descriptor_set.get());
4074 last_bound.push_descriptor_set = nullptr;
4075 return true;
4076 }
4077 return false;
4078 };
4079
4080 // Clean up the "disturbed" before and after the range to be set
4081 if (required_size < current_size) {
4082 if (last_bound.per_set[last_binding_index].compat_id_for_set != pipe_compat_ids[last_binding_index]) {
4083 // We're disturbing those after last, we'll shrink below, but first need to check for and cleanup the push_descriptor
4084 for (auto set_idx = required_size; set_idx < current_size; ++set_idx) {
4085 if (push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set)) break;
4086 }
4087 } else {
4088 // We're not disturbing past last, so leave the upper binding data alone.
4089 required_size = current_size;
4090 }
4091 }
4092
4093 // We resize if we need more set entries or if those past "last" are disturbed
4094 if (required_size != current_size) {
4095 last_bound.per_set.resize(required_size);
4096 }
4097
4098 // For any previously bound sets, need to set them to "invalid" if they were disturbed by this update
4099 for (uint32_t set_idx = 0; set_idx < first_set; ++set_idx) {
4100 if (last_bound.per_set[set_idx].compat_id_for_set != pipe_compat_ids[set_idx]) {
4101 push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set);
4102 last_bound.per_set[set_idx].bound_descriptor_set = nullptr;
4103 last_bound.per_set[set_idx].dynamicOffsets.clear();
4104 last_bound.per_set[set_idx].compat_id_for_set = pipe_compat_ids[set_idx];
4105 }
4106 }
4107
4108 // Now update the bound sets with the input sets
4109 const uint32_t *input_dynamic_offsets = p_dynamic_offsets; // "read" pointer for dynamic offset data
4110 for (uint32_t input_idx = 0; input_idx < set_count; input_idx++) {
4111 auto set_idx = input_idx + first_set; // set_idx is index within layout, input_idx is index within input descriptor sets
4112 cvdescriptorset::DescriptorSet *descriptor_set =
4113 push_descriptor_set ? push_descriptor_set : GetSetNode(pDescriptorSets[input_idx]);
4114
4115 // Record binding (or push)
4116 if (descriptor_set != last_bound.push_descriptor_set.get()) {
4117 // Only cleanup the push descriptors if they aren't the currently used set.
4118 push_descriptor_cleanup(last_bound.per_set[set_idx].bound_descriptor_set);
4119 }
4120 last_bound.per_set[set_idx].bound_descriptor_set = descriptor_set;
4121 last_bound.per_set[set_idx].compat_id_for_set = pipe_compat_ids[set_idx]; // compat ids are canonical *per* set index
4122
4123 if (descriptor_set) {
4124 auto set_dynamic_descriptor_count = descriptor_set->GetDynamicDescriptorCount();
4125 // TODO: Add logic for tracking push_descriptor offsets (here or in caller)
4126 if (set_dynamic_descriptor_count && input_dynamic_offsets) {
4127 const uint32_t *end_offset = input_dynamic_offsets + set_dynamic_descriptor_count;
4128 last_bound.per_set[set_idx].dynamicOffsets = std::vector<uint32_t>(input_dynamic_offsets, end_offset);
4129 input_dynamic_offsets = end_offset;
4130 assert(input_dynamic_offsets <= (p_dynamic_offsets + dynamic_offset_count));
4131 } else {
4132 last_bound.per_set[set_idx].dynamicOffsets.clear();
4133 }
4134 if (!descriptor_set->IsPushDescriptor()) {
4135 // Can't cache validation of push_descriptors
4136 cb_state->validated_descriptor_sets.insert(descriptor_set);
4137 }
4138 }
4139 }
4140}
4141
4142// Update the bound state for the bind point, including the effects of incompatible pipeline layouts
4143void ValidationStateTracker::PreCallRecordCmdBindDescriptorSets(VkCommandBuffer commandBuffer,
4144 VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout,
4145 uint32_t firstSet, uint32_t setCount,
4146 const VkDescriptorSet *pDescriptorSets, uint32_t dynamicOffsetCount,
4147 const uint32_t *pDynamicOffsets) {
4148 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4149 auto pipeline_layout = GetPipelineLayout(layout);
4150
4151 // Resize binding arrays
4152 uint32_t last_set_index = firstSet + setCount - 1;
locke-lunargb8d7a7a2020-10-25 16:01:52 -06004153 const auto lv_bind_point = ConvertToLvlBindPoint(pipelineBindPoint);
4154 if (last_set_index >= cb_state->lastBound[lv_bind_point].per_set.size()) {
4155 cb_state->lastBound[lv_bind_point].per_set.resize(last_set_index + 1);
locke-lunargd556cc32019-09-17 01:21:23 -06004156 }
4157
4158 UpdateLastBoundDescriptorSets(cb_state, pipelineBindPoint, pipeline_layout, firstSet, setCount, pDescriptorSets, nullptr,
4159 dynamicOffsetCount, pDynamicOffsets);
locke-lunargb8d7a7a2020-10-25 16:01:52 -06004160 cb_state->lastBound[lv_bind_point].pipeline_layout = layout;
locke-lunargd556cc32019-09-17 01:21:23 -06004161 ResetCommandBufferPushConstantDataIfIncompatible(cb_state, layout);
locke-lunargb8d7a7a2020-10-25 16:01:52 -06004162 UpdateSamplerDescriptorsUsedByImage(cb_state->lastBound[lv_bind_point]);
locke-lunargd556cc32019-09-17 01:21:23 -06004163}
4164
4165void ValidationStateTracker::RecordCmdPushDescriptorSetState(CMD_BUFFER_STATE *cb_state, VkPipelineBindPoint pipelineBindPoint,
4166 VkPipelineLayout layout, uint32_t set, uint32_t descriptorWriteCount,
4167 const VkWriteDescriptorSet *pDescriptorWrites) {
4168 const auto &pipeline_layout = GetPipelineLayout(layout);
4169 // Short circuit invalid updates
4170 if (!pipeline_layout || (set >= pipeline_layout->set_layouts.size()) || !pipeline_layout->set_layouts[set] ||
4171 !pipeline_layout->set_layouts[set]->IsPushDescriptor())
4172 return;
4173
4174 // We need a descriptor set to update the bindings with, compatible with the passed layout
4175 const auto dsl = pipeline_layout->set_layouts[set];
locke-lunargb8d7a7a2020-10-25 16:01:52 -06004176 const auto lv_bind_point = ConvertToLvlBindPoint(pipelineBindPoint);
4177 auto &last_bound = cb_state->lastBound[lv_bind_point];
locke-lunargd556cc32019-09-17 01:21:23 -06004178 auto &push_descriptor_set = last_bound.push_descriptor_set;
4179 // If we are disturbing the current push_desriptor_set clear it
4180 if (!push_descriptor_set || !CompatForSet(set, last_bound, pipeline_layout->compat_for_set)) {
John Zulaufd2c3dae2019-12-12 11:02:17 -07004181 last_bound.UnbindAndResetPushDescriptorSet(new cvdescriptorset::DescriptorSet(0, nullptr, dsl, 0, this));
locke-lunargd556cc32019-09-17 01:21:23 -06004182 }
4183
4184 UpdateLastBoundDescriptorSets(cb_state, pipelineBindPoint, pipeline_layout, set, 1, nullptr, push_descriptor_set.get(), 0,
4185 nullptr);
4186 last_bound.pipeline_layout = layout;
4187
4188 // Now that we have either the new or extant push_descriptor set ... do the write updates against it
Jeff Bolz41a1ced2019-10-11 11:40:49 -05004189 push_descriptor_set->PerformPushDescriptorsUpdate(this, descriptorWriteCount, pDescriptorWrites);
locke-lunargd556cc32019-09-17 01:21:23 -06004190}
4191
4192void ValidationStateTracker::PreCallRecordCmdPushDescriptorSetKHR(VkCommandBuffer commandBuffer,
4193 VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout,
4194 uint32_t set, uint32_t descriptorWriteCount,
4195 const VkWriteDescriptorSet *pDescriptorWrites) {
4196 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4197 RecordCmdPushDescriptorSetState(cb_state, pipelineBindPoint, layout, set, descriptorWriteCount, pDescriptorWrites);
4198}
4199
Tony-LunarG6bb1d0c2019-09-23 10:39:25 -06004200void ValidationStateTracker::PostCallRecordCmdPushConstants(VkCommandBuffer commandBuffer, VkPipelineLayout layout,
4201 VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size,
4202 const void *pValues) {
4203 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4204 if (cb_state != nullptr) {
4205 ResetCommandBufferPushConstantDataIfIncompatible(cb_state, layout);
4206
4207 auto &push_constant_data = cb_state->push_constant_data;
4208 assert((offset + size) <= static_cast<uint32_t>(push_constant_data.size()));
4209 std::memcpy(push_constant_data.data() + offset, pValues, static_cast<std::size_t>(size));
locke-lunargde3f0fa2020-09-10 11:55:31 -06004210 cb_state->push_constant_pipeline_layout_set = layout;
4211
4212 auto flags = stageFlags;
4213 uint32_t bit_shift = 0;
4214 while (flags) {
4215 if (flags & 1) {
4216 VkShaderStageFlagBits flag = static_cast<VkShaderStageFlagBits>(1 << bit_shift);
4217 const auto it = cb_state->push_constant_data_update.find(flag);
4218
4219 if (it != cb_state->push_constant_data_update.end()) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -06004220 std::memset(it->second.data() + offset, PC_Byte_Updated, static_cast<std::size_t>(size));
locke-lunargde3f0fa2020-09-10 11:55:31 -06004221 }
4222 }
4223 flags = flags >> 1;
4224 ++bit_shift;
4225 }
Tony-LunarG6bb1d0c2019-09-23 10:39:25 -06004226 }
4227}
4228
locke-lunargd556cc32019-09-17 01:21:23 -06004229void ValidationStateTracker::PreCallRecordCmdBindIndexBuffer(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4230 VkIndexType indexType) {
locke-lunargd556cc32019-09-17 01:21:23 -06004231 auto cb_state = GetCBState(commandBuffer);
4232
4233 cb_state->status |= CBSTATUS_INDEX_BUFFER_BOUND;
Piers Daniell39842ee2020-07-10 16:42:33 -06004234 cb_state->static_status &= ~CBSTATUS_INDEX_BUFFER_BOUND;
locke-lunarg1ae57d62020-11-18 10:49:19 -07004235 cb_state->index_buffer_binding.buffer_state = GetShared<BUFFER_STATE>(buffer);
4236 cb_state->index_buffer_binding.size = cb_state->index_buffer_binding.buffer_state->createInfo.size;
locke-lunargd556cc32019-09-17 01:21:23 -06004237 cb_state->index_buffer_binding.offset = offset;
4238 cb_state->index_buffer_binding.index_type = indexType;
4239 // Add binding for this index buffer to this commandbuffer
locke-lunarg1ae57d62020-11-18 10:49:19 -07004240 AddCommandBufferBindingBuffer(cb_state, cb_state->index_buffer_binding.buffer_state.get());
locke-lunargd556cc32019-09-17 01:21:23 -06004241}
4242
4243void ValidationStateTracker::PreCallRecordCmdBindVertexBuffers(VkCommandBuffer commandBuffer, uint32_t firstBinding,
4244 uint32_t bindingCount, const VkBuffer *pBuffers,
4245 const VkDeviceSize *pOffsets) {
4246 auto cb_state = GetCBState(commandBuffer);
4247
4248 uint32_t end = firstBinding + bindingCount;
4249 if (cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings.size() < end) {
4250 cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings.resize(end);
4251 }
4252
4253 for (uint32_t i = 0; i < bindingCount; ++i) {
4254 auto &vertex_buffer_binding = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings[i + firstBinding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07004255 vertex_buffer_binding.buffer_state = GetShared<BUFFER_STATE>(pBuffers[i]);
locke-lunargd556cc32019-09-17 01:21:23 -06004256 vertex_buffer_binding.offset = pOffsets[i];
Piers Daniell39842ee2020-07-10 16:42:33 -06004257 vertex_buffer_binding.size = VK_WHOLE_SIZE;
4258 vertex_buffer_binding.stride = 0;
locke-lunargd556cc32019-09-17 01:21:23 -06004259 // Add binding for this vertex buffer to this commandbuffer
Jeff Bolz165818a2020-05-08 11:19:03 -05004260 if (pBuffers[i]) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07004261 AddCommandBufferBindingBuffer(cb_state, vertex_buffer_binding.buffer_state.get());
Jeff Bolz165818a2020-05-08 11:19:03 -05004262 }
locke-lunargd556cc32019-09-17 01:21:23 -06004263 }
4264}
4265
4266void ValidationStateTracker::PostCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer,
4267 VkDeviceSize dstOffset, VkDeviceSize dataSize, const void *pData) {
4268 auto cb_state = GetCBState(commandBuffer);
4269 auto dst_buffer_state = GetBufferState(dstBuffer);
4270
4271 // Update bindings between buffer and cmd buffer
4272 AddCommandBufferBindingBuffer(cb_state, dst_buffer_state);
4273}
4274
Jeff Bolz310775c2019-10-09 00:46:33 -05004275bool ValidationStateTracker::SetEventStageMask(VkEvent event, VkPipelineStageFlags stageMask,
4276 EventToStageMap *localEventToStageMap) {
4277 (*localEventToStageMap)[event] = stageMask;
locke-lunargd556cc32019-09-17 01:21:23 -06004278 return false;
4279}
4280
4281void ValidationStateTracker::PreCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4282 VkPipelineStageFlags stageMask) {
4283 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4284 auto event_state = GetEventState(event);
4285 if (event_state) {
Jeff Bolzadbfa852019-10-04 13:53:30 -05004286 AddCommandBufferBinding(event_state->cb_bindings, VulkanTypedHandle(event, kVulkanObjectTypeEvent, event_state), cb_state);
locke-lunargd556cc32019-09-17 01:21:23 -06004287 }
4288 cb_state->events.push_back(event);
4289 if (!cb_state->waitedEvents.count(event)) {
4290 cb_state->writeEventsBeforeWait.push_back(event);
4291 }
Jeff Bolz310775c2019-10-09 00:46:33 -05004292 cb_state->eventUpdates.emplace_back(
4293 [event, stageMask](const ValidationStateTracker *device_data, bool do_validate, EventToStageMap *localEventToStageMap) {
4294 return SetEventStageMask(event, stageMask, localEventToStageMap);
4295 });
locke-lunargd556cc32019-09-17 01:21:23 -06004296}
4297
4298void ValidationStateTracker::PreCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4299 VkPipelineStageFlags stageMask) {
4300 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4301 auto event_state = GetEventState(event);
4302 if (event_state) {
Jeff Bolzadbfa852019-10-04 13:53:30 -05004303 AddCommandBufferBinding(event_state->cb_bindings, VulkanTypedHandle(event, kVulkanObjectTypeEvent, event_state), cb_state);
locke-lunargd556cc32019-09-17 01:21:23 -06004304 }
4305 cb_state->events.push_back(event);
4306 if (!cb_state->waitedEvents.count(event)) {
4307 cb_state->writeEventsBeforeWait.push_back(event);
4308 }
4309
4310 cb_state->eventUpdates.emplace_back(
Jeff Bolz310775c2019-10-09 00:46:33 -05004311 [event](const ValidationStateTracker *, bool do_validate, EventToStageMap *localEventToStageMap) {
4312 return SetEventStageMask(event, VkPipelineStageFlags(0), localEventToStageMap);
4313 });
locke-lunargd556cc32019-09-17 01:21:23 -06004314}
4315
4316void ValidationStateTracker::PreCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4317 VkPipelineStageFlags sourceStageMask, VkPipelineStageFlags dstStageMask,
4318 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4319 uint32_t bufferMemoryBarrierCount,
4320 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4321 uint32_t imageMemoryBarrierCount,
4322 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4323 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4324 for (uint32_t i = 0; i < eventCount; ++i) {
4325 auto event_state = GetEventState(pEvents[i]);
4326 if (event_state) {
Jeff Bolzadbfa852019-10-04 13:53:30 -05004327 AddCommandBufferBinding(event_state->cb_bindings, VulkanTypedHandle(pEvents[i], kVulkanObjectTypeEvent, event_state),
4328 cb_state);
locke-lunargd556cc32019-09-17 01:21:23 -06004329 }
4330 cb_state->waitedEvents.insert(pEvents[i]);
4331 cb_state->events.push_back(pEvents[i]);
4332 }
4333}
4334
Jeff Bolz310775c2019-10-09 00:46:33 -05004335bool ValidationStateTracker::SetQueryState(QueryObject object, QueryState value, QueryMap *localQueryToStateMap) {
4336 (*localQueryToStateMap)[object] = value;
4337 return false;
4338}
4339
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02004340bool ValidationStateTracker::SetQueryStateMulti(VkQueryPool queryPool, uint32_t firstQuery, uint32_t queryCount, uint32_t perfPass,
4341 QueryState value, QueryMap *localQueryToStateMap) {
Jeff Bolz310775c2019-10-09 00:46:33 -05004342 for (uint32_t i = 0; i < queryCount; i++) {
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02004343 QueryObject object = QueryObject(QueryObject(queryPool, firstQuery + i), perfPass);
Jeff Bolz310775c2019-10-09 00:46:33 -05004344 (*localQueryToStateMap)[object] = value;
locke-lunargd556cc32019-09-17 01:21:23 -06004345 }
4346 return false;
4347}
4348
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02004349QueryState ValidationStateTracker::GetQueryState(const QueryMap *localQueryToStateMap, VkQueryPool queryPool, uint32_t queryIndex,
4350 uint32_t perfPass) const {
4351 QueryObject query = QueryObject(QueryObject(queryPool, queryIndex), perfPass);
locke-lunargd556cc32019-09-17 01:21:23 -06004352
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02004353 auto iter = localQueryToStateMap->find(query);
4354 if (iter != localQueryToStateMap->end()) return iter->second;
Jeff Bolz310775c2019-10-09 00:46:33 -05004355
Jeff Bolz310775c2019-10-09 00:46:33 -05004356 return QUERYSTATE_UNKNOWN;
locke-lunargd556cc32019-09-17 01:21:23 -06004357}
4358
4359void ValidationStateTracker::RecordCmdBeginQuery(CMD_BUFFER_STATE *cb_state, const QueryObject &query_obj) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06004360 if (disabled[query_validation]) return;
locke-lunargd556cc32019-09-17 01:21:23 -06004361 cb_state->activeQueries.insert(query_obj);
4362 cb_state->startedQueries.insert(query_obj);
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02004363 cb_state->queryUpdates.emplace_back([query_obj](const ValidationStateTracker *device_data, bool do_validate,
4364 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass,
4365 QueryMap *localQueryToStateMap) {
4366 SetQueryState(QueryObject(query_obj, perfQueryPass), QUERYSTATE_RUNNING, localQueryToStateMap);
4367 return false;
4368 });
Jeff Bolzadbfa852019-10-04 13:53:30 -05004369 auto pool_state = GetQueryPoolState(query_obj.pool);
4370 AddCommandBufferBinding(pool_state->cb_bindings, VulkanTypedHandle(query_obj.pool, kVulkanObjectTypeQueryPool, pool_state),
4371 cb_state);
locke-lunargd556cc32019-09-17 01:21:23 -06004372}
4373
4374void ValidationStateTracker::PostCallRecordCmdBeginQuery(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t slot,
4375 VkFlags flags) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06004376 if (disabled[query_validation]) return;
locke-lunargd556cc32019-09-17 01:21:23 -06004377 QueryObject query = {queryPool, slot};
4378 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4379 RecordCmdBeginQuery(cb_state, query);
4380}
4381
4382void ValidationStateTracker::RecordCmdEndQuery(CMD_BUFFER_STATE *cb_state, const QueryObject &query_obj) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06004383 if (disabled[query_validation]) return;
locke-lunargd556cc32019-09-17 01:21:23 -06004384 cb_state->activeQueries.erase(query_obj);
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02004385 cb_state->queryUpdates.emplace_back([query_obj](const ValidationStateTracker *device_data, bool do_validate,
4386 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass,
4387 QueryMap *localQueryToStateMap) {
4388 return SetQueryState(QueryObject(query_obj, perfQueryPass), QUERYSTATE_ENDED, localQueryToStateMap);
4389 });
Jeff Bolzadbfa852019-10-04 13:53:30 -05004390 auto pool_state = GetQueryPoolState(query_obj.pool);
4391 AddCommandBufferBinding(pool_state->cb_bindings, VulkanTypedHandle(query_obj.pool, kVulkanObjectTypeQueryPool, pool_state),
4392 cb_state);
locke-lunargd556cc32019-09-17 01:21:23 -06004393}
4394
4395void ValidationStateTracker::PostCallRecordCmdEndQuery(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t slot) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06004396 if (disabled[query_validation]) return;
locke-lunargd556cc32019-09-17 01:21:23 -06004397 QueryObject query_obj = {queryPool, slot};
4398 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4399 RecordCmdEndQuery(cb_state, query_obj);
4400}
4401
4402void ValidationStateTracker::PostCallRecordCmdResetQueryPool(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4403 uint32_t firstQuery, uint32_t queryCount) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06004404 if (disabled[query_validation]) return;
locke-lunargd556cc32019-09-17 01:21:23 -06004405 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4406
Lionel Landwerlinb1e5a422020-02-18 16:49:09 +02004407 for (uint32_t slot = firstQuery; slot < (firstQuery + queryCount); slot++) {
4408 QueryObject query = {queryPool, slot};
4409 cb_state->resetQueries.insert(query);
4410 }
4411
Jeff Bolz310775c2019-10-09 00:46:33 -05004412 cb_state->queryUpdates.emplace_back([queryPool, firstQuery, queryCount](const ValidationStateTracker *device_data,
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02004413 bool do_validate, VkQueryPool &firstPerfQueryPool,
4414 uint32_t perfQueryPass,
4415 QueryMap *localQueryToStateMap) {
4416 return SetQueryStateMulti(queryPool, firstQuery, queryCount, perfQueryPass, QUERYSTATE_RESET, localQueryToStateMap);
locke-lunargd556cc32019-09-17 01:21:23 -06004417 });
Jeff Bolzadbfa852019-10-04 13:53:30 -05004418 auto pool_state = GetQueryPoolState(queryPool);
4419 AddCommandBufferBinding(pool_state->cb_bindings, VulkanTypedHandle(queryPool, kVulkanObjectTypeQueryPool, pool_state),
locke-lunargd556cc32019-09-17 01:21:23 -06004420 cb_state);
4421}
4422
4423void ValidationStateTracker::PostCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4424 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4425 VkDeviceSize dstOffset, VkDeviceSize stride,
4426 VkQueryResultFlags flags) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06004427 if (disabled[query_validation]) return;
locke-lunargd556cc32019-09-17 01:21:23 -06004428 auto cb_state = GetCBState(commandBuffer);
4429 auto dst_buff_state = GetBufferState(dstBuffer);
4430 AddCommandBufferBindingBuffer(cb_state, dst_buff_state);
Jeff Bolzadbfa852019-10-04 13:53:30 -05004431 auto pool_state = GetQueryPoolState(queryPool);
4432 AddCommandBufferBinding(pool_state->cb_bindings, VulkanTypedHandle(queryPool, kVulkanObjectTypeQueryPool, pool_state),
locke-lunargd556cc32019-09-17 01:21:23 -06004433 cb_state);
4434}
4435
4436void ValidationStateTracker::PostCallRecordCmdWriteTimestamp(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4437 VkQueryPool queryPool, uint32_t slot) {
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06004438 if (disabled[query_validation]) return;
locke-lunargd556cc32019-09-17 01:21:23 -06004439 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
Jeff Bolzadbfa852019-10-04 13:53:30 -05004440 auto pool_state = GetQueryPoolState(queryPool);
4441 AddCommandBufferBinding(pool_state->cb_bindings, VulkanTypedHandle(queryPool, kVulkanObjectTypeQueryPool, pool_state),
locke-lunargd556cc32019-09-17 01:21:23 -06004442 cb_state);
4443 QueryObject query = {queryPool, slot};
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02004444 cb_state->queryUpdates.emplace_back([query](const ValidationStateTracker *device_data, bool do_validate,
4445 VkQueryPool &firstPerfQueryPool, uint32_t perfQueryPass,
4446 QueryMap *localQueryToStateMap) {
4447 return SetQueryState(QueryObject(query, perfQueryPass), QUERYSTATE_ENDED, localQueryToStateMap);
4448 });
locke-lunargd556cc32019-09-17 01:21:23 -06004449}
4450
4451void ValidationStateTracker::PostCallRecordCreateFramebuffer(VkDevice device, const VkFramebufferCreateInfo *pCreateInfo,
4452 const VkAllocationCallbacks *pAllocator, VkFramebuffer *pFramebuffer,
4453 VkResult result) {
4454 if (VK_SUCCESS != result) return;
4455 // Shadow create info and store in map
Jeff Bolz6ae39612019-10-11 20:57:36 -05004456 auto fb_state = std::make_shared<FRAMEBUFFER_STATE>(*pFramebuffer, pCreateInfo, GetRenderPassShared(pCreateInfo->renderPass));
locke-lunargd556cc32019-09-17 01:21:23 -06004457
4458 if ((pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT_KHR) == 0) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07004459 fb_state->attachments_view_state.resize(pCreateInfo->attachmentCount);
4460
locke-lunargd556cc32019-09-17 01:21:23 -06004461 for (uint32_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07004462 fb_state->attachments_view_state[i] = GetShared<IMAGE_VIEW_STATE>(pCreateInfo->pAttachments[i]);
locke-lunargd556cc32019-09-17 01:21:23 -06004463 }
4464 }
4465 frameBufferMap[*pFramebuffer] = std::move(fb_state);
4466}
4467
4468void ValidationStateTracker::RecordRenderPassDAG(RenderPassCreateVersion rp_version, const VkRenderPassCreateInfo2KHR *pCreateInfo,
4469 RENDER_PASS_STATE *render_pass) {
4470 auto &subpass_to_node = render_pass->subpassToNode;
4471 subpass_to_node.resize(pCreateInfo->subpassCount);
4472 auto &self_dependencies = render_pass->self_dependencies;
4473 self_dependencies.resize(pCreateInfo->subpassCount);
John Zulauf4aff5d92020-02-21 08:29:35 -07004474 auto &subpass_dependencies = render_pass->subpass_dependencies;
4475 subpass_dependencies.resize(pCreateInfo->subpassCount);
locke-lunargd556cc32019-09-17 01:21:23 -06004476
4477 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
4478 subpass_to_node[i].pass = i;
4479 self_dependencies[i].clear();
John Zulauf4aff5d92020-02-21 08:29:35 -07004480 subpass_dependencies[i].pass = i;
locke-lunargd556cc32019-09-17 01:21:23 -06004481 }
4482 for (uint32_t i = 0; i < pCreateInfo->dependencyCount; ++i) {
John Zulaufbaea94f2020-09-15 17:55:16 -06004483 const auto &dependency = pCreateInfo->pDependencies[i];
John Zulauf4aff5d92020-02-21 08:29:35 -07004484 const auto srcSubpass = dependency.srcSubpass;
4485 const auto dstSubpass = dependency.dstSubpass;
locke-lunargd556cc32019-09-17 01:21:23 -06004486 if ((dependency.srcSubpass != VK_SUBPASS_EXTERNAL) && (dependency.dstSubpass != VK_SUBPASS_EXTERNAL)) {
4487 if (dependency.srcSubpass == dependency.dstSubpass) {
4488 self_dependencies[dependency.srcSubpass].push_back(i);
4489 } else {
4490 subpass_to_node[dependency.dstSubpass].prev.push_back(dependency.srcSubpass);
4491 subpass_to_node[dependency.srcSubpass].next.push_back(dependency.dstSubpass);
4492 }
4493 }
John Zulauf4aff5d92020-02-21 08:29:35 -07004494 if (srcSubpass == VK_SUBPASS_EXTERNAL) {
4495 assert(dstSubpass != VK_SUBPASS_EXTERNAL); // this is invalid per VUID-VkSubpassDependency-srcSubpass-00865
John Zulaufbaea94f2020-09-15 17:55:16 -06004496 subpass_dependencies[dstSubpass].barrier_from_external.emplace_back(&dependency);
John Zulauf4aff5d92020-02-21 08:29:35 -07004497 } else if (dstSubpass == VK_SUBPASS_EXTERNAL) {
John Zulaufbaea94f2020-09-15 17:55:16 -06004498 subpass_dependencies[srcSubpass].barrier_to_external.emplace_back(&dependency);
John Zulauf4aff5d92020-02-21 08:29:35 -07004499 } else if (dependency.srcSubpass != dependency.dstSubpass) {
4500 // ignore self dependencies in prev and next
John Zulaufbaea94f2020-09-15 17:55:16 -06004501 subpass_dependencies[srcSubpass].next[&subpass_dependencies[dstSubpass]].emplace_back(&dependency);
4502 subpass_dependencies[dstSubpass].prev[&subpass_dependencies[srcSubpass]].emplace_back(&dependency);
John Zulauf4aff5d92020-02-21 08:29:35 -07004503 }
4504 }
4505
4506 //
4507 // Determine "asynchrononous" subpassess
4508 // syncronization is only interested in asyncronous stages *earlier* that the current one... so we'll only look towards those.
4509 // NOTE: This is O(N^3), which we could shrink to O(N^2logN) using sets instead of arrays, but given that N is likely to be
4510 // small and the K for |= from the prev is must less than for set, we'll accept the brute force.
4511 std::vector<std::vector<bool>> pass_depends(pCreateInfo->subpassCount);
4512 for (uint32_t i = 1; i < pCreateInfo->subpassCount; ++i) {
4513 auto &depends = pass_depends[i];
4514 depends.resize(i);
4515 auto &subpass_dep = subpass_dependencies[i];
4516 for (const auto &prev : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -06004517 const auto prev_pass = prev.first->pass;
John Zulauf4aff5d92020-02-21 08:29:35 -07004518 const auto &prev_depends = pass_depends[prev_pass];
4519 for (uint32_t j = 0; j < prev_pass; j++) {
4520 depends[j] = depends[j] | prev_depends[j];
4521 }
4522 depends[prev_pass] = true;
4523 }
4524 for (uint32_t pass = 0; pass < subpass_dep.pass; pass++) {
4525 if (!depends[pass]) {
4526 subpass_dep.async.push_back(pass);
4527 }
4528 }
locke-lunargd556cc32019-09-17 01:21:23 -06004529 }
4530}
4531
John Zulauf4aff5d92020-02-21 08:29:35 -07004532static VkSubpassDependency2 ImplicitDependencyFromExternal(uint32_t subpass) {
4533 VkSubpassDependency2 from_external = {VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2,
4534 nullptr,
4535 VK_SUBPASS_EXTERNAL,
4536 subpass,
4537 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
4538 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4539 0,
4540 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
4541 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
4542 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
4543 0,
4544 0};
4545 return from_external;
4546}
4547
4548static VkSubpassDependency2 ImplicitDependencyToExternal(uint32_t subpass) {
4549 VkSubpassDependency2 to_external = {VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2,
4550 nullptr,
4551 subpass,
4552 VK_SUBPASS_EXTERNAL,
4553 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4554 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
4555 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
4556 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
4557 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
4558 0,
4559 0,
4560 0};
4561 return to_external;
4562}
4563
locke-lunargd556cc32019-09-17 01:21:23 -06004564void ValidationStateTracker::RecordCreateRenderPassState(RenderPassCreateVersion rp_version,
4565 std::shared_ptr<RENDER_PASS_STATE> &render_pass,
4566 VkRenderPass *pRenderPass) {
4567 render_pass->renderPass = *pRenderPass;
4568 auto create_info = render_pass->createInfo.ptr();
4569
4570 RecordRenderPassDAG(RENDER_PASS_VERSION_1, create_info, render_pass.get());
4571
John Zulauf8863c332020-03-20 10:34:33 -06004572 struct AttachmentTracker { // This is really only of local interest, but a bit big for a lambda
4573 RENDER_PASS_STATE *const rp;
John Zulaufbb9f07f2020-03-19 16:53:06 -06004574 std::vector<uint32_t> &first;
John Zulauf1507ee42020-05-18 11:33:09 -06004575 std::vector<bool> &first_is_transition;
John Zulaufbb9f07f2020-03-19 16:53:06 -06004576 std::vector<uint32_t> &last;
John Zulauf8863c332020-03-20 10:34:33 -06004577 std::vector<std::vector<RENDER_PASS_STATE::AttachmentTransition>> &subpass_transitions;
John Zulaufbb9f07f2020-03-19 16:53:06 -06004578 std::unordered_map<uint32_t, bool> &first_read;
4579 const uint32_t attachment_count;
John Zulauf8863c332020-03-20 10:34:33 -06004580 std::vector<VkImageLayout> attachment_layout;
John Zulauf2bc1fde2020-04-24 15:09:51 -06004581 std::vector<std::vector<VkImageLayout>> subpass_attachment_layout;
John Zulauf8863c332020-03-20 10:34:33 -06004582 AttachmentTracker(std::shared_ptr<RENDER_PASS_STATE> &render_pass)
4583 : rp(render_pass.get()),
4584 first(rp->attachment_first_subpass),
John Zulauf1507ee42020-05-18 11:33:09 -06004585 first_is_transition(rp->attachment_first_is_transition),
John Zulauf8863c332020-03-20 10:34:33 -06004586 last(rp->attachment_last_subpass),
4587 subpass_transitions(rp->subpass_transitions),
4588 first_read(rp->attachment_first_read),
4589 attachment_count(rp->createInfo.attachmentCount),
John Zulauf2bc1fde2020-04-24 15:09:51 -06004590 attachment_layout(),
4591 subpass_attachment_layout() {
John Zulaufbb9f07f2020-03-19 16:53:06 -06004592 first.resize(attachment_count, VK_SUBPASS_EXTERNAL);
John Zulauf1507ee42020-05-18 11:33:09 -06004593 first_is_transition.resize(attachment_count, false);
John Zulaufbb9f07f2020-03-19 16:53:06 -06004594 last.resize(attachment_count, VK_SUBPASS_EXTERNAL);
John Zulauf8863c332020-03-20 10:34:33 -06004595 subpass_transitions.resize(rp->createInfo.subpassCount + 1); // Add an extra for EndRenderPass
4596 attachment_layout.reserve(attachment_count);
John Zulauf2bc1fde2020-04-24 15:09:51 -06004597 subpass_attachment_layout.resize(rp->createInfo.subpassCount);
4598 for (auto &subpass_layouts : subpass_attachment_layout) {
4599 subpass_layouts.resize(attachment_count, kInvalidLayout);
4600 }
4601
John Zulauf8863c332020-03-20 10:34:33 -06004602 for (uint32_t j = 0; j < attachment_count; j++) {
4603 attachment_layout.push_back(rp->createInfo.pAttachments[j].initialLayout);
4604 }
John Zulauf4aff5d92020-02-21 08:29:35 -07004605 }
John Zulauf4aff5d92020-02-21 08:29:35 -07004606
John Zulaufbb9f07f2020-03-19 16:53:06 -06004607 void Update(uint32_t subpass, const VkAttachmentReference2 *attach_ref, uint32_t count, bool is_read) {
4608 if (nullptr == attach_ref) return;
4609 for (uint32_t j = 0; j < count; ++j) {
4610 const auto attachment = attach_ref[j].attachment;
4611 if (attachment != VK_ATTACHMENT_UNUSED) {
John Zulauf8863c332020-03-20 10:34:33 -06004612 const auto layout = attach_ref[j].layout;
John Zulaufbb9f07f2020-03-19 16:53:06 -06004613 // Take advantage of the fact that insert won't overwrite, so we'll only write the first time.
4614 first_read.insert(std::make_pair(attachment, is_read));
John Zulauf2bc1fde2020-04-24 15:09:51 -06004615 if (first[attachment] == VK_SUBPASS_EXTERNAL) {
4616 first[attachment] = subpass;
4617 const auto initial_layout = rp->createInfo.pAttachments[attachment].initialLayout;
John Zulauf1507ee42020-05-18 11:33:09 -06004618 if (initial_layout != layout) {
4619 subpass_transitions[subpass].emplace_back(VK_SUBPASS_EXTERNAL, attachment, initial_layout, layout);
4620 first_is_transition[attachment] = true;
4621 }
John Zulauf2bc1fde2020-04-24 15:09:51 -06004622 }
John Zulaufbb9f07f2020-03-19 16:53:06 -06004623 last[attachment] = subpass;
John Zulauf8863c332020-03-20 10:34:33 -06004624
John Zulauf2bc1fde2020-04-24 15:09:51 -06004625 for (const auto &prev : rp->subpass_dependencies[subpass].prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -06004626 const auto prev_pass = prev.first->pass;
John Zulauf2bc1fde2020-04-24 15:09:51 -06004627 const auto prev_layout = subpass_attachment_layout[prev_pass][attachment];
4628 if ((prev_layout != kInvalidLayout) && (prev_layout != layout)) {
4629 subpass_transitions[subpass].emplace_back(prev_pass, attachment, prev_layout, layout);
4630 }
John Zulauf8863c332020-03-20 10:34:33 -06004631 }
John Zulauf2bc1fde2020-04-24 15:09:51 -06004632 attachment_layout[attachment] = layout;
John Zulauf8863c332020-03-20 10:34:33 -06004633 }
4634 }
4635 }
4636 void FinalTransitions() {
4637 auto &final_transitions = subpass_transitions[rp->createInfo.subpassCount];
4638
4639 for (uint32_t attachment = 0; attachment < attachment_count; ++attachment) {
4640 const auto final_layout = rp->createInfo.pAttachments[attachment].finalLayout;
John Zulauf2bc1fde2020-04-24 15:09:51 -06004641 // Add final transitions for attachments that were used and change layout.
4642 if ((last[attachment] != VK_SUBPASS_EXTERNAL) && final_layout != attachment_layout[attachment]) {
4643 final_transitions.emplace_back(last[attachment], attachment, attachment_layout[attachment], final_layout);
John Zulaufbb9f07f2020-03-19 16:53:06 -06004644 }
locke-lunargd556cc32019-09-17 01:21:23 -06004645 }
4646 }
John Zulaufbb9f07f2020-03-19 16:53:06 -06004647 };
John Zulauf8863c332020-03-20 10:34:33 -06004648 AttachmentTracker attachment_tracker(render_pass);
John Zulaufbb9f07f2020-03-19 16:53:06 -06004649
4650 for (uint32_t subpass_index = 0; subpass_index < create_info->subpassCount; ++subpass_index) {
4651 const VkSubpassDescription2KHR &subpass = create_info->pSubpasses[subpass_index];
John Zulauf8863c332020-03-20 10:34:33 -06004652 attachment_tracker.Update(subpass_index, subpass.pColorAttachments, subpass.colorAttachmentCount, false);
4653 attachment_tracker.Update(subpass_index, subpass.pResolveAttachments, subpass.colorAttachmentCount, false);
4654 attachment_tracker.Update(subpass_index, subpass.pDepthStencilAttachment, 1, false);
4655 attachment_tracker.Update(subpass_index, subpass.pInputAttachments, subpass.inputAttachmentCount, true);
John Zulauf4aff5d92020-02-21 08:29:35 -07004656 }
John Zulauf8863c332020-03-20 10:34:33 -06004657 attachment_tracker.FinalTransitions();
John Zulauf4aff5d92020-02-21 08:29:35 -07004658
John Zulaufbb9f07f2020-03-19 16:53:06 -06004659 // Add implicit dependencies
John Zulauf8863c332020-03-20 10:34:33 -06004660 for (uint32_t attachment = 0; attachment < attachment_tracker.attachment_count; attachment++) {
4661 const auto first_use = attachment_tracker.first[attachment];
John Zulauf4aff5d92020-02-21 08:29:35 -07004662 if (first_use != VK_SUBPASS_EXTERNAL) {
4663 auto &subpass_dep = render_pass->subpass_dependencies[first_use];
John Zulaufbaea94f2020-09-15 17:55:16 -06004664 if (subpass_dep.barrier_from_external.size() == 0) {
4665 // Add implicit from barrier if they're aren't any
John Zulauf4aff5d92020-02-21 08:29:35 -07004666 subpass_dep.implicit_barrier_from_external.reset(
4667 new VkSubpassDependency2(ImplicitDependencyFromExternal(first_use)));
John Zulaufbaea94f2020-09-15 17:55:16 -06004668 subpass_dep.barrier_from_external.emplace_back(subpass_dep.implicit_barrier_from_external.get());
John Zulauf4aff5d92020-02-21 08:29:35 -07004669 }
4670 }
4671
John Zulauf8863c332020-03-20 10:34:33 -06004672 const auto last_use = attachment_tracker.last[attachment];
John Zulauf4aff5d92020-02-21 08:29:35 -07004673 if (last_use != VK_SUBPASS_EXTERNAL) {
4674 auto &subpass_dep = render_pass->subpass_dependencies[last_use];
John Zulaufbaea94f2020-09-15 17:55:16 -06004675 if (render_pass->subpass_dependencies[last_use].barrier_to_external.size() == 0) {
4676 // Add implicit to barrier if they're aren't any
John Zulauf4aff5d92020-02-21 08:29:35 -07004677 subpass_dep.implicit_barrier_to_external.reset(new VkSubpassDependency2(ImplicitDependencyToExternal(last_use)));
John Zulaufbaea94f2020-09-15 17:55:16 -06004678 subpass_dep.barrier_to_external.emplace_back(subpass_dep.implicit_barrier_to_external.get());
John Zulauf4aff5d92020-02-21 08:29:35 -07004679 }
locke-lunargd556cc32019-09-17 01:21:23 -06004680 }
4681 }
4682
4683 // Even though render_pass is an rvalue-ref parameter, still must move s.t. move assignment is invoked.
4684 renderPassMap[*pRenderPass] = std::move(render_pass);
4685}
4686
4687// Style note:
4688// Use of rvalue reference exceeds reccommended usage of rvalue refs in google style guide, but intentionally forces caller to move
4689// or copy. This is clearer than passing a pointer to shared_ptr and avoids the atomic increment/decrement of shared_ptr copy
4690// construction or assignment.
4691void ValidationStateTracker::PostCallRecordCreateRenderPass(VkDevice device, const VkRenderPassCreateInfo *pCreateInfo,
4692 const VkAllocationCallbacks *pAllocator, VkRenderPass *pRenderPass,
4693 VkResult result) {
4694 if (VK_SUCCESS != result) return;
4695 auto render_pass_state = std::make_shared<RENDER_PASS_STATE>(pCreateInfo);
4696 RecordCreateRenderPassState(RENDER_PASS_VERSION_1, render_pass_state, pRenderPass);
4697}
4698
Tony-LunarG977448c2019-12-02 14:52:02 -07004699void ValidationStateTracker::RecordCreateRenderPass2(VkDevice device, const VkRenderPassCreateInfo2KHR *pCreateInfo,
4700 const VkAllocationCallbacks *pAllocator, VkRenderPass *pRenderPass,
4701 VkResult result) {
locke-lunargd556cc32019-09-17 01:21:23 -06004702 if (VK_SUCCESS != result) return;
4703 auto render_pass_state = std::make_shared<RENDER_PASS_STATE>(pCreateInfo);
4704 RecordCreateRenderPassState(RENDER_PASS_VERSION_2, render_pass_state, pRenderPass);
4705}
4706
Tony-LunarG977448c2019-12-02 14:52:02 -07004707void ValidationStateTracker::PostCallRecordCreateRenderPass2KHR(VkDevice device, const VkRenderPassCreateInfo2KHR *pCreateInfo,
4708 const VkAllocationCallbacks *pAllocator, VkRenderPass *pRenderPass,
4709 VkResult result) {
4710 RecordCreateRenderPass2(device, pCreateInfo, pAllocator, pRenderPass, result);
4711}
4712
4713void ValidationStateTracker::PostCallRecordCreateRenderPass2(VkDevice device, const VkRenderPassCreateInfo2KHR *pCreateInfo,
4714 const VkAllocationCallbacks *pAllocator, VkRenderPass *pRenderPass,
4715 VkResult result) {
4716 RecordCreateRenderPass2(device, pCreateInfo, pAllocator, pRenderPass, result);
4717}
4718
locke-lunargd556cc32019-09-17 01:21:23 -06004719void ValidationStateTracker::RecordCmdBeginRenderPassState(VkCommandBuffer commandBuffer,
4720 const VkRenderPassBeginInfo *pRenderPassBegin,
4721 const VkSubpassContents contents) {
4722 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
locke-lunargaecf2152020-05-12 17:15:41 -06004723 auto render_pass_state = pRenderPassBegin ? GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass) : nullptr;
4724 auto framebuffer = pRenderPassBegin ? GetShared<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer) : nullptr;
locke-lunargd556cc32019-09-17 01:21:23 -06004725
4726 if (render_pass_state) {
locke-lunargaecf2152020-05-12 17:15:41 -06004727 cb_state->activeFramebuffer = framebuffer;
locke-lunargd556cc32019-09-17 01:21:23 -06004728 cb_state->activeRenderPass = render_pass_state;
Tony-LunarG61e7c0c2020-03-03 16:09:11 -07004729 cb_state->activeRenderPassBeginInfo = safe_VkRenderPassBeginInfo(pRenderPassBegin);
locke-lunargd556cc32019-09-17 01:21:23 -06004730 cb_state->activeSubpass = 0;
4731 cb_state->activeSubpassContents = contents;
locke-lunargfc78e932020-11-19 17:06:24 -07004732
locke-lunargd556cc32019-09-17 01:21:23 -06004733 // Connect this RP to cmdBuffer
locke-lunargaecf2152020-05-12 17:15:41 -06004734 AddCommandBufferBinding(
4735 render_pass_state->cb_bindings,
4736 VulkanTypedHandle(render_pass_state->renderPass, kVulkanObjectTypeRenderPass, render_pass_state.get()), cb_state);
locke-lunargd556cc32019-09-17 01:21:23 -06004737
4738 auto chained_device_group_struct = lvl_find_in_chain<VkDeviceGroupRenderPassBeginInfo>(pRenderPassBegin->pNext);
4739 if (chained_device_group_struct) {
4740 cb_state->active_render_pass_device_mask = chained_device_group_struct->deviceMask;
4741 } else {
4742 cb_state->active_render_pass_device_mask = cb_state->initial_device_mask;
4743 }
Lionel Landwerlin484d10f2020-04-24 01:34:47 +03004744
locke-lunargfc78e932020-11-19 17:06:24 -07004745 cb_state->active_subpasses = nullptr;
4746 cb_state->active_attachments = nullptr;
4747
4748 if (framebuffer) {
4749 cb_state->framebuffers.insert(framebuffer);
4750
4751 // Set cb_state->active_subpasses
4752 cb_state->active_subpasses =
4753 std::make_shared<std::vector<SUBPASS_INFO>>(cb_state->activeFramebuffer->createInfo.attachmentCount);
4754 const auto &subpass = cb_state->activeRenderPass->createInfo.pSubpasses[cb_state->activeSubpass];
4755 UpdateSubpassAttachments(subpass, *cb_state->active_subpasses);
4756
4757 // Set cb_state->active_attachments & cb_state->attachments_view_states
4758 cb_state->active_attachments =
4759 std::make_shared<std::vector<IMAGE_VIEW_STATE *>>(framebuffer->createInfo.attachmentCount);
4760 UpdateAttachmentsView(*this, *cb_state, *cb_state->activeFramebuffer, pRenderPassBegin);
4761
4762 // Connect this framebuffer and its children to this cmdBuffer
4763 AddFramebufferBinding(cb_state, framebuffer.get());
Lionel Landwerlin484d10f2020-04-24 01:34:47 +03004764 }
locke-lunargd556cc32019-09-17 01:21:23 -06004765 }
4766}
4767
4768void ValidationStateTracker::PreCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer,
4769 const VkRenderPassBeginInfo *pRenderPassBegin,
4770 VkSubpassContents contents) {
4771 RecordCmdBeginRenderPassState(commandBuffer, pRenderPassBegin, contents);
4772}
4773
4774void ValidationStateTracker::PreCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
4775 const VkRenderPassBeginInfo *pRenderPassBegin,
4776 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) {
4777 RecordCmdBeginRenderPassState(commandBuffer, pRenderPassBegin, pSubpassBeginInfo->contents);
4778}
4779
Jeremy Hayes9bda85a2020-05-21 16:36:17 -06004780void ValidationStateTracker::PostCallRecordCmdBeginTransformFeedbackEXT(VkCommandBuffer commandBuffer, uint32_t firstCounterBuffer,
4781 uint32_t counterBufferCount,
4782 const VkBuffer *pCounterBuffers,
4783 const VkDeviceSize *pCounterBufferOffsets) {
4784 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4785
4786 cb_state->transform_feedback_active = true;
4787}
4788
4789void ValidationStateTracker::PostCallRecordCmdEndTransformFeedbackEXT(VkCommandBuffer commandBuffer, uint32_t firstCounterBuffer,
4790 uint32_t counterBufferCount, const VkBuffer *pCounterBuffers,
4791 const VkDeviceSize *pCounterBufferOffsets) {
4792 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4793
4794 cb_state->transform_feedback_active = false;
4795}
4796
Tony-LunarG977448c2019-12-02 14:52:02 -07004797void ValidationStateTracker::PreCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer,
4798 const VkRenderPassBeginInfo *pRenderPassBegin,
4799 const VkSubpassBeginInfoKHR *pSubpassBeginInfo) {
4800 RecordCmdBeginRenderPassState(commandBuffer, pRenderPassBegin, pSubpassBeginInfo->contents);
4801}
4802
locke-lunargd556cc32019-09-17 01:21:23 -06004803void ValidationStateTracker::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
4804 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4805 cb_state->activeSubpass++;
4806 cb_state->activeSubpassContents = contents;
locke-lunargfc78e932020-11-19 17:06:24 -07004807
4808 // Update cb_state->active_subpasses
4809 if (cb_state->activeRenderPass && cb_state->activeFramebuffer) {
4810 cb_state->active_subpasses = nullptr;
4811 cb_state->active_subpasses =
4812 std::make_shared<std::vector<SUBPASS_INFO>>(cb_state->activeFramebuffer->createInfo.attachmentCount);
4813
4814 const auto &subpass = cb_state->activeRenderPass->createInfo.pSubpasses[cb_state->activeSubpass];
4815 UpdateSubpassAttachments(subpass, *cb_state->active_subpasses);
4816 }
locke-lunargd556cc32019-09-17 01:21:23 -06004817}
4818
4819void ValidationStateTracker::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
4820 RecordCmdNextSubpass(commandBuffer, contents);
4821}
4822
4823void ValidationStateTracker::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer,
4824 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
4825 const VkSubpassEndInfoKHR *pSubpassEndInfo) {
4826 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo->contents);
4827}
4828
Tony-LunarG977448c2019-12-02 14:52:02 -07004829void ValidationStateTracker::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer,
4830 const VkSubpassBeginInfoKHR *pSubpassBeginInfo,
4831 const VkSubpassEndInfoKHR *pSubpassEndInfo) {
4832 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo->contents);
4833}
4834
locke-lunargd556cc32019-09-17 01:21:23 -06004835void ValidationStateTracker::RecordCmdEndRenderPassState(VkCommandBuffer commandBuffer) {
4836 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4837 cb_state->activeRenderPass = nullptr;
locke-lunargfc78e932020-11-19 17:06:24 -07004838 cb_state->active_attachments = nullptr;
4839 cb_state->active_subpasses = nullptr;
locke-lunargd556cc32019-09-17 01:21:23 -06004840 cb_state->activeSubpass = 0;
4841 cb_state->activeFramebuffer = VK_NULL_HANDLE;
4842}
4843
4844void ValidationStateTracker::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
4845 RecordCmdEndRenderPassState(commandBuffer);
4846}
4847
4848void ValidationStateTracker::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
4849 const VkSubpassEndInfoKHR *pSubpassEndInfo) {
4850 RecordCmdEndRenderPassState(commandBuffer);
4851}
4852
Tony-LunarG977448c2019-12-02 14:52:02 -07004853void ValidationStateTracker::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer,
4854 const VkSubpassEndInfoKHR *pSubpassEndInfo) {
4855 RecordCmdEndRenderPassState(commandBuffer);
4856}
locke-lunargd556cc32019-09-17 01:21:23 -06004857void ValidationStateTracker::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBuffersCount,
4858 const VkCommandBuffer *pCommandBuffers) {
4859 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
4860
4861 CMD_BUFFER_STATE *sub_cb_state = NULL;
4862 for (uint32_t i = 0; i < commandBuffersCount; i++) {
4863 sub_cb_state = GetCBState(pCommandBuffers[i]);
4864 assert(sub_cb_state);
4865 if (!(sub_cb_state->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
4866 if (cb_state->beginInfo.flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) {
4867 // TODO: Because this is a state change, clearing the VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT needs to be moved
4868 // from the validation step to the recording step
4869 cb_state->beginInfo.flags &= ~VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
4870 }
4871 }
4872
4873 // Propagate inital layout and current layout state to the primary cmd buffer
4874 // NOTE: The update/population of the image_layout_map is done in CoreChecks, but for other classes derived from
4875 // ValidationStateTracker these maps will be empty, so leaving the propagation in the the state tracker should be a no-op
4876 // for those other classes.
4877 for (const auto &sub_layout_map_entry : sub_cb_state->image_layout_map) {
4878 const auto image = sub_layout_map_entry.first;
4879 const auto *image_state = GetImageState(image);
4880 if (!image_state) continue; // Can't set layouts of a dead image
4881
4882 auto *cb_subres_map = GetImageSubresourceLayoutMap(cb_state, *image_state);
4883 const auto *sub_cb_subres_map = sub_layout_map_entry.second.get();
4884 assert(cb_subres_map && sub_cb_subres_map); // Non const get and map traversal should never be null
4885 cb_subres_map->UpdateFrom(*sub_cb_subres_map);
4886 }
4887
4888 sub_cb_state->primaryCommandBuffer = cb_state->commandBuffer;
4889 cb_state->linkedCommandBuffers.insert(sub_cb_state);
4890 sub_cb_state->linkedCommandBuffers.insert(cb_state);
4891 for (auto &function : sub_cb_state->queryUpdates) {
4892 cb_state->queryUpdates.push_back(function);
4893 }
4894 for (auto &function : sub_cb_state->queue_submit_functions) {
4895 cb_state->queue_submit_functions.push_back(function);
4896 }
4897 }
4898}
4899
4900void ValidationStateTracker::PostCallRecordMapMemory(VkDevice device, VkDeviceMemory mem, VkDeviceSize offset, VkDeviceSize size,
4901 VkFlags flags, void **ppData, VkResult result) {
4902 if (VK_SUCCESS != result) return;
4903 RecordMappedMemory(mem, offset, size, ppData);
4904}
4905
4906void ValidationStateTracker::PreCallRecordUnmapMemory(VkDevice device, VkDeviceMemory mem) {
4907 auto mem_info = GetDevMemState(mem);
4908 if (mem_info) {
4909 mem_info->mapped_range = MemRange();
4910 mem_info->p_driver_data = nullptr;
4911 }
4912}
4913
4914void ValidationStateTracker::UpdateBindImageMemoryState(const VkBindImageMemoryInfo &bindInfo) {
4915 IMAGE_STATE *image_state = GetImageState(bindInfo.image);
4916 if (image_state) {
locke-lunargae26eac2020-04-16 15:29:05 -06004917 // An Android sepcial image cannot get VkSubresourceLayout until the image binds a memory.
4918 // See: VUID-vkGetImageSubresourceLayout-image-01895
4919 image_state->fragment_encoder =
4920 std::unique_ptr<const subresource_adapter::ImageRangeEncoder>(new subresource_adapter::ImageRangeEncoder(*image_state));
locke-lunargd556cc32019-09-17 01:21:23 -06004921 const auto swapchain_info = lvl_find_in_chain<VkBindImageMemorySwapchainInfoKHR>(bindInfo.pNext);
4922 if (swapchain_info) {
4923 auto swapchain = GetSwapchainState(swapchain_info->swapchain);
4924 if (swapchain) {
locke-lunargb3584732019-10-28 20:18:36 -06004925 swapchain->images[swapchain_info->imageIndex].bound_images.emplace(image_state->image);
locke-lunargd556cc32019-09-17 01:21:23 -06004926 image_state->bind_swapchain = swapchain_info->swapchain;
4927 image_state->bind_swapchain_imageIndex = swapchain_info->imageIndex;
4928 }
4929 } else {
4930 // Track bound memory range information
4931 auto mem_info = GetDevMemState(bindInfo.memory);
4932 if (mem_info) {
sfricke-samsungd7ea5de2020-04-08 09:19:18 -07004933 InsertImageMemoryRange(bindInfo.image, mem_info, bindInfo.memoryOffset);
locke-lunargd556cc32019-09-17 01:21:23 -06004934 }
4935
4936 // Track objects tied to memory
4937 SetMemBinding(bindInfo.memory, image_state, bindInfo.memoryOffset,
4938 VulkanTypedHandle(bindInfo.image, kVulkanObjectTypeImage));
4939 }
Tony-LunarG330cf4c2020-03-04 16:29:03 -07004940 if ((image_state->createInfo.flags & VK_IMAGE_CREATE_ALIAS_BIT) || swapchain_info) {
locke-lunargd556cc32019-09-17 01:21:23 -06004941 AddAliasingImage(image_state);
4942 }
4943 }
4944}
4945
4946void ValidationStateTracker::PostCallRecordBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory mem,
4947 VkDeviceSize memoryOffset, VkResult result) {
4948 if (VK_SUCCESS != result) return;
4949 VkBindImageMemoryInfo bindInfo = {};
4950 bindInfo.sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;
4951 bindInfo.image = image;
4952 bindInfo.memory = mem;
4953 bindInfo.memoryOffset = memoryOffset;
4954 UpdateBindImageMemoryState(bindInfo);
4955}
4956
4957void ValidationStateTracker::PostCallRecordBindImageMemory2(VkDevice device, uint32_t bindInfoCount,
4958 const VkBindImageMemoryInfoKHR *pBindInfos, VkResult result) {
4959 if (VK_SUCCESS != result) return;
4960 for (uint32_t i = 0; i < bindInfoCount; i++) {
4961 UpdateBindImageMemoryState(pBindInfos[i]);
4962 }
4963}
4964
4965void ValidationStateTracker::PostCallRecordBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount,
4966 const VkBindImageMemoryInfoKHR *pBindInfos, VkResult result) {
4967 if (VK_SUCCESS != result) return;
4968 for (uint32_t i = 0; i < bindInfoCount; i++) {
4969 UpdateBindImageMemoryState(pBindInfos[i]);
4970 }
4971}
4972
4973void ValidationStateTracker::PreCallRecordSetEvent(VkDevice device, VkEvent event) {
4974 auto event_state = GetEventState(event);
4975 if (event_state) {
4976 event_state->stageMask = VK_PIPELINE_STAGE_HOST_BIT;
4977 }
locke-lunargd556cc32019-09-17 01:21:23 -06004978}
4979
4980void ValidationStateTracker::PostCallRecordImportSemaphoreFdKHR(VkDevice device,
4981 const VkImportSemaphoreFdInfoKHR *pImportSemaphoreFdInfo,
4982 VkResult result) {
4983 if (VK_SUCCESS != result) return;
4984 RecordImportSemaphoreState(pImportSemaphoreFdInfo->semaphore, pImportSemaphoreFdInfo->handleType,
4985 pImportSemaphoreFdInfo->flags);
4986}
4987
4988void ValidationStateTracker::RecordGetExternalSemaphoreState(VkSemaphore semaphore,
4989 VkExternalSemaphoreHandleTypeFlagBitsKHR handle_type) {
4990 SEMAPHORE_STATE *semaphore_state = GetSemaphoreState(semaphore);
4991 if (semaphore_state && handle_type != VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR) {
4992 // Cannot track semaphore state once it is exported, except for Sync FD handle types which have copy transference
4993 semaphore_state->scope = kSyncScopeExternalPermanent;
4994 }
4995}
4996
4997#ifdef VK_USE_PLATFORM_WIN32_KHR
4998void ValidationStateTracker::PostCallRecordImportSemaphoreWin32HandleKHR(
4999 VkDevice device, const VkImportSemaphoreWin32HandleInfoKHR *pImportSemaphoreWin32HandleInfo, VkResult result) {
5000 if (VK_SUCCESS != result) return;
5001 RecordImportSemaphoreState(pImportSemaphoreWin32HandleInfo->semaphore, pImportSemaphoreWin32HandleInfo->handleType,
5002 pImportSemaphoreWin32HandleInfo->flags);
5003}
5004
5005void ValidationStateTracker::PostCallRecordGetSemaphoreWin32HandleKHR(VkDevice device,
5006 const VkSemaphoreGetWin32HandleInfoKHR *pGetWin32HandleInfo,
5007 HANDLE *pHandle, VkResult result) {
5008 if (VK_SUCCESS != result) return;
5009 RecordGetExternalSemaphoreState(pGetWin32HandleInfo->semaphore, pGetWin32HandleInfo->handleType);
5010}
5011
5012void ValidationStateTracker::PostCallRecordImportFenceWin32HandleKHR(
5013 VkDevice device, const VkImportFenceWin32HandleInfoKHR *pImportFenceWin32HandleInfo, VkResult result) {
5014 if (VK_SUCCESS != result) return;
5015 RecordImportFenceState(pImportFenceWin32HandleInfo->fence, pImportFenceWin32HandleInfo->handleType,
5016 pImportFenceWin32HandleInfo->flags);
5017}
5018
5019void ValidationStateTracker::PostCallRecordGetFenceWin32HandleKHR(VkDevice device,
5020 const VkFenceGetWin32HandleInfoKHR *pGetWin32HandleInfo,
5021 HANDLE *pHandle, VkResult result) {
5022 if (VK_SUCCESS != result) return;
5023 RecordGetExternalFenceState(pGetWin32HandleInfo->fence, pGetWin32HandleInfo->handleType);
5024}
5025#endif
5026
5027void ValidationStateTracker::PostCallRecordGetSemaphoreFdKHR(VkDevice device, const VkSemaphoreGetFdInfoKHR *pGetFdInfo, int *pFd,
5028 VkResult result) {
5029 if (VK_SUCCESS != result) return;
5030 RecordGetExternalSemaphoreState(pGetFdInfo->semaphore, pGetFdInfo->handleType);
5031}
5032
5033void ValidationStateTracker::RecordImportFenceState(VkFence fence, VkExternalFenceHandleTypeFlagBitsKHR handle_type,
5034 VkFenceImportFlagsKHR flags) {
5035 FENCE_STATE *fence_node = GetFenceState(fence);
5036 if (fence_node && fence_node->scope != kSyncScopeExternalPermanent) {
5037 if ((handle_type == VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT_KHR || flags & VK_FENCE_IMPORT_TEMPORARY_BIT_KHR) &&
5038 fence_node->scope == kSyncScopeInternal) {
5039 fence_node->scope = kSyncScopeExternalTemporary;
5040 } else {
5041 fence_node->scope = kSyncScopeExternalPermanent;
5042 }
5043 }
5044}
5045
5046void ValidationStateTracker::PostCallRecordImportFenceFdKHR(VkDevice device, const VkImportFenceFdInfoKHR *pImportFenceFdInfo,
5047 VkResult result) {
5048 if (VK_SUCCESS != result) return;
5049 RecordImportFenceState(pImportFenceFdInfo->fence, pImportFenceFdInfo->handleType, pImportFenceFdInfo->flags);
5050}
5051
5052void ValidationStateTracker::RecordGetExternalFenceState(VkFence fence, VkExternalFenceHandleTypeFlagBitsKHR handle_type) {
5053 FENCE_STATE *fence_state = GetFenceState(fence);
5054 if (fence_state) {
5055 if (handle_type != VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT_KHR) {
5056 // Export with reference transference becomes external
5057 fence_state->scope = kSyncScopeExternalPermanent;
5058 } else if (fence_state->scope == kSyncScopeInternal) {
5059 // Export with copy transference has a side effect of resetting the fence
5060 fence_state->state = FENCE_UNSIGNALED;
5061 }
5062 }
5063}
5064
5065void ValidationStateTracker::PostCallRecordGetFenceFdKHR(VkDevice device, const VkFenceGetFdInfoKHR *pGetFdInfo, int *pFd,
5066 VkResult result) {
5067 if (VK_SUCCESS != result) return;
5068 RecordGetExternalFenceState(pGetFdInfo->fence, pGetFdInfo->handleType);
5069}
5070
5071void ValidationStateTracker::PostCallRecordCreateEvent(VkDevice device, const VkEventCreateInfo *pCreateInfo,
5072 const VkAllocationCallbacks *pAllocator, VkEvent *pEvent, VkResult result) {
5073 if (VK_SUCCESS != result) return;
5074 eventMap[*pEvent].write_in_use = 0;
5075 eventMap[*pEvent].stageMask = VkPipelineStageFlags(0);
5076}
5077
5078void ValidationStateTracker::RecordCreateSwapchainState(VkResult result, const VkSwapchainCreateInfoKHR *pCreateInfo,
5079 VkSwapchainKHR *pSwapchain, SURFACE_STATE *surface_state,
5080 SWAPCHAIN_NODE *old_swapchain_state) {
5081 if (VK_SUCCESS == result) {
Jeff Bolze7fc67b2019-10-04 12:29:31 -05005082 auto swapchain_state = std::make_shared<SWAPCHAIN_NODE>(pCreateInfo, *pSwapchain);
locke-lunargd556cc32019-09-17 01:21:23 -06005083 if (VK_PRESENT_MODE_SHARED_DEMAND_REFRESH_KHR == pCreateInfo->presentMode ||
5084 VK_PRESENT_MODE_SHARED_CONTINUOUS_REFRESH_KHR == pCreateInfo->presentMode) {
5085 swapchain_state->shared_presentable = true;
5086 }
5087 surface_state->swapchain = swapchain_state.get();
5088 swapchainMap[*pSwapchain] = std::move(swapchain_state);
5089 } else {
5090 surface_state->swapchain = nullptr;
5091 }
5092 // Spec requires that even if CreateSwapchainKHR fails, oldSwapchain is retired
5093 if (old_swapchain_state) {
5094 old_swapchain_state->retired = true;
5095 }
5096 return;
5097}
5098
5099void ValidationStateTracker::PostCallRecordCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo,
5100 const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchain,
5101 VkResult result) {
5102 auto surface_state = GetSurfaceState(pCreateInfo->surface);
5103 auto old_swapchain_state = GetSwapchainState(pCreateInfo->oldSwapchain);
5104 RecordCreateSwapchainState(result, pCreateInfo, pSwapchain, surface_state, old_swapchain_state);
5105}
5106
5107void ValidationStateTracker::PreCallRecordDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain,
5108 const VkAllocationCallbacks *pAllocator) {
5109 if (!swapchain) return;
5110 auto swapchain_data = GetSwapchainState(swapchain);
5111 if (swapchain_data) {
5112 for (const auto &swapchain_image : swapchain_data->images) {
locke-lunargb3584732019-10-28 20:18:36 -06005113 ClearMemoryObjectBindings(VulkanTypedHandle(swapchain_image.image, kVulkanObjectTypeImage));
5114 imageMap.erase(swapchain_image.image);
5115 RemoveAliasingImages(swapchain_image.bound_images);
locke-lunargd556cc32019-09-17 01:21:23 -06005116 }
5117
5118 auto surface_state = GetSurfaceState(swapchain_data->createInfo.surface);
5119 if (surface_state) {
5120 if (surface_state->swapchain == swapchain_data) surface_state->swapchain = nullptr;
5121 }
Jeff Bolze7fc67b2019-10-04 12:29:31 -05005122 swapchain_data->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06005123 swapchainMap.erase(swapchain);
5124 }
5125}
5126
5127void ValidationStateTracker::PostCallRecordQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *pPresentInfo, VkResult result) {
5128 // Semaphore waits occur before error generation, if the call reached the ICD. (Confirm?)
5129 for (uint32_t i = 0; i < pPresentInfo->waitSemaphoreCount; ++i) {
5130 auto pSemaphore = GetSemaphoreState(pPresentInfo->pWaitSemaphores[i]);
5131 if (pSemaphore) {
5132 pSemaphore->signaler.first = VK_NULL_HANDLE;
5133 pSemaphore->signaled = false;
5134 }
5135 }
5136
5137 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
5138 // Note: this is imperfect, in that we can get confused about what did or didn't succeed-- but if the app does that, it's
5139 // confused itself just as much.
5140 auto local_result = pPresentInfo->pResults ? pPresentInfo->pResults[i] : result;
5141 if (local_result != VK_SUCCESS && local_result != VK_SUBOPTIMAL_KHR) continue; // this present didn't actually happen.
5142 // Mark the image as having been released to the WSI
5143 auto swapchain_data = GetSwapchainState(pPresentInfo->pSwapchains[i]);
5144 if (swapchain_data && (swapchain_data->images.size() > pPresentInfo->pImageIndices[i])) {
locke-lunargb3584732019-10-28 20:18:36 -06005145 auto image = swapchain_data->images[pPresentInfo->pImageIndices[i]].image;
locke-lunargd556cc32019-09-17 01:21:23 -06005146 auto image_state = GetImageState(image);
5147 if (image_state) {
5148 image_state->acquired = false;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05005149 if (image_state->shared_presentable) {
5150 image_state->layout_locked = true;
5151 }
locke-lunargd556cc32019-09-17 01:21:23 -06005152 }
5153 }
5154 }
5155 // Note: even though presentation is directed to a queue, there is no direct ordering between QP and subsequent work, so QP (and
5156 // its semaphore waits) /never/ participate in any completion proof.
5157}
5158
5159void ValidationStateTracker::PostCallRecordCreateSharedSwapchainsKHR(VkDevice device, uint32_t swapchainCount,
5160 const VkSwapchainCreateInfoKHR *pCreateInfos,
5161 const VkAllocationCallbacks *pAllocator,
5162 VkSwapchainKHR *pSwapchains, VkResult result) {
5163 if (pCreateInfos) {
5164 for (uint32_t i = 0; i < swapchainCount; i++) {
5165 auto surface_state = GetSurfaceState(pCreateInfos[i].surface);
5166 auto old_swapchain_state = GetSwapchainState(pCreateInfos[i].oldSwapchain);
5167 RecordCreateSwapchainState(result, &pCreateInfos[i], &pSwapchains[i], surface_state, old_swapchain_state);
5168 }
5169 }
5170}
5171
5172void ValidationStateTracker::RecordAcquireNextImageState(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
5173 VkSemaphore semaphore, VkFence fence, uint32_t *pImageIndex) {
5174 auto pFence = GetFenceState(fence);
5175 if (pFence && pFence->scope == kSyncScopeInternal) {
5176 // Treat as inflight since it is valid to wait on this fence, even in cases where it is technically a temporary
5177 // import
5178 pFence->state = FENCE_INFLIGHT;
5179 pFence->signaler.first = VK_NULL_HANDLE; // ANI isn't on a queue, so this can't participate in a completion proof.
5180 }
5181
5182 auto pSemaphore = GetSemaphoreState(semaphore);
5183 if (pSemaphore && pSemaphore->scope == kSyncScopeInternal) {
5184 // Treat as signaled since it is valid to wait on this semaphore, even in cases where it is technically a
5185 // temporary import
5186 pSemaphore->signaled = true;
5187 pSemaphore->signaler.first = VK_NULL_HANDLE;
5188 }
5189
5190 // Mark the image as acquired.
5191 auto swapchain_data = GetSwapchainState(swapchain);
5192 if (swapchain_data && (swapchain_data->images.size() > *pImageIndex)) {
locke-lunargb3584732019-10-28 20:18:36 -06005193 auto image = swapchain_data->images[*pImageIndex].image;
locke-lunargd556cc32019-09-17 01:21:23 -06005194 auto image_state = GetImageState(image);
5195 if (image_state) {
5196 image_state->acquired = true;
5197 image_state->shared_presentable = swapchain_data->shared_presentable;
5198 }
5199 }
5200}
5201
5202void ValidationStateTracker::PostCallRecordAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
5203 VkSemaphore semaphore, VkFence fence, uint32_t *pImageIndex,
5204 VkResult result) {
5205 if ((VK_SUCCESS != result) && (VK_SUBOPTIMAL_KHR != result)) return;
5206 RecordAcquireNextImageState(device, swapchain, timeout, semaphore, fence, pImageIndex);
5207}
5208
5209void ValidationStateTracker::PostCallRecordAcquireNextImage2KHR(VkDevice device, const VkAcquireNextImageInfoKHR *pAcquireInfo,
5210 uint32_t *pImageIndex, VkResult result) {
5211 if ((VK_SUCCESS != result) && (VK_SUBOPTIMAL_KHR != result)) return;
5212 RecordAcquireNextImageState(device, pAcquireInfo->swapchain, pAcquireInfo->timeout, pAcquireInfo->semaphore,
5213 pAcquireInfo->fence, pImageIndex);
5214}
5215
5216void ValidationStateTracker::PostCallRecordEnumeratePhysicalDevices(VkInstance instance, uint32_t *pPhysicalDeviceCount,
5217 VkPhysicalDevice *pPhysicalDevices, VkResult result) {
5218 if ((NULL != pPhysicalDevices) && ((result == VK_SUCCESS || result == VK_INCOMPLETE))) {
5219 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++) {
5220 auto &phys_device_state = physical_device_map[pPhysicalDevices[i]];
5221 phys_device_state.phys_device = pPhysicalDevices[i];
5222 // Init actual features for each physical device
5223 DispatchGetPhysicalDeviceFeatures(pPhysicalDevices[i], &phys_device_state.features2.features);
5224 }
5225 }
5226}
5227
5228// Common function to update state for GetPhysicalDeviceQueueFamilyProperties & 2KHR version
5229static void StateUpdateCommonGetPhysicalDeviceQueueFamilyProperties(PHYSICAL_DEVICE_STATE *pd_state, uint32_t count,
5230 VkQueueFamilyProperties2KHR *pQueueFamilyProperties) {
5231 pd_state->queue_family_known_count = std::max(pd_state->queue_family_known_count, count);
5232
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005233 if (pQueueFamilyProperties) { // Save queue family properties
locke-lunargd556cc32019-09-17 01:21:23 -06005234 pd_state->queue_family_properties.resize(std::max(static_cast<uint32_t>(pd_state->queue_family_properties.size()), count));
5235 for (uint32_t i = 0; i < count; ++i) {
5236 pd_state->queue_family_properties[i] = pQueueFamilyProperties[i].queueFamilyProperties;
5237 }
5238 }
5239}
5240
5241void ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice physicalDevice,
5242 uint32_t *pQueueFamilyPropertyCount,
5243 VkQueueFamilyProperties *pQueueFamilyProperties) {
5244 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
5245 assert(physical_device_state);
5246 VkQueueFamilyProperties2KHR *pqfp = nullptr;
5247 std::vector<VkQueueFamilyProperties2KHR> qfp;
5248 qfp.resize(*pQueueFamilyPropertyCount);
5249 if (pQueueFamilyProperties) {
5250 for (uint32_t i = 0; i < *pQueueFamilyPropertyCount; ++i) {
5251 qfp[i].sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_PROPERTIES_2_KHR;
5252 qfp[i].pNext = nullptr;
5253 qfp[i].queueFamilyProperties = pQueueFamilyProperties[i];
5254 }
5255 pqfp = qfp.data();
5256 }
5257 StateUpdateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount, pqfp);
5258}
5259
5260void ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2(
5261 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount, VkQueueFamilyProperties2KHR *pQueueFamilyProperties) {
5262 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
5263 assert(physical_device_state);
5264 StateUpdateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
5265 pQueueFamilyProperties);
5266}
5267
5268void ValidationStateTracker::PostCallRecordGetPhysicalDeviceQueueFamilyProperties2KHR(
5269 VkPhysicalDevice physicalDevice, uint32_t *pQueueFamilyPropertyCount, VkQueueFamilyProperties2KHR *pQueueFamilyProperties) {
5270 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
5271 assert(physical_device_state);
5272 StateUpdateCommonGetPhysicalDeviceQueueFamilyProperties(physical_device_state, *pQueueFamilyPropertyCount,
5273 pQueueFamilyProperties);
5274}
5275void ValidationStateTracker::PreCallRecordDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface,
5276 const VkAllocationCallbacks *pAllocator) {
Jeff Bolze7fc67b2019-10-04 12:29:31 -05005277 if (!surface) return;
5278 auto surface_state = GetSurfaceState(surface);
5279 surface_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06005280 surface_map.erase(surface);
5281}
5282
5283void ValidationStateTracker::RecordVulkanSurface(VkSurfaceKHR *pSurface) {
Jeff Bolze7fc67b2019-10-04 12:29:31 -05005284 surface_map[*pSurface] = std::make_shared<SURFACE_STATE>(*pSurface);
locke-lunargd556cc32019-09-17 01:21:23 -06005285}
5286
5287void ValidationStateTracker::PostCallRecordCreateDisplayPlaneSurfaceKHR(VkInstance instance,
5288 const VkDisplaySurfaceCreateInfoKHR *pCreateInfo,
5289 const VkAllocationCallbacks *pAllocator,
5290 VkSurfaceKHR *pSurface, VkResult result) {
5291 if (VK_SUCCESS != result) return;
5292 RecordVulkanSurface(pSurface);
5293}
5294
5295#ifdef VK_USE_PLATFORM_ANDROID_KHR
5296void ValidationStateTracker::PostCallRecordCreateAndroidSurfaceKHR(VkInstance instance,
5297 const VkAndroidSurfaceCreateInfoKHR *pCreateInfo,
5298 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface,
5299 VkResult result) {
5300 if (VK_SUCCESS != result) return;
5301 RecordVulkanSurface(pSurface);
5302}
5303#endif // VK_USE_PLATFORM_ANDROID_KHR
5304
5305#ifdef VK_USE_PLATFORM_IOS_MVK
5306void ValidationStateTracker::PostCallRecordCreateIOSSurfaceMVK(VkInstance instance, const VkIOSSurfaceCreateInfoMVK *pCreateInfo,
5307 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface,
5308 VkResult result) {
5309 if (VK_SUCCESS != result) return;
5310 RecordVulkanSurface(pSurface);
5311}
5312#endif // VK_USE_PLATFORM_IOS_MVK
5313
5314#ifdef VK_USE_PLATFORM_MACOS_MVK
5315void ValidationStateTracker::PostCallRecordCreateMacOSSurfaceMVK(VkInstance instance,
5316 const VkMacOSSurfaceCreateInfoMVK *pCreateInfo,
5317 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface,
5318 VkResult result) {
5319 if (VK_SUCCESS != result) return;
5320 RecordVulkanSurface(pSurface);
5321}
5322#endif // VK_USE_PLATFORM_MACOS_MVK
5323
Jeremy Kniagerf33a67c2019-12-09 09:44:39 -07005324#ifdef VK_USE_PLATFORM_METAL_EXT
5325void ValidationStateTracker::PostCallRecordCreateMetalSurfaceEXT(VkInstance instance,
5326 const VkMetalSurfaceCreateInfoEXT *pCreateInfo,
5327 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface,
5328 VkResult result) {
5329 if (VK_SUCCESS != result) return;
5330 RecordVulkanSurface(pSurface);
5331}
5332#endif // VK_USE_PLATFORM_METAL_EXT
5333
locke-lunargd556cc32019-09-17 01:21:23 -06005334#ifdef VK_USE_PLATFORM_WAYLAND_KHR
5335void ValidationStateTracker::PostCallRecordCreateWaylandSurfaceKHR(VkInstance instance,
5336 const VkWaylandSurfaceCreateInfoKHR *pCreateInfo,
5337 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface,
5338 VkResult result) {
5339 if (VK_SUCCESS != result) return;
5340 RecordVulkanSurface(pSurface);
5341}
5342#endif // VK_USE_PLATFORM_WAYLAND_KHR
5343
5344#ifdef VK_USE_PLATFORM_WIN32_KHR
5345void ValidationStateTracker::PostCallRecordCreateWin32SurfaceKHR(VkInstance instance,
5346 const VkWin32SurfaceCreateInfoKHR *pCreateInfo,
5347 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface,
5348 VkResult result) {
5349 if (VK_SUCCESS != result) return;
5350 RecordVulkanSurface(pSurface);
5351}
5352#endif // VK_USE_PLATFORM_WIN32_KHR
5353
5354#ifdef VK_USE_PLATFORM_XCB_KHR
5355void ValidationStateTracker::PostCallRecordCreateXcbSurfaceKHR(VkInstance instance, const VkXcbSurfaceCreateInfoKHR *pCreateInfo,
5356 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface,
5357 VkResult result) {
5358 if (VK_SUCCESS != result) return;
5359 RecordVulkanSurface(pSurface);
5360}
5361#endif // VK_USE_PLATFORM_XCB_KHR
5362
5363#ifdef VK_USE_PLATFORM_XLIB_KHR
5364void ValidationStateTracker::PostCallRecordCreateXlibSurfaceKHR(VkInstance instance, const VkXlibSurfaceCreateInfoKHR *pCreateInfo,
5365 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface,
5366 VkResult result) {
5367 if (VK_SUCCESS != result) return;
5368 RecordVulkanSurface(pSurface);
5369}
5370#endif // VK_USE_PLATFORM_XLIB_KHR
5371
Niklas Haas8b84af12020-04-19 22:20:11 +02005372void ValidationStateTracker::PostCallRecordCreateHeadlessSurfaceEXT(VkInstance instance,
5373 const VkHeadlessSurfaceCreateInfoEXT *pCreateInfo,
5374 const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface,
5375 VkResult result) {
5376 if (VK_SUCCESS != result) return;
5377 RecordVulkanSurface(pSurface);
5378}
5379
Cort23cf2282019-09-20 18:58:18 +02005380void ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures(VkPhysicalDevice physicalDevice,
Cortffba2642019-09-20 22:09:41 +02005381 VkPhysicalDeviceFeatures *pFeatures) {
5382 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
Yilong Li358152a2020-07-08 02:16:45 -07005383 // Reset the features2 safe struct before setting up the features field.
5384 physical_device_state->features2 = safe_VkPhysicalDeviceFeatures2();
Cortffba2642019-09-20 22:09:41 +02005385 physical_device_state->features2.features = *pFeatures;
Cort23cf2282019-09-20 18:58:18 +02005386}
5387
5388void ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
Cortffba2642019-09-20 22:09:41 +02005389 VkPhysicalDeviceFeatures2 *pFeatures) {
5390 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
Cortffba2642019-09-20 22:09:41 +02005391 physical_device_state->features2.initialize(pFeatures);
Cort23cf2282019-09-20 18:58:18 +02005392}
5393
5394void ValidationStateTracker::PostCallRecordGetPhysicalDeviceFeatures2KHR(VkPhysicalDevice physicalDevice,
Cortffba2642019-09-20 22:09:41 +02005395 VkPhysicalDeviceFeatures2 *pFeatures) {
5396 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
Cortffba2642019-09-20 22:09:41 +02005397 physical_device_state->features2.initialize(pFeatures);
Cort23cf2282019-09-20 18:58:18 +02005398}
5399
locke-lunargd556cc32019-09-17 01:21:23 -06005400void ValidationStateTracker::PostCallRecordGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,
5401 VkSurfaceKHR surface,
5402 VkSurfaceCapabilitiesKHR *pSurfaceCapabilities,
5403 VkResult result) {
5404 if (VK_SUCCESS != result) return;
5405 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
locke-lunargd556cc32019-09-17 01:21:23 -06005406 physical_device_state->surfaceCapabilities = *pSurfaceCapabilities;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005407
5408 // TODO May make sense to move this to BestPractices, but needs further refactoring in CoreChecks first
5409 physical_device_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHR_called = true;
locke-lunargd556cc32019-09-17 01:21:23 -06005410}
5411
5412void ValidationStateTracker::PostCallRecordGetPhysicalDeviceSurfaceCapabilities2KHR(
5413 VkPhysicalDevice physicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
5414 VkSurfaceCapabilities2KHR *pSurfaceCapabilities, VkResult result) {
5415 if (VK_SUCCESS != result) return;
5416 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
locke-lunargd556cc32019-09-17 01:21:23 -06005417 physical_device_state->surfaceCapabilities = pSurfaceCapabilities->surfaceCapabilities;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005418
5419 // TODO May make sense to move this to BestPractices, but needs further refactoring in CoreChecks first
5420 physical_device_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHR_called = true;
locke-lunargd556cc32019-09-17 01:21:23 -06005421}
5422
5423void ValidationStateTracker::PostCallRecordGetPhysicalDeviceSurfaceCapabilities2EXT(VkPhysicalDevice physicalDevice,
5424 VkSurfaceKHR surface,
5425 VkSurfaceCapabilities2EXT *pSurfaceCapabilities,
5426 VkResult result) {
5427 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
locke-lunargd556cc32019-09-17 01:21:23 -06005428 physical_device_state->surfaceCapabilities.minImageCount = pSurfaceCapabilities->minImageCount;
5429 physical_device_state->surfaceCapabilities.maxImageCount = pSurfaceCapabilities->maxImageCount;
5430 physical_device_state->surfaceCapabilities.currentExtent = pSurfaceCapabilities->currentExtent;
5431 physical_device_state->surfaceCapabilities.minImageExtent = pSurfaceCapabilities->minImageExtent;
5432 physical_device_state->surfaceCapabilities.maxImageExtent = pSurfaceCapabilities->maxImageExtent;
5433 physical_device_state->surfaceCapabilities.maxImageArrayLayers = pSurfaceCapabilities->maxImageArrayLayers;
5434 physical_device_state->surfaceCapabilities.supportedTransforms = pSurfaceCapabilities->supportedTransforms;
5435 physical_device_state->surfaceCapabilities.currentTransform = pSurfaceCapabilities->currentTransform;
5436 physical_device_state->surfaceCapabilities.supportedCompositeAlpha = pSurfaceCapabilities->supportedCompositeAlpha;
5437 physical_device_state->surfaceCapabilities.supportedUsageFlags = pSurfaceCapabilities->supportedUsageFlags;
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005438
5439 // TODO May make sense to move this to BestPractices, but needs further refactoring in CoreChecks first
5440 physical_device_state->vkGetPhysicalDeviceSurfaceCapabilitiesKHR_called = true;
locke-lunargd556cc32019-09-17 01:21:23 -06005441}
5442
5443void ValidationStateTracker::PostCallRecordGetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice physicalDevice,
5444 uint32_t queueFamilyIndex, VkSurfaceKHR surface,
5445 VkBool32 *pSupported, VkResult result) {
5446 if (VK_SUCCESS != result) return;
5447 auto surface_state = GetSurfaceState(surface);
5448 surface_state->gpu_queue_support[{physicalDevice, queueFamilyIndex}] = (*pSupported == VK_TRUE);
5449}
5450
5451void ValidationStateTracker::PostCallRecordGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice physicalDevice,
5452 VkSurfaceKHR surface,
5453 uint32_t *pPresentModeCount,
5454 VkPresentModeKHR *pPresentModes,
5455 VkResult result) {
5456 if ((VK_SUCCESS != result) && (VK_INCOMPLETE != result)) return;
5457
5458 // TODO: This isn't quite right -- available modes may differ by surface AND physical device.
5459 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
locke-lunargd556cc32019-09-17 01:21:23 -06005460
5461 if (*pPresentModeCount) {
locke-lunargd556cc32019-09-17 01:21:23 -06005462 if (*pPresentModeCount > physical_device_state->present_modes.size())
5463 physical_device_state->present_modes.resize(*pPresentModeCount);
5464 }
5465 if (pPresentModes) {
locke-lunargd556cc32019-09-17 01:21:23 -06005466 for (uint32_t i = 0; i < *pPresentModeCount; i++) {
5467 physical_device_state->present_modes[i] = pPresentModes[i];
5468 }
5469 }
5470}
5471
5472void ValidationStateTracker::PostCallRecordGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
5473 uint32_t *pSurfaceFormatCount,
5474 VkSurfaceFormatKHR *pSurfaceFormats,
5475 VkResult result) {
5476 if ((VK_SUCCESS != result) && (VK_INCOMPLETE != result)) return;
5477
5478 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
locke-lunargd556cc32019-09-17 01:21:23 -06005479
5480 if (*pSurfaceFormatCount) {
locke-lunargd556cc32019-09-17 01:21:23 -06005481 if (*pSurfaceFormatCount > physical_device_state->surface_formats.size())
5482 physical_device_state->surface_formats.resize(*pSurfaceFormatCount);
5483 }
5484 if (pSurfaceFormats) {
locke-lunargd556cc32019-09-17 01:21:23 -06005485 for (uint32_t i = 0; i < *pSurfaceFormatCount; i++) {
5486 physical_device_state->surface_formats[i] = pSurfaceFormats[i];
5487 }
5488 }
5489}
5490
5491void ValidationStateTracker::PostCallRecordGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice physicalDevice,
5492 const VkPhysicalDeviceSurfaceInfo2KHR *pSurfaceInfo,
5493 uint32_t *pSurfaceFormatCount,
5494 VkSurfaceFormat2KHR *pSurfaceFormats,
5495 VkResult result) {
5496 if ((VK_SUCCESS != result) && (VK_INCOMPLETE != result)) return;
5497
5498 auto physicalDeviceState = GetPhysicalDeviceState(physicalDevice);
5499 if (*pSurfaceFormatCount) {
locke-lunargd556cc32019-09-17 01:21:23 -06005500 if (*pSurfaceFormatCount > physicalDeviceState->surface_formats.size())
5501 physicalDeviceState->surface_formats.resize(*pSurfaceFormatCount);
5502 }
5503 if (pSurfaceFormats) {
locke-lunargd556cc32019-09-17 01:21:23 -06005504 for (uint32_t i = 0; i < *pSurfaceFormatCount; i++) {
5505 physicalDeviceState->surface_formats[i] = pSurfaceFormats[i].surfaceFormat;
5506 }
5507 }
5508}
5509
5510void ValidationStateTracker::PreCallRecordCmdBeginDebugUtilsLabelEXT(VkCommandBuffer commandBuffer,
5511 const VkDebugUtilsLabelEXT *pLabelInfo) {
5512 BeginCmdDebugUtilsLabel(report_data, commandBuffer, pLabelInfo);
5513}
5514
5515void ValidationStateTracker::PostCallRecordCmdEndDebugUtilsLabelEXT(VkCommandBuffer commandBuffer) {
5516 EndCmdDebugUtilsLabel(report_data, commandBuffer);
5517}
5518
5519void ValidationStateTracker::PreCallRecordCmdInsertDebugUtilsLabelEXT(VkCommandBuffer commandBuffer,
5520 const VkDebugUtilsLabelEXT *pLabelInfo) {
5521 InsertCmdDebugUtilsLabel(report_data, commandBuffer, pLabelInfo);
5522
5523 // Squirrel away an easily accessible copy.
5524 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
5525 cb_state->debug_label = LoggingLabel(pLabelInfo);
5526}
5527
5528void ValidationStateTracker::RecordEnumeratePhysicalDeviceGroupsState(
5529 uint32_t *pPhysicalDeviceGroupCount, VkPhysicalDeviceGroupPropertiesKHR *pPhysicalDeviceGroupProperties) {
5530 if (NULL != pPhysicalDeviceGroupProperties) {
5531 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
5532 for (uint32_t j = 0; j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount; j++) {
5533 VkPhysicalDevice cur_phys_dev = pPhysicalDeviceGroupProperties[i].physicalDevices[j];
5534 auto &phys_device_state = physical_device_map[cur_phys_dev];
5535 phys_device_state.phys_device = cur_phys_dev;
5536 // Init actual features for each physical device
5537 DispatchGetPhysicalDeviceFeatures(cur_phys_dev, &phys_device_state.features2.features);
5538 }
5539 }
5540 }
5541}
5542
5543void ValidationStateTracker::PostCallRecordEnumeratePhysicalDeviceGroups(
5544 VkInstance instance, uint32_t *pPhysicalDeviceGroupCount, VkPhysicalDeviceGroupPropertiesKHR *pPhysicalDeviceGroupProperties,
5545 VkResult result) {
5546 if ((VK_SUCCESS != result) && (VK_INCOMPLETE != result)) return;
5547 RecordEnumeratePhysicalDeviceGroupsState(pPhysicalDeviceGroupCount, pPhysicalDeviceGroupProperties);
5548}
5549
5550void ValidationStateTracker::PostCallRecordEnumeratePhysicalDeviceGroupsKHR(
5551 VkInstance instance, uint32_t *pPhysicalDeviceGroupCount, VkPhysicalDeviceGroupPropertiesKHR *pPhysicalDeviceGroupProperties,
5552 VkResult result) {
5553 if ((VK_SUCCESS != result) && (VK_INCOMPLETE != result)) return;
5554 RecordEnumeratePhysicalDeviceGroupsState(pPhysicalDeviceGroupCount, pPhysicalDeviceGroupProperties);
5555}
5556
Lionel Landwerlinc7420912019-05-23 00:33:42 +01005557void ValidationStateTracker::RecordEnumeratePhysicalDeviceQueueFamilyPerformanceQueryCounters(VkPhysicalDevice physicalDevice,
5558 uint32_t queueFamilyIndex,
5559 uint32_t *pCounterCount,
5560 VkPerformanceCounterKHR *pCounters) {
5561 if (NULL == pCounters) return;
5562
5563 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
5564 assert(physical_device_state);
5565
5566 std::unique_ptr<QUEUE_FAMILY_PERF_COUNTERS> queueFamilyCounters(new QUEUE_FAMILY_PERF_COUNTERS());
5567 queueFamilyCounters->counters.resize(*pCounterCount);
5568 for (uint32_t i = 0; i < *pCounterCount; i++) queueFamilyCounters->counters[i] = pCounters[i];
5569
5570 physical_device_state->perf_counters[queueFamilyIndex] = std::move(queueFamilyCounters);
5571}
5572
5573void ValidationStateTracker::PostCallRecordEnumeratePhysicalDeviceQueueFamilyPerformanceQueryCountersKHR(
5574 VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex, uint32_t *pCounterCount, VkPerformanceCounterKHR *pCounters,
5575 VkPerformanceCounterDescriptionKHR *pCounterDescriptions, VkResult result) {
5576 if ((VK_SUCCESS != result) && (VK_INCOMPLETE != result)) return;
5577 RecordEnumeratePhysicalDeviceQueueFamilyPerformanceQueryCounters(physicalDevice, queueFamilyIndex, pCounterCount, pCounters);
5578}
5579
5580void ValidationStateTracker::PostCallRecordAcquireProfilingLockKHR(VkDevice device, const VkAcquireProfilingLockInfoKHR *pInfo,
5581 VkResult result) {
5582 if (result == VK_SUCCESS) performance_lock_acquired = true;
5583}
5584
Lionel Landwerlinc7420912019-05-23 00:33:42 +01005585void ValidationStateTracker::PostCallRecordReleaseProfilingLockKHR(VkDevice device) {
5586 performance_lock_acquired = false;
5587 for (auto &cmd_buffer : commandBufferMap) {
5588 cmd_buffer.second->performance_lock_released = true;
5589 }
5590}
5591
locke-lunargd556cc32019-09-17 01:21:23 -06005592void ValidationStateTracker::PreCallRecordDestroyDescriptorUpdateTemplate(VkDevice device,
5593 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
5594 const VkAllocationCallbacks *pAllocator) {
5595 if (!descriptorUpdateTemplate) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05005596 auto template_state = GetDescriptorTemplateState(descriptorUpdateTemplate);
5597 template_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06005598 desc_template_map.erase(descriptorUpdateTemplate);
5599}
5600
5601void ValidationStateTracker::PreCallRecordDestroyDescriptorUpdateTemplateKHR(VkDevice device,
5602 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
5603 const VkAllocationCallbacks *pAllocator) {
5604 if (!descriptorUpdateTemplate) return;
Jeff Bolze7fc67b2019-10-04 12:29:31 -05005605 auto template_state = GetDescriptorTemplateState(descriptorUpdateTemplate);
5606 template_state->destroyed = true;
locke-lunargd556cc32019-09-17 01:21:23 -06005607 desc_template_map.erase(descriptorUpdateTemplate);
5608}
5609
5610void ValidationStateTracker::RecordCreateDescriptorUpdateTemplateState(const VkDescriptorUpdateTemplateCreateInfoKHR *pCreateInfo,
5611 VkDescriptorUpdateTemplateKHR *pDescriptorUpdateTemplate) {
5612 safe_VkDescriptorUpdateTemplateCreateInfo local_create_info(pCreateInfo);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05005613 auto template_state = std::make_shared<TEMPLATE_STATE>(*pDescriptorUpdateTemplate, &local_create_info);
locke-lunargd556cc32019-09-17 01:21:23 -06005614 desc_template_map[*pDescriptorUpdateTemplate] = std::move(template_state);
5615}
5616
5617void ValidationStateTracker::PostCallRecordCreateDescriptorUpdateTemplate(
5618 VkDevice device, const VkDescriptorUpdateTemplateCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator,
5619 VkDescriptorUpdateTemplateKHR *pDescriptorUpdateTemplate, VkResult result) {
5620 if (VK_SUCCESS != result) return;
5621 RecordCreateDescriptorUpdateTemplateState(pCreateInfo, pDescriptorUpdateTemplate);
5622}
5623
5624void ValidationStateTracker::PostCallRecordCreateDescriptorUpdateTemplateKHR(
5625 VkDevice device, const VkDescriptorUpdateTemplateCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator,
5626 VkDescriptorUpdateTemplateKHR *pDescriptorUpdateTemplate, VkResult result) {
5627 if (VK_SUCCESS != result) return;
5628 RecordCreateDescriptorUpdateTemplateState(pCreateInfo, pDescriptorUpdateTemplate);
5629}
5630
5631void ValidationStateTracker::RecordUpdateDescriptorSetWithTemplateState(VkDescriptorSet descriptorSet,
5632 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
5633 const void *pData) {
5634 auto const template_map_entry = desc_template_map.find(descriptorUpdateTemplate);
5635 if ((template_map_entry == desc_template_map.end()) || (template_map_entry->second.get() == nullptr)) {
5636 assert(0);
5637 } else {
5638 const TEMPLATE_STATE *template_state = template_map_entry->second.get();
5639 // TODO: Record template push descriptor updates
5640 if (template_state->create_info.templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET) {
5641 PerformUpdateDescriptorSetsWithTemplateKHR(descriptorSet, template_state, pData);
5642 }
5643 }
5644}
5645
5646void ValidationStateTracker::PreCallRecordUpdateDescriptorSetWithTemplate(VkDevice device, VkDescriptorSet descriptorSet,
5647 VkDescriptorUpdateTemplate descriptorUpdateTemplate,
5648 const void *pData) {
5649 RecordUpdateDescriptorSetWithTemplateState(descriptorSet, descriptorUpdateTemplate, pData);
5650}
5651
5652void ValidationStateTracker::PreCallRecordUpdateDescriptorSetWithTemplateKHR(VkDevice device, VkDescriptorSet descriptorSet,
5653 VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate,
5654 const void *pData) {
5655 RecordUpdateDescriptorSetWithTemplateState(descriptorSet, descriptorUpdateTemplate, pData);
5656}
5657
5658void ValidationStateTracker::PreCallRecordCmdPushDescriptorSetWithTemplateKHR(
5659 VkCommandBuffer commandBuffer, VkDescriptorUpdateTemplateKHR descriptorUpdateTemplate, VkPipelineLayout layout, uint32_t set,
5660 const void *pData) {
5661 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
5662
5663 const auto template_state = GetDescriptorTemplateState(descriptorUpdateTemplate);
5664 if (template_state) {
5665 auto layout_data = GetPipelineLayout(layout);
5666 auto dsl = GetDslFromPipelineLayout(layout_data, set);
5667 const auto &template_ci = template_state->create_info;
Jeff Bolz6ae39612019-10-11 20:57:36 -05005668 if (dsl && !dsl->destroyed) {
locke-lunargd556cc32019-09-17 01:21:23 -06005669 // Decode the template into a set of write updates
5670 cvdescriptorset::DecodedTemplateUpdate decoded_template(this, VK_NULL_HANDLE, template_state, pData,
5671 dsl->GetDescriptorSetLayout());
5672 RecordCmdPushDescriptorSetState(cb_state, template_ci.pipelineBindPoint, layout, set,
5673 static_cast<uint32_t>(decoded_template.desc_writes.size()),
5674 decoded_template.desc_writes.data());
5675 }
5676 }
5677}
5678
5679void ValidationStateTracker::RecordGetPhysicalDeviceDisplayPlanePropertiesState(VkPhysicalDevice physicalDevice,
5680 uint32_t *pPropertyCount, void *pProperties) {
5681 auto physical_device_state = GetPhysicalDeviceState(physicalDevice);
5682 if (*pPropertyCount) {
locke-lunargd556cc32019-09-17 01:21:23 -06005683 physical_device_state->display_plane_property_count = *pPropertyCount;
5684 }
Nathaniel Cesario24184fe2020-10-06 12:46:12 -06005685 if (*pPropertyCount || pProperties) {
5686 physical_device_state->vkGetPhysicalDeviceDisplayPlanePropertiesKHR_called = true;
locke-lunargd556cc32019-09-17 01:21:23 -06005687 }
5688}
5689
5690void ValidationStateTracker::PostCallRecordGetPhysicalDeviceDisplayPlanePropertiesKHR(VkPhysicalDevice physicalDevice,
5691 uint32_t *pPropertyCount,
5692 VkDisplayPlanePropertiesKHR *pProperties,
5693 VkResult result) {
5694 if ((VK_SUCCESS != result) && (VK_INCOMPLETE != result)) return;
5695 RecordGetPhysicalDeviceDisplayPlanePropertiesState(physicalDevice, pPropertyCount, pProperties);
5696}
5697
5698void ValidationStateTracker::PostCallRecordGetPhysicalDeviceDisplayPlaneProperties2KHR(VkPhysicalDevice physicalDevice,
5699 uint32_t *pPropertyCount,
5700 VkDisplayPlaneProperties2KHR *pProperties,
5701 VkResult result) {
5702 if ((VK_SUCCESS != result) && (VK_INCOMPLETE != result)) return;
5703 RecordGetPhysicalDeviceDisplayPlanePropertiesState(physicalDevice, pPropertyCount, pProperties);
5704}
5705
5706void ValidationStateTracker::PostCallRecordCmdBeginQueryIndexedEXT(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
5707 uint32_t query, VkQueryControlFlags flags, uint32_t index) {
5708 QueryObject query_obj = {queryPool, query, index};
5709 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
5710 RecordCmdBeginQuery(cb_state, query_obj);
5711}
5712
5713void ValidationStateTracker::PostCallRecordCmdEndQueryIndexedEXT(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
5714 uint32_t query, uint32_t index) {
5715 QueryObject query_obj = {queryPool, query, index};
5716 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
5717 RecordCmdEndQuery(cb_state, query_obj);
5718}
5719
5720void ValidationStateTracker::RecordCreateSamplerYcbcrConversionState(const VkSamplerYcbcrConversionCreateInfo *create_info,
5721 VkSamplerYcbcrConversion ycbcr_conversion) {
sfricke-samsungbe3584f2020-04-22 14:58:06 -07005722 auto ycbcr_state = std::make_shared<SAMPLER_YCBCR_CONVERSION_STATE>();
5723
locke-lunargd556cc32019-09-17 01:21:23 -06005724 if (device_extensions.vk_android_external_memory_android_hardware_buffer) {
sfricke-samsungbe3584f2020-04-22 14:58:06 -07005725 RecordCreateSamplerYcbcrConversionANDROID(create_info, ycbcr_conversion, ycbcr_state.get());
locke-lunargd556cc32019-09-17 01:21:23 -06005726 }
sfricke-samsungbe3584f2020-04-22 14:58:06 -07005727
5728 const VkFormat conversion_format = create_info->format;
5729
5730 if (conversion_format != VK_FORMAT_UNDEFINED) {
5731 // If format is VK_FORMAT_UNDEFINED, will be set by external AHB features
5732 ycbcr_state->format_features = GetPotentialFormatFeatures(conversion_format);
5733 }
5734
5735 ycbcr_state->chromaFilter = create_info->chromaFilter;
5736 ycbcr_state->format = conversion_format;
5737 samplerYcbcrConversionMap[ycbcr_conversion] = std::move(ycbcr_state);
locke-lunargd556cc32019-09-17 01:21:23 -06005738}
5739
5740void ValidationStateTracker::PostCallRecordCreateSamplerYcbcrConversion(VkDevice device,
5741 const VkSamplerYcbcrConversionCreateInfo *pCreateInfo,
5742 const VkAllocationCallbacks *pAllocator,
5743 VkSamplerYcbcrConversion *pYcbcrConversion,
5744 VkResult result) {
5745 if (VK_SUCCESS != result) return;
5746 RecordCreateSamplerYcbcrConversionState(pCreateInfo, *pYcbcrConversion);
5747}
5748
5749void ValidationStateTracker::PostCallRecordCreateSamplerYcbcrConversionKHR(VkDevice device,
5750 const VkSamplerYcbcrConversionCreateInfo *pCreateInfo,
5751 const VkAllocationCallbacks *pAllocator,
5752 VkSamplerYcbcrConversion *pYcbcrConversion,
5753 VkResult result) {
5754 if (VK_SUCCESS != result) return;
5755 RecordCreateSamplerYcbcrConversionState(pCreateInfo, *pYcbcrConversion);
5756}
5757
sfricke-samsungbe3584f2020-04-22 14:58:06 -07005758void ValidationStateTracker::RecordDestroySamplerYcbcrConversionState(VkSamplerYcbcrConversion ycbcr_conversion) {
5759 if (device_extensions.vk_android_external_memory_android_hardware_buffer) {
5760 RecordDestroySamplerYcbcrConversionANDROID(ycbcr_conversion);
5761 }
5762
5763 auto ycbcr_state = GetSamplerYcbcrConversionState(ycbcr_conversion);
5764 ycbcr_state->destroyed = true;
5765 samplerYcbcrConversionMap.erase(ycbcr_conversion);
5766}
5767
locke-lunargd556cc32019-09-17 01:21:23 -06005768void ValidationStateTracker::PostCallRecordDestroySamplerYcbcrConversion(VkDevice device, VkSamplerYcbcrConversion ycbcrConversion,
5769 const VkAllocationCallbacks *pAllocator) {
5770 if (!ycbcrConversion) return;
sfricke-samsungbe3584f2020-04-22 14:58:06 -07005771 RecordDestroySamplerYcbcrConversionState(ycbcrConversion);
locke-lunargd556cc32019-09-17 01:21:23 -06005772}
5773
5774void ValidationStateTracker::PostCallRecordDestroySamplerYcbcrConversionKHR(VkDevice device,
5775 VkSamplerYcbcrConversion ycbcrConversion,
5776 const VkAllocationCallbacks *pAllocator) {
5777 if (!ycbcrConversion) return;
sfricke-samsungbe3584f2020-04-22 14:58:06 -07005778 RecordDestroySamplerYcbcrConversionState(ycbcrConversion);
locke-lunargd556cc32019-09-17 01:21:23 -06005779}
5780
Tony-LunarG977448c2019-12-02 14:52:02 -07005781void ValidationStateTracker::RecordResetQueryPool(VkDevice device, VkQueryPool queryPool, uint32_t firstQuery,
5782 uint32_t queryCount) {
locke-lunargd556cc32019-09-17 01:21:23 -06005783 // Do nothing if the feature is not enabled.
Piers Daniell41b8c5d2020-01-10 15:42:00 -07005784 if (!enabled_features.core12.hostQueryReset) return;
locke-lunargd556cc32019-09-17 01:21:23 -06005785
5786 // Do nothing if the query pool has been destroyed.
5787 auto query_pool_state = GetQueryPoolState(queryPool);
5788 if (!query_pool_state) return;
5789
5790 // Reset the state of existing entries.
5791 QueryObject query_obj{queryPool, 0};
5792 const uint32_t max_query_count = std::min(queryCount, query_pool_state->createInfo.queryCount - firstQuery);
5793 for (uint32_t i = 0; i < max_query_count; ++i) {
5794 query_obj.query = firstQuery + i;
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02005795 queryToStateMap[query_obj] = QUERYSTATE_RESET;
Lionel Landwerlinc7420912019-05-23 00:33:42 +01005796 if (query_pool_state->createInfo.queryType == VK_QUERY_TYPE_PERFORMANCE_QUERY_KHR) {
5797 for (uint32_t passIndex = 0; passIndex < query_pool_state->n_performance_passes; passIndex++) {
Lionel Landwerlin7dc796b2020-02-18 18:17:10 +02005798 query_obj.perf_pass = passIndex;
5799 queryToStateMap[query_obj] = QUERYSTATE_RESET;
Lionel Landwerlinc7420912019-05-23 00:33:42 +01005800 }
5801 }
locke-lunargd556cc32019-09-17 01:21:23 -06005802 }
5803}
5804
Tony-LunarG977448c2019-12-02 14:52:02 -07005805void ValidationStateTracker::PostCallRecordResetQueryPoolEXT(VkDevice device, VkQueryPool queryPool, uint32_t firstQuery,
5806 uint32_t queryCount) {
5807 RecordResetQueryPool(device, queryPool, firstQuery, queryCount);
5808}
5809
5810void ValidationStateTracker::PostCallRecordResetQueryPool(VkDevice device, VkQueryPool queryPool, uint32_t firstQuery,
5811 uint32_t queryCount) {
5812 RecordResetQueryPool(device, queryPool, firstQuery, queryCount);
5813}
5814
locke-lunargd556cc32019-09-17 01:21:23 -06005815void ValidationStateTracker::PerformUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet,
5816 const TEMPLATE_STATE *template_state, const void *pData) {
5817 // Translate the templated update into a normal update for validation...
5818 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
5819 cvdescriptorset::PerformUpdateDescriptorSets(this, static_cast<uint32_t>(decoded_update.desc_writes.size()),
5820 decoded_update.desc_writes.data(), 0, NULL);
5821}
5822
5823// Update the common AllocateDescriptorSetsData
5824void ValidationStateTracker::UpdateAllocateDescriptorSetsData(const VkDescriptorSetAllocateInfo *p_alloc_info,
Jeff Bolz46c0ea02019-10-09 13:06:29 -05005825 cvdescriptorset::AllocateDescriptorSetsData *ds_data) const {
locke-lunargd556cc32019-09-17 01:21:23 -06005826 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolz6ae39612019-10-11 20:57:36 -05005827 auto layout = GetDescriptorSetLayoutShared(p_alloc_info->pSetLayouts[i]);
locke-lunargd556cc32019-09-17 01:21:23 -06005828 if (layout) {
5829 ds_data->layout_nodes[i] = layout;
5830 // Count total descriptors required per type
5831 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
5832 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
5833 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
5834 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
5835 }
5836 }
5837 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
5838 }
5839}
5840
5841// Decrement allocated sets from the pool and insert new sets into set_map
5842void ValidationStateTracker::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
5843 const VkDescriptorSet *descriptor_sets,
5844 const cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
5845 auto pool_state = descriptorPoolMap[p_alloc_info->descriptorPool].get();
5846 // Account for sets and individual descriptors allocated from pool
5847 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
5848 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
5849 pool_state->availableDescriptorTypeCount[it->first] -= ds_data->required_descriptors_by_type.at(it->first);
5850 }
5851
5852 const auto *variable_count_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
5853 bool variable_count_valid = variable_count_info && variable_count_info->descriptorSetCount == p_alloc_info->descriptorSetCount;
5854
5855 // Create tracking object for each descriptor set; insert into global map and the pool's set.
5856 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
5857 uint32_t variable_count = variable_count_valid ? variable_count_info->pDescriptorCounts[i] : 0;
5858
Jeff Bolz41a1ced2019-10-11 11:40:49 -05005859 auto new_ds = std::make_shared<cvdescriptorset::DescriptorSet>(descriptor_sets[i], pool_state, ds_data->layout_nodes[i],
John Zulaufd2c3dae2019-12-12 11:02:17 -07005860 variable_count, this);
locke-lunargd556cc32019-09-17 01:21:23 -06005861 pool_state->sets.insert(new_ds.get());
5862 new_ds->in_use.store(0);
5863 setMap[descriptor_sets[i]] = std::move(new_ds);
5864 }
5865}
5866
5867// Generic function to handle state update for all CmdDraw* and CmdDispatch* type functions
Jeremy Kniager05631e72020-06-08 14:21:35 -06005868void ValidationStateTracker::UpdateStateCmdDrawDispatchType(CMD_BUFFER_STATE *cb_state, CMD_TYPE cmd_type,
locke-lunarg540b2252020-08-03 13:23:36 -06005869 VkPipelineBindPoint bind_point, const char *function) {
5870 UpdateDrawState(cb_state, cmd_type, bind_point, function);
locke-lunargd556cc32019-09-17 01:21:23 -06005871 cb_state->hasDispatchCmd = true;
5872}
5873
locke-lunargd556cc32019-09-17 01:21:23 -06005874// Generic function to handle state update for all CmdDraw* type functions
locke-lunarg540b2252020-08-03 13:23:36 -06005875void ValidationStateTracker::UpdateStateCmdDrawType(CMD_BUFFER_STATE *cb_state, CMD_TYPE cmd_type, VkPipelineBindPoint bind_point,
5876 const char *function) {
5877 UpdateStateCmdDrawDispatchType(cb_state, cmd_type, bind_point, function);
locke-lunargd556cc32019-09-17 01:21:23 -06005878 cb_state->hasDrawCmd = true;
5879}
5880
5881void ValidationStateTracker::PostCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
5882 uint32_t firstVertex, uint32_t firstInstance) {
5883 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
locke-lunarg540b2252020-08-03 13:23:36 -06005884 UpdateStateCmdDrawType(cb_state, CMD_DRAW, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw()");
locke-lunargd556cc32019-09-17 01:21:23 -06005885}
5886
5887void ValidationStateTracker::PostCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount,
5888 uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset,
5889 uint32_t firstInstance) {
5890 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
locke-lunarg540b2252020-08-03 13:23:36 -06005891 UpdateStateCmdDrawType(cb_state, CMD_DRAWINDEXED, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed()");
locke-lunargd556cc32019-09-17 01:21:23 -06005892}
5893
5894void ValidationStateTracker::PostCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
5895 uint32_t count, uint32_t stride) {
5896 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
5897 BUFFER_STATE *buffer_state = GetBufferState(buffer);
locke-lunarg540b2252020-08-03 13:23:36 -06005898 UpdateStateCmdDrawType(cb_state, CMD_DRAWINDIRECT, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect()");
locke-lunargd556cc32019-09-17 01:21:23 -06005899 AddCommandBufferBindingBuffer(cb_state, buffer_state);
5900}
5901
5902void ValidationStateTracker::PostCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer,
5903 VkDeviceSize offset, uint32_t count, uint32_t stride) {
5904 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
5905 BUFFER_STATE *buffer_state = GetBufferState(buffer);
locke-lunarg540b2252020-08-03 13:23:36 -06005906 UpdateStateCmdDrawType(cb_state, CMD_DRAWINDEXEDINDIRECT, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect()");
locke-lunargd556cc32019-09-17 01:21:23 -06005907 AddCommandBufferBindingBuffer(cb_state, buffer_state);
5908}
5909
5910void ValidationStateTracker::PostCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
5911 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
locke-lunarg540b2252020-08-03 13:23:36 -06005912 UpdateStateCmdDrawDispatchType(cb_state, CMD_DISPATCH, VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch()");
locke-lunargd556cc32019-09-17 01:21:23 -06005913}
5914
5915void ValidationStateTracker::PostCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer,
5916 VkDeviceSize offset) {
5917 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
locke-lunarg540b2252020-08-03 13:23:36 -06005918 UpdateStateCmdDrawDispatchType(cb_state, CMD_DISPATCHINDIRECT, VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect()");
locke-lunargd556cc32019-09-17 01:21:23 -06005919 BUFFER_STATE *buffer_state = GetBufferState(buffer);
5920 AddCommandBufferBindingBuffer(cb_state, buffer_state);
5921}
5922
Tony-LunarG977448c2019-12-02 14:52:02 -07005923void ValidationStateTracker::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
5924 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
locke-lunarg540b2252020-08-03 13:23:36 -06005925 uint32_t stride, const char *function) {
Tony-LunarG977448c2019-12-02 14:52:02 -07005926 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
5927 BUFFER_STATE *buffer_state = GetBufferState(buffer);
5928 BUFFER_STATE *count_buffer_state = GetBufferState(countBuffer);
locke-lunarg540b2252020-08-03 13:23:36 -06005929 UpdateStateCmdDrawType(cb_state, CMD_DRAWINDIRECTCOUNT, VK_PIPELINE_BIND_POINT_GRAPHICS, function);
Tony-LunarG977448c2019-12-02 14:52:02 -07005930 AddCommandBufferBindingBuffer(cb_state, buffer_state);
5931 AddCommandBufferBindingBuffer(cb_state, count_buffer_state);
5932}
5933
locke-lunargd556cc32019-09-17 01:21:23 -06005934void ValidationStateTracker::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
5935 VkDeviceSize offset, VkBuffer countBuffer,
5936 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
5937 uint32_t stride) {
locke-lunarg540b2252020-08-03 13:23:36 -06005938 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
5939 "vkCmdDrawIndirectCountKHR()");
Tony-LunarG977448c2019-12-02 14:52:02 -07005940}
5941
5942void ValidationStateTracker::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
5943 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
5944 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg540b2252020-08-03 13:23:36 -06005945 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
5946 "vkCmdDrawIndirectCount()");
Tony-LunarG977448c2019-12-02 14:52:02 -07005947}
5948
5949void ValidationStateTracker::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
5950 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
locke-lunarg540b2252020-08-03 13:23:36 -06005951 uint32_t maxDrawCount, uint32_t stride, const char *function) {
locke-lunargd556cc32019-09-17 01:21:23 -06005952 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
5953 BUFFER_STATE *buffer_state = GetBufferState(buffer);
5954 BUFFER_STATE *count_buffer_state = GetBufferState(countBuffer);
locke-lunarg540b2252020-08-03 13:23:36 -06005955 UpdateStateCmdDrawType(cb_state, CMD_DRAWINDEXEDINDIRECTCOUNT, VK_PIPELINE_BIND_POINT_GRAPHICS, function);
locke-lunargd556cc32019-09-17 01:21:23 -06005956 AddCommandBufferBindingBuffer(cb_state, buffer_state);
5957 AddCommandBufferBindingBuffer(cb_state, count_buffer_state);
5958}
5959
5960void ValidationStateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
5961 VkDeviceSize offset, VkBuffer countBuffer,
5962 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
5963 uint32_t stride) {
locke-lunarg540b2252020-08-03 13:23:36 -06005964 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
5965 "vkCmdDrawIndexedIndirectCountKHR()");
Tony-LunarG977448c2019-12-02 14:52:02 -07005966}
5967
5968void ValidationStateTracker::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer,
5969 VkDeviceSize offset, VkBuffer countBuffer,
5970 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
5971 uint32_t stride) {
locke-lunarg540b2252020-08-03 13:23:36 -06005972 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
5973 "vkCmdDrawIndexedIndirectCount()");
locke-lunargd556cc32019-09-17 01:21:23 -06005974}
5975
5976void ValidationStateTracker::PreCallRecordCmdDrawMeshTasksNV(VkCommandBuffer commandBuffer, uint32_t taskCount,
5977 uint32_t firstTask) {
5978 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
locke-lunarg540b2252020-08-03 13:23:36 -06005979 UpdateStateCmdDrawType(cb_state, CMD_DRAWMESHTASKSNV, VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawMeshTasksNV()");
locke-lunargd556cc32019-09-17 01:21:23 -06005980}
5981
5982void ValidationStateTracker::PreCallRecordCmdDrawMeshTasksIndirectNV(VkCommandBuffer commandBuffer, VkBuffer buffer,
5983 VkDeviceSize offset, uint32_t drawCount, uint32_t stride) {
5984 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
locke-lunarg540b2252020-08-03 13:23:36 -06005985 UpdateStateCmdDrawType(cb_state, CMD_DRAWMESHTASKSINDIRECTNV, VK_PIPELINE_BIND_POINT_GRAPHICS,
5986 "vkCmdDrawMeshTasksIndirectNV()");
locke-lunargd556cc32019-09-17 01:21:23 -06005987 BUFFER_STATE *buffer_state = GetBufferState(buffer);
5988 if (buffer_state) {
5989 AddCommandBufferBindingBuffer(cb_state, buffer_state);
5990 }
5991}
5992
5993void ValidationStateTracker::PreCallRecordCmdDrawMeshTasksIndirectCountNV(VkCommandBuffer commandBuffer, VkBuffer buffer,
5994 VkDeviceSize offset, VkBuffer countBuffer,
5995 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
5996 uint32_t stride) {
5997 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
5998 BUFFER_STATE *buffer_state = GetBufferState(buffer);
5999 BUFFER_STATE *count_buffer_state = GetBufferState(countBuffer);
locke-lunarg540b2252020-08-03 13:23:36 -06006000 UpdateStateCmdDrawType(cb_state, CMD_DRAWMESHTASKSINDIRECTCOUNTNV, VK_PIPELINE_BIND_POINT_GRAPHICS,
6001 "vkCmdDrawMeshTasksIndirectCountNV()");
locke-lunargd556cc32019-09-17 01:21:23 -06006002 if (buffer_state) {
6003 AddCommandBufferBindingBuffer(cb_state, buffer_state);
6004 }
6005 if (count_buffer_state) {
6006 AddCommandBufferBindingBuffer(cb_state, count_buffer_state);
6007 }
6008}
6009
6010void ValidationStateTracker::PostCallRecordCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
6011 const VkAllocationCallbacks *pAllocator,
6012 VkShaderModule *pShaderModule, VkResult result,
6013 void *csm_state_data) {
6014 if (VK_SUCCESS != result) return;
6015 create_shader_module_api_state *csm_state = reinterpret_cast<create_shader_module_api_state *>(csm_state_data);
6016
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06006017 spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled));
locke-lunargd556cc32019-09-17 01:21:23 -06006018 bool is_spirv = (pCreateInfo->pCode[0] == spv::MagicNumber);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05006019 auto new_shader_module = is_spirv ? std::make_shared<SHADER_MODULE_STATE>(pCreateInfo, *pShaderModule, spirv_environment,
6020 csm_state->unique_shader_id)
6021 : std::make_shared<SHADER_MODULE_STATE>();
locke-lunargde3f0fa2020-09-10 11:55:31 -06006022 SetPushConstantUsedInShader(*new_shader_module);
locke-lunargd556cc32019-09-17 01:21:23 -06006023 shaderModuleMap[*pShaderModule] = std::move(new_shader_module);
6024}
6025
6026void ValidationStateTracker::RecordPipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, PIPELINE_STATE *pipeline,
Jeff Bolz46c0ea02019-10-09 13:06:29 -05006027 PIPELINE_STATE::StageState *stage_state) const {
locke-lunargd556cc32019-09-17 01:21:23 -06006028 // Validation shouldn't rely on anything in stage state being valid if the spirv isn't
locke-lunargde3f0fa2020-09-10 11:55:31 -06006029 stage_state->entry_point_name = pStage->pName;
6030 stage_state->shader_state = GetShared<SHADER_MODULE_STATE>(pStage->module);
6031 auto module = stage_state->shader_state.get();
locke-lunargd556cc32019-09-17 01:21:23 -06006032 if (!module->has_valid_spirv) return;
6033
6034 // Validation shouldn't rely on anything in stage state being valid if the entrypoint isn't present
6035 auto entrypoint = FindEntrypoint(module, pStage->pName, pStage->stage);
6036 if (entrypoint == module->end()) return;
6037
locke-lunarg654e3692020-06-04 17:19:15 -06006038 stage_state->stage_flag = pStage->stage;
6039
locke-lunargd556cc32019-09-17 01:21:23 -06006040 // Mark accessible ids
6041 stage_state->accessible_ids = MarkAccessibleIds(module, entrypoint);
6042 ProcessExecutionModes(module, entrypoint, pipeline);
6043
locke-lunarg63e4daf2020-08-17 17:53:25 -06006044 stage_state->descriptor_uses = CollectInterfaceByDescriptorSlot(
6045 module, stage_state->accessible_ids, &stage_state->has_writable_descriptor, &stage_state->has_atomic_descriptor);
locke-lunargd556cc32019-09-17 01:21:23 -06006046 // Capture descriptor uses for the pipeline
locke-lunarg36045992020-08-20 16:54:37 -06006047 for (const auto &use : stage_state->descriptor_uses) {
locke-lunargd556cc32019-09-17 01:21:23 -06006048 // While validating shaders capture which slots are used by the pipeline
John Zulauf649edd52019-10-02 14:39:41 -06006049 const uint32_t slot = use.first.first;
locke-lunarg351c9d82020-10-23 14:43:21 -06006050 pipeline->active_slots[slot][use.first.second].is_writable |= use.second.is_writable;
locke-lunarg36045992020-08-20 16:54:37 -06006051 auto &reqs = pipeline->active_slots[slot][use.first.second].reqs;
locke-lunargd556cc32019-09-17 01:21:23 -06006052 reqs = descriptor_req(reqs | DescriptorTypeToReqs(module, use.second.type_id));
locke-lunarg25b6c352020-08-06 17:44:18 -06006053 if (use.second.is_atomic_operation) reqs = descriptor_req(reqs | DESCRIPTOR_REQ_VIEW_ATOMIC_OPERATION);
locke-lunarg12d20992020-09-21 12:46:49 -06006054 if (use.second.is_sampler_implicitLod_dref_proj) reqs = descriptor_req(reqs | DESCRIPTOR_REQ_SAMPLER_IMPLICITLOD_DREF_PROJ);
locke-lunargae2a43c2020-09-22 17:21:57 -06006055 if (use.second.is_sampler_bias_offset) reqs = descriptor_req(reqs | DESCRIPTOR_REQ_SAMPLER_BIAS_OFFSET);
locke-lunarg12d20992020-09-21 12:46:49 -06006056
John Zulauf649edd52019-10-02 14:39:41 -06006057 pipeline->max_active_slot = std::max(pipeline->max_active_slot, slot);
locke-lunarg36045992020-08-20 16:54:37 -06006058 if (use.second.samplers_used_by_image.size()) {
locke-lunarg654a9052020-10-13 16:28:42 -06006059 auto &samplers_used_by_image = pipeline->active_slots[slot][use.first.second].samplers_used_by_image;
6060 if (use.second.samplers_used_by_image.size() > samplers_used_by_image.size()) {
6061 samplers_used_by_image.resize(use.second.samplers_used_by_image.size());
6062 }
locke-lunarg654a9052020-10-13 16:28:42 -06006063 uint32_t image_index = 0;
6064 for (const auto &samplers : use.second.samplers_used_by_image) {
6065 for (const auto &sampler : samplers) {
locke-lunargb8be8222020-10-20 00:34:37 -06006066 samplers_used_by_image[image_index].emplace(sampler, nullptr);
locke-lunarg654a9052020-10-13 16:28:42 -06006067 }
6068 ++image_index;
6069 }
locke-lunarg36045992020-08-20 16:54:37 -06006070 }
locke-lunargd556cc32019-09-17 01:21:23 -06006071 }
locke-lunarg78486832020-09-09 19:39:42 -06006072
locke-lunarg96dc9632020-06-10 17:22:18 -06006073 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
6074 pipeline->fragmentShader_writable_output_location_list = CollectWritableOutputLocationinFS(*module, *pStage);
6075 }
locke-lunargd556cc32019-09-17 01:21:23 -06006076}
6077
6078void ValidationStateTracker::ResetCommandBufferPushConstantDataIfIncompatible(CMD_BUFFER_STATE *cb_state, VkPipelineLayout layout) {
6079 if (cb_state == nullptr) {
6080 return;
6081 }
6082
6083 const PIPELINE_LAYOUT_STATE *pipeline_layout_state = GetPipelineLayout(layout);
6084 if (pipeline_layout_state == nullptr) {
6085 return;
6086 }
6087
6088 if (cb_state->push_constant_data_ranges != pipeline_layout_state->push_constant_ranges) {
6089 cb_state->push_constant_data_ranges = pipeline_layout_state->push_constant_ranges;
6090 cb_state->push_constant_data.clear();
locke-lunargde3f0fa2020-09-10 11:55:31 -06006091 cb_state->push_constant_data_update.clear();
locke-lunargd556cc32019-09-17 01:21:23 -06006092 uint32_t size_needed = 0;
6093 for (auto push_constant_range : *cb_state->push_constant_data_ranges) {
locke-lunargde3f0fa2020-09-10 11:55:31 -06006094 auto size = push_constant_range.offset + push_constant_range.size;
6095 size_needed = std::max(size_needed, size);
6096
6097 auto stageFlags = push_constant_range.stageFlags;
6098 uint32_t bit_shift = 0;
6099 while (stageFlags) {
6100 if (stageFlags & 1) {
6101 VkShaderStageFlagBits flag = static_cast<VkShaderStageFlagBits>(1 << bit_shift);
6102 const auto it = cb_state->push_constant_data_update.find(flag);
6103
6104 if (it != cb_state->push_constant_data_update.end()) {
6105 if (it->second.size() < push_constant_range.offset) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -06006106 it->second.resize(push_constant_range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -06006107 }
6108 if (it->second.size() < size) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -06006109 it->second.resize(size, PC_Byte_Not_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -06006110 }
6111 } else {
locke-lunarg3d8b8f32020-10-26 17:04:16 -06006112 std::vector<uint8_t> bytes;
6113 bytes.resize(push_constant_range.offset, PC_Byte_Not_Set);
6114 bytes.resize(size, PC_Byte_Not_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -06006115 cb_state->push_constant_data_update[flag] = bytes;
6116 }
6117 }
6118 stageFlags = stageFlags >> 1;
6119 ++bit_shift;
6120 }
locke-lunargd556cc32019-09-17 01:21:23 -06006121 }
6122 cb_state->push_constant_data.resize(size_needed, 0);
6123 }
6124}
John Zulauf22b0fbe2019-10-15 06:26:16 -06006125
6126void ValidationStateTracker::PostCallRecordGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain,
6127 uint32_t *pSwapchainImageCount, VkImage *pSwapchainImages,
6128 VkResult result) {
6129 if ((result != VK_SUCCESS) && (result != VK_INCOMPLETE)) return;
6130 auto swapchain_state = GetSwapchainState(swapchain);
6131
6132 if (*pSwapchainImageCount > swapchain_state->images.size()) swapchain_state->images.resize(*pSwapchainImageCount);
6133
6134 if (pSwapchainImages) {
John Zulauf22b0fbe2019-10-15 06:26:16 -06006135 for (uint32_t i = 0; i < *pSwapchainImageCount; ++i) {
locke-lunargb3584732019-10-28 20:18:36 -06006136 if (swapchain_state->images[i].image != VK_NULL_HANDLE) continue; // Already retrieved this.
John Zulauf22b0fbe2019-10-15 06:26:16 -06006137
6138 // Add imageMap entries for each swapchain image
6139 VkImageCreateInfo image_ci;
6140 image_ci.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
Mark Lobodzinskid3ec86f2020-03-18 11:23:04 -06006141 image_ci.pNext = nullptr; // to be set later
6142 image_ci.flags = 0; // to be updated below
John Zulauf22b0fbe2019-10-15 06:26:16 -06006143 image_ci.imageType = VK_IMAGE_TYPE_2D;
6144 image_ci.format = swapchain_state->createInfo.imageFormat;
6145 image_ci.extent.width = swapchain_state->createInfo.imageExtent.width;
6146 image_ci.extent.height = swapchain_state->createInfo.imageExtent.height;
6147 image_ci.extent.depth = 1;
6148 image_ci.mipLevels = 1;
6149 image_ci.arrayLayers = swapchain_state->createInfo.imageArrayLayers;
6150 image_ci.samples = VK_SAMPLE_COUNT_1_BIT;
6151 image_ci.tiling = VK_IMAGE_TILING_OPTIMAL;
6152 image_ci.usage = swapchain_state->createInfo.imageUsage;
6153 image_ci.sharingMode = swapchain_state->createInfo.imageSharingMode;
6154 image_ci.queueFamilyIndexCount = swapchain_state->createInfo.queueFamilyIndexCount;
6155 image_ci.pQueueFamilyIndices = swapchain_state->createInfo.pQueueFamilyIndices;
6156 image_ci.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
6157
6158 image_ci.pNext = lvl_find_in_chain<VkImageFormatListCreateInfoKHR>(swapchain_state->createInfo.pNext);
6159
6160 if (swapchain_state->createInfo.flags & VK_SWAPCHAIN_CREATE_SPLIT_INSTANCE_BIND_REGIONS_BIT_KHR)
6161 image_ci.flags |= VK_IMAGE_CREATE_SPLIT_INSTANCE_BIND_REGIONS_BIT;
6162 if (swapchain_state->createInfo.flags & VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR)
6163 image_ci.flags |= VK_IMAGE_CREATE_PROTECTED_BIT;
6164 if (swapchain_state->createInfo.flags & VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR)
6165 image_ci.flags |= (VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR);
6166
locke-lunarg296a3c92020-03-25 01:04:29 -06006167 imageMap[pSwapchainImages[i]] = std::make_shared<IMAGE_STATE>(device, pSwapchainImages[i], &image_ci);
John Zulauf22b0fbe2019-10-15 06:26:16 -06006168 auto &image_state = imageMap[pSwapchainImages[i]];
6169 image_state->valid = false;
6170 image_state->create_from_swapchain = swapchain;
6171 image_state->bind_swapchain = swapchain;
6172 image_state->bind_swapchain_imageIndex = i;
Tony-LunarGe64e4fe2020-02-17 16:21:55 -07006173 image_state->is_swapchain_image = true;
locke-lunargb3584732019-10-28 20:18:36 -06006174 swapchain_state->images[i].image = pSwapchainImages[i];
6175 swapchain_state->images[i].bound_images.emplace(pSwapchainImages[i]);
Petr Kraus44f1c482020-04-25 20:09:25 +02006176
6177 AddImageStateProps(*image_state, device, physical_device);
John Zulauf22b0fbe2019-10-15 06:26:16 -06006178 }
6179 }
6180
6181 if (*pSwapchainImageCount) {
John Zulauf22b0fbe2019-10-15 06:26:16 -06006182 swapchain_state->get_swapchain_image_count = *pSwapchainImageCount;
6183 }
6184}
sourav parmar35e7a002020-06-09 17:58:44 -07006185
sourav parmar35e7a002020-06-09 17:58:44 -07006186void ValidationStateTracker::PostCallRecordCmdCopyAccelerationStructureKHR(VkCommandBuffer commandBuffer,
6187 const VkCopyAccelerationStructureInfoKHR *pInfo) {
6188 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6189 if (cb_state) {
sourav parmarcd5fb182020-07-17 12:58:44 -07006190 ACCELERATION_STRUCTURE_STATE_KHR *src_as_state = GetAccelerationStructureStateKHR(pInfo->src);
6191 ACCELERATION_STRUCTURE_STATE_KHR *dst_as_state = GetAccelerationStructureStateKHR(pInfo->dst);
sourav parmar35e7a002020-06-09 17:58:44 -07006192 if (dst_as_state != nullptr && src_as_state != nullptr) {
6193 dst_as_state->built = true;
6194 dst_as_state->build_info_khr = src_as_state->build_info_khr;
6195 AddCommandBufferBindingAccelerationStructure(cb_state, dst_as_state);
6196 AddCommandBufferBindingAccelerationStructure(cb_state, src_as_state);
6197 }
6198 }
6199}
Piers Daniell39842ee2020-07-10 16:42:33 -06006200
6201void ValidationStateTracker::PreCallRecordCmdSetCullModeEXT(VkCommandBuffer commandBuffer, VkCullModeFlags cullMode) {
6202 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6203 cb_state->status |= CBSTATUS_CULL_MODE_SET;
6204 cb_state->static_status &= ~CBSTATUS_CULL_MODE_SET;
6205}
6206
6207void ValidationStateTracker::PreCallRecordCmdSetFrontFaceEXT(VkCommandBuffer commandBuffer, VkFrontFace frontFace) {
6208 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6209 cb_state->status |= CBSTATUS_FRONT_FACE_SET;
6210 cb_state->static_status &= ~CBSTATUS_FRONT_FACE_SET;
6211}
6212
6213void ValidationStateTracker::PreCallRecordCmdSetPrimitiveTopologyEXT(VkCommandBuffer commandBuffer,
6214 VkPrimitiveTopology primitiveTopology) {
6215 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6216 cb_state->primitiveTopology = primitiveTopology;
6217 cb_state->status |= CBSTATUS_PRIMITIVE_TOPOLOGY_SET;
6218 cb_state->static_status &= ~CBSTATUS_PRIMITIVE_TOPOLOGY_SET;
6219}
6220
6221void ValidationStateTracker::PreCallRecordCmdSetViewportWithCountEXT(VkCommandBuffer commandBuffer, uint32_t viewportCount,
6222 const VkViewport *pViewports) {
6223 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6224 cb_state->viewportWithCountMask |= (1u << viewportCount) - 1u;
Tobias Hector6663c9b2020-11-05 10:18:02 +00006225 cb_state->viewportWithCountCount = viewportCount;
Piers Daniell39842ee2020-07-10 16:42:33 -06006226 cb_state->status |= CBSTATUS_VIEWPORT_WITH_COUNT_SET;
6227 cb_state->static_status &= ~CBSTATUS_VIEWPORT_WITH_COUNT_SET;
6228}
6229
6230void ValidationStateTracker::PreCallRecordCmdSetScissorWithCountEXT(VkCommandBuffer commandBuffer, uint32_t scissorCount,
6231 const VkRect2D *pScissors) {
6232 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6233 cb_state->scissorWithCountMask |= (1u << scissorCount) - 1u;
6234 cb_state->status |= CBSTATUS_SCISSOR_WITH_COUNT_SET;
6235 cb_state->static_status &= ~CBSTATUS_SCISSOR_WITH_COUNT_SET;
6236}
6237
6238void ValidationStateTracker::PreCallRecordCmdBindVertexBuffers2EXT(VkCommandBuffer commandBuffer, uint32_t firstBinding,
6239 uint32_t bindingCount, const VkBuffer *pBuffers,
6240 const VkDeviceSize *pOffsets, const VkDeviceSize *pSizes,
6241 const VkDeviceSize *pStrides) {
6242 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6243 if (pStrides) {
6244 cb_state->status |= CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET;
6245 cb_state->static_status &= ~CBSTATUS_VERTEX_INPUT_BINDING_STRIDE_SET;
6246 }
6247
6248 uint32_t end = firstBinding + bindingCount;
6249 if (cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings.size() < end) {
6250 cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings.resize(end);
6251 }
6252
6253 for (uint32_t i = 0; i < bindingCount; ++i) {
6254 auto &vertex_buffer_binding = cb_state->current_vertex_buffer_binding_info.vertex_buffer_bindings[i + firstBinding];
locke-lunarg1ae57d62020-11-18 10:49:19 -07006255 vertex_buffer_binding.buffer_state = GetShared<BUFFER_STATE>(pBuffers[i]);
Piers Daniell39842ee2020-07-10 16:42:33 -06006256 vertex_buffer_binding.offset = pOffsets[i];
6257 vertex_buffer_binding.size = (pSizes) ? pSizes[i] : VK_WHOLE_SIZE;
6258 vertex_buffer_binding.stride = (pStrides) ? pStrides[i] : 0;
6259 // Add binding for this vertex buffer to this commandbuffer
6260 if (pBuffers[i]) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07006261 AddCommandBufferBindingBuffer(cb_state, vertex_buffer_binding.buffer_state.get());
Piers Daniell39842ee2020-07-10 16:42:33 -06006262 }
6263 }
6264}
6265
6266void ValidationStateTracker::PreCallRecordCmdSetDepthTestEnableEXT(VkCommandBuffer commandBuffer, VkBool32 depthTestEnable) {
6267 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6268 cb_state->status |= CBSTATUS_DEPTH_TEST_ENABLE_SET;
6269 cb_state->static_status &= ~CBSTATUS_DEPTH_TEST_ENABLE_SET;
6270}
6271
6272void ValidationStateTracker::PreCallRecordCmdSetDepthWriteEnableEXT(VkCommandBuffer commandBuffer, VkBool32 depthWriteEnable) {
6273 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6274 cb_state->status |= CBSTATUS_DEPTH_WRITE_ENABLE_SET;
6275 cb_state->static_status &= ~CBSTATUS_DEPTH_WRITE_ENABLE_SET;
6276}
6277
6278void ValidationStateTracker::PreCallRecordCmdSetDepthCompareOpEXT(VkCommandBuffer commandBuffer, VkCompareOp depthCompareOp) {
6279 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6280 cb_state->status |= CBSTATUS_DEPTH_COMPARE_OP_SET;
6281 cb_state->static_status &= ~CBSTATUS_DEPTH_COMPARE_OP_SET;
6282}
6283
6284void ValidationStateTracker::PreCallRecordCmdSetDepthBoundsTestEnableEXT(VkCommandBuffer commandBuffer,
6285 VkBool32 depthBoundsTestEnable) {
6286 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6287 cb_state->status |= CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET;
6288 cb_state->static_status &= ~CBSTATUS_DEPTH_BOUNDS_TEST_ENABLE_SET;
6289}
6290void ValidationStateTracker::PreCallRecordCmdSetStencilTestEnableEXT(VkCommandBuffer commandBuffer, VkBool32 stencilTestEnable) {
6291 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6292 cb_state->status |= CBSTATUS_STENCIL_TEST_ENABLE_SET;
6293 cb_state->static_status &= ~CBSTATUS_STENCIL_TEST_ENABLE_SET;
6294}
6295
6296void ValidationStateTracker::PreCallRecordCmdSetStencilOpEXT(VkCommandBuffer commandBuffer, VkStencilFaceFlags faceMask,
6297 VkStencilOp failOp, VkStencilOp passOp, VkStencilOp depthFailOp,
6298 VkCompareOp compareOp) {
6299 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6300 cb_state->status |= CBSTATUS_STENCIL_OP_SET;
6301 cb_state->static_status &= ~CBSTATUS_STENCIL_OP_SET;
6302}
locke-lunarg4189aa22020-10-21 00:23:48 -06006303
6304void ValidationStateTracker::PreCallRecordCmdSetDiscardRectangleEXT(VkCommandBuffer commandBuffer, uint32_t firstDiscardRectangle,
6305 uint32_t discardRectangleCount,
6306 const VkRect2D *pDiscardRectangles) {
6307 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6308 cb_state->status |= CBSTATUS_DISCARD_RECTANGLE_SET;
6309 cb_state->static_status &= ~CBSTATUS_DISCARD_RECTANGLE_SET;
6310}
6311
6312void ValidationStateTracker::PreCallRecordCmdSetSampleLocationsEXT(VkCommandBuffer commandBuffer,
6313 const VkSampleLocationsInfoEXT *pSampleLocationsInfo) {
6314 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6315 cb_state->status |= CBSTATUS_SAMPLE_LOCATIONS_SET;
6316 cb_state->static_status &= ~CBSTATUS_SAMPLE_LOCATIONS_SET;
6317}
6318
6319void ValidationStateTracker::PreCallRecordCmdSetCoarseSampleOrderNV(VkCommandBuffer commandBuffer,
6320 VkCoarseSampleOrderTypeNV sampleOrderType,
6321 uint32_t customSampleOrderCount,
6322 const VkCoarseSampleOrderCustomNV *pCustomSampleOrders) {
6323 CMD_BUFFER_STATE *cb_state = GetCBState(commandBuffer);
6324 cb_state->status |= CBSTATUS_COARSE_SAMPLE_ORDER_SET;
6325 cb_state->static_status &= ~CBSTATUS_COARSE_SAMPLE_ORDER_SET;
6326}