blob: 11dcc83b259f53577b4e5c3c8dac1e699e43c8f1 [file] [log] [blame]
sfricke-samsung691299b2021-01-01 20:48:48 -08001/* Copyright (c) 2015-2021 The Khronos Group Inc.
2 * Copyright (c) 2015-2021 Valve Corporation
3 * Copyright (c) 2015-2021 LunarG, Inc.
4 * Copyright (C) 2015-2021 Google Inc.
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Tobin Ehlis <tobine@google.com>
John Zulaufc483f442017-12-15 14:02:06 -070019 * John Zulauf <jzulauf@lunarg.com>
Jeremy Kniagere6827432020-04-01 09:05:56 -060020 * Jeremy Kniager <jeremyk@lunarg.com>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060021 */
22
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070023#include "chassis.h"
Mark Lobodzinski76d76662019-02-14 14:38:21 -070024#include "core_validation_error_enums.h"
25#include "core_validation.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060026#include "descriptor_sets.h"
John Zulaufd47d0612018-02-16 13:00:34 -070027#include "hash_vk_types.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060028#include "vk_enum_string_helper.h"
29#include "vk_safe_struct.h"
Jeff Bolzfdf96072018-04-10 14:32:18 -050030#include "vk_typemap_helper.h"
Tobin Ehlisc8266452017-04-07 12:20:30 -060031#include "buffer_validation.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060032#include <sstream>
Mark Lobodzinski2eee5d82016-12-02 15:33:18 -070033#include <algorithm>
John Zulauff4c07882019-01-24 14:03:36 -070034#include <array>
John Zulauf1f8174b2018-02-16 12:58:37 -070035#include <memory>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060036
Jeff Bolzfdf96072018-04-10 14:32:18 -050037// ExtendedBinding collects a VkDescriptorSetLayoutBinding and any extended
38// state that comes from a different array/structure so they can stay together
39// while being sorted by binding number.
40struct ExtendedBinding {
Mike Schuchardt2df08912020-12-15 16:28:09 -080041 ExtendedBinding(const VkDescriptorSetLayoutBinding *l, VkDescriptorBindingFlags f) : layout_binding(l), binding_flags(f) {}
Jeff Bolzfdf96072018-04-10 14:32:18 -050042
43 const VkDescriptorSetLayoutBinding *layout_binding;
Mike Schuchardt2df08912020-12-15 16:28:09 -080044 VkDescriptorBindingFlags binding_flags;
Jeff Bolzfdf96072018-04-10 14:32:18 -050045};
46
John Zulauf508d13a2018-01-05 15:10:34 -070047struct BindingNumCmp {
Jeff Bolzfdf96072018-04-10 14:32:18 -050048 bool operator()(const ExtendedBinding &a, const ExtendedBinding &b) const {
49 return a.layout_binding->binding < b.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070050 }
51};
52
John Zulauf613fd982019-06-04 15:14:41 -060053using DescriptorSet = cvdescriptorset::DescriptorSet;
John Zulauf4a015c92019-06-04 09:50:05 -060054using DescriptorSetLayout = cvdescriptorset::DescriptorSetLayout;
John Zulaufd47d0612018-02-16 13:00:34 -070055using DescriptorSetLayoutDef = cvdescriptorset::DescriptorSetLayoutDef;
56using DescriptorSetLayoutId = cvdescriptorset::DescriptorSetLayoutId;
57
John Zulauf34ebf272018-02-16 13:08:47 -070058// Canonical dictionary of DescriptorSetLayoutDef (without any handle/device specific information)
59cvdescriptorset::DescriptorSetLayoutDict descriptor_set_layout_dict;
John Zulaufd47d0612018-02-16 13:00:34 -070060
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060061DescriptorSetLayoutId GetCanonicalId(const VkDescriptorSetLayoutCreateInfo *p_create_info) {
John Zulauf34ebf272018-02-16 13:08:47 -070062 return descriptor_set_layout_dict.look_up(DescriptorSetLayoutDef(p_create_info));
John Zulaufd47d0612018-02-16 13:00:34 -070063}
John Zulauf34ebf272018-02-16 13:08:47 -070064
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060065// Construct DescriptorSetLayout instance from given create info
John Zulauf48a6a702017-12-22 17:14:54 -070066// Proactively reserve and resize as possible, as the reallocation was visible in profiling
John Zulauf1f8174b2018-02-16 12:58:37 -070067cvdescriptorset::DescriptorSetLayoutDef::DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info)
68 : flags_(p_create_info->flags), binding_count_(0), descriptor_count_(0), dynamic_descriptor_count_(0) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -070069 const auto *flags_create_info = LvlFindInChain<VkDescriptorSetLayoutBindingFlagsCreateInfo>(p_create_info->pNext);
Jeff Bolzfdf96072018-04-10 14:32:18 -050070
John Zulauf48a6a702017-12-22 17:14:54 -070071 binding_type_stats_ = {0, 0, 0};
Jeff Bolzfdf96072018-04-10 14:32:18 -050072 std::set<ExtendedBinding, BindingNumCmp> sorted_bindings;
John Zulauf508d13a2018-01-05 15:10:34 -070073 const uint32_t input_bindings_count = p_create_info->bindingCount;
74 // Sort the input bindings in binding number order, eliminating duplicates
75 for (uint32_t i = 0; i < input_bindings_count; i++) {
Mike Schuchardt2df08912020-12-15 16:28:09 -080076 VkDescriptorBindingFlags flags = 0;
Jeff Bolzfdf96072018-04-10 14:32:18 -050077 if (flags_create_info && flags_create_info->bindingCount == p_create_info->bindingCount) {
78 flags = flags_create_info->pBindingFlags[i];
79 }
80 sorted_bindings.insert(ExtendedBinding(p_create_info->pBindings + i, flags));
John Zulaufb6d71202017-12-22 16:47:09 -070081 }
82
83 // Store the create info in the sorted order from above
Tobin Ehlisa3525e02016-11-17 10:50:52 -070084 std::map<uint32_t, uint32_t> binding_to_dyn_count;
John Zulauf508d13a2018-01-05 15:10:34 -070085 uint32_t index = 0;
86 binding_count_ = static_cast<uint32_t>(sorted_bindings.size());
87 bindings_.reserve(binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -050088 binding_flags_.reserve(binding_count_);
John Zulauf508d13a2018-01-05 15:10:34 -070089 binding_to_index_map_.reserve(binding_count_);
John Zulauf79f06582021-02-27 18:38:39 -070090 for (const auto &input_binding : sorted_bindings) {
John Zulauf508d13a2018-01-05 15:10:34 -070091 // Add to binding and map, s.t. it is robust to invalid duplication of binding_num
Jeff Bolzfdf96072018-04-10 14:32:18 -050092 const auto binding_num = input_binding.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070093 binding_to_index_map_[binding_num] = index++;
Jeff Bolzfdf96072018-04-10 14:32:18 -050094 bindings_.emplace_back(input_binding.layout_binding);
John Zulauf508d13a2018-01-05 15:10:34 -070095 auto &binding_info = bindings_.back();
Jeff Bolzfdf96072018-04-10 14:32:18 -050096 binding_flags_.emplace_back(input_binding.binding_flags);
John Zulauf508d13a2018-01-05 15:10:34 -070097
John Zulaufb6d71202017-12-22 16:47:09 -070098 descriptor_count_ += binding_info.descriptorCount;
99 if (binding_info.descriptorCount > 0) {
100 non_empty_bindings_.insert(binding_num);
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700101 }
John Zulaufb6d71202017-12-22 16:47:09 -0700102
sfricke-samsung11b31212021-02-10 00:41:50 -0800103 if (IsDyanmicDescriptor(binding_info.descriptorType)) {
John Zulaufb6d71202017-12-22 16:47:09 -0700104 binding_to_dyn_count[binding_num] = binding_info.descriptorCount;
105 dynamic_descriptor_count_ += binding_info.descriptorCount;
John Zulauf48a6a702017-12-22 17:14:54 -0700106 binding_type_stats_.dynamic_buffer_count++;
107 } else if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
108 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
109 binding_type_stats_.non_dynamic_buffer_count++;
110 } else {
111 binding_type_stats_.image_sampler_count++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600112 }
113 }
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700114 assert(bindings_.size() == binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500115 assert(binding_flags_.size() == binding_count_);
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700116 uint32_t global_index = 0;
John Zulauf7705bfc2019-06-10 09:52:04 -0600117 global_index_range_.reserve(binding_count_);
118 // Vector order is finalized so build vectors of descriptors and dynamic offsets by binding index
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700119 for (uint32_t i = 0; i < binding_count_; ++i) {
John Zulaufc483f442017-12-15 14:02:06 -0700120 auto final_index = global_index + bindings_[i].descriptorCount;
John Zulauf7705bfc2019-06-10 09:52:04 -0600121 global_index_range_.emplace_back(global_index, final_index);
John Zulaufc483f442017-12-15 14:02:06 -0700122 global_index = final_index;
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700123 }
John Zulaufb6d71202017-12-22 16:47:09 -0700124
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700125 // Now create dyn offset array mapping for any dynamic descriptors
126 uint32_t dyn_array_idx = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700127 binding_to_dynamic_array_idx_map_.reserve(binding_to_dyn_count.size());
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700128 for (const auto &bc_pair : binding_to_dyn_count) {
129 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
130 dyn_array_idx += bc_pair.second;
131 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600132}
Tobin Ehlis154c2692016-10-25 09:36:53 -0600133
John Zulaufd47d0612018-02-16 13:00:34 -0700134size_t cvdescriptorset::DescriptorSetLayoutDef::hash() const {
135 hash_util::HashCombiner hc;
136 hc << flags_;
137 hc.Combine(bindings_);
John Zulauf223b69d2018-11-09 16:00:59 -0700138 hc.Combine(binding_flags_);
John Zulaufd47d0612018-02-16 13:00:34 -0700139 return hc.Value();
140}
141//
142
John Zulauf1f8174b2018-02-16 12:58:37 -0700143// Return valid index or "end" i.e. binding_count_;
144// The asserts in "Get" are reduced to the set where no valid answer(like null or 0) could be given
145// Common code for all binding lookups.
146uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromBinding(uint32_t binding) const {
147 const auto &bi_itr = binding_to_index_map_.find(binding);
148 if (bi_itr != binding_to_index_map_.cend()) return bi_itr->second;
149 return GetBindingCount();
150}
151VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorSetLayoutBindingPtrFromIndex(
152 const uint32_t index) const {
153 if (index >= bindings_.size()) return nullptr;
154 return bindings_[index].ptr();
155}
156// Return descriptorCount for given index, 0 if index is unavailable
157uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorCountFromIndex(const uint32_t index) const {
158 if (index >= bindings_.size()) return 0;
159 return bindings_[index].descriptorCount;
160}
161// For the given index, return descriptorType
162VkDescriptorType cvdescriptorset::DescriptorSetLayoutDef::GetTypeFromIndex(const uint32_t index) const {
163 assert(index < bindings_.size());
164 if (index < bindings_.size()) return bindings_[index].descriptorType;
165 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
166}
167// For the given index, return stageFlags
168VkShaderStageFlags cvdescriptorset::DescriptorSetLayoutDef::GetStageFlagsFromIndex(const uint32_t index) const {
169 assert(index < bindings_.size());
170 if (index < bindings_.size()) return bindings_[index].stageFlags;
171 return VkShaderStageFlags(0);
172}
Jeff Bolzfdf96072018-04-10 14:32:18 -0500173// Return binding flags for given index, 0 if index is unavailable
Mike Schuchardt2df08912020-12-15 16:28:09 -0800174VkDescriptorBindingFlags cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorBindingFlagsFromIndex(const uint32_t index) const {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500175 if (index >= binding_flags_.size()) return 0;
176 return binding_flags_[index];
177}
John Zulauf1f8174b2018-02-16 12:58:37 -0700178
John Zulauf7705bfc2019-06-10 09:52:04 -0600179const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromIndex(uint32_t index) const {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700180 const static IndexRange k_invalid_range = {0xFFFFFFFF, 0xFFFFFFFF};
181 if (index >= binding_flags_.size()) return k_invalid_range;
John Zulauf7705bfc2019-06-10 09:52:04 -0600182 return global_index_range_[index];
John Zulauf1f8174b2018-02-16 12:58:37 -0700183}
184
John Zulauf7705bfc2019-06-10 09:52:04 -0600185// For the given binding, return the global index range (half open)
186// As start and end are often needed in pairs, get both with a single lookup.
John Zulauf1f8174b2018-02-16 12:58:37 -0700187const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromBinding(
188 const uint32_t binding) const {
John Zulauf7705bfc2019-06-10 09:52:04 -0600189 uint32_t index = GetIndexFromBinding(binding);
190 return GetGlobalIndexRangeFromIndex(index);
John Zulauf1f8174b2018-02-16 12:58:37 -0700191}
192
193// For given binding, return ptr to ImmutableSampler array
194VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
195 const auto &bi_itr = binding_to_index_map_.find(binding);
196 if (bi_itr != binding_to_index_map_.end()) {
197 return bindings_[bi_itr->second].pImmutableSamplers;
198 }
199 return nullptr;
200}
201// Move to next valid binding having a non-zero binding count
202uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetNextValidBinding(const uint32_t binding) const {
203 auto it = non_empty_bindings_.upper_bound(binding);
204 assert(it != non_empty_bindings_.cend());
205 if (it != non_empty_bindings_.cend()) return *it;
206 return GetMaxBinding() + 1;
207}
208// For given index, return ptr to ImmutableSampler array
209VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
210 if (index < bindings_.size()) {
211 return bindings_[index].pImmutableSamplers;
212 }
213 return nullptr;
214}
John Zulauf9ce3b252019-06-06 15:20:22 -0600215
216// If our layout is compatible with rh_ds_layout, return true.
217bool cvdescriptorset::DescriptorSetLayout::IsCompatible(DescriptorSetLayout const *rh_ds_layout) const {
218 bool compatible = (this == rh_ds_layout) || (GetLayoutDef() == rh_ds_layout->GetLayoutDef());
219 return compatible;
220}
John Zulauf1f8174b2018-02-16 12:58:37 -0700221
John Zulauff43695f2019-09-13 17:56:26 -0600222// TODO: Find a way to add smarts to the autogenerated version of this
223static std::string smart_string_VkShaderStageFlags(VkShaderStageFlags stage_flags) {
224 if (stage_flags == VK_SHADER_STAGE_ALL) {
225 return string_VkShaderStageFlagBits(VK_SHADER_STAGE_ALL);
226 }
227
228 return string_VkShaderStageFlags(stage_flags);
229}
230
231// If our layout is compatible with bound_dsl, return true,
232// else return false and fill in error_msg will description of what causes incompatibility
233bool cvdescriptorset::VerifySetLayoutCompatibility(const debug_report_data *report_data, DescriptorSetLayout const *layout_dsl,
234 DescriptorSetLayout const *bound_dsl, std::string *error_msg) {
235 // Short circuit the detailed check.
236 if (layout_dsl->IsCompatible(bound_dsl)) return true;
237
238 // Do a detailed compatibility check of this lhs def (referenced by layout_dsl), vs. the rhs (layout and def)
John Zulauf9ce3b252019-06-06 15:20:22 -0600239 // Should only be run if trivial accept has failed, and in that context should return false.
John Zulauff43695f2019-09-13 17:56:26 -0600240 VkDescriptorSetLayout layout_dsl_handle = layout_dsl->GetDescriptorSetLayout();
241 VkDescriptorSetLayout bound_dsl_handle = bound_dsl->GetDescriptorSetLayout();
242 DescriptorSetLayoutDef const *layout_ds_layout_def = layout_dsl->GetLayoutDef();
243 DescriptorSetLayoutDef const *bound_ds_layout_def = bound_dsl->GetLayoutDef();
John Zulauf9ce3b252019-06-06 15:20:22 -0600244
245 // Check descriptor counts
John Zulauff43695f2019-09-13 17:56:26 -0600246 const auto bound_total_count = bound_ds_layout_def->GetTotalDescriptorCount();
247 if (layout_ds_layout_def->GetTotalDescriptorCount() != bound_ds_layout_def->GetTotalDescriptorCount()) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700248 std::stringstream error_str;
John Zulauff43695f2019-09-13 17:56:26 -0600249 error_str << report_data->FormatHandle(layout_dsl_handle) << " from pipeline layout has "
250 << layout_ds_layout_def->GetTotalDescriptorCount() << " total descriptors, but "
251 << report_data->FormatHandle(bound_dsl_handle) << ", which is bound, has " << bound_total_count
252 << " total descriptors.";
John Zulauf1f8174b2018-02-16 12:58:37 -0700253 *error_msg = error_str.str();
254 return false; // trivial fail case
255 }
John Zulaufd47d0612018-02-16 13:00:34 -0700256
John Zulauf1f8174b2018-02-16 12:58:37 -0700257 // Descriptor counts match so need to go through bindings one-by-one
258 // and verify that type and stageFlags match
John Zulauff43695f2019-09-13 17:56:26 -0600259 for (const auto &layout_binding : layout_ds_layout_def->GetBindings()) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700260 // TODO : Do we also need to check immutable samplers?
John Zulauff43695f2019-09-13 17:56:26 -0600261 const auto bound_binding = bound_ds_layout_def->GetBindingInfoFromBinding(layout_binding.binding);
262 if (layout_binding.descriptorCount != bound_binding->descriptorCount) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700263 std::stringstream error_str;
John Zulauff43695f2019-09-13 17:56:26 -0600264 error_str << "Binding " << layout_binding.binding << " for " << report_data->FormatHandle(layout_dsl_handle)
265 << " from pipeline layout has a descriptorCount of " << layout_binding.descriptorCount << " but binding "
266 << layout_binding.binding << " for " << report_data->FormatHandle(bound_dsl_handle)
267 << ", which is bound, has a descriptorCount of " << bound_binding->descriptorCount;
John Zulauf1f8174b2018-02-16 12:58:37 -0700268 *error_msg = error_str.str();
269 return false;
John Zulauff43695f2019-09-13 17:56:26 -0600270 } else if (layout_binding.descriptorType != bound_binding->descriptorType) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700271 std::stringstream error_str;
John Zulauff43695f2019-09-13 17:56:26 -0600272 error_str << "Binding " << layout_binding.binding << " for " << report_data->FormatHandle(layout_dsl_handle)
273 << " from pipeline layout is type '" << string_VkDescriptorType(layout_binding.descriptorType)
274 << "' but binding " << layout_binding.binding << " for " << report_data->FormatHandle(bound_dsl_handle)
275 << ", which is bound, is type '" << string_VkDescriptorType(bound_binding->descriptorType) << "'";
John Zulauf1f8174b2018-02-16 12:58:37 -0700276 *error_msg = error_str.str();
277 return false;
John Zulauff43695f2019-09-13 17:56:26 -0600278 } else if (layout_binding.stageFlags != bound_binding->stageFlags) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700279 std::stringstream error_str;
John Zulauff43695f2019-09-13 17:56:26 -0600280 error_str << "Binding " << layout_binding.binding << " for " << report_data->FormatHandle(layout_dsl_handle)
281 << " from pipeline layout has stageFlags " << smart_string_VkShaderStageFlags(layout_binding.stageFlags)
282 << " but binding " << layout_binding.binding << " for " << report_data->FormatHandle(bound_dsl_handle)
283 << ", which is bound, has stageFlags " << smart_string_VkShaderStageFlags(bound_binding->stageFlags);
John Zulauf1f8174b2018-02-16 12:58:37 -0700284 *error_msg = error_str.str();
285 return false;
286 }
287 }
Tony-LunarG692b8b42019-09-30 16:07:26 -0600288
289 const auto &ds_layout_flags = layout_ds_layout_def->GetBindingFlags();
290 const auto &bound_layout_flags = bound_ds_layout_def->GetBindingFlags();
291 if (bound_layout_flags != ds_layout_flags) {
292 std::stringstream error_str;
293 assert(ds_layout_flags.size() == bound_layout_flags.size());
294 size_t i;
295 for (i = 0; i < ds_layout_flags.size(); i++) {
296 if (ds_layout_flags[i] != bound_layout_flags[i]) break;
297 }
298 error_str << report_data->FormatHandle(layout_dsl_handle)
299 << " from pipeline layout does not have the same binding flags at binding " << i << " ( "
300 << string_VkDescriptorBindingFlagsEXT(ds_layout_flags[i]) << " ) as "
301 << report_data->FormatHandle(bound_dsl_handle) << " ( "
302 << string_VkDescriptorBindingFlagsEXT(bound_layout_flags[i]) << " ), which is bound";
303 *error_msg = error_str.str();
304 return false;
305 }
306
John Zulauf9ce3b252019-06-06 15:20:22 -0600307 // No detailed check should succeed if the trivial check failed -- or the dictionary has failed somehow.
308 bool compatible = true;
309 assert(!compatible);
310 return compatible;
John Zulauf1f8174b2018-02-16 12:58:37 -0700311}
312
313bool cvdescriptorset::DescriptorSetLayoutDef::IsNextBindingConsistent(const uint32_t binding) const {
314 if (!binding_to_index_map_.count(binding + 1)) return false;
315 auto const &bi_itr = binding_to_index_map_.find(binding);
316 if (bi_itr != binding_to_index_map_.end()) {
317 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
318 if (next_bi_itr != binding_to_index_map_.end()) {
319 auto type = bindings_[bi_itr->second].descriptorType;
320 auto stage_flags = bindings_[bi_itr->second].stageFlags;
321 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500322 auto flags = binding_flags_[bi_itr->second];
John Zulauf1f8174b2018-02-16 12:58:37 -0700323 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
324 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
Jeff Bolzfdf96072018-04-10 14:32:18 -0500325 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false)) ||
326 (flags != binding_flags_[next_bi_itr->second])) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700327 return false;
328 }
329 return true;
330 }
331 }
332 return false;
333}
John Zulauf1f8174b2018-02-16 12:58:37 -0700334
335// The DescriptorSetLayout stores the per handle data for a descriptor set layout, and references the common defintion for the
336// handle invariant portion
337cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
338 const VkDescriptorSetLayout layout)
Jeff Bolz6ae39612019-10-11 20:57:36 -0500339 : layout_(layout), layout_id_(GetCanonicalId(p_create_info)) {}
John Zulauf1f8174b2018-02-16 12:58:37 -0700340
Tobin Ehlis154c2692016-10-25 09:36:53 -0600341// Validate descriptor set layout create info
John Zulaufd9435c32019-06-05 15:55:36 -0600342bool cvdescriptorset::ValidateDescriptorSetLayoutCreateInfo(
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700343 const ValidationObject *val_obj, const VkDescriptorSetLayoutCreateInfo *create_info, const bool push_descriptor_ext,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500344 const uint32_t max_push_descriptors, const bool descriptor_indexing_ext,
Piers Daniell41b8c5d2020-01-10 15:42:00 -0700345 const VkPhysicalDeviceVulkan12Features *core12_features,
Jeff Bolze54ae892018-09-08 12:16:29 -0500346 const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features,
Tony-LunarGd6744bc2019-08-23 09:57:10 -0600347 const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props, const DeviceExtensions *device_extensions) {
Tobin Ehlis154c2692016-10-25 09:36:53 -0600348 bool skip = false;
349 std::unordered_set<uint32_t> bindings;
John Zulauf0fdeab32018-01-23 11:27:35 -0700350 uint64_t total_descriptors = 0;
351
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700352 const auto *flags_create_info = LvlFindInChain<VkDescriptorSetLayoutBindingFlagsCreateInfo>(create_info->pNext);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500353
354 const bool push_descriptor_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR);
John Zulauf0fdeab32018-01-23 11:27:35 -0700355 if (push_descriptor_set && !push_descriptor_ext) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700356 skip |= val_obj->LogError(val_obj->device, kVUID_Core_DrawState_ExtensionNotEnabled,
357 "Attempted to use %s in %s but its required extension %s has not been enabled.\n",
358 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR",
359 "VkDescriptorSetLayoutCreateInfo::flags", VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
John Zulauf0fdeab32018-01-23 11:27:35 -0700360 }
361
Mike Schuchardt2df08912020-12-15 16:28:09 -0800362 const bool update_after_bind_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500363 if (update_after_bind_set && !descriptor_indexing_ext) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700364 skip |= val_obj->LogError(val_obj->device, kVUID_Core_DrawState_ExtensionNotEnabled,
365 "Attemped to use %s in %s but its required extension %s has not been enabled.\n",
Mike Schuchardt2df08912020-12-15 16:28:09 -0800366 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT",
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700367 "VkDescriptorSetLayoutCreateInfo::flags", VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500368 }
369
John Zulauf0fdeab32018-01-23 11:27:35 -0700370 auto valid_type = [push_descriptor_set](const VkDescriptorType type) {
371 return !push_descriptor_set ||
Dave Houlton142c4cb2018-10-17 15:04:41 -0600372 ((type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && (type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) &&
Jeff Bolze54ae892018-09-08 12:16:29 -0500373 (type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT));
John Zulauf0fdeab32018-01-23 11:27:35 -0700374 };
375
Jeff Bolzfdf96072018-04-10 14:32:18 -0500376 uint32_t max_binding = 0;
377
Tobin Ehlis154c2692016-10-25 09:36:53 -0600378 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
John Zulauf0fdeab32018-01-23 11:27:35 -0700379 const auto &binding_info = create_info->pBindings[i];
Jeff Bolzfdf96072018-04-10 14:32:18 -0500380 max_binding = std::max(max_binding, binding_info.binding);
381
John Zulauf0fdeab32018-01-23 11:27:35 -0700382 if (!bindings.insert(binding_info.binding).second) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700383 skip |= val_obj->LogError(val_obj->device, "VUID-VkDescriptorSetLayoutCreateInfo-binding-00279",
384 "duplicated binding number in VkDescriptorSetLayoutBinding.");
Tobin Ehlis154c2692016-10-25 09:36:53 -0600385 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700386 if (!valid_type(binding_info.descriptorType)) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700387 skip |= val_obj->LogError(val_obj->device,
388 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
389 ? "VUID-VkDescriptorSetLayoutCreateInfo-flags-02208"
390 : "VUID-VkDescriptorSetLayoutCreateInfo-flags-00280",
391 "invalid type %s ,for push descriptors in VkDescriptorSetLayoutBinding entry %" PRIu32 ".",
392 string_VkDescriptorType(binding_info.descriptorType), i);
John Zulauf0fdeab32018-01-23 11:27:35 -0700393 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500394
395 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
sfricke-samsungc1e27c32021-01-16 09:32:49 -0800396 if (!inline_uniform_block_features->inlineUniformBlock) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700397 skip |= val_obj->LogError(
sfricke-samsungc1e27c32021-01-16 09:32:49 -0800398 val_obj->device, "VUID-VkDescriptorSetLayoutBinding-descriptorType-04604",
399 "Creating VkDescriptorSetLayout with descriptor type VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT "
400 "but inlineUniformBlock is not enabled",
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700401 VK_EXT_INLINE_UNIFORM_BLOCK_EXTENSION_NAME);
Tony-LunarGd6744bc2019-08-23 09:57:10 -0600402 } else {
403 if ((binding_info.descriptorCount % 4) != 0) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700404 skip |=
405 val_obj->LogError(val_obj->device, "VUID-VkDescriptorSetLayoutBinding-descriptorType-02209",
406 "descriptorCount =(%" PRIu32 ") must be a multiple of 4", binding_info.descriptorCount);
Tony-LunarGd6744bc2019-08-23 09:57:10 -0600407 }
408 if (binding_info.descriptorCount > inline_uniform_block_props->maxInlineUniformBlockSize) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700409 skip |=
410 val_obj->LogError(val_obj->device, "VUID-VkDescriptorSetLayoutBinding-descriptorType-02210",
411 "descriptorCount =(%" PRIu32 ") must be less than or equal to maxInlineUniformBlockSize",
412 binding_info.descriptorCount);
Tony-LunarGd6744bc2019-08-23 09:57:10 -0600413 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500414 }
415 }
416
Tony-LunarG7337b312020-04-15 16:40:25 -0600417 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
418 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
419 binding_info.pImmutableSamplers && device_extensions->vk_ext_custom_border_color) {
420 const CoreChecks *core_checks = reinterpret_cast<const CoreChecks *>(val_obj);
421 for (uint32_t j = 0; j < binding_info.descriptorCount; j++) {
422 const SAMPLER_STATE *sampler_state = core_checks->GetSamplerState(binding_info.pImmutableSamplers[j]);
423 if (sampler_state && (sampler_state->createInfo.borderColor == VK_BORDER_COLOR_INT_CUSTOM_EXT ||
424 sampler_state->createInfo.borderColor == VK_BORDER_COLOR_FLOAT_CUSTOM_EXT)) {
425 skip |= val_obj->LogError(val_obj->device, "VUID-VkDescriptorSetLayoutBinding-pImmutableSamplers-04009",
426 "Sampler %" PRIu64 " presented as immutable has a custom border color",
427 binding_info.pImmutableSamplers[j]);
428 }
429 }
430 }
431
John Zulauf0fdeab32018-01-23 11:27:35 -0700432 total_descriptors += binding_info.descriptorCount;
Tobin Ehlis154c2692016-10-25 09:36:53 -0600433 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700434
Jeff Bolzfdf96072018-04-10 14:32:18 -0500435 if (flags_create_info) {
436 if (flags_create_info->bindingCount != 0 && flags_create_info->bindingCount != create_info->bindingCount) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700437 skip |= val_obj->LogError(val_obj->device, "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-bindingCount-03002",
438 "VkDescriptorSetLayoutCreateInfo::bindingCount (%d) != "
Mike Schuchardt2df08912020-12-15 16:28:09 -0800439 "VkDescriptorSetLayoutBindingFlagsCreateInfo::bindingCount (%d)",
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700440 create_info->bindingCount, flags_create_info->bindingCount);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500441 }
442
443 if (flags_create_info->bindingCount == create_info->bindingCount) {
444 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
445 const auto &binding_info = create_info->pBindings[i];
446
Mike Schuchardt2df08912020-12-15 16:28:09 -0800447 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT) {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500448 if (!update_after_bind_set) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700449 skip |= val_obj->LogError(val_obj->device, "VUID-VkDescriptorSetLayoutCreateInfo-flags-03000",
450 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500451 }
452
453 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER &&
Piers Daniell41b8c5d2020-01-10 15:42:00 -0700454 !core12_features->descriptorBindingUniformBufferUpdateAfterBind) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700455 skip |= val_obj->LogError(val_obj->device,
456 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-"
457 "descriptorBindingUniformBufferUpdateAfterBind-03005",
458 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500459 }
460 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
461 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
462 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) &&
Piers Daniell41b8c5d2020-01-10 15:42:00 -0700463 !core12_features->descriptorBindingSampledImageUpdateAfterBind) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700464 skip |= val_obj->LogError(val_obj->device,
465 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-"
466 "descriptorBindingSampledImageUpdateAfterBind-03006",
467 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500468 }
469 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE &&
Piers Daniell41b8c5d2020-01-10 15:42:00 -0700470 !core12_features->descriptorBindingStorageImageUpdateAfterBind) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700471 skip |= val_obj->LogError(val_obj->device,
472 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-"
473 "descriptorBindingStorageImageUpdateAfterBind-03007",
474 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500475 }
476 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER &&
Piers Daniell41b8c5d2020-01-10 15:42:00 -0700477 !core12_features->descriptorBindingStorageBufferUpdateAfterBind) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700478 skip |= val_obj->LogError(val_obj->device,
479 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-"
480 "descriptorBindingStorageBufferUpdateAfterBind-03008",
481 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500482 }
483 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
Piers Daniell41b8c5d2020-01-10 15:42:00 -0700484 !core12_features->descriptorBindingUniformTexelBufferUpdateAfterBind) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700485 skip |= val_obj->LogError(val_obj->device,
486 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-"
487 "descriptorBindingUniformTexelBufferUpdateAfterBind-03009",
488 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500489 }
490 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER &&
Piers Daniell41b8c5d2020-01-10 15:42:00 -0700491 !core12_features->descriptorBindingStorageTexelBufferUpdateAfterBind) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700492 skip |= val_obj->LogError(val_obj->device,
493 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-"
494 "descriptorBindingStorageTexelBufferUpdateAfterBind-03010",
495 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500496 }
497 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT ||
498 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
499 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700500 skip |= val_obj->LogError(val_obj->device, "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-None-03011",
501 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500502 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500503
504 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT &&
505 !inline_uniform_block_features->descriptorBindingInlineUniformBlockUpdateAfterBind) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700506 skip |= val_obj->LogError(val_obj->device,
507 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-"
508 "descriptorBindingInlineUniformBlockUpdateAfterBind-02211",
Mike Schuchardt2df08912020-12-15 16:28:09 -0800509 "Invalid flags (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT) for "
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700510 "VkDescriptorSetLayoutBinding entry %" PRIu32
511 " with descriptorBindingInlineUniformBlockUpdateAfterBind not enabled",
512 i);
Jeff Bolze54ae892018-09-08 12:16:29 -0500513 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500514 }
515
Mike Schuchardt2df08912020-12-15 16:28:09 -0800516 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -0700517 if (!core12_features->descriptorBindingUpdateUnusedWhilePending) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700518 skip |= val_obj->LogError(
519 val_obj->device,
Mike Schuchardt65847d92019-12-20 13:50:47 -0800520 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-descriptorBindingUpdateUnusedWhilePending-03012",
Dave Houltond8ed0212018-05-16 17:18:24 -0600521 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500522 }
523 }
524
Mike Schuchardt2df08912020-12-15 16:28:09 -0800525 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -0700526 if (!core12_features->descriptorBindingPartiallyBound) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700527 skip |= val_obj->LogError(
528 val_obj->device,
529 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-descriptorBindingPartiallyBound-03013",
530 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500531 }
532 }
533
Mike Schuchardt2df08912020-12-15 16:28:09 -0800534 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT) {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500535 if (binding_info.binding != max_binding) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700536 skip |= val_obj->LogError(val_obj->device,
537 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-pBindingFlags-03004",
538 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500539 }
540
Piers Daniell41b8c5d2020-01-10 15:42:00 -0700541 if (!core12_features->descriptorBindingVariableDescriptorCount) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700542 skip |= val_obj->LogError(
543 val_obj->device,
Mike Schuchardt65847d92019-12-20 13:50:47 -0800544 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-descriptorBindingVariableDescriptorCount-03014",
Dave Houltond8ed0212018-05-16 17:18:24 -0600545 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500546 }
sfricke-samsung11b31212021-02-10 00:41:50 -0800547 if (IsDyanmicDescriptor(binding_info.descriptorType)) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700548 skip |= val_obj->LogError(val_obj->device,
549 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-pBindingFlags-03015",
550 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500551 }
552 }
553
554 if (push_descriptor_set &&
555 (flags_create_info->pBindingFlags[i] &
Mike Schuchardt2df08912020-12-15 16:28:09 -0800556 (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT |
557 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT))) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700558 skip |= val_obj->LogError(val_obj->device, "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfo-flags-03003",
559 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500560 }
561 }
562 }
563 }
564
John Zulauf0fdeab32018-01-23 11:27:35 -0700565 if ((push_descriptor_set) && (total_descriptors > max_push_descriptors)) {
566 const char *undefined = push_descriptor_ext ? "" : " -- undefined";
Mark Lobodzinskid18de902020-01-15 12:20:37 -0700567 skip |= val_obj->LogError(
568 val_obj->device, "VUID-VkDescriptorSetLayoutCreateInfo-flags-00281",
569 "for push descriptor, total descriptor count in layout (%" PRIu64
570 ") must not be greater than VkPhysicalDevicePushDescriptorPropertiesKHR::maxPushDescriptors (%" PRIu32 "%s).",
571 total_descriptors, max_push_descriptors, undefined);
John Zulauf0fdeab32018-01-23 11:27:35 -0700572 }
573
Tobin Ehlis154c2692016-10-25 09:36:53 -0600574 return skip;
575}
576
Mark Lobodzinskie12b6e32020-06-29 11:44:15 -0600577void cvdescriptorset::AllocateDescriptorSetsData::Init(uint32_t count) {
578 layout_nodes.resize(count);
Mark Lobodzinskie12b6e32020-06-29 11:44:15 -0600579}
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600580
Jeff Bolz41a1ced2019-10-11 11:40:49 -0500581cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, DESCRIPTOR_POOL_STATE *pool_state,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500582 const std::shared_ptr<DescriptorSetLayout const> &layout, uint32_t variable_count,
John Zulaufd2c3dae2019-12-12 11:02:17 -0700583 const cvdescriptorset::DescriptorSet::StateTracker *state_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700584 : some_update_(false),
585 set_(set),
Jeff Bolz41a1ced2019-10-11 11:40:49 -0500586 pool_state_(pool_state),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700587 layout_(layout),
John Zulaufd2c3dae2019-12-12 11:02:17 -0700588 state_data_(state_data),
Jeff Bolzdd4cfa12019-08-11 20:57:51 -0500589 variable_count_(variable_count),
590 change_count_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600591 // Foreach binding, create default descriptors of given type
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700592 descriptors_.reserve(layout_->GetTotalDescriptorCount());
593 descriptor_store_.resize(layout_->GetTotalDescriptorCount());
John Zulaufe4850d42019-12-30 16:10:55 -0700594 auto free_descriptor = descriptor_store_.data();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700595 for (uint32_t i = 0; i < layout_->GetBindingCount(); ++i) {
596 auto type = layout_->GetTypeFromIndex(i);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600597 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700598 case VK_DESCRIPTOR_TYPE_SAMPLER: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700599 auto immut_sampler = layout_->GetImmutableSamplerPtrFromIndex(i);
600 for (uint32_t di = 0; di < layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600601 if (immut_sampler) {
John Zulaufe4850d42019-12-30 16:10:55 -0700602 descriptors_.emplace_back(new ((free_descriptor++)->Sampler())
603 SamplerDescriptor(state_data, immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600604 some_update_ = true; // Immutable samplers are updated at creation
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700605 } else {
John Zulaufe4850d42019-12-30 16:10:55 -0700606 descriptors_.emplace_back(new ((free_descriptor++)->Sampler()) SamplerDescriptor(state_data, nullptr));
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700607 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700608 }
609 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600610 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700611 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700612 auto immut = layout_->GetImmutableSamplerPtrFromIndex(i);
613 for (uint32_t di = 0; di < layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600614 if (immut) {
John Zulaufe4850d42019-12-30 16:10:55 -0700615 descriptors_.emplace_back(new ((free_descriptor++)->ImageSampler())
616 ImageSamplerDescriptor(state_data, immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600617 some_update_ = true; // Immutable samplers are updated at creation
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700618 } else {
John Zulaufe4850d42019-12-30 16:10:55 -0700619 descriptors_.emplace_back(new ((free_descriptor++)->ImageSampler())
620 ImageSamplerDescriptor(state_data, nullptr));
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700621 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700622 }
623 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600624 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700625 // ImageDescriptors
626 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
627 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
628 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700629 for (uint32_t di = 0; di < layout_->GetDescriptorCountFromIndex(i); ++di) {
John Zulaufe4850d42019-12-30 16:10:55 -0700630 descriptors_.emplace_back(new ((free_descriptor++)->Image()) ImageDescriptor(type));
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700631 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700632 break;
633 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
634 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700635 for (uint32_t di = 0; di < layout_->GetDescriptorCountFromIndex(i); ++di) {
John Zulaufe4850d42019-12-30 16:10:55 -0700636 descriptors_.emplace_back(new ((free_descriptor++)->Texel()) TexelDescriptor(type));
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700637 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700638 break;
639 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
640 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
641 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
642 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700643 for (uint32_t di = 0; di < layout_->GetDescriptorCountFromIndex(i); ++di) {
John Zulaufe4850d42019-12-30 16:10:55 -0700644 descriptors_.emplace_back(new ((free_descriptor++)->Buffer()) BufferDescriptor(type));
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700645 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700646 break;
Jeff Bolze54ae892018-09-08 12:16:29 -0500647 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700648 for (uint32_t di = 0; di < layout_->GetDescriptorCountFromIndex(i); ++di) {
John Zulaufe4850d42019-12-30 16:10:55 -0700649 descriptors_.emplace_back(new ((free_descriptor++)->InlineUniform()) InlineUniformDescriptor(type));
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700650 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500651 break;
Eric Werness30127fd2018-10-31 21:01:03 -0700652 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700653 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700654 for (uint32_t di = 0; di < layout_->GetDescriptorCountFromIndex(i); ++di) {
John Zulaufe4850d42019-12-30 16:10:55 -0700655 descriptors_.emplace_back(new ((free_descriptor++)->AccelerationStructure())
656 AccelerationStructureDescriptor(type));
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700657 }
Jeff Bolzfbe51582018-09-13 10:01:35 -0500658 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700659 default:
660 assert(0); // Bad descriptor type specified
661 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600662 }
663 }
664}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600665
Jeff Bolz41a1ced2019-10-11 11:40:49 -0500666cvdescriptorset::DescriptorSet::~DescriptorSet() {}
Chris Forbes57989132016-07-26 17:06:10 +1200667
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600668static std::string StringDescriptorReqViewType(descriptor_req req) {
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700669 std::string result("");
Mark Lobodzinski29f451a2020-02-10 16:15:30 -0700670 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_CUBE_ARRAY; i++) {
Chris Forbes57989132016-07-26 17:06:10 +1200671 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700672 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700673 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200674 }
675 }
676
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700677 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700678
679 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200680}
681
Chris Forbesda01e8d2018-08-27 15:36:57 -0700682static char const *StringDescriptorReqComponentType(descriptor_req req) {
683 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_SINT) return "SINT";
684 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_UINT) return "UINT";
685 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT) return "FLOAT";
686 return "(none)";
687}
688
Jeff Bolz6cede832019-08-09 23:30:39 -0500689unsigned DescriptorRequirementsBitsFromFormat(VkFormat fmt) {
Chris Forbesda01e8d2018-08-27 15:36:57 -0700690 if (FormatIsSInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
691 if (FormatIsUInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
692 if (FormatIsDepthAndStencil(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT | DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
693 if (fmt == VK_FORMAT_UNDEFINED) return 0;
694 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
695 return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
696}
697
Tobin Ehlis3066db62016-08-22 08:12:23 -0600698// Validate that the state of this set is appropriate for the given bindings and dynamic_offsets at Draw time
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600699// This includes validating that all descriptors in the given bindings are updated,
700// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
701// Return true if state is acceptable, or false and write an error message into error string
locke-lunargb8d7a7a2020-10-25 16:01:52 -0600702bool CoreChecks::ValidateDrawState(const DescriptorSet *descriptor_set, const BindingReqMap &bindings,
703 const std::vector<uint32_t> &dynamic_offsets, const CMD_BUFFER_STATE *cb_node,
locke-lunargfc78e932020-11-19 17:06:24 -0700704 const std::vector<IMAGE_VIEW_STATE *> *attachments, const std::vector<SUBPASS_INFO> &subpasses,
705 const char *caller, const DrawDispatchVuid &vuids) const {
Tony-LunarGace473a2020-05-06 12:48:04 -0600706 bool result = false;
locke-lunarg540b2252020-08-03 13:23:36 -0600707 VkFramebuffer framebuffer = cb_node->activeFramebuffer ? cb_node->activeFramebuffer->framebuffer : VK_NULL_HANDLE;
John Zulauf79f06582021-02-27 18:38:39 -0700708 for (const auto &binding_pair : bindings) {
709 const auto binding = binding_pair.first;
John Zulauf382e1912019-06-10 15:27:44 -0600710 DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(), binding);
711 if (binding_it.AtEnd()) { // End at construction is the condition for an invalid binding.
Tony-LunarGace473a2020-05-06 12:48:04 -0600712 auto set = descriptor_set->GetSet();
locke-lunarg1328e8e2020-08-20 12:40:08 -0600713 result |= LogError(set, vuids.descriptor_valid,
Tony-LunarGace473a2020-05-06 12:48:04 -0600714 "%s encountered the following validation error at %s time: Attempting to "
715 "validate DrawState for binding #%u which is an invalid binding for this descriptor set.",
716 report_data->FormatHandle(set).c_str(), caller, binding);
717 return result;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600718 }
Jeff Bolz6cede832019-08-09 23:30:39 -0500719
720 if (binding_it.GetDescriptorBindingFlags() &
Mike Schuchardt2df08912020-12-15 16:28:09 -0800721 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT)) {
Jeff Bolz6cede832019-08-09 23:30:39 -0500722 // Can't validate the descriptor because it may not have been updated,
723 // or the view could have been destroyed
724 continue;
725 }
John Zulauf81dd1f12021-01-26 16:49:16 -0700726 // // This is a record time only path
727 const bool record_time_validate = true;
locke-lunargfc78e932020-11-19 17:06:24 -0700728 result |= ValidateDescriptorSetBindingData(cb_node, descriptor_set, dynamic_offsets, binding_pair, framebuffer, attachments,
John Zulauf81dd1f12021-01-26 16:49:16 -0700729 subpasses, record_time_validate, caller, vuids);
unknown3087a642019-09-26 17:21:05 -0600730 }
Tony-LunarGace473a2020-05-06 12:48:04 -0600731 return result;
unknown3087a642019-09-26 17:21:05 -0600732}
Jeff Bolz6cede832019-08-09 23:30:39 -0500733
locke-lunargb8be8222020-10-20 00:34:37 -0600734bool CoreChecks::ValidateDescriptorSetBindingData(const CMD_BUFFER_STATE *cb_node, const DescriptorSet *descriptor_set,
735 const std::vector<uint32_t> &dynamic_offsets,
John Zulauf79f06582021-02-27 18:38:39 -0700736 const std::pair<const uint32_t, DescriptorRequirement> &binding_info,
Mark Lobodzinski85ebd402020-12-03 12:56:07 -0700737 VkFramebuffer framebuffer, const std::vector<IMAGE_VIEW_STATE *> *attachments,
John Zulauf81dd1f12021-01-26 16:49:16 -0700738 const std::vector<SUBPASS_INFO> &subpasses, bool record_time_validate,
739 const char *caller, const DrawDispatchVuid &vuids) const {
unknown3087a642019-09-26 17:21:05 -0600740 using DescriptorClass = cvdescriptorset::DescriptorClass;
741 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
742 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
743 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
744 using SamplerDescriptor = cvdescriptorset::SamplerDescriptor;
745 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
Jeff Bolz95176d02020-04-01 00:36:16 -0500746 using AccelerationStructureDescriptor = cvdescriptorset::AccelerationStructureDescriptor;
locke-lunarg36045992020-08-20 16:54:37 -0600747 const auto reqs = binding_info.second.reqs;
748 const auto binding = binding_info.first;
unknown3087a642019-09-26 17:21:05 -0600749 DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(), binding);
750 {
John Zulauf382e1912019-06-10 15:27:44 -0600751 // Copy the range, the end range is subject to update based on variable length descriptor arrays.
752 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700753 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Jeff Bolzfdf96072018-04-10 14:32:18 -0500754
John Zulauf382e1912019-06-10 15:27:44 -0600755 if (binding_it.IsVariableDescriptorCount()) {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500756 // Only validate the first N descriptors if it uses variable_count
John Zulauf382e1912019-06-10 15:27:44 -0600757 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
Jeff Bolzfdf96072018-04-10 14:32:18 -0500758 }
759
John Zulaufc483f442017-12-15 14:02:06 -0700760 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
Lockeb994adf2019-03-29 23:52:31 -0600761 uint32_t index = i - index_range.start;
John Zulauf382e1912019-06-10 15:27:44 -0600762 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
Lockeb994adf2019-03-29 23:52:31 -0600763
Jeff Bolz6cede832019-08-09 23:30:39 -0500764 if (descriptor->GetClass() == DescriptorClass::InlineUniform) {
765 // Can't validate the descriptor because it may not have been updated.
Jeff Bolzfdf96072018-04-10 14:32:18 -0500766 continue;
John Zulauf382e1912019-06-10 15:27:44 -0600767 } else if (!descriptor->updated) {
Tony-LunarGace473a2020-05-06 12:48:04 -0600768 auto set = descriptor_set->GetSet();
locke-lunarg1328e8e2020-08-20 12:40:08 -0600769 return LogError(set, vuids.descriptor_valid,
Tony-LunarGace473a2020-05-06 12:48:04 -0600770 "%s encountered the following validation error at %s time: Descriptor in binding #%" PRIu32
771 " index %" PRIu32
772 " is being used in draw but has never been updated via vkUpdateDescriptorSets() or a similar call.",
773 report_data->FormatHandle(set).c_str(), caller, binding, index);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700774 } else {
John Zulauf382e1912019-06-10 15:27:44 -0600775 auto descriptor_class = descriptor->GetClass();
John Zulaufc93c4252019-06-25 09:19:49 -0600776 if (descriptor_class == DescriptorClass::GeneralBuffer) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700777 // Verify that buffers are valid
John Zulauf382e1912019-06-10 15:27:44 -0600778 auto buffer = static_cast<const BufferDescriptor *>(descriptor)->GetBuffer();
Jeff Bolzfaffeb32019-10-04 12:47:16 -0500779 auto buffer_node = static_cast<const BufferDescriptor *>(descriptor)->GetBufferState();
Karl Schultz76d16a42020-11-11 05:05:33 -0700780 if ((!buffer_node && !enabled_features.robustness2_features.nullDescriptor) ||
781 (buffer_node && buffer_node->destroyed)) {
782 auto set = descriptor_set->GetSet();
783 return LogError(set, vuids.descriptor_valid,
784 "%s encountered the following validation error at %s time: Descriptor in "
785 "binding #%" PRIu32 " index %" PRIu32
786 " is using buffer %s that is invalid or has been destroyed.",
787 report_data->FormatHandle(set).c_str(), caller, binding, index,
788 report_data->FormatHandle(buffer).c_str());
789 }
Jeff Bolz165818a2020-05-08 11:19:03 -0500790 if (buffer) {
Karl Schultz76d16a42020-11-11 05:05:33 -0700791 if (!buffer_node->sparse) {
John Zulauf79f06582021-02-27 18:38:39 -0700792 for (const auto *mem_binding : buffer_node->GetBoundMemory()) {
Jeff Bolz165818a2020-05-08 11:19:03 -0500793 if (mem_binding->destroyed) {
794 auto set = descriptor_set->GetSet();
locke-lunarg1328e8e2020-08-20 12:40:08 -0600795 return LogError(set, vuids.descriptor_valid,
Jeff Bolz165818a2020-05-08 11:19:03 -0500796 "%s encountered the following validation error at %s time: Descriptor in "
797 "binding #%" PRIu32 " index %" PRIu32
798 " is uses buffer %s that references invalid memory %s.",
799 report_data->FormatHandle(set).c_str(), caller, binding, index,
800 report_data->FormatHandle(buffer).c_str(),
801 report_data->FormatHandle(mem_binding->mem).c_str());
802 }
Tobin Ehlisc8266452017-04-07 12:20:30 -0600803 }
804 }
locke-lunarg351c9d82020-10-23 14:43:21 -0600805 if (enabled_features.core11.protectedMemory == VK_TRUE) {
806 if (ValidateProtectedBuffer(cb_node, buffer_node, caller, vuids.unprotected_command_buffer,
807 "Buffer is in a descriptorSet")) {
808 return true;
809 }
810 if (binding_info.second.is_writable &&
811 ValidateUnprotectedBuffer(cb_node, buffer_node, caller, vuids.protected_command_buffer,
812 "Buffer is in a descriptorSet")) {
813 return true;
814 }
815 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700816 }
John Zulaufc93c4252019-06-25 09:19:49 -0600817 } else if (descriptor_class == DescriptorClass::ImageSampler || descriptor_class == DescriptorClass::Image) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700818 VkImageView image_view;
819 VkImageLayout image_layout;
Jeff Bolzfaffeb32019-10-04 12:47:16 -0500820 const IMAGE_VIEW_STATE *image_view_state;
locke-lunarg4e1e4632020-10-26 01:52:19 -0600821 std::vector<const SAMPLER_STATE *> sampler_states;
locke-lunarg36045992020-08-20 16:54:37 -0600822
John Zulaufc93c4252019-06-25 09:19:49 -0600823 if (descriptor_class == DescriptorClass::ImageSampler) {
locke-lunarg36045992020-08-20 16:54:37 -0600824 const ImageSamplerDescriptor *image_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
825 image_view = image_descriptor->GetImageView();
826 image_view_state = image_descriptor->GetImageViewState();
827 image_layout = image_descriptor->GetImageLayout();
locke-lunarg4e1e4632020-10-26 01:52:19 -0600828 sampler_states.emplace_back(image_descriptor->GetSamplerState());
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700829 } else {
locke-lunarg36045992020-08-20 16:54:37 -0600830 const ImageDescriptor *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
831 image_view = image_descriptor->GetImageView();
832 image_view_state = image_descriptor->GetImageViewState();
833 image_layout = image_descriptor->GetImageLayout();
locke-lunarg4e1e4632020-10-26 01:52:19 -0600834
835 if (binding_info.second.samplers_used_by_image.size() > index) {
836 for (auto &sampler : binding_info.second.samplers_used_by_image[index]) {
Nathaniel Cesarioaa8dd222021-02-17 23:45:11 -0700837 // NOTE: This check _shouldn't_ be necessary due to the checks made in IsSpecificDescriptorType in
838 // shader_validation.cpp. However, without this check some traces still crash.
839 if (sampler.second &&
840 (sampler.second->GetClass() == cvdescriptorset::DescriptorClass::PlainSampler)) {
locke-lunarg4e1e4632020-10-26 01:52:19 -0600841 const auto *sampler_state =
842 static_cast<const cvdescriptorset::SamplerDescriptor *>(sampler.second)->GetSamplerState();
843 if (sampler_state) sampler_states.emplace_back(sampler_state);
844 }
845 }
846 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700847 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700848
Karl Schultz76d16a42020-11-11 05:05:33 -0700849 if ((!image_view_state && !enabled_features.robustness2_features.nullDescriptor) ||
850 (image_view_state && image_view_state->destroyed)) {
851 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
852 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
853
854 auto set = descriptor_set->GetSet();
855 return LogError(set, vuids.descriptor_valid,
856 "%s encountered the following validation error at %s time: Descriptor in "
857 "binding #%" PRIu32 " index %" PRIu32
858 " is using imageView %s that is invalid or has been destroyed.",
859 report_data->FormatHandle(set).c_str(), caller, binding, index,
860 report_data->FormatHandle(image_view).c_str());
861 }
Jeff Bolz165818a2020-05-08 11:19:03 -0500862 if (image_view) {
Jeff Bolz165818a2020-05-08 11:19:03 -0500863 const auto &image_view_ci = image_view_state->create_info;
locke-lunarg4e1e4632020-10-26 01:52:19 -0600864 const auto *image_state = image_view_state->image_state.get();
865
Jeff Bolz165818a2020-05-08 11:19:03 -0500866 if (reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) {
867 if (~reqs & (1 << image_view_ci.viewType)) {
868 auto set = descriptor_set->GetSet();
869 return LogError(
locke-lunarg1328e8e2020-08-20 12:40:08 -0600870 set, vuids.descriptor_valid,
Jeff Bolz165818a2020-05-08 11:19:03 -0500871 "%s encountered the following validation error at %s time: Descriptor "
872 "in binding #%" PRIu32 " index %" PRIu32 " requires an image view of type %s but got %s.",
873 report_data->FormatHandle(set).c_str(), caller, binding, index,
874 StringDescriptorReqViewType(reqs).c_str(), string_VkImageViewType(image_view_ci.viewType));
875 }
876
877 if (!(reqs & image_view_state->descriptor_format_bits)) {
878 // bad component type
879 auto set = descriptor_set->GetSet();
locke-lunarg1328e8e2020-08-20 12:40:08 -0600880 return LogError(set, vuids.descriptor_valid,
Jeff Bolz165818a2020-05-08 11:19:03 -0500881 "%s encountered the following validation error at %s time: Descriptor in binding "
882 "#%" PRIu32 " index %" PRIu32
883 " requires %s component type, but bound descriptor format is %s.",
884 report_data->FormatHandle(set).c_str(), caller, binding, index,
885 StringDescriptorReqComponentType(reqs), string_VkFormat(image_view_ci.format));
886 }
887 }
888
John Zulauf81dd1f12021-01-26 16:49:16 -0700889 // NOTE: Submit time validation of UPDATE_AFTER_BIND image layout is not possible with the
890 // image layout tracking as currently implemented, so only record_time_validation is done
891 if (!disabled[image_layout_validation] && record_time_validate) {
Jeff Bolz165818a2020-05-08 11:19:03 -0500892 auto image_node = image_view_state->image_state.get();
893 assert(image_node);
894 // Verify Image Layout
895 // No "invalid layout" VUID required for this call, since the optimal_layout parameter is UNDEFINED.
896 bool hit_error = false;
897 VerifyImageLayout(cb_node, image_node, image_view_state->normalized_subresource_range,
898 image_view_ci.subresourceRange.aspectMask, image_layout, VK_IMAGE_LAYOUT_UNDEFINED,
899 caller, kVUIDUndefined, "VUID-VkDescriptorImageInfo-imageLayout-00344", &hit_error);
900 if (hit_error) {
901 auto set = descriptor_set->GetSet();
902 return LogError(
locke-lunarg1328e8e2020-08-20 12:40:08 -0600903 set, vuids.descriptor_valid,
Jeff Bolz165818a2020-05-08 11:19:03 -0500904 "%s encountered the following validation error at %s time: Image layout specified "
905 "at vkUpdateDescriptorSet* or vkCmdPushDescriptorSet* time "
906 "doesn't match actual image layout at time descriptor is used. See previous error callback for "
907 "specific details.",
908 report_data->FormatHandle(set).c_str(), caller);
909 }
910 }
911
912 // Verify Sample counts
913 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_view_state->samples != VK_SAMPLE_COUNT_1_BIT) {
914 auto set = descriptor_set->GetSet();
locke-lunarg1328e8e2020-08-20 12:40:08 -0600915 return LogError(set, vuids.descriptor_valid,
Jeff Bolz165818a2020-05-08 11:19:03 -0500916 "%s encountered the following validation error at %s time: Descriptor in "
917 "binding #%" PRIu32 " index %" PRIu32
918 " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got %s.",
919 report_data->FormatHandle(set).c_str(), caller, binding, index,
920 string_VkSampleCountFlagBits(image_view_state->samples));
921 }
922 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_view_state->samples == VK_SAMPLE_COUNT_1_BIT) {
Tony-LunarGace473a2020-05-06 12:48:04 -0600923 auto set = descriptor_set->GetSet();
924 return LogError(
locke-lunarg1328e8e2020-08-20 12:40:08 -0600925 set, vuids.descriptor_valid,
Jeff Bolz165818a2020-05-08 11:19:03 -0500926 "%s encountered the following validation error at %s time: Descriptor in binding #%" PRIu32
927 " index %" PRIu32 " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.",
928 report_data->FormatHandle(set).c_str(), caller, binding, index);
Jeff Bolz6cede832019-08-09 23:30:39 -0500929 }
locke-lunarg25b6c352020-08-06 17:44:18 -0600930
John Zulauf79f06582021-02-27 18:38:39 -0700931 const VkDescriptorType descriptor_type = binding_it.GetType();
locke-lunarg540b2252020-08-03 13:23:36 -0600932
locke-lunarg25b6c352020-08-06 17:44:18 -0600933 // Verify VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT
934 if ((reqs & DESCRIPTOR_REQ_VIEW_ATOMIC_OPERATION) &&
locke-lunarg540b2252020-08-03 13:23:36 -0600935 (descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) &&
locke-lunarg25b6c352020-08-06 17:44:18 -0600936 !(image_view_state->format_features & VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT)) {
937 auto set = descriptor_set->GetSet();
938 LogObjectList objlist(set);
939 objlist.add(image_view);
940 return LogError(
941 objlist, vuids.imageview_atomic,
942 "%s encountered the following validation error at %s time: Descriptor in binding #%" PRIu32
943 " index %" PRIu32
944 ", %s, format %s, doesn't "
945 "contain VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT.",
946 report_data->FormatHandle(set).c_str(), caller, binding, index,
947 report_data->FormatHandle(image_view).c_str(), string_VkFormat(image_view_ci.format));
948 }
locke-lunarg540b2252020-08-03 13:23:36 -0600949
950 // Verify if attachments are used in DescriptorSet
locke-lunargfc78e932020-11-19 17:06:24 -0700951 if (attachments && attachments->size() > 0 && (descriptor_type != VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
locke-lunarg5e1bdde2020-11-16 17:08:44 -0700952 bool ds_aspect = (image_view_state->create_info.subresourceRange.aspectMask &
953 (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))
954 ? true
955 : false;
locke-lunargfc78e932020-11-19 17:06:24 -0700956 uint32_t att_index = 0;
957 for (const auto &view_state : *attachments) {
958 if (!subpasses[att_index].used || !view_state || view_state->destroyed) {
locke-lunarg1ae57d62020-11-18 10:49:19 -0700959 continue;
960 }
locke-lunargfc78e932020-11-19 17:06:24 -0700961 if (ds_aspect && subpasses[att_index].usage == VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
locke-lunarg5e1bdde2020-11-16 17:08:44 -0700962 if ((image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL ||
963 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL ||
964 image_layout == VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL) &&
locke-lunargfc78e932020-11-19 17:06:24 -0700965 (subpasses[att_index].layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL ||
966 subpasses[att_index].layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL ||
967 subpasses[att_index].layout == VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL)) {
locke-lunarg5e1bdde2020-11-16 17:08:44 -0700968 continue;
969 }
970 if ((image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL &&
locke-lunargfc78e932020-11-19 17:06:24 -0700971 subpasses[att_index].layout ==
972 VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL) ||
973 (subpasses[att_index].layout ==
974 VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL &&
locke-lunarg5e1bdde2020-11-16 17:08:44 -0700975 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)) {
976 continue;
977 }
978 }
locke-lunargfc78e932020-11-19 17:06:24 -0700979 if (view_state->image_view == image_view) {
locke-lunarg540b2252020-08-03 13:23:36 -0600980 auto set = descriptor_set->GetSet();
981 LogObjectList objlist(set);
982 objlist.add(image_view);
983 objlist.add(framebuffer);
984 return LogError(objlist, vuids.image_subresources,
985 "%s encountered the following validation error at %s time: %s is used in "
986 "Descriptor in binding #%" PRIu32 " index %" PRIu32
987 " and %s attachment # %" PRIu32 ".",
988 report_data->FormatHandle(set).c_str(), caller,
989 report_data->FormatHandle(image_view).c_str(), binding, index,
locke-lunargfc78e932020-11-19 17:06:24 -0700990 report_data->FormatHandle(framebuffer).c_str(), att_index);
991 } else {
992 if (image_view_state->OverlapSubresource(*view_state)) {
locke-lunarg540b2252020-08-03 13:23:36 -0600993 auto set = descriptor_set->GetSet();
994 LogObjectList objlist(set);
995 objlist.add(image_view);
996 objlist.add(framebuffer);
locke-lunargfc78e932020-11-19 17:06:24 -0700997 objlist.add(view_state->image_view);
locke-lunarg540b2252020-08-03 13:23:36 -0600998 return LogError(
999 objlist, vuids.image_subresources,
1000 "%s encountered the following validation error at %s time: Image subresources of %s in "
1001 "Descriptor in binding #%" PRIu32 " index %" PRIu32
1002 " and %s in %s attachment # %" PRIu32 " overlap.",
1003 report_data->FormatHandle(set).c_str(), caller,
1004 report_data->FormatHandle(image_view).c_str(), binding, index,
locke-lunargfc78e932020-11-19 17:06:24 -07001005 report_data->FormatHandle(view_state->image_view).c_str(),
1006 report_data->FormatHandle(framebuffer).c_str(), att_index);
locke-lunarg540b2252020-08-03 13:23:36 -06001007 }
1008 }
locke-lunargfc78e932020-11-19 17:06:24 -07001009 ++att_index;
locke-lunarg540b2252020-08-03 13:23:36 -06001010 }
locke-lunarg351c9d82020-10-23 14:43:21 -06001011 if (enabled_features.core11.protectedMemory == VK_TRUE) {
1012 if (ValidateProtectedImage(cb_node, image_view_state->image_state.get(), caller,
1013 vuids.unprotected_command_buffer, "Image is in a descriptorSet")) {
1014 return true;
1015 }
1016 if (binding_info.second.is_writable &&
1017 ValidateUnprotectedImage(cb_node, image_view_state->image_state.get(), caller,
1018 vuids.protected_command_buffer, "Image is in a descriptorSet")) {
1019 return true;
1020 }
1021 }
locke-lunarg540b2252020-08-03 13:23:36 -06001022 }
locke-lunarg36045992020-08-20 16:54:37 -06001023
locke-lunarg4e1e4632020-10-26 01:52:19 -06001024 for (const auto *sampler_state : sampler_states) {
1025 if (!sampler_state || sampler_state->destroyed) {
1026 continue;
1027 }
locke-lunarg654a9052020-10-13 16:28:42 -06001028
locke-lunarg4e1e4632020-10-26 01:52:19 -06001029 // TODO: Validate 04015 for DescriptorClass::PlainSampler
1030 if ((sampler_state->createInfo.borderColor == VK_BORDER_COLOR_INT_CUSTOM_EXT ||
1031 sampler_state->createInfo.borderColor == VK_BORDER_COLOR_FLOAT_CUSTOM_EXT) &&
1032 (sampler_state->customCreateInfo.format == VK_FORMAT_UNDEFINED)) {
1033 if (image_view_state->create_info.format == VK_FORMAT_B4G4R4A4_UNORM_PACK16 ||
1034 image_view_state->create_info.format == VK_FORMAT_B5G6R5_UNORM_PACK16 ||
1035 image_view_state->create_info.format == VK_FORMAT_B5G5R5A1_UNORM_PACK16) {
1036 auto set = descriptor_set->GetSet();
1037 LogObjectList objlist(set);
1038 objlist.add(sampler_state->sampler);
1039 objlist.add(image_view_state->image_view);
1040 return LogError(objlist, "VUID-VkSamplerCustomBorderColorCreateInfoEXT-format-04015",
1041 "%s encountered the following validation error at %s time: Sampler %s in "
1042 "binding #%" PRIu32 " index %" PRIu32
1043 " has a custom border color with format = VK_FORMAT_UNDEFINED and is used to "
1044 "sample an image view %s with format %s",
1045 report_data->FormatHandle(set).c_str(), caller,
1046 report_data->FormatHandle(sampler_state->sampler).c_str(), binding, index,
1047 report_data->FormatHandle(image_view_state->image_view).c_str(),
1048 string_VkFormat(image_view_state->create_info.format));
1049 }
1050 }
1051 VkFilter sampler_mag_filter = sampler_state->createInfo.magFilter;
1052 VkFilter sampler_min_filter = sampler_state->createInfo.minFilter;
1053 VkBool32 sampler_compare_enable = sampler_state->createInfo.compareEnable;
1054 if ((sampler_mag_filter == VK_FILTER_LINEAR || sampler_min_filter == VK_FILTER_LINEAR) &&
1055 (sampler_compare_enable == VK_FALSE) &&
1056 !(image_view_state->format_features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)) {
locke-lunarg36045992020-08-20 16:54:37 -06001057 auto set = descriptor_set->GetSet();
1058 LogObjectList objlist(set);
locke-lunarg36045992020-08-20 16:54:37 -06001059 objlist.add(sampler_state->sampler);
locke-lunarg4e1e4632020-10-26 01:52:19 -06001060 objlist.add(image_view_state->image_view);
1061 return LogError(objlist, vuids.linear_sampler,
sfricke-samsung691299b2021-01-01 20:48:48 -08001062 "sampler (%s) in descriptor set (%s) is set to use VK_FILTER_LINEAR with "
1063 "compareEnable is set to VK_TRUE, but image view's (%s) format (%s) does not "
locke-lunarg4e1e4632020-10-26 01:52:19 -06001064 "contain VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT in its format features.",
1065 report_data->FormatHandle(sampler_state->sampler).c_str(),
1066 report_data->FormatHandle(set).c_str(),
1067 report_data->FormatHandle(image_view_state->image_view).c_str(),
1068 string_VkFormat(image_view_state->create_info.format));
locke-lunarg36045992020-08-20 16:54:37 -06001069 }
locke-lunarg9939d4b2020-10-26 20:11:08 -06001070 if (sampler_mag_filter == VK_FILTER_CUBIC_EXT || sampler_min_filter == VK_FILTER_CUBIC_EXT) {
1071 if (!(image_view_state->format_features & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_EXT)) {
1072 auto set = descriptor_set->GetSet();
1073 LogObjectList objlist(set);
1074 objlist.add(sampler_state->sampler);
1075 objlist.add(image_view_state->image_view);
1076 return LogError(objlist, vuids.cubic_sampler,
Mark Lobodzinski45034452020-11-30 16:56:48 -07001077 "sampler (%s) in descriptor set (%s) is set to use VK_FILTER_CUBIC_EXT, then "
1078 "image view's (%s) format (%s) MUST contain "
1079 "VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_CUBIC_BIT_EXT in its format features.",
locke-lunarg9939d4b2020-10-26 20:11:08 -06001080 report_data->FormatHandle(sampler_state->sampler).c_str(),
1081 report_data->FormatHandle(set).c_str(),
1082 report_data->FormatHandle(image_view_state->image_view).c_str(),
1083 string_VkFormat(image_view_state->create_info.format));
1084 }
1085
Mark Lobodzinski45034452020-11-30 16:56:48 -07001086 if (IsExtEnabled(device_extensions.vk_ext_filter_cubic)) {
1087 const auto reduction_mode_info =
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001088 LvlFindInChain<VkSamplerReductionModeCreateInfo>(sampler_state->createInfo.pNext);
Mark Lobodzinski45034452020-11-30 16:56:48 -07001089 if (reduction_mode_info &&
1090 (reduction_mode_info->reductionMode == VK_SAMPLER_REDUCTION_MODE_MIN ||
1091 reduction_mode_info->reductionMode == VK_SAMPLER_REDUCTION_MODE_MAX) &&
1092 !image_view_state->filter_cubic_props.filterCubicMinmax) {
1093 auto set = descriptor_set->GetSet();
1094 LogObjectList objlist(set);
1095 objlist.add(sampler_state->sampler);
1096 objlist.add(image_view_state->image_view);
1097 return LogError(
1098 objlist, vuids.filter_cubic_min_max,
1099 "sampler (%s) in descriptor set (%s) is set to use VK_FILTER_CUBIC_EXT & %s, "
1100 "but image view (%s) doesn't support filterCubicMinmax.",
1101 report_data->FormatHandle(sampler_state->sampler).c_str(),
1102 string_VkSamplerReductionMode(reduction_mode_info->reductionMode),
1103 report_data->FormatHandle(set).c_str(),
1104 report_data->FormatHandle(image_view_state->image_view).c_str());
1105 }
1106
1107 if (!image_view_state->filter_cubic_props.filterCubic) {
1108 auto set = descriptor_set->GetSet();
1109 LogObjectList objlist(set);
1110 objlist.add(sampler_state->sampler);
1111 objlist.add(image_view_state->image_view);
1112 return LogError(objlist, vuids.filter_cubic,
1113 "sampler (%s) in descriptor set (%s) is set to use VK_FILTER_CUBIC_EXT, "
1114 "but image view (%s) doesn't support filterCubic.",
1115 report_data->FormatHandle(sampler_state->sampler).c_str(),
1116 report_data->FormatHandle(set).c_str(),
1117 report_data->FormatHandle(image_view_state->image_view).c_str());
1118 }
locke-lunarg9939d4b2020-10-26 20:11:08 -06001119 }
1120
Mark Lobodzinski45034452020-11-30 16:56:48 -07001121 if (IsExtEnabled(device_extensions.vk_img_filter_cubic)) {
1122 if (image_view_state->create_info.viewType &
1123 (VK_IMAGE_VIEW_TYPE_3D | VK_IMAGE_VIEW_TYPE_CUBE | VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)) {
1124 auto set = descriptor_set->GetSet();
1125 LogObjectList objlist(set);
1126 objlist.add(sampler_state->sampler);
1127 objlist.add(image_view_state->image_view);
1128 return LogError(objlist, vuids.img_filter_cubic,
1129 "sampler (%s) in descriptor set (%s) "
1130 "is set to use VK_FILTER_CUBIC_EXT while the VK_IMG_filter_cubic extension "
1131 "is enabled, but image view (%s) has an invalid imageViewType (%s).",
1132 report_data->FormatHandle(sampler_state->sampler).c_str(),
1133 report_data->FormatHandle(set).c_str(),
1134 report_data->FormatHandle(image_view_state->image_view).c_str(),
1135 string_VkImageViewType(image_view_state->create_info.viewType));
1136 }
locke-lunarg9939d4b2020-10-26 20:11:08 -06001137 }
locke-lunarg654a9052020-10-13 16:28:42 -06001138 }
1139
locke-lunarg4e1e4632020-10-26 01:52:19 -06001140 if ((image_state->createInfo.flags & VK_IMAGE_CREATE_CORNER_SAMPLED_BIT_NV) &&
1141 (sampler_state->createInfo.addressModeU != VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE ||
1142 sampler_state->createInfo.addressModeV != VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE ||
1143 sampler_state->createInfo.addressModeW != VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE)) {
1144 std::string address_mode_letter =
locke-lunarg9939d4b2020-10-26 20:11:08 -06001145 (sampler_state->createInfo.addressModeU != VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE)
1146 ? "U"
1147 : (sampler_state->createInfo.addressModeV != VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE) ? "V"
1148 : "W";
locke-lunarg4e1e4632020-10-26 01:52:19 -06001149 VkSamplerAddressMode address_mode =
1150 (sampler_state->createInfo.addressModeU != VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE)
1151 ? sampler_state->createInfo.addressModeU
locke-lunarg9939d4b2020-10-26 20:11:08 -06001152 : (sampler_state->createInfo.addressModeV != VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE)
1153 ? sampler_state->createInfo.addressModeV
1154 : sampler_state->createInfo.addressModeW;
locke-lunargae2a43c2020-09-22 17:21:57 -06001155 auto set = descriptor_set->GetSet();
1156 LogObjectList objlist(set);
locke-lunargae2a43c2020-09-22 17:21:57 -06001157 objlist.add(sampler_state->sampler);
locke-lunarg4e1e4632020-10-26 01:52:19 -06001158 objlist.add(image_state->image);
1159 objlist.add(image_view_state->image_view);
1160 return LogError(objlist, vuids.corner_sampled_address_mode,
1161 "image (%s) in image view (%s) in descriptor set (%s) is created with flag "
1162 "VK_IMAGE_CREATE_CORNER_SAMPLED_BIT_NV and can only be sampled using "
1163 "VK_SAMPLER_ADDRESS_MODE_CLAMP_EDGE, but sampler (%s) has "
1164 "createInfo.addressMode%s set to %s.",
1165 report_data->FormatHandle(image_state->image).c_str(),
1166 report_data->FormatHandle(image_view_state->image_view).c_str(),
1167 report_data->FormatHandle(set).c_str(),
1168 report_data->FormatHandle(sampler_state->sampler).c_str(),
1169 address_mode_letter.c_str(), string_VkSamplerAddressMode(address_mode));
1170 }
1171
1172 // UnnormalizedCoordinates sampler validations
1173 if (sampler_state->createInfo.unnormalizedCoordinates) {
1174 // If ImageView is used by a unnormalizedCoordinates sampler, it needs to check ImageView type
1175 if (image_view_ci.viewType == VK_IMAGE_VIEW_TYPE_3D ||
1176 image_view_ci.viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
1177 image_view_ci.viewType == VK_IMAGE_VIEW_TYPE_1D_ARRAY ||
1178 image_view_ci.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY ||
1179 image_view_ci.viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
1180 auto set = descriptor_set->GetSet();
1181 LogObjectList objlist(set);
1182 objlist.add(image_view);
1183 objlist.add(sampler_state->sampler);
1184 return LogError(objlist, vuids.sampler_imageview_type,
1185 "%s encountered the following validation error at %s time: %s, type: %s in "
1186 "Descriptor in binding #%" PRIu32 " index %" PRIu32 "is used by %s.",
1187 report_data->FormatHandle(set).c_str(), caller,
1188 report_data->FormatHandle(image_view).c_str(),
1189 string_VkImageViewType(image_view_ci.viewType), binding, index,
1190 report_data->FormatHandle(sampler_state->sampler).c_str());
1191 }
1192
1193 // sampler must not be used with any of the SPIR-V OpImageSample* or OpImageSparseSample*
1194 // instructions with ImplicitLod, Dref or Proj in their name
1195 if (reqs & DESCRIPTOR_REQ_SAMPLER_IMPLICITLOD_DREF_PROJ) {
1196 auto set = descriptor_set->GetSet();
1197 LogObjectList objlist(set);
1198 objlist.add(image_view);
1199 objlist.add(sampler_state->sampler);
1200 return LogError(objlist, vuids.sampler_implicitLod_dref_proj,
1201 "%s encountered the following validation error at %s time: %s in "
1202 "Descriptor in binding #%" PRIu32 " index %" PRIu32
1203 " is used by %s that uses invalid operator.",
1204 report_data->FormatHandle(set).c_str(), caller,
1205 report_data->FormatHandle(image_view).c_str(), binding, index,
1206 report_data->FormatHandle(sampler_state->sampler).c_str());
1207 }
1208
1209 // sampler must not be used with any of the SPIR-V OpImageSample* or OpImageSparseSample*
1210 // instructions that includes a LOD bias or any offset values
1211 if (reqs & DESCRIPTOR_REQ_SAMPLER_BIAS_OFFSET) {
1212 auto set = descriptor_set->GetSet();
1213 LogObjectList objlist(set);
1214 objlist.add(image_view);
1215 objlist.add(sampler_state->sampler);
1216 return LogError(objlist, vuids.sampler_bias_offset,
1217 "%s encountered the following validation error at %s time: %s in "
1218 "Descriptor in binding #%" PRIu32 " index %" PRIu32
1219 " is used by %s that uses invalid bias or offset operator.",
1220 report_data->FormatHandle(set).c_str(), caller,
1221 report_data->FormatHandle(image_view).c_str(), binding, index,
1222 report_data->FormatHandle(sampler_state->sampler).c_str());
1223 }
locke-lunargae2a43c2020-09-22 17:21:57 -06001224 }
locke-lunarg36045992020-08-20 16:54:37 -06001225 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -07001226 }
John Zulaufc93c4252019-06-25 09:19:49 -06001227 } else if (descriptor_class == DescriptorClass::TexelBuffer) {
John Zulauf382e1912019-06-10 15:27:44 -06001228 auto texel_buffer = static_cast<const TexelDescriptor *>(descriptor);
Jeff Bolzfaffeb32019-10-04 12:47:16 -05001229 auto buffer_view = texel_buffer->GetBufferView();
1230 auto buffer_view_state = texel_buffer->GetBufferViewState();
Chris Forbese92dd1d2019-01-21 15:58:57 -08001231
Karl Schultz76d16a42020-11-11 05:05:33 -07001232 if ((!buffer_view_state && !enabled_features.robustness2_features.nullDescriptor) ||
1233 (buffer_view_state && buffer_view_state->destroyed)) {
1234 auto set = descriptor_set->GetSet();
1235 return LogError(set, vuids.descriptor_valid,
1236 "%s encountered the following validation error at %s time: Descriptor in "
1237 "binding #%" PRIu32 " index %" PRIu32
1238 " is using bufferView %s that is invalid or has been destroyed.",
1239 report_data->FormatHandle(set).c_str(), caller, binding, index,
1240 report_data->FormatHandle(buffer_view).c_str());
1241 }
Jeff Bolz165818a2020-05-08 11:19:03 -05001242 if (buffer_view) {
Jeff Bolz165818a2020-05-08 11:19:03 -05001243 auto buffer = buffer_view_state->create_info.buffer;
1244 auto buffer_state = buffer_view_state->buffer_state.get();
1245 if (buffer_state->destroyed) {
1246 auto set = descriptor_set->GetSet();
locke-lunarg1328e8e2020-08-20 12:40:08 -06001247 return LogError(set, vuids.descriptor_valid,
Jeff Bolz165818a2020-05-08 11:19:03 -05001248 "%s encountered the following validation error at %s time: Descriptor in "
1249 "binding #%" PRIu32 " index %" PRIu32 " is using buffer %s that has been destroyed.",
1250 report_data->FormatHandle(set).c_str(), caller, binding, index,
1251 report_data->FormatHandle(buffer).c_str());
1252 }
1253 auto format_bits = DescriptorRequirementsBitsFromFormat(buffer_view_state->create_info.format);
Chris Forbese92dd1d2019-01-21 15:58:57 -08001254
Jeff Bolz165818a2020-05-08 11:19:03 -05001255 if (!(reqs & format_bits)) {
1256 // bad component type
1257 auto set = descriptor_set->GetSet();
locke-lunarg1328e8e2020-08-20 12:40:08 -06001258 return LogError(set, vuids.descriptor_valid,
Jeff Bolz165818a2020-05-08 11:19:03 -05001259 "%s encountered the following validation error at %s time: Descriptor in "
1260 "binding #%" PRIu32 " index %" PRIu32
1261 " requires %s component type, but bound descriptor format is %s.",
1262 report_data->FormatHandle(set).c_str(), caller, binding, index,
1263 StringDescriptorReqComponentType(reqs),
1264 string_VkFormat(buffer_view_state->create_info.format));
1265 }
locke-lunarg25b6c352020-08-06 17:44:18 -06001266
1267 // Verify VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT
1268 if ((reqs & DESCRIPTOR_REQ_VIEW_ATOMIC_OPERATION) &&
locke-lunarge4bf18f2020-08-21 10:23:08 -06001269 (descriptor_set->GetTypeFromBinding(binding) == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) &&
locke-lunarg25b6c352020-08-06 17:44:18 -06001270 !(buffer_view_state->format_features & VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT)) {
1271 auto set = descriptor_set->GetSet();
1272 LogObjectList objlist(set);
1273 objlist.add(buffer_view);
1274 return LogError(
1275 objlist, "UNASSIGNED-None-MismatchAtomicBufferFeature",
1276 "%s encountered the following validation error at %s time: Descriptor in binding #%" PRIu32
1277 " index %" PRIu32
1278 ", %s, format %s, doesn't "
1279 "contain VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT.",
1280 report_data->FormatHandle(set).c_str(), caller, binding, index,
1281 report_data->FormatHandle(buffer_view).c_str(),
1282 string_VkFormat(buffer_view_state->create_info.format));
1283 }
locke-lunarg351c9d82020-10-23 14:43:21 -06001284 if (enabled_features.core11.protectedMemory == VK_TRUE) {
1285 if (ValidateProtectedBuffer(cb_node, buffer_view_state->buffer_state.get(), caller,
1286 vuids.unprotected_command_buffer, "Buffer is in a descriptorSet")) {
1287 return true;
1288 }
1289 if (binding_info.second.is_writable &&
1290 ValidateUnprotectedBuffer(cb_node, buffer_view_state->buffer_state.get(), caller,
1291 vuids.protected_command_buffer, "Buffer is in a descriptorSet")) {
1292 return true;
1293 }
1294 }
Chris Forbese92dd1d2019-01-21 15:58:57 -08001295 }
Jeff Bolz95176d02020-04-01 00:36:16 -05001296 } else if (descriptor_class == DescriptorClass::AccelerationStructure) {
1297 // Verify that acceleration structures are valid
sourav parmarcd51aa82020-11-17 12:04:52 -08001298 bool is_khr = (*((cvdescriptorset::AccelerationStructureDescriptor *)(descriptor))).is_khr();
1299 if (is_khr) {
1300 auto acc = static_cast<const AccelerationStructureDescriptor *>(descriptor)->GetAccelerationStructure();
1301 auto acc_node =
1302 static_cast<const AccelerationStructureDescriptor *>(descriptor)->GetAccelerationStructureStateKHR();
1303 if (!acc_node || acc_node->destroyed) {
1304 if (acc != VK_NULL_HANDLE || !enabled_features.robustness2_features.nullDescriptor) {
1305 auto set = descriptor_set->GetSet();
1306 return LogError(
1307 set, vuids.descriptor_valid,
1308 "%s encountered the following validation error at %s time: Descriptor in binding #%" PRIu32
1309 " index %" PRIu32 " is using acceleration structure %s that is invalid or has been destroyed.",
1310 report_data->FormatHandle(set).c_str(), caller, binding, index,
1311 report_data->FormatHandle(acc).c_str());
1312 }
1313 } else {
John Zulauf79f06582021-02-27 18:38:39 -07001314 for (const auto *mem_binding : acc_node->GetBoundMemory()) {
sourav parmarcd51aa82020-11-17 12:04:52 -08001315 if (mem_binding->destroyed) {
1316 auto set = descriptor_set->GetSet();
1317 return LogError(set, vuids.descriptor_valid,
1318 "%s encountered the following validation error at %s time: Descriptor in "
1319 "binding #%" PRIu32 " index %" PRIu32
1320 " is using acceleration structure %s that references invalid memory %s.",
1321 report_data->FormatHandle(set).c_str(), caller, binding, index,
1322 report_data->FormatHandle(acc).c_str(),
1323 report_data->FormatHandle(mem_binding->mem).c_str());
1324 }
1325 }
Ricardo Garcia3f145ae2020-10-28 16:53:35 +01001326 }
Jeff Bolz95176d02020-04-01 00:36:16 -05001327 } else {
sourav parmarcd51aa82020-11-17 12:04:52 -08001328 auto acc = static_cast<const AccelerationStructureDescriptor *>(descriptor)->GetAccelerationStructureNV();
1329 auto acc_node =
1330 static_cast<const AccelerationStructureDescriptor *>(descriptor)->GetAccelerationStructureStateNV();
1331 if (!acc_node || acc_node->destroyed) {
1332 if (acc != VK_NULL_HANDLE || !enabled_features.robustness2_features.nullDescriptor) {
Tony-LunarGace473a2020-05-06 12:48:04 -06001333 auto set = descriptor_set->GetSet();
sourav parmarcd51aa82020-11-17 12:04:52 -08001334 return LogError(
1335 set, vuids.descriptor_valid,
1336 "%s encountered the following validation error at %s time: Descriptor in binding #%" PRIu32
1337 " index %" PRIu32 " is using acceleration structure %s that is invalid or has been destroyed.",
1338 report_data->FormatHandle(set).c_str(), caller, binding, index,
1339 report_data->FormatHandle(acc).c_str());
1340 }
1341 } else {
John Zulauf79f06582021-02-27 18:38:39 -07001342 for (const auto *mem_binding : acc_node->GetBoundMemory()) {
sourav parmarcd51aa82020-11-17 12:04:52 -08001343 if (mem_binding->destroyed) {
1344 auto set = descriptor_set->GetSet();
1345 return LogError(set, vuids.descriptor_valid,
1346 "%s encountered the following validation error at %s time: Descriptor in "
1347 "binding #%" PRIu32 " index %" PRIu32
1348 " is using acceleration structure %s that references invalid memory %s.",
1349 report_data->FormatHandle(set).c_str(), caller, binding, index,
1350 report_data->FormatHandle(acc).c_str(),
1351 report_data->FormatHandle(mem_binding->mem).c_str());
1352 }
Jeff Bolz95176d02020-04-01 00:36:16 -05001353 }
1354 }
1355 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001356 }
locke-lunarg4e1e4632020-10-26 01:52:19 -06001357
1358 // If the validation is related to both of image and sampler,
locke-lunarg9939d4b2020-10-26 20:11:08 -06001359 // please leave it in (descriptor_class == DescriptorClass::ImageSampler || descriptor_class ==
1360 // DescriptorClass::Image) Here is to validate for only sampler.
John Zulaufc93c4252019-06-25 09:19:49 -06001361 if (descriptor_class == DescriptorClass::ImageSampler || descriptor_class == DescriptorClass::PlainSampler) {
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -06001362 // Verify Sampler still valid
1363 VkSampler sampler;
Jeff Bolzfaffeb32019-10-04 12:47:16 -05001364 const SAMPLER_STATE *sampler_state;
John Zulaufc93c4252019-06-25 09:19:49 -06001365 if (descriptor_class == DescriptorClass::ImageSampler) {
John Zulauf382e1912019-06-10 15:27:44 -06001366 sampler = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetSampler();
Jeff Bolzfaffeb32019-10-04 12:47:16 -05001367 sampler_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetSamplerState();
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -06001368 } else {
John Zulauf382e1912019-06-10 15:27:44 -06001369 sampler = static_cast<const SamplerDescriptor *>(descriptor)->GetSampler();
Jeff Bolzfaffeb32019-10-04 12:47:16 -05001370 sampler_state = static_cast<const SamplerDescriptor *>(descriptor)->GetSamplerState();
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -06001371 }
Jeff Bolzfaffeb32019-10-04 12:47:16 -05001372 if (!sampler_state || sampler_state->destroyed) {
Tony-LunarGace473a2020-05-06 12:48:04 -06001373 auto set = descriptor_set->GetSet();
locke-lunarg1328e8e2020-08-20 12:40:08 -06001374 return LogError(set, vuids.descriptor_valid,
Tony-LunarGace473a2020-05-06 12:48:04 -06001375 "%s encountered the following validation error at %s time: Descriptor in "
1376 "binding #%" PRIu32 " index %" PRIu32
1377 " is using sampler %s that is invalid or has been destroyed.",
1378 report_data->FormatHandle(set).c_str(), caller, binding, index,
1379 report_data->FormatHandle(sampler).c_str());
Lockea223c102019-04-05 00:38:24 -06001380 } else {
John Zulauf382e1912019-06-10 15:27:44 -06001381 if (sampler_state->samplerConversion && !descriptor->IsImmutableSampler()) {
Tony-LunarGace473a2020-05-06 12:48:04 -06001382 auto set = descriptor_set->GetSet();
locke-lunarg1328e8e2020-08-20 12:40:08 -06001383 return LogError(set, vuids.descriptor_valid,
Tony-LunarGace473a2020-05-06 12:48:04 -06001384 "%s encountered the following validation error at %s time: sampler (%s) "
1385 "in the descriptor set (%s) caontains a YCBCR conversion (%s), then the sampler MUST "
1386 "also exist as an immutable sampler.",
1387 report_data->FormatHandle(set).c_str(), caller,
1388 report_data->FormatHandle(sampler).c_str(),
1389 report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1390 report_data->FormatHandle(sampler_state->samplerConversion).c_str());
Lockea223c102019-04-05 00:38:24 -06001391 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -06001392 }
1393 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001394 }
1395 }
1396 }
Tony-LunarGace473a2020-05-06 12:48:04 -06001397 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001398}
Chris Forbes57989132016-07-26 17:06:10 +12001399
Tobin Ehlis9906d9d2016-05-17 14:23:46 -06001400// Set is being deleted or updates so invalidate all bound cmd buffers
Jeff Bolz41a1ced2019-10-11 11:40:49 -05001401void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers(ValidationStateTracker *state_data) {
1402 state_data->InvalidateCommandBuffers(cb_bindings, VulkanTypedHandle(set_, kVulkanObjectTypeDescriptorSet), /*unlink*/ false);
Tobin Ehlis9906d9d2016-05-17 14:23:46 -06001403}
John Zulauf1d27e0a2018-11-05 10:12:48 -07001404
1405// Loop through the write updates to do for a push descriptor set, ignoring dstSet
Jeff Bolz41a1ced2019-10-11 11:40:49 -05001406void cvdescriptorset::DescriptorSet::PerformPushDescriptorsUpdate(ValidationStateTracker *dev_data, uint32_t write_count,
1407 const VkWriteDescriptorSet *p_wds) {
John Zulauf1d27e0a2018-11-05 10:12:48 -07001408 assert(IsPushDescriptor());
1409 for (uint32_t i = 0; i < write_count; i++) {
Jeff Bolz41a1ced2019-10-11 11:40:49 -05001410 PerformWriteUpdate(dev_data, &p_wds[i]);
John Zulauf1d27e0a2018-11-05 10:12:48 -07001411 }
Jason Macnak83cfd582019-07-31 10:14:24 -07001412
1413 push_descriptor_set_writes.clear();
1414 push_descriptor_set_writes.reserve(static_cast<std::size_t>(write_count));
1415 for (uint32_t i = 0; i < write_count; i++) {
1416 push_descriptor_set_writes.push_back(safe_VkWriteDescriptorSet(&p_wds[i]));
1417 }
John Zulauf1d27e0a2018-11-05 10:12:48 -07001418}
1419
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001420// Perform write update in given update struct
Jeff Bolz41a1ced2019-10-11 11:40:49 -05001421void cvdescriptorset::DescriptorSet::PerformWriteUpdate(ValidationStateTracker *dev_data, const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001422 // Perform update on a per-binding basis as consecutive updates roll over to next binding
1423 auto descriptors_remaining = update->descriptorCount;
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001424 auto offset = update->dstArrayElement;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001425 auto orig_binding = DescriptorSetLayout::ConstBindingIterator(layout_.get(), update->dstBinding);
locke-lunarge46b7782019-09-10 01:44:20 -06001426 auto current_binding = orig_binding;
1427
Tobin Ehlise16805c2017-08-09 09:10:37 -06001428 uint32_t update_index = 0;
locke-lunarge46b7782019-09-10 01:44:20 -06001429 // Verify next consecutive binding matches type, stage flags & immutable sampler use and if AtEnd
1430 while (descriptors_remaining && orig_binding.IsConsistent(current_binding)) {
1431 const auto &index_range = current_binding.GetGlobalIndexRange();
1432 auto global_idx = index_range.start + offset;
1433 // global_idx is which descriptor is needed to update. If global_idx > index_range.end, it means the descriptor isn't in
1434 // this binding, maybe in next binding.
1435 if (global_idx >= index_range.end) {
1436 offset -= current_binding.GetDescriptorCount();
1437 ++current_binding;
1438 continue;
1439 }
1440
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001441 // Loop over the updates for a single binding at a time
locke-lunarge46b7782019-09-10 01:44:20 -06001442 uint32_t update_count = std::min(descriptors_remaining, current_binding.GetDescriptorCount() - offset);
Tobin Ehlise16805c2017-08-09 09:10:37 -06001443 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
John Zulaufd2c3dae2019-12-12 11:02:17 -07001444 descriptors_[global_idx + di]->WriteUpdate(state_data_, update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001445 }
1446 // Roll over to next binding in case of consecutive update
1447 descriptors_remaining -= update_count;
locke-lunarge46b7782019-09-10 01:44:20 -06001448 if (descriptors_remaining) {
1449 // Starting offset is beyond the current binding. Check consistency, update counters and advance to the next binding,
1450 // looking for the start point. All bindings (even those skipped) must be consistent with the update and with the
1451 // original binding.
1452 offset = 0;
1453 ++current_binding;
1454 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001455 }
Jeff Bolzdd4cfa12019-08-11 20:57:51 -05001456 if (update->descriptorCount) {
1457 some_update_ = true;
1458 change_count_++;
1459 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001460
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001461 if (!(layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
Mike Schuchardt2df08912020-12-15 16:28:09 -08001462 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT))) {
Jeff Bolz41a1ced2019-10-11 11:40:49 -05001463 InvalidateBoundCmdBuffers(dev_data);
Jeff Bolzfdf96072018-04-10 14:32:18 -05001464 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001465}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001466// Validate Copy update
John Zulaufc93c4252019-06-25 09:19:49 -06001467bool CoreChecks::ValidateCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *dst_set, const DescriptorSet *src_set,
Jeff Bolz46c0ea02019-10-09 13:06:29 -05001468 const char *func_name, std::string *error_code, std::string *error_msg) const {
Jeff Bolz6aad1742019-10-16 11:10:09 -05001469 auto dst_layout = dst_set->GetLayout().get();
1470 auto src_layout = src_set->GetLayout().get();
John Zulaufd9435c32019-06-05 15:55:36 -06001471
John Zulauf5dfd45c2018-01-17 11:06:34 -07001472 // Verify dst layout still valid
Jeff Bolz6ae39612019-10-11 20:57:36 -05001473 if (dst_layout->destroyed) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001474 *error_code = "VUID-VkCopyDescriptorSet-dstSet-parameter";
Mark Lobodzinski23e395e2020-04-09 10:17:31 -06001475 std::ostringstream str;
1476 str << "Cannot call " << func_name << " to perform copy update on dstSet " << report_data->FormatHandle(dst_set->GetSet())
1477 << " created with destroyed " << report_data->FormatHandle(dst_layout->GetDescriptorSetLayout()) << ".";
1478 *error_msg = str.str();
John Zulauf5dfd45c2018-01-17 11:06:34 -07001479 return false;
1480 }
1481
1482 // Verify src layout still valid
Jeff Bolz6ae39612019-10-11 20:57:36 -05001483 if (src_layout->destroyed) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001484 *error_code = "VUID-VkCopyDescriptorSet-srcSet-parameter";
Mark Lobodzinski23e395e2020-04-09 10:17:31 -06001485 std::ostringstream str;
1486 str << "Cannot call " << func_name << " to perform copy update on dstSet " << report_data->FormatHandle(dst_set->GetSet())
1487 << " from srcSet " << report_data->FormatHandle(src_set->GetSet()) << " created with destroyed "
1488 << report_data->FormatHandle(src_layout->GetDescriptorSetLayout()) << ".";
1489 *error_msg = str.str();
John Zulauf5dfd45c2018-01-17 11:06:34 -07001490 return false;
1491 }
1492
John Zulaufd9435c32019-06-05 15:55:36 -06001493 if (!dst_layout->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001494 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-00347";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001495 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001496 error_str << "DescriptorSet " << report_data->FormatHandle(dst_set->GetSet())
1497 << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001498 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001499 return false;
1500 }
1501 if (!src_set->HasBinding(update->srcBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001502 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-00345";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001503 std::stringstream error_str;
sourav parmarf4a78252020-04-10 13:04:21 -07001504 error_str << "DescriptorSet " << report_data->FormatHandle(src_set->GetSet())
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001505 << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001506 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001507 return false;
1508 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001509 // Verify idle ds
John Zulaufd9435c32019-06-05 15:55:36 -06001510 if (dst_set->in_use.load() &&
1511 !(dst_layout->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
Mike Schuchardt2df08912020-12-15 16:28:09 -08001512 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT))) {
Jeff Bolzfdf96072018-04-10 14:32:18 -05001513 // TODO : Re-using Free Idle error code, need copy update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001514 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001515 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001516 error_str << "Cannot call " << func_name << " to perform copy update on descriptor set "
1517 << report_data->FormatHandle(dst_set->GetSet()) << " that is in use by a command buffer";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001518 *error_msg = error_str.str();
1519 return false;
1520 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001521 // src & dst set bindings are valid
1522 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -07001523 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001524 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
1525 // SRC update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001526 *error_code = "VUID-VkCopyDescriptorSet-srcArrayElement-00346";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001527 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001528 error_str << "Attempting copy update from descriptorSet " << report_data->FormatHandle(update->srcSet) << " binding#"
1529 << update->srcBinding << " with offset index of "
1530 << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start << " plus update array offset of "
1531 << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001532 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001533 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001534 return false;
1535 }
John Zulaufd9435c32019-06-05 15:55:36 -06001536 auto dst_start_idx = dst_layout->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
1537 if ((dst_start_idx + update->descriptorCount) > dst_layout->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001538 // DST update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001539 *error_code = "VUID-VkCopyDescriptorSet-dstArrayElement-00348";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001540 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001541 error_str << "Attempting copy update to descriptorSet " << report_data->FormatHandle(dst_set->GetSet()) << " binding#"
1542 << update->dstBinding << " with offset index of "
1543 << dst_layout->GetGlobalIndexRangeFromBinding(update->dstBinding).start << " plus update array offset of "
1544 << update->dstArrayElement << " and update of " << update->descriptorCount
John Zulaufd9435c32019-06-05 15:55:36 -06001545 << " descriptors oversteps total number of descriptors in set: " << dst_layout->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001546 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001547 return false;
1548 }
1549 // Check that types match
Shannon McPhersonafe55122020-05-25 16:20:19 -06001550 // TODO : Base default error case going from here is "VUID-VkAcquireNextImageInfoKHR-semaphore-parameter" 2ba which covers all
Dave Houltond8ed0212018-05-16 17:18:24 -06001551 // consistency issues, need more fine-grained error codes
Dave Houlton00c154e2018-05-24 13:20:50 -06001552 *error_code = "VUID-VkCopyDescriptorSet-srcSet-00349";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001553 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
John Zulaufd9435c32019-06-05 15:55:36 -06001554 auto dst_type = dst_layout->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001555 if (src_type != dst_type) {
sourav parmarf4a78252020-04-10 13:04:21 -07001556 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02632";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001557 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001558 error_str << "Attempting copy update to descriptorSet " << report_data->FormatHandle(dst_set->GetSet()) << " binding #"
1559 << update->dstBinding << " with type " << string_VkDescriptorType(dst_type) << " from descriptorSet "
1560 << report_data->FormatHandle(src_set->GetSet()) << " binding #" << update->srcBinding << " with type "
1561 << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001562 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001563 return false;
1564 }
1565 // Verify consistency of src & dst bindings if update crosses binding boundaries
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001566 if ((!VerifyUpdateConsistency(report_data, DescriptorSetLayout::ConstBindingIterator(src_layout, update->srcBinding),
John Zulauf4a015c92019-06-04 09:50:05 -06001567 update->srcArrayElement, update->descriptorCount, "copy update from", src_set->GetSet(),
1568 error_msg)) ||
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001569 (!VerifyUpdateConsistency(report_data, DescriptorSetLayout::ConstBindingIterator(dst_layout, update->dstBinding),
John Zulaufd9435c32019-06-05 15:55:36 -06001570 update->dstArrayElement, update->descriptorCount, "copy update to", dst_set->GetSet(),
1571 error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001572 return false;
1573 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001574
Mike Schuchardt2df08912020-12-15 16:28:09 -08001575 if ((src_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) &&
1576 !(dst_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001577 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01918";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001578 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001579 error_str << "If pname:srcSet's (" << report_data->FormatHandle(update->srcSet)
Jeff Bolzfdf96072018-04-10 14:32:18 -05001580 << ") layout was created with the "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001581 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT flag "
Jeff Bolzfdf96072018-04-10 14:32:18 -05001582 "set, then pname:dstSet's ("
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001583 << report_data->FormatHandle(update->dstSet)
Jeff Bolzfdf96072018-04-10 14:32:18 -05001584 << ") layout must: also have been created with the "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001585 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT flag set";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001586 *error_msg = error_str.str();
1587 return false;
1588 }
1589
Mike Schuchardt2df08912020-12-15 16:28:09 -08001590 if (!(src_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) &&
1591 (dst_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001592 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01919";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001593 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001594 error_str << "If pname:srcSet's (" << report_data->FormatHandle(update->srcSet)
Jeff Bolzfdf96072018-04-10 14:32:18 -05001595 << ") layout was created without the "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001596 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT flag "
Jeff Bolzfdf96072018-04-10 14:32:18 -05001597 "set, then pname:dstSet's ("
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001598 << report_data->FormatHandle(update->dstSet)
Jeff Bolzfdf96072018-04-10 14:32:18 -05001599 << ") layout must: also have been created without the "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001600 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT flag set";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001601 *error_msg = error_str.str();
1602 return false;
1603 }
1604
Mike Schuchardt2df08912020-12-15 16:28:09 -08001605 if ((src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT) &&
1606 !(dst_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001607 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01920";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001608 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001609 error_str << "If the descriptor pool from which pname:srcSet (" << report_data->FormatHandle(update->srcSet)
Jeff Bolzfdf96072018-04-10 14:32:18 -05001610 << ") was allocated was created "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001611 "with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT flag "
Jeff Bolzfdf96072018-04-10 14:32:18 -05001612 "set, then the descriptor pool from which pname:dstSet ("
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001613 << report_data->FormatHandle(update->dstSet)
Jeff Bolzfdf96072018-04-10 14:32:18 -05001614 << ") was allocated must: "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001615 "also have been created with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT flag set";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001616 *error_msg = error_str.str();
1617 return false;
1618 }
1619
Mike Schuchardt2df08912020-12-15 16:28:09 -08001620 if (!(src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT) &&
1621 (dst_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001622 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01921";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001623 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001624 error_str << "If the descriptor pool from which pname:srcSet (" << report_data->FormatHandle(update->srcSet)
Jeff Bolzfdf96072018-04-10 14:32:18 -05001625 << ") was allocated was created "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001626 "without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT flag "
Jeff Bolzfdf96072018-04-10 14:32:18 -05001627 "set, then the descriptor pool from which pname:dstSet ("
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06001628 << report_data->FormatHandle(update->dstSet)
Jeff Bolzfdf96072018-04-10 14:32:18 -05001629 << ") was allocated must: "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001630 "also have been created without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT flag set";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001631 *error_msg = error_str.str();
1632 return false;
1633 }
1634
Jeff Bolze54ae892018-09-08 12:16:29 -05001635 if (src_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1636 if ((update->srcArrayElement % 4) != 0) {
1637 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02223";
1638 std::stringstream error_str;
1639 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1640 << "srcArrayElement " << update->srcArrayElement << " not a multiple of 4";
1641 *error_msg = error_str.str();
1642 return false;
1643 }
1644 if ((update->dstArrayElement % 4) != 0) {
1645 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02224";
1646 std::stringstream error_str;
1647 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1648 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1649 *error_msg = error_str.str();
1650 return false;
1651 }
1652 if ((update->descriptorCount % 4) != 0) {
1653 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02225";
1654 std::stringstream error_str;
1655 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1656 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1657 *error_msg = error_str.str();
1658 return false;
1659 }
1660 }
1661
Tobin Ehlisd41e7b62016-05-19 07:56:18 -06001662 // Update parameters all look good and descriptor updated so verify update contents
Mark Lobodzinskif4ed6c12020-01-03 11:21:58 -07001663 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, dst_set, dst_type, dst_start_idx, func_name, error_code,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001664 error_msg)) {
Mark Lobodzinskif4ed6c12020-01-03 11:21:58 -07001665 return false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001666 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001667
1668 // All checks passed so update is good
1669 return true;
1670}
1671// Perform Copy update
Jeff Bolz41a1ced2019-10-11 11:40:49 -05001672void cvdescriptorset::DescriptorSet::PerformCopyUpdate(ValidationStateTracker *dev_data, const VkCopyDescriptorSet *update,
1673 const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -07001674 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001675 auto dst_start_idx = layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001676 // Update parameters all look good so perform update
1677 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001678 auto src = src_set->descriptors_[src_start_idx + di].get();
1679 auto dst = descriptors_[dst_start_idx + di].get();
1680 if (src->updated) {
John Zulaufd2c3dae2019-12-12 11:02:17 -07001681 dst->CopyUpdate(state_data_, src);
Józef Kucia5297e372017-10-13 22:31:34 +02001682 some_update_ = true;
Jeff Bolzdd4cfa12019-08-11 20:57:51 -05001683 change_count_++;
Józef Kucia5297e372017-10-13 22:31:34 +02001684 } else {
1685 dst->updated = false;
1686 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001687 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001688
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001689 if (!(layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
Mike Schuchardt2df08912020-12-15 16:28:09 -08001690 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT))) {
Jeff Bolz41a1ced2019-10-11 11:40:49 -05001691 InvalidateBoundCmdBuffers(dev_data);
Jeff Bolzfdf96072018-04-10 14:32:18 -05001692 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001693}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001694
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001695// Update the drawing state for the affected descriptors.
1696// Set cb_node to this set and this set to cb_node.
1697// Add the bindings of the descriptor
1698// Set the layout based on the current descriptor layout (will mask subsequent layer mismatch errors)
1699// TODO: Modify the UpdateDrawState virtural functions to *only* set initial layout and not change layouts
Tobin Ehlisf9519102016-08-17 09:49:13 -06001700// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
1701// to be used in a draw by the given cb_node
Jeremy Kniagere6827432020-04-01 09:05:56 -06001702void cvdescriptorset::DescriptorSet::UpdateDrawState(ValidationStateTracker *device_data, CMD_BUFFER_STATE *cb_node,
1703 CMD_TYPE cmd_type, const PIPELINE_STATE *pipe,
Karl Schultz7090a052020-11-10 08:54:21 -07001704 const BindingReqMap &binding_req_map, const char *function) {
Tony-LunarG77822802020-05-28 16:35:46 -06001705 if (!device_data->disabled[command_buffer_state] && !IsPushDescriptor()) {
Jeff Bolzafa429a2019-08-14 09:59:22 -05001706 // bind cb to this descriptor set
1707 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Jeff Bolzadbfa852019-10-04 13:53:30 -05001708 if (device_data->AddCommandBufferBinding(cb_bindings, VulkanTypedHandle(set_, kVulkanObjectTypeDescriptorSet, this),
1709 cb_node)) {
1710 device_data->AddCommandBufferBinding(pool_state_->cb_bindings,
1711 VulkanTypedHandle(pool_state_->pool, kVulkanObjectTypeDescriptorPool, pool_state_),
1712 cb_node);
Jeff Bolzafa429a2019-08-14 09:59:22 -05001713 }
1714 }
Jeff Bolze18e7242019-08-12 20:55:22 -05001715
1716 // Descriptor UpdateDrawState functions do two things - associate resources to the command buffer,
1717 // and call image layout validation callbacks. If both are disabled, skip the entire loop.
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06001718 if (device_data->disabled[command_buffer_state] && device_data->disabled[image_layout_validation]) {
Jeff Bolze18e7242019-08-12 20:55:22 -05001719 return;
1720 }
1721
Tobin Ehlisf9519102016-08-17 09:49:13 -06001722 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
1723 // resources
locke-lunarg540b2252020-08-03 13:23:36 -06001724 CMD_BUFFER_STATE::CmdDrawDispatchInfo cmd_info = {};
John Zulauf79f06582021-02-27 18:38:39 -07001725 for (const auto &binding_req_pair : binding_req_map) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001726 auto index = layout_->GetIndexFromBinding(binding_req_pair.first);
locke-gb3ce08f2019-09-30 12:30:56 -06001727
Tony-LunarG62c5dba2018-12-20 14:27:23 -07001728 // We aren't validating descriptors created with PARTIALLY_BOUND or UPDATE_AFTER_BIND, so don't record state
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001729 auto flags = layout_->GetDescriptorBindingFlagsFromIndex(index);
Mike Schuchardt2df08912020-12-15 16:28:09 -08001730 if (flags & (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT)) {
1731 if (!(flags & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT)) {
locke-lunarg36045992020-08-20 16:54:37 -06001732 cmd_info.binding_infos.emplace_back(binding_req_pair);
locke-gb3ce08f2019-09-30 12:30:56 -06001733 }
Tony-LunarG62c5dba2018-12-20 14:27:23 -07001734 continue;
1735 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001736 auto range = layout_->GetGlobalIndexRangeFromIndex(index);
John Zulaufc483f442017-12-15 14:02:06 -07001737 for (uint32_t i = range.start; i < range.end; ++i) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001738 descriptors_[i]->UpdateDrawState(device_data, cb_node);
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001739 }
1740 }
locke-lunarg540b2252020-08-03 13:23:36 -06001741
1742 if (cmd_info.binding_infos.size() > 0) {
1743 cmd_info.cmd_type = cmd_type;
1744 cmd_info.function = function;
1745 if (cb_node->activeFramebuffer) {
1746 cmd_info.framebuffer = cb_node->activeFramebuffer->framebuffer;
locke-lunargfc78e932020-11-19 17:06:24 -07001747 cmd_info.attachments = cb_node->active_attachments;
1748 cmd_info.subpasses = cb_node->active_subpasses;
locke-lunarg540b2252020-08-03 13:23:36 -06001749 }
1750 cb_node->validate_descriptorsets_in_queuesubmit[set_].emplace_back(cmd_info);
1751 }
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001752}
1753
John Zulauffbf3c202019-07-17 14:57:14 -06001754void cvdescriptorset::DescriptorSet::FilterOneBindingReq(const BindingReqMap::value_type &binding_req_pair, BindingReqMap *out_req,
1755 const TrackedBindings &bindings, uint32_t limit) {
1756 if (bindings.size() < limit) {
1757 const auto it = bindings.find(binding_req_pair.first);
1758 if (it == bindings.cend()) out_req->emplace(binding_req_pair);
John Zulauf48a6a702017-12-22 17:14:54 -07001759 }
1760}
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001761
John Zulauffbf3c202019-07-17 14:57:14 -06001762void cvdescriptorset::DescriptorSet::FilterBindingReqs(const CMD_BUFFER_STATE &cb_state, const PIPELINE_STATE &pipeline,
1763 const BindingReqMap &in_req, BindingReqMap *out_req) const {
1764 // For const cleanliness we have to find in the maps...
1765 const auto validated_it = cached_validation_.find(&cb_state);
1766 if (validated_it == cached_validation_.cend()) {
1767 // We have nothing validated, copy in to out
1768 for (const auto &binding_req_pair : in_req) {
1769 out_req->emplace(binding_req_pair);
John Zulauf48a6a702017-12-22 17:14:54 -07001770 }
John Zulauffbf3c202019-07-17 14:57:14 -06001771 return;
John Zulauf48a6a702017-12-22 17:14:54 -07001772 }
John Zulauffbf3c202019-07-17 14:57:14 -06001773 const auto &validated = validated_it->second;
John Zulauf48a6a702017-12-22 17:14:54 -07001774
John Zulauffbf3c202019-07-17 14:57:14 -06001775 const auto image_sample_version_it = validated.image_samplers.find(&pipeline);
1776 const VersionedBindings *image_sample_version = nullptr;
1777 if (image_sample_version_it != validated.image_samplers.cend()) {
1778 image_sample_version = &(image_sample_version_it->second);
1779 }
1780 const auto &dynamic_buffers = validated.dynamic_buffers;
1781 const auto &non_dynamic_buffers = validated.non_dynamic_buffers;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001782 const auto &stats = layout_->GetBindingTypeStats();
John Zulauf48a6a702017-12-22 17:14:54 -07001783 for (const auto &binding_req_pair : in_req) {
1784 auto binding = binding_req_pair.first;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001785 VkDescriptorSetLayoutBinding const *layout_binding = layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
John Zulauf48a6a702017-12-22 17:14:54 -07001786 if (!layout_binding) {
1787 continue;
1788 }
1789 // Caching criteria differs per type.
1790 // If image_layout have changed , the image descriptors need to be validated against them.
sfricke-samsung11b31212021-02-10 00:41:50 -08001791 if (IsDyanmicDescriptor(layout_binding->descriptorType)) {
John Zulauffbf3c202019-07-17 14:57:14 -06001792 FilterOneBindingReq(binding_req_pair, out_req, dynamic_buffers, stats.dynamic_buffer_count);
John Zulauf48a6a702017-12-22 17:14:54 -07001793 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1794 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
John Zulauffbf3c202019-07-17 14:57:14 -06001795 FilterOneBindingReq(binding_req_pair, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
John Zulauf48a6a702017-12-22 17:14:54 -07001796 } else {
1797 // This is rather crude, as the changed layouts may not impact the bound descriptors,
1798 // but the simple "versioning" is a simple "dirt" test.
John Zulauffbf3c202019-07-17 14:57:14 -06001799 bool stale = true;
1800 if (image_sample_version) {
1801 const auto version_it = image_sample_version->find(binding);
1802 if (version_it != image_sample_version->cend() && (version_it->second == cb_state.image_layout_change_count)) {
1803 stale = false;
1804 }
1805 }
1806 if (stale) {
John Zulauf48a6a702017-12-22 17:14:54 -07001807 out_req->emplace(binding_req_pair);
1808 }
1809 }
1810 }
1811}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001812
John Zulauffbf3c202019-07-17 14:57:14 -06001813void cvdescriptorset::DescriptorSet::UpdateValidationCache(const CMD_BUFFER_STATE &cb_state, const PIPELINE_STATE &pipeline,
1814 const BindingReqMap &updated_bindings) {
1815 // For const cleanliness we have to find in the maps...
1816 auto &validated = cached_validation_[&cb_state];
1817
1818 auto &image_sample_version = validated.image_samplers[&pipeline];
1819 auto &dynamic_buffers = validated.dynamic_buffers;
1820 auto &non_dynamic_buffers = validated.non_dynamic_buffers;
1821 for (const auto &binding_req_pair : updated_bindings) {
1822 auto binding = binding_req_pair.first;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001823 VkDescriptorSetLayoutBinding const *layout_binding = layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
John Zulauffbf3c202019-07-17 14:57:14 -06001824 if (!layout_binding) {
1825 continue;
1826 }
1827 // Caching criteria differs per type.
sfricke-samsung11b31212021-02-10 00:41:50 -08001828 if (IsDyanmicDescriptor(layout_binding->descriptorType)) {
John Zulauffbf3c202019-07-17 14:57:14 -06001829 dynamic_buffers.emplace(binding);
1830 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1831 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
1832 non_dynamic_buffers.emplace(binding);
1833 } else {
1834 // Save the layout change version...
1835 image_sample_version[binding] = cb_state.image_layout_change_count;
1836 }
1837 }
1838}
1839
John Zulaufd2c3dae2019-12-12 11:02:17 -07001840cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const ValidationStateTracker *dev_data, const VkSampler *immut)
Karl Schultz76d16a42020-11-11 05:05:33 -07001841 : immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001842 updated = false;
1843 descriptor_class = PlainSampler;
1844 if (immut) {
Karl Schultz76d16a42020-11-11 05:05:33 -07001845 sampler_state_ = dev_data->GetConstCastShared<SAMPLER_STATE>(*immut);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001846 immutable_ = true;
1847 updated = true;
1848 }
1849}
Tobin Ehlise2f80292016-06-02 10:08:53 -06001850// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
John Zulaufc93c4252019-06-25 09:19:49 -06001851bool CoreChecks::ValidateSampler(const VkSampler sampler) const { return (GetSamplerState(sampler) != nullptr); }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001852
John Zulaufc93c4252019-06-25 09:19:49 -06001853bool CoreChecks::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
John Zulaufbd9b3412019-08-22 17:16:11 -06001854 const char *func_name, std::string *error_code, std::string *error_msg) const {
John Zulaufc93c4252019-06-25 09:19:49 -06001855 auto iv_state = GetImageViewState(image_view);
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001856 assert(iv_state);
1857
Tobin Ehlis81280962016-07-20 14:04:20 -06001858 // Note that when an imageview is created, we validated that memory is bound so no need to re-check here
Tobin Ehlis1809f912016-05-25 09:24:36 -06001859 // Validate that imageLayout is compatible with aspect_mask and image format
1860 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001861 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
1862 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001863 VkFormat format = VK_FORMAT_MAX_ENUM;
1864 VkImageUsageFlags usage = 0;
John Zulaufc93c4252019-06-25 09:19:49 -06001865 auto image_node = GetImageState(image);
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001866 assert(image_node);
Chris Forbes67757ff2017-07-21 13:59:01 -07001867
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001868 format = image_node->createInfo.format;
1869 usage = image_node->createInfo.usage;
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001870 const auto stencil_usage_info = LvlFindInChain<VkImageStencilUsageCreateInfo>(image_node->createInfo.pNext);
Ricardo Garcia3f5984c2020-04-09 10:56:34 +02001871 if (stencil_usage_info) {
1872 usage |= stencil_usage_info->stencilUsage;
1873 }
Mark Lobodzinski03d00062020-06-15 14:35:45 -06001874
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001875 // Validate that memory is bound to image
Mark Lobodzinski03d00062020-06-15 14:35:45 -06001876 if (ValidateMemoryIsBoundToImage(image_node, func_name, "UNASSIGNED-CoreValidation-BoundResourceFreedMemoryAccess")) {
1877 *error_code = "UNASSIGNED-CoreValidation-BoundResourceFreedMemoryAccess";
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001878 *error_msg = "No memory bound to image.";
Tobin Ehlis1809f912016-05-25 09:24:36 -06001879 return false;
1880 }
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001881
1882 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
1883 // but not binding them to descriptor sets.
1884 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D && (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
1885 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
1886 *error_code = "VUID-VkDescriptorImageInfo-imageView-00343";
1887 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
1888 return false;
1889 }
1890
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001891 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
1892 // vkCreateImageView(). What's the best way to create unique id for these cases?
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001893 *error_code = "UNASSIGNED-CoreValidation-DrawState-InvalidImageView";
Dave Houlton1d2022c2017-03-29 11:43:58 -06001894 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -06001895 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001896 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1897 // Only Color bit must be set
1898 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001899 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -07001900 error_str
Mark Lobodzinski298f0fd2020-04-09 11:50:19 -06001901 << "ImageView (" << report_data->FormatHandle(image_view)
Dave Houltona9df0ce2018-02-07 10:51:23 -07001902 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001903 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001904 return false;
1905 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001906 // format must NOT be DS
1907 if (ds) {
1908 std::stringstream error_str;
Mark Lobodzinski298f0fd2020-04-09 11:50:19 -06001909 error_str << "ImageView (" << report_data->FormatHandle(image_view)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001910 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1911 << string_VkFormat(format) << " which is not a color format.";
1912 *error_msg = error_str.str();
1913 return false;
1914 }
1915 break;
1916 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1917 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1918 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001919 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1920 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1921 // both must NOT be set
1922 std::stringstream error_str;
Mark Lobodzinski298f0fd2020-04-09 11:50:19 -06001923 error_str << "ImageView (" << report_data->FormatHandle(image_view)
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001924 << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001925 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001926 return false;
1927 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001928 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1929 // Neither were set
1930 std::stringstream error_str;
Mark Lobodzinski298f0fd2020-04-09 11:50:19 -06001931 error_str << "ImageView (" << report_data->FormatHandle(image_view) << ") has layout "
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001932 << string_VkImageLayout(image_layout) << " but does not have STENCIL or DEPTH aspects set";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001933 *error_msg = error_str.str();
1934 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001935 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001936 // format must be DS
1937 if (!ds) {
1938 std::stringstream error_str;
Mark Lobodzinski298f0fd2020-04-09 11:50:19 -06001939 error_str << "ImageView (" << report_data->FormatHandle(image_view) << ") has layout "
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001940 << string_VkImageLayout(image_layout) << " but the image format is " << string_VkFormat(format)
1941 << " which is not a depth/stencil format.";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001942 *error_msg = error_str.str();
1943 return false;
1944 }
1945 break;
1946 default:
1947 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1948 if (ds) {
1949 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1950 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1951 // both must NOT be set
1952 std::stringstream error_str;
Mark Lobodzinski298f0fd2020-04-09 11:50:19 -06001953 error_str << "ImageView (" << report_data->FormatHandle(image_view) << ") has layout "
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001954 << string_VkImageLayout(image_layout) << " and is using depth/stencil image of format "
1955 << string_VkFormat(format)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001956 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1957 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1958 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1959 "reads respectively.";
Mark Lobodzinski4d05d7a2019-06-25 09:12:06 -06001960 *error_code = "VUID-VkDescriptorImageInfo-imageView-01976";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001961 *error_msg = error_str.str();
1962 return false;
1963 }
1964 }
1965 }
1966 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001967 }
1968 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001969 // As we're switching per-type, if any type has specific layout requirements, check those here as well
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001970 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1971 // under vkCreateImage()
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001972 const char *error_usage_bit = nullptr;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001973 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001974 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
sfricke-samsung088f1242020-06-06 02:15:35 -07001975 if (iv_state->samplerConversion != VK_NULL_HANDLE) {
1976 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01946";
1977 std::stringstream error_str;
1978 error_str << "ImageView (" << report_data->FormatHandle(image_view) << ")"
1979 << "used as a VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE can't be created with VkSamplerYcbcrConversion";
1980 *error_msg = error_str.str();
1981 return false;
1982 }
1983 // drop through
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001984 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1985 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1986 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
sfricke-samsung7923f212020-02-29 21:17:35 -08001987 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00337";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001988 }
1989 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001990 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001991 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1992 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1993 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
sfricke-samsung7923f212020-02-29 21:17:35 -08001994 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00339";
sfricke-samsungada55a12020-08-15 03:39:41 -07001995 } else if ((VK_IMAGE_LAYOUT_GENERAL != image_layout) || ((VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) &&
1996 (device_extensions.vk_khr_shared_presentable_image))) {
sfricke-samsungf1058982020-09-10 22:36:49 -07001997 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-04152";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001998 std::stringstream error_str;
sfricke-samsungada55a12020-08-15 03:39:41 -07001999 error_str << "Descriptor update with descriptorType VK_DESCRIPTOR_TYPE_STORAGE_IMAGE"
2000 << " is being updated with invalid imageLayout " << string_VkImageLayout(image_layout) << " for image "
2001 << report_data->FormatHandle(image) << " in imageView " << report_data->FormatHandle(image_view)
2002 << ". Allowed layouts are: VK_IMAGE_LAYOUT_GENERAL";
2003 if (device_extensions.vk_khr_shared_presentable_image) {
2004 error_str << " or VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06002005 }
sfricke-samsungada55a12020-08-15 03:39:41 -07002006 *error_msg = error_str.str();
2007 return false;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002008 }
2009 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06002010 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002011 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
2012 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
2013 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
sfricke-samsung7923f212020-02-29 21:17:35 -08002014 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00338";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002015 }
2016 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06002017 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002018 default:
2019 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06002020 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002021 if (error_usage_bit) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06002022 std::stringstream error_str;
Mark Lobodzinski298f0fd2020-04-09 11:50:19 -06002023 error_str << "ImageView (" << report_data->FormatHandle(image_view) << ") with usage mask " << std::hex << std::showbase
2024 << usage << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
2025 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002026 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06002027 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002028 }
John Zulauff4c07882019-01-24 14:03:36 -07002029
sfricke-samsungada55a12020-08-15 03:39:41 -07002030 // All the following types share the same image layouts
2031 // checkf or Storage Images above
2032 if ((type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) || (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||
2033 (type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
John Zulauff4c07882019-01-24 14:03:36 -07002034 // Test that the layout is compatible with the descriptorType for the two sampled image types
2035 const static std::array<VkImageLayout, 3> valid_layouts = {
Jeremy Hayesd0549f62019-06-05 10:15:36 -06002036 {VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL}};
John Zulauff4c07882019-01-24 14:03:36 -07002037
2038 struct ExtensionLayout {
2039 VkImageLayout layout;
Tony-LunarG2ec96bb2019-11-26 13:43:02 -07002040 ExtEnabled DeviceExtensions::*extension;
John Zulauff4c07882019-01-24 14:03:36 -07002041 };
Jeremy Gebben579aaca2021-02-15 13:36:18 -07002042 const static std::array<ExtensionLayout, 5> extended_layouts{{
2043 // Note double brace req'd for aggregate initialization
2044 {VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR, &DeviceExtensions::vk_khr_shared_presentable_image},
2045 {VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2},
2046 {VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2},
2047 {VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL_KHR, &DeviceExtensions::vk_khr_synchronization_2},
2048 {VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR, &DeviceExtensions::vk_khr_synchronization_2},
2049 }};
John Zulaufc93c4252019-06-25 09:19:49 -06002050 auto is_layout = [image_layout, this](const ExtensionLayout &ext_layout) {
2051 return device_extensions.*(ext_layout.extension) && (ext_layout.layout == image_layout);
John Zulauff4c07882019-01-24 14:03:36 -07002052 };
2053
2054 bool valid_layout = (std::find(valid_layouts.cbegin(), valid_layouts.cend(), image_layout) != valid_layouts.cend()) ||
2055 std::any_of(extended_layouts.cbegin(), extended_layouts.cend(), is_layout);
2056
2057 if (!valid_layout) {
sfricke-samsungf1058982020-09-10 22:36:49 -07002058 // The following works as currently all 3 descriptor types share the same set of valid layouts
sfricke-samsungada55a12020-08-15 03:39:41 -07002059 switch (type) {
2060 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
Shannon McPherson2c793ba2020-08-28 12:13:24 -06002061 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-04149";
sfricke-samsungada55a12020-08-15 03:39:41 -07002062 break;
2063 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
Shannon McPherson2c793ba2020-08-28 12:13:24 -06002064 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-04150";
sfricke-samsungada55a12020-08-15 03:39:41 -07002065 break;
2066 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
sfricke-samsungf1058982020-09-10 22:36:49 -07002067 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-04151";
sfricke-samsungada55a12020-08-15 03:39:41 -07002068 break;
2069 default:
2070 break;
2071 }
John Zulauff4c07882019-01-24 14:03:36 -07002072 std::stringstream error_str;
2073 error_str << "Descriptor update with descriptorType " << string_VkDescriptorType(type)
Mark Lobodzinski74eddba2019-06-21 14:16:33 -06002074 << " is being updated with invalid imageLayout " << string_VkImageLayout(image_layout) << " for image "
Mark Lobodzinski298f0fd2020-04-09 11:50:19 -06002075 << report_data->FormatHandle(image) << " in imageView " << report_data->FormatHandle(image_view)
John Zulauff4c07882019-01-24 14:03:36 -07002076 << ". Allowed layouts are: VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, "
2077 << "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL";
2078 for (auto &ext_layout : extended_layouts) {
John Zulaufc93c4252019-06-25 09:19:49 -06002079 if (device_extensions.*(ext_layout.extension)) {
John Zulauff4c07882019-01-24 14:03:36 -07002080 error_str << ", " << string_VkImageLayout(ext_layout.layout);
2081 }
2082 }
2083 *error_msg = error_str.str();
2084 return false;
2085 }
2086 }
2087
sfricke-samsungbd0e8052020-06-06 01:36:39 -07002088 if ((type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) || (type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)) {
2089 const VkComponentMapping components = iv_state->create_info.components;
2090 if (IsIdentitySwizzle(components) == false) {
2091 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00336";
2092 std::stringstream error_str;
2093 error_str << "ImageView (" << report_data->FormatHandle(image_view) << ") has a non-identiy swizzle component, "
2094 << " r swizzle = " << string_VkComponentSwizzle(components.r) << ","
2095 << " g swizzle = " << string_VkComponentSwizzle(components.g) << ","
2096 << " b swizzle = " << string_VkComponentSwizzle(components.b) << ","
2097 << " a swizzle = " << string_VkComponentSwizzle(components.a) << ".";
2098 *error_msg = error_str.str();
2099 return false;
2100 }
2101 }
2102
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002103 return true;
2104}
Tobin Ehlis56a30942016-05-19 08:00:00 -06002105
John Zulaufd2c3dae2019-12-12 11:02:17 -07002106void cvdescriptorset::SamplerDescriptor::WriteUpdate(const ValidationStateTracker *dev_data, const VkWriteDescriptorSet *update,
Jeff Bolzfaffeb32019-10-04 12:47:16 -05002107 const uint32_t index) {
Chris Forbesfea2c542018-04-13 09:34:15 -07002108 if (!immutable_) {
Karl Schultz76d16a42020-11-11 05:05:33 -07002109 sampler_state_ = dev_data->GetConstCastShared<SAMPLER_STATE>(update->pImageInfo[index].sampler);
Chris Forbesfea2c542018-04-13 09:34:15 -07002110 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002111 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002112}
2113
John Zulaufd2c3dae2019-12-12 11:02:17 -07002114void cvdescriptorset::SamplerDescriptor::CopyUpdate(const ValidationStateTracker *dev_data, const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002115 if (!immutable_) {
Karl Schultz76d16a42020-11-11 05:05:33 -07002116 sampler_state_ = static_cast<const SamplerDescriptor *>(src)->sampler_state_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002117 }
2118 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002119}
Tobin Ehlis56a30942016-05-19 08:00:00 -06002120
John Zulauffbf3c202019-07-17 14:57:14 -06002121void cvdescriptorset::SamplerDescriptor::UpdateDrawState(ValidationStateTracker *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06002122 if (!immutable_) {
Jeff Bolzfaffeb32019-10-04 12:47:16 -05002123 auto sampler_state = GetSamplerState();
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002124 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06002125 }
2126}
2127
John Zulaufd2c3dae2019-12-12 11:02:17 -07002128cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const ValidationStateTracker *dev_data, const VkSampler *immut)
Karl Schultz76d16a42020-11-11 05:05:33 -07002129 : immutable_(false), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002130 updated = false;
2131 descriptor_class = ImageSampler;
2132 if (immut) {
Karl Schultz76d16a42020-11-11 05:05:33 -07002133 sampler_state_ = dev_data->GetConstCastShared<SAMPLER_STATE>(*immut);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002134 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002135 }
2136}
Tobin Ehlis56a30942016-05-19 08:00:00 -06002137
John Zulaufd2c3dae2019-12-12 11:02:17 -07002138void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const ValidationStateTracker *dev_data,
2139 const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002140 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06002141 const auto &image_info = update->pImageInfo[index];
Chris Forbesfea2c542018-04-13 09:34:15 -07002142 if (!immutable_) {
Karl Schultz76d16a42020-11-11 05:05:33 -07002143 sampler_state_ = dev_data->GetConstCastShared<SAMPLER_STATE>(image_info.sampler);
Chris Forbesfea2c542018-04-13 09:34:15 -07002144 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002145 image_layout_ = image_info.imageLayout;
Karl Schultz76d16a42020-11-11 05:05:33 -07002146 image_view_state_ = dev_data->GetConstCastShared<IMAGE_VIEW_STATE>(image_info.imageView);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002147}
2148
John Zulaufd2c3dae2019-12-12 11:02:17 -07002149void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const ValidationStateTracker *dev_data, const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002150 if (!immutable_) {
Karl Schultz76d16a42020-11-11 05:05:33 -07002151 sampler_state_ = static_cast<const ImageSamplerDescriptor *>(src)->sampler_state_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002152 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002153 updated = true;
Karl Schultz76d16a42020-11-11 05:05:33 -07002154 image_layout_ = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
2155 image_view_state_ = static_cast<const ImageSamplerDescriptor *>(src)->image_view_state_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002156}
2157
John Zulauffbf3c202019-07-17 14:57:14 -06002158void cvdescriptorset::ImageSamplerDescriptor::UpdateDrawState(ValidationStateTracker *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06002159 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06002160 if (!immutable_) {
Jeff Bolzfaffeb32019-10-04 12:47:16 -05002161 auto sampler_state = GetSamplerState();
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002162 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06002163 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06002164 // Add binding for image
Jeff Bolzfaffeb32019-10-04 12:47:16 -05002165 auto iv_state = GetImageViewState();
Tobin Ehlis8b26a382016-09-14 08:02:49 -06002166 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07002167 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
John Zulauffbf3c202019-07-17 14:57:14 -06002168 dev_data->CallSetImageViewInitialLayoutCallback(cb_node, *iv_state, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06002169 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06002170}
2171
Tobin Ehlis300888c2016-05-18 13:43:26 -06002172cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
Karl Schultz76d16a42020-11-11 05:05:33 -07002173 : storage_(false), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002174 updated = false;
2175 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002176 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01002177}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002178
John Zulaufd2c3dae2019-12-12 11:02:17 -07002179void cvdescriptorset::ImageDescriptor::WriteUpdate(const ValidationStateTracker *dev_data, const VkWriteDescriptorSet *update,
Jeff Bolzfaffeb32019-10-04 12:47:16 -05002180 const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002181 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06002182 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06002183 image_layout_ = image_info.imageLayout;
Karl Schultz76d16a42020-11-11 05:05:33 -07002184 image_view_state_ = dev_data->GetConstCastShared<IMAGE_VIEW_STATE>(image_info.imageView);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002185}
2186
John Zulaufd2c3dae2019-12-12 11:02:17 -07002187void cvdescriptorset::ImageDescriptor::CopyUpdate(const ValidationStateTracker *dev_data, const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002188 updated = true;
Karl Schultz76d16a42020-11-11 05:05:33 -07002189 image_layout_ = static_cast<const ImageDescriptor *>(src)->image_layout_;
2190 image_view_state_ = static_cast<const ImageDescriptor *>(src)->image_view_state_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002191}
2192
John Zulauffbf3c202019-07-17 14:57:14 -06002193void cvdescriptorset::ImageDescriptor::UpdateDrawState(ValidationStateTracker *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06002194 // Add binding for image
Jeff Bolzfaffeb32019-10-04 12:47:16 -05002195 auto iv_state = GetImageViewState();
Tobin Ehlis8b26a382016-09-14 08:02:49 -06002196 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07002197 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
John Zulauffbf3c202019-07-17 14:57:14 -06002198 dev_data->CallSetImageViewInitialLayoutCallback(cb_node, *iv_state, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06002199 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06002200}
2201
Tobin Ehlis300888c2016-05-18 13:43:26 -06002202cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
Karl Schultz76d16a42020-11-11 05:05:33 -07002203 : storage_(false), dynamic_(false), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002204 updated = false;
2205 descriptor_class = GeneralBuffer;
sfricke-samsung11b31212021-02-10 00:41:50 -08002206 dynamic_ = IsDyanmicDescriptor(type);
2207 storage_ = ((VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) || (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002208}
John Zulaufd2c3dae2019-12-12 11:02:17 -07002209void cvdescriptorset::BufferDescriptor::WriteUpdate(const ValidationStateTracker *dev_data, const VkWriteDescriptorSet *update,
Jeff Bolzfaffeb32019-10-04 12:47:16 -05002210 const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002211 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06002212 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06002213 offset_ = buffer_info.offset;
2214 range_ = buffer_info.range;
Karl Schultz76d16a42020-11-11 05:05:33 -07002215 buffer_state_ = dev_data->GetConstCastShared<BUFFER_STATE>(buffer_info.buffer);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002216}
2217
John Zulaufd2c3dae2019-12-12 11:02:17 -07002218void cvdescriptorset::BufferDescriptor::CopyUpdate(const ValidationStateTracker *dev_data, const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002219 updated = true;
Karl Schultz76d16a42020-11-11 05:05:33 -07002220 const auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis300888c2016-05-18 13:43:26 -06002221 offset_ = buff_desc->offset_;
2222 range_ = buff_desc->range_;
Karl Schultz76d16a42020-11-11 05:05:33 -07002223 buffer_state_ = buff_desc->buffer_state_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002224}
2225
John Zulauffbf3c202019-07-17 14:57:14 -06002226void cvdescriptorset::BufferDescriptor::UpdateDrawState(ValidationStateTracker *dev_data, CMD_BUFFER_STATE *cb_node) {
Jeff Bolzfaffeb32019-10-04 12:47:16 -05002227 auto buffer_node = GetBufferState();
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07002228 if (buffer_node) dev_data->AddCommandBufferBindingBuffer(cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06002229}
2230
Karl Schultz76d16a42020-11-11 05:05:33 -07002231cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002232 updated = false;
2233 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002234 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01002235}
Tobin Ehlis56a30942016-05-19 08:00:00 -06002236
John Zulaufd2c3dae2019-12-12 11:02:17 -07002237void cvdescriptorset::TexelDescriptor::WriteUpdate(const ValidationStateTracker *dev_data, const VkWriteDescriptorSet *update,
Jeff Bolzfaffeb32019-10-04 12:47:16 -05002238 const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002239 updated = true;
Karl Schultz76d16a42020-11-11 05:05:33 -07002240 buffer_view_state_ = dev_data->GetConstCastShared<BUFFER_VIEW_STATE>(update->pTexelBufferView[index]);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002241}
2242
John Zulaufd2c3dae2019-12-12 11:02:17 -07002243void cvdescriptorset::TexelDescriptor::CopyUpdate(const ValidationStateTracker *dev_data, const Descriptor *src) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002244 updated = true;
Karl Schultz76d16a42020-11-11 05:05:33 -07002245 buffer_view_state_ = static_cast<const TexelDescriptor *>(src)->buffer_view_state_;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002246}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06002247
John Zulauffbf3c202019-07-17 14:57:14 -06002248void cvdescriptorset::TexelDescriptor::UpdateDrawState(ValidationStateTracker *dev_data, CMD_BUFFER_STATE *cb_node) {
Jeff Bolzfaffeb32019-10-04 12:47:16 -05002249 auto bv_state = GetBufferViewState();
Tobin Ehlis8b872462016-09-14 08:12:08 -06002250 if (bv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07002251 dev_data->AddCommandBufferBindingBufferView(cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06002252 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06002253}
2254
Jeff Bolz95176d02020-04-01 00:36:16 -05002255cvdescriptorset::AccelerationStructureDescriptor::AccelerationStructureDescriptor(const VkDescriptorType type)
sourav parmarcd5fb182020-07-17 12:58:44 -07002256 : acc_(VK_NULL_HANDLE), acc_nv_(VK_NULL_HANDLE) {
Jeff Bolz95176d02020-04-01 00:36:16 -05002257 updated = false;
sourav parmarcd5fb182020-07-17 12:58:44 -07002258 is_khr_ = false;
Jeff Bolz95176d02020-04-01 00:36:16 -05002259 descriptor_class = AccelerationStructure;
2260}
2261void cvdescriptorset::AccelerationStructureDescriptor::WriteUpdate(const ValidationStateTracker *dev_data,
2262 const VkWriteDescriptorSet *update, const uint32_t index) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002263 const auto *acc_info = LvlFindInChain<VkWriteDescriptorSetAccelerationStructureKHR>(update->pNext);
2264 const auto *acc_info_nv = LvlFindInChain<VkWriteDescriptorSetAccelerationStructureNV>(update->pNext);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002265 assert(acc_info || acc_info_nv);
2266 is_khr_ = (acc_info != NULL);
Jeff Bolz95176d02020-04-01 00:36:16 -05002267 updated = true;
sourav parmarcd5fb182020-07-17 12:58:44 -07002268 if (is_khr_) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002269 acc_ = acc_info->pAccelerationStructures[index];
sourav parmarcd5fb182020-07-17 12:58:44 -07002270 acc_state_ = dev_data->GetConstCastShared<ACCELERATION_STRUCTURE_STATE_KHR>(acc_);
2271 } else {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002272 acc_nv_ = acc_info_nv->pAccelerationStructures[index];
sourav parmarcd5fb182020-07-17 12:58:44 -07002273 acc_state_nv_ = dev_data->GetConstCastShared<ACCELERATION_STRUCTURE_STATE>(acc_nv_);
2274 }
Jeff Bolz95176d02020-04-01 00:36:16 -05002275}
2276
2277void cvdescriptorset::AccelerationStructureDescriptor::CopyUpdate(const ValidationStateTracker *dev_data, const Descriptor *src) {
2278 auto acc_desc = static_cast<const AccelerationStructureDescriptor *>(src);
2279 updated = true;
sourav parmarcd5fb182020-07-17 12:58:44 -07002280 if (is_khr_) {
2281 acc_ = acc_desc->acc_;
2282 acc_state_ = dev_data->GetConstCastShared<ACCELERATION_STRUCTURE_STATE_KHR>(acc_);
2283 } else {
2284 acc_nv_ = acc_desc->acc_nv_;
2285 acc_state_nv_ = dev_data->GetConstCastShared<ACCELERATION_STRUCTURE_STATE>(acc_nv_);
2286 }
Jeff Bolz95176d02020-04-01 00:36:16 -05002287}
2288
2289void cvdescriptorset::AccelerationStructureDescriptor::UpdateDrawState(ValidationStateTracker *dev_data,
2290 CMD_BUFFER_STATE *cb_node) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002291 if (is_khr_) {
2292 auto acc_node = GetAccelerationStructureStateKHR();
2293 if (acc_node) dev_data->AddCommandBufferBindingAccelerationStructure(cb_node, acc_node);
2294 } else {
2295 auto acc_node = GetAccelerationStructureStateNV();
2296 if (acc_node) dev_data->AddCommandBufferBindingAccelerationStructure(cb_node, acc_node);
2297 }
Jeff Bolz95176d02020-04-01 00:36:16 -05002298}
2299
Tobin Ehlis300888c2016-05-18 13:43:26 -06002300// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
2301// sets, and then calls their respective Validate[Write|Copy]Update functions.
2302// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
2303// be skipped, then true is returned.
2304// If there is no issue with the update, then false is returned.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002305bool CoreChecks::ValidateUpdateDescriptorSets(uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002306 const VkCopyDescriptorSet *p_cds, const char *func_name) const {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002307 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002308 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06002309 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002310 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07002311 auto set_node = GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06002312 if (!set_node) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002313 skip |=
2314 LogError(dest_set, kVUID_Core_DrawState_InvalidDescriptorSet, "Cannot call %s on %s that has not been allocated.",
2315 func_name, report_data->FormatHandle(dest_set).c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06002316 } else {
Dave Houltond8ed0212018-05-16 17:18:24 -06002317 std::string error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002318 std::string error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06002319 if (!ValidateWriteUpdate(set_node, &p_wds[i], func_name, &error_code, &error_str)) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002320 skip |= LogError(dest_set, error_code, "%s failed write update validation for %s with error: %s.", func_name,
2321 report_data->FormatHandle(dest_set).c_str(), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06002322 }
2323 }
sourav parmara24fb7b2020-05-26 10:50:04 -07002324 if (p_wds[i].pNext) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002325 const auto *pnext_struct = LvlFindInChain<VkWriteDescriptorSetAccelerationStructureKHR>(p_wds[i].pNext);
Mark Lobodzinski17dc4602020-05-29 07:48:40 -06002326 if (pnext_struct) {
2327 for (uint32_t j = 0; j < pnext_struct->accelerationStructureCount; ++j) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002328 const ACCELERATION_STRUCTURE_STATE_KHR *as_state =
2329 GetAccelerationStructureStateKHR(pnext_struct->pAccelerationStructures[j]);
2330 if (as_state && (as_state->create_infoKHR.sType == VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR &&
sourav parmar766e2a72020-12-03 16:17:11 -08002331 (as_state->create_infoKHR.type != VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR &&
2332 as_state->create_infoKHR.type != VK_ACCELERATION_STRUCTURE_TYPE_GENERIC_KHR))) {
sourav parmara24fb7b2020-05-26 10:50:04 -07002333 skip |=
sourav parmarcd5fb182020-07-17 12:58:44 -07002334 LogError(dest_set, "VUID-VkWriteDescriptorSetAccelerationStructureKHR-pAccelerationStructures-03579",
2335 "%s: Each acceleration structure in pAccelerationStructures must have been created with "
sourav parmarbcee7512020-12-28 14:34:49 -08002336 "VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR or VK_ACCELERATION_STRUCTURE_TYPE_GENERIC_KHR.",
sourav parmara24fb7b2020-05-26 10:50:04 -07002337 func_name);
2338 }
2339 }
2340 }
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002341 const auto *pnext_struct_nv = LvlFindInChain<VkWriteDescriptorSetAccelerationStructureNV>(p_wds[i].pNext);
sourav parmarcd5fb182020-07-17 12:58:44 -07002342 if (pnext_struct_nv) {
2343 for (uint32_t j = 0; j < pnext_struct_nv->accelerationStructureCount; ++j) {
2344 const ACCELERATION_STRUCTURE_STATE *as_state =
2345 GetAccelerationStructureStateNV(pnext_struct_nv->pAccelerationStructures[j]);
2346 if (as_state && (as_state->create_infoNV.sType == VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_NV &&
2347 as_state->create_infoNV.info.type != VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_NV)) {
2348 skip |= LogError(dest_set, "VUID-VkWriteDescriptorSetAccelerationStructureNV-pAccelerationStructures-03748",
2349 "%s: Each acceleration structure in pAccelerationStructures must have been created with"
2350 " VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_NV.",
2351 func_name);
2352 }
2353 }
2354 }
sourav parmara24fb7b2020-05-26 10:50:04 -07002355 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002356 }
2357 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06002358 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002359 auto dst_set = p_cds[i].dstSet;
2360 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07002361 auto src_node = GetSetNode(src_set);
2362 auto dst_node = GetSetNode(dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07002363 // Object_tracker verifies that src & dest descriptor set are valid
2364 assert(src_node);
2365 assert(dst_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06002366 std::string error_code;
Tobin Ehlisa1712752017-01-04 09:41:47 -07002367 std::string error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06002368 if (!ValidateCopyUpdate(&p_cds[i], dst_node, src_node, func_name, &error_code, &error_str)) {
Mark Lobodzinski9d38ea22020-03-16 18:22:16 -06002369 LogObjectList objlist(dst_set);
2370 objlist.add(src_set);
locke-lunarg9edc2812019-06-17 23:18:52 -06002371 skip |=
Mark Lobodzinski9d38ea22020-03-16 18:22:16 -06002372 LogError(objlist, error_code, "%s failed copy update from %s to %s with error: %s.", func_name,
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002373 report_data->FormatHandle(src_set).c_str(), report_data->FormatHandle(dst_set).c_str(), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06002374 }
2375 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002376 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002377}
2378// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
2379// sets, and then calls their respective Perform[Write|Copy]Update functions.
2380// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
2381// with the same set of updates.
2382// This is split from the validate code to allow validation prior to calling down the chain, and then update after
2383// calling down the chain.
John Zulaufe3b35f32019-06-25 14:21:21 -06002384void cvdescriptorset::PerformUpdateDescriptorSets(ValidationStateTracker *dev_data, uint32_t write_count,
2385 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
2386 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002387 // Write updates first
2388 uint32_t i = 0;
2389 for (i = 0; i < write_count; ++i) {
2390 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07002391 auto set_node = dev_data->GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06002392 if (set_node) {
Jeff Bolz41a1ced2019-10-11 11:40:49 -05002393 set_node->PerformWriteUpdate(dev_data, &p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06002394 }
2395 }
2396 // Now copy updates
2397 for (i = 0; i < copy_count; ++i) {
2398 auto dst_set = p_cds[i].dstSet;
2399 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07002400 auto src_node = dev_data->GetSetNode(src_set);
2401 auto dst_node = dev_data->GetSetNode(dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06002402 if (src_node && dst_node) {
Jeff Bolz41a1ced2019-10-11 11:40:49 -05002403 dst_node->PerformCopyUpdate(dev_data, &p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06002404 }
2405 }
2406}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07002407
John Zulaufe3b35f32019-06-25 14:21:21 -06002408cvdescriptorset::DecodedTemplateUpdate::DecodedTemplateUpdate(const ValidationStateTracker *device_data,
2409 VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
2410 const void *pData, VkDescriptorSetLayout push_layout) {
John Zulaufb845eb22018-10-12 11:41:06 -06002411 auto const &create_info = template_state->create_info;
2412 inline_infos.resize(create_info.descriptorUpdateEntryCount); // Make sure we have one if we need it
sourav parmar480d2772021-01-24 22:24:54 -08002413 inline_infos_khr.resize(create_info.descriptorUpdateEntryCount);
2414 inline_infos_nv.resize(create_info.descriptorUpdateEntryCount);
John Zulaufb845eb22018-10-12 11:41:06 -06002415 desc_writes.reserve(create_info.descriptorUpdateEntryCount); // emplaced, so reserved without initialization
John Zulauf1d27e0a2018-11-05 10:12:48 -07002416 VkDescriptorSetLayout effective_dsl = create_info.templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
2417 ? create_info.descriptorSetLayout
2418 : push_layout;
Jeff Bolz6ae39612019-10-11 20:57:36 -05002419 auto layout_obj = device_data->GetDescriptorSetLayoutShared(effective_dsl);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07002420
2421 // Create a WriteDescriptorSet struct for each template update entry
2422 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
2423 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
2424 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
2425 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
2426
John Zulaufb6d71202017-12-22 16:47:09 -07002427 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07002428 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
2429 desc_writes.emplace_back();
2430 auto &write_entry = desc_writes.back();
2431
2432 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
2433 char *update_entry = (char *)(pData) + offset;
2434
2435 if (dst_array_element >= binding_count) {
2436 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07002437 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07002438 }
2439
2440 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
2441 write_entry.pNext = NULL;
2442 write_entry.dstSet = descriptorSet;
2443 write_entry.dstBinding = binding_being_updated;
2444 write_entry.dstArrayElement = dst_array_element;
2445 write_entry.descriptorCount = 1;
2446 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
2447
2448 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
2449 case VK_DESCRIPTOR_TYPE_SAMPLER:
2450 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
2451 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2452 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
2453 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2454 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
2455 break;
2456
2457 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2458 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2459 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2460 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
2461 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
2462 break;
2463
2464 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2465 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2466 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
2467 break;
Dave Houlton142c4cb2018-10-17 15:04:41 -06002468 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
2469 VkWriteDescriptorSetInlineUniformBlockEXT *inline_info = &inline_infos[i];
2470 inline_info->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT;
2471 inline_info->pNext = nullptr;
2472 inline_info->dataSize = create_info.pDescriptorUpdateEntries[i].descriptorCount;
2473 inline_info->pData = update_entry;
2474 write_entry.pNext = inline_info;
Ricardo Garciafee15732019-05-28 11:13:31 +02002475 // descriptorCount must match the dataSize member of the VkWriteDescriptorSetInlineUniformBlockEXT structure
2476 write_entry.descriptorCount = inline_info->dataSize;
Dave Houlton142c4cb2018-10-17 15:04:41 -06002477 // skip the rest of the array, they just represent bytes in the update
2478 j = create_info.pDescriptorUpdateEntries[i].descriptorCount;
2479 break;
2480 }
sourav parmar480d2772021-01-24 22:24:54 -08002481 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR: {
2482 VkWriteDescriptorSetAccelerationStructureKHR *inline_info_khr = &inline_infos_khr[i];
2483 inline_info_khr->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR;
2484 inline_info_khr->pNext = nullptr;
2485 inline_info_khr->accelerationStructureCount = create_info.pDescriptorUpdateEntries[i].descriptorCount;
2486 inline_info_khr->pAccelerationStructures = reinterpret_cast<VkAccelerationStructureKHR *>(update_entry);
2487 write_entry.pNext = inline_info_khr;
2488 break;
2489 }
2490 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV: {
2491 VkWriteDescriptorSetAccelerationStructureNV *inline_info_nv = &inline_infos_nv[i];
2492 inline_info_nv->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_NV;
2493 inline_info_nv->pNext = nullptr;
2494 inline_info_nv->accelerationStructureCount = create_info.pDescriptorUpdateEntries[i].descriptorCount;
2495 inline_info_nv->pAccelerationStructures = reinterpret_cast<VkAccelerationStructureNV *>(update_entry);
2496 write_entry.pNext = inline_info_nv;
2497 break;
2498 }
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07002499 default:
2500 assert(0);
2501 break;
2502 }
2503 dst_array_element++;
2504 }
2505 }
John Zulaufb845eb22018-10-12 11:41:06 -06002506}
John Zulaufb45fdc32018-10-12 15:14:17 -06002507// These helper functions carry out the validate and record descriptor updates peformed via update templates. They decode
2508// the templatized data and leverage the non-template UpdateDescriptor helper functions.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002509bool CoreChecks::ValidateUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002510 const void *pData) const {
John Zulaufb45fdc32018-10-12 15:14:17 -06002511 // Translate the templated update into a normal update for validation...
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002512 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
2513 return ValidateUpdateDescriptorSets(static_cast<uint32_t>(decoded_update.desc_writes.size()), decoded_update.desc_writes.data(),
2514 0, NULL, "vkUpdateDescriptorSetWithTemplate()");
John Zulaufb45fdc32018-10-12 15:14:17 -06002515}
John Zulaufb845eb22018-10-12 11:41:06 -06002516
John Zulauf4e7bcb52018-11-02 10:46:30 -06002517std::string cvdescriptorset::DescriptorSet::StringifySetAndLayout() const {
2518 std::string out;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002519 auto layout_handle = layout_->GetDescriptorSetLayout();
John Zulauf4e7bcb52018-11-02 10:46:30 -06002520 if (IsPushDescriptor()) {
Mark Lobodzinski23e395e2020-04-09 10:17:31 -06002521 std::ostringstream str;
Tony-LunarG1d3ee2d2020-10-27 15:54:52 -06002522 str << "Push Descriptors defined with " << state_data_->report_data->FormatHandle(layout_handle);
Mark Lobodzinski23e395e2020-04-09 10:17:31 -06002523 out = str.str();
John Zulauf4e7bcb52018-11-02 10:46:30 -06002524 } else {
Mark Lobodzinski23e395e2020-04-09 10:17:31 -06002525 std::ostringstream str;
Tony-LunarG1d3ee2d2020-10-27 15:54:52 -06002526 str << state_data_->report_data->FormatHandle(set_) << " allocated with "
Mark Lobodzinski23e395e2020-04-09 10:17:31 -06002527 << state_data_->report_data->FormatHandle(layout_handle);
2528 out = str.str();
John Zulauf4e7bcb52018-11-02 10:46:30 -06002529 }
2530 return out;
2531};
2532
John Zulauf1d27e0a2018-11-05 10:12:48 -07002533// Loop through the write updates to validate for a push descriptor set, ignoring dstSet
John Zulaufc93c4252019-06-25 09:19:49 -06002534bool CoreChecks::ValidatePushDescriptorsUpdate(const DescriptorSet *push_set, uint32_t write_count,
John Zulaufbd9b3412019-08-22 17:16:11 -06002535 const VkWriteDescriptorSet *p_wds, const char *func_name) const {
John Zulaufd9435c32019-06-05 15:55:36 -06002536 assert(push_set->IsPushDescriptor());
John Zulauf1d27e0a2018-11-05 10:12:48 -07002537 bool skip = false;
2538 for (uint32_t i = 0; i < write_count; i++) {
2539 std::string error_code;
2540 std::string error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06002541 if (!ValidateWriteUpdate(push_set, &p_wds[i], func_name, &error_code, &error_str)) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002542 skip |= LogError(push_set->GetDescriptorSetLayout(), error_code, "%s failed update validation: %s.", func_name,
2543 error_str.c_str());
John Zulauf1d27e0a2018-11-05 10:12:48 -07002544 }
2545 }
2546 return skip;
2547}
2548
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002549// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002550// If there's an error, update the error_msg string with details and return false, else return true
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002551bool cvdescriptorset::ValidateBufferUsage(debug_report_data *report_data, BUFFER_STATE const *buffer_node, VkDescriptorType type,
2552 std::string *error_code, std::string *error_msg) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002553 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06002554 auto usage = buffer_node->createInfo.usage;
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002555 const char *error_usage_bit = nullptr;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002556 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002557 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2558 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002559 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00334";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002560 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
2561 }
2562 break;
2563 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2564 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002565 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00335";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002566 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
2567 }
2568 break;
2569 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2570 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2571 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002572 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00330";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002573 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
2574 }
2575 break;
2576 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2577 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
2578 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002579 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00331";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002580 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
2581 }
2582 break;
2583 default:
2584 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002585 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002586 if (error_usage_bit) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002587 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002588 error_str << "Buffer (" << report_data->FormatHandle(buffer_node->buffer) << ") with usage mask " << std::hex
2589 << std::showbase << usage << " being used for a descriptor update of type " << string_VkDescriptorType(type)
2590 << " does not have " << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002591 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002592 return false;
2593 }
2594 return true;
2595}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002596// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
2597// 1. buffer is valid
2598// 2. buffer was created with correct usage flags
2599// 3. offset is less than buffer size
2600// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002601// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002602// If there's an error, update the error_msg string with details and return false, else return true
John Zulaufc93c4252019-06-25 09:19:49 -06002603bool CoreChecks::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type, const char *func_name,
John Zulaufbd9b3412019-08-22 17:16:11 -06002604 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002605 // First make sure that buffer is valid
John Zulaufc93c4252019-06-25 09:19:49 -06002606 auto buffer_node = GetBufferState(buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07002607 // Any invalid buffer should already be caught by object_tracker
2608 assert(buffer_node);
John Zulaufc93c4252019-06-25 09:19:49 -06002609 if (ValidateMemoryIsBoundToBuffer(buffer_node, func_name, "VUID-VkWriteDescriptorSet-descriptorType-00329")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002610 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00329";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002611 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06002612 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06002613 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002614 // Verify usage bits
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002615 if (!cvdescriptorset::ValidateBufferUsage(report_data, buffer_node, type, error_code, error_msg)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002616 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002617 return false;
2618 }
2619 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002620 if (buffer_info->offset >= buffer_node->createInfo.size) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002621 *error_code = "VUID-VkDescriptorBufferInfo-offset-00340";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002622 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002623 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002624 << report_data->FormatHandle(buffer_node->buffer) << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002625 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002626 return false;
2627 }
2628 if (buffer_info->range != VK_WHOLE_SIZE) {
2629 // Range must be VK_WHOLE_SIZE or > 0
2630 if (!buffer_info->range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002631 *error_code = "VUID-VkDescriptorBufferInfo-range-00341";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002632 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002633 error_str << "For buffer " << report_data->FormatHandle(buffer_node->buffer)
2634 << " VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002635 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002636 return false;
2637 }
2638 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
2639 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002640 *error_code = "VUID-VkDescriptorBufferInfo-range-00342";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002641 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002642 error_str << "For buffer " << report_data->FormatHandle(buffer_node->buffer) << " VkDescriptorBufferInfo range is "
2643 << buffer_info->range << " which is greater than buffer size (" << buffer_node->createInfo.size
2644 << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002645 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002646 return false;
2647 }
2648 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002649 // Check buffer update sizes against device limits
John Zulaufc93c4252019-06-25 09:19:49 -06002650 const auto &limits = phys_dev_props.limits;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002651 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
John Zulaufd9435c32019-06-05 15:55:36 -06002652 auto max_ub_range = limits.maxUniformBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002653 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002654 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002655 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002656 error_str << "For buffer " << report_data->FormatHandle(buffer_node->buffer) << " VkDescriptorBufferInfo range is "
2657 << buffer_info->range << " which is greater than this device's maxUniformBufferRange (" << max_ub_range
2658 << ")";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002659 *error_msg = error_str.str();
2660 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002661 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_ub_range) {
2662 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
2663 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002664 error_str << "For buffer " << report_data->FormatHandle(buffer_node->buffer)
2665 << " VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
Peter Kohaut18f413d2018-07-16 13:15:42 +02002666 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002667 << "maxUniformBufferRange (" << max_ub_range << ")";
2668 *error_msg = error_str.str();
2669 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002670 }
2671 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
John Zulaufd9435c32019-06-05 15:55:36 -06002672 auto max_sb_range = limits.maxStorageBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002673 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002674 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002675 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002676 error_str << "For buffer " << report_data->FormatHandle(buffer_node->buffer) << " VkDescriptorBufferInfo range is "
2677 << buffer_info->range << " which is greater than this device's maxStorageBufferRange (" << max_sb_range
2678 << ")";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002679 *error_msg = error_str.str();
2680 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002681 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_sb_range) {
2682 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
2683 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002684 error_str << "For buffer " << report_data->FormatHandle(buffer_node->buffer)
2685 << " VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
Peter Kohaut18f413d2018-07-16 13:15:42 +02002686 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002687 << "maxStorageBufferRange (" << max_sb_range << ")";
2688 *error_msg = error_str.str();
2689 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002690 }
2691 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002692 return true;
2693}
sourav parmarcd5fb182020-07-17 12:58:44 -07002694template <typename T>
2695bool CoreChecks::ValidateAccelerationStructureUpdate(T acc_node, const char *func_name, std::string *error_code,
Jeff Bolz95176d02020-04-01 00:36:16 -05002696 std::string *error_msg) const {
Jeff Bolz95176d02020-04-01 00:36:16 -05002697 // Any invalid acc struct should already be caught by object_tracker
2698 assert(acc_node);
2699 if (ValidateMemoryIsBoundToAccelerationStructure(acc_node, func_name, kVUIDUndefined)) {
2700 *error_code = kVUIDUndefined;
2701 *error_msg = "No memory bound to acceleration structure.";
2702 return false;
2703 }
2704 return true;
2705}
2706
Tobin Ehlis300888c2016-05-18 13:43:26 -06002707// Verify that the contents of the update are ok, but don't perform actual update
Mark Lobodzinskif4ed6c12020-01-03 11:21:58 -07002708bool CoreChecks::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
2709 VkDescriptorType src_type, uint32_t src_index, const DescriptorSet *dst_set,
2710 VkDescriptorType dst_type, uint32_t dst_index, const char *func_name,
2711 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002712 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
2713 // for write updates
John Zulaufc93c4252019-06-25 09:19:49 -06002714 using DescriptorClass = cvdescriptorset::DescriptorClass;
2715 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2716 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2717 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2718 using SamplerDescriptor = cvdescriptorset::SamplerDescriptor;
2719 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2720
2721 auto device_data = this;
Mark Lobodzinskif4ed6c12020-01-03 11:21:58 -07002722
2723 if (dst_type == VK_DESCRIPTOR_TYPE_SAMPLER) {
2724 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2725 const auto dst_desc = dst_set->GetDescriptorFromGlobalIndex(dst_index + di);
2726 if (!dst_desc->updated) continue;
2727 if (dst_desc->IsImmutableSampler()) {
2728 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02753";
2729 std::stringstream error_str;
2730 error_str << "Attempted copy update to an immutable sampler descriptor.";
2731 *error_msg = error_str.str();
2732 return false;
2733 }
2734 }
2735 }
2736
2737 switch (src_set->GetDescriptorFromGlobalIndex(src_index)->descriptor_class) {
John Zulaufc93c4252019-06-25 09:19:49 -06002738 case DescriptorClass::PlainSampler: {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002739 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Mark Lobodzinskif4ed6c12020-01-03 11:21:58 -07002740 const auto src_desc = src_set->GetDescriptorFromGlobalIndex(src_index + di);
Józef Kucia5297e372017-10-13 22:31:34 +02002741 if (!src_desc->updated) continue;
2742 if (!src_desc->IsImmutableSampler()) {
John Zulaufd9435c32019-06-05 15:55:36 -06002743 auto update_sampler = static_cast<const SamplerDescriptor *>(src_desc)->GetSampler();
John Zulaufc93c4252019-06-25 09:19:49 -06002744 if (!ValidateSampler(update_sampler)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002745 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002746 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002747 error_str << "Attempted copy update to sampler descriptor with invalid sampler: "
2748 << report_data->FormatHandle(update_sampler) << ".";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002749 *error_msg = error_str.str();
2750 return false;
2751 }
2752 } else {
2753 // TODO : Warn here
2754 }
2755 }
2756 break;
2757 }
John Zulaufc93c4252019-06-25 09:19:49 -06002758 case DescriptorClass::ImageSampler: {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002759 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Mark Lobodzinskif4ed6c12020-01-03 11:21:58 -07002760 const auto src_desc = src_set->GetDescriptorFromGlobalIndex(src_index + di);
Józef Kucia5297e372017-10-13 22:31:34 +02002761 if (!src_desc->updated) continue;
2762 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002763 // First validate sampler
2764 if (!img_samp_desc->IsImmutableSampler()) {
2765 auto update_sampler = img_samp_desc->GetSampler();
John Zulaufc93c4252019-06-25 09:19:49 -06002766 if (!ValidateSampler(update_sampler)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002767 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002768 std::stringstream error_str;
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002769 error_str << "Attempted copy update to sampler descriptor with invalid sampler: "
2770 << report_data->FormatHandle(update_sampler) << ".";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002771 *error_msg = error_str.str();
2772 return false;
2773 }
2774 } else {
2775 // TODO : Warn here
2776 }
2777 // Validate image
2778 auto image_view = img_samp_desc->GetImageView();
2779 auto image_layout = img_samp_desc->GetImageLayout();
Jeff Bolz165818a2020-05-08 11:19:03 -05002780 if (image_view) {
2781 if (!ValidateImageUpdate(image_view, image_layout, src_type, func_name, error_code, error_msg)) {
2782 std::stringstream error_str;
2783 error_str << "Attempted copy update to combined image sampler descriptor failed due to: "
2784 << error_msg->c_str();
2785 *error_msg = error_str.str();
2786 return false;
2787 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002788 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002789 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002790 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002791 }
John Zulaufc93c4252019-06-25 09:19:49 -06002792 case DescriptorClass::Image: {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002793 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Mark Lobodzinskif4ed6c12020-01-03 11:21:58 -07002794 const auto src_desc = src_set->GetDescriptorFromGlobalIndex(src_index + di);
Józef Kucia5297e372017-10-13 22:31:34 +02002795 if (!src_desc->updated) continue;
2796 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002797 auto image_view = img_desc->GetImageView();
2798 auto image_layout = img_desc->GetImageLayout();
Jeff Bolz165818a2020-05-08 11:19:03 -05002799 if (image_view) {
2800 if (!ValidateImageUpdate(image_view, image_layout, src_type, func_name, error_code, error_msg)) {
2801 std::stringstream error_str;
2802 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
2803 *error_msg = error_str.str();
2804 return false;
2805 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002806 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002807 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002808 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002809 }
John Zulaufc93c4252019-06-25 09:19:49 -06002810 case DescriptorClass::TexelBuffer: {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002811 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Mark Lobodzinskif4ed6c12020-01-03 11:21:58 -07002812 const auto src_desc = src_set->GetDescriptorFromGlobalIndex(src_index + di);
Józef Kucia5297e372017-10-13 22:31:34 +02002813 if (!src_desc->updated) continue;
John Zulaufd9435c32019-06-05 15:55:36 -06002814 auto buffer_view = static_cast<const TexelDescriptor *>(src_desc)->GetBufferView();
Jeff Bolz165818a2020-05-08 11:19:03 -05002815 if (buffer_view) {
2816 auto bv_state = device_data->GetBufferViewState(buffer_view);
2817 if (!bv_state) {
2818 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02994";
2819 std::stringstream error_str;
2820 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: "
2821 << report_data->FormatHandle(buffer_view);
2822 *error_msg = error_str.str();
2823 return false;
2824 }
2825 auto buffer = bv_state->create_info.buffer;
2826 if (!cvdescriptorset::ValidateBufferUsage(report_data, GetBufferState(buffer), src_type, error_code,
2827 error_msg)) {
2828 std::stringstream error_str;
2829 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
2830 *error_msg = error_str.str();
2831 return false;
2832 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002833 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002834 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002835 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002836 }
John Zulaufc93c4252019-06-25 09:19:49 -06002837 case DescriptorClass::GeneralBuffer: {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002838 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Mark Lobodzinskif4ed6c12020-01-03 11:21:58 -07002839 const auto src_desc = src_set->GetDescriptorFromGlobalIndex(src_index + di);
Józef Kucia5297e372017-10-13 22:31:34 +02002840 if (!src_desc->updated) continue;
John Zulaufd9435c32019-06-05 15:55:36 -06002841 auto buffer = static_cast<const BufferDescriptor *>(src_desc)->GetBuffer();
Jeff Bolz165818a2020-05-08 11:19:03 -05002842 if (buffer) {
2843 if (!cvdescriptorset::ValidateBufferUsage(report_data, GetBufferState(buffer), src_type, error_code,
2844 error_msg)) {
2845 std::stringstream error_str;
2846 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
2847 *error_msg = error_str.str();
2848 return false;
2849 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002850 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06002851 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002852 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002853 }
John Zulaufc93c4252019-06-25 09:19:49 -06002854 case DescriptorClass::InlineUniform:
2855 case DescriptorClass::AccelerationStructure:
Jeff Bolze54ae892018-09-08 12:16:29 -05002856 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002857 default:
2858 assert(0); // We've already verified update type so should never get here
2859 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002860 }
2861 // All checks passed so update contents are good
2862 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12002863}
Tobin Ehlisee471462016-05-26 11:21:59 -06002864// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002865bool CoreChecks::ValidateAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002866 const cvdescriptorset::AllocateDescriptorSetsData *ds_data) const {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002867 bool skip = false;
Mark Lobodzinski7804bd42019-03-06 11:28:48 -07002868 auto pool_state = GetDescriptorPoolState(p_alloc_info->descriptorPool);
Tobin Ehlisee471462016-05-26 11:21:59 -06002869
2870 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolz6ae39612019-10-11 20:57:36 -05002871 auto layout = GetDescriptorSetLayoutShared(p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07002872 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
John Zulauf1d27e0a2018-11-05 10:12:48 -07002873 if (layout->IsPushDescriptor()) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002874 skip |= LogError(p_alloc_info->pSetLayouts[i], "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-00308",
2875 "%s specified at pSetLayouts[%" PRIu32
2876 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
2877 report_data->FormatHandle(p_alloc_info->pSetLayouts[i]).c_str(), i,
2878 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07002879 }
Mike Schuchardt2df08912020-12-15 16:28:09 -08002880 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT &&
2881 !(pool_state->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT)) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002882 skip |= LogError(device, "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-03044",
2883 "Descriptor set layout create flags and pool create flags mismatch for index (%d)", i);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002884 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002885 }
2886 }
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002887 if (!device_extensions.vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002888 // Track number of descriptorSets allowable in this pool
2889 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002890 skip |= LogError(pool_state->pool, "VUID-VkDescriptorSetAllocateInfo-descriptorSetCount-00306",
2891 "Unable to allocate %u descriptorSets from %s"
2892 ". This pool only has %d descriptorSets remaining.",
2893 p_alloc_info->descriptorSetCount, report_data->FormatHandle(pool_state->pool).c_str(),
2894 pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002895 }
2896 // Determine whether descriptor counts are satisfiable
Jeff Bolze54ae892018-09-08 12:16:29 -05002897 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002898 auto count_iter = pool_state->availableDescriptorTypeCount.find(it->first);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002899 uint32_t available_count = (count_iter != pool_state->availableDescriptorTypeCount.end()) ? count_iter->second : 0;
Jeff Bolz46c0ea02019-10-09 13:06:29 -05002900
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002901 if (ds_data->required_descriptors_by_type.at(it->first) > available_count) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002902 skip |= LogError(pool_state->pool, "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307",
2903 "Unable to allocate %u descriptors of type %s from %s"
2904 ". This pool only has %d descriptors of this type remaining.",
2905 ds_data->required_descriptors_by_type.at(it->first),
2906 string_VkDescriptorType(VkDescriptorType(it->first)),
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002907 report_data->FormatHandle(pool_state->pool).c_str(), available_count);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002908 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002909 }
2910 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06002911
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002912 const auto *count_allocate_info = LvlFindInChain<VkDescriptorSetVariableDescriptorCountAllocateInfo>(p_alloc_info->pNext);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002913
2914 if (count_allocate_info) {
2915 if (count_allocate_info->descriptorSetCount != 0 &&
2916 count_allocate_info->descriptorSetCount != p_alloc_info->descriptorSetCount) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002917 skip |= LogError(device, "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfo-descriptorSetCount-03045",
2918 "VkDescriptorSetAllocateInfo::descriptorSetCount (%d) != "
Mike Schuchardt2df08912020-12-15 16:28:09 -08002919 "VkDescriptorSetVariableDescriptorCountAllocateInfo::descriptorSetCount (%d)",
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002920 p_alloc_info->descriptorSetCount, count_allocate_info->descriptorSetCount);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002921 }
2922 if (count_allocate_info->descriptorSetCount == p_alloc_info->descriptorSetCount) {
2923 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolz6ae39612019-10-11 20:57:36 -05002924 auto layout = GetDescriptorSetLayoutShared(p_alloc_info->pSetLayouts[i]);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002925 if (count_allocate_info->pDescriptorCounts[i] > layout->GetDescriptorCountFromBinding(layout->GetMaxBinding())) {
Mark Lobodzinskid18de902020-01-15 12:20:37 -07002926 skip |= LogError(device, "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfo-pSetLayouts-03046",
2927 "pDescriptorCounts[%d] = (%d), binding's descriptorCount = (%d)", i,
2928 count_allocate_info->pDescriptorCounts[i],
2929 layout->GetDescriptorCountFromBinding(layout->GetMaxBinding()));
Jeff Bolzfdf96072018-04-10 14:32:18 -05002930 }
2931 }
2932 }
2933 }
2934
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002935 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06002936}
John Zulauf48a6a702017-12-22 17:14:54 -07002937
Jeff Bolzdd4cfa12019-08-11 20:57:51 -05002938const BindingReqMap &cvdescriptorset::PrefilterBindRequestMap::FilteredMap(const CMD_BUFFER_STATE &cb_state,
2939 const PIPELINE_STATE &pipeline) {
John Zulauffbf3c202019-07-17 14:57:14 -06002940 if (IsManyDescriptors()) {
Karl Schultz7090a052020-11-10 08:54:21 -07002941 filtered_map_.reset(new BindingReqMap);
John Zulauffbf3c202019-07-17 14:57:14 -06002942 descriptor_set_.FilterBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
2943 return *filtered_map_;
John Zulauf48a6a702017-12-22 17:14:54 -07002944 }
John Zulauffbf3c202019-07-17 14:57:14 -06002945 return orig_map_;
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01002946}
John Zulauf4a015c92019-06-04 09:50:05 -06002947
2948// Starting at offset descriptor of given binding, parse over update_count
2949// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
2950// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
2951// If so, return true. If not, fill in error_msg and return false
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002952bool cvdescriptorset::VerifyUpdateConsistency(debug_report_data *report_data,
2953 DescriptorSetLayout::ConstBindingIterator current_binding, uint32_t offset,
John Zulauf4a015c92019-06-04 09:50:05 -06002954 uint32_t update_count, const char *type, const VkDescriptorSet set,
2955 std::string *error_msg) {
locke-lunarge46b7782019-09-10 01:44:20 -06002956 bool pass = true;
John Zulauf4a015c92019-06-04 09:50:05 -06002957 // Verify consecutive bindings match (if needed)
2958 auto orig_binding = current_binding;
locke-lunarge46b7782019-09-10 01:44:20 -06002959
2960 while (pass && update_count) {
2961 // First, it's legal to offset beyond your own binding so handle that case
2962 if (offset > 0) {
2963 const auto &index_range = current_binding.GetGlobalIndexRange();
2964 // index_range.start + offset is which descriptor is needed to update. If it > index_range.end, it means the descriptor
2965 // isn't in this binding, maybe in next binding.
2966 if ((index_range.start + offset) >= index_range.end) {
2967 // Advance to next binding, decrement offset by binding size
2968 offset -= current_binding.GetDescriptorCount();
2969 ++current_binding;
2970 // Verify next consecutive binding matches type, stage flags & immutable sampler use and if AtEnd
2971 if (!orig_binding.IsConsistent(current_binding)) {
2972 pass = false;
2973 }
2974 continue;
John Zulauf4a015c92019-06-04 09:50:05 -06002975 }
John Zulauf4a015c92019-06-04 09:50:05 -06002976 }
locke-lunarge46b7782019-09-10 01:44:20 -06002977
2978 update_count -= std::min(update_count, current_binding.GetDescriptorCount() - offset);
2979 if (update_count) {
2980 // Starting offset is beyond the current binding. Check consistency, update counters and advance to the next binding,
2981 // looking for the start point. All bindings (even those skipped) must be consistent with the update and with the
2982 // original binding.
2983 offset = 0;
2984 ++current_binding;
2985 // Verify next consecutive binding matches type, stage flags & immutable sampler use and if AtEnd
2986 if (!orig_binding.IsConsistent(current_binding)) {
2987 pass = false;
2988 }
2989 }
John Zulauf4a015c92019-06-04 09:50:05 -06002990 }
locke-lunarge46b7782019-09-10 01:44:20 -06002991
2992 if (!pass) {
2993 std::stringstream error_str;
2994 error_str << "Attempting " << type;
2995 if (current_binding.Layout()->IsPushDescriptor()) {
2996 error_str << " push descriptors";
2997 } else {
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06002998 error_str << " descriptor set " << report_data->FormatHandle(set);
locke-lunarge46b7782019-09-10 01:44:20 -06002999 }
3000 error_str << " binding #" << orig_binding.Binding() << " with #" << update_count
3001 << " descriptors being updated but this update oversteps the bounds of this binding and the next binding is "
3002 "not consistent with current binding so this update is invalid.";
3003 *error_msg = error_str.str();
3004 }
3005 return pass;
John Zulauf4a015c92019-06-04 09:50:05 -06003006}
John Zulauf4956fff2019-06-04 16:54:38 -06003007
3008// Validate the state for a given write update but don't actually perform the update
3009// If an error would occur for this update, return false and fill in details in error_msg string
John Zulaufc93c4252019-06-25 09:19:49 -06003010bool CoreChecks::ValidateWriteUpdate(const DescriptorSet *dest_set, const VkWriteDescriptorSet *update, const char *func_name,
John Zulaufbd9b3412019-08-22 17:16:11 -06003011 std::string *error_code, std::string *error_msg) const {
Jeff Bolz6aad1742019-10-16 11:10:09 -05003012 const auto dest_layout = dest_set->GetLayout().get();
John Zulauf4956fff2019-06-04 16:54:38 -06003013
3014 // Verify dst layout still valid
Jeff Bolz6ae39612019-10-11 20:57:36 -05003015 if (dest_layout->destroyed) {
John Zulauf4956fff2019-06-04 16:54:38 -06003016 *error_code = "VUID-VkWriteDescriptorSet-dstSet-00320";
Mark Lobodzinski23e395e2020-04-09 10:17:31 -06003017 std::ostringstream str;
3018 str << "Cannot call " << func_name << " to perform write update on " << dest_set->StringifySetAndLayout()
3019 << " which has been destroyed";
3020 *error_msg = str.str();
John Zulauf4956fff2019-06-04 16:54:38 -06003021 return false;
3022 }
3023 // Verify dst binding exists
3024 if (!dest_layout->HasBinding(update->dstBinding)) {
3025 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00315";
3026 std::stringstream error_str;
3027 error_str << dest_set->StringifySetAndLayout() << " does not have binding " << update->dstBinding;
3028 *error_msg = error_str.str();
3029 return false;
3030 }
3031
Jeff Bolz6aad1742019-10-16 11:10:09 -05003032 DescriptorSetLayout::ConstBindingIterator dest(dest_layout, update->dstBinding);
John Zulauf4956fff2019-06-04 16:54:38 -06003033 // Make sure binding isn't empty
3034 if (0 == dest.GetDescriptorCount()) {
3035 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00316";
3036 std::stringstream error_str;
3037 error_str << dest_set->StringifySetAndLayout() << " cannot updated binding " << update->dstBinding
3038 << " that has 0 descriptors";
3039 *error_msg = error_str.str();
3040 return false;
3041 }
3042
3043 // Verify idle ds
Mike Schuchardt2df08912020-12-15 16:28:09 -08003044 if (dest_set->in_use.load() && !(dest.GetDescriptorBindingFlags() & (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT |
3045 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT))) {
John Zulauf4956fff2019-06-04 16:54:38 -06003046 // TODO : Re-using Free Idle error code, need write update idle error code
3047 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
3048 std::stringstream error_str;
3049 error_str << "Cannot call " << func_name << " to perform write update on " << dest_set->StringifySetAndLayout()
3050 << " that is in use by a command buffer";
3051 *error_msg = error_str.str();
3052 return false;
3053 }
3054 // We know that binding is valid, verify update and do update on each descriptor
3055 auto start_idx = dest.GetGlobalIndexRange().start + update->dstArrayElement;
3056 auto type = dest.GetType();
3057 if (type != update->descriptorType) {
3058 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00319";
3059 std::stringstream error_str;
3060 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
3061 << " with type " << string_VkDescriptorType(type) << " but update type is "
3062 << string_VkDescriptorType(update->descriptorType);
3063 *error_msg = error_str.str();
3064 return false;
3065 }
John Zulauf4956fff2019-06-04 16:54:38 -06003066 if (type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
3067 if ((update->dstArrayElement % 4) != 0) {
3068 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02219";
3069 std::stringstream error_str;
3070 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
3071 << " with "
3072 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
3073 *error_msg = error_str.str();
3074 return false;
3075 }
3076 if ((update->descriptorCount % 4) != 0) {
3077 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02220";
3078 std::stringstream error_str;
3079 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
3080 << " with "
3081 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
3082 *error_msg = error_str.str();
3083 return false;
3084 }
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07003085 const auto *write_inline_info = LvlFindInChain<VkWriteDescriptorSetInlineUniformBlockEXT>(update->pNext);
John Zulauf4956fff2019-06-04 16:54:38 -06003086 if (!write_inline_info || write_inline_info->dataSize != update->descriptorCount) {
3087 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02221";
3088 std::stringstream error_str;
3089 if (!write_inline_info) {
3090 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #"
3091 << update->dstBinding << " with "
3092 << "VkWriteDescriptorSetInlineUniformBlockEXT missing";
3093 } else {
3094 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #"
3095 << update->dstBinding << " with "
3096 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
3097 << " not equal to "
3098 << "VkWriteDescriptorSet descriptorCount " << update->descriptorCount;
3099 }
3100 *error_msg = error_str.str();
3101 return false;
3102 }
3103 // This error is probably unreachable due to the previous two errors
3104 if (write_inline_info && (write_inline_info->dataSize % 4) != 0) {
3105 *error_code = "VUID-VkWriteDescriptorSetInlineUniformBlockEXT-dataSize-02222";
3106 std::stringstream error_str;
3107 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
3108 << " with "
3109 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
3110 << " not a multiple of 4";
3111 *error_msg = error_str.str();
3112 return false;
3113 }
3114 }
sfricke-samsung941d48b2020-02-10 00:20:01 -08003115 // Verify all bindings update share identical properties across all items
3116 if (update->descriptorCount > 0) {
3117 // Save first binding information and error if something different is found
3118 DescriptorSetLayout::ConstBindingIterator current_binding(dest_layout, update->dstBinding);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003119 VkShaderStageFlags stage_flags = current_binding.GetStageFlags();
3120 VkDescriptorType descriptor_type = current_binding.GetType();
3121 bool immutable_samplers = (current_binding.GetImmutableSamplerPtr() == nullptr);
3122 uint32_t dst_array_element = update->dstArrayElement;
sfricke-samsung941d48b2020-02-10 00:20:01 -08003123
Jeff Bolz9198e882020-03-18 13:03:30 -05003124 for (uint32_t i = 0; i < update->descriptorCount;) {
sfricke-samsung941d48b2020-02-10 00:20:01 -08003125 if (current_binding.AtEnd() == true) {
3126 break; // prevents setting error here if bindings don't exist
3127 }
3128
3129 // Check for consistent stageFlags and descriptorType
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003130 if ((current_binding.GetStageFlags() != stage_flags) || (current_binding.GetType() != descriptor_type)) {
sfricke-samsung941d48b2020-02-10 00:20:01 -08003131 *error_code = "VUID-VkWriteDescriptorSet-descriptorCount-00317";
3132 std::stringstream error_str;
3133 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding index #"
3134 << current_binding.GetIndex() << " (" << i << " from dstBinding offset)"
3135 << " with a different stageFlag and/or descriptorType from previous bindings."
3136 << " All bindings must have consecutive stageFlag and/or descriptorType across a VkWriteDescriptorSet";
3137 *error_msg = error_str.str();
3138 return false;
3139 }
3140 // Check if all immutableSamplers or not
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003141 if ((current_binding.GetImmutableSamplerPtr() == nullptr) != immutable_samplers) {
sfricke-samsung941d48b2020-02-10 00:20:01 -08003142 *error_code = "VUID-VkWriteDescriptorSet-descriptorCount-00318";
3143 std::stringstream error_str;
3144 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding index #"
3145 << current_binding.GetIndex() << " (" << i << " from dstBinding offset)"
3146 << " with a different usage of immutable samplers from previous bindings."
3147 << " All bindings must have all or none usage of immutable samplers across a VkWriteDescriptorSet";
3148 *error_msg = error_str.str();
3149 return false;
3150 }
Jeff Bolz9198e882020-03-18 13:03:30 -05003151
3152 // Skip the remaining descriptors for this binding, and move to the next binding
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003153 i += (current_binding.GetDescriptorCount() - dst_array_element);
3154 dst_array_element = 0;
sfricke-samsung941d48b2020-02-10 00:20:01 -08003155 ++current_binding;
3156 }
3157 }
3158
John Zulauf4956fff2019-06-04 16:54:38 -06003159 // Verify consecutive bindings match (if needed)
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06003160 if (!VerifyUpdateConsistency(report_data, DescriptorSetLayout::ConstBindingIterator(dest_layout, update->dstBinding),
John Zulauf4956fff2019-06-04 16:54:38 -06003161 update->dstArrayElement, update->descriptorCount, "write update to", dest_set->GetSet(),
3162 error_msg)) {
3163 // TODO : Should break out "consecutive binding updates" language into valid usage statements
3164 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
3165 return false;
3166 }
Tony-LunarG1f79c952020-10-27 15:55:51 -06003167 // Verify write to variable descriptor
3168 if (dest_set->IsVariableDescriptorCount(update->dstBinding)) {
3169 if ((update->dstArrayElement + update->descriptorCount) > dest_set->GetVariableDescriptorCount()) {
3170 std::stringstream error_str;
3171 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
3172 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding index #"
3173 << update->dstBinding << " array element " << update->dstArrayElement << " with " << update->descriptorCount
3174 << " writes but variable descriptor size is " << dest_set->GetVariableDescriptorCount();
3175 *error_msg = error_str.str();
3176 return false;
3177 }
3178 }
John Zulauf4956fff2019-06-04 16:54:38 -06003179 // Update is within bounds and consistent so last step is to validate update contents
John Zulauf459939f2019-06-04 16:49:35 -06003180 if (!VerifyWriteUpdateContents(dest_set, update, start_idx, func_name, error_code, error_msg)) {
John Zulauf4956fff2019-06-04 16:54:38 -06003181 std::stringstream error_str;
3182 error_str << "Write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
3183 << " failed with error message: " << error_msg->c_str();
3184 *error_msg = error_str.str();
3185 return false;
3186 }
3187 // All checks passed, update is clean
3188 return true;
3189}
John Zulaufadb3f542019-06-04 17:01:00 -06003190
3191// Verify that the contents of the update are ok, but don't perform actual update
John Zulaufc93c4252019-06-25 09:19:49 -06003192bool CoreChecks::VerifyWriteUpdateContents(const DescriptorSet *dest_set, const VkWriteDescriptorSet *update, const uint32_t index,
John Zulaufbd9b3412019-08-22 17:16:11 -06003193 const char *func_name, std::string *error_code, std::string *error_msg) const {
John Zulaufc93c4252019-06-25 09:19:49 -06003194 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
Nathaniel Cesarioff521512020-12-11 16:00:26 -07003195 using Descriptor = cvdescriptorset::Descriptor;
John Zulaufc93c4252019-06-25 09:19:49 -06003196
John Zulaufadb3f542019-06-04 17:01:00 -06003197 switch (update->descriptorType) {
3198 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
3199 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
3200 // Validate image
3201 auto image_view = update->pImageInfo[di].imageView;
3202 auto image_layout = update->pImageInfo[di].imageLayout;
Mark Lobodzinski3ca937b2020-02-14 14:56:06 -07003203 auto sampler = update->pImageInfo[di].sampler;
sfricke-samsung27e5d5a2020-01-07 21:07:08 -08003204 auto iv_state = GetImageViewState(image_view);
Nathaniel Cesarioff521512020-12-11 16:00:26 -07003205 const ImageSamplerDescriptor *desc =
3206 (const ImageSamplerDescriptor *)dest_set->GetDescriptorFromGlobalIndex(index + di);
Jeff Bolz165818a2020-05-08 11:19:03 -05003207 if (image_view) {
3208 auto image_state = iv_state->image_state.get();
3209 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, func_name, error_code, error_msg)) {
3210 std::stringstream error_str;
3211 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
3212 << error_msg->c_str();
3213 *error_msg = error_str.str();
3214 return false;
3215 }
3216 if (device_extensions.vk_khr_sampler_ycbcr_conversion) {
3217 if (desc->IsImmutableSampler()) {
3218 auto sampler_state = GetSamplerState(desc->GetSampler());
3219 if (iv_state && sampler_state) {
3220 if (iv_state->samplerConversion != sampler_state->samplerConversion) {
3221 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01948";
3222 std::stringstream error_str;
3223 error_str
3224 << "Attempted write update to combined image sampler and image view and sampler ycbcr "
3225 "conversions are not identical, sampler: "
3226 << report_data->FormatHandle(desc->GetSampler())
3227 << " image view: " << report_data->FormatHandle(iv_state->image_view) << ".";
3228 *error_msg = error_str.str();
3229 return false;
3230 }
3231 }
3232 } else {
3233 if (iv_state && (iv_state->samplerConversion != VK_NULL_HANDLE)) {
3234 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02738";
John Zulaufadb3f542019-06-04 17:01:00 -06003235 std::stringstream error_str;
Jeff Bolz165818a2020-05-08 11:19:03 -05003236 error_str << "Because dstSet (" << report_data->FormatHandle(update->dstSet)
3237 << ") is bound to image view (" << report_data->FormatHandle(iv_state->image_view)
3238 << ") that includes a YCBCR conversion, it must have been allocated with a layout that "
3239 "includes an immutable sampler.";
John Zulaufadb3f542019-06-04 17:01:00 -06003240 *error_msg = error_str.str();
3241 return false;
3242 }
3243 }
Jeff Bolz165818a2020-05-08 11:19:03 -05003244 }
John Baumanda8abff2020-10-19 21:25:21 +00003245 // If there is an immutable sampler then |sampler| isn't used, so the following VU does not apply.
3246 if (sampler && !desc->IsImmutableSampler() && FormatIsMultiplane(image_state->createInfo.format)) {
Jeff Bolz165818a2020-05-08 11:19:03 -05003247 // multiplane formats must be created with mutable format bit
3248 if (0 == (image_state->createInfo.flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) {
3249 *error_code = "VUID-VkDescriptorImageInfo-sampler-01564";
John Zulaufadb3f542019-06-04 17:01:00 -06003250 std::stringstream error_str;
Jeff Bolz165818a2020-05-08 11:19:03 -05003251 error_str << "image " << report_data->FormatHandle(image_state->image)
3252 << " combined image sampler is a multi-planar "
3253 << "format and was not was not created with the VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT";
John Zulaufadb3f542019-06-04 17:01:00 -06003254 *error_msg = error_str.str();
3255 return false;
3256 }
Jeff Bolz165818a2020-05-08 11:19:03 -05003257 // image view need aspect mask for only the planes supported of format
3258 VkImageAspectFlags legal_aspect_flags = (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT);
3259 legal_aspect_flags |=
3260 (FormatPlaneCount(image_state->createInfo.format) == 3) ? VK_IMAGE_ASPECT_PLANE_2_BIT : 0;
3261 if (0 != (iv_state->create_info.subresourceRange.aspectMask & (~legal_aspect_flags))) {
3262 *error_code = "VUID-VkDescriptorImageInfo-sampler-01564";
3263 std::stringstream error_str;
3264 error_str << "image " << report_data->FormatHandle(image_state->image)
3265 << " combined image sampler is a multi-planar "
3266 << "format and " << report_data->FormatHandle(iv_state->image_view)
3267 << " aspectMask must only include " << string_VkImageAspectFlags(legal_aspect_flags);
3268 *error_msg = error_str.str();
3269 return false;
3270 }
sfricke-samsung27e5d5a2020-01-07 21:07:08 -08003271 }
Nathaniel Cesario23afadd2020-11-17 12:51:45 -07003272
3273 // Verify portability
3274 auto sampler_state = GetSamplerState(sampler);
3275 if (sampler_state) {
3276 if (ExtEnabled::kNotEnabled != device_extensions.vk_khr_portability_subset) {
3277 if ((VK_FALSE == enabled_features.portability_subset_features.mutableComparisonSamplers) &&
3278 (VK_FALSE != sampler_state->createInfo.compareEnable)) {
3279 LogError(device, "VUID-VkDescriptorImageInfo-mutableComparisonSamplers-04450",
3280 "%s (portability error): sampler comparison not available.", func_name);
3281 }
3282 }
3283 }
sfricke-samsung27e5d5a2020-01-07 21:07:08 -08003284 }
John Zulaufadb3f542019-06-04 17:01:00 -06003285 }
3286 }
Mark Lobodzinskiac727772020-01-08 10:47:30 -07003287 // Fall through
John Zulaufadb3f542019-06-04 17:01:00 -06003288 case VK_DESCRIPTOR_TYPE_SAMPLER: {
3289 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003290 const auto *desc = static_cast<const Descriptor *>(dest_set->GetDescriptorFromGlobalIndex(index + di));
John Zulaufadb3f542019-06-04 17:01:00 -06003291 if (!desc->IsImmutableSampler()) {
John Zulaufc93c4252019-06-25 09:19:49 -06003292 if (!ValidateSampler(update->pImageInfo[di].sampler)) {
John Zulaufadb3f542019-06-04 17:01:00 -06003293 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
3294 std::stringstream error_str;
3295 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
Mark Lobodzinskidb35b8b2020-04-09 08:46:59 -06003296 << report_data->FormatHandle(update->pImageInfo[di].sampler) << ".";
John Zulaufadb3f542019-06-04 17:01:00 -06003297 *error_msg = error_str.str();
3298 return false;
3299 }
Mark Lobodzinskiac727772020-01-08 10:47:30 -07003300 } else if (update->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) {
Mark Lobodzinskif4ed6c12020-01-03 11:21:58 -07003301 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02752";
3302 std::stringstream error_str;
3303 error_str << "Attempted write update to an immutable sampler descriptor.";
3304 *error_msg = error_str.str();
3305 return false;
John Zulaufadb3f542019-06-04 17:01:00 -06003306 }
3307 }
3308 break;
3309 }
3310 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
3311 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
3312 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
3313 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
3314 auto image_view = update->pImageInfo[di].imageView;
3315 auto image_layout = update->pImageInfo[di].imageLayout;
Jeff Bolz165818a2020-05-08 11:19:03 -05003316 if (image_view) {
3317 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, func_name, error_code, error_msg)) {
3318 std::stringstream error_str;
3319 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
3320 *error_msg = error_str.str();
3321 return false;
3322 }
John Zulaufadb3f542019-06-04 17:01:00 -06003323 }
3324 }
3325 break;
3326 }
3327 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
3328 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
3329 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
3330 auto buffer_view = update->pTexelBufferView[di];
Jeff Bolz165818a2020-05-08 11:19:03 -05003331 if (buffer_view) {
3332 auto bv_state = GetBufferViewState(buffer_view);
3333 if (!bv_state) {
3334 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02994";
3335 std::stringstream error_str;
3336 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: "
3337 << report_data->FormatHandle(buffer_view);
3338 *error_msg = error_str.str();
3339 return false;
3340 }
3341 auto buffer = bv_state->create_info.buffer;
3342 auto buffer_state = GetBufferState(buffer);
3343 // Verify that buffer underlying the view hasn't been destroyed prematurely
3344 if (!buffer_state) {
3345 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02994";
3346 std::stringstream error_str;
3347 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer ("
3348 << report_data->FormatHandle(buffer) << ") has been destroyed: " << error_msg->c_str();
3349 *error_msg = error_str.str();
3350 return false;
3351 } else if (!cvdescriptorset::ValidateBufferUsage(report_data, buffer_state, update->descriptorType, error_code,
3352 error_msg)) {
3353 std::stringstream error_str;
3354 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
3355 *error_msg = error_str.str();
3356 return false;
3357 }
John Zulaufadb3f542019-06-04 17:01:00 -06003358 }
3359 }
3360 break;
3361 }
3362 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
3363 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
3364 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
3365 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
3366 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Jeff Bolz165818a2020-05-08 11:19:03 -05003367 if (update->pBufferInfo[di].buffer) {
3368 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, func_name, error_code, error_msg)) {
3369 std::stringstream error_str;
3370 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
3371 *error_msg = error_str.str();
3372 return false;
3373 }
John Zulaufadb3f542019-06-04 17:01:00 -06003374 }
3375 }
3376 break;
3377 }
3378 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
3379 break;
Jeff Bolz95176d02020-04-01 00:36:16 -05003380 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV: {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07003381 const auto *acc_info = LvlFindInChain<VkWriteDescriptorSetAccelerationStructureNV>(update->pNext);
Jeff Bolz95176d02020-04-01 00:36:16 -05003382 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003383 if (!ValidateAccelerationStructureUpdate(GetAccelerationStructureStateNV(acc_info->pAccelerationStructures[di]),
Mark Lobodzinski85ebd402020-12-03 12:56:07 -07003384 func_name, error_code, error_msg)) {
Jeff Bolz95176d02020-04-01 00:36:16 -05003385 std::stringstream error_str;
3386 error_str << "Attempted write update to acceleration structure descriptor failed due to: "
3387 << error_msg->c_str();
3388 *error_msg = error_str.str();
3389 return false;
3390 }
3391 }
3392
3393 } break;
sourav parmarcd5fb182020-07-17 12:58:44 -07003394 // KHR acceleration structures don't require memory to be bound manually to them.
3395 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR:
3396 break;
John Zulaufadb3f542019-06-04 17:01:00 -06003397 default:
3398 assert(0); // We've already verified update type so should never get here
3399 break;
3400 }
3401 // All checks passed so update contents are good
3402 return true;
3403}