blob: 0c2715bab7ffe6539970a77b04df4f8c05a34772 [file] [log] [blame]
John Zulauff4c07882019-01-24 14:03:36 -07001/* Copyright (c) 2015-2019 The Khronos Group Inc.
2 * Copyright (c) 2015-2019 Valve Corporation
3 * Copyright (c) 2015-2019 LunarG, Inc.
4 * Copyright (C) 2015-2019 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>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060020 */
21
Tobin Ehlisf922ef82016-11-30 10:19:14 -070022// Allow use of STL min and max functions in Windows
23#define NOMINMAX
24
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070025#include "chassis.h"
Mark Lobodzinski76d76662019-02-14 14:38:21 -070026#include "core_validation_error_enums.h"
27#include "core_validation.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060028#include "descriptor_sets.h"
John Zulaufd47d0612018-02-16 13:00:34 -070029#include "hash_vk_types.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060030#include "vk_enum_string_helper.h"
31#include "vk_safe_struct.h"
Jeff Bolzfdf96072018-04-10 14:32:18 -050032#include "vk_typemap_helper.h"
Tobin Ehlisc8266452017-04-07 12:20:30 -060033#include "buffer_validation.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060034#include <sstream>
Mark Lobodzinski2eee5d82016-12-02 15:33:18 -070035#include <algorithm>
John Zulauff4c07882019-01-24 14:03:36 -070036#include <array>
John Zulauf1f8174b2018-02-16 12:58:37 -070037#include <memory>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060038
Jeff Bolzfdf96072018-04-10 14:32:18 -050039// ExtendedBinding collects a VkDescriptorSetLayoutBinding and any extended
40// state that comes from a different array/structure so they can stay together
41// while being sorted by binding number.
42struct ExtendedBinding {
43 ExtendedBinding(const VkDescriptorSetLayoutBinding *l, VkDescriptorBindingFlagsEXT f) : layout_binding(l), binding_flags(f) {}
44
45 const VkDescriptorSetLayoutBinding *layout_binding;
46 VkDescriptorBindingFlagsEXT binding_flags;
47};
48
John Zulauf508d13a2018-01-05 15:10:34 -070049struct BindingNumCmp {
Jeff Bolzfdf96072018-04-10 14:32:18 -050050 bool operator()(const ExtendedBinding &a, const ExtendedBinding &b) const {
51 return a.layout_binding->binding < b.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070052 }
53};
54
John Zulauf613fd982019-06-04 15:14:41 -060055using DescriptorSet = cvdescriptorset::DescriptorSet;
John Zulauf4a015c92019-06-04 09:50:05 -060056using DescriptorSetLayout = cvdescriptorset::DescriptorSetLayout;
John Zulaufd47d0612018-02-16 13:00:34 -070057using DescriptorSetLayoutDef = cvdescriptorset::DescriptorSetLayoutDef;
58using DescriptorSetLayoutId = cvdescriptorset::DescriptorSetLayoutId;
59
John Zulauf34ebf272018-02-16 13:08:47 -070060// Canonical dictionary of DescriptorSetLayoutDef (without any handle/device specific information)
61cvdescriptorset::DescriptorSetLayoutDict descriptor_set_layout_dict;
John Zulaufd47d0612018-02-16 13:00:34 -070062
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060063DescriptorSetLayoutId GetCanonicalId(const VkDescriptorSetLayoutCreateInfo *p_create_info) {
John Zulauf34ebf272018-02-16 13:08:47 -070064 return descriptor_set_layout_dict.look_up(DescriptorSetLayoutDef(p_create_info));
John Zulaufd47d0612018-02-16 13:00:34 -070065}
John Zulauf34ebf272018-02-16 13:08:47 -070066
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060067// Construct DescriptorSetLayout instance from given create info
John Zulauf48a6a702017-12-22 17:14:54 -070068// Proactively reserve and resize as possible, as the reallocation was visible in profiling
John Zulauf1f8174b2018-02-16 12:58:37 -070069cvdescriptorset::DescriptorSetLayoutDef::DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info)
70 : flags_(p_create_info->flags), binding_count_(0), descriptor_count_(0), dynamic_descriptor_count_(0) {
Jeff Bolzfdf96072018-04-10 14:32:18 -050071 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(p_create_info->pNext);
72
John Zulauf48a6a702017-12-22 17:14:54 -070073 binding_type_stats_ = {0, 0, 0};
Jeff Bolzfdf96072018-04-10 14:32:18 -050074 std::set<ExtendedBinding, BindingNumCmp> sorted_bindings;
John Zulauf508d13a2018-01-05 15:10:34 -070075 const uint32_t input_bindings_count = p_create_info->bindingCount;
76 // Sort the input bindings in binding number order, eliminating duplicates
77 for (uint32_t i = 0; i < input_bindings_count; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -050078 VkDescriptorBindingFlagsEXT flags = 0;
79 if (flags_create_info && flags_create_info->bindingCount == p_create_info->bindingCount) {
80 flags = flags_create_info->pBindingFlags[i];
81 }
82 sorted_bindings.insert(ExtendedBinding(p_create_info->pBindings + i, flags));
John Zulaufb6d71202017-12-22 16:47:09 -070083 }
84
85 // Store the create info in the sorted order from above
Tobin Ehlisa3525e02016-11-17 10:50:52 -070086 std::map<uint32_t, uint32_t> binding_to_dyn_count;
John Zulauf508d13a2018-01-05 15:10:34 -070087 uint32_t index = 0;
88 binding_count_ = static_cast<uint32_t>(sorted_bindings.size());
89 bindings_.reserve(binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -050090 binding_flags_.reserve(binding_count_);
John Zulauf508d13a2018-01-05 15:10:34 -070091 binding_to_index_map_.reserve(binding_count_);
92 for (auto input_binding : sorted_bindings) {
93 // Add to binding and map, s.t. it is robust to invalid duplication of binding_num
Jeff Bolzfdf96072018-04-10 14:32:18 -050094 const auto binding_num = input_binding.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070095 binding_to_index_map_[binding_num] = index++;
Jeff Bolzfdf96072018-04-10 14:32:18 -050096 bindings_.emplace_back(input_binding.layout_binding);
John Zulauf508d13a2018-01-05 15:10:34 -070097 auto &binding_info = bindings_.back();
Jeff Bolzfdf96072018-04-10 14:32:18 -050098 binding_flags_.emplace_back(input_binding.binding_flags);
John Zulauf508d13a2018-01-05 15:10:34 -070099
John Zulaufb6d71202017-12-22 16:47:09 -0700100 descriptor_count_ += binding_info.descriptorCount;
101 if (binding_info.descriptorCount > 0) {
102 non_empty_bindings_.insert(binding_num);
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700103 }
John Zulaufb6d71202017-12-22 16:47:09 -0700104
105 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
106 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
107 binding_to_dyn_count[binding_num] = binding_info.descriptorCount;
108 dynamic_descriptor_count_ += binding_info.descriptorCount;
John Zulauf48a6a702017-12-22 17:14:54 -0700109 binding_type_stats_.dynamic_buffer_count++;
110 } else if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
111 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
112 binding_type_stats_.non_dynamic_buffer_count++;
113 } else {
114 binding_type_stats_.image_sampler_count++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600115 }
116 }
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700117 assert(bindings_.size() == binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500118 assert(binding_flags_.size() == binding_count_);
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700119 uint32_t global_index = 0;
John Zulauf7705bfc2019-06-10 09:52:04 -0600120 global_index_range_.reserve(binding_count_);
121 // Vector order is finalized so build vectors of descriptors and dynamic offsets by binding index
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700122 for (uint32_t i = 0; i < binding_count_; ++i) {
John Zulaufc483f442017-12-15 14:02:06 -0700123 auto final_index = global_index + bindings_[i].descriptorCount;
John Zulauf7705bfc2019-06-10 09:52:04 -0600124 global_index_range_.emplace_back(global_index, final_index);
John Zulaufc483f442017-12-15 14:02:06 -0700125 global_index = final_index;
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700126 }
John Zulaufb6d71202017-12-22 16:47:09 -0700127
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700128 // Now create dyn offset array mapping for any dynamic descriptors
129 uint32_t dyn_array_idx = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700130 binding_to_dynamic_array_idx_map_.reserve(binding_to_dyn_count.size());
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700131 for (const auto &bc_pair : binding_to_dyn_count) {
132 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
133 dyn_array_idx += bc_pair.second;
134 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600135}
Tobin Ehlis154c2692016-10-25 09:36:53 -0600136
John Zulaufd47d0612018-02-16 13:00:34 -0700137size_t cvdescriptorset::DescriptorSetLayoutDef::hash() const {
138 hash_util::HashCombiner hc;
139 hc << flags_;
140 hc.Combine(bindings_);
John Zulauf223b69d2018-11-09 16:00:59 -0700141 hc.Combine(binding_flags_);
John Zulaufd47d0612018-02-16 13:00:34 -0700142 return hc.Value();
143}
144//
145
John Zulauf1f8174b2018-02-16 12:58:37 -0700146// Return valid index or "end" i.e. binding_count_;
147// The asserts in "Get" are reduced to the set where no valid answer(like null or 0) could be given
148// Common code for all binding lookups.
149uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromBinding(uint32_t binding) const {
150 const auto &bi_itr = binding_to_index_map_.find(binding);
151 if (bi_itr != binding_to_index_map_.cend()) return bi_itr->second;
152 return GetBindingCount();
153}
154VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorSetLayoutBindingPtrFromIndex(
155 const uint32_t index) const {
156 if (index >= bindings_.size()) return nullptr;
157 return bindings_[index].ptr();
158}
159// Return descriptorCount for given index, 0 if index is unavailable
160uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorCountFromIndex(const uint32_t index) const {
161 if (index >= bindings_.size()) return 0;
162 return bindings_[index].descriptorCount;
163}
164// For the given index, return descriptorType
165VkDescriptorType cvdescriptorset::DescriptorSetLayoutDef::GetTypeFromIndex(const uint32_t index) const {
166 assert(index < bindings_.size());
167 if (index < bindings_.size()) return bindings_[index].descriptorType;
168 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
169}
170// For the given index, return stageFlags
171VkShaderStageFlags cvdescriptorset::DescriptorSetLayoutDef::GetStageFlagsFromIndex(const uint32_t index) const {
172 assert(index < bindings_.size());
173 if (index < bindings_.size()) return bindings_[index].stageFlags;
174 return VkShaderStageFlags(0);
175}
Jeff Bolzfdf96072018-04-10 14:32:18 -0500176// Return binding flags for given index, 0 if index is unavailable
177VkDescriptorBindingFlagsEXT cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorBindingFlagsFromIndex(
178 const uint32_t index) const {
179 if (index >= binding_flags_.size()) return 0;
180 return binding_flags_[index];
181}
John Zulauf1f8174b2018-02-16 12:58:37 -0700182
John Zulauf7705bfc2019-06-10 09:52:04 -0600183const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromIndex(uint32_t index) const {
184 const static IndexRange kInvalidRange = {0xFFFFFFFF, 0xFFFFFFFF};
185 if (index >= binding_flags_.size()) return kInvalidRange;
186 return global_index_range_[index];
John Zulauf1f8174b2018-02-16 12:58:37 -0700187}
188
John Zulauf7705bfc2019-06-10 09:52:04 -0600189// For the given binding, return the global index range (half open)
190// As start and end are often needed in pairs, get both with a single lookup.
John Zulauf1f8174b2018-02-16 12:58:37 -0700191const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromBinding(
192 const uint32_t binding) const {
John Zulauf7705bfc2019-06-10 09:52:04 -0600193 uint32_t index = GetIndexFromBinding(binding);
194 return GetGlobalIndexRangeFromIndex(index);
John Zulauf1f8174b2018-02-16 12:58:37 -0700195}
196
197// For given binding, return ptr to ImmutableSampler array
198VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
199 const auto &bi_itr = binding_to_index_map_.find(binding);
200 if (bi_itr != binding_to_index_map_.end()) {
201 return bindings_[bi_itr->second].pImmutableSamplers;
202 }
203 return nullptr;
204}
205// Move to next valid binding having a non-zero binding count
206uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetNextValidBinding(const uint32_t binding) const {
207 auto it = non_empty_bindings_.upper_bound(binding);
208 assert(it != non_empty_bindings_.cend());
209 if (it != non_empty_bindings_.cend()) return *it;
210 return GetMaxBinding() + 1;
211}
212// For given index, return ptr to ImmutableSampler array
213VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
214 if (index < bindings_.size()) {
215 return bindings_[index].pImmutableSamplers;
216 }
217 return nullptr;
218}
John Zulauf9ce3b252019-06-06 15:20:22 -0600219
220// If our layout is compatible with rh_ds_layout, return true.
221bool cvdescriptorset::DescriptorSetLayout::IsCompatible(DescriptorSetLayout const *rh_ds_layout) const {
222 bool compatible = (this == rh_ds_layout) || (GetLayoutDef() == rh_ds_layout->GetLayoutDef());
223 return compatible;
224}
John Zulauf1f8174b2018-02-16 12:58:37 -0700225
John Zulauff43695f2019-09-13 17:56:26 -0600226// TODO: Find a way to add smarts to the autogenerated version of this
227static std::string smart_string_VkShaderStageFlags(VkShaderStageFlags stage_flags) {
228 if (stage_flags == VK_SHADER_STAGE_ALL) {
229 return string_VkShaderStageFlagBits(VK_SHADER_STAGE_ALL);
230 }
231
232 return string_VkShaderStageFlags(stage_flags);
233}
234
235// If our layout is compatible with bound_dsl, return true,
236// else return false and fill in error_msg will description of what causes incompatibility
237bool cvdescriptorset::VerifySetLayoutCompatibility(const debug_report_data *report_data, DescriptorSetLayout const *layout_dsl,
238 DescriptorSetLayout const *bound_dsl, std::string *error_msg) {
239 // Short circuit the detailed check.
240 if (layout_dsl->IsCompatible(bound_dsl)) return true;
241
242 // 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 -0600243 // Should only be run if trivial accept has failed, and in that context should return false.
John Zulauff43695f2019-09-13 17:56:26 -0600244 VkDescriptorSetLayout layout_dsl_handle = layout_dsl->GetDescriptorSetLayout();
245 VkDescriptorSetLayout bound_dsl_handle = bound_dsl->GetDescriptorSetLayout();
246 DescriptorSetLayoutDef const *layout_ds_layout_def = layout_dsl->GetLayoutDef();
247 DescriptorSetLayoutDef const *bound_ds_layout_def = bound_dsl->GetLayoutDef();
John Zulauf9ce3b252019-06-06 15:20:22 -0600248
249 // Check descriptor counts
John Zulauff43695f2019-09-13 17:56:26 -0600250 const auto bound_total_count = bound_ds_layout_def->GetTotalDescriptorCount();
251 if (layout_ds_layout_def->GetTotalDescriptorCount() != bound_ds_layout_def->GetTotalDescriptorCount()) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700252 std::stringstream error_str;
John Zulauff43695f2019-09-13 17:56:26 -0600253 error_str << report_data->FormatHandle(layout_dsl_handle) << " from pipeline layout has "
254 << layout_ds_layout_def->GetTotalDescriptorCount() << " total descriptors, but "
255 << report_data->FormatHandle(bound_dsl_handle) << ", which is bound, has " << bound_total_count
256 << " total descriptors.";
John Zulauf1f8174b2018-02-16 12:58:37 -0700257 *error_msg = error_str.str();
258 return false; // trivial fail case
259 }
John Zulaufd47d0612018-02-16 13:00:34 -0700260
John Zulauf1f8174b2018-02-16 12:58:37 -0700261 // Descriptor counts match so need to go through bindings one-by-one
262 // and verify that type and stageFlags match
John Zulauff43695f2019-09-13 17:56:26 -0600263 for (const auto &layout_binding : layout_ds_layout_def->GetBindings()) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700264 // TODO : Do we also need to check immutable samplers?
John Zulauff43695f2019-09-13 17:56:26 -0600265 const auto bound_binding = bound_ds_layout_def->GetBindingInfoFromBinding(layout_binding.binding);
266 if (layout_binding.descriptorCount != bound_binding->descriptorCount) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700267 std::stringstream error_str;
John Zulauff43695f2019-09-13 17:56:26 -0600268 error_str << "Binding " << layout_binding.binding << " for " << report_data->FormatHandle(layout_dsl_handle)
269 << " from pipeline layout has a descriptorCount of " << layout_binding.descriptorCount << " but binding "
270 << layout_binding.binding << " for " << report_data->FormatHandle(bound_dsl_handle)
271 << ", which is bound, has a descriptorCount of " << bound_binding->descriptorCount;
John Zulauf1f8174b2018-02-16 12:58:37 -0700272 *error_msg = error_str.str();
273 return false;
John Zulauff43695f2019-09-13 17:56:26 -0600274 } else if (layout_binding.descriptorType != bound_binding->descriptorType) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700275 std::stringstream error_str;
John Zulauff43695f2019-09-13 17:56:26 -0600276 error_str << "Binding " << layout_binding.binding << " for " << report_data->FormatHandle(layout_dsl_handle)
277 << " from pipeline layout is type '" << string_VkDescriptorType(layout_binding.descriptorType)
278 << "' but binding " << layout_binding.binding << " for " << report_data->FormatHandle(bound_dsl_handle)
279 << ", which is bound, is type '" << string_VkDescriptorType(bound_binding->descriptorType) << "'";
John Zulauf1f8174b2018-02-16 12:58:37 -0700280 *error_msg = error_str.str();
281 return false;
John Zulauff43695f2019-09-13 17:56:26 -0600282 } else if (layout_binding.stageFlags != bound_binding->stageFlags) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700283 std::stringstream error_str;
John Zulauff43695f2019-09-13 17:56:26 -0600284 error_str << "Binding " << layout_binding.binding << " for " << report_data->FormatHandle(layout_dsl_handle)
285 << " from pipeline layout has stageFlags " << smart_string_VkShaderStageFlags(layout_binding.stageFlags)
286 << " but binding " << layout_binding.binding << " for " << report_data->FormatHandle(bound_dsl_handle)
287 << ", which is bound, has stageFlags " << smart_string_VkShaderStageFlags(bound_binding->stageFlags);
John Zulauf1f8174b2018-02-16 12:58:37 -0700288 *error_msg = error_str.str();
289 return false;
290 }
291 }
Tony-LunarG692b8b42019-09-30 16:07:26 -0600292
293 const auto &ds_layout_flags = layout_ds_layout_def->GetBindingFlags();
294 const auto &bound_layout_flags = bound_ds_layout_def->GetBindingFlags();
295 if (bound_layout_flags != ds_layout_flags) {
296 std::stringstream error_str;
297 assert(ds_layout_flags.size() == bound_layout_flags.size());
298 size_t i;
299 for (i = 0; i < ds_layout_flags.size(); i++) {
300 if (ds_layout_flags[i] != bound_layout_flags[i]) break;
301 }
302 error_str << report_data->FormatHandle(layout_dsl_handle)
303 << " from pipeline layout does not have the same binding flags at binding " << i << " ( "
304 << string_VkDescriptorBindingFlagsEXT(ds_layout_flags[i]) << " ) as "
305 << report_data->FormatHandle(bound_dsl_handle) << " ( "
306 << string_VkDescriptorBindingFlagsEXT(bound_layout_flags[i]) << " ), which is bound";
307 *error_msg = error_str.str();
308 return false;
309 }
310
John Zulauf9ce3b252019-06-06 15:20:22 -0600311 // No detailed check should succeed if the trivial check failed -- or the dictionary has failed somehow.
312 bool compatible = true;
313 assert(!compatible);
314 return compatible;
John Zulauf1f8174b2018-02-16 12:58:37 -0700315}
316
317bool cvdescriptorset::DescriptorSetLayoutDef::IsNextBindingConsistent(const uint32_t binding) const {
318 if (!binding_to_index_map_.count(binding + 1)) return false;
319 auto const &bi_itr = binding_to_index_map_.find(binding);
320 if (bi_itr != binding_to_index_map_.end()) {
321 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
322 if (next_bi_itr != binding_to_index_map_.end()) {
323 auto type = bindings_[bi_itr->second].descriptorType;
324 auto stage_flags = bindings_[bi_itr->second].stageFlags;
325 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500326 auto flags = binding_flags_[bi_itr->second];
John Zulauf1f8174b2018-02-16 12:58:37 -0700327 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
328 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
Jeff Bolzfdf96072018-04-10 14:32:18 -0500329 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false)) ||
330 (flags != binding_flags_[next_bi_itr->second])) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700331 return false;
332 }
333 return true;
334 }
335 }
336 return false;
337}
John Zulauf1f8174b2018-02-16 12:58:37 -0700338
339// The DescriptorSetLayout stores the per handle data for a descriptor set layout, and references the common defintion for the
340// handle invariant portion
341cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
342 const VkDescriptorSetLayout layout)
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600343 : layout_(layout), layout_destroyed_(false), layout_id_(GetCanonicalId(p_create_info)) {}
John Zulauf1f8174b2018-02-16 12:58:37 -0700344
Tobin Ehlis154c2692016-10-25 09:36:53 -0600345// Validate descriptor set layout create info
John Zulaufd9435c32019-06-05 15:55:36 -0600346bool cvdescriptorset::ValidateDescriptorSetLayoutCreateInfo(
Jeff Bolzfdf96072018-04-10 14:32:18 -0500347 const debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *create_info, const bool push_descriptor_ext,
348 const uint32_t max_push_descriptors, const bool descriptor_indexing_ext,
Jeff Bolze54ae892018-09-08 12:16:29 -0500349 const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features,
350 const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features,
Tony-LunarGd6744bc2019-08-23 09:57:10 -0600351 const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props, const DeviceExtensions *device_extensions) {
Tobin Ehlis154c2692016-10-25 09:36:53 -0600352 bool skip = false;
353 std::unordered_set<uint32_t> bindings;
John Zulauf0fdeab32018-01-23 11:27:35 -0700354 uint64_t total_descriptors = 0;
355
Jeff Bolzfdf96072018-04-10 14:32:18 -0500356 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(create_info->pNext);
357
358 const bool push_descriptor_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR);
John Zulauf0fdeab32018-01-23 11:27:35 -0700359 if (push_descriptor_set && !push_descriptor_ext) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600360 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -0600361 kVUID_Core_DrawState_ExtensionNotEnabled,
Mark Lobodzinskifb5a3e62018-04-13 10:46:48 -0600362 "Attempted to use %s in %s but its required extension %s has not been enabled.\n",
John Zulauf0fdeab32018-01-23 11:27:35 -0700363 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR", "VkDescriptorSetLayoutCreateInfo::flags",
364 VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
365 }
366
Jeff Bolzfdf96072018-04-10 14:32:18 -0500367 const bool update_after_bind_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT);
368 if (update_after_bind_set && !descriptor_indexing_ext) {
369 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -0600370 kVUID_Core_DrawState_ExtensionNotEnabled,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500371 "Attemped to use %s in %s but its required extension %s has not been enabled.\n",
372 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT", "VkDescriptorSetLayoutCreateInfo::flags",
373 VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
374 }
375
John Zulauf0fdeab32018-01-23 11:27:35 -0700376 auto valid_type = [push_descriptor_set](const VkDescriptorType type) {
377 return !push_descriptor_set ||
Dave Houlton142c4cb2018-10-17 15:04:41 -0600378 ((type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && (type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) &&
Jeff Bolze54ae892018-09-08 12:16:29 -0500379 (type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT));
John Zulauf0fdeab32018-01-23 11:27:35 -0700380 };
381
Jeff Bolzfdf96072018-04-10 14:32:18 -0500382 uint32_t max_binding = 0;
383
Tobin Ehlis154c2692016-10-25 09:36:53 -0600384 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
John Zulauf0fdeab32018-01-23 11:27:35 -0700385 const auto &binding_info = create_info->pBindings[i];
Jeff Bolzfdf96072018-04-10 14:32:18 -0500386 max_binding = std::max(max_binding, binding_info.binding);
387
John Zulauf0fdeab32018-01-23 11:27:35 -0700388 if (!bindings.insert(binding_info.binding).second) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600389 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houltond8ed0212018-05-16 17:18:24 -0600390 "VUID-VkDescriptorSetLayoutCreateInfo-binding-00279",
391 "duplicated binding number in VkDescriptorSetLayoutBinding.");
Tobin Ehlis154c2692016-10-25 09:36:53 -0600392 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700393 if (!valid_type(binding_info.descriptorType)) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600394 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton142c4cb2018-10-17 15:04:41 -0600395 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
396 ? "VUID-VkDescriptorSetLayoutCreateInfo-flags-02208"
397 : "VUID-VkDescriptorSetLayoutCreateInfo-flags-00280",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600398 "invalid type %s ,for push descriptors in VkDescriptorSetLayoutBinding entry %" PRIu32 ".",
399 string_VkDescriptorType(binding_info.descriptorType), i);
John Zulauf0fdeab32018-01-23 11:27:35 -0700400 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500401
402 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
Tony-LunarGd6744bc2019-08-23 09:57:10 -0600403 if (!device_extensions->vk_ext_inline_uniform_block) {
404 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0,
405 "UNASSIGNED-Extension not enabled",
406 "Creating VkDescriptorSetLayout with descriptor type VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT "
407 "but extension %s is missing",
408 VK_EXT_INLINE_UNIFORM_BLOCK_EXTENSION_NAME);
409 } else {
410 if ((binding_info.descriptorCount % 4) != 0) {
411 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
412 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02209",
413 "descriptorCount =(%" PRIu32 ") must be a multiple of 4", binding_info.descriptorCount);
414 }
415 if (binding_info.descriptorCount > inline_uniform_block_props->maxInlineUniformBlockSize) {
416 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
417 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02210",
418 "descriptorCount =(%" PRIu32 ") must be less than or equal to maxInlineUniformBlockSize",
419 binding_info.descriptorCount);
420 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500421 }
422 }
423
John Zulauf0fdeab32018-01-23 11:27:35 -0700424 total_descriptors += binding_info.descriptorCount;
Tobin Ehlis154c2692016-10-25 09:36:53 -0600425 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700426
Jeff Bolzfdf96072018-04-10 14:32:18 -0500427 if (flags_create_info) {
428 if (flags_create_info->bindingCount != 0 && flags_create_info->bindingCount != create_info->bindingCount) {
429 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houltond8ed0212018-05-16 17:18:24 -0600430 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-bindingCount-03002",
Jeff Bolzfdf96072018-04-10 14:32:18 -0500431 "VkDescriptorSetLayoutCreateInfo::bindingCount (%d) != "
432 "VkDescriptorSetLayoutBindingFlagsCreateInfoEXT::bindingCount (%d)",
433 create_info->bindingCount, flags_create_info->bindingCount);
434 }
435
436 if (flags_create_info->bindingCount == create_info->bindingCount) {
437 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
438 const auto &binding_info = create_info->pBindings[i];
439
440 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) {
441 if (!update_after_bind_set) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600442 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
443 "VUID-VkDescriptorSetLayoutCreateInfo-flags-03000",
444 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500445 }
446
447 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER &&
448 !descriptor_indexing_features->descriptorBindingUniformBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600449 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
450 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
451 "descriptorBindingUniformBufferUpdateAfterBind-03005",
452 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500453 }
454 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
455 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
456 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) &&
457 !descriptor_indexing_features->descriptorBindingSampledImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600458 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
459 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
460 "descriptorBindingSampledImageUpdateAfterBind-03006",
461 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500462 }
463 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE &&
464 !descriptor_indexing_features->descriptorBindingStorageImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600465 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
466 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
467 "descriptorBindingStorageImageUpdateAfterBind-03007",
468 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500469 }
470 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER &&
471 !descriptor_indexing_features->descriptorBindingStorageBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600472 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
473 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
474 "descriptorBindingStorageBufferUpdateAfterBind-03008",
475 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500476 }
477 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
478 !descriptor_indexing_features->descriptorBindingUniformTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600479 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
480 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
481 "descriptorBindingUniformTexelBufferUpdateAfterBind-03009",
482 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500483 }
484 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER &&
485 !descriptor_indexing_features->descriptorBindingStorageTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600486 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
487 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
488 "descriptorBindingStorageTexelBufferUpdateAfterBind-03010",
489 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500490 }
491 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT ||
492 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
493 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600494 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
495 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-None-03011",
496 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500497 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500498
499 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT &&
500 !inline_uniform_block_features->descriptorBindingInlineUniformBlockUpdateAfterBind) {
501 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton142c4cb2018-10-17 15:04:41 -0600502 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
503 "descriptorBindingInlineUniformBlockUpdateAfterBind-02211",
504 "Invalid flags (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) for "
505 "VkDescriptorSetLayoutBinding entry %" PRIu32
506 " with descriptorBindingInlineUniformBlockUpdateAfterBind not enabled",
507 i);
Jeff Bolze54ae892018-09-08 12:16:29 -0500508 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500509 }
510
511 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT) {
512 if (!descriptor_indexing_features->descriptorBindingUpdateUnusedWhilePending) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600513 skip |= log_msg(
514 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
515 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingUpdateUnusedWhilePending-03012",
516 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500517 }
518 }
519
520 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) {
521 if (!descriptor_indexing_features->descriptorBindingPartiallyBound) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600522 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
523 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingPartiallyBound-03013",
524 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500525 }
526 }
527
528 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT) {
529 if (binding_info.binding != max_binding) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600530 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
531 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03004",
532 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500533 }
534
535 if (!descriptor_indexing_features->descriptorBindingVariableDescriptorCount) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600536 skip |= log_msg(
537 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
538 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingVariableDescriptorCount-03014",
539 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500540 }
541 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
542 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600543 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
544 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03015",
545 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500546 }
547 }
548
549 if (push_descriptor_set &&
550 (flags_create_info->pBindingFlags[i] &
551 (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
552 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT))) {
553 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houltond8ed0212018-05-16 17:18:24 -0600554 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-flags-03003",
555 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500556 }
557 }
558 }
559 }
560
John Zulauf0fdeab32018-01-23 11:27:35 -0700561 if ((push_descriptor_set) && (total_descriptors > max_push_descriptors)) {
562 const char *undefined = push_descriptor_ext ? "" : " -- undefined";
Dave Houltond8ed0212018-05-16 17:18:24 -0600563 skip |=
564 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
565 "VUID-VkDescriptorSetLayoutCreateInfo-flags-00281",
566 "for push descriptor, total descriptor count in layout (%" PRIu64
567 ") must not be greater than VkPhysicalDevicePushDescriptorPropertiesKHR::maxPushDescriptors (%" PRIu32 "%s).",
568 total_descriptors, max_push_descriptors, undefined);
John Zulauf0fdeab32018-01-23 11:27:35 -0700569 }
570
Tobin Ehlis154c2692016-10-25 09:36:53 -0600571 return skip;
572}
573
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600574cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
575 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
576
Tobin Ehlis93f22372016-10-12 14:34:12 -0600577cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500578 const std::shared_ptr<DescriptorSetLayout const> &layout, uint32_t variable_count,
John Zulaufc93c4252019-06-25 09:19:49 -0600579 cvdescriptorset::DescriptorSet::StateTracker *state_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700580 : some_update_(false),
581 set_(set),
582 pool_state_(nullptr),
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600583 p_layout_(layout),
John Zulaufc93c4252019-06-25 09:19:49 -0600584 state_data_(state_data),
Jeff Bolzdd4cfa12019-08-11 20:57:51 -0500585 variable_count_(variable_count),
586 change_count_(0) {
John Zulaufc93c4252019-06-25 09:19:49 -0600587 pool_state_ = state_data->GetDescriptorPoolState(pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600588 // Foreach binding, create default descriptors of given type
John Zulaufb6d71202017-12-22 16:47:09 -0700589 descriptors_.reserve(p_layout_->GetTotalDescriptorCount());
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600590 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
591 auto type = p_layout_->GetTypeFromIndex(i);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600592 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700593 case VK_DESCRIPTOR_TYPE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600594 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
595 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600596 if (immut_sampler) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700597 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600598 some_update_ = true; // Immutable samplers are updated at creation
599 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700600 descriptors_.emplace_back(new SamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700601 }
602 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600603 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700604 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600605 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
606 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600607 if (immut) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700608 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600609 some_update_ = true; // Immutable samplers are updated at creation
610 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700611 descriptors_.emplace_back(new ImageSamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700612 }
613 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600614 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700615 // ImageDescriptors
616 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
617 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
618 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600619 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700620 descriptors_.emplace_back(new ImageDescriptor(type));
621 break;
622 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
623 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600624 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700625 descriptors_.emplace_back(new TexelDescriptor(type));
626 break;
627 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
628 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
629 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
630 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600631 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700632 descriptors_.emplace_back(new BufferDescriptor(type));
633 break;
Jeff Bolze54ae892018-09-08 12:16:29 -0500634 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
635 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
636 descriptors_.emplace_back(new InlineUniformDescriptor(type));
637 break;
Eric Werness30127fd2018-10-31 21:01:03 -0700638 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -0500639 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
640 descriptors_.emplace_back(new AccelerationStructureDescriptor(type));
641 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700642 default:
643 assert(0); // Bad descriptor type specified
644 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600645 }
646 }
647}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600648
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700649cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200650
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600651static std::string StringDescriptorReqViewType(descriptor_req req) {
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700652 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200653 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
654 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700655 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700656 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200657 }
658 }
659
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700660 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700661
662 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200663}
664
Chris Forbesda01e8d2018-08-27 15:36:57 -0700665static char const *StringDescriptorReqComponentType(descriptor_req req) {
666 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_SINT) return "SINT";
667 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_UINT) return "UINT";
668 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT) return "FLOAT";
669 return "(none)";
670}
671
Jeff Bolz6cede832019-08-09 23:30:39 -0500672unsigned DescriptorRequirementsBitsFromFormat(VkFormat fmt) {
Chris Forbesda01e8d2018-08-27 15:36:57 -0700673 if (FormatIsSInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
674 if (FormatIsUInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
675 if (FormatIsDepthAndStencil(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT | DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
676 if (fmt == VK_FORMAT_UNDEFINED) return 0;
677 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
678 return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
679}
680
Tobin Ehlis3066db62016-08-22 08:12:23 -0600681// 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 -0600682// This includes validating that all descriptors in the given bindings are updated,
683// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
684// Return true if state is acceptable, or false and write an error message into error string
John Zulaufc93c4252019-06-25 09:19:49 -0600685bool CoreChecks::ValidateDrawState(const DescriptorSet *descriptor_set, const std::map<uint32_t, descriptor_req> &bindings,
John Zulauffbf3c202019-07-17 14:57:14 -0600686 const std::vector<uint32_t> &dynamic_offsets, const CMD_BUFFER_STATE *cb_node,
687 const char *caller, std::string *error) const {
John Zulaufc93c4252019-06-25 09:19:49 -0600688 using DescriptorClass = cvdescriptorset::DescriptorClass;
689 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
690 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
691 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
692 using SamplerDescriptor = cvdescriptorset::SamplerDescriptor;
693 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
Chris Forbesc7090a82016-07-25 18:10:41 +1200694 for (auto binding_pair : bindings) {
695 auto binding = binding_pair.first;
John Zulauf382e1912019-06-10 15:27:44 -0600696 DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(), binding);
697 if (binding_it.AtEnd()) { // End at construction is the condition for an invalid binding.
Tobin Ehlis58c59582016-06-21 12:34:33 -0600698 std::stringstream error_str;
699 error_str << "Attempting to validate DrawState for binding #" << binding
700 << " which is an invalid binding for this descriptor set.";
701 *error = error_str.str();
702 return false;
703 }
Jeff Bolz6cede832019-08-09 23:30:39 -0500704
705 if (binding_it.GetDescriptorBindingFlags() &
706 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) {
707 // Can't validate the descriptor because it may not have been updated,
708 // or the view could have been destroyed
709 continue;
710 }
711
John Zulauf382e1912019-06-10 15:27:44 -0600712 // Copy the range, the end range is subject to update based on variable length descriptor arrays.
713 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700714 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Jeff Bolzfdf96072018-04-10 14:32:18 -0500715
John Zulauf382e1912019-06-10 15:27:44 -0600716 if (binding_it.IsVariableDescriptorCount()) {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500717 // Only validate the first N descriptors if it uses variable_count
John Zulauf382e1912019-06-10 15:27:44 -0600718 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
Jeff Bolzfdf96072018-04-10 14:32:18 -0500719 }
720
John Zulaufc483f442017-12-15 14:02:06 -0700721 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
Lockeb994adf2019-03-29 23:52:31 -0600722 uint32_t index = i - index_range.start;
John Zulauf382e1912019-06-10 15:27:44 -0600723 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
Lockeb994adf2019-03-29 23:52:31 -0600724
Jeff Bolz6cede832019-08-09 23:30:39 -0500725 if (descriptor->GetClass() == DescriptorClass::InlineUniform) {
726 // Can't validate the descriptor because it may not have been updated.
Jeff Bolzfdf96072018-04-10 14:32:18 -0500727 continue;
John Zulauf382e1912019-06-10 15:27:44 -0600728 } else if (!descriptor->updated) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700729 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600730 error_str << "Descriptor in binding #" << binding << " index " << index
Tobin Ehlisc604bd72019-06-19 11:54:51 -0600731 << " is being used in draw but has never been updated via vkUpdateDescriptorSets() or a similar call.";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700732 *error = error_str.str();
733 return false;
734 } else {
John Zulauf382e1912019-06-10 15:27:44 -0600735 auto descriptor_class = descriptor->GetClass();
John Zulaufc93c4252019-06-25 09:19:49 -0600736 if (descriptor_class == DescriptorClass::GeneralBuffer) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700737 // Verify that buffers are valid
John Zulauf382e1912019-06-10 15:27:44 -0600738 auto buffer = static_cast<const BufferDescriptor *>(descriptor)->GetBuffer();
John Zulaufc93c4252019-06-25 09:19:49 -0600739 auto buffer_node = GetBufferState(buffer);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700740 if (!buffer_node) {
741 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600742 error_str << "Descriptor in binding #" << binding << " index " << index << " references invalid buffer "
743 << buffer << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700744 *error = error_str.str();
745 return false;
John Zulauf48a6a702017-12-22 17:14:54 -0700746 } else if (!buffer_node->sparse) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700747 for (auto mem_binding : buffer_node->GetBoundMemory()) {
John Zulaufc93c4252019-06-25 09:19:49 -0600748 if (!GetDevMemState(mem_binding)) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700749 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600750 error_str << "Descriptor in binding #" << binding << " index " << index << " uses buffer " << buffer
751 << " that references invalid memory " << mem_binding << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700752 *error = error_str.str();
Tobin Ehlisc8266452017-04-07 12:20:30 -0600753 return false;
754 }
755 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700756 }
John Zulauf382e1912019-06-10 15:27:44 -0600757 if (descriptor->IsDynamic()) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700758 // Validate that dynamic offsets are within the buffer
759 auto buffer_size = buffer_node->createInfo.size;
John Zulauf382e1912019-06-10 15:27:44 -0600760 auto range = static_cast<const BufferDescriptor *>(descriptor)->GetRange();
761 auto desc_offset = static_cast<const BufferDescriptor *>(descriptor)->GetOffset();
762 auto dyn_offset = dynamic_offsets[binding_it.GetDynamicOffsetIndex() + array_idx];
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700763 if (VK_WHOLE_SIZE == range) {
764 if ((dyn_offset + desc_offset) > buffer_size) {
765 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600766 error_str << "Dynamic descriptor in binding #" << binding << " index " << index << " uses buffer "
767 << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
768 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
769 << buffer_size << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700770 *error = error_str.str();
771 return false;
772 }
773 } else {
774 if ((dyn_offset + desc_offset + range) > buffer_size) {
775 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600776 error_str << "Dynamic descriptor in binding #" << binding << " index " << index << " uses buffer "
777 << buffer << " with dynamic offset " << dyn_offset << " combined with offset "
778 << desc_offset << " and range " << range << " that oversteps the buffer size of "
779 << buffer_size << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700780 *error = error_str.str();
781 return false;
782 }
783 }
784 }
John Zulaufc93c4252019-06-25 09:19:49 -0600785 } else if (descriptor_class == DescriptorClass::ImageSampler || descriptor_class == DescriptorClass::Image) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700786 VkImageView image_view;
787 VkImageLayout image_layout;
John Zulaufc93c4252019-06-25 09:19:49 -0600788 if (descriptor_class == DescriptorClass::ImageSampler) {
John Zulauf382e1912019-06-10 15:27:44 -0600789 image_view = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageView();
790 image_layout = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageLayout();
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700791 } else {
John Zulauf382e1912019-06-10 15:27:44 -0600792 image_view = static_cast<const ImageDescriptor *>(descriptor)->GetImageView();
793 image_layout = static_cast<const ImageDescriptor *>(descriptor)->GetImageLayout();
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700794 }
795 auto reqs = binding_pair.second;
796
John Zulaufc93c4252019-06-25 09:19:49 -0600797 auto image_view_state = GetImageViewState(image_view);
Tobin Ehlis836a1372017-07-14 11:25:21 -0600798 if (nullptr == image_view_state) {
799 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
800 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
801 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600802 error_str << "Descriptor in binding #" << binding << " index " << index << " is using imageView "
John Zulaufc93c4252019-06-25 09:19:49 -0600803 << report_data->FormatHandle(image_view).c_str() << " that has been destroyed.";
Tobin Ehlis836a1372017-07-14 11:25:21 -0600804 *error = error_str.str();
805 return false;
806 }
Jeff Bolz6cede832019-08-09 23:30:39 -0500807 const auto &image_view_ci = image_view_state->create_info;
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700808
unknown9b8a6852019-08-13 17:53:15 -0600809 if (reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) {
810 if (~reqs & (1 << image_view_ci.viewType)) {
811 // bad view type
812 std::stringstream error_str;
813 error_str << "Descriptor in binding #" << binding << " index " << index
814 << " requires an image view of type " << StringDescriptorReqViewType(reqs) << " but got "
815 << string_VkImageViewType(image_view_ci.viewType) << ".";
816 *error = error_str.str();
817 return false;
818 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700819
unknown9b8a6852019-08-13 17:53:15 -0600820 if (!(reqs & image_view_state->descriptor_format_bits)) {
821 // bad component type
822 std::stringstream error_str;
823 error_str << "Descriptor in binding #" << binding << " index " << index << " requires "
824 << StringDescriptorReqComponentType(reqs)
825 << " component type, but bound descriptor format is " << string_VkFormat(image_view_ci.format)
826 << ".";
827 *error = error_str.str();
828 return false;
829 }
Chris Forbesda01e8d2018-08-27 15:36:57 -0700830 }
831
Jeff Bolz6cede832019-08-09 23:30:39 -0500832 if (!disabled.image_layout_validation) {
833 auto image_node = GetImageState(image_view_ci.image);
834 assert(image_node);
835 // Verify Image Layout
836 // No "invalid layout" VUID required for this call, since the optimal_layout parameter is UNDEFINED.
837 bool hit_error = false;
838 VerifyImageLayout(cb_node, image_node, image_view_state->normalized_subresource_range,
839 image_view_ci.subresourceRange.aspectMask, image_layout, VK_IMAGE_LAYOUT_UNDEFINED,
840 caller, kVUIDUndefined, "VUID-VkDescriptorImageInfo-imageLayout-00344", &hit_error);
841 if (hit_error) {
842 *error =
843 "Image layout specified at vkUpdateDescriptorSet* or vkCmdPushDescriptorSet* time "
844 "doesn't match actual image layout at time descriptor is used. See previous error callback for "
845 "specific details.";
846 return false;
847 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700848 }
John Zulauff660ad62019-03-23 07:16:05 -0600849
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700850 // Verify Sample counts
Jeff Bolz6cede832019-08-09 23:30:39 -0500851 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_view_state->samples != VK_SAMPLE_COUNT_1_BIT) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700852 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600853 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700854 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
Jeff Bolz6cede832019-08-09 23:30:39 -0500855 << string_VkSampleCountFlagBits(image_view_state->samples) << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700856 *error = error_str.str();
857 return false;
858 }
Jeff Bolz6cede832019-08-09 23:30:39 -0500859 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_view_state->samples == VK_SAMPLE_COUNT_1_BIT) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700860 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600861 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700862 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
863 *error = error_str.str();
864 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200865 }
John Zulaufc93c4252019-06-25 09:19:49 -0600866 } else if (descriptor_class == DescriptorClass::TexelBuffer) {
John Zulauf382e1912019-06-10 15:27:44 -0600867 auto texel_buffer = static_cast<const TexelDescriptor *>(descriptor);
John Zulaufc93c4252019-06-25 09:19:49 -0600868 auto buffer_view = GetBufferViewState(texel_buffer->GetBufferView());
Chris Forbese92dd1d2019-01-21 15:58:57 -0800869
Locke2d9c3dd2019-04-08 16:29:09 -0600870 if (nullptr == buffer_view) {
871 std::stringstream error_str;
872 error_str << "Descriptor in binding #" << binding << " index " << index << " is using bufferView "
873 << buffer_view << " that has been destroyed.";
874 *error = error_str.str();
875 return false;
876 }
877 auto buffer = buffer_view->create_info.buffer;
John Zulaufc93c4252019-06-25 09:19:49 -0600878 auto buffer_state = GetBufferState(buffer);
Locke2d9c3dd2019-04-08 16:29:09 -0600879 if (!buffer_state) {
880 std::stringstream error_str;
881 error_str << "Descriptor in binding #" << binding << " index " << index << " is using buffer "
882 << buffer_state << " that has been destroyed.";
883 *error = error_str.str();
884 return false;
885 }
Chris Forbese92dd1d2019-01-21 15:58:57 -0800886 auto reqs = binding_pair.second;
887 auto format_bits = DescriptorRequirementsBitsFromFormat(buffer_view->create_info.format);
888
889 if (!(reqs & format_bits)) {
890 // bad component type
891 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600892 error_str << "Descriptor in binding #" << binding << " index " << index << " requires "
Chris Forbese92dd1d2019-01-21 15:58:57 -0800893 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
894 << string_VkFormat(buffer_view->create_info.format) << ".";
895 *error = error_str.str();
896 return false;
897 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600898 }
John Zulaufc93c4252019-06-25 09:19:49 -0600899 if (descriptor_class == DescriptorClass::ImageSampler || descriptor_class == DescriptorClass::PlainSampler) {
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600900 // Verify Sampler still valid
901 VkSampler sampler;
John Zulaufc93c4252019-06-25 09:19:49 -0600902 if (descriptor_class == DescriptorClass::ImageSampler) {
John Zulauf382e1912019-06-10 15:27:44 -0600903 sampler = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetSampler();
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600904 } else {
John Zulauf382e1912019-06-10 15:27:44 -0600905 sampler = static_cast<const SamplerDescriptor *>(descriptor)->GetSampler();
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600906 }
John Zulaufc93c4252019-06-25 09:19:49 -0600907 if (!ValidateSampler(sampler)) {
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600908 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600909 error_str << "Descriptor in binding #" << binding << " index " << index << " is using sampler " << sampler
910 << " that has been destroyed.";
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600911 *error = error_str.str();
912 return false;
Lockea223c102019-04-05 00:38:24 -0600913 } else {
John Zulauffbf3c202019-07-17 14:57:14 -0600914 const SAMPLER_STATE *sampler_state = GetSamplerState(sampler);
John Zulauf382e1912019-06-10 15:27:44 -0600915 if (sampler_state->samplerConversion && !descriptor->IsImmutableSampler()) {
Lockea223c102019-04-05 00:38:24 -0600916 std::stringstream error_str;
John Zulauf382e1912019-06-10 15:27:44 -0600917 error_str << "sampler (" << sampler << ") in the descriptor set (" << descriptor_set->GetSet()
Lockea223c102019-04-05 00:38:24 -0600918 << ") contains a YCBCR conversion (" << sampler_state->samplerConversion
919 << ") , then the sampler MUST also exists as an immutable sampler.";
920 *error = error_str.str();
921 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600922 }
923 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600924 }
925 }
926 }
927 return true;
928}
Chris Forbes57989132016-07-26 17:06:10 +1200929
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600930// Set is being deleted or updates so invalidate all bound cmd buffers
931void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
John Zulaufc93c4252019-06-25 09:19:49 -0600932 state_data_->InvalidateCommandBuffers(cb_bindings, VulkanTypedHandle(set_, kVulkanObjectTypeDescriptorSet));
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600933}
John Zulauf1d27e0a2018-11-05 10:12:48 -0700934
935// Loop through the write updates to do for a push descriptor set, ignoring dstSet
936void cvdescriptorset::DescriptorSet::PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds) {
937 assert(IsPushDescriptor());
938 for (uint32_t i = 0; i < write_count; i++) {
939 PerformWriteUpdate(&p_wds[i]);
940 }
Jason Macnak83cfd582019-07-31 10:14:24 -0700941
942 push_descriptor_set_writes.clear();
943 push_descriptor_set_writes.reserve(static_cast<std::size_t>(write_count));
944 for (uint32_t i = 0; i < write_count; i++) {
945 push_descriptor_set_writes.push_back(safe_VkWriteDescriptorSet(&p_wds[i]));
946 }
John Zulauf1d27e0a2018-11-05 10:12:48 -0700947}
948
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600949// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600950void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700951 // Perform update on a per-binding basis as consecutive updates roll over to next binding
952 auto descriptors_remaining = update->descriptorCount;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700953 auto offset = update->dstArrayElement;
locke-lunarge46b7782019-09-10 01:44:20 -0600954 auto orig_binding = DescriptorSetLayout::ConstBindingIterator(p_layout_.get(), update->dstBinding);
955 auto current_binding = orig_binding;
956
Tobin Ehlise16805c2017-08-09 09:10:37 -0600957 uint32_t update_index = 0;
locke-lunarge46b7782019-09-10 01:44:20 -0600958 // Verify next consecutive binding matches type, stage flags & immutable sampler use and if AtEnd
959 while (descriptors_remaining && orig_binding.IsConsistent(current_binding)) {
960 const auto &index_range = current_binding.GetGlobalIndexRange();
961 auto global_idx = index_range.start + offset;
962 // global_idx is which descriptor is needed to update. If global_idx > index_range.end, it means the descriptor isn't in
963 // this binding, maybe in next binding.
964 if (global_idx >= index_range.end) {
965 offset -= current_binding.GetDescriptorCount();
966 ++current_binding;
967 continue;
968 }
969
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700970 // Loop over the updates for a single binding at a time
locke-lunarge46b7782019-09-10 01:44:20 -0600971 uint32_t update_count = std::min(descriptors_remaining, current_binding.GetDescriptorCount() - offset);
Tobin Ehlise16805c2017-08-09 09:10:37 -0600972 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
973 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700974 }
975 // Roll over to next binding in case of consecutive update
976 descriptors_remaining -= update_count;
locke-lunarge46b7782019-09-10 01:44:20 -0600977 if (descriptors_remaining) {
978 // Starting offset is beyond the current binding. Check consistency, update counters and advance to the next binding,
979 // looking for the start point. All bindings (even those skipped) must be consistent with the update and with the
980 // original binding.
981 offset = 0;
982 ++current_binding;
983 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600984 }
Jeff Bolzdd4cfa12019-08-11 20:57:51 -0500985 if (update->descriptorCount) {
986 some_update_ = true;
987 change_count_++;
988 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600989
Jeff Bolzfdf96072018-04-10 14:32:18 -0500990 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
991 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
992 InvalidateBoundCmdBuffers();
993 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600994}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600995// Validate Copy update
John Zulaufc93c4252019-06-25 09:19:49 -0600996bool CoreChecks::ValidateCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *dst_set, const DescriptorSet *src_set,
997 const char *func_name, std::string *error_code, std::string *error_msg) {
John Zulaufd9435c32019-06-05 15:55:36 -0600998 auto dst_layout = dst_set->GetLayout();
999 auto src_layout = src_set->GetLayout();
1000
John Zulauf5dfd45c2018-01-17 11:06:34 -07001001 // Verify dst layout still valid
John Zulaufd9435c32019-06-05 15:55:36 -06001002 if (dst_layout->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001003 *error_code = "VUID-VkCopyDescriptorSet-dstSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -07001004 string_sprintf(error_msg,
locke-lunarg9edc2812019-06-17 23:18:52 -06001005 "Cannot call %s to perform copy update on dstSet %s"
1006 " created with destroyed %s.",
John Zulaufd9435c32019-06-05 15:55:36 -06001007 func_name, report_data->FormatHandle(dst_set->GetSet()).c_str(),
1008 report_data->FormatHandle(dst_layout->GetDescriptorSetLayout()).c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001009 return false;
1010 }
1011
1012 // Verify src layout still valid
John Zulaufd9435c32019-06-05 15:55:36 -06001013 if (src_layout->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001014 *error_code = "VUID-VkCopyDescriptorSet-srcSet-parameter";
John Zulaufb45fdc32018-10-12 15:14:17 -06001015 string_sprintf(error_msg,
Lockeca0d9792019-03-03 23:48:13 -07001016 "Cannot call %s to perform copy update of dstSet %s"
locke-lunarg9edc2812019-06-17 23:18:52 -06001017 " from srcSet %s"
1018 " created with destroyed %s.",
John Zulaufd9435c32019-06-05 15:55:36 -06001019 func_name, report_data->FormatHandle(dst_set->GetSet()).c_str(),
1020 report_data->FormatHandle(src_set->GetSet()).c_str(),
1021 report_data->FormatHandle(src_layout->GetDescriptorSetLayout()).c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001022 return false;
1023 }
1024
John Zulaufd9435c32019-06-05 15:55:36 -06001025 if (!dst_layout->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001026 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-00347";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001027 std::stringstream error_str;
John Zulaufd9435c32019-06-05 15:55:36 -06001028 error_str << "DescriptorSet " << dst_set->GetSet() << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001029 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001030 return false;
1031 }
1032 if (!src_set->HasBinding(update->srcBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001033 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-00345";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001034 std::stringstream error_str;
John Zulaufd9435c32019-06-05 15:55:36 -06001035 error_str << "DescriptorSet " << dst_set->GetSet() << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001036 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001037 return false;
1038 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001039 // Verify idle ds
John Zulaufd9435c32019-06-05 15:55:36 -06001040 if (dst_set->in_use.load() &&
1041 !(dst_layout->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
Jeff Bolzfdf96072018-04-10 14:32:18 -05001042 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1043 // TODO : Re-using Free Idle error code, need copy update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001044 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001045 std::stringstream error_str;
John Zulaufd9435c32019-06-05 15:55:36 -06001046 error_str << "Cannot call " << func_name << " to perform copy update on descriptor set " << dst_set->GetSet()
Jeff Bolzfdf96072018-04-10 14:32:18 -05001047 << " that is in use by a command buffer";
1048 *error_msg = error_str.str();
1049 return false;
1050 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001051 // src & dst set bindings are valid
1052 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -07001053 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001054 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
1055 // SRC update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001056 *error_code = "VUID-VkCopyDescriptorSet-srcArrayElement-00346";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001057 std::stringstream error_str;
1058 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
John Zulaufc483f442017-12-15 14:02:06 -07001059 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001060 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001061 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001062 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001063 return false;
1064 }
John Zulaufd9435c32019-06-05 15:55:36 -06001065 auto dst_start_idx = dst_layout->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
1066 if ((dst_start_idx + update->descriptorCount) > dst_layout->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001067 // DST update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001068 *error_code = "VUID-VkCopyDescriptorSet-dstArrayElement-00348";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001069 std::stringstream error_str;
John Zulaufd9435c32019-06-05 15:55:36 -06001070 error_str << "Attempting copy update to descriptorSet " << dst_set->GetSet() << " binding#" << update->dstBinding
1071 << " with offset index of " << dst_layout->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001072 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
John Zulaufd9435c32019-06-05 15:55:36 -06001073 << " descriptors oversteps total number of descriptors in set: " << dst_layout->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001074 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001075 return false;
1076 }
1077 // Check that types match
Dave Houltond8ed0212018-05-16 17:18:24 -06001078 // TODO : Base default error case going from here is "VUID-VkAcquireNextImageInfoKHR-semaphore-parameter"2ba which covers all
1079 // consistency issues, need more fine-grained error codes
Dave Houlton00c154e2018-05-24 13:20:50 -06001080 *error_code = "VUID-VkCopyDescriptorSet-srcSet-00349";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001081 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
John Zulaufd9435c32019-06-05 15:55:36 -06001082 auto dst_type = dst_layout->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001083 if (src_type != dst_type) {
1084 std::stringstream error_str;
John Zulaufd9435c32019-06-05 15:55:36 -06001085 error_str << "Attempting copy update to descriptorSet " << dst_set->GetSet() << " binding #" << update->dstBinding
1086 << " with type " << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet()
1087 << " binding #" << update->srcBinding << " with type " << string_VkDescriptorType(src_type)
1088 << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001089 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001090 return false;
1091 }
1092 // Verify consistency of src & dst bindings if update crosses binding boundaries
John Zulaufd9435c32019-06-05 15:55:36 -06001093 if ((!VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator(src_layout.get(), update->srcBinding),
John Zulauf4a015c92019-06-04 09:50:05 -06001094 update->srcArrayElement, update->descriptorCount, "copy update from", src_set->GetSet(),
1095 error_msg)) ||
John Zulaufd9435c32019-06-05 15:55:36 -06001096 (!VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator(dst_layout.get(), update->dstBinding),
1097 update->dstArrayElement, update->descriptorCount, "copy update to", dst_set->GetSet(),
1098 error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001099 return false;
1100 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001101
John Zulaufd9435c32019-06-05 15:55:36 -06001102 if ((src_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1103 !(dst_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001104 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01918";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001105 std::stringstream error_str;
1106 error_str << "If pname:srcSet's (" << update->srcSet
1107 << ") layout was created with the "
1108 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1109 "set, then pname:dstSet's ("
1110 << update->dstSet
1111 << ") layout must: also have been created with the "
1112 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1113 *error_msg = error_str.str();
1114 return false;
1115 }
1116
John Zulaufd9435c32019-06-05 15:55:36 -06001117 if (!(src_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1118 (dst_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001119 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01919";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001120 std::stringstream error_str;
1121 error_str << "If pname:srcSet's (" << update->srcSet
1122 << ") layout was created without the "
1123 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1124 "set, then pname:dstSet's ("
1125 << update->dstSet
1126 << ") layout must: also have been created without the "
1127 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1128 *error_msg = error_str.str();
1129 return false;
1130 }
1131
1132 if ((src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
John Zulaufd9435c32019-06-05 15:55:36 -06001133 !(dst_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001134 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01920";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001135 std::stringstream error_str;
1136 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1137 << ") was allocated was created "
1138 "with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1139 "set, then the descriptor pool from which pname:dstSet ("
1140 << update->dstSet
1141 << ") was allocated must: "
1142 "also have been created with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1143 *error_msg = error_str.str();
1144 return false;
1145 }
1146
1147 if (!(src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
John Zulaufd9435c32019-06-05 15:55:36 -06001148 (dst_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001149 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01921";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001150 std::stringstream error_str;
1151 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1152 << ") was allocated was created "
1153 "without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1154 "set, then the descriptor pool from which pname:dstSet ("
1155 << update->dstSet
1156 << ") was allocated must: "
1157 "also have been created without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1158 *error_msg = error_str.str();
1159 return false;
1160 }
1161
Jeff Bolze54ae892018-09-08 12:16:29 -05001162 if (src_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1163 if ((update->srcArrayElement % 4) != 0) {
1164 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02223";
1165 std::stringstream error_str;
1166 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1167 << "srcArrayElement " << update->srcArrayElement << " not a multiple of 4";
1168 *error_msg = error_str.str();
1169 return false;
1170 }
1171 if ((update->dstArrayElement % 4) != 0) {
1172 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02224";
1173 std::stringstream error_str;
1174 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1175 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1176 *error_msg = error_str.str();
1177 return false;
1178 }
1179 if ((update->descriptorCount % 4) != 0) {
1180 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02225";
1181 std::stringstream error_str;
1182 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1183 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1184 *error_msg = error_str.str();
1185 return false;
1186 }
1187 }
1188
Tobin Ehlisd41e7b62016-05-19 07:56:18 -06001189 // Update parameters all look good and descriptor updated so verify update contents
John Zulaufc93c4252019-06-25 09:19:49 -06001190 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, func_name, error_code, error_msg)) return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001191
1192 // All checks passed so update is good
1193 return true;
1194}
1195// Perform Copy update
1196void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -07001197 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
1198 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001199 // Update parameters all look good so perform update
1200 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001201 auto src = src_set->descriptors_[src_start_idx + di].get();
1202 auto dst = descriptors_[dst_start_idx + di].get();
1203 if (src->updated) {
1204 dst->CopyUpdate(src);
1205 some_update_ = true;
Jeff Bolzdd4cfa12019-08-11 20:57:51 -05001206 change_count_++;
Józef Kucia5297e372017-10-13 22:31:34 +02001207 } else {
1208 dst->updated = false;
1209 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001210 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001211
Jeff Bolzfdf96072018-04-10 14:32:18 -05001212 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1213 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1214 InvalidateBoundCmdBuffers();
1215 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001216}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001217
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001218// Update the drawing state for the affected descriptors.
1219// Set cb_node to this set and this set to cb_node.
1220// Add the bindings of the descriptor
1221// Set the layout based on the current descriptor layout (will mask subsequent layer mismatch errors)
1222// TODO: Modify the UpdateDrawState virtural functions to *only* set initial layout and not change layouts
Tobin Ehlisf9519102016-08-17 09:49:13 -06001223// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
1224// to be used in a draw by the given cb_node
John Zulauffbf3c202019-07-17 14:57:14 -06001225void cvdescriptorset::DescriptorSet::UpdateDrawState(ValidationStateTracker *device_data, CMD_BUFFER_STATE *cb_node,
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001226 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Jeff Bolzafa429a2019-08-14 09:59:22 -05001227 if (!device_data->disabled.command_buffer_state) {
1228 // bind cb to this descriptor set
1229 // Add bindings for descriptor set, the set's pool, and individual objects in the set
1230 auto inserted = cb_node->object_bindings.emplace(set_, kVulkanObjectTypeDescriptorSet);
1231 if (inserted.second) {
1232 cb_bindings.insert(cb_node);
1233 auto inserted2 = cb_node->object_bindings.emplace(pool_state_->pool, kVulkanObjectTypeDescriptorPool);
1234 if (inserted2.second) {
1235 pool_state_->cb_bindings.insert(cb_node);
1236 }
1237 }
1238 }
Jeff Bolze18e7242019-08-12 20:55:22 -05001239
1240 // Descriptor UpdateDrawState functions do two things - associate resources to the command buffer,
1241 // and call image layout validation callbacks. If both are disabled, skip the entire loop.
1242 if (device_data->disabled.command_buffer_state && device_data->disabled.image_layout_validation) {
1243 return;
1244 }
1245
Tobin Ehlisf9519102016-08-17 09:49:13 -06001246 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
1247 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -07001248 for (auto binding_req_pair : binding_req_map) {
1249 auto binding = binding_req_pair.first;
Tony-LunarG62c5dba2018-12-20 14:27:23 -07001250 // We aren't validating descriptors created with PARTIALLY_BOUND or UPDATE_AFTER_BIND, so don't record state
1251 if (p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
1252 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) {
1253 continue;
1254 }
John Zulaufc483f442017-12-15 14:02:06 -07001255 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
1256 for (uint32_t i = range.start; i < range.end; ++i) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001257 descriptors_[i]->UpdateDrawState(device_data, cb_node);
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001258 }
1259 }
1260}
1261
John Zulauffbf3c202019-07-17 14:57:14 -06001262void cvdescriptorset::DescriptorSet::FilterOneBindingReq(const BindingReqMap::value_type &binding_req_pair, BindingReqMap *out_req,
1263 const TrackedBindings &bindings, uint32_t limit) {
1264 if (bindings.size() < limit) {
1265 const auto it = bindings.find(binding_req_pair.first);
1266 if (it == bindings.cend()) out_req->emplace(binding_req_pair);
John Zulauf48a6a702017-12-22 17:14:54 -07001267 }
1268}
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001269
John Zulauffbf3c202019-07-17 14:57:14 -06001270void cvdescriptorset::DescriptorSet::FilterBindingReqs(const CMD_BUFFER_STATE &cb_state, const PIPELINE_STATE &pipeline,
1271 const BindingReqMap &in_req, BindingReqMap *out_req) const {
1272 // For const cleanliness we have to find in the maps...
1273 const auto validated_it = cached_validation_.find(&cb_state);
1274 if (validated_it == cached_validation_.cend()) {
1275 // We have nothing validated, copy in to out
1276 for (const auto &binding_req_pair : in_req) {
1277 out_req->emplace(binding_req_pair);
John Zulauf48a6a702017-12-22 17:14:54 -07001278 }
John Zulauffbf3c202019-07-17 14:57:14 -06001279 return;
John Zulauf48a6a702017-12-22 17:14:54 -07001280 }
John Zulauffbf3c202019-07-17 14:57:14 -06001281 const auto &validated = validated_it->second;
John Zulauf48a6a702017-12-22 17:14:54 -07001282
John Zulauffbf3c202019-07-17 14:57:14 -06001283 const auto image_sample_version_it = validated.image_samplers.find(&pipeline);
1284 const VersionedBindings *image_sample_version = nullptr;
1285 if (image_sample_version_it != validated.image_samplers.cend()) {
1286 image_sample_version = &(image_sample_version_it->second);
1287 }
1288 const auto &dynamic_buffers = validated.dynamic_buffers;
1289 const auto &non_dynamic_buffers = validated.non_dynamic_buffers;
John Zulauf48a6a702017-12-22 17:14:54 -07001290 const auto &stats = p_layout_->GetBindingTypeStats();
1291 for (const auto &binding_req_pair : in_req) {
1292 auto binding = binding_req_pair.first;
1293 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
1294 if (!layout_binding) {
1295 continue;
1296 }
1297 // Caching criteria differs per type.
1298 // If image_layout have changed , the image descriptors need to be validated against them.
1299 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1300 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
John Zulauffbf3c202019-07-17 14:57:14 -06001301 FilterOneBindingReq(binding_req_pair, out_req, dynamic_buffers, stats.dynamic_buffer_count);
John Zulauf48a6a702017-12-22 17:14:54 -07001302 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1303 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
John Zulauffbf3c202019-07-17 14:57:14 -06001304 FilterOneBindingReq(binding_req_pair, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
John Zulauf48a6a702017-12-22 17:14:54 -07001305 } else {
1306 // This is rather crude, as the changed layouts may not impact the bound descriptors,
1307 // but the simple "versioning" is a simple "dirt" test.
John Zulauffbf3c202019-07-17 14:57:14 -06001308 bool stale = true;
1309 if (image_sample_version) {
1310 const auto version_it = image_sample_version->find(binding);
1311 if (version_it != image_sample_version->cend() && (version_it->second == cb_state.image_layout_change_count)) {
1312 stale = false;
1313 }
1314 }
1315 if (stale) {
John Zulauf48a6a702017-12-22 17:14:54 -07001316 out_req->emplace(binding_req_pair);
1317 }
1318 }
1319 }
1320}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001321
John Zulauffbf3c202019-07-17 14:57:14 -06001322void cvdescriptorset::DescriptorSet::UpdateValidationCache(const CMD_BUFFER_STATE &cb_state, const PIPELINE_STATE &pipeline,
1323 const BindingReqMap &updated_bindings) {
1324 // For const cleanliness we have to find in the maps...
1325 auto &validated = cached_validation_[&cb_state];
1326
1327 auto &image_sample_version = validated.image_samplers[&pipeline];
1328 auto &dynamic_buffers = validated.dynamic_buffers;
1329 auto &non_dynamic_buffers = validated.non_dynamic_buffers;
1330 for (const auto &binding_req_pair : updated_bindings) {
1331 auto binding = binding_req_pair.first;
1332 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
1333 if (!layout_binding) {
1334 continue;
1335 }
1336 // Caching criteria differs per type.
1337 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1338 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
1339 dynamic_buffers.emplace(binding);
1340 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1341 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
1342 non_dynamic_buffers.emplace(binding);
1343 } else {
1344 // Save the layout change version...
1345 image_sample_version[binding] = cb_state.image_layout_change_count;
1346 }
1347 }
1348}
1349
Tobin Ehlis300888c2016-05-18 13:43:26 -06001350cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001351 updated = false;
1352 descriptor_class = PlainSampler;
1353 if (immut) {
1354 sampler_ = *immut;
1355 immutable_ = true;
1356 updated = true;
1357 }
1358}
Tobin Ehlise2f80292016-06-02 10:08:53 -06001359// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
John Zulaufc93c4252019-06-25 09:19:49 -06001360bool CoreChecks::ValidateSampler(const VkSampler sampler) const { return (GetSamplerState(sampler) != nullptr); }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001361
John Zulaufc93c4252019-06-25 09:19:49 -06001362bool CoreChecks::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
John Zulaufbd9b3412019-08-22 17:16:11 -06001363 const char *func_name, std::string *error_code, std::string *error_msg) const {
Dave Houlton00c154e2018-05-24 13:20:50 -06001364 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00326";
John Zulaufc93c4252019-06-25 09:19:49 -06001365 auto iv_state = GetImageViewState(image_view);
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001366 assert(iv_state);
1367
Tobin Ehlis81280962016-07-20 14:04:20 -06001368 // 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 -06001369 // Validate that imageLayout is compatible with aspect_mask and image format
1370 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001371 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
1372 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001373 VkFormat format = VK_FORMAT_MAX_ENUM;
1374 VkImageUsageFlags usage = 0;
John Zulaufc93c4252019-06-25 09:19:49 -06001375 auto image_node = GetImageState(image);
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001376 assert(image_node);
Chris Forbes67757ff2017-07-21 13:59:01 -07001377
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001378 format = image_node->createInfo.format;
1379 usage = image_node->createInfo.usage;
1380 // Validate that memory is bound to image
1381 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
1382 // the error here occurs is if memory bound to a created imageView has been freed.
1383 if (ValidateMemoryIsBoundToImage(image_node, func_name, "VUID-VkImageViewCreateInfo-image-01020")) {
1384 *error_code = "VUID-VkImageViewCreateInfo-image-01020";
1385 *error_msg = "No memory bound to image.";
Tobin Ehlis1809f912016-05-25 09:24:36 -06001386 return false;
1387 }
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001388
1389 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
1390 // but not binding them to descriptor sets.
1391 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D && (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
1392 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
1393 *error_code = "VUID-VkDescriptorImageInfo-imageView-00343";
1394 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
1395 return false;
1396 }
1397
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001398 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
1399 // vkCreateImageView(). What's the best way to create unique id for these cases?
Mark Lobodzinski3fc3d752019-06-24 14:22:46 -06001400 *error_code = "UNASSIGNED-CoreValidation-DrawState-InvalidImageView";
Dave Houlton1d2022c2017-03-29 11:43:58 -06001401 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -06001402 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001403 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1404 // Only Color bit must be set
1405 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001406 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -07001407 error_str
John Zulaufc93c4252019-06-25 09:19:49 -06001408 << "ImageView (" << report_data->FormatHandle(image_view).c_str()
Dave Houltona9df0ce2018-02-07 10:51:23 -07001409 << ") 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 -06001410 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001411 return false;
1412 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001413 // format must NOT be DS
1414 if (ds) {
1415 std::stringstream error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06001416 error_str << "ImageView (" << report_data->FormatHandle(image_view).c_str()
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001417 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1418 << string_VkFormat(format) << " which is not a color format.";
1419 *error_msg = error_str.str();
1420 return false;
1421 }
1422 break;
1423 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1424 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1425 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001426 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1427 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1428 // both must NOT be set
1429 std::stringstream error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06001430 error_str << "ImageView (" << report_data->FormatHandle(image_view).c_str()
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001431 << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001432 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001433 return false;
1434 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001435 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1436 // Neither were set
1437 std::stringstream error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06001438 error_str << "ImageView (" << report_data->FormatHandle(image_view).c_str() << ") has layout "
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001439 << string_VkImageLayout(image_layout) << " but does not have STENCIL or DEPTH aspects set";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001440 *error_msg = error_str.str();
1441 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001442 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001443 // format must be DS
1444 if (!ds) {
1445 std::stringstream error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06001446 error_str << "ImageView (" << report_data->FormatHandle(image_view).c_str() << ") has layout "
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001447 << string_VkImageLayout(image_layout) << " but the image format is " << string_VkFormat(format)
1448 << " which is not a depth/stencil format.";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001449 *error_msg = error_str.str();
1450 return false;
1451 }
1452 break;
1453 default:
1454 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1455 if (ds) {
1456 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1457 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1458 // both must NOT be set
1459 std::stringstream error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06001460 error_str << "ImageView (" << report_data->FormatHandle(image_view).c_str() << ") has layout "
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001461 << string_VkImageLayout(image_layout) << " and is using depth/stencil image of format "
1462 << string_VkFormat(format)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001463 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1464 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1465 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1466 "reads respectively.";
Mark Lobodzinski4d05d7a2019-06-25 09:12:06 -06001467 *error_code = "VUID-VkDescriptorImageInfo-imageView-01976";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001468 *error_msg = error_str.str();
1469 return false;
1470 }
1471 }
1472 }
1473 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001474 }
1475 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001476 // 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 -06001477 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1478 // under vkCreateImage()
Dave Houltond8ed0212018-05-16 17:18:24 -06001479 // TODO : Need to also validate case "VUID-VkWriteDescriptorSet-descriptorType-00336" where STORAGE_IMAGE & INPUT_ATTACH types
1480 // must have been created with identify swizzle
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001481 const char *error_usage_bit = nullptr;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001482 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001483 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1484 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1485 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1486 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
1487 }
1488 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001489 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001490 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1491 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1492 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
1493 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
1494 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001495 // TODO : Need to create custom enum error codes for these cases
1496 if (image_node->shared_presentable) {
1497 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
John Zulaufc93c4252019-06-25 09:19:49 -06001498 error_str << "ImageView (" << report_data->FormatHandle(image_view).c_str()
Dave Houltona9df0ce2018-02-07 10:51:23 -07001499 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
1500 "layout "
1501 << string_VkImageLayout(image_layout)
1502 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report "
1503 "support for VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the "
1504 "VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001505 *error_msg = error_str.str();
1506 return false;
1507 }
1508 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
John Zulaufc93c4252019-06-25 09:19:49 -06001509 error_str << "ImageView (" << report_data->FormatHandle(image_view).c_str()
Dave Houltona9df0ce2018-02-07 10:51:23 -07001510 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
1511 << string_VkImageLayout(image_layout)
1512 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage "
1513 "images can only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001514 *error_msg = error_str.str();
1515 return false;
1516 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001517 }
1518 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001519 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001520 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
1521 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
1522 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
1523 }
1524 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001525 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001526 default:
1527 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001528 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001529 if (error_usage_bit) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001530 std::stringstream error_str;
Mark Lobodzinski54a67c42019-06-24 14:35:21 -06001531 error_str << "ImageView (" << report_data->FormatHandle(image_view).c_str() << ") with usage mask " << std::hex
1532 << std::showbase << usage << " being used for a descriptor update of type " << string_VkDescriptorType(type)
1533 << " does not have " << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001534 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001535 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001536 }
John Zulauff4c07882019-01-24 14:03:36 -07001537
1538 if ((type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) || (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)) {
1539 // Test that the layout is compatible with the descriptorType for the two sampled image types
1540 const static std::array<VkImageLayout, 3> valid_layouts = {
Jeremy Hayesd0549f62019-06-05 10:15:36 -06001541 {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 -07001542
1543 struct ExtensionLayout {
1544 VkImageLayout layout;
1545 bool DeviceExtensions::*extension;
1546 };
1547
1548 const static std::array<ExtensionLayout, 3> extended_layouts{
1549 {// Note double brace req'd for aggregate initialization
1550 {VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR, &DeviceExtensions::vk_khr_shared_presentable_image},
1551 {VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2},
1552 {VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2}}};
John Zulaufc93c4252019-06-25 09:19:49 -06001553 auto is_layout = [image_layout, this](const ExtensionLayout &ext_layout) {
1554 return device_extensions.*(ext_layout.extension) && (ext_layout.layout == image_layout);
John Zulauff4c07882019-01-24 14:03:36 -07001555 };
1556
1557 bool valid_layout = (std::find(valid_layouts.cbegin(), valid_layouts.cend(), image_layout) != valid_layouts.cend()) ||
1558 std::any_of(extended_layouts.cbegin(), extended_layouts.cend(), is_layout);
1559
1560 if (!valid_layout) {
1561 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01403";
1562 std::stringstream error_str;
1563 error_str << "Descriptor update with descriptorType " << string_VkDescriptorType(type)
Mark Lobodzinski74eddba2019-06-21 14:16:33 -06001564 << " is being updated with invalid imageLayout " << string_VkImageLayout(image_layout) << " for image "
John Zulaufc93c4252019-06-25 09:19:49 -06001565 << report_data->FormatHandle(image).c_str() << " in imageView "
1566 << report_data->FormatHandle(image_view).c_str()
John Zulauff4c07882019-01-24 14:03:36 -07001567 << ". Allowed layouts are: VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, "
1568 << "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL";
1569 for (auto &ext_layout : extended_layouts) {
John Zulaufc93c4252019-06-25 09:19:49 -06001570 if (device_extensions.*(ext_layout.extension)) {
John Zulauff4c07882019-01-24 14:03:36 -07001571 error_str << ", " << string_VkImageLayout(ext_layout.layout);
1572 }
1573 }
1574 *error_msg = error_str.str();
1575 return false;
1576 }
1577 }
1578
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001579 return true;
1580}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001581
Tobin Ehlis300888c2016-05-18 13:43:26 -06001582void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Chris Forbesfea2c542018-04-13 09:34:15 -07001583 if (!immutable_) {
1584 sampler_ = update->pImageInfo[index].sampler;
1585 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001586 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001587}
1588
Tobin Ehlis300888c2016-05-18 13:43:26 -06001589void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001590 if (!immutable_) {
1591 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001592 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001593 }
1594 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001595}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001596
John Zulauffbf3c202019-07-17 14:57:14 -06001597void cvdescriptorset::SamplerDescriptor::UpdateDrawState(ValidationStateTracker *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001598 if (!immutable_) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001599 auto sampler_state = dev_data->GetSamplerState(sampler_);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001600 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001601 }
1602}
1603
Tobin Ehlis300888c2016-05-18 13:43:26 -06001604cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001605 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001606 updated = false;
1607 descriptor_class = ImageSampler;
1608 if (immut) {
1609 sampler_ = *immut;
1610 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001611 }
1612}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001613
Tobin Ehlis300888c2016-05-18 13:43:26 -06001614void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001615 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001616 const auto &image_info = update->pImageInfo[index];
Chris Forbesfea2c542018-04-13 09:34:15 -07001617 if (!immutable_) {
1618 sampler_ = image_info.sampler;
1619 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001620 image_view_ = image_info.imageView;
1621 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001622}
1623
Tobin Ehlis300888c2016-05-18 13:43:26 -06001624void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001625 if (!immutable_) {
1626 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001627 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001628 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001629 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1630 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001631 updated = true;
1632 image_view_ = image_view;
1633 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001634}
1635
John Zulauffbf3c202019-07-17 14:57:14 -06001636void cvdescriptorset::ImageSamplerDescriptor::UpdateDrawState(ValidationStateTracker *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001637 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001638 if (!immutable_) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001639 auto sampler_state = dev_data->GetSamplerState(sampler_);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001640 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001641 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001642 // Add binding for image
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001643 auto iv_state = dev_data->GetImageViewState(image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001644 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001645 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
John Zulauffbf3c202019-07-17 14:57:14 -06001646 dev_data->CallSetImageViewInitialLayoutCallback(cb_node, *iv_state, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06001647 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001648}
1649
Tobin Ehlis300888c2016-05-18 13:43:26 -06001650cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1651 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001652 updated = false;
1653 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001654 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001655}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001656
Tobin Ehlis300888c2016-05-18 13:43:26 -06001657void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001658 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001659 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001660 image_view_ = image_info.imageView;
1661 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001662}
1663
Tobin Ehlis300888c2016-05-18 13:43:26 -06001664void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001665 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1666 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001667 updated = true;
1668 image_view_ = image_view;
1669 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001670}
1671
John Zulauffbf3c202019-07-17 14:57:14 -06001672void cvdescriptorset::ImageDescriptor::UpdateDrawState(ValidationStateTracker *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001673 // Add binding for image
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001674 auto iv_state = dev_data->GetImageViewState(image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001675 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001676 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
John Zulauffbf3c202019-07-17 14:57:14 -06001677 dev_data->CallSetImageViewInitialLayoutCallback(cb_node, *iv_state, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06001678 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001679}
1680
Tobin Ehlis300888c2016-05-18 13:43:26 -06001681cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1682 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001683 updated = false;
1684 descriptor_class = GeneralBuffer;
1685 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1686 dynamic_ = true;
1687 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1688 storage_ = true;
1689 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1690 dynamic_ = true;
1691 storage_ = true;
1692 }
1693}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001694void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001695 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001696 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001697 buffer_ = buffer_info.buffer;
1698 offset_ = buffer_info.offset;
1699 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001700}
1701
Tobin Ehlis300888c2016-05-18 13:43:26 -06001702void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1703 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001704 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001705 buffer_ = buff_desc->buffer_;
1706 offset_ = buff_desc->offset_;
1707 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001708}
1709
John Zulauffbf3c202019-07-17 14:57:14 -06001710void cvdescriptorset::BufferDescriptor::UpdateDrawState(ValidationStateTracker *dev_data, CMD_BUFFER_STATE *cb_node) {
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07001711 auto buffer_node = dev_data->GetBufferState(buffer_);
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001712 if (buffer_node) dev_data->AddCommandBufferBindingBuffer(cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001713}
1714
Tobin Ehlis300888c2016-05-18 13:43:26 -06001715cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001716 updated = false;
1717 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001718 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001719}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001720
Tobin Ehlis300888c2016-05-18 13:43:26 -06001721void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001722 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001723 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001724}
1725
Tobin Ehlis300888c2016-05-18 13:43:26 -06001726void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1727 updated = true;
1728 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1729}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001730
John Zulauffbf3c202019-07-17 14:57:14 -06001731void cvdescriptorset::TexelDescriptor::UpdateDrawState(ValidationStateTracker *dev_data, CMD_BUFFER_STATE *cb_node) {
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07001732 auto bv_state = dev_data->GetBufferViewState(buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001733 if (bv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001734 dev_data->AddCommandBufferBindingBufferView(cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001735 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001736}
1737
Tobin Ehlis300888c2016-05-18 13:43:26 -06001738// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1739// sets, and then calls their respective Validate[Write|Copy]Update functions.
1740// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1741// be skipped, then true is returned.
1742// If there is no issue with the update, then false is returned.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001743bool CoreChecks::ValidateUpdateDescriptorSets(uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001744 const VkCopyDescriptorSet *p_cds, const char *func_name) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001745 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001746 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001747 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001748 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001749 auto set_node = GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001750 if (!set_node) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001751 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1752 HandleToUint64(dest_set), kVUID_Core_DrawState_InvalidDescriptorSet,
locke-lunarg9edc2812019-06-17 23:18:52 -06001753 "Cannot call %s on %s that has not been allocated.", func_name,
Lockeca0d9792019-03-03 23:48:13 -07001754 report_data->FormatHandle(dest_set).c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001755 } else {
Dave Houltond8ed0212018-05-16 17:18:24 -06001756 std::string error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001757 std::string error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06001758 if (!ValidateWriteUpdate(set_node, &p_wds[i], func_name, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001759 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
locke-lunarg9edc2812019-06-17 23:18:52 -06001760 HandleToUint64(dest_set), error_code, "%s failed write update validation for %s with error: %s.",
1761 func_name, report_data->FormatHandle(dest_set).c_str(), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001762 }
1763 }
1764 }
1765 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001766 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001767 auto dst_set = p_cds[i].dstSet;
1768 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001769 auto src_node = GetSetNode(src_set);
1770 auto dst_node = GetSetNode(dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001771 // Object_tracker verifies that src & dest descriptor set are valid
1772 assert(src_node);
1773 assert(dst_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001774 std::string error_code;
Tobin Ehlisa1712752017-01-04 09:41:47 -07001775 std::string error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06001776 if (!ValidateCopyUpdate(&p_cds[i], dst_node, src_node, func_name, &error_code, &error_str)) {
locke-lunarg9edc2812019-06-17 23:18:52 -06001777 skip |=
1778 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1779 HandleToUint64(dst_set), error_code, "%s failed copy update from %s to %s with error: %s.", func_name,
1780 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 -06001781 }
1782 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001783 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001784}
1785// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1786// sets, and then calls their respective Perform[Write|Copy]Update functions.
1787// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1788// with the same set of updates.
1789// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1790// calling down the chain.
John Zulaufe3b35f32019-06-25 14:21:21 -06001791void cvdescriptorset::PerformUpdateDescriptorSets(ValidationStateTracker *dev_data, uint32_t write_count,
1792 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1793 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001794 // Write updates first
1795 uint32_t i = 0;
1796 for (i = 0; i < write_count; ++i) {
1797 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001798 auto set_node = dev_data->GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001799 if (set_node) {
1800 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001801 }
1802 }
1803 // Now copy updates
1804 for (i = 0; i < copy_count; ++i) {
1805 auto dst_set = p_cds[i].dstSet;
1806 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001807 auto src_node = dev_data->GetSetNode(src_set);
1808 auto dst_node = dev_data->GetSetNode(dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001809 if (src_node && dst_node) {
1810 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001811 }
1812 }
1813}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001814
John Zulaufe3b35f32019-06-25 14:21:21 -06001815cvdescriptorset::DecodedTemplateUpdate::DecodedTemplateUpdate(const ValidationStateTracker *device_data,
1816 VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
1817 const void *pData, VkDescriptorSetLayout push_layout) {
John Zulaufb845eb22018-10-12 11:41:06 -06001818 auto const &create_info = template_state->create_info;
1819 inline_infos.resize(create_info.descriptorUpdateEntryCount); // Make sure we have one if we need it
1820 desc_writes.reserve(create_info.descriptorUpdateEntryCount); // emplaced, so reserved without initialization
John Zulauf1d27e0a2018-11-05 10:12:48 -07001821 VkDescriptorSetLayout effective_dsl = create_info.templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
1822 ? create_info.descriptorSetLayout
1823 : push_layout;
1824 auto layout_obj = GetDescriptorSetLayout(device_data, effective_dsl);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001825
1826 // Create a WriteDescriptorSet struct for each template update entry
1827 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1828 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1829 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1830 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1831
John Zulaufb6d71202017-12-22 16:47:09 -07001832 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001833 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1834 desc_writes.emplace_back();
1835 auto &write_entry = desc_writes.back();
1836
1837 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1838 char *update_entry = (char *)(pData) + offset;
1839
1840 if (dst_array_element >= binding_count) {
1841 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001842 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001843 }
1844
1845 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1846 write_entry.pNext = NULL;
1847 write_entry.dstSet = descriptorSet;
1848 write_entry.dstBinding = binding_being_updated;
1849 write_entry.dstArrayElement = dst_array_element;
1850 write_entry.descriptorCount = 1;
1851 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1852
1853 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1854 case VK_DESCRIPTOR_TYPE_SAMPLER:
1855 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1856 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1857 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1858 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1859 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1860 break;
1861
1862 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1863 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1864 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1865 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1866 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1867 break;
1868
1869 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1870 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1871 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1872 break;
Dave Houlton142c4cb2018-10-17 15:04:41 -06001873 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1874 VkWriteDescriptorSetInlineUniformBlockEXT *inline_info = &inline_infos[i];
1875 inline_info->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT;
1876 inline_info->pNext = nullptr;
1877 inline_info->dataSize = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1878 inline_info->pData = update_entry;
1879 write_entry.pNext = inline_info;
Ricardo Garciafee15732019-05-28 11:13:31 +02001880 // descriptorCount must match the dataSize member of the VkWriteDescriptorSetInlineUniformBlockEXT structure
1881 write_entry.descriptorCount = inline_info->dataSize;
Dave Houlton142c4cb2018-10-17 15:04:41 -06001882 // skip the rest of the array, they just represent bytes in the update
1883 j = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1884 break;
1885 }
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001886 default:
1887 assert(0);
1888 break;
1889 }
1890 dst_array_element++;
1891 }
1892 }
John Zulaufb845eb22018-10-12 11:41:06 -06001893}
John Zulaufb45fdc32018-10-12 15:14:17 -06001894// These helper functions carry out the validate and record descriptor updates peformed via update templates. They decode
1895// the templatized data and leverage the non-template UpdateDescriptor helper functions.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001896bool CoreChecks::ValidateUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
1897 const void *pData) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001898 // Translate the templated update into a normal update for validation...
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001899 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
1900 return ValidateUpdateDescriptorSets(static_cast<uint32_t>(decoded_update.desc_writes.size()), decoded_update.desc_writes.data(),
1901 0, NULL, "vkUpdateDescriptorSetWithTemplate()");
John Zulaufb45fdc32018-10-12 15:14:17 -06001902}
John Zulaufb845eb22018-10-12 11:41:06 -06001903
John Zulauf4e7bcb52018-11-02 10:46:30 -06001904std::string cvdescriptorset::DescriptorSet::StringifySetAndLayout() const {
1905 std::string out;
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001906 auto layout_handle = p_layout_->GetDescriptorSetLayout();
John Zulauf4e7bcb52018-11-02 10:46:30 -06001907 if (IsPushDescriptor()) {
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001908 string_sprintf(&out, "Push Descriptors defined with VkDescriptorSetLayout %s",
John Zulaufc93c4252019-06-25 09:19:49 -06001909 state_data_->report_data->FormatHandle(layout_handle).c_str());
John Zulauf4e7bcb52018-11-02 10:46:30 -06001910 } else {
Mark Lobodzinski6b042922019-06-21 14:46:42 -06001911 string_sprintf(&out, "VkDescriptorSet %s allocated with VkDescriptorSetLayout %s",
John Zulaufc93c4252019-06-25 09:19:49 -06001912 state_data_->report_data->FormatHandle(set_).c_str(),
1913 state_data_->report_data->FormatHandle(layout_handle).c_str());
John Zulauf4e7bcb52018-11-02 10:46:30 -06001914 }
1915 return out;
1916};
1917
John Zulauf1d27e0a2018-11-05 10:12:48 -07001918// Loop through the write updates to validate for a push descriptor set, ignoring dstSet
John Zulaufc93c4252019-06-25 09:19:49 -06001919bool CoreChecks::ValidatePushDescriptorsUpdate(const DescriptorSet *push_set, uint32_t write_count,
John Zulaufbd9b3412019-08-22 17:16:11 -06001920 const VkWriteDescriptorSet *p_wds, const char *func_name) const {
John Zulaufd9435c32019-06-05 15:55:36 -06001921 assert(push_set->IsPushDescriptor());
John Zulauf1d27e0a2018-11-05 10:12:48 -07001922 bool skip = false;
1923 for (uint32_t i = 0; i < write_count; i++) {
1924 std::string error_code;
1925 std::string error_str;
John Zulaufc93c4252019-06-25 09:19:49 -06001926 if (!ValidateWriteUpdate(push_set, &p_wds[i], func_name, &error_code, &error_str)) {
John Zulauf1d27e0a2018-11-05 10:12:48 -07001927 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
John Zulaufd9435c32019-06-05 15:55:36 -06001928 HandleToUint64(push_set->GetDescriptorSetLayout()), error_code, "%s failed update validation: %s.",
John Zulauf1d27e0a2018-11-05 10:12:48 -07001929 func_name, error_str.c_str());
1930 }
1931 }
1932 return skip;
1933}
1934
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001935// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001936// If there's an error, update the error_msg string with details and return false, else return true
John Zulaufd9435c32019-06-05 15:55:36 -06001937bool cvdescriptorset::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type, std::string *error_code,
1938 std::string *error_msg) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001939 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001940 auto usage = buffer_node->createInfo.usage;
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001941 const char *error_usage_bit = nullptr;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001942 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001943 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1944 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001945 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00334";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001946 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1947 }
1948 break;
1949 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1950 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001951 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00335";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001952 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1953 }
1954 break;
1955 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1956 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1957 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001958 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00330";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001959 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1960 }
1961 break;
1962 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1963 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1964 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001965 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00331";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001966 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1967 }
1968 break;
1969 default:
1970 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001971 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001972 if (error_usage_bit) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001973 std::stringstream error_str;
Mark Lobodzinski54a67c42019-06-24 14:35:21 -06001974 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask " << std::hex << std::showbase << usage
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001975 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1976 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001977 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001978 return false;
1979 }
1980 return true;
1981}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001982// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1983// 1. buffer is valid
1984// 2. buffer was created with correct usage flags
1985// 3. offset is less than buffer size
1986// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001987// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001988// 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 -06001989bool CoreChecks::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type, const char *func_name,
John Zulaufbd9b3412019-08-22 17:16:11 -06001990 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001991 // First make sure that buffer is valid
John Zulaufc93c4252019-06-25 09:19:49 -06001992 auto buffer_node = GetBufferState(buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07001993 // Any invalid buffer should already be caught by object_tracker
1994 assert(buffer_node);
John Zulaufc93c4252019-06-25 09:19:49 -06001995 if (ValidateMemoryIsBoundToBuffer(buffer_node, func_name, "VUID-VkWriteDescriptorSet-descriptorType-00329")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001996 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00329";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001997 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001998 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001999 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002000 // Verify usage bits
John Zulaufc93c4252019-06-25 09:19:49 -06002001 if (!cvdescriptorset::ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002002 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002003 return false;
2004 }
2005 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002006 if (buffer_info->offset >= buffer_node->createInfo.size) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002007 *error_code = "VUID-VkDescriptorBufferInfo-offset-00340";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002008 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002009 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
2010 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002011 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002012 return false;
2013 }
2014 if (buffer_info->range != VK_WHOLE_SIZE) {
2015 // Range must be VK_WHOLE_SIZE or > 0
2016 if (!buffer_info->range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002017 *error_code = "VUID-VkDescriptorBufferInfo-range-00341";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002018 std::stringstream error_str;
2019 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002020 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002021 return false;
2022 }
2023 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
2024 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002025 *error_code = "VUID-VkDescriptorBufferInfo-range-00342";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002026 std::stringstream error_str;
2027 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
2028 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002029 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002030 return false;
2031 }
2032 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002033 // Check buffer update sizes against device limits
John Zulaufc93c4252019-06-25 09:19:49 -06002034 const auto &limits = phys_dev_props.limits;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002035 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
John Zulaufd9435c32019-06-05 15:55:36 -06002036 auto max_ub_range = limits.maxUniformBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002037 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002038 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002039 std::stringstream error_str;
2040 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2041 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
2042 *error_msg = error_str.str();
2043 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002044 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_ub_range) {
2045 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
2046 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002047 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2048 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002049 << "maxUniformBufferRange (" << max_ub_range << ")";
2050 *error_msg = error_str.str();
2051 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002052 }
2053 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
John Zulaufd9435c32019-06-05 15:55:36 -06002054 auto max_sb_range = limits.maxStorageBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002055 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002056 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002057 std::stringstream error_str;
2058 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2059 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
2060 *error_msg = error_str.str();
2061 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002062 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_sb_range) {
2063 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
2064 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002065 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2066 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002067 << "maxStorageBufferRange (" << max_sb_range << ")";
2068 *error_msg = error_str.str();
2069 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002070 }
2071 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002072 return true;
2073}
Tobin Ehlis300888c2016-05-18 13:43:26 -06002074// Verify that the contents of the update are ok, but don't perform actual update
John Zulaufc93c4252019-06-25 09:19:49 -06002075bool CoreChecks::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set, VkDescriptorType type,
2076 uint32_t index, const char *func_name, std::string *error_code, std::string *error_msg) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002077 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
2078 // for write updates
John Zulaufc93c4252019-06-25 09:19:49 -06002079 using DescriptorClass = cvdescriptorset::DescriptorClass;
2080 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2081 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
2082 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2083 using SamplerDescriptor = cvdescriptorset::SamplerDescriptor;
2084 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2085
2086 auto device_data = this;
John Zulaufd9435c32019-06-05 15:55:36 -06002087 switch (src_set->GetDescriptorFromGlobalIndex(index)->descriptor_class) {
John Zulaufc93c4252019-06-25 09:19:49 -06002088 case DescriptorClass::PlainSampler: {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002089 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufd9435c32019-06-05 15:55:36 -06002090 const auto src_desc = src_set->GetDescriptorFromGlobalIndex(index + di);
Józef Kucia5297e372017-10-13 22:31:34 +02002091 if (!src_desc->updated) continue;
2092 if (!src_desc->IsImmutableSampler()) {
John Zulaufd9435c32019-06-05 15:55:36 -06002093 auto update_sampler = static_cast<const SamplerDescriptor *>(src_desc)->GetSampler();
John Zulaufc93c4252019-06-25 09:19:49 -06002094 if (!ValidateSampler(update_sampler)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002095 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002096 std::stringstream error_str;
2097 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2098 *error_msg = error_str.str();
2099 return false;
2100 }
2101 } else {
2102 // TODO : Warn here
2103 }
2104 }
2105 break;
2106 }
John Zulaufc93c4252019-06-25 09:19:49 -06002107 case DescriptorClass::ImageSampler: {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002108 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufd9435c32019-06-05 15:55:36 -06002109 const auto src_desc = src_set->GetDescriptorFromGlobalIndex(index + di);
Józef Kucia5297e372017-10-13 22:31:34 +02002110 if (!src_desc->updated) continue;
2111 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002112 // First validate sampler
2113 if (!img_samp_desc->IsImmutableSampler()) {
2114 auto update_sampler = img_samp_desc->GetSampler();
John Zulaufc93c4252019-06-25 09:19:49 -06002115 if (!ValidateSampler(update_sampler)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002116 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002117 std::stringstream error_str;
2118 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2119 *error_msg = error_str.str();
2120 return false;
2121 }
2122 } else {
2123 // TODO : Warn here
2124 }
2125 // Validate image
2126 auto image_view = img_samp_desc->GetImageView();
2127 auto image_layout = img_samp_desc->GetImageLayout();
John Zulaufc93c4252019-06-25 09:19:49 -06002128 if (!ValidateImageUpdate(image_view, image_layout, type, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002129 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002130 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002131 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002132 return false;
2133 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002134 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002135 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002136 }
John Zulaufc93c4252019-06-25 09:19:49 -06002137 case DescriptorClass::Image: {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002138 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufd9435c32019-06-05 15:55:36 -06002139 const auto src_desc = src_set->GetDescriptorFromGlobalIndex(index + di);
Józef Kucia5297e372017-10-13 22:31:34 +02002140 if (!src_desc->updated) continue;
2141 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002142 auto image_view = img_desc->GetImageView();
2143 auto image_layout = img_desc->GetImageLayout();
John Zulaufc93c4252019-06-25 09:19:49 -06002144 if (!ValidateImageUpdate(image_view, image_layout, type, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002145 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002146 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002147 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002148 return false;
2149 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002150 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002151 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002152 }
John Zulaufc93c4252019-06-25 09:19:49 -06002153 case DescriptorClass::TexelBuffer: {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002154 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufd9435c32019-06-05 15:55:36 -06002155 const auto src_desc = src_set->GetDescriptorFromGlobalIndex(index + di);
Józef Kucia5297e372017-10-13 22:31:34 +02002156 if (!src_desc->updated) continue;
John Zulaufd9435c32019-06-05 15:55:36 -06002157 auto buffer_view = static_cast<const TexelDescriptor *>(src_desc)->GetBufferView();
2158 auto bv_state = device_data->GetBufferViewState(buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002159 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002160 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002161 std::stringstream error_str;
2162 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2163 *error_msg = error_str.str();
2164 return false;
2165 }
2166 auto buffer = bv_state->create_info.buffer;
John Zulaufc93c4252019-06-25 09:19:49 -06002167 if (!cvdescriptorset::ValidateBufferUsage(GetBufferState(buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002168 std::stringstream error_str;
2169 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
2170 *error_msg = error_str.str();
2171 return false;
2172 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002173 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002174 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002175 }
John Zulaufc93c4252019-06-25 09:19:49 -06002176 case DescriptorClass::GeneralBuffer: {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002177 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufd9435c32019-06-05 15:55:36 -06002178 const auto src_desc = src_set->GetDescriptorFromGlobalIndex(index + di);
Józef Kucia5297e372017-10-13 22:31:34 +02002179 if (!src_desc->updated) continue;
John Zulaufd9435c32019-06-05 15:55:36 -06002180 auto buffer = static_cast<const BufferDescriptor *>(src_desc)->GetBuffer();
John Zulaufc93c4252019-06-25 09:19:49 -06002181 if (!cvdescriptorset::ValidateBufferUsage(GetBufferState(buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002182 std::stringstream error_str;
2183 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
2184 *error_msg = error_str.str();
2185 return false;
2186 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06002187 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002188 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002189 }
John Zulaufc93c4252019-06-25 09:19:49 -06002190 case DescriptorClass::InlineUniform:
2191 case DescriptorClass::AccelerationStructure:
Jeff Bolze54ae892018-09-08 12:16:29 -05002192 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002193 default:
2194 assert(0); // We've already verified update type so should never get here
2195 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002196 }
2197 // All checks passed so update contents are good
2198 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12002199}
Tobin Ehlisee471462016-05-26 11:21:59 -06002200// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002201bool CoreChecks::ValidateAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002202 const cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002203 bool skip = false;
Mark Lobodzinski7804bd42019-03-06 11:28:48 -07002204 auto pool_state = GetDescriptorPoolState(p_alloc_info->descriptorPool);
Tobin Ehlisee471462016-05-26 11:21:59 -06002205
2206 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002207 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07002208 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
John Zulauf1d27e0a2018-11-05 10:12:48 -07002209 if (layout->IsPushDescriptor()) {
John Zulauf5562d062018-01-24 11:54:05 -07002210 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
Dave Houltond8ed0212018-05-16 17:18:24 -06002211 HandleToUint64(p_alloc_info->pSetLayouts[i]), "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-00308",
locke-lunarg9edc2812019-06-17 23:18:52 -06002212 "%s specified at pSetLayouts[%" PRIu32
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002213 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
Lockeca0d9792019-03-03 23:48:13 -07002214 report_data->FormatHandle(p_alloc_info->pSetLayouts[i]).c_str(), i,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002215 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07002216 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002217 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT &&
2218 !(pool_state->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
2219 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
Dave Houltond8ed0212018-05-16 17:18:24 -06002220 0, "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-03044",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002221 "Descriptor set layout create flags and pool create flags mismatch for index (%d)", i);
2222 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002223 }
2224 }
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002225 if (!device_extensions.vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002226 // Track number of descriptorSets allowable in this pool
2227 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002228 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
Dave Houltond8ed0212018-05-16 17:18:24 -06002229 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorSetCount-00306",
locke-lunarg9edc2812019-06-17 23:18:52 -06002230 "Unable to allocate %u descriptorSets from %s"
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002231 ". This pool only has %d descriptorSets remaining.",
Lockeca0d9792019-03-03 23:48:13 -07002232 p_alloc_info->descriptorSetCount, report_data->FormatHandle(pool_state->pool).c_str(),
2233 pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002234 }
2235 // Determine whether descriptor counts are satisfiable
Jeff Bolze54ae892018-09-08 12:16:29 -05002236 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2237 if (ds_data->required_descriptors_by_type.at(it->first) > pool_state->availableDescriptorTypeCount[it->first]) {
Lockeca0d9792019-03-03 23:48:13 -07002238 skip |= log_msg(
2239 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
2240 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307",
locke-lunarg9edc2812019-06-17 23:18:52 -06002241 "Unable to allocate %u descriptors of type %s from %s"
Lockeca0d9792019-03-03 23:48:13 -07002242 ". This pool only has %d descriptors of this type remaining.",
2243 ds_data->required_descriptors_by_type.at(it->first), string_VkDescriptorType(VkDescriptorType(it->first)),
2244 report_data->FormatHandle(pool_state->pool).c_str(), pool_state->availableDescriptorTypeCount[it->first]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002245 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002246 }
2247 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06002248
Jeff Bolzfdf96072018-04-10 14:32:18 -05002249 const auto *count_allocate_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2250
2251 if (count_allocate_info) {
2252 if (count_allocate_info->descriptorSetCount != 0 &&
2253 count_allocate_info->descriptorSetCount != p_alloc_info->descriptorSetCount) {
2254 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, 0,
Dave Houltond8ed0212018-05-16 17:18:24 -06002255 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-descriptorSetCount-03045",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002256 "VkDescriptorSetAllocateInfo::descriptorSetCount (%d) != "
2257 "VkDescriptorSetVariableDescriptorCountAllocateInfoEXT::descriptorSetCount (%d)",
2258 p_alloc_info->descriptorSetCount, count_allocate_info->descriptorSetCount);
2259 }
2260 if (count_allocate_info->descriptorSetCount == p_alloc_info->descriptorSetCount) {
2261 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002262 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002263 if (count_allocate_info->pDescriptorCounts[i] > layout->GetDescriptorCountFromBinding(layout->GetMaxBinding())) {
2264 skip |= log_msg(
2265 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, 0,
Dave Houltond8ed0212018-05-16 17:18:24 -06002266 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-pSetLayouts-03046",
2267 "pDescriptorCounts[%d] = (%d), binding's descriptorCount = (%d)", i,
Jeff Bolzfdf96072018-04-10 14:32:18 -05002268 count_allocate_info->pDescriptorCounts[i], layout->GetDescriptorCountFromBinding(layout->GetMaxBinding()));
2269 }
2270 }
2271 }
2272 }
2273
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002274 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06002275}
John Zulauf48a6a702017-12-22 17:14:54 -07002276
Jeff Bolzdd4cfa12019-08-11 20:57:51 -05002277const BindingReqMap &cvdescriptorset::PrefilterBindRequestMap::FilteredMap(const CMD_BUFFER_STATE &cb_state,
2278 const PIPELINE_STATE &pipeline) {
John Zulauffbf3c202019-07-17 14:57:14 -06002279 if (IsManyDescriptors()) {
John Zulauf48a6a702017-12-22 17:14:54 -07002280 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
John Zulauffbf3c202019-07-17 14:57:14 -06002281 descriptor_set_.FilterBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
2282 return *filtered_map_;
John Zulauf48a6a702017-12-22 17:14:54 -07002283 }
John Zulauffbf3c202019-07-17 14:57:14 -06002284 return orig_map_;
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01002285}
John Zulauf4a015c92019-06-04 09:50:05 -06002286
2287// Starting at offset descriptor of given binding, parse over update_count
2288// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
2289// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
2290// If so, return true. If not, fill in error_msg and return false
2291bool cvdescriptorset::VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator current_binding, uint32_t offset,
2292 uint32_t update_count, const char *type, const VkDescriptorSet set,
2293 std::string *error_msg) {
locke-lunarge46b7782019-09-10 01:44:20 -06002294 bool pass = true;
John Zulauf4a015c92019-06-04 09:50:05 -06002295 // Verify consecutive bindings match (if needed)
2296 auto orig_binding = current_binding;
locke-lunarge46b7782019-09-10 01:44:20 -06002297
2298 while (pass && update_count) {
2299 // First, it's legal to offset beyond your own binding so handle that case
2300 if (offset > 0) {
2301 const auto &index_range = current_binding.GetGlobalIndexRange();
2302 // index_range.start + offset is which descriptor is needed to update. If it > index_range.end, it means the descriptor
2303 // isn't in this binding, maybe in next binding.
2304 if ((index_range.start + offset) >= index_range.end) {
2305 // Advance to next binding, decrement offset by binding size
2306 offset -= current_binding.GetDescriptorCount();
2307 ++current_binding;
2308 // Verify next consecutive binding matches type, stage flags & immutable sampler use and if AtEnd
2309 if (!orig_binding.IsConsistent(current_binding)) {
2310 pass = false;
2311 }
2312 continue;
John Zulauf4a015c92019-06-04 09:50:05 -06002313 }
John Zulauf4a015c92019-06-04 09:50:05 -06002314 }
locke-lunarge46b7782019-09-10 01:44:20 -06002315
2316 update_count -= std::min(update_count, current_binding.GetDescriptorCount() - offset);
2317 if (update_count) {
2318 // Starting offset is beyond the current binding. Check consistency, update counters and advance to the next binding,
2319 // looking for the start point. All bindings (even those skipped) must be consistent with the update and with the
2320 // original binding.
2321 offset = 0;
2322 ++current_binding;
2323 // Verify next consecutive binding matches type, stage flags & immutable sampler use and if AtEnd
2324 if (!orig_binding.IsConsistent(current_binding)) {
2325 pass = false;
2326 }
2327 }
John Zulauf4a015c92019-06-04 09:50:05 -06002328 }
locke-lunarge46b7782019-09-10 01:44:20 -06002329
2330 if (!pass) {
2331 std::stringstream error_str;
2332 error_str << "Attempting " << type;
2333 if (current_binding.Layout()->IsPushDescriptor()) {
2334 error_str << " push descriptors";
2335 } else {
2336 error_str << " descriptor set " << set;
2337 }
2338 error_str << " binding #" << orig_binding.Binding() << " with #" << update_count
2339 << " descriptors being updated but this update oversteps the bounds of this binding and the next binding is "
2340 "not consistent with current binding so this update is invalid.";
2341 *error_msg = error_str.str();
2342 }
2343 return pass;
John Zulauf4a015c92019-06-04 09:50:05 -06002344}
John Zulauf4956fff2019-06-04 16:54:38 -06002345
2346// Validate the state for a given write update but don't actually perform the update
2347// 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 -06002348bool CoreChecks::ValidateWriteUpdate(const DescriptorSet *dest_set, const VkWriteDescriptorSet *update, const char *func_name,
John Zulaufbd9b3412019-08-22 17:16:11 -06002349 std::string *error_code, std::string *error_msg) const {
John Zulauf4956fff2019-06-04 16:54:38 -06002350 const auto dest_layout = dest_set->GetLayout();
2351
2352 // Verify dst layout still valid
2353 if (dest_layout->IsDestroyed()) {
2354 *error_code = "VUID-VkWriteDescriptorSet-dstSet-00320";
2355 string_sprintf(error_msg, "Cannot call %s to perform write update on %s which has been destroyed", func_name,
2356 dest_set->StringifySetAndLayout().c_str());
2357 return false;
2358 }
2359 // Verify dst binding exists
2360 if (!dest_layout->HasBinding(update->dstBinding)) {
2361 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00315";
2362 std::stringstream error_str;
2363 error_str << dest_set->StringifySetAndLayout() << " does not have binding " << update->dstBinding;
2364 *error_msg = error_str.str();
2365 return false;
2366 }
2367
2368 DescriptorSetLayout::ConstBindingIterator dest(dest_layout.get(), update->dstBinding);
2369 // Make sure binding isn't empty
2370 if (0 == dest.GetDescriptorCount()) {
2371 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00316";
2372 std::stringstream error_str;
2373 error_str << dest_set->StringifySetAndLayout() << " cannot updated binding " << update->dstBinding
2374 << " that has 0 descriptors";
2375 *error_msg = error_str.str();
2376 return false;
2377 }
2378
2379 // Verify idle ds
2380 if (dest_set->in_use.load() && !(dest.GetDescriptorBindingFlags() & (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
2381 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
2382 // TODO : Re-using Free Idle error code, need write update idle error code
2383 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
2384 std::stringstream error_str;
2385 error_str << "Cannot call " << func_name << " to perform write update on " << dest_set->StringifySetAndLayout()
2386 << " that is in use by a command buffer";
2387 *error_msg = error_str.str();
2388 return false;
2389 }
2390 // We know that binding is valid, verify update and do update on each descriptor
2391 auto start_idx = dest.GetGlobalIndexRange().start + update->dstArrayElement;
2392 auto type = dest.GetType();
2393 if (type != update->descriptorType) {
2394 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00319";
2395 std::stringstream error_str;
2396 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2397 << " with type " << string_VkDescriptorType(type) << " but update type is "
2398 << string_VkDescriptorType(update->descriptorType);
2399 *error_msg = error_str.str();
2400 return false;
2401 }
John Zulauf4956fff2019-06-04 16:54:38 -06002402 if (type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
2403 if ((update->dstArrayElement % 4) != 0) {
2404 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02219";
2405 std::stringstream error_str;
2406 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2407 << " with "
2408 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
2409 *error_msg = error_str.str();
2410 return false;
2411 }
2412 if ((update->descriptorCount % 4) != 0) {
2413 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02220";
2414 std::stringstream error_str;
2415 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2416 << " with "
2417 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
2418 *error_msg = error_str.str();
2419 return false;
2420 }
2421 const auto *write_inline_info = lvl_find_in_chain<VkWriteDescriptorSetInlineUniformBlockEXT>(update->pNext);
2422 if (!write_inline_info || write_inline_info->dataSize != update->descriptorCount) {
2423 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02221";
2424 std::stringstream error_str;
2425 if (!write_inline_info) {
2426 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #"
2427 << update->dstBinding << " with "
2428 << "VkWriteDescriptorSetInlineUniformBlockEXT missing";
2429 } else {
2430 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #"
2431 << update->dstBinding << " with "
2432 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2433 << " not equal to "
2434 << "VkWriteDescriptorSet descriptorCount " << update->descriptorCount;
2435 }
2436 *error_msg = error_str.str();
2437 return false;
2438 }
2439 // This error is probably unreachable due to the previous two errors
2440 if (write_inline_info && (write_inline_info->dataSize % 4) != 0) {
2441 *error_code = "VUID-VkWriteDescriptorSetInlineUniformBlockEXT-dataSize-02222";
2442 std::stringstream error_str;
2443 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2444 << " with "
2445 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2446 << " not a multiple of 4";
2447 *error_msg = error_str.str();
2448 return false;
2449 }
2450 }
2451 // Verify consecutive bindings match (if needed)
2452 if (!VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator(dest_layout.get(), update->dstBinding),
2453 update->dstArrayElement, update->descriptorCount, "write update to", dest_set->GetSet(),
2454 error_msg)) {
2455 // TODO : Should break out "consecutive binding updates" language into valid usage statements
2456 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
2457 return false;
2458 }
2459 // Update is within bounds and consistent so last step is to validate update contents
John Zulauf459939f2019-06-04 16:49:35 -06002460 if (!VerifyWriteUpdateContents(dest_set, update, start_idx, func_name, error_code, error_msg)) {
John Zulauf4956fff2019-06-04 16:54:38 -06002461 std::stringstream error_str;
2462 error_str << "Write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2463 << " failed with error message: " << error_msg->c_str();
2464 *error_msg = error_str.str();
2465 return false;
2466 }
2467 // All checks passed, update is clean
2468 return true;
2469}
John Zulaufadb3f542019-06-04 17:01:00 -06002470
2471// Verify that the contents of the update are ok, but don't perform actual update
John Zulaufc93c4252019-06-25 09:19:49 -06002472bool CoreChecks::VerifyWriteUpdateContents(const DescriptorSet *dest_set, const VkWriteDescriptorSet *update, const uint32_t index,
John Zulaufbd9b3412019-08-22 17:16:11 -06002473 const char *func_name, std::string *error_code, std::string *error_msg) const {
John Zulaufc93c4252019-06-25 09:19:49 -06002474 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
2475 using SamplerDescriptor = cvdescriptorset::SamplerDescriptor;
2476
John Zulaufadb3f542019-06-04 17:01:00 -06002477 switch (update->descriptorType) {
2478 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
2479 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2480 // Validate image
2481 auto image_view = update->pImageInfo[di].imageView;
2482 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufc93c4252019-06-25 09:19:49 -06002483 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, func_name, error_code, error_msg)) {
John Zulaufadb3f542019-06-04 17:01:00 -06002484 std::stringstream error_str;
2485 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
2486 << error_msg->c_str();
2487 *error_msg = error_str.str();
2488 return false;
2489 }
John Zulaufc93c4252019-06-25 09:19:49 -06002490 if (device_extensions.vk_khr_sampler_ycbcr_conversion) {
John Zulaufadb3f542019-06-04 17:01:00 -06002491 ImageSamplerDescriptor *desc = (ImageSamplerDescriptor *)dest_set->GetDescriptorFromGlobalIndex(index + di);
2492 if (desc->IsImmutableSampler()) {
John Zulaufc93c4252019-06-25 09:19:49 -06002493 auto sampler_state = GetSamplerState(desc->GetSampler());
2494 auto iv_state = GetImageViewState(image_view);
John Zulaufadb3f542019-06-04 17:01:00 -06002495 if (iv_state && sampler_state) {
2496 if (iv_state->samplerConversion != sampler_state->samplerConversion) {
2497 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01948";
2498 std::stringstream error_str;
2499 error_str << "Attempted write update to combined image sampler and image view and sampler ycbcr "
2500 "conversions are not identical, sampler: "
2501 << desc->GetSampler() << " image view: " << iv_state->image_view << ".";
2502 *error_msg = error_str.str();
2503 return false;
2504 }
2505 }
2506 } else {
John Zulaufc93c4252019-06-25 09:19:49 -06002507 auto iv_state = GetImageViewState(image_view);
John Zulaufadb3f542019-06-04 17:01:00 -06002508 if (iv_state && (iv_state->samplerConversion != VK_NULL_HANDLE)) {
Shannon McPhersonf7d9cf62019-06-26 09:23:57 -06002509 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02738";
John Zulaufadb3f542019-06-04 17:01:00 -06002510 std::stringstream error_str;
2511 error_str << "Because dstSet (" << update->dstSet << ") is bound to image view ("
2512 << iv_state->image_view
2513 << ") that includes a YCBCR conversion, it must have been allocated with a layout that "
2514 "includes an immutable sampler.";
2515 *error_msg = error_str.str();
2516 return false;
2517 }
2518 }
2519 }
2520 }
2521 }
2522 // fall through
2523 case VK_DESCRIPTOR_TYPE_SAMPLER: {
2524 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2525 SamplerDescriptor *desc = (SamplerDescriptor *)dest_set->GetDescriptorFromGlobalIndex(index + di);
2526 if (!desc->IsImmutableSampler()) {
John Zulaufc93c4252019-06-25 09:19:49 -06002527 if (!ValidateSampler(update->pImageInfo[di].sampler)) {
John Zulaufadb3f542019-06-04 17:01:00 -06002528 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
2529 std::stringstream error_str;
2530 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
2531 << update->pImageInfo[di].sampler << ".";
2532 *error_msg = error_str.str();
2533 return false;
2534 }
2535 } else {
2536 // TODO : Warn here
2537 }
2538 }
2539 break;
2540 }
2541 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2542 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2543 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2544 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2545 auto image_view = update->pImageInfo[di].imageView;
2546 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufc93c4252019-06-25 09:19:49 -06002547 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, func_name, error_code, error_msg)) {
John Zulaufadb3f542019-06-04 17:01:00 -06002548 std::stringstream error_str;
2549 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
2550 *error_msg = error_str.str();
2551 return false;
2552 }
2553 }
2554 break;
2555 }
2556 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2557 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
2558 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2559 auto buffer_view = update->pTexelBufferView[di];
John Zulaufc93c4252019-06-25 09:19:49 -06002560 auto bv_state = GetBufferViewState(buffer_view);
John Zulaufadb3f542019-06-04 17:01:00 -06002561 if (!bv_state) {
2562 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
2563 std::stringstream error_str;
2564 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2565 *error_msg = error_str.str();
2566 return false;
2567 }
2568 auto buffer = bv_state->create_info.buffer;
John Zulaufc93c4252019-06-25 09:19:49 -06002569 auto buffer_state = GetBufferState(buffer);
John Zulaufadb3f542019-06-04 17:01:00 -06002570 // Verify that buffer underlying the view hasn't been destroyed prematurely
2571 if (!buffer_state) {
2572 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
2573 std::stringstream error_str;
2574 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
2575 << ") has been destroyed: " << error_msg->c_str();
2576 *error_msg = error_str.str();
2577 return false;
John Zulaufc93c4252019-06-25 09:19:49 -06002578 } else if (!cvdescriptorset::ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
John Zulaufadb3f542019-06-04 17:01:00 -06002579 std::stringstream error_str;
2580 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
2581 *error_msg = error_str.str();
2582 return false;
2583 }
2584 }
2585 break;
2586 }
2587 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2588 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2589 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2590 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2591 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufc93c4252019-06-25 09:19:49 -06002592 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, func_name, error_code, error_msg)) {
John Zulaufadb3f542019-06-04 17:01:00 -06002593 std::stringstream error_str;
2594 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
2595 *error_msg = error_str.str();
2596 return false;
2597 }
2598 }
2599 break;
2600 }
2601 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
2602 break;
2603 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
2604 // XXX TODO
2605 break;
2606 default:
2607 assert(0); // We've already verified update type so should never get here
2608 break;
2609 }
2610 // All checks passed so update contents are good
2611 return true;
2612}