blob: eff8dd4b657b891b380f9d45927f99f5f170b23b [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 Zulaufb6d71202017-12-22 16:47:09 -0700120 binding_to_global_index_range_map_.reserve(binding_count_);
121 // Vector order is finalized so create maps of bindings to descriptors and descriptors to indices
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700122 for (uint32_t i = 0; i < binding_count_; ++i) {
123 auto binding_num = bindings_[i].binding;
John Zulaufc483f442017-12-15 14:02:06 -0700124 auto final_index = global_index + bindings_[i].descriptorCount;
125 binding_to_global_index_range_map_[binding_num] = IndexRange(global_index, final_index);
John Zulaufb6d71202017-12-22 16:47:09 -0700126 if (final_index != global_index) {
127 global_start_to_index_map_[global_index] = i;
128 }
John Zulaufc483f442017-12-15 14:02:06 -0700129 global_index = final_index;
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700130 }
John Zulaufb6d71202017-12-22 16:47:09 -0700131
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700132 // Now create dyn offset array mapping for any dynamic descriptors
133 uint32_t dyn_array_idx = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700134 binding_to_dynamic_array_idx_map_.reserve(binding_to_dyn_count.size());
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700135 for (const auto &bc_pair : binding_to_dyn_count) {
136 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
137 dyn_array_idx += bc_pair.second;
138 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600139}
Tobin Ehlis154c2692016-10-25 09:36:53 -0600140
John Zulaufd47d0612018-02-16 13:00:34 -0700141size_t cvdescriptorset::DescriptorSetLayoutDef::hash() const {
142 hash_util::HashCombiner hc;
143 hc << flags_;
144 hc.Combine(bindings_);
John Zulauf223b69d2018-11-09 16:00:59 -0700145 hc.Combine(binding_flags_);
John Zulaufd47d0612018-02-16 13:00:34 -0700146 return hc.Value();
147}
148//
149
John Zulauf1f8174b2018-02-16 12:58:37 -0700150// Return valid index or "end" i.e. binding_count_;
151// The asserts in "Get" are reduced to the set where no valid answer(like null or 0) could be given
152// Common code for all binding lookups.
153uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromBinding(uint32_t binding) const {
154 const auto &bi_itr = binding_to_index_map_.find(binding);
155 if (bi_itr != binding_to_index_map_.cend()) return bi_itr->second;
156 return GetBindingCount();
157}
158VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorSetLayoutBindingPtrFromIndex(
159 const uint32_t index) const {
160 if (index >= bindings_.size()) return nullptr;
161 return bindings_[index].ptr();
162}
163// Return descriptorCount for given index, 0 if index is unavailable
164uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorCountFromIndex(const uint32_t index) const {
165 if (index >= bindings_.size()) return 0;
166 return bindings_[index].descriptorCount;
167}
168// For the given index, return descriptorType
169VkDescriptorType cvdescriptorset::DescriptorSetLayoutDef::GetTypeFromIndex(const uint32_t index) const {
170 assert(index < bindings_.size());
171 if (index < bindings_.size()) return bindings_[index].descriptorType;
172 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
173}
174// For the given index, return stageFlags
175VkShaderStageFlags cvdescriptorset::DescriptorSetLayoutDef::GetStageFlagsFromIndex(const uint32_t index) const {
176 assert(index < bindings_.size());
177 if (index < bindings_.size()) return bindings_[index].stageFlags;
178 return VkShaderStageFlags(0);
179}
Jeff Bolzfdf96072018-04-10 14:32:18 -0500180// Return binding flags for given index, 0 if index is unavailable
181VkDescriptorBindingFlagsEXT cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorBindingFlagsFromIndex(
182 const uint32_t index) const {
183 if (index >= binding_flags_.size()) return 0;
184 return binding_flags_[index];
185}
John Zulauf1f8174b2018-02-16 12:58:37 -0700186
187// For the given global index, return index
188uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromGlobalIndex(const uint32_t global_index) const {
189 auto start_it = global_start_to_index_map_.upper_bound(global_index);
190 uint32_t index = binding_count_;
191 assert(start_it != global_start_to_index_map_.cbegin());
192 if (start_it != global_start_to_index_map_.cbegin()) {
193 --start_it;
194 index = start_it->second;
195#ifndef NDEBUG
196 const auto &range = GetGlobalIndexRangeFromBinding(bindings_[index].binding);
197 assert(range.start <= global_index && global_index < range.end);
198#endif
199 }
200 return index;
201}
202
203// For the given binding, return the global index range
204// As start and end are often needed in pairs, get both with a single hash lookup.
205const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromBinding(
206 const uint32_t binding) const {
207 assert(binding_to_global_index_range_map_.count(binding));
208 // In error case max uint32_t so index is out of bounds to break ASAP
209 const static IndexRange kInvalidRange = {0xFFFFFFFF, 0xFFFFFFFF};
210 const auto &range_it = binding_to_global_index_range_map_.find(binding);
211 if (range_it != binding_to_global_index_range_map_.end()) {
212 return range_it->second;
213 }
214 return kInvalidRange;
215}
216
217// For given binding, return ptr to ImmutableSampler array
218VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
219 const auto &bi_itr = binding_to_index_map_.find(binding);
220 if (bi_itr != binding_to_index_map_.end()) {
221 return bindings_[bi_itr->second].pImmutableSamplers;
222 }
223 return nullptr;
224}
225// Move to next valid binding having a non-zero binding count
226uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetNextValidBinding(const uint32_t binding) const {
227 auto it = non_empty_bindings_.upper_bound(binding);
228 assert(it != non_empty_bindings_.cend());
229 if (it != non_empty_bindings_.cend()) return *it;
230 return GetMaxBinding() + 1;
231}
232// For given index, return ptr to ImmutableSampler array
233VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
234 if (index < bindings_.size()) {
235 return bindings_[index].pImmutableSamplers;
236 }
237 return nullptr;
238}
239// If our layout is compatible with rh_ds_layout, return true,
240// else return false and fill in error_msg will description of what causes incompatibility
241bool cvdescriptorset::DescriptorSetLayout::IsCompatible(DescriptorSetLayout const *const rh_ds_layout,
242 std::string *error_msg) const {
243 // Trivial case
244 if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) return true;
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600245 if (GetLayoutDef() == rh_ds_layout->GetLayoutDef()) return true;
John Zulaufd47d0612018-02-16 13:00:34 -0700246 bool detailed_compat_check =
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600247 GetLayoutDef()->IsCompatible(layout_, rh_ds_layout->GetDescriptorSetLayout(), rh_ds_layout->GetLayoutDef(), error_msg);
John Zulaufd47d0612018-02-16 13:00:34 -0700248 // The detailed check should never tell us mismatching DSL are compatible
249 assert(!detailed_compat_check);
250 return detailed_compat_check;
John Zulauf1f8174b2018-02-16 12:58:37 -0700251}
252
John Zulaufdf3c5c12018-03-06 16:44:43 -0700253// Do a detailed compatibility check of this def (referenced by ds_layout), vs. the rhs (layout and def)
254// Should only be called if trivial accept has failed, and in that context should return false.
John Zulauf1f8174b2018-02-16 12:58:37 -0700255bool cvdescriptorset::DescriptorSetLayoutDef::IsCompatible(VkDescriptorSetLayout ds_layout, VkDescriptorSetLayout rh_ds_layout,
256 DescriptorSetLayoutDef const *const rh_ds_layout_def,
257 std::string *error_msg) const {
258 if (descriptor_count_ != rh_ds_layout_def->descriptor_count_) {
259 std::stringstream error_str;
260 error_str << "DescriptorSetLayout " << ds_layout << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
261 << rh_ds_layout << ", which comes from pipelineLayout, has " << rh_ds_layout_def->descriptor_count_
262 << " descriptors.";
263 *error_msg = error_str.str();
264 return false; // trivial fail case
265 }
John Zulaufd47d0612018-02-16 13:00:34 -0700266
John Zulauf1f8174b2018-02-16 12:58:37 -0700267 // Descriptor counts match so need to go through bindings one-by-one
268 // and verify that type and stageFlags match
269 for (auto binding : bindings_) {
270 // TODO : Do we also need to check immutable samplers?
271 // VkDescriptorSetLayoutBinding *rh_binding;
272 if (binding.descriptorCount != rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding)) {
273 std::stringstream error_str;
274 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has a descriptorCount of "
275 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
276 << rh_ds_layout << ", which comes from pipelineLayout, has a descriptorCount of "
277 << rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding);
278 *error_msg = error_str.str();
279 return false;
280 } else if (binding.descriptorType != rh_ds_layout_def->GetTypeFromBinding(binding.binding)) {
281 std::stringstream error_str;
282 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " is type '"
283 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
284 << " for DescriptorSetLayout " << rh_ds_layout << ", which comes from pipelineLayout, is type '"
285 << string_VkDescriptorType(rh_ds_layout_def->GetTypeFromBinding(binding.binding)) << "'";
286 *error_msg = error_str.str();
287 return false;
288 } else if (binding.stageFlags != rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding)) {
289 std::stringstream error_str;
290 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has stageFlags "
291 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout " << rh_ds_layout
292 << ", which comes from pipelineLayout, has stageFlags "
293 << rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding);
294 *error_msg = error_str.str();
295 return false;
296 }
297 }
298 return true;
299}
300
301bool cvdescriptorset::DescriptorSetLayoutDef::IsNextBindingConsistent(const uint32_t binding) const {
302 if (!binding_to_index_map_.count(binding + 1)) return false;
303 auto const &bi_itr = binding_to_index_map_.find(binding);
304 if (bi_itr != binding_to_index_map_.end()) {
305 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
306 if (next_bi_itr != binding_to_index_map_.end()) {
307 auto type = bindings_[bi_itr->second].descriptorType;
308 auto stage_flags = bindings_[bi_itr->second].stageFlags;
309 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500310 auto flags = binding_flags_[bi_itr->second];
John Zulauf1f8174b2018-02-16 12:58:37 -0700311 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
312 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
Jeff Bolzfdf96072018-04-10 14:32:18 -0500313 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false)) ||
314 (flags != binding_flags_[next_bi_itr->second])) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700315 return false;
316 }
317 return true;
318 }
319 }
320 return false;
321}
John Zulauf1f8174b2018-02-16 12:58:37 -0700322
323// The DescriptorSetLayout stores the per handle data for a descriptor set layout, and references the common defintion for the
324// handle invariant portion
325cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
326 const VkDescriptorSetLayout layout)
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600327 : layout_(layout), layout_destroyed_(false), layout_id_(GetCanonicalId(p_create_info)) {}
John Zulauf1f8174b2018-02-16 12:58:37 -0700328
Tobin Ehlis154c2692016-10-25 09:36:53 -0600329// Validate descriptor set layout create info
Jeff Bolzfdf96072018-04-10 14:32:18 -0500330bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(
331 const debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *create_info, const bool push_descriptor_ext,
332 const uint32_t max_push_descriptors, const bool descriptor_indexing_ext,
Jeff Bolze54ae892018-09-08 12:16:29 -0500333 const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features,
334 const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features,
335 const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props) {
Tobin Ehlis154c2692016-10-25 09:36:53 -0600336 bool skip = false;
337 std::unordered_set<uint32_t> bindings;
John Zulauf0fdeab32018-01-23 11:27:35 -0700338 uint64_t total_descriptors = 0;
339
Jeff Bolzfdf96072018-04-10 14:32:18 -0500340 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(create_info->pNext);
341
342 const bool push_descriptor_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR);
John Zulauf0fdeab32018-01-23 11:27:35 -0700343 if (push_descriptor_set && !push_descriptor_ext) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600344 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 -0600345 kVUID_Core_DrawState_ExtensionNotEnabled,
Mark Lobodzinskifb5a3e62018-04-13 10:46:48 -0600346 "Attempted to use %s in %s but its required extension %s has not been enabled.\n",
John Zulauf0fdeab32018-01-23 11:27:35 -0700347 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR", "VkDescriptorSetLayoutCreateInfo::flags",
348 VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
349 }
350
Jeff Bolzfdf96072018-04-10 14:32:18 -0500351 const bool update_after_bind_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT);
352 if (update_after_bind_set && !descriptor_indexing_ext) {
353 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 -0600354 kVUID_Core_DrawState_ExtensionNotEnabled,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500355 "Attemped to use %s in %s but its required extension %s has not been enabled.\n",
356 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT", "VkDescriptorSetLayoutCreateInfo::flags",
357 VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
358 }
359
John Zulauf0fdeab32018-01-23 11:27:35 -0700360 auto valid_type = [push_descriptor_set](const VkDescriptorType type) {
361 return !push_descriptor_set ||
Dave Houlton142c4cb2018-10-17 15:04:41 -0600362 ((type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && (type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) &&
Jeff Bolze54ae892018-09-08 12:16:29 -0500363 (type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT));
John Zulauf0fdeab32018-01-23 11:27:35 -0700364 };
365
Jeff Bolzfdf96072018-04-10 14:32:18 -0500366 uint32_t max_binding = 0;
367
Tobin Ehlis154c2692016-10-25 09:36:53 -0600368 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
John Zulauf0fdeab32018-01-23 11:27:35 -0700369 const auto &binding_info = create_info->pBindings[i];
Jeff Bolzfdf96072018-04-10 14:32:18 -0500370 max_binding = std::max(max_binding, binding_info.binding);
371
John Zulauf0fdeab32018-01-23 11:27:35 -0700372 if (!bindings.insert(binding_info.binding).second) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600373 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 -0600374 "VUID-VkDescriptorSetLayoutCreateInfo-binding-00279",
375 "duplicated binding number in VkDescriptorSetLayoutBinding.");
Tobin Ehlis154c2692016-10-25 09:36:53 -0600376 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700377 if (!valid_type(binding_info.descriptorType)) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600378 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 -0600379 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
380 ? "VUID-VkDescriptorSetLayoutCreateInfo-flags-02208"
381 : "VUID-VkDescriptorSetLayoutCreateInfo-flags-00280",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600382 "invalid type %s ,for push descriptors in VkDescriptorSetLayoutBinding entry %" PRIu32 ".",
383 string_VkDescriptorType(binding_info.descriptorType), i);
John Zulauf0fdeab32018-01-23 11:27:35 -0700384 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500385
386 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
387 if ((binding_info.descriptorCount % 4) != 0) {
388 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
389 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02209",
Dave Houlton142c4cb2018-10-17 15:04:41 -0600390 "descriptorCount =(%" PRIu32 ") must be a multiple of 4", binding_info.descriptorCount);
Jeff Bolze54ae892018-09-08 12:16:29 -0500391 }
392 if (binding_info.descriptorCount > inline_uniform_block_props->maxInlineUniformBlockSize) {
393 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
394 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02210",
395 "descriptorCount =(%" PRIu32 ") must be less than or equal to maxInlineUniformBlockSize",
396 binding_info.descriptorCount);
397 }
398 }
399
John Zulauf0fdeab32018-01-23 11:27:35 -0700400 total_descriptors += binding_info.descriptorCount;
Tobin Ehlis154c2692016-10-25 09:36:53 -0600401 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700402
Jeff Bolzfdf96072018-04-10 14:32:18 -0500403 if (flags_create_info) {
404 if (flags_create_info->bindingCount != 0 && flags_create_info->bindingCount != create_info->bindingCount) {
405 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 -0600406 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-bindingCount-03002",
Jeff Bolzfdf96072018-04-10 14:32:18 -0500407 "VkDescriptorSetLayoutCreateInfo::bindingCount (%d) != "
408 "VkDescriptorSetLayoutBindingFlagsCreateInfoEXT::bindingCount (%d)",
409 create_info->bindingCount, flags_create_info->bindingCount);
410 }
411
412 if (flags_create_info->bindingCount == create_info->bindingCount) {
413 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
414 const auto &binding_info = create_info->pBindings[i];
415
416 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) {
417 if (!update_after_bind_set) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600418 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
419 "VUID-VkDescriptorSetLayoutCreateInfo-flags-03000",
420 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500421 }
422
423 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER &&
424 !descriptor_indexing_features->descriptorBindingUniformBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600425 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
426 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
427 "descriptorBindingUniformBufferUpdateAfterBind-03005",
428 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500429 }
430 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
431 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
432 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) &&
433 !descriptor_indexing_features->descriptorBindingSampledImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600434 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
435 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
436 "descriptorBindingSampledImageUpdateAfterBind-03006",
437 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500438 }
439 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE &&
440 !descriptor_indexing_features->descriptorBindingStorageImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600441 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
442 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
443 "descriptorBindingStorageImageUpdateAfterBind-03007",
444 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500445 }
446 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER &&
447 !descriptor_indexing_features->descriptorBindingStorageBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600448 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
449 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
450 "descriptorBindingStorageBufferUpdateAfterBind-03008",
451 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500452 }
453 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
454 !descriptor_indexing_features->descriptorBindingUniformTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600455 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
456 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
457 "descriptorBindingUniformTexelBufferUpdateAfterBind-03009",
458 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500459 }
460 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER &&
461 !descriptor_indexing_features->descriptorBindingStorageTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600462 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
463 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
464 "descriptorBindingStorageTexelBufferUpdateAfterBind-03010",
465 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500466 }
467 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT ||
468 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
469 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600470 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
471 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-None-03011",
472 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500473 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500474
475 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT &&
476 !inline_uniform_block_features->descriptorBindingInlineUniformBlockUpdateAfterBind) {
477 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 -0600478 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
479 "descriptorBindingInlineUniformBlockUpdateAfterBind-02211",
480 "Invalid flags (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) for "
481 "VkDescriptorSetLayoutBinding entry %" PRIu32
482 " with descriptorBindingInlineUniformBlockUpdateAfterBind not enabled",
483 i);
Jeff Bolze54ae892018-09-08 12:16:29 -0500484 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500485 }
486
487 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT) {
488 if (!descriptor_indexing_features->descriptorBindingUpdateUnusedWhilePending) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600489 skip |= log_msg(
490 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
491 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingUpdateUnusedWhilePending-03012",
492 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500493 }
494 }
495
496 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) {
497 if (!descriptor_indexing_features->descriptorBindingPartiallyBound) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600498 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
499 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingPartiallyBound-03013",
500 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500501 }
502 }
503
504 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT) {
505 if (binding_info.binding != max_binding) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600506 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
507 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03004",
508 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500509 }
510
511 if (!descriptor_indexing_features->descriptorBindingVariableDescriptorCount) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600512 skip |= log_msg(
513 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
514 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingVariableDescriptorCount-03014",
515 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500516 }
517 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
518 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600519 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
520 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03015",
521 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500522 }
523 }
524
525 if (push_descriptor_set &&
526 (flags_create_info->pBindingFlags[i] &
527 (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
528 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT))) {
529 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 -0600530 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-flags-03003",
531 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500532 }
533 }
534 }
535 }
536
John Zulauf0fdeab32018-01-23 11:27:35 -0700537 if ((push_descriptor_set) && (total_descriptors > max_push_descriptors)) {
538 const char *undefined = push_descriptor_ext ? "" : " -- undefined";
Dave Houltond8ed0212018-05-16 17:18:24 -0600539 skip |=
540 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
541 "VUID-VkDescriptorSetLayoutCreateInfo-flags-00281",
542 "for push descriptor, total descriptor count in layout (%" PRIu64
543 ") must not be greater than VkPhysicalDevicePushDescriptorPropertiesKHR::maxPushDescriptors (%" PRIu32 "%s).",
544 total_descriptors, max_push_descriptors, undefined);
John Zulauf0fdeab32018-01-23 11:27:35 -0700545 }
546
Tobin Ehlis154c2692016-10-25 09:36:53 -0600547 return skip;
548}
549
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600550cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
551 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
552
Tobin Ehlis93f22372016-10-12 14:34:12 -0600553cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500554 const std::shared_ptr<DescriptorSetLayout const> &layout, uint32_t variable_count,
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700555 CoreChecks *dev_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700556 : some_update_(false),
557 set_(set),
558 pool_state_(nullptr),
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600559 p_layout_(layout),
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700560 device_data_(dev_data),
Mark Lobodzinski57a44272019-02-27 12:40:50 -0700561 limits_(dev_data->phys_dev_props.limits),
Jeff Bolzfdf96072018-04-10 14:32:18 -0500562 variable_count_(variable_count) {
Mark Lobodzinski7804bd42019-03-06 11:28:48 -0700563 pool_state_ = dev_data->GetDescriptorPoolState(pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600564 // Foreach binding, create default descriptors of given type
John Zulaufb6d71202017-12-22 16:47:09 -0700565 descriptors_.reserve(p_layout_->GetTotalDescriptorCount());
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600566 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
567 auto type = p_layout_->GetTypeFromIndex(i);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600568 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700569 case VK_DESCRIPTOR_TYPE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600570 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
571 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600572 if (immut_sampler) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700573 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600574 some_update_ = true; // Immutable samplers are updated at creation
575 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700576 descriptors_.emplace_back(new SamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700577 }
578 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600579 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700580 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600581 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
582 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600583 if (immut) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700584 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600585 some_update_ = true; // Immutable samplers are updated at creation
586 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700587 descriptors_.emplace_back(new ImageSamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700588 }
589 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600590 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700591 // ImageDescriptors
592 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
593 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
594 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600595 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700596 descriptors_.emplace_back(new ImageDescriptor(type));
597 break;
598 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
599 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600600 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700601 descriptors_.emplace_back(new TexelDescriptor(type));
602 break;
603 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
604 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
605 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
606 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600607 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700608 descriptors_.emplace_back(new BufferDescriptor(type));
609 break;
Jeff Bolze54ae892018-09-08 12:16:29 -0500610 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
611 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
612 descriptors_.emplace_back(new InlineUniformDescriptor(type));
613 break;
Eric Werness30127fd2018-10-31 21:01:03 -0700614 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -0500615 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
616 descriptors_.emplace_back(new AccelerationStructureDescriptor(type));
617 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700618 default:
619 assert(0); // Bad descriptor type specified
620 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600621 }
622 }
623}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600624
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700625cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200626
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600627static std::string StringDescriptorReqViewType(descriptor_req req) {
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700628 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200629 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
630 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700631 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700632 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200633 }
634 }
635
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700636 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700637
638 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200639}
640
Chris Forbesda01e8d2018-08-27 15:36:57 -0700641static char const *StringDescriptorReqComponentType(descriptor_req req) {
642 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_SINT) return "SINT";
643 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_UINT) return "UINT";
644 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT) return "FLOAT";
645 return "(none)";
646}
647
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600648// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600649bool cvdescriptorset::DescriptorSet::IsCompatible(DescriptorSetLayout const *const layout, std::string *error) const {
650 return layout->IsCompatible(p_layout_.get(), error);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600651}
Chris Forbes57989132016-07-26 17:06:10 +1200652
Chris Forbesda01e8d2018-08-27 15:36:57 -0700653static unsigned DescriptorRequirementsBitsFromFormat(VkFormat fmt) {
654 if (FormatIsSInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
655 if (FormatIsUInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
656 if (FormatIsDepthAndStencil(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT | DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
657 if (fmt == VK_FORMAT_UNDEFINED) return 0;
658 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
659 return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
660}
661
Tobin Ehlis3066db62016-08-22 08:12:23 -0600662// 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 -0600663// This includes validating that all descriptors in the given bindings are updated,
664// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
665// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600666bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600667 const std::vector<uint32_t> &dynamic_offsets, CMD_BUFFER_STATE *cb_node,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600668 const char *caller, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200669 for (auto binding_pair : bindings) {
670 auto binding = binding_pair.first;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600671 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600672 std::stringstream error_str;
673 error_str << "Attempting to validate DrawState for binding #" << binding
674 << " which is an invalid binding for this descriptor set.";
675 *error = error_str.str();
676 return false;
677 }
John Zulaufc483f442017-12-15 14:02:06 -0700678 IndexRange index_range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700679 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Jeff Bolzfdf96072018-04-10 14:32:18 -0500680
681 if (IsVariableDescriptorCount(binding)) {
682 // Only validate the first N descriptors if it uses variable_count
683 index_range.end = index_range.start + GetVariableDescriptorCount();
684 }
685
John Zulaufc483f442017-12-15 14:02:06 -0700686 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
Lockeb994adf2019-03-29 23:52:31 -0600687 uint32_t index = i - index_range.start;
688
Tobin Ehlisda77de72018-12-13 08:53:28 -0700689 if ((p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
690 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) ||
Jeff Bolze54ae892018-09-08 12:16:29 -0500691 descriptors_[i]->GetClass() == InlineUniform) {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500692 // Can't validate the descriptor because it may not have been updated,
693 // or the view could have been destroyed
694 continue;
695 } else if (!descriptors_[i]->updated) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700696 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600697 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700698 << " is being used in draw but has not been updated.";
699 *error = error_str.str();
700 return false;
701 } else {
702 auto descriptor_class = descriptors_[i]->GetClass();
703 if (descriptor_class == GeneralBuffer) {
704 // Verify that buffers are valid
705 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Mark Lobodzinski6ed74142019-03-06 11:35:39 -0700706 auto buffer_node = device_data_->GetBufferState(buffer);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700707 if (!buffer_node) {
708 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600709 error_str << "Descriptor in binding #" << binding << " index " << index << " references invalid buffer "
710 << buffer << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700711 *error = error_str.str();
712 return false;
John Zulauf48a6a702017-12-22 17:14:54 -0700713 } else if (!buffer_node->sparse) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700714 for (auto mem_binding : buffer_node->GetBoundMemory()) {
Mark Lobodzinski0a41e0e2019-04-25 12:12:40 -0600715 if (!device_data_->GetDevMemState(mem_binding)) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700716 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600717 error_str << "Descriptor in binding #" << binding << " index " << index << " uses buffer " << buffer
718 << " that references invalid memory " << mem_binding << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700719 *error = error_str.str();
Tobin Ehlisc8266452017-04-07 12:20:30 -0600720 return false;
721 }
722 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700723 }
724 if (descriptors_[i]->IsDynamic()) {
725 // Validate that dynamic offsets are within the buffer
726 auto buffer_size = buffer_node->createInfo.size;
727 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
728 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
729 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
730 if (VK_WHOLE_SIZE == range) {
731 if ((dyn_offset + desc_offset) > buffer_size) {
732 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600733 error_str << "Dynamic descriptor in binding #" << binding << " index " << index << " uses buffer "
734 << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
735 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
736 << buffer_size << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700737 *error = error_str.str();
738 return false;
739 }
740 } else {
741 if ((dyn_offset + desc_offset + range) > buffer_size) {
742 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600743 error_str << "Dynamic descriptor in binding #" << binding << " index " << index << " uses buffer "
744 << buffer << " with dynamic offset " << dyn_offset << " combined with offset "
745 << desc_offset << " and range " << range << " that oversteps the buffer size of "
746 << buffer_size << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700747 *error = error_str.str();
748 return false;
749 }
750 }
751 }
752 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
753 VkImageView image_view;
754 VkImageLayout image_layout;
755 if (descriptor_class == ImageSampler) {
756 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
757 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
758 } else {
759 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
760 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
761 }
762 auto reqs = binding_pair.second;
763
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -0700764 auto image_view_state = device_data_->GetImageViewState(image_view);
Tobin Ehlis836a1372017-07-14 11:25:21 -0600765 if (nullptr == image_view_state) {
766 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
767 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
768 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600769 error_str << "Descriptor in binding #" << binding << " index " << index << " is using imageView "
770 << image_view << " that has been destroyed.";
Tobin Ehlis836a1372017-07-14 11:25:21 -0600771 *error = error_str.str();
772 return false;
773 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700774 auto image_view_ci = image_view_state->create_info;
775
776 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
777 // bad view type
778 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600779 error_str << "Descriptor in binding #" << binding << " index " << index
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600780 << " requires an image view of type " << StringDescriptorReqViewType(reqs) << " but got "
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700781 << string_VkImageViewType(image_view_ci.viewType) << ".";
782 *error = error_str.str();
783 return false;
784 }
785
Chris Forbesda01e8d2018-08-27 15:36:57 -0700786 auto format_bits = DescriptorRequirementsBitsFromFormat(image_view_ci.format);
787 if (!(reqs & format_bits)) {
788 // bad component type
789 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600790 error_str << "Descriptor in binding #" << binding << " index " << index << " requires "
Chris Forbesda01e8d2018-08-27 15:36:57 -0700791 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
792 << string_VkFormat(image_view_ci.format) << ".";
793 *error = error_str.str();
794 return false;
795 }
796
Mark Lobodzinski8ddc23f2019-03-06 11:48:49 -0700797 auto image_node = device_data_->GetImageState(image_view_ci.image);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700798 assert(image_node);
799 // Verify Image Layout
John Zulauff660ad62019-03-23 07:16:05 -0600800 // No "invalid layout" VUID required for this call, since the optimal_layout parameter is UNDEFINED.
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700801 bool hit_error = false;
John Zulauff660ad62019-03-23 07:16:05 -0600802 device_data_->VerifyImageLayout(cb_node, image_node, image_view_state->normalized_subresource_range,
John Zulaufabcc8292019-04-08 18:07:44 -0600803 image_view_ci.subresourceRange.aspectMask, image_layout,
804 VK_IMAGE_LAYOUT_UNDEFINED, caller, kVUIDUndefined,
John Zulauff660ad62019-03-23 07:16:05 -0600805 "VUID-VkDescriptorImageInfo-imageLayout-00344", &hit_error);
806 if (hit_error) {
807 *error =
808 "Image layout specified at vkUpdateDescriptorSet* or vkCmdPushDescriptorSet* time "
809 "doesn't match actual image layout at time descriptor is used. See previous error callback for "
810 "specific details.";
811 return false;
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700812 }
John Zulauff660ad62019-03-23 07:16:05 -0600813
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700814 // Verify Sample counts
815 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
816 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600817 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700818 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
819 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
820 *error = error_str.str();
821 return false;
822 }
823 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
824 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600825 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700826 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
827 *error = error_str.str();
828 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200829 }
Chris Forbese92dd1d2019-01-21 15:58:57 -0800830 } else if (descriptor_class == TexelBuffer) {
831 auto texel_buffer = static_cast<TexelDescriptor *>(descriptors_[i].get());
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -0700832 auto buffer_view = device_data_->GetBufferViewState(texel_buffer->GetBufferView());
Chris Forbese92dd1d2019-01-21 15:58:57 -0800833
Locke2d9c3dd2019-04-08 16:29:09 -0600834 if (nullptr == buffer_view) {
835 std::stringstream error_str;
836 error_str << "Descriptor in binding #" << binding << " index " << index << " is using bufferView "
837 << buffer_view << " that has been destroyed.";
838 *error = error_str.str();
839 return false;
840 }
841 auto buffer = buffer_view->create_info.buffer;
842 auto buffer_state = device_data_->GetBufferState(buffer);
843 if (!buffer_state) {
844 std::stringstream error_str;
845 error_str << "Descriptor in binding #" << binding << " index " << index << " is using buffer "
846 << buffer_state << " that has been destroyed.";
847 *error = error_str.str();
848 return false;
849 }
Chris Forbese92dd1d2019-01-21 15:58:57 -0800850 auto reqs = binding_pair.second;
851 auto format_bits = DescriptorRequirementsBitsFromFormat(buffer_view->create_info.format);
852
853 if (!(reqs & format_bits)) {
854 // bad component type
855 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600856 error_str << "Descriptor in binding #" << binding << " index " << index << " requires "
Chris Forbese92dd1d2019-01-21 15:58:57 -0800857 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
858 << string_VkFormat(buffer_view->create_info.format) << ".";
859 *error = error_str.str();
860 return false;
861 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600862 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600863 if (descriptor_class == ImageSampler || descriptor_class == PlainSampler) {
864 // Verify Sampler still valid
865 VkSampler sampler;
866 if (descriptor_class == ImageSampler) {
867 sampler = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetSampler();
868 } else {
869 sampler = static_cast<SamplerDescriptor *>(descriptors_[i].get())->GetSampler();
870 }
871 if (!ValidateSampler(sampler, device_data_)) {
872 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600873 error_str << "Descriptor in binding #" << binding << " index " << index << " is using sampler " << sampler
874 << " that has been destroyed.";
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600875 *error = error_str.str();
876 return false;
Lockea223c102019-04-05 00:38:24 -0600877 } else {
878 SAMPLER_STATE *sampler_state = device_data_->GetSamplerState(sampler);
879 if (sampler_state->samplerConversion && !descriptors_[i].get()->IsImmutableSampler()) {
880 std::stringstream error_str;
881 error_str << "sampler (" << sampler << ") in the descriptor set (" << set_
882 << ") contains a YCBCR conversion (" << sampler_state->samplerConversion
883 << ") , then the sampler MUST also exists as an immutable sampler.";
884 *error = error_str.str();
885 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600886 }
887 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600888 }
889 }
890 }
891 return true;
892}
Chris Forbes57989132016-07-26 17:06:10 +1200893
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600894// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600895uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600896 std::unordered_set<VkBuffer> *buffer_set,
897 std::unordered_set<VkImageView> *image_set) const {
898 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200899 for (auto binding_pair : bindings) {
900 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600901 // If a binding doesn't exist, skip it
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600902 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600903 continue;
904 }
John Zulaufc483f442017-12-15 14:02:06 -0700905 uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600906 if (descriptors_[start_idx]->IsStorage()) {
907 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600908 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600909 if (descriptors_[start_idx + i]->updated) {
910 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600911 num_updates++;
912 }
913 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600914 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600915 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600916 if (descriptors_[start_idx + i]->updated) {
917 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -0700918 auto bv_state = device_data_->GetBufferViewState(bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600919 if (bv_state) {
920 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600921 num_updates++;
922 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600923 }
924 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600925 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600926 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600927 if (descriptors_[start_idx + i]->updated) {
928 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600929 num_updates++;
930 }
931 }
932 }
933 }
934 }
935 return num_updates;
936}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600937// Set is being deleted or updates so invalidate all bound cmd buffers
938void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
John Zulauf4fea6622019-04-01 11:38:18 -0600939 device_data_->InvalidateCommandBuffers(cb_bindings, VulkanTypedHandle(set_, kVulkanObjectTypeDescriptorSet));
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600940}
John Zulauf1d27e0a2018-11-05 10:12:48 -0700941
942// Loop through the write updates to do for a push descriptor set, ignoring dstSet
943void cvdescriptorset::DescriptorSet::PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds) {
944 assert(IsPushDescriptor());
945 for (uint32_t i = 0; i < write_count; i++) {
946 PerformWriteUpdate(&p_wds[i]);
947 }
948}
949
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600950// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600951void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700952 // Perform update on a per-binding basis as consecutive updates roll over to next binding
953 auto descriptors_remaining = update->descriptorCount;
954 auto binding_being_updated = update->dstBinding;
955 auto offset = update->dstArrayElement;
Tobin Ehlise16805c2017-08-09 09:10:37 -0600956 uint32_t update_index = 0;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700957 while (descriptors_remaining) {
958 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
John Zulaufc483f442017-12-15 14:02:06 -0700959 auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700960 // Loop over the updates for a single binding at a time
Tobin Ehlise16805c2017-08-09 09:10:37 -0600961 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
962 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700963 }
964 // Roll over to next binding in case of consecutive update
965 descriptors_remaining -= update_count;
966 offset = 0;
967 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600968 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700969 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600970
Jeff Bolzfdf96072018-04-10 14:32:18 -0500971 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
972 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
973 InvalidateBoundCmdBuffers();
974 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600975}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600976// Validate Copy update
977bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -0600978 const DescriptorSet *src_set, const char *func_name,
979 std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -0700980 // Verify dst layout still valid
981 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600982 *error_code = "VUID-VkCopyDescriptorSet-dstSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -0700983 string_sprintf(error_msg,
Lockeca0d9792019-03-03 23:48:13 -0700984 "Cannot call %s to perform copy update on descriptor set dstSet %s"
985 " created with destroyed VkDescriptorSetLayout %s.",
986 func_name, report_data->FormatHandle(set_).c_str(),
987 report_data->FormatHandle(p_layout_->GetDescriptorSetLayout()).c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -0700988 return false;
989 }
990
991 // Verify src layout still valid
992 if (src_set->p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600993 *error_code = "VUID-VkCopyDescriptorSet-srcSet-parameter";
John Zulaufb45fdc32018-10-12 15:14:17 -0600994 string_sprintf(error_msg,
Lockeca0d9792019-03-03 23:48:13 -0700995 "Cannot call %s to perform copy update of dstSet %s"
996 " from descriptor set srcSet %s"
997 " created with destroyed VkDescriptorSetLayout %s.",
998 func_name, report_data->FormatHandle(set_).c_str(), report_data->FormatHandle(src_set->set_).c_str(),
999 report_data->FormatHandle(src_set->p_layout_->GetDescriptorSetLayout()).c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001000 return false;
1001 }
1002
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001003 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001004 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-00347";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001005 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001006 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001007 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001008 return false;
1009 }
1010 if (!src_set->HasBinding(update->srcBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001011 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-00345";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001012 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001013 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001014 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001015 return false;
1016 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001017 // Verify idle ds
1018 if (in_use.load() &&
1019 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1020 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1021 // TODO : Re-using Free Idle error code, need copy update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001022 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001023 std::stringstream error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001024 error_str << "Cannot call " << func_name << " to perform copy update on descriptor set " << set_
Jeff Bolzfdf96072018-04-10 14:32:18 -05001025 << " that is in use by a command buffer";
1026 *error_msg = error_str.str();
1027 return false;
1028 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001029 // src & dst set bindings are valid
1030 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -07001031 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001032 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
1033 // SRC update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001034 *error_code = "VUID-VkCopyDescriptorSet-srcArrayElement-00346";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001035 std::stringstream error_str;
1036 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
John Zulaufc483f442017-12-15 14:02:06 -07001037 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001038 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001039 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001040 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001041 return false;
1042 }
John Zulaufc483f442017-12-15 14:02:06 -07001043 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001044 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001045 // DST update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001046 *error_code = "VUID-VkCopyDescriptorSet-dstArrayElement-00348";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001047 std::stringstream error_str;
1048 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
John Zulaufc483f442017-12-15 14:02:06 -07001049 << " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001050 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001051 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001052 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001053 return false;
1054 }
1055 // Check that types match
Dave Houltond8ed0212018-05-16 17:18:24 -06001056 // TODO : Base default error case going from here is "VUID-VkAcquireNextImageInfoKHR-semaphore-parameter"2ba which covers all
1057 // consistency issues, need more fine-grained error codes
Dave Houlton00c154e2018-05-24 13:20:50 -06001058 *error_code = "VUID-VkCopyDescriptorSet-srcSet-00349";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001059 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001060 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001061 if (src_type != dst_type) {
1062 std::stringstream error_str;
1063 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
1064 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001065 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001066 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001067 return false;
1068 }
1069 // Verify consistency of src & dst bindings if update crosses binding boundaries
John Zulauf4a015c92019-06-04 09:50:05 -06001070 if ((!VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator(p_layout_.get(), update->srcBinding),
1071 update->srcArrayElement, update->descriptorCount, "copy update from", src_set->GetSet(),
1072 error_msg)) ||
1073 (!VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator(p_layout_.get(), update->dstBinding),
1074 update->dstArrayElement, update->descriptorCount, "copy update to", set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001075 return false;
1076 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001077
1078 if ((src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1079 !(GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001080 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01918";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001081 std::stringstream error_str;
1082 error_str << "If pname:srcSet's (" << update->srcSet
1083 << ") layout was created with the "
1084 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1085 "set, then pname:dstSet's ("
1086 << update->dstSet
1087 << ") layout must: also have been created with the "
1088 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1089 *error_msg = error_str.str();
1090 return false;
1091 }
1092
1093 if (!(src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1094 (GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001095 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01919";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001096 std::stringstream error_str;
1097 error_str << "If pname:srcSet's (" << update->srcSet
1098 << ") layout was created without the "
1099 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1100 "set, then pname:dstSet's ("
1101 << update->dstSet
1102 << ") layout must: also have been created without the "
1103 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1104 *error_msg = error_str.str();
1105 return false;
1106 }
1107
1108 if ((src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1109 !(GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001110 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01920";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001111 std::stringstream error_str;
1112 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1113 << ") was allocated was created "
1114 "with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1115 "set, then the descriptor pool from which pname:dstSet ("
1116 << update->dstSet
1117 << ") was allocated must: "
1118 "also have been created with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1119 *error_msg = error_str.str();
1120 return false;
1121 }
1122
1123 if (!(src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1124 (GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001125 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01921";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001126 std::stringstream error_str;
1127 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1128 << ") was allocated was created "
1129 "without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1130 "set, then the descriptor pool from which pname:dstSet ("
1131 << update->dstSet
1132 << ") was allocated must: "
1133 "also have been created without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1134 *error_msg = error_str.str();
1135 return false;
1136 }
1137
Jeff Bolze54ae892018-09-08 12:16:29 -05001138 if (src_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1139 if ((update->srcArrayElement % 4) != 0) {
1140 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02223";
1141 std::stringstream error_str;
1142 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1143 << "srcArrayElement " << update->srcArrayElement << " not a multiple of 4";
1144 *error_msg = error_str.str();
1145 return false;
1146 }
1147 if ((update->dstArrayElement % 4) != 0) {
1148 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02224";
1149 std::stringstream error_str;
1150 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1151 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1152 *error_msg = error_str.str();
1153 return false;
1154 }
1155 if ((update->descriptorCount % 4) != 0) {
1156 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02225";
1157 std::stringstream error_str;
1158 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1159 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1160 *error_msg = error_str.str();
1161 return false;
1162 }
1163 }
1164
Tobin Ehlisd41e7b62016-05-19 07:56:18 -06001165 // Update parameters all look good and descriptor updated so verify update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06001166 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 -06001167
1168 // All checks passed so update is good
1169 return true;
1170}
1171// Perform Copy update
1172void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -07001173 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
1174 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001175 // Update parameters all look good so perform update
1176 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001177 auto src = src_set->descriptors_[src_start_idx + di].get();
1178 auto dst = descriptors_[dst_start_idx + di].get();
1179 if (src->updated) {
1180 dst->CopyUpdate(src);
1181 some_update_ = true;
1182 } else {
1183 dst->updated = false;
1184 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001185 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001186
Jeff Bolzfdf96072018-04-10 14:32:18 -05001187 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1188 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1189 InvalidateBoundCmdBuffers();
1190 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001191}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001192
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001193// Update the drawing state for the affected descriptors.
1194// Set cb_node to this set and this set to cb_node.
1195// Add the bindings of the descriptor
1196// Set the layout based on the current descriptor layout (will mask subsequent layer mismatch errors)
1197// TODO: Modify the UpdateDrawState virtural functions to *only* set initial layout and not change layouts
Tobin Ehlisf9519102016-08-17 09:49:13 -06001198// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
1199// to be used in a draw by the given cb_node
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001200void cvdescriptorset::DescriptorSet::UpdateDrawState(CoreChecks *device_data, CMD_BUFFER_STATE *cb_node,
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001201 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001202 // bind cb to this descriptor set
1203 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001204 // Add bindings for descriptor set, the set's pool, and individual objects in the set
John Zulauf4fea6622019-04-01 11:38:18 -06001205 cb_node->object_bindings.emplace(set_, kVulkanObjectTypeDescriptorSet);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001206 pool_state_->cb_bindings.insert(cb_node);
John Zulauf4fea6622019-04-01 11:38:18 -06001207 cb_node->object_bindings.emplace(pool_state_->pool, kVulkanObjectTypeDescriptorPool);
Tobin Ehlisf9519102016-08-17 09:49:13 -06001208 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
1209 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -07001210 for (auto binding_req_pair : binding_req_map) {
1211 auto binding = binding_req_pair.first;
Tony-LunarG62c5dba2018-12-20 14:27:23 -07001212 // We aren't validating descriptors created with PARTIALLY_BOUND or UPDATE_AFTER_BIND, so don't record state
1213 if (p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
1214 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) {
1215 continue;
1216 }
John Zulaufc483f442017-12-15 14:02:06 -07001217 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
1218 for (uint32_t i = range.start; i < range.end; ++i) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001219 descriptors_[i]->UpdateDrawState(device_data, cb_node);
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001220 }
1221 }
1222}
1223
John Zulauf48a6a702017-12-22 17:14:54 -07001224void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1225 const BindingReqMap &in_req, BindingReqMap *out_req,
1226 TrackedBindings *bindings) {
1227 assert(out_req);
1228 assert(bindings);
1229 const auto binding = binding_req_pair.first;
1230 // Use insert and look at the boolean ("was inserted") in the returned pair to see if this is a new set member.
1231 // Saves one hash lookup vs. find ... compare w/ end ... insert.
1232 const auto it_bool_pair = bindings->insert(binding);
1233 if (it_bool_pair.second) {
1234 out_req->emplace(binding_req_pair);
1235 }
1236}
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001237
John Zulauf48a6a702017-12-22 17:14:54 -07001238void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1239 const BindingReqMap &in_req, BindingReqMap *out_req,
1240 TrackedBindings *bindings, uint32_t limit) {
1241 if (bindings->size() < limit) FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, bindings);
1242}
1243
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001244void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(CMD_BUFFER_STATE *cb_state, const BindingReqMap &in_req,
John Zulauf48a6a702017-12-22 17:14:54 -07001245 BindingReqMap *out_req) {
1246 TrackedBindings &bound = cached_validation_[cb_state].command_binding_and_usage;
1247 if (bound.size() == GetBindingCount()) {
1248 return; // All bindings are bound, out req is empty
1249 }
1250 for (const auto &binding_req_pair : in_req) {
1251 const auto binding = binding_req_pair.first;
1252 // If a binding doesn't exist, or has already been bound, skip it
1253 if (p_layout_->HasBinding(binding)) {
1254 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, &bound);
1255 }
1256 }
1257}
1258
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001259void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(CMD_BUFFER_STATE *cb_state, PIPELINE_STATE *pipeline,
John Zulauf48a6a702017-12-22 17:14:54 -07001260 const BindingReqMap &in_req, BindingReqMap *out_req) {
1261 auto &validated = cached_validation_[cb_state];
1262 auto &image_sample_val = validated.image_samplers[pipeline];
1263 auto *const dynamic_buffers = &validated.dynamic_buffers;
1264 auto *const non_dynamic_buffers = &validated.non_dynamic_buffers;
1265 const auto &stats = p_layout_->GetBindingTypeStats();
1266 for (const auto &binding_req_pair : in_req) {
1267 auto binding = binding_req_pair.first;
1268 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
1269 if (!layout_binding) {
1270 continue;
1271 }
1272 // Caching criteria differs per type.
1273 // If image_layout have changed , the image descriptors need to be validated against them.
1274 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1275 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
1276 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, dynamic_buffers, stats.dynamic_buffer_count);
1277 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1278 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
1279 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
1280 } else {
1281 // This is rather crude, as the changed layouts may not impact the bound descriptors,
1282 // but the simple "versioning" is a simple "dirt" test.
1283 auto &version = image_sample_val[binding]; // Take advantage of default construtor zero initialzing new entries
1284 if (version != cb_state->image_layout_change_count) {
1285 version = cb_state->image_layout_change_count;
1286 out_req->emplace(binding_req_pair);
1287 }
1288 }
1289 }
1290}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001291
Tobin Ehlis300888c2016-05-18 13:43:26 -06001292cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001293 updated = false;
1294 descriptor_class = PlainSampler;
1295 if (immut) {
1296 sampler_ = *immut;
1297 immutable_ = true;
1298 updated = true;
1299 }
1300}
Tobin Ehlise2f80292016-06-02 10:08:53 -06001301// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001302bool cvdescriptorset::ValidateSampler(const VkSampler sampler, CoreChecks *dev_data) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001303 return (dev_data->GetSamplerState(sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001304}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001305
Tobin Ehlis554bf382016-05-24 11:14:43 -06001306bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001307 CoreChecks *dev_data, const char *func_name, std::string *error_code,
John Zulaufb45fdc32018-10-12 15:14:17 -06001308 std::string *error_msg) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001309 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00326";
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001310 auto iv_state = dev_data->GetImageViewState(image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001311 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001312 std::stringstream error_str;
1313 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001314 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001315 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001316 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001317 // 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 -06001318 // Validate that imageLayout is compatible with aspect_mask and image format
1319 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001320 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
1321 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001322 VkFormat format = VK_FORMAT_MAX_ENUM;
1323 VkImageUsageFlags usage = 0;
Mark Lobodzinski8ddc23f2019-03-06 11:48:49 -07001324 auto image_node = dev_data->GetImageState(image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001325 if (image_node) {
1326 format = image_node->createInfo.format;
1327 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001328 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001329 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
1330 // the error here occurs is if memory bound to a created imageView has been freed.
Mark Lobodzinski0ebe9712019-03-07 13:39:07 -07001331 if (dev_data->ValidateMemoryIsBoundToImage(image_node, func_name, "VUID-VkImageViewCreateInfo-image-01020")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001332 *error_code = "VUID-VkImageViewCreateInfo-image-01020";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001333 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001334 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001335 }
Chris Forbes67757ff2017-07-21 13:59:01 -07001336
1337 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
1338 // but not binding them to descriptor sets.
1339 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D &&
1340 (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
1341 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001342 *error_code = "VUID-VkDescriptorImageInfo-imageView-00343";
Chris Forbes67757ff2017-07-21 13:59:01 -07001343 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
1344 return false;
1345 }
Tobin Ehlis1809f912016-05-25 09:24:36 -06001346 }
1347 // First validate that format and layout are compatible
1348 if (format == VK_FORMAT_MAX_ENUM) {
1349 std::stringstream error_str;
1350 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001351 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001352 return false;
1353 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001354 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
1355 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -06001356 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -06001357 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001358 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1359 // Only Color bit must be set
1360 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001361 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -07001362 error_str
1363 << "ImageView (" << image_view
1364 << ") 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 -06001365 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001366 return false;
1367 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001368 // format must NOT be DS
1369 if (ds) {
1370 std::stringstream error_str;
1371 error_str << "ImageView (" << image_view
1372 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1373 << string_VkFormat(format) << " which is not a color format.";
1374 *error_msg = error_str.str();
1375 return false;
1376 }
1377 break;
1378 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1379 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1380 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001381 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1382 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1383 // both must NOT be set
1384 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001385 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001386 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001387 return false;
1388 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001389 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1390 // Neither were set
1391 std::stringstream error_str;
1392 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1393 << " but does not have STENCIL or DEPTH aspects set";
1394 *error_msg = error_str.str();
1395 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001396 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001397 // format must be DS
1398 if (!ds) {
1399 std::stringstream error_str;
1400 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1401 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
1402 *error_msg = error_str.str();
1403 return false;
1404 }
1405 break;
1406 default:
1407 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1408 if (ds) {
1409 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1410 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1411 // both must NOT be set
1412 std::stringstream error_str;
1413 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1414 << " and is using depth/stencil image of format " << string_VkFormat(format)
1415 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1416 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1417 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1418 "reads respectively.";
1419 *error_msg = error_str.str();
1420 return false;
1421 }
1422 }
1423 }
1424 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001425 }
1426 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001427 // 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 -06001428 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1429 // under vkCreateImage()
Dave Houltond8ed0212018-05-16 17:18:24 -06001430 // TODO : Need to also validate case "VUID-VkWriteDescriptorSet-descriptorType-00336" where STORAGE_IMAGE & INPUT_ATTACH types
1431 // must have been created with identify swizzle
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001432 const char *error_usage_bit = nullptr;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001433 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001434 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1435 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1436 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1437 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
1438 }
1439 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001440 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001441 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1442 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1443 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
1444 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
1445 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001446 // TODO : Need to create custom enum error codes for these cases
1447 if (image_node->shared_presentable) {
1448 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001449 error_str << "ImageView (" << image_view
1450 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
1451 "layout "
1452 << string_VkImageLayout(image_layout)
1453 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report "
1454 "support for VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the "
1455 "VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001456 *error_msg = error_str.str();
1457 return false;
1458 }
1459 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001460 error_str << "ImageView (" << image_view
1461 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
1462 << string_VkImageLayout(image_layout)
1463 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage "
1464 "images can only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001465 *error_msg = error_str.str();
1466 return false;
1467 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001468 }
1469 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001470 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001471 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
1472 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
1473 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
1474 }
1475 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001476 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001477 default:
1478 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001479 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001480 if (error_usage_bit) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001481 std::stringstream error_str;
1482 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
1483 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1484 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001485 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001486 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001487 }
John Zulauff4c07882019-01-24 14:03:36 -07001488
1489 if ((type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) || (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)) {
1490 // Test that the layout is compatible with the descriptorType for the two sampled image types
1491 const static std::array<VkImageLayout, 3> valid_layouts = {
Jeremy Hayesd0549f62019-06-05 10:15:36 -06001492 {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 -07001493
1494 struct ExtensionLayout {
1495 VkImageLayout layout;
1496 bool DeviceExtensions::*extension;
1497 };
1498
1499 const static std::array<ExtensionLayout, 3> extended_layouts{
1500 {// Note double brace req'd for aggregate initialization
1501 {VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR, &DeviceExtensions::vk_khr_shared_presentable_image},
1502 {VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2},
1503 {VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2}}};
1504 auto is_layout = [image_layout, dev_data](const ExtensionLayout &ext_layout) {
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001505 return dev_data->device_extensions.*(ext_layout.extension) && (ext_layout.layout == image_layout);
John Zulauff4c07882019-01-24 14:03:36 -07001506 };
1507
1508 bool valid_layout = (std::find(valid_layouts.cbegin(), valid_layouts.cend(), image_layout) != valid_layouts.cend()) ||
1509 std::any_of(extended_layouts.cbegin(), extended_layouts.cend(), is_layout);
1510
1511 if (!valid_layout) {
1512 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01403";
1513 std::stringstream error_str;
1514 error_str << "Descriptor update with descriptorType " << string_VkDescriptorType(type)
1515 << " is being updated with invalid imageLayout " << string_VkImageLayout(image_layout)
1516 << ". Allowed layouts are: VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, "
1517 << "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL";
1518 for (auto &ext_layout : extended_layouts) {
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001519 if (dev_data->device_extensions.*(ext_layout.extension)) {
John Zulauff4c07882019-01-24 14:03:36 -07001520 error_str << ", " << string_VkImageLayout(ext_layout.layout);
1521 }
1522 }
1523 *error_msg = error_str.str();
1524 return false;
1525 }
1526 }
1527
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001528 return true;
1529}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001530
Tobin Ehlis300888c2016-05-18 13:43:26 -06001531void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Chris Forbesfea2c542018-04-13 09:34:15 -07001532 if (!immutable_) {
1533 sampler_ = update->pImageInfo[index].sampler;
1534 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001535 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001536}
1537
Tobin Ehlis300888c2016-05-18 13:43:26 -06001538void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001539 if (!immutable_) {
1540 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001541 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001542 }
1543 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001544}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001545
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001546void cvdescriptorset::SamplerDescriptor::UpdateDrawState(CoreChecks *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001547 if (!immutable_) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001548 auto sampler_state = dev_data->GetSamplerState(sampler_);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001549 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001550 }
1551}
1552
Tobin Ehlis300888c2016-05-18 13:43:26 -06001553cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001554 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001555 updated = false;
1556 descriptor_class = ImageSampler;
1557 if (immut) {
1558 sampler_ = *immut;
1559 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001560 }
1561}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001562
Tobin Ehlis300888c2016-05-18 13:43:26 -06001563void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001564 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001565 const auto &image_info = update->pImageInfo[index];
Chris Forbesfea2c542018-04-13 09:34:15 -07001566 if (!immutable_) {
1567 sampler_ = image_info.sampler;
1568 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001569 image_view_ = image_info.imageView;
1570 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001571}
1572
Tobin Ehlis300888c2016-05-18 13:43:26 -06001573void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001574 if (!immutable_) {
1575 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001576 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001577 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001578 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1579 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001580 updated = true;
1581 image_view_ = image_view;
1582 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001583}
1584
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001585void cvdescriptorset::ImageSamplerDescriptor::UpdateDrawState(CoreChecks *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001586 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001587 if (!immutable_) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001588 auto sampler_state = dev_data->GetSamplerState(sampler_);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001589 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001590 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001591 // Add binding for image
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001592 auto iv_state = dev_data->GetImageViewState(image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001593 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001594 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
John Zulauff660ad62019-03-23 07:16:05 -06001595 dev_data->SetImageViewInitialLayout(cb_node, *iv_state, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06001596 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001597}
1598
Tobin Ehlis300888c2016-05-18 13:43:26 -06001599cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1600 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001601 updated = false;
1602 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001603 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001604}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001605
Tobin Ehlis300888c2016-05-18 13:43:26 -06001606void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001607 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001608 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001609 image_view_ = image_info.imageView;
1610 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001611}
1612
Tobin Ehlis300888c2016-05-18 13:43:26 -06001613void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001614 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1615 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001616 updated = true;
1617 image_view_ = image_view;
1618 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001619}
1620
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001621void cvdescriptorset::ImageDescriptor::UpdateDrawState(CoreChecks *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001622 // Add binding for image
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001623 auto iv_state = dev_data->GetImageViewState(image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001624 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001625 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
John Zulauff660ad62019-03-23 07:16:05 -06001626 dev_data->SetImageViewInitialLayout(cb_node, *iv_state, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06001627 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001628}
1629
Tobin Ehlis300888c2016-05-18 13:43:26 -06001630cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1631 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001632 updated = false;
1633 descriptor_class = GeneralBuffer;
1634 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1635 dynamic_ = true;
1636 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1637 storage_ = true;
1638 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1639 dynamic_ = true;
1640 storage_ = true;
1641 }
1642}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001643void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001644 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001645 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001646 buffer_ = buffer_info.buffer;
1647 offset_ = buffer_info.offset;
1648 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001649}
1650
Tobin Ehlis300888c2016-05-18 13:43:26 -06001651void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1652 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001653 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001654 buffer_ = buff_desc->buffer_;
1655 offset_ = buff_desc->offset_;
1656 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001657}
1658
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001659void cvdescriptorset::BufferDescriptor::UpdateDrawState(CoreChecks *dev_data, CMD_BUFFER_STATE *cb_node) {
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07001660 auto buffer_node = dev_data->GetBufferState(buffer_);
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001661 if (buffer_node) dev_data->AddCommandBufferBindingBuffer(cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001662}
1663
Tobin Ehlis300888c2016-05-18 13:43:26 -06001664cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001665 updated = false;
1666 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001667 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001668}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001669
Tobin Ehlis300888c2016-05-18 13:43:26 -06001670void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001671 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001672 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001673}
1674
Tobin Ehlis300888c2016-05-18 13:43:26 -06001675void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1676 updated = true;
1677 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1678}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001679
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001680void cvdescriptorset::TexelDescriptor::UpdateDrawState(CoreChecks *dev_data, CMD_BUFFER_STATE *cb_node) {
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07001681 auto bv_state = dev_data->GetBufferViewState(buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001682 if (bv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001683 dev_data->AddCommandBufferBindingBufferView(cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001684 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001685}
1686
Tobin Ehlis300888c2016-05-18 13:43:26 -06001687// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1688// sets, and then calls their respective Validate[Write|Copy]Update functions.
1689// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1690// be skipped, then true is returned.
1691// If there is no issue with the update, then false is returned.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001692bool CoreChecks::ValidateUpdateDescriptorSets(uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001693 const VkCopyDescriptorSet *p_cds, const char *func_name) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001694 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001695 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001696 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001697 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001698 auto set_node = GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001699 if (!set_node) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001700 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1701 HandleToUint64(dest_set), kVUID_Core_DrawState_InvalidDescriptorSet,
Lockeca0d9792019-03-03 23:48:13 -07001702 "Cannot call %s on descriptor set %s that has not been allocated.", func_name,
1703 report_data->FormatHandle(dest_set).c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001704 } else {
Dave Houltond8ed0212018-05-16 17:18:24 -06001705 std::string error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001706 std::string error_str;
John Zulauf613fd982019-06-04 15:14:41 -06001707 if (!ValidateWriteUpdate(set_node, report_data, &p_wds[i], func_name, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001708 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Mark Lobodzinski88529492018-04-01 10:38:15 -06001709 HandleToUint64(dest_set), error_code,
Lockeca0d9792019-03-03 23:48:13 -07001710 "%s failed write update validation for Descriptor Set %s with error: %s.", func_name,
1711 report_data->FormatHandle(dest_set).c_str(), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001712 }
1713 }
1714 }
1715 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001716 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001717 auto dst_set = p_cds[i].dstSet;
1718 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001719 auto src_node = GetSetNode(src_set);
1720 auto dst_node = GetSetNode(dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001721 // Object_tracker verifies that src & dest descriptor set are valid
1722 assert(src_node);
1723 assert(dst_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001724 std::string error_code;
Tobin Ehlisa1712752017-01-04 09:41:47 -07001725 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001726 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, func_name, &error_code, &error_str)) {
Lockeca0d9792019-03-03 23:48:13 -07001727 skip |= log_msg(
1728 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, HandleToUint64(dst_set),
1729 error_code, "%s failed copy update from Descriptor Set %s to Descriptor Set %s with error: %s.", func_name,
1730 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 -06001731 }
1732 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001733 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001734}
1735// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1736// sets, and then calls their respective Perform[Write|Copy]Update functions.
1737// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1738// with the same set of updates.
1739// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1740// calling down the chain.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001741void cvdescriptorset::PerformUpdateDescriptorSets(CoreChecks *dev_data, uint32_t write_count, const VkWriteDescriptorSet *p_wds,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001742 uint32_t copy_count, const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001743 // Write updates first
1744 uint32_t i = 0;
1745 for (i = 0; i < write_count; ++i) {
1746 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001747 auto set_node = dev_data->GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001748 if (set_node) {
1749 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001750 }
1751 }
1752 // Now copy updates
1753 for (i = 0; i < copy_count; ++i) {
1754 auto dst_set = p_cds[i].dstSet;
1755 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001756 auto src_node = dev_data->GetSetNode(src_set);
1757 auto dst_node = dev_data->GetSetNode(dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001758 if (src_node && dst_node) {
1759 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001760 }
1761 }
1762}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001763
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001764cvdescriptorset::DecodedTemplateUpdate::DecodedTemplateUpdate(CoreChecks *device_data, VkDescriptorSet descriptorSet,
John Zulauf1d27e0a2018-11-05 10:12:48 -07001765 const TEMPLATE_STATE *template_state, const void *pData,
1766 VkDescriptorSetLayout push_layout) {
John Zulaufb845eb22018-10-12 11:41:06 -06001767 auto const &create_info = template_state->create_info;
1768 inline_infos.resize(create_info.descriptorUpdateEntryCount); // Make sure we have one if we need it
1769 desc_writes.reserve(create_info.descriptorUpdateEntryCount); // emplaced, so reserved without initialization
John Zulauf1d27e0a2018-11-05 10:12:48 -07001770 VkDescriptorSetLayout effective_dsl = create_info.templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
1771 ? create_info.descriptorSetLayout
1772 : push_layout;
1773 auto layout_obj = GetDescriptorSetLayout(device_data, effective_dsl);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001774
1775 // Create a WriteDescriptorSet struct for each template update entry
1776 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1777 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1778 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1779 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1780
John Zulaufb6d71202017-12-22 16:47:09 -07001781 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001782 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1783 desc_writes.emplace_back();
1784 auto &write_entry = desc_writes.back();
1785
1786 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1787 char *update_entry = (char *)(pData) + offset;
1788
1789 if (dst_array_element >= binding_count) {
1790 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001791 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001792 }
1793
1794 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1795 write_entry.pNext = NULL;
1796 write_entry.dstSet = descriptorSet;
1797 write_entry.dstBinding = binding_being_updated;
1798 write_entry.dstArrayElement = dst_array_element;
1799 write_entry.descriptorCount = 1;
1800 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1801
1802 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1803 case VK_DESCRIPTOR_TYPE_SAMPLER:
1804 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1805 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1806 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1807 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1808 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1809 break;
1810
1811 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1812 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1813 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1814 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1815 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1816 break;
1817
1818 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1819 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1820 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1821 break;
Dave Houlton142c4cb2018-10-17 15:04:41 -06001822 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1823 VkWriteDescriptorSetInlineUniformBlockEXT *inline_info = &inline_infos[i];
1824 inline_info->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT;
1825 inline_info->pNext = nullptr;
1826 inline_info->dataSize = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1827 inline_info->pData = update_entry;
1828 write_entry.pNext = inline_info;
1829 // skip the rest of the array, they just represent bytes in the update
1830 j = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1831 break;
1832 }
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001833 default:
1834 assert(0);
1835 break;
1836 }
1837 dst_array_element++;
1838 }
1839 }
John Zulaufb845eb22018-10-12 11:41:06 -06001840}
John Zulaufb45fdc32018-10-12 15:14:17 -06001841// These helper functions carry out the validate and record descriptor updates peformed via update templates. They decode
1842// the templatized data and leverage the non-template UpdateDescriptor helper functions.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001843bool CoreChecks::ValidateUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
1844 const void *pData) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001845 // Translate the templated update into a normal update for validation...
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001846 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
1847 return ValidateUpdateDescriptorSets(static_cast<uint32_t>(decoded_update.desc_writes.size()), decoded_update.desc_writes.data(),
1848 0, NULL, "vkUpdateDescriptorSetWithTemplate()");
John Zulaufb45fdc32018-10-12 15:14:17 -06001849}
John Zulaufb845eb22018-10-12 11:41:06 -06001850
Mark Lobodzinski254c8512019-03-09 12:21:15 -07001851void CoreChecks::PerformUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
1852 const void *pData) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001853 // Translate the templated update into a normal update for validation...
Mark Lobodzinski254c8512019-03-09 12:21:15 -07001854 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
1855 cvdescriptorset::PerformUpdateDescriptorSets(this, static_cast<uint32_t>(decoded_update.desc_writes.size()),
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001856 decoded_update.desc_writes.data(), 0, NULL);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001857}
John Zulauf4e7bcb52018-11-02 10:46:30 -06001858
1859std::string cvdescriptorset::DescriptorSet::StringifySetAndLayout() const {
1860 std::string out;
1861 uint64_t layout_handle = HandleToUint64(p_layout_->GetDescriptorSetLayout());
1862 if (IsPushDescriptor()) {
1863 string_sprintf(&out, "Push Descriptors defined with VkDescriptorSetLayout 0x%" PRIxLEAST64, layout_handle);
John Zulauf4e7bcb52018-11-02 10:46:30 -06001864 } else {
Bob Ellisonaf2f8532019-03-12 11:05:47 -06001865 string_sprintf(&out, "VkDescriptorSet 0x%" PRIxLEAST64 " allocated with VkDescriptorSetLayout 0x%" PRIxLEAST64,
John Zulauf4e7bcb52018-11-02 10:46:30 -06001866 HandleToUint64(set_), layout_handle);
1867 }
1868 return out;
1869};
1870
John Zulauf1d27e0a2018-11-05 10:12:48 -07001871// Loop through the write updates to validate for a push descriptor set, ignoring dstSet
1872bool cvdescriptorset::DescriptorSet::ValidatePushDescriptorsUpdate(const debug_report_data *report_data, uint32_t write_count,
1873 const VkWriteDescriptorSet *p_wds, const char *func_name) {
1874 assert(IsPushDescriptor());
1875 bool skip = false;
1876 for (uint32_t i = 0; i < write_count; i++) {
1877 std::string error_code;
1878 std::string error_str;
John Zulauf613fd982019-06-04 15:14:41 -06001879 if (!ValidateWriteUpdate(this, report_data, &p_wds[i], func_name, &error_code, &error_str)) {
John Zulauf1d27e0a2018-11-05 10:12:48 -07001880 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1881 HandleToUint64(p_layout_->GetDescriptorSetLayout()), error_code, "%s failed update validation: %s.",
1882 func_name, error_str.c_str());
1883 }
1884 }
1885 return skip;
1886}
1887
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001888// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001889// If there's an error, update the error_msg string with details and return false, else return true
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001890bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06001891 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001892 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001893 auto usage = buffer_node->createInfo.usage;
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001894 const char *error_usage_bit = nullptr;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001895 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001896 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1897 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001898 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00334";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001899 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1900 }
1901 break;
1902 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1903 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001904 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00335";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001905 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1906 }
1907 break;
1908 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1909 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1910 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001911 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00330";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001912 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1913 }
1914 break;
1915 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1916 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1917 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001918 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00331";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001919 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1920 }
1921 break;
1922 default:
1923 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001924 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001925 if (error_usage_bit) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001926 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001927 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1928 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1929 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001930 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001931 return false;
1932 }
1933 return true;
1934}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001935// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1936// 1. buffer is valid
1937// 2. buffer was created with correct usage flags
1938// 3. offset is less than buffer size
1939// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001940// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001941// If there's an error, update the error_msg string with details and return false, else return true
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001942bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
John Zulaufb45fdc32018-10-12 15:14:17 -06001943 const char *func_name, std::string *error_code,
1944 std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001945 // First make sure that buffer is valid
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07001946 auto buffer_node = device_data_->GetBufferState(buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07001947 // Any invalid buffer should already be caught by object_tracker
1948 assert(buffer_node);
Mark Lobodzinski0ebe9712019-03-07 13:39:07 -07001949 if (device_data_->ValidateMemoryIsBoundToBuffer(buffer_node, func_name, "VUID-VkWriteDescriptorSet-descriptorType-00329")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001950 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00329";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001951 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001952 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001953 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001954 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001955 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1956 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001957 return false;
1958 }
1959 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001960 if (buffer_info->offset >= buffer_node->createInfo.size) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001961 *error_code = "VUID-VkDescriptorBufferInfo-offset-00340";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001962 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001963 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
1964 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001965 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001966 return false;
1967 }
1968 if (buffer_info->range != VK_WHOLE_SIZE) {
1969 // Range must be VK_WHOLE_SIZE or > 0
1970 if (!buffer_info->range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001971 *error_code = "VUID-VkDescriptorBufferInfo-range-00341";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001972 std::stringstream error_str;
1973 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001974 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001975 return false;
1976 }
1977 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1978 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001979 *error_code = "VUID-VkDescriptorBufferInfo-range-00342";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001980 std::stringstream error_str;
1981 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1982 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001983 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001984 return false;
1985 }
1986 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001987 // Check buffer update sizes against device limits
1988 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1989 auto max_ub_range = limits_.maxUniformBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001990 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001991 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001992 std::stringstream error_str;
1993 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1994 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
1995 *error_msg = error_str.str();
1996 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02001997 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_ub_range) {
1998 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
1999 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002000 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2001 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002002 << "maxUniformBufferRange (" << max_ub_range << ")";
2003 *error_msg = error_str.str();
2004 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002005 }
2006 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
2007 auto max_sb_range = limits_.maxStorageBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002008 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002009 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002010 std::stringstream error_str;
2011 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2012 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
2013 *error_msg = error_str.str();
2014 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002015 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_sb_range) {
2016 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
2017 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002018 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2019 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002020 << "maxStorageBufferRange (" << max_sb_range << ")";
2021 *error_msg = error_str.str();
2022 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002023 }
2024 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002025 return true;
2026}
2027
Tobin Ehlis300888c2016-05-18 13:43:26 -06002028// Verify that the contents of the update are ok, but don't perform actual update
2029bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
John Zulaufb45fdc32018-10-12 15:14:17 -06002030 const char *func_name, std::string *error_code,
2031 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002032 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002033 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
2034 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2035 // Validate image
2036 auto image_view = update->pImageInfo[di].imageView;
2037 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002038 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2039 error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002040 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002041 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
2042 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002043 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002044 return false;
2045 }
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002046 if (device_data_->device_extensions.vk_khr_sampler_ycbcr_conversion) {
Juan A. Suarez Romerob6809602019-04-24 07:52:26 +00002047 ImageSamplerDescriptor *desc = (ImageSamplerDescriptor *)descriptors_[index + di].get();
Tony-LunarG352f08f2019-01-03 10:11:24 -07002048 if (desc->IsImmutableSampler()) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07002049 auto sampler_state = device_data_->GetSamplerState(desc->GetSampler());
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07002050 auto iv_state = device_data_->GetImageViewState(image_view);
Tony-LunarG352f08f2019-01-03 10:11:24 -07002051 if (iv_state && sampler_state) {
2052 if (iv_state->samplerConversion != sampler_state->samplerConversion) {
2053 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01948";
2054 std::stringstream error_str;
Tony-LunarG8a51f0a2019-01-04 16:14:14 -07002055 error_str << "Attempted write update to combined image sampler and image view and sampler ycbcr "
2056 "conversions are not identical, sampler: "
Tony-LunarG352f08f2019-01-03 10:11:24 -07002057 << desc->GetSampler() << " image view: " << iv_state->image_view << ".";
2058 *error_msg = error_str.str();
2059 return false;
2060 }
2061 }
Lockea223c102019-04-05 00:38:24 -06002062 } else {
2063 auto iv_state = device_data_->GetImageViewState(image_view);
Locke5ce35a72019-05-06 14:24:36 -06002064 if (iv_state && (iv_state->samplerConversion != VK_NULL_HANDLE)) {
2065 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01947";
2066 std::stringstream error_str;
2067 error_str << "Because dstSet (" << update->dstSet << ") is bound to image view ("
2068 << iv_state->image_view
2069 << ") that includes a YCBCR conversion, it must have been allocated with a layout that "
2070 "includes an immutable sampler.";
2071 *error_msg = error_str.str();
2072 return false;
Lockea223c102019-04-05 00:38:24 -06002073 }
Tony-LunarG352f08f2019-01-03 10:11:24 -07002074 }
2075 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002076 }
2077 }
Tobin Ehlis648b1cf2018-04-13 15:24:13 -06002078 // fall through
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002079 case VK_DESCRIPTOR_TYPE_SAMPLER: {
2080 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2081 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
2082 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002083 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002084 std::stringstream error_str;
2085 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
2086 << update->pImageInfo[di].sampler << ".";
2087 *error_msg = error_str.str();
2088 return false;
2089 }
2090 } else {
2091 // TODO : Warn here
2092 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002093 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002094 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002095 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002096 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2097 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2098 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2099 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2100 auto image_view = update->pImageInfo[di].imageView;
2101 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002102 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2103 error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002104 std::stringstream error_str;
2105 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
2106 *error_msg = error_str.str();
2107 return false;
2108 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002109 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002110 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002111 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002112 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2113 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
2114 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2115 auto buffer_view = update->pTexelBufferView[di];
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07002116 auto bv_state = device_data_->GetBufferViewState(buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002117 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002118 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002119 std::stringstream error_str;
2120 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2121 *error_msg = error_str.str();
2122 return false;
2123 }
2124 auto buffer = bv_state->create_info.buffer;
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002125 auto buffer_state = device_data_->GetBufferState(buffer);
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002126 // Verify that buffer underlying the view hasn't been destroyed prematurely
2127 if (!buffer_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002128 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002129 std::stringstream error_str;
2130 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
2131 << ") has been destroyed: " << error_msg->c_str();
2132 *error_msg = error_str.str();
2133 return false;
2134 } else if (!ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002135 std::stringstream error_str;
2136 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
2137 *error_msg = error_str.str();
2138 return false;
2139 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002140 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002141 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002142 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002143 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2144 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2145 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2146 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2147 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufb45fdc32018-10-12 15:14:17 -06002148 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, func_name, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002149 std::stringstream error_str;
2150 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
2151 *error_msg = error_str.str();
2152 return false;
2153 }
2154 }
2155 break;
2156 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002157 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
2158 break;
Eric Werness30127fd2018-10-31 21:01:03 -07002159 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002160 // XXX TODO
2161 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002162 default:
2163 assert(0); // We've already verified update type so should never get here
2164 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002165 }
2166 // All checks passed so update contents are good
2167 return true;
2168}
2169// Verify that the contents of the update are ok, but don't perform actual update
2170bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
John Zulaufb45fdc32018-10-12 15:14:17 -06002171 VkDescriptorType type, uint32_t index, const char *func_name,
2172 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002173 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
2174 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06002175 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002176 case PlainSampler: {
2177 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002178 const auto src_desc = src_set->descriptors_[index + di].get();
2179 if (!src_desc->updated) continue;
2180 if (!src_desc->IsImmutableSampler()) {
2181 auto update_sampler = static_cast<SamplerDescriptor *>(src_desc)->GetSampler();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002182 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002183 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002184 std::stringstream error_str;
2185 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2186 *error_msg = error_str.str();
2187 return false;
2188 }
2189 } else {
2190 // TODO : Warn here
2191 }
2192 }
2193 break;
2194 }
2195 case ImageSampler: {
2196 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002197 const auto src_desc = src_set->descriptors_[index + di].get();
2198 if (!src_desc->updated) continue;
2199 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002200 // First validate sampler
2201 if (!img_samp_desc->IsImmutableSampler()) {
2202 auto update_sampler = img_samp_desc->GetSampler();
2203 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002204 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002205 std::stringstream error_str;
2206 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2207 *error_msg = error_str.str();
2208 return false;
2209 }
2210 } else {
2211 // TODO : Warn here
2212 }
2213 // Validate image
2214 auto image_view = img_samp_desc->GetImageView();
2215 auto image_layout = img_samp_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002216 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002217 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002218 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002219 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002220 return false;
2221 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002222 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002223 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002224 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002225 case Image: {
2226 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002227 const auto src_desc = src_set->descriptors_[index + di].get();
2228 if (!src_desc->updated) continue;
2229 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002230 auto image_view = img_desc->GetImageView();
2231 auto image_layout = img_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002232 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002233 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002234 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002235 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002236 return false;
2237 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002238 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002239 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002240 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002241 case TexelBuffer: {
2242 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002243 const auto src_desc = src_set->descriptors_[index + di].get();
2244 if (!src_desc->updated) continue;
2245 auto buffer_view = static_cast<TexelDescriptor *>(src_desc)->GetBufferView();
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07002246 auto bv_state = device_data_->GetBufferViewState(buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002247 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002248 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002249 std::stringstream error_str;
2250 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2251 *error_msg = error_str.str();
2252 return false;
2253 }
2254 auto buffer = bv_state->create_info.buffer;
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002255 if (!ValidateBufferUsage(device_data_->GetBufferState(buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002256 std::stringstream error_str;
2257 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
2258 *error_msg = error_str.str();
2259 return false;
2260 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002261 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002262 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002263 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002264 case GeneralBuffer: {
2265 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002266 const auto src_desc = src_set->descriptors_[index + di].get();
2267 if (!src_desc->updated) continue;
2268 auto buffer = static_cast<BufferDescriptor *>(src_desc)->GetBuffer();
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002269 if (!ValidateBufferUsage(device_data_->GetBufferState(buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002270 std::stringstream error_str;
2271 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
2272 *error_msg = error_str.str();
2273 return false;
2274 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06002275 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002276 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002277 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002278 case InlineUniform:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002279 case AccelerationStructure:
Jeff Bolze54ae892018-09-08 12:16:29 -05002280 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002281 default:
2282 assert(0); // We've already verified update type so should never get here
2283 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002284 }
2285 // All checks passed so update contents are good
2286 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12002287}
Tobin Ehlisf320b192017-03-14 11:22:50 -06002288// Update the common AllocateDescriptorSetsData
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002289void CoreChecks::UpdateAllocateDescriptorSetsData(const VkDescriptorSetAllocateInfo *p_alloc_info,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002290 cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisf320b192017-03-14 11:22:50 -06002291 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002292 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
Tobin Ehlisf320b192017-03-14 11:22:50 -06002293 if (layout) {
2294 ds_data->layout_nodes[i] = layout;
2295 // Count total descriptors required per type
2296 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
2297 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
2298 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
2299 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
2300 }
2301 }
2302 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
2303 }
Petr Kraus13c98a62017-12-09 00:22:39 +01002304}
Tobin Ehlisee471462016-05-26 11:21:59 -06002305// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002306bool CoreChecks::ValidateAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002307 const cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002308 bool skip = false;
Mark Lobodzinski7804bd42019-03-06 11:28:48 -07002309 auto pool_state = GetDescriptorPoolState(p_alloc_info->descriptorPool);
Tobin Ehlisee471462016-05-26 11:21:59 -06002310
2311 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002312 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07002313 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
John Zulauf1d27e0a2018-11-05 10:12:48 -07002314 if (layout->IsPushDescriptor()) {
John Zulauf5562d062018-01-24 11:54:05 -07002315 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 -06002316 HandleToUint64(p_alloc_info->pSetLayouts[i]), "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-00308",
Lockeca0d9792019-03-03 23:48:13 -07002317 "Layout %s specified at pSetLayouts[%" PRIu32
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002318 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
Lockeca0d9792019-03-03 23:48:13 -07002319 report_data->FormatHandle(p_alloc_info->pSetLayouts[i]).c_str(), i,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002320 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07002321 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002322 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT &&
2323 !(pool_state->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
2324 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 -06002325 0, "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-03044",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002326 "Descriptor set layout create flags and pool create flags mismatch for index (%d)", i);
2327 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002328 }
2329 }
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002330 if (!device_extensions.vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002331 // Track number of descriptorSets allowable in this pool
2332 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002333 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 -06002334 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorSetCount-00306",
Lockeca0d9792019-03-03 23:48:13 -07002335 "Unable to allocate %u descriptorSets from pool %s"
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002336 ". This pool only has %d descriptorSets remaining.",
Lockeca0d9792019-03-03 23:48:13 -07002337 p_alloc_info->descriptorSetCount, report_data->FormatHandle(pool_state->pool).c_str(),
2338 pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002339 }
2340 // Determine whether descriptor counts are satisfiable
Jeff Bolze54ae892018-09-08 12:16:29 -05002341 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2342 if (ds_data->required_descriptors_by_type.at(it->first) > pool_state->availableDescriptorTypeCount[it->first]) {
Lockeca0d9792019-03-03 23:48:13 -07002343 skip |= log_msg(
2344 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
2345 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307",
2346 "Unable to allocate %u descriptors of type %s from pool %s"
2347 ". This pool only has %d descriptors of this type remaining.",
2348 ds_data->required_descriptors_by_type.at(it->first), string_VkDescriptorType(VkDescriptorType(it->first)),
2349 report_data->FormatHandle(pool_state->pool).c_str(), pool_state->availableDescriptorTypeCount[it->first]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002350 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002351 }
2352 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06002353
Jeff Bolzfdf96072018-04-10 14:32:18 -05002354 const auto *count_allocate_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2355
2356 if (count_allocate_info) {
2357 if (count_allocate_info->descriptorSetCount != 0 &&
2358 count_allocate_info->descriptorSetCount != p_alloc_info->descriptorSetCount) {
2359 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 -06002360 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-descriptorSetCount-03045",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002361 "VkDescriptorSetAllocateInfo::descriptorSetCount (%d) != "
2362 "VkDescriptorSetVariableDescriptorCountAllocateInfoEXT::descriptorSetCount (%d)",
2363 p_alloc_info->descriptorSetCount, count_allocate_info->descriptorSetCount);
2364 }
2365 if (count_allocate_info->descriptorSetCount == p_alloc_info->descriptorSetCount) {
2366 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002367 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002368 if (count_allocate_info->pDescriptorCounts[i] > layout->GetDescriptorCountFromBinding(layout->GetMaxBinding())) {
2369 skip |= log_msg(
2370 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 -06002371 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-pSetLayouts-03046",
2372 "pDescriptorCounts[%d] = (%d), binding's descriptorCount = (%d)", i,
Jeff Bolzfdf96072018-04-10 14:32:18 -05002373 count_allocate_info->pDescriptorCounts[i], layout->GetDescriptorCountFromBinding(layout->GetMaxBinding()));
2374 }
2375 }
2376 }
2377 }
2378
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002379 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06002380}
2381// Decrement allocated sets from the pool and insert new sets into set_map
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002382void CoreChecks::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
2383 const VkDescriptorSet *descriptor_sets,
Mark Lobodzinskia33af952019-04-25 14:59:05 -06002384 const cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
2385 auto pool_state = descriptorPoolMap[p_alloc_info->descriptorPool].get();
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002386 // Account for sets and individual descriptors allocated from pool
Tobin Ehlisee471462016-05-26 11:21:59 -06002387 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Jeff Bolze54ae892018-09-08 12:16:29 -05002388 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2389 pool_state->availableDescriptorTypeCount[it->first] -= ds_data->required_descriptors_by_type.at(it->first);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06002390 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002391
2392 const auto *variable_count_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2393 bool variable_count_valid = variable_count_info && variable_count_info->descriptorSetCount == p_alloc_info->descriptorSetCount;
2394
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002395 // Create tracking object for each descriptor set; insert into global map and the pool's set.
Tobin Ehlisee471462016-05-26 11:21:59 -06002396 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -05002397 uint32_t variable_count = variable_count_valid ? variable_count_info->pDescriptorCounts[i] : 0;
2398
Mark Lobodzinski14112ab2019-04-25 15:29:34 -06002399 std::unique_ptr<cvdescriptorset::DescriptorSet> new_ds(new cvdescriptorset::DescriptorSet(
2400 descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i], variable_count, this));
2401 pool_state->sets.insert(new_ds.get());
Tobin Ehlisee471462016-05-26 11:21:59 -06002402 new_ds->in_use.store(0);
Mark Lobodzinski14112ab2019-04-25 15:29:34 -06002403 setMap[descriptor_sets[i]] = std::move(new_ds);
Tobin Ehlisee471462016-05-26 11:21:59 -06002404 }
2405}
John Zulauf48a6a702017-12-22 17:14:54 -07002406
2407cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06002408 CMD_BUFFER_STATE *cb_state)
John Zulauf48a6a702017-12-22 17:14:54 -07002409 : filtered_map_(), orig_map_(in_map) {
2410 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2411 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2412 ds.FilterAndTrackBindingReqs(cb_state, orig_map_, filtered_map_.get());
2413 }
2414}
2415cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06002416 CMD_BUFFER_STATE *cb_state, PIPELINE_STATE *pipeline)
John Zulauf48a6a702017-12-22 17:14:54 -07002417 : filtered_map_(), orig_map_(in_map) {
2418 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2419 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2420 ds.FilterAndTrackBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
2421 }
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01002422}
John Zulauf4a015c92019-06-04 09:50:05 -06002423
2424// Starting at offset descriptor of given binding, parse over update_count
2425// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
2426// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
2427// If so, return true. If not, fill in error_msg and return false
2428bool cvdescriptorset::VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator current_binding, uint32_t offset,
2429 uint32_t update_count, const char *type, const VkDescriptorSet set,
2430 std::string *error_msg) {
2431 // Verify consecutive bindings match (if needed)
2432 auto orig_binding = current_binding;
2433 // Track count of descriptors in the current_bindings that are remaining to be updated
2434 auto binding_remaining = current_binding.GetDescriptorCount();
2435 // First, it's legal to offset beyond your own binding so handle that case
2436 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
2437 while (offset >= binding_remaining && !current_binding.AtEnd()) {
2438 // Advance to next binding, decrement offset by binding size
2439 offset -= binding_remaining;
2440 ++current_binding;
2441 binding_remaining = current_binding.GetDescriptorCount(); // Accessors are safe if AtEnd
2442 }
2443 assert(!current_binding.AtEnd()); // As written assumes range check has been made before calling
2444 binding_remaining -= offset;
2445 while (update_count > binding_remaining) { // While our updates overstep current binding
2446 // Verify next consecutive binding matches type, stage flags & immutable sampler use
2447 auto next_binding = current_binding.Next();
2448 if (!current_binding.IsConsistent(next_binding)) {
2449 std::stringstream error_str;
2450 error_str << "Attempting " << type;
2451 if (current_binding.Layout()->IsPushDescriptor()) {
2452 error_str << " push descriptors";
2453 } else {
2454 error_str << " descriptor set " << set;
2455 }
2456 error_str << " binding #" << orig_binding.Binding() << " with #" << update_count
2457 << " descriptors being updated but this update oversteps the bounds of this binding and the next binding is "
2458 "not consistent with current binding so this update is invalid.";
2459 *error_msg = error_str.str();
2460 return false;
2461 }
2462 current_binding = next_binding;
2463 // For sake of this check consider the bindings updated and grab count for next binding
2464 update_count -= binding_remaining;
2465 binding_remaining = current_binding.GetDescriptorCount();
2466 }
2467 return true;
2468}
John Zulauf4956fff2019-06-04 16:54:38 -06002469
2470// Validate the state for a given write update but don't actually perform the update
2471// If an error would occur for this update, return false and fill in details in error_msg string
2472bool cvdescriptorset::ValidateWriteUpdate(const DescriptorSet *dest_set, const debug_report_data *report_data,
2473 const VkWriteDescriptorSet *update, const char *func_name, std::string *error_code,
2474 std::string *error_msg) {
2475 const auto dest_layout = dest_set->GetLayout();
2476
2477 // Verify dst layout still valid
2478 if (dest_layout->IsDestroyed()) {
2479 *error_code = "VUID-VkWriteDescriptorSet-dstSet-00320";
2480 string_sprintf(error_msg, "Cannot call %s to perform write update on %s which has been destroyed", func_name,
2481 dest_set->StringifySetAndLayout().c_str());
2482 return false;
2483 }
2484 // Verify dst binding exists
2485 if (!dest_layout->HasBinding(update->dstBinding)) {
2486 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00315";
2487 std::stringstream error_str;
2488 error_str << dest_set->StringifySetAndLayout() << " does not have binding " << update->dstBinding;
2489 *error_msg = error_str.str();
2490 return false;
2491 }
2492
2493 DescriptorSetLayout::ConstBindingIterator dest(dest_layout.get(), update->dstBinding);
2494 // Make sure binding isn't empty
2495 if (0 == dest.GetDescriptorCount()) {
2496 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00316";
2497 std::stringstream error_str;
2498 error_str << dest_set->StringifySetAndLayout() << " cannot updated binding " << update->dstBinding
2499 << " that has 0 descriptors";
2500 *error_msg = error_str.str();
2501 return false;
2502 }
2503
2504 // Verify idle ds
2505 if (dest_set->in_use.load() && !(dest.GetDescriptorBindingFlags() & (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
2506 VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
2507 // TODO : Re-using Free Idle error code, need write update idle error code
2508 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
2509 std::stringstream error_str;
2510 error_str << "Cannot call " << func_name << " to perform write update on " << dest_set->StringifySetAndLayout()
2511 << " that is in use by a command buffer";
2512 *error_msg = error_str.str();
2513 return false;
2514 }
2515 // We know that binding is valid, verify update and do update on each descriptor
2516 auto start_idx = dest.GetGlobalIndexRange().start + update->dstArrayElement;
2517 auto type = dest.GetType();
2518 if (type != update->descriptorType) {
2519 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00319";
2520 std::stringstream error_str;
2521 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2522 << " with type " << string_VkDescriptorType(type) << " but update type is "
2523 << string_VkDescriptorType(update->descriptorType);
2524 *error_msg = error_str.str();
2525 return false;
2526 }
2527 auto total_descriptors = dest_layout->GetTotalDescriptorCount();
2528 if (update->descriptorCount > (total_descriptors - start_idx)) {
2529 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
2530 std::stringstream error_str;
2531 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2532 << " with " << total_descriptors - start_idx
2533 << " descriptors in that binding and all successive bindings of the set, but update of "
2534 << update->descriptorCount << " descriptors combined with update array element offset of "
2535 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
2536 *error_msg = error_str.str();
2537 return false;
2538 }
2539 if (type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
2540 if ((update->dstArrayElement % 4) != 0) {
2541 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02219";
2542 std::stringstream error_str;
2543 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2544 << " with "
2545 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
2546 *error_msg = error_str.str();
2547 return false;
2548 }
2549 if ((update->descriptorCount % 4) != 0) {
2550 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02220";
2551 std::stringstream error_str;
2552 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2553 << " with "
2554 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
2555 *error_msg = error_str.str();
2556 return false;
2557 }
2558 const auto *write_inline_info = lvl_find_in_chain<VkWriteDescriptorSetInlineUniformBlockEXT>(update->pNext);
2559 if (!write_inline_info || write_inline_info->dataSize != update->descriptorCount) {
2560 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02221";
2561 std::stringstream error_str;
2562 if (!write_inline_info) {
2563 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #"
2564 << update->dstBinding << " with "
2565 << "VkWriteDescriptorSetInlineUniformBlockEXT missing";
2566 } else {
2567 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #"
2568 << update->dstBinding << " with "
2569 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2570 << " not equal to "
2571 << "VkWriteDescriptorSet descriptorCount " << update->descriptorCount;
2572 }
2573 *error_msg = error_str.str();
2574 return false;
2575 }
2576 // This error is probably unreachable due to the previous two errors
2577 if (write_inline_info && (write_inline_info->dataSize % 4) != 0) {
2578 *error_code = "VUID-VkWriteDescriptorSetInlineUniformBlockEXT-dataSize-02222";
2579 std::stringstream error_str;
2580 error_str << "Attempting write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2581 << " with "
2582 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2583 << " not a multiple of 4";
2584 *error_msg = error_str.str();
2585 return false;
2586 }
2587 }
2588 // Verify consecutive bindings match (if needed)
2589 if (!VerifyUpdateConsistency(DescriptorSetLayout::ConstBindingIterator(dest_layout.get(), update->dstBinding),
2590 update->dstArrayElement, update->descriptorCount, "write update to", dest_set->GetSet(),
2591 error_msg)) {
2592 // TODO : Should break out "consecutive binding updates" language into valid usage statements
2593 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
2594 return false;
2595 }
2596 // Update is within bounds and consistent so last step is to validate update contents
2597 if (!dest_set->VerifyWriteUpdateContents(update, start_idx, func_name, error_code, error_msg)) {
2598 std::stringstream error_str;
2599 error_str << "Write update to " << dest_set->StringifySetAndLayout() << " binding #" << update->dstBinding
2600 << " failed with error message: " << error_msg->c_str();
2601 *error_msg = error_str.str();
2602 return false;
2603 }
2604 // All checks passed, update is clean
2605 return true;
2606}