blob: eb3ed4f5f481a9c64a03f1e943723eab28dfc91b [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 Zulaufd47d0612018-02-16 13:00:34 -070055using DescriptorSetLayoutDef = cvdescriptorset::DescriptorSetLayoutDef;
56using DescriptorSetLayoutId = cvdescriptorset::DescriptorSetLayoutId;
57
John Zulauf34ebf272018-02-16 13:08:47 -070058// Canonical dictionary of DescriptorSetLayoutDef (without any handle/device specific information)
59cvdescriptorset::DescriptorSetLayoutDict descriptor_set_layout_dict;
John Zulaufd47d0612018-02-16 13:00:34 -070060
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060061DescriptorSetLayoutId GetCanonicalId(const VkDescriptorSetLayoutCreateInfo *p_create_info) {
John Zulauf34ebf272018-02-16 13:08:47 -070062 return descriptor_set_layout_dict.look_up(DescriptorSetLayoutDef(p_create_info));
John Zulaufd47d0612018-02-16 13:00:34 -070063}
John Zulauf34ebf272018-02-16 13:08:47 -070064
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060065// Construct DescriptorSetLayout instance from given create info
John Zulauf48a6a702017-12-22 17:14:54 -070066// Proactively reserve and resize as possible, as the reallocation was visible in profiling
John Zulauf1f8174b2018-02-16 12:58:37 -070067cvdescriptorset::DescriptorSetLayoutDef::DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info)
68 : flags_(p_create_info->flags), binding_count_(0), descriptor_count_(0), dynamic_descriptor_count_(0) {
Jeff Bolzfdf96072018-04-10 14:32:18 -050069 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(p_create_info->pNext);
70
John Zulauf48a6a702017-12-22 17:14:54 -070071 binding_type_stats_ = {0, 0, 0};
Jeff Bolzfdf96072018-04-10 14:32:18 -050072 std::set<ExtendedBinding, BindingNumCmp> sorted_bindings;
John Zulauf508d13a2018-01-05 15:10:34 -070073 const uint32_t input_bindings_count = p_create_info->bindingCount;
74 // Sort the input bindings in binding number order, eliminating duplicates
75 for (uint32_t i = 0; i < input_bindings_count; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -050076 VkDescriptorBindingFlagsEXT flags = 0;
77 if (flags_create_info && flags_create_info->bindingCount == p_create_info->bindingCount) {
78 flags = flags_create_info->pBindingFlags[i];
79 }
80 sorted_bindings.insert(ExtendedBinding(p_create_info->pBindings + i, flags));
John Zulaufb6d71202017-12-22 16:47:09 -070081 }
82
83 // Store the create info in the sorted order from above
Tobin Ehlisa3525e02016-11-17 10:50:52 -070084 std::map<uint32_t, uint32_t> binding_to_dyn_count;
John Zulauf508d13a2018-01-05 15:10:34 -070085 uint32_t index = 0;
86 binding_count_ = static_cast<uint32_t>(sorted_bindings.size());
87 bindings_.reserve(binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -050088 binding_flags_.reserve(binding_count_);
John Zulauf508d13a2018-01-05 15:10:34 -070089 binding_to_index_map_.reserve(binding_count_);
90 for (auto input_binding : sorted_bindings) {
91 // Add to binding and map, s.t. it is robust to invalid duplication of binding_num
Jeff Bolzfdf96072018-04-10 14:32:18 -050092 const auto binding_num = input_binding.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070093 binding_to_index_map_[binding_num] = index++;
Jeff Bolzfdf96072018-04-10 14:32:18 -050094 bindings_.emplace_back(input_binding.layout_binding);
John Zulauf508d13a2018-01-05 15:10:34 -070095 auto &binding_info = bindings_.back();
Jeff Bolzfdf96072018-04-10 14:32:18 -050096 binding_flags_.emplace_back(input_binding.binding_flags);
John Zulauf508d13a2018-01-05 15:10:34 -070097
John Zulaufb6d71202017-12-22 16:47:09 -070098 descriptor_count_ += binding_info.descriptorCount;
99 if (binding_info.descriptorCount > 0) {
100 non_empty_bindings_.insert(binding_num);
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700101 }
John Zulaufb6d71202017-12-22 16:47:09 -0700102
103 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
104 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
105 binding_to_dyn_count[binding_num] = binding_info.descriptorCount;
106 dynamic_descriptor_count_ += binding_info.descriptorCount;
John Zulauf48a6a702017-12-22 17:14:54 -0700107 binding_type_stats_.dynamic_buffer_count++;
108 } else if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
109 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
110 binding_type_stats_.non_dynamic_buffer_count++;
111 } else {
112 binding_type_stats_.image_sampler_count++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600113 }
114 }
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700115 assert(bindings_.size() == binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500116 assert(binding_flags_.size() == binding_count_);
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700117 uint32_t global_index = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700118 binding_to_global_index_range_map_.reserve(binding_count_);
119 // Vector order is finalized so create maps of bindings to descriptors and descriptors to indices
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700120 for (uint32_t i = 0; i < binding_count_; ++i) {
121 auto binding_num = bindings_[i].binding;
John Zulaufc483f442017-12-15 14:02:06 -0700122 auto final_index = global_index + bindings_[i].descriptorCount;
123 binding_to_global_index_range_map_[binding_num] = IndexRange(global_index, final_index);
John Zulaufb6d71202017-12-22 16:47:09 -0700124 if (final_index != global_index) {
125 global_start_to_index_map_[global_index] = i;
126 }
John Zulaufc483f442017-12-15 14:02:06 -0700127 global_index = final_index;
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700128 }
John Zulaufb6d71202017-12-22 16:47:09 -0700129
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700130 // Now create dyn offset array mapping for any dynamic descriptors
131 uint32_t dyn_array_idx = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700132 binding_to_dynamic_array_idx_map_.reserve(binding_to_dyn_count.size());
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700133 for (const auto &bc_pair : binding_to_dyn_count) {
134 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
135 dyn_array_idx += bc_pair.second;
136 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600137}
Tobin Ehlis154c2692016-10-25 09:36:53 -0600138
John Zulaufd47d0612018-02-16 13:00:34 -0700139size_t cvdescriptorset::DescriptorSetLayoutDef::hash() const {
140 hash_util::HashCombiner hc;
141 hc << flags_;
142 hc.Combine(bindings_);
John Zulauf223b69d2018-11-09 16:00:59 -0700143 hc.Combine(binding_flags_);
John Zulaufd47d0612018-02-16 13:00:34 -0700144 return hc.Value();
145}
146//
147
John Zulauf1f8174b2018-02-16 12:58:37 -0700148// Return valid index or "end" i.e. binding_count_;
149// The asserts in "Get" are reduced to the set where no valid answer(like null or 0) could be given
150// Common code for all binding lookups.
151uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromBinding(uint32_t binding) const {
152 const auto &bi_itr = binding_to_index_map_.find(binding);
153 if (bi_itr != binding_to_index_map_.cend()) return bi_itr->second;
154 return GetBindingCount();
155}
156VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorSetLayoutBindingPtrFromIndex(
157 const uint32_t index) const {
158 if (index >= bindings_.size()) return nullptr;
159 return bindings_[index].ptr();
160}
161// Return descriptorCount for given index, 0 if index is unavailable
162uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorCountFromIndex(const uint32_t index) const {
163 if (index >= bindings_.size()) return 0;
164 return bindings_[index].descriptorCount;
165}
166// For the given index, return descriptorType
167VkDescriptorType cvdescriptorset::DescriptorSetLayoutDef::GetTypeFromIndex(const uint32_t index) const {
168 assert(index < bindings_.size());
169 if (index < bindings_.size()) return bindings_[index].descriptorType;
170 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
171}
172// For the given index, return stageFlags
173VkShaderStageFlags cvdescriptorset::DescriptorSetLayoutDef::GetStageFlagsFromIndex(const uint32_t index) const {
174 assert(index < bindings_.size());
175 if (index < bindings_.size()) return bindings_[index].stageFlags;
176 return VkShaderStageFlags(0);
177}
Jeff Bolzfdf96072018-04-10 14:32:18 -0500178// Return binding flags for given index, 0 if index is unavailable
179VkDescriptorBindingFlagsEXT cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorBindingFlagsFromIndex(
180 const uint32_t index) const {
181 if (index >= binding_flags_.size()) return 0;
182 return binding_flags_[index];
183}
John Zulauf1f8174b2018-02-16 12:58:37 -0700184
185// For the given global index, return index
186uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromGlobalIndex(const uint32_t global_index) const {
187 auto start_it = global_start_to_index_map_.upper_bound(global_index);
188 uint32_t index = binding_count_;
189 assert(start_it != global_start_to_index_map_.cbegin());
190 if (start_it != global_start_to_index_map_.cbegin()) {
191 --start_it;
192 index = start_it->second;
193#ifndef NDEBUG
194 const auto &range = GetGlobalIndexRangeFromBinding(bindings_[index].binding);
195 assert(range.start <= global_index && global_index < range.end);
196#endif
197 }
198 return index;
199}
200
201// For the given binding, return the global index range
202// As start and end are often needed in pairs, get both with a single hash lookup.
203const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromBinding(
204 const uint32_t binding) const {
205 assert(binding_to_global_index_range_map_.count(binding));
206 // In error case max uint32_t so index is out of bounds to break ASAP
207 const static IndexRange kInvalidRange = {0xFFFFFFFF, 0xFFFFFFFF};
208 const auto &range_it = binding_to_global_index_range_map_.find(binding);
209 if (range_it != binding_to_global_index_range_map_.end()) {
210 return range_it->second;
211 }
212 return kInvalidRange;
213}
214
215// For given binding, return ptr to ImmutableSampler array
216VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
217 const auto &bi_itr = binding_to_index_map_.find(binding);
218 if (bi_itr != binding_to_index_map_.end()) {
219 return bindings_[bi_itr->second].pImmutableSamplers;
220 }
221 return nullptr;
222}
223// Move to next valid binding having a non-zero binding count
224uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetNextValidBinding(const uint32_t binding) const {
225 auto it = non_empty_bindings_.upper_bound(binding);
226 assert(it != non_empty_bindings_.cend());
227 if (it != non_empty_bindings_.cend()) return *it;
228 return GetMaxBinding() + 1;
229}
230// For given index, return ptr to ImmutableSampler array
231VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
232 if (index < bindings_.size()) {
233 return bindings_[index].pImmutableSamplers;
234 }
235 return nullptr;
236}
237// If our layout is compatible with rh_ds_layout, return true,
238// else return false and fill in error_msg will description of what causes incompatibility
239bool cvdescriptorset::DescriptorSetLayout::IsCompatible(DescriptorSetLayout const *const rh_ds_layout,
240 std::string *error_msg) const {
241 // Trivial case
242 if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) return true;
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600243 if (GetLayoutDef() == rh_ds_layout->GetLayoutDef()) return true;
John Zulaufd47d0612018-02-16 13:00:34 -0700244 bool detailed_compat_check =
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600245 GetLayoutDef()->IsCompatible(layout_, rh_ds_layout->GetDescriptorSetLayout(), rh_ds_layout->GetLayoutDef(), error_msg);
John Zulaufd47d0612018-02-16 13:00:34 -0700246 // The detailed check should never tell us mismatching DSL are compatible
247 assert(!detailed_compat_check);
248 return detailed_compat_check;
John Zulauf1f8174b2018-02-16 12:58:37 -0700249}
250
John Zulaufdf3c5c12018-03-06 16:44:43 -0700251// Do a detailed compatibility check of this def (referenced by ds_layout), vs. the rhs (layout and def)
252// Should only be called if trivial accept has failed, and in that context should return false.
John Zulauf1f8174b2018-02-16 12:58:37 -0700253bool cvdescriptorset::DescriptorSetLayoutDef::IsCompatible(VkDescriptorSetLayout ds_layout, VkDescriptorSetLayout rh_ds_layout,
254 DescriptorSetLayoutDef const *const rh_ds_layout_def,
255 std::string *error_msg) const {
256 if (descriptor_count_ != rh_ds_layout_def->descriptor_count_) {
257 std::stringstream error_str;
258 error_str << "DescriptorSetLayout " << ds_layout << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
259 << rh_ds_layout << ", which comes from pipelineLayout, has " << rh_ds_layout_def->descriptor_count_
260 << " descriptors.";
261 *error_msg = error_str.str();
262 return false; // trivial fail case
263 }
John Zulaufd47d0612018-02-16 13:00:34 -0700264
John Zulauf1f8174b2018-02-16 12:58:37 -0700265 // Descriptor counts match so need to go through bindings one-by-one
266 // and verify that type and stageFlags match
267 for (auto binding : bindings_) {
268 // TODO : Do we also need to check immutable samplers?
269 // VkDescriptorSetLayoutBinding *rh_binding;
270 if (binding.descriptorCount != rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding)) {
271 std::stringstream error_str;
272 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has a descriptorCount of "
273 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
274 << rh_ds_layout << ", which comes from pipelineLayout, has a descriptorCount of "
275 << rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding);
276 *error_msg = error_str.str();
277 return false;
278 } else if (binding.descriptorType != rh_ds_layout_def->GetTypeFromBinding(binding.binding)) {
279 std::stringstream error_str;
280 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " is type '"
281 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
282 << " for DescriptorSetLayout " << rh_ds_layout << ", which comes from pipelineLayout, is type '"
283 << string_VkDescriptorType(rh_ds_layout_def->GetTypeFromBinding(binding.binding)) << "'";
284 *error_msg = error_str.str();
285 return false;
286 } else if (binding.stageFlags != rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding)) {
287 std::stringstream error_str;
288 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has stageFlags "
289 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout " << rh_ds_layout
290 << ", which comes from pipelineLayout, has stageFlags "
291 << rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding);
292 *error_msg = error_str.str();
293 return false;
294 }
295 }
296 return true;
297}
298
299bool cvdescriptorset::DescriptorSetLayoutDef::IsNextBindingConsistent(const uint32_t binding) const {
300 if (!binding_to_index_map_.count(binding + 1)) return false;
301 auto const &bi_itr = binding_to_index_map_.find(binding);
302 if (bi_itr != binding_to_index_map_.end()) {
303 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
304 if (next_bi_itr != binding_to_index_map_.end()) {
305 auto type = bindings_[bi_itr->second].descriptorType;
306 auto stage_flags = bindings_[bi_itr->second].stageFlags;
307 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500308 auto flags = binding_flags_[bi_itr->second];
John Zulauf1f8174b2018-02-16 12:58:37 -0700309 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
310 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
Jeff Bolzfdf96072018-04-10 14:32:18 -0500311 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false)) ||
312 (flags != binding_flags_[next_bi_itr->second])) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700313 return false;
314 }
315 return true;
316 }
317 }
318 return false;
319}
320// Starting at offset descriptor of given binding, parse over update_count
321// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
322// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
323// If so, return true. If not, fill in error_msg and return false
324bool cvdescriptorset::DescriptorSetLayoutDef::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset,
325 uint32_t update_count, const char *type,
326 const VkDescriptorSet set, std::string *error_msg) const {
327 // Verify consecutive bindings match (if needed)
328 auto orig_binding = current_binding;
329 // Track count of descriptors in the current_bindings that are remaining to be updated
330 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
331 // First, it's legal to offset beyond your own binding so handle that case
332 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
333 while (offset >= binding_remaining) {
334 // Advance to next binding, decrement offset by binding size
335 offset -= binding_remaining;
336 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
337 }
338 binding_remaining -= offset;
339 while (update_count > binding_remaining) { // While our updates overstep current binding
340 // Verify next consecutive binding matches type, stage flags & immutable sampler use
341 if (!IsNextBindingConsistent(current_binding++)) {
342 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -0600343 error_str << "Attempting " << type;
344 if (IsPushDescriptor()) {
345 error_str << " push descriptors";
346 } else {
347 error_str << " descriptor set " << set;
348 }
349 error_str << " binding #" << orig_binding << " with #" << update_count
John Zulauf1f8174b2018-02-16 12:58:37 -0700350 << " descriptors being updated but this update oversteps the bounds of this binding and the next binding is "
351 "not consistent with current binding so this update is invalid.";
352 *error_msg = error_str.str();
353 return false;
354 }
355 // For sake of this check consider the bindings updated and grab count for next binding
356 update_count -= binding_remaining;
357 binding_remaining = GetDescriptorCountFromBinding(current_binding);
358 }
359 return true;
360}
361
362// The DescriptorSetLayout stores the per handle data for a descriptor set layout, and references the common defintion for the
363// handle invariant portion
364cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
365 const VkDescriptorSetLayout layout)
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600366 : layout_(layout), layout_destroyed_(false), layout_id_(GetCanonicalId(p_create_info)) {}
John Zulauf1f8174b2018-02-16 12:58:37 -0700367
Tobin Ehlis154c2692016-10-25 09:36:53 -0600368// Validate descriptor set layout create info
Jeff Bolzfdf96072018-04-10 14:32:18 -0500369bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(
370 const debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *create_info, const bool push_descriptor_ext,
371 const uint32_t max_push_descriptors, const bool descriptor_indexing_ext,
Jeff Bolze54ae892018-09-08 12:16:29 -0500372 const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features,
373 const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features,
374 const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props) {
Tobin Ehlis154c2692016-10-25 09:36:53 -0600375 bool skip = false;
376 std::unordered_set<uint32_t> bindings;
John Zulauf0fdeab32018-01-23 11:27:35 -0700377 uint64_t total_descriptors = 0;
378
Jeff Bolzfdf96072018-04-10 14:32:18 -0500379 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(create_info->pNext);
380
381 const bool push_descriptor_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR);
John Zulauf0fdeab32018-01-23 11:27:35 -0700382 if (push_descriptor_set && !push_descriptor_ext) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600383 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 -0600384 kVUID_Core_DrawState_ExtensionNotEnabled,
Mark Lobodzinskifb5a3e62018-04-13 10:46:48 -0600385 "Attempted to use %s in %s but its required extension %s has not been enabled.\n",
John Zulauf0fdeab32018-01-23 11:27:35 -0700386 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR", "VkDescriptorSetLayoutCreateInfo::flags",
387 VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
388 }
389
Jeff Bolzfdf96072018-04-10 14:32:18 -0500390 const bool update_after_bind_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT);
391 if (update_after_bind_set && !descriptor_indexing_ext) {
392 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 -0600393 kVUID_Core_DrawState_ExtensionNotEnabled,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500394 "Attemped to use %s in %s but its required extension %s has not been enabled.\n",
395 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT", "VkDescriptorSetLayoutCreateInfo::flags",
396 VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
397 }
398
John Zulauf0fdeab32018-01-23 11:27:35 -0700399 auto valid_type = [push_descriptor_set](const VkDescriptorType type) {
400 return !push_descriptor_set ||
Dave Houlton142c4cb2018-10-17 15:04:41 -0600401 ((type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && (type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) &&
Jeff Bolze54ae892018-09-08 12:16:29 -0500402 (type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT));
John Zulauf0fdeab32018-01-23 11:27:35 -0700403 };
404
Jeff Bolzfdf96072018-04-10 14:32:18 -0500405 uint32_t max_binding = 0;
406
Tobin Ehlis154c2692016-10-25 09:36:53 -0600407 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
John Zulauf0fdeab32018-01-23 11:27:35 -0700408 const auto &binding_info = create_info->pBindings[i];
Jeff Bolzfdf96072018-04-10 14:32:18 -0500409 max_binding = std::max(max_binding, binding_info.binding);
410
John Zulauf0fdeab32018-01-23 11:27:35 -0700411 if (!bindings.insert(binding_info.binding).second) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600412 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 -0600413 "VUID-VkDescriptorSetLayoutCreateInfo-binding-00279",
414 "duplicated binding number in VkDescriptorSetLayoutBinding.");
Tobin Ehlis154c2692016-10-25 09:36:53 -0600415 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700416 if (!valid_type(binding_info.descriptorType)) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600417 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 -0600418 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
419 ? "VUID-VkDescriptorSetLayoutCreateInfo-flags-02208"
420 : "VUID-VkDescriptorSetLayoutCreateInfo-flags-00280",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600421 "invalid type %s ,for push descriptors in VkDescriptorSetLayoutBinding entry %" PRIu32 ".",
422 string_VkDescriptorType(binding_info.descriptorType), i);
John Zulauf0fdeab32018-01-23 11:27:35 -0700423 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500424
425 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
426 if ((binding_info.descriptorCount % 4) != 0) {
427 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
428 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02209",
Dave Houlton142c4cb2018-10-17 15:04:41 -0600429 "descriptorCount =(%" PRIu32 ") must be a multiple of 4", binding_info.descriptorCount);
Jeff Bolze54ae892018-09-08 12:16:29 -0500430 }
431 if (binding_info.descriptorCount > inline_uniform_block_props->maxInlineUniformBlockSize) {
432 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
433 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02210",
434 "descriptorCount =(%" PRIu32 ") must be less than or equal to maxInlineUniformBlockSize",
435 binding_info.descriptorCount);
436 }
437 }
438
John Zulauf0fdeab32018-01-23 11:27:35 -0700439 total_descriptors += binding_info.descriptorCount;
Tobin Ehlis154c2692016-10-25 09:36:53 -0600440 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700441
Jeff Bolzfdf96072018-04-10 14:32:18 -0500442 if (flags_create_info) {
443 if (flags_create_info->bindingCount != 0 && flags_create_info->bindingCount != create_info->bindingCount) {
444 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 -0600445 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-bindingCount-03002",
Jeff Bolzfdf96072018-04-10 14:32:18 -0500446 "VkDescriptorSetLayoutCreateInfo::bindingCount (%d) != "
447 "VkDescriptorSetLayoutBindingFlagsCreateInfoEXT::bindingCount (%d)",
448 create_info->bindingCount, flags_create_info->bindingCount);
449 }
450
451 if (flags_create_info->bindingCount == create_info->bindingCount) {
452 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
453 const auto &binding_info = create_info->pBindings[i];
454
455 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) {
456 if (!update_after_bind_set) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600457 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
458 "VUID-VkDescriptorSetLayoutCreateInfo-flags-03000",
459 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500460 }
461
462 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER &&
463 !descriptor_indexing_features->descriptorBindingUniformBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600464 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
465 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
466 "descriptorBindingUniformBufferUpdateAfterBind-03005",
467 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500468 }
469 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
470 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
471 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) &&
472 !descriptor_indexing_features->descriptorBindingSampledImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600473 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
474 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
475 "descriptorBindingSampledImageUpdateAfterBind-03006",
476 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500477 }
478 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE &&
479 !descriptor_indexing_features->descriptorBindingStorageImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600480 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
481 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
482 "descriptorBindingStorageImageUpdateAfterBind-03007",
483 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500484 }
485 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER &&
486 !descriptor_indexing_features->descriptorBindingStorageBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600487 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
488 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
489 "descriptorBindingStorageBufferUpdateAfterBind-03008",
490 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500491 }
492 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
493 !descriptor_indexing_features->descriptorBindingUniformTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600494 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
495 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
496 "descriptorBindingUniformTexelBufferUpdateAfterBind-03009",
497 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500498 }
499 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER &&
500 !descriptor_indexing_features->descriptorBindingStorageTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600501 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
502 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
503 "descriptorBindingStorageTexelBufferUpdateAfterBind-03010",
504 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500505 }
506 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT ||
507 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
508 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600509 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
510 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-None-03011",
511 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500512 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500513
514 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT &&
515 !inline_uniform_block_features->descriptorBindingInlineUniformBlockUpdateAfterBind) {
516 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 -0600517 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
518 "descriptorBindingInlineUniformBlockUpdateAfterBind-02211",
519 "Invalid flags (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) for "
520 "VkDescriptorSetLayoutBinding entry %" PRIu32
521 " with descriptorBindingInlineUniformBlockUpdateAfterBind not enabled",
522 i);
Jeff Bolze54ae892018-09-08 12:16:29 -0500523 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500524 }
525
526 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT) {
527 if (!descriptor_indexing_features->descriptorBindingUpdateUnusedWhilePending) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600528 skip |= log_msg(
529 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
530 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingUpdateUnusedWhilePending-03012",
531 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500532 }
533 }
534
535 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) {
536 if (!descriptor_indexing_features->descriptorBindingPartiallyBound) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600537 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
538 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingPartiallyBound-03013",
539 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500540 }
541 }
542
543 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT) {
544 if (binding_info.binding != max_binding) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600545 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
546 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03004",
547 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500548 }
549
550 if (!descriptor_indexing_features->descriptorBindingVariableDescriptorCount) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600551 skip |= log_msg(
552 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
553 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingVariableDescriptorCount-03014",
554 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500555 }
556 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
557 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600558 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
559 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03015",
560 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500561 }
562 }
563
564 if (push_descriptor_set &&
565 (flags_create_info->pBindingFlags[i] &
566 (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
567 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT))) {
568 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 -0600569 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-flags-03003",
570 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500571 }
572 }
573 }
574 }
575
John Zulauf0fdeab32018-01-23 11:27:35 -0700576 if ((push_descriptor_set) && (total_descriptors > max_push_descriptors)) {
577 const char *undefined = push_descriptor_ext ? "" : " -- undefined";
Dave Houltond8ed0212018-05-16 17:18:24 -0600578 skip |=
579 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
580 "VUID-VkDescriptorSetLayoutCreateInfo-flags-00281",
581 "for push descriptor, total descriptor count in layout (%" PRIu64
582 ") must not be greater than VkPhysicalDevicePushDescriptorPropertiesKHR::maxPushDescriptors (%" PRIu32 "%s).",
583 total_descriptors, max_push_descriptors, undefined);
John Zulauf0fdeab32018-01-23 11:27:35 -0700584 }
585
Tobin Ehlis154c2692016-10-25 09:36:53 -0600586 return skip;
587}
588
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600589cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
590 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
591
Tobin Ehlis93f22372016-10-12 14:34:12 -0600592cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500593 const std::shared_ptr<DescriptorSetLayout const> &layout, uint32_t variable_count,
Mark Lobodzinski3840ca02019-03-08 18:36:11 -0700594 CoreChecks *dev_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700595 : some_update_(false),
596 set_(set),
597 pool_state_(nullptr),
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600598 p_layout_(layout),
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700599 device_data_(dev_data),
Mark Lobodzinski57a44272019-02-27 12:40:50 -0700600 limits_(dev_data->phys_dev_props.limits),
Jeff Bolzfdf96072018-04-10 14:32:18 -0500601 variable_count_(variable_count) {
Mark Lobodzinski7804bd42019-03-06 11:28:48 -0700602 pool_state_ = dev_data->GetDescriptorPoolState(pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600603 // Foreach binding, create default descriptors of given type
John Zulaufb6d71202017-12-22 16:47:09 -0700604 descriptors_.reserve(p_layout_->GetTotalDescriptorCount());
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600605 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
606 auto type = p_layout_->GetTypeFromIndex(i);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600607 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700608 case VK_DESCRIPTOR_TYPE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600609 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
610 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600611 if (immut_sampler) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700612 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600613 some_update_ = true; // Immutable samplers are updated at creation
614 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700615 descriptors_.emplace_back(new SamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700616 }
617 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600618 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700619 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600620 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
621 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600622 if (immut) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700623 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600624 some_update_ = true; // Immutable samplers are updated at creation
625 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700626 descriptors_.emplace_back(new ImageSamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700627 }
628 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600629 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700630 // ImageDescriptors
631 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
632 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
633 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600634 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700635 descriptors_.emplace_back(new ImageDescriptor(type));
636 break;
637 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
638 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600639 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700640 descriptors_.emplace_back(new TexelDescriptor(type));
641 break;
642 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
643 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
644 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
645 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600646 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700647 descriptors_.emplace_back(new BufferDescriptor(type));
648 break;
Jeff Bolze54ae892018-09-08 12:16:29 -0500649 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
650 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
651 descriptors_.emplace_back(new InlineUniformDescriptor(type));
652 break;
Eric Werness30127fd2018-10-31 21:01:03 -0700653 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -0500654 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
655 descriptors_.emplace_back(new AccelerationStructureDescriptor(type));
656 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700657 default:
658 assert(0); // Bad descriptor type specified
659 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600660 }
661 }
662}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600663
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700664cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200665
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600666static std::string StringDescriptorReqViewType(descriptor_req req) {
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700667 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200668 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
669 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700670 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700671 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200672 }
673 }
674
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700675 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700676
677 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200678}
679
Chris Forbesda01e8d2018-08-27 15:36:57 -0700680static char const *StringDescriptorReqComponentType(descriptor_req req) {
681 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_SINT) return "SINT";
682 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_UINT) return "UINT";
683 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT) return "FLOAT";
684 return "(none)";
685}
686
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600687// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600688bool cvdescriptorset::DescriptorSet::IsCompatible(DescriptorSetLayout const *const layout, std::string *error) const {
689 return layout->IsCompatible(p_layout_.get(), error);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600690}
Chris Forbes57989132016-07-26 17:06:10 +1200691
Chris Forbesda01e8d2018-08-27 15:36:57 -0700692static unsigned DescriptorRequirementsBitsFromFormat(VkFormat fmt) {
693 if (FormatIsSInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
694 if (FormatIsUInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
695 if (FormatIsDepthAndStencil(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT | DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
696 if (fmt == VK_FORMAT_UNDEFINED) return 0;
697 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
698 return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
699}
700
Tobin Ehlis3066db62016-08-22 08:12:23 -0600701// 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 -0600702// This includes validating that all descriptors in the given bindings are updated,
703// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
704// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600705bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
John Zulauf48a6a702017-12-22 17:14:54 -0700706 const std::vector<uint32_t> &dynamic_offsets, GLOBAL_CB_NODE *cb_node,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600707 const char *caller, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200708 for (auto binding_pair : bindings) {
709 auto binding = binding_pair.first;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600710 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600711 std::stringstream error_str;
712 error_str << "Attempting to validate DrawState for binding #" << binding
713 << " which is an invalid binding for this descriptor set.";
714 *error = error_str.str();
715 return false;
716 }
John Zulaufc483f442017-12-15 14:02:06 -0700717 IndexRange index_range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700718 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Jeff Bolzfdf96072018-04-10 14:32:18 -0500719
720 if (IsVariableDescriptorCount(binding)) {
721 // Only validate the first N descriptors if it uses variable_count
722 index_range.end = index_range.start + GetVariableDescriptorCount();
723 }
724
John Zulaufc483f442017-12-15 14:02:06 -0700725 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
Lockeb994adf2019-03-29 23:52:31 -0600726 uint32_t index = i - index_range.start;
727
Tobin Ehlisda77de72018-12-13 08:53:28 -0700728 if ((p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
729 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) ||
Jeff Bolze54ae892018-09-08 12:16:29 -0500730 descriptors_[i]->GetClass() == InlineUniform) {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500731 // Can't validate the descriptor because it may not have been updated,
732 // or the view could have been destroyed
733 continue;
734 } else if (!descriptors_[i]->updated) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700735 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600736 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700737 << " is being used in draw but has not been updated.";
738 *error = error_str.str();
739 return false;
740 } else {
741 auto descriptor_class = descriptors_[i]->GetClass();
742 if (descriptor_class == GeneralBuffer) {
743 // Verify that buffers are valid
744 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Mark Lobodzinski6ed74142019-03-06 11:35:39 -0700745 auto buffer_node = device_data_->GetBufferState(buffer);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700746 if (!buffer_node) {
747 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600748 error_str << "Descriptor in binding #" << binding << " index " << index << " references invalid buffer "
749 << buffer << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700750 *error = error_str.str();
751 return false;
John Zulauf48a6a702017-12-22 17:14:54 -0700752 } else if (!buffer_node->sparse) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700753 for (auto mem_binding : buffer_node->GetBoundMemory()) {
Mark Lobodzinski97f83f72019-03-07 10:01:52 -0700754 if (!device_data_->GetMemObjInfo(mem_binding)) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700755 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600756 error_str << "Descriptor in binding #" << binding << " index " << index << " uses buffer " << buffer
757 << " that references invalid memory " << mem_binding << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700758 *error = error_str.str();
Tobin Ehlisc8266452017-04-07 12:20:30 -0600759 return false;
760 }
761 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700762 }
763 if (descriptors_[i]->IsDynamic()) {
764 // Validate that dynamic offsets are within the buffer
765 auto buffer_size = buffer_node->createInfo.size;
766 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
767 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
768 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
769 if (VK_WHOLE_SIZE == range) {
770 if ((dyn_offset + desc_offset) > buffer_size) {
771 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600772 error_str << "Dynamic descriptor in binding #" << binding << " index " << index << " uses buffer "
773 << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
774 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
775 << buffer_size << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700776 *error = error_str.str();
777 return false;
778 }
779 } else {
780 if ((dyn_offset + desc_offset + range) > buffer_size) {
781 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600782 error_str << "Dynamic descriptor in binding #" << binding << " index " << index << " uses buffer "
783 << buffer << " with dynamic offset " << dyn_offset << " combined with offset "
784 << desc_offset << " and range " << range << " that oversteps the buffer size of "
785 << buffer_size << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700786 *error = error_str.str();
787 return false;
788 }
789 }
790 }
791 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
792 VkImageView image_view;
793 VkImageLayout image_layout;
794 if (descriptor_class == ImageSampler) {
795 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
796 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
797 } else {
798 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
799 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
800 }
801 auto reqs = binding_pair.second;
802
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -0700803 auto image_view_state = device_data_->GetImageViewState(image_view);
Tobin Ehlis836a1372017-07-14 11:25:21 -0600804 if (nullptr == image_view_state) {
805 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
806 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
807 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600808 error_str << "Descriptor in binding #" << binding << " index " << index << " is using imageView "
809 << image_view << " that has been destroyed.";
Tobin Ehlis836a1372017-07-14 11:25:21 -0600810 *error = error_str.str();
811 return false;
812 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700813 auto image_view_ci = image_view_state->create_info;
814
815 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
816 // bad view type
817 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600818 error_str << "Descriptor in binding #" << binding << " index " << index
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600819 << " requires an image view of type " << StringDescriptorReqViewType(reqs) << " but got "
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700820 << string_VkImageViewType(image_view_ci.viewType) << ".";
821 *error = error_str.str();
822 return false;
823 }
824
Chris Forbesda01e8d2018-08-27 15:36:57 -0700825 auto format_bits = DescriptorRequirementsBitsFromFormat(image_view_ci.format);
826 if (!(reqs & format_bits)) {
827 // bad component type
828 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600829 error_str << "Descriptor in binding #" << binding << " index " << index << " requires "
Chris Forbesda01e8d2018-08-27 15:36:57 -0700830 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
831 << string_VkFormat(image_view_ci.format) << ".";
832 *error = error_str.str();
833 return false;
834 }
835
Mark Lobodzinski8ddc23f2019-03-06 11:48:49 -0700836 auto image_node = device_data_->GetImageState(image_view_ci.image);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700837 assert(image_node);
838 // Verify Image Layout
John Zulauff660ad62019-03-23 07:16:05 -0600839 // No "invalid layout" VUID required for this call, since the optimal_layout parameter is UNDEFINED.
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700840 bool hit_error = false;
John Zulauff660ad62019-03-23 07:16:05 -0600841 device_data_->VerifyImageLayout(cb_node, image_node, image_view_state->normalized_subresource_range,
842 image_layout, VK_IMAGE_LAYOUT_UNDEFINED, caller, kVUIDUndefined,
843 "VUID-VkDescriptorImageInfo-imageLayout-00344", &hit_error);
844 if (hit_error) {
845 *error =
846 "Image layout specified at vkUpdateDescriptorSet* or vkCmdPushDescriptorSet* time "
847 "doesn't match actual image layout at time descriptor is used. See previous error callback for "
848 "specific details.";
849 return false;
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700850 }
John Zulauff660ad62019-03-23 07:16:05 -0600851
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700852 // Verify Sample counts
853 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
854 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600855 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700856 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
857 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
858 *error = error_str.str();
859 return false;
860 }
861 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
862 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600863 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700864 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
865 *error = error_str.str();
866 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200867 }
Chris Forbese92dd1d2019-01-21 15:58:57 -0800868 } else if (descriptor_class == TexelBuffer) {
869 auto texel_buffer = static_cast<TexelDescriptor *>(descriptors_[i].get());
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -0700870 auto buffer_view = device_data_->GetBufferViewState(texel_buffer->GetBufferView());
Chris Forbese92dd1d2019-01-21 15:58:57 -0800871
872 auto reqs = binding_pair.second;
873 auto format_bits = DescriptorRequirementsBitsFromFormat(buffer_view->create_info.format);
874
875 if (!(reqs & format_bits)) {
876 // bad component type
877 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600878 error_str << "Descriptor in binding #" << binding << " index " << index << " requires "
Chris Forbese92dd1d2019-01-21 15:58:57 -0800879 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
880 << string_VkFormat(buffer_view->create_info.format) << ".";
881 *error = error_str.str();
882 return false;
883 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600884 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600885 if (descriptor_class == ImageSampler || descriptor_class == PlainSampler) {
886 // Verify Sampler still valid
887 VkSampler sampler;
888 if (descriptor_class == ImageSampler) {
889 sampler = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetSampler();
890 } else {
891 sampler = static_cast<SamplerDescriptor *>(descriptors_[i].get())->GetSampler();
892 }
893 if (!ValidateSampler(sampler, device_data_)) {
894 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600895 error_str << "Descriptor in binding #" << binding << " index " << index << " is using sampler " << sampler
896 << " that has been destroyed.";
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600897 *error = error_str.str();
898 return false;
899 }
900 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600901 }
902 }
903 }
904 return true;
905}
Chris Forbes57989132016-07-26 17:06:10 +1200906
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600907// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600908uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600909 std::unordered_set<VkBuffer> *buffer_set,
910 std::unordered_set<VkImageView> *image_set) const {
911 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200912 for (auto binding_pair : bindings) {
913 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600914 // If a binding doesn't exist, skip it
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600915 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600916 continue;
917 }
John Zulaufc483f442017-12-15 14:02:06 -0700918 uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600919 if (descriptors_[start_idx]->IsStorage()) {
920 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600921 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600922 if (descriptors_[start_idx + i]->updated) {
923 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600924 num_updates++;
925 }
926 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600927 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600928 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600929 if (descriptors_[start_idx + i]->updated) {
930 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -0700931 auto bv_state = device_data_->GetBufferViewState(bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600932 if (bv_state) {
933 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600934 num_updates++;
935 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600936 }
937 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600938 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600939 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600940 if (descriptors_[start_idx + i]->updated) {
941 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600942 num_updates++;
943 }
944 }
945 }
946 }
947 }
948 return num_updates;
949}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600950// Set is being deleted or updates so invalidate all bound cmd buffers
951void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Mark Lobodzinski9ddb7182019-03-08 17:31:09 -0700952 device_data_->InvalidateCommandBuffers(cb_bindings, {HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600953}
John Zulauf1d27e0a2018-11-05 10:12:48 -0700954
955// Loop through the write updates to do for a push descriptor set, ignoring dstSet
956void cvdescriptorset::DescriptorSet::PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds) {
957 assert(IsPushDescriptor());
958 for (uint32_t i = 0; i < write_count; i++) {
959 PerformWriteUpdate(&p_wds[i]);
960 }
961}
962
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600963// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600964void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700965 // Perform update on a per-binding basis as consecutive updates roll over to next binding
966 auto descriptors_remaining = update->descriptorCount;
967 auto binding_being_updated = update->dstBinding;
968 auto offset = update->dstArrayElement;
Tobin Ehlise16805c2017-08-09 09:10:37 -0600969 uint32_t update_index = 0;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700970 while (descriptors_remaining) {
971 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
John Zulaufc483f442017-12-15 14:02:06 -0700972 auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700973 // Loop over the updates for a single binding at a time
Tobin Ehlise16805c2017-08-09 09:10:37 -0600974 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
975 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700976 }
977 // Roll over to next binding in case of consecutive update
978 descriptors_remaining -= update_count;
979 offset = 0;
980 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600981 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700982 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600983
Jeff Bolzfdf96072018-04-10 14:32:18 -0500984 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
985 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
986 InvalidateBoundCmdBuffers();
987 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600988}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600989// Validate Copy update
990bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -0600991 const DescriptorSet *src_set, const char *func_name,
992 std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -0700993 // Verify dst layout still valid
994 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600995 *error_code = "VUID-VkCopyDescriptorSet-dstSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -0700996 string_sprintf(error_msg,
Lockeca0d9792019-03-03 23:48:13 -0700997 "Cannot call %s to perform copy update on descriptor set dstSet %s"
998 " created with destroyed VkDescriptorSetLayout %s.",
999 func_name, report_data->FormatHandle(set_).c_str(),
1000 report_data->FormatHandle(p_layout_->GetDescriptorSetLayout()).c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001001 return false;
1002 }
1003
1004 // Verify src layout still valid
1005 if (src_set->p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001006 *error_code = "VUID-VkCopyDescriptorSet-srcSet-parameter";
John Zulaufb45fdc32018-10-12 15:14:17 -06001007 string_sprintf(error_msg,
Lockeca0d9792019-03-03 23:48:13 -07001008 "Cannot call %s to perform copy update of dstSet %s"
1009 " from descriptor set srcSet %s"
1010 " created with destroyed VkDescriptorSetLayout %s.",
1011 func_name, report_data->FormatHandle(set_).c_str(), report_data->FormatHandle(src_set->set_).c_str(),
1012 report_data->FormatHandle(src_set->p_layout_->GetDescriptorSetLayout()).c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001013 return false;
1014 }
1015
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001016 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001017 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-00347";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001018 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001019 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001020 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001021 return false;
1022 }
1023 if (!src_set->HasBinding(update->srcBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001024 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-00345";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001025 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001026 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001027 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001028 return false;
1029 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001030 // Verify idle ds
1031 if (in_use.load() &&
1032 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1033 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1034 // TODO : Re-using Free Idle error code, need copy update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001035 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001036 std::stringstream error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001037 error_str << "Cannot call " << func_name << " to perform copy update on descriptor set " << set_
Jeff Bolzfdf96072018-04-10 14:32:18 -05001038 << " that is in use by a command buffer";
1039 *error_msg = error_str.str();
1040 return false;
1041 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001042 // src & dst set bindings are valid
1043 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -07001044 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001045 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
1046 // SRC update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001047 *error_code = "VUID-VkCopyDescriptorSet-srcArrayElement-00346";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001048 std::stringstream error_str;
1049 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
John Zulaufc483f442017-12-15 14:02:06 -07001050 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001051 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001052 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001053 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001054 return false;
1055 }
John Zulaufc483f442017-12-15 14:02:06 -07001056 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001057 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001058 // DST update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001059 *error_code = "VUID-VkCopyDescriptorSet-dstArrayElement-00348";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001060 std::stringstream error_str;
1061 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
John Zulaufc483f442017-12-15 14:02:06 -07001062 << " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001063 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001064 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001065 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001066 return false;
1067 }
1068 // Check that types match
Dave Houltond8ed0212018-05-16 17:18:24 -06001069 // TODO : Base default error case going from here is "VUID-VkAcquireNextImageInfoKHR-semaphore-parameter"2ba which covers all
1070 // consistency issues, need more fine-grained error codes
Dave Houlton00c154e2018-05-24 13:20:50 -06001071 *error_code = "VUID-VkCopyDescriptorSet-srcSet-00349";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001072 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001073 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001074 if (src_type != dst_type) {
1075 std::stringstream error_str;
1076 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
1077 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001078 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001079 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001080 return false;
1081 }
1082 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -06001083 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001084 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001085 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001086 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001087 return false;
1088 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001089
1090 if ((src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1091 !(GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001092 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01918";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001093 std::stringstream error_str;
1094 error_str << "If pname:srcSet's (" << update->srcSet
1095 << ") layout was created with the "
1096 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1097 "set, then pname:dstSet's ("
1098 << update->dstSet
1099 << ") layout must: also have been created with the "
1100 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1101 *error_msg = error_str.str();
1102 return false;
1103 }
1104
1105 if (!(src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1106 (GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001107 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01919";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001108 std::stringstream error_str;
1109 error_str << "If pname:srcSet's (" << update->srcSet
1110 << ") layout was created without the "
1111 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1112 "set, then pname:dstSet's ("
1113 << update->dstSet
1114 << ") layout must: also have been created without the "
1115 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1116 *error_msg = error_str.str();
1117 return false;
1118 }
1119
1120 if ((src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1121 !(GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001122 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01920";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001123 std::stringstream error_str;
1124 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1125 << ") was allocated was created "
1126 "with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1127 "set, then the descriptor pool from which pname:dstSet ("
1128 << update->dstSet
1129 << ") was allocated must: "
1130 "also have been created with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1131 *error_msg = error_str.str();
1132 return false;
1133 }
1134
1135 if (!(src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1136 (GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001137 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01921";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001138 std::stringstream error_str;
1139 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1140 << ") was allocated was created "
1141 "without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1142 "set, then the descriptor pool from which pname:dstSet ("
1143 << update->dstSet
1144 << ") was allocated must: "
1145 "also have been created without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1146 *error_msg = error_str.str();
1147 return false;
1148 }
1149
Jeff Bolze54ae892018-09-08 12:16:29 -05001150 if (src_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1151 if ((update->srcArrayElement % 4) != 0) {
1152 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02223";
1153 std::stringstream error_str;
1154 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1155 << "srcArrayElement " << update->srcArrayElement << " not a multiple of 4";
1156 *error_msg = error_str.str();
1157 return false;
1158 }
1159 if ((update->dstArrayElement % 4) != 0) {
1160 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02224";
1161 std::stringstream error_str;
1162 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1163 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1164 *error_msg = error_str.str();
1165 return false;
1166 }
1167 if ((update->descriptorCount % 4) != 0) {
1168 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02225";
1169 std::stringstream error_str;
1170 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1171 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1172 *error_msg = error_str.str();
1173 return false;
1174 }
1175 }
1176
Tobin Ehlisd41e7b62016-05-19 07:56:18 -06001177 // Update parameters all look good and descriptor updated so verify update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06001178 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 -06001179
1180 // All checks passed so update is good
1181 return true;
1182}
1183// Perform Copy update
1184void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -07001185 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
1186 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001187 // Update parameters all look good so perform update
1188 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001189 auto src = src_set->descriptors_[src_start_idx + di].get();
1190 auto dst = descriptors_[dst_start_idx + di].get();
1191 if (src->updated) {
1192 dst->CopyUpdate(src);
1193 some_update_ = true;
1194 } else {
1195 dst->updated = false;
1196 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001197 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001198
Jeff Bolzfdf96072018-04-10 14:32:18 -05001199 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1200 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1201 InvalidateBoundCmdBuffers();
1202 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001203}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001204
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001205// Update the drawing state for the affected descriptors.
1206// Set cb_node to this set and this set to cb_node.
1207// Add the bindings of the descriptor
1208// Set the layout based on the current descriptor layout (will mask subsequent layer mismatch errors)
1209// TODO: Modify the UpdateDrawState virtural functions to *only* set initial layout and not change layouts
Tobin Ehlisf9519102016-08-17 09:49:13 -06001210// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
1211// to be used in a draw by the given cb_node
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001212void cvdescriptorset::DescriptorSet::UpdateDrawState(CoreChecks *device_data, GLOBAL_CB_NODE *cb_node,
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001213 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001214 // bind cb to this descriptor set
1215 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001216 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Petr Krausbc7f5442017-05-14 23:43:38 +02001217 cb_node->object_bindings.insert({HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001218 pool_state_->cb_bindings.insert(cb_node);
Petr Krausbc7f5442017-05-14 23:43:38 +02001219 cb_node->object_bindings.insert({HandleToUint64(pool_state_->pool), kVulkanObjectTypeDescriptorPool});
Tobin Ehlisf9519102016-08-17 09:49:13 -06001220 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
1221 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -07001222 for (auto binding_req_pair : binding_req_map) {
1223 auto binding = binding_req_pair.first;
Tony-LunarG62c5dba2018-12-20 14:27:23 -07001224 // We aren't validating descriptors created with PARTIALLY_BOUND or UPDATE_AFTER_BIND, so don't record state
1225 if (p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
1226 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) {
1227 continue;
1228 }
John Zulaufc483f442017-12-15 14:02:06 -07001229 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
1230 for (uint32_t i = range.start; i < range.end; ++i) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001231 descriptors_[i]->UpdateDrawState(device_data, cb_node);
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001232 }
1233 }
1234}
1235
John Zulauf48a6a702017-12-22 17:14:54 -07001236void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1237 const BindingReqMap &in_req, BindingReqMap *out_req,
1238 TrackedBindings *bindings) {
1239 assert(out_req);
1240 assert(bindings);
1241 const auto binding = binding_req_pair.first;
1242 // Use insert and look at the boolean ("was inserted") in the returned pair to see if this is a new set member.
1243 // Saves one hash lookup vs. find ... compare w/ end ... insert.
1244 const auto it_bool_pair = bindings->insert(binding);
1245 if (it_bool_pair.second) {
1246 out_req->emplace(binding_req_pair);
1247 }
1248}
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001249
John Zulauf48a6a702017-12-22 17:14:54 -07001250void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1251 const BindingReqMap &in_req, BindingReqMap *out_req,
1252 TrackedBindings *bindings, uint32_t limit) {
1253 if (bindings->size() < limit) FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, bindings);
1254}
1255
1256void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, const BindingReqMap &in_req,
1257 BindingReqMap *out_req) {
1258 TrackedBindings &bound = cached_validation_[cb_state].command_binding_and_usage;
1259 if (bound.size() == GetBindingCount()) {
1260 return; // All bindings are bound, out req is empty
1261 }
1262 for (const auto &binding_req_pair : in_req) {
1263 const auto binding = binding_req_pair.first;
1264 // If a binding doesn't exist, or has already been bound, skip it
1265 if (p_layout_->HasBinding(binding)) {
1266 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, &bound);
1267 }
1268 }
1269}
1270
1271void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline,
1272 const BindingReqMap &in_req, BindingReqMap *out_req) {
1273 auto &validated = cached_validation_[cb_state];
1274 auto &image_sample_val = validated.image_samplers[pipeline];
1275 auto *const dynamic_buffers = &validated.dynamic_buffers;
1276 auto *const non_dynamic_buffers = &validated.non_dynamic_buffers;
1277 const auto &stats = p_layout_->GetBindingTypeStats();
1278 for (const auto &binding_req_pair : in_req) {
1279 auto binding = binding_req_pair.first;
1280 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
1281 if (!layout_binding) {
1282 continue;
1283 }
1284 // Caching criteria differs per type.
1285 // If image_layout have changed , the image descriptors need to be validated against them.
1286 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1287 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
1288 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, dynamic_buffers, stats.dynamic_buffer_count);
1289 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1290 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
1291 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
1292 } else {
1293 // This is rather crude, as the changed layouts may not impact the bound descriptors,
1294 // but the simple "versioning" is a simple "dirt" test.
1295 auto &version = image_sample_val[binding]; // Take advantage of default construtor zero initialzing new entries
1296 if (version != cb_state->image_layout_change_count) {
1297 version = cb_state->image_layout_change_count;
1298 out_req->emplace(binding_req_pair);
1299 }
1300 }
1301 }
1302}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001303
Tobin Ehlis300888c2016-05-18 13:43:26 -06001304cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001305 updated = false;
1306 descriptor_class = PlainSampler;
1307 if (immut) {
1308 sampler_ = *immut;
1309 immutable_ = true;
1310 updated = true;
1311 }
1312}
Tobin Ehlise2f80292016-06-02 10:08:53 -06001313// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001314bool cvdescriptorset::ValidateSampler(const VkSampler sampler, CoreChecks *dev_data) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001315 return (dev_data->GetSamplerState(sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001316}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001317
Tobin Ehlis554bf382016-05-24 11:14:43 -06001318bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001319 CoreChecks *dev_data, const char *func_name, std::string *error_code,
John Zulaufb45fdc32018-10-12 15:14:17 -06001320 std::string *error_msg) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001321 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00326";
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001322 auto iv_state = dev_data->GetImageViewState(image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001323 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001324 std::stringstream error_str;
1325 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001326 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001327 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001328 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001329 // 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 -06001330 // Validate that imageLayout is compatible with aspect_mask and image format
1331 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001332 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
1333 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001334 VkFormat format = VK_FORMAT_MAX_ENUM;
1335 VkImageUsageFlags usage = 0;
Mark Lobodzinski8ddc23f2019-03-06 11:48:49 -07001336 auto image_node = dev_data->GetImageState(image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001337 if (image_node) {
1338 format = image_node->createInfo.format;
1339 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001340 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001341 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
1342 // the error here occurs is if memory bound to a created imageView has been freed.
Mark Lobodzinski0ebe9712019-03-07 13:39:07 -07001343 if (dev_data->ValidateMemoryIsBoundToImage(image_node, func_name, "VUID-VkImageViewCreateInfo-image-01020")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001344 *error_code = "VUID-VkImageViewCreateInfo-image-01020";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001345 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001346 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001347 }
Chris Forbes67757ff2017-07-21 13:59:01 -07001348
1349 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
1350 // but not binding them to descriptor sets.
1351 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D &&
1352 (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
1353 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001354 *error_code = "VUID-VkDescriptorImageInfo-imageView-00343";
Chris Forbes67757ff2017-07-21 13:59:01 -07001355 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
1356 return false;
1357 }
Tobin Ehlis1809f912016-05-25 09:24:36 -06001358 }
1359 // First validate that format and layout are compatible
1360 if (format == VK_FORMAT_MAX_ENUM) {
1361 std::stringstream error_str;
1362 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001363 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001364 return false;
1365 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001366 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
1367 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -06001368 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -06001369 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001370 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1371 // Only Color bit must be set
1372 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001373 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -07001374 error_str
1375 << "ImageView (" << image_view
1376 << ") 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 -06001377 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001378 return false;
1379 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001380 // format must NOT be DS
1381 if (ds) {
1382 std::stringstream error_str;
1383 error_str << "ImageView (" << image_view
1384 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1385 << string_VkFormat(format) << " which is not a color format.";
1386 *error_msg = error_str.str();
1387 return false;
1388 }
1389 break;
1390 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1391 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1392 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001393 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1394 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1395 // both must NOT be set
1396 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001397 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001398 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001399 return false;
1400 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001401 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1402 // Neither were set
1403 std::stringstream error_str;
1404 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1405 << " but does not have STENCIL or DEPTH aspects set";
1406 *error_msg = error_str.str();
1407 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001408 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001409 // format must be DS
1410 if (!ds) {
1411 std::stringstream error_str;
1412 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1413 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
1414 *error_msg = error_str.str();
1415 return false;
1416 }
1417 break;
1418 default:
1419 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1420 if (ds) {
1421 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1422 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1423 // both must NOT be set
1424 std::stringstream error_str;
1425 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1426 << " and is using depth/stencil image of format " << string_VkFormat(format)
1427 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1428 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1429 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1430 "reads respectively.";
1431 *error_msg = error_str.str();
1432 return false;
1433 }
1434 }
1435 }
1436 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001437 }
1438 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001439 // 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 -06001440 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1441 // under vkCreateImage()
Dave Houltond8ed0212018-05-16 17:18:24 -06001442 // TODO : Need to also validate case "VUID-VkWriteDescriptorSet-descriptorType-00336" where STORAGE_IMAGE & INPUT_ATTACH types
1443 // must have been created with identify swizzle
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001444 const char *error_usage_bit = nullptr;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001445 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001446 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1447 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1448 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1449 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
1450 }
1451 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001452 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001453 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1454 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1455 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
1456 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
1457 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001458 // TODO : Need to create custom enum error codes for these cases
1459 if (image_node->shared_presentable) {
1460 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001461 error_str << "ImageView (" << image_view
1462 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
1463 "layout "
1464 << string_VkImageLayout(image_layout)
1465 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report "
1466 "support for VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the "
1467 "VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001468 *error_msg = error_str.str();
1469 return false;
1470 }
1471 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001472 error_str << "ImageView (" << image_view
1473 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
1474 << string_VkImageLayout(image_layout)
1475 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage "
1476 "images can only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001477 *error_msg = error_str.str();
1478 return false;
1479 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001480 }
1481 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001482 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001483 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
1484 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
1485 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
1486 }
1487 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001488 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001489 default:
1490 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001491 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001492 if (error_usage_bit) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001493 std::stringstream error_str;
1494 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
1495 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1496 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001497 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001498 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001499 }
John Zulauff4c07882019-01-24 14:03:36 -07001500
1501 if ((type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) || (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)) {
1502 // Test that the layout is compatible with the descriptorType for the two sampled image types
1503 const static std::array<VkImageLayout, 3> valid_layouts = {
1504 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL};
1505
1506 struct ExtensionLayout {
1507 VkImageLayout layout;
1508 bool DeviceExtensions::*extension;
1509 };
1510
1511 const static std::array<ExtensionLayout, 3> extended_layouts{
1512 {// Note double brace req'd for aggregate initialization
1513 {VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR, &DeviceExtensions::vk_khr_shared_presentable_image},
1514 {VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2},
1515 {VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2}}};
1516 auto is_layout = [image_layout, dev_data](const ExtensionLayout &ext_layout) {
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001517 return dev_data->device_extensions.*(ext_layout.extension) && (ext_layout.layout == image_layout);
John Zulauff4c07882019-01-24 14:03:36 -07001518 };
1519
1520 bool valid_layout = (std::find(valid_layouts.cbegin(), valid_layouts.cend(), image_layout) != valid_layouts.cend()) ||
1521 std::any_of(extended_layouts.cbegin(), extended_layouts.cend(), is_layout);
1522
1523 if (!valid_layout) {
1524 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01403";
1525 std::stringstream error_str;
1526 error_str << "Descriptor update with descriptorType " << string_VkDescriptorType(type)
1527 << " is being updated with invalid imageLayout " << string_VkImageLayout(image_layout)
1528 << ". Allowed layouts are: VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, "
1529 << "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL";
1530 for (auto &ext_layout : extended_layouts) {
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001531 if (dev_data->device_extensions.*(ext_layout.extension)) {
John Zulauff4c07882019-01-24 14:03:36 -07001532 error_str << ", " << string_VkImageLayout(ext_layout.layout);
1533 }
1534 }
1535 *error_msg = error_str.str();
1536 return false;
1537 }
1538 }
1539
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001540 return true;
1541}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001542
Tobin Ehlis300888c2016-05-18 13:43:26 -06001543void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Chris Forbesfea2c542018-04-13 09:34:15 -07001544 if (!immutable_) {
1545 sampler_ = update->pImageInfo[index].sampler;
1546 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001547 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001548}
1549
Tobin Ehlis300888c2016-05-18 13:43:26 -06001550void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001551 if (!immutable_) {
1552 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001553 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001554 }
1555 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001556}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001557
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001558void cvdescriptorset::SamplerDescriptor::UpdateDrawState(CoreChecks *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001559 if (!immutable_) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001560 auto sampler_state = dev_data->GetSamplerState(sampler_);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001561 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001562 }
1563}
1564
Tobin Ehlis300888c2016-05-18 13:43:26 -06001565cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001566 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001567 updated = false;
1568 descriptor_class = ImageSampler;
1569 if (immut) {
1570 sampler_ = *immut;
1571 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001572 }
1573}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001574
Tobin Ehlis300888c2016-05-18 13:43:26 -06001575void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001576 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001577 const auto &image_info = update->pImageInfo[index];
Chris Forbesfea2c542018-04-13 09:34:15 -07001578 if (!immutable_) {
1579 sampler_ = image_info.sampler;
1580 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001581 image_view_ = image_info.imageView;
1582 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001583}
1584
Tobin Ehlis300888c2016-05-18 13:43:26 -06001585void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001586 if (!immutable_) {
1587 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001588 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001589 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001590 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1591 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001592 updated = true;
1593 image_view_ = image_view;
1594 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001595}
1596
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001597void cvdescriptorset::ImageSamplerDescriptor::UpdateDrawState(CoreChecks *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001598 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001599 if (!immutable_) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001600 auto sampler_state = dev_data->GetSamplerState(sampler_);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001601 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001602 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001603 // Add binding for image
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001604 auto iv_state = dev_data->GetImageViewState(image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001605 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001606 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
John Zulauff660ad62019-03-23 07:16:05 -06001607 dev_data->SetImageViewInitialLayout(cb_node, *iv_state, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06001608 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001609}
1610
Tobin Ehlis300888c2016-05-18 13:43:26 -06001611cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1612 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001613 updated = false;
1614 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001615 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001616}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001617
Tobin Ehlis300888c2016-05-18 13:43:26 -06001618void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001619 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001620 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001621 image_view_ = image_info.imageView;
1622 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001623}
1624
Tobin Ehlis300888c2016-05-18 13:43:26 -06001625void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001626 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1627 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001628 updated = true;
1629 image_view_ = image_view;
1630 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001631}
1632
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001633void cvdescriptorset::ImageDescriptor::UpdateDrawState(CoreChecks *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001634 // Add binding for image
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001635 auto iv_state = dev_data->GetImageViewState(image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001636 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001637 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
John Zulauff660ad62019-03-23 07:16:05 -06001638 dev_data->SetImageViewInitialLayout(cb_node, *iv_state, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06001639 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001640}
1641
Tobin Ehlis300888c2016-05-18 13:43:26 -06001642cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1643 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001644 updated = false;
1645 descriptor_class = GeneralBuffer;
1646 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1647 dynamic_ = true;
1648 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1649 storage_ = true;
1650 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1651 dynamic_ = true;
1652 storage_ = true;
1653 }
1654}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001655void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001656 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001657 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001658 buffer_ = buffer_info.buffer;
1659 offset_ = buffer_info.offset;
1660 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001661}
1662
Tobin Ehlis300888c2016-05-18 13:43:26 -06001663void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1664 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001665 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001666 buffer_ = buff_desc->buffer_;
1667 offset_ = buff_desc->offset_;
1668 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001669}
1670
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001671void cvdescriptorset::BufferDescriptor::UpdateDrawState(CoreChecks *dev_data, GLOBAL_CB_NODE *cb_node) {
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07001672 auto buffer_node = dev_data->GetBufferState(buffer_);
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001673 if (buffer_node) dev_data->AddCommandBufferBindingBuffer(cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001674}
1675
Tobin Ehlis300888c2016-05-18 13:43:26 -06001676cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001677 updated = false;
1678 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001679 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001680}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001681
Tobin Ehlis300888c2016-05-18 13:43:26 -06001682void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001683 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001684 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001685}
1686
Tobin Ehlis300888c2016-05-18 13:43:26 -06001687void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1688 updated = true;
1689 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1690}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001691
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001692void cvdescriptorset::TexelDescriptor::UpdateDrawState(CoreChecks *dev_data, GLOBAL_CB_NODE *cb_node) {
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07001693 auto bv_state = dev_data->GetBufferViewState(buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001694 if (bv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001695 dev_data->AddCommandBufferBindingBufferView(cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001696 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001697}
1698
Tobin Ehlis300888c2016-05-18 13:43:26 -06001699// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1700// sets, and then calls their respective Validate[Write|Copy]Update functions.
1701// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1702// be skipped, then true is returned.
1703// If there is no issue with the update, then false is returned.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001704bool CoreChecks::ValidateUpdateDescriptorSets(uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001705 const VkCopyDescriptorSet *p_cds, const char *func_name) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001706 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001707 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001708 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001709 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001710 auto set_node = GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001711 if (!set_node) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001712 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1713 HandleToUint64(dest_set), kVUID_Core_DrawState_InvalidDescriptorSet,
Lockeca0d9792019-03-03 23:48:13 -07001714 "Cannot call %s on descriptor set %s that has not been allocated.", func_name,
1715 report_data->FormatHandle(dest_set).c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001716 } else {
Dave Houltond8ed0212018-05-16 17:18:24 -06001717 std::string error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001718 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001719 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], func_name, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001720 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 -06001721 HandleToUint64(dest_set), error_code,
Lockeca0d9792019-03-03 23:48:13 -07001722 "%s failed write update validation for Descriptor Set %s with error: %s.", func_name,
1723 report_data->FormatHandle(dest_set).c_str(), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001724 }
1725 }
1726 }
1727 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001728 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001729 auto dst_set = p_cds[i].dstSet;
1730 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001731 auto src_node = GetSetNode(src_set);
1732 auto dst_node = GetSetNode(dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001733 // Object_tracker verifies that src & dest descriptor set are valid
1734 assert(src_node);
1735 assert(dst_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001736 std::string error_code;
Tobin Ehlisa1712752017-01-04 09:41:47 -07001737 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001738 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, func_name, &error_code, &error_str)) {
Lockeca0d9792019-03-03 23:48:13 -07001739 skip |= log_msg(
1740 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, HandleToUint64(dst_set),
1741 error_code, "%s failed copy update from Descriptor Set %s to Descriptor Set %s with error: %s.", func_name,
1742 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 -06001743 }
1744 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001745 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001746}
1747// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1748// sets, and then calls their respective Perform[Write|Copy]Update functions.
1749// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1750// with the same set of updates.
1751// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1752// calling down the chain.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001753void cvdescriptorset::PerformUpdateDescriptorSets(CoreChecks *dev_data, uint32_t write_count, const VkWriteDescriptorSet *p_wds,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001754 uint32_t copy_count, const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001755 // Write updates first
1756 uint32_t i = 0;
1757 for (i = 0; i < write_count; ++i) {
1758 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001759 auto set_node = dev_data->GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001760 if (set_node) {
1761 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001762 }
1763 }
1764 // Now copy updates
1765 for (i = 0; i < copy_count; ++i) {
1766 auto dst_set = p_cds[i].dstSet;
1767 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001768 auto src_node = dev_data->GetSetNode(src_set);
1769 auto dst_node = dev_data->GetSetNode(dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001770 if (src_node && dst_node) {
1771 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001772 }
1773 }
1774}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001775
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001776cvdescriptorset::DecodedTemplateUpdate::DecodedTemplateUpdate(CoreChecks *device_data, VkDescriptorSet descriptorSet,
John Zulauf1d27e0a2018-11-05 10:12:48 -07001777 const TEMPLATE_STATE *template_state, const void *pData,
1778 VkDescriptorSetLayout push_layout) {
John Zulaufb845eb22018-10-12 11:41:06 -06001779 auto const &create_info = template_state->create_info;
1780 inline_infos.resize(create_info.descriptorUpdateEntryCount); // Make sure we have one if we need it
1781 desc_writes.reserve(create_info.descriptorUpdateEntryCount); // emplaced, so reserved without initialization
John Zulauf1d27e0a2018-11-05 10:12:48 -07001782 VkDescriptorSetLayout effective_dsl = create_info.templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
1783 ? create_info.descriptorSetLayout
1784 : push_layout;
1785 auto layout_obj = GetDescriptorSetLayout(device_data, effective_dsl);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001786
1787 // Create a WriteDescriptorSet struct for each template update entry
1788 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1789 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1790 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1791 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1792
John Zulaufb6d71202017-12-22 16:47:09 -07001793 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001794 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1795 desc_writes.emplace_back();
1796 auto &write_entry = desc_writes.back();
1797
1798 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1799 char *update_entry = (char *)(pData) + offset;
1800
1801 if (dst_array_element >= binding_count) {
1802 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001803 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001804 }
1805
1806 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1807 write_entry.pNext = NULL;
1808 write_entry.dstSet = descriptorSet;
1809 write_entry.dstBinding = binding_being_updated;
1810 write_entry.dstArrayElement = dst_array_element;
1811 write_entry.descriptorCount = 1;
1812 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1813
1814 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1815 case VK_DESCRIPTOR_TYPE_SAMPLER:
1816 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1817 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1818 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1819 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1820 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1821 break;
1822
1823 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1824 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1825 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1826 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1827 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1828 break;
1829
1830 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1831 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1832 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1833 break;
Dave Houlton142c4cb2018-10-17 15:04:41 -06001834 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1835 VkWriteDescriptorSetInlineUniformBlockEXT *inline_info = &inline_infos[i];
1836 inline_info->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT;
1837 inline_info->pNext = nullptr;
1838 inline_info->dataSize = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1839 inline_info->pData = update_entry;
1840 write_entry.pNext = inline_info;
1841 // skip the rest of the array, they just represent bytes in the update
1842 j = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1843 break;
1844 }
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001845 default:
1846 assert(0);
1847 break;
1848 }
1849 dst_array_element++;
1850 }
1851 }
John Zulaufb845eb22018-10-12 11:41:06 -06001852}
John Zulaufb45fdc32018-10-12 15:14:17 -06001853// These helper functions carry out the validate and record descriptor updates peformed via update templates. They decode
1854// the templatized data and leverage the non-template UpdateDescriptor helper functions.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001855bool CoreChecks::ValidateUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
1856 const void *pData) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001857 // Translate the templated update into a normal update for validation...
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001858 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
1859 return ValidateUpdateDescriptorSets(static_cast<uint32_t>(decoded_update.desc_writes.size()), decoded_update.desc_writes.data(),
1860 0, NULL, "vkUpdateDescriptorSetWithTemplate()");
John Zulaufb45fdc32018-10-12 15:14:17 -06001861}
John Zulaufb845eb22018-10-12 11:41:06 -06001862
Mark Lobodzinski254c8512019-03-09 12:21:15 -07001863void CoreChecks::PerformUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
1864 const void *pData) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001865 // Translate the templated update into a normal update for validation...
Mark Lobodzinski254c8512019-03-09 12:21:15 -07001866 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
1867 cvdescriptorset::PerformUpdateDescriptorSets(this, static_cast<uint32_t>(decoded_update.desc_writes.size()),
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001868 decoded_update.desc_writes.data(), 0, NULL);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001869}
John Zulauf4e7bcb52018-11-02 10:46:30 -06001870
1871std::string cvdescriptorset::DescriptorSet::StringifySetAndLayout() const {
1872 std::string out;
1873 uint64_t layout_handle = HandleToUint64(p_layout_->GetDescriptorSetLayout());
1874 if (IsPushDescriptor()) {
1875 string_sprintf(&out, "Push Descriptors defined with VkDescriptorSetLayout 0x%" PRIxLEAST64, layout_handle);
John Zulauf4e7bcb52018-11-02 10:46:30 -06001876 } else {
Bob Ellisonaf2f8532019-03-12 11:05:47 -06001877 string_sprintf(&out, "VkDescriptorSet 0x%" PRIxLEAST64 " allocated with VkDescriptorSetLayout 0x%" PRIxLEAST64,
John Zulauf4e7bcb52018-11-02 10:46:30 -06001878 HandleToUint64(set_), layout_handle);
1879 }
1880 return out;
1881};
1882
John Zulauf1d27e0a2018-11-05 10:12:48 -07001883// Loop through the write updates to validate for a push descriptor set, ignoring dstSet
1884bool cvdescriptorset::DescriptorSet::ValidatePushDescriptorsUpdate(const debug_report_data *report_data, uint32_t write_count,
1885 const VkWriteDescriptorSet *p_wds, const char *func_name) {
1886 assert(IsPushDescriptor());
1887 bool skip = false;
1888 for (uint32_t i = 0; i < write_count; i++) {
1889 std::string error_code;
1890 std::string error_str;
1891 if (!ValidateWriteUpdate(report_data, &p_wds[i], func_name, &error_code, &error_str)) {
1892 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1893 HandleToUint64(p_layout_->GetDescriptorSetLayout()), error_code, "%s failed update validation: %s.",
1894 func_name, error_str.c_str());
1895 }
1896 }
1897 return skip;
1898}
1899
Tobin Ehlis300888c2016-05-18 13:43:26 -06001900// Validate the state for a given write update but don't actually perform the update
1901// If an error would occur for this update, return false and fill in details in error_msg string
1902bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -06001903 const char *func_name, std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001904 // Verify dst layout still valid
1905 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001906 *error_code = "VUID-VkWriteDescriptorSet-dstSet-00320";
John Zulauf4e7bcb52018-11-02 10:46:30 -06001907 string_sprintf(error_msg, "Cannot call %s to perform write update on %s which has been destroyed", func_name,
1908 StringifySetAndLayout().c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001909 return false;
1910 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001911 // Verify dst binding exists
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001912 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001913 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00315";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001914 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001915 error_str << StringifySetAndLayout() << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001916 *error_msg = error_str.str();
1917 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001918 } else {
1919 // Make sure binding isn't empty
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001920 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001921 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00316";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001922 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001923 error_str << StringifySetAndLayout() << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001924 *error_msg = error_str.str();
1925 return false;
1926 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001927 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001928 // Verify idle ds
1929 if (in_use.load() &&
1930 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1931 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1932 // TODO : Re-using Free Idle error code, need write update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001933 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001934 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001935 error_str << "Cannot call " << func_name << " to perform write update on " << StringifySetAndLayout()
Jeff Bolzfdf96072018-04-10 14:32:18 -05001936 << " that is in use by a command buffer";
1937 *error_msg = error_str.str();
1938 return false;
1939 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001940 // We know that binding is valid, verify update and do update on each descriptor
John Zulaufc483f442017-12-15 14:02:06 -07001941 auto start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001942 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001943 if (type != update->descriptorType) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001944 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00319";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001945 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001946 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with type "
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001947 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1948 *error_msg = error_str.str();
1949 return false;
1950 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001951 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001952 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001953 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001954 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001955 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001956 << " descriptors in that binding and all successive bindings of the set, but update of "
1957 << update->descriptorCount << " descriptors combined with update array element offset of "
1958 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001959 *error_msg = error_str.str();
1960 return false;
1961 }
Jeff Bolze54ae892018-09-08 12:16:29 -05001962 if (type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1963 if ((update->dstArrayElement % 4) != 0) {
1964 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02219";
1965 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001966 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001967 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1968 *error_msg = error_str.str();
1969 return false;
1970 }
1971 if ((update->descriptorCount % 4) != 0) {
1972 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02220";
1973 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001974 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001975 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1976 *error_msg = error_str.str();
1977 return false;
1978 }
1979 const auto *write_inline_info = lvl_find_in_chain<VkWriteDescriptorSetInlineUniformBlockEXT>(update->pNext);
1980 if (!write_inline_info || write_inline_info->dataSize != update->descriptorCount) {
1981 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02221";
1982 std::stringstream error_str;
1983 if (!write_inline_info) {
John Zulauf4e7bcb52018-11-02 10:46:30 -06001984 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
1985 << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001986 << "VkWriteDescriptorSetInlineUniformBlockEXT missing";
1987 } else {
John Zulauf4e7bcb52018-11-02 10:46:30 -06001988 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
1989 << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06001990 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
1991 << " not equal to "
Jeff Bolze54ae892018-09-08 12:16:29 -05001992 << "VkWriteDescriptorSet descriptorCount " << update->descriptorCount;
1993 }
1994 *error_msg = error_str.str();
1995 return false;
1996 }
1997 // This error is probably unreachable due to the previous two errors
1998 if (write_inline_info && (write_inline_info->dataSize % 4) != 0) {
1999 *error_code = "VUID-VkWriteDescriptorSetInlineUniformBlockEXT-dataSize-02222";
2000 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002001 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06002002 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2003 << " not a multiple of 4";
Jeff Bolze54ae892018-09-08 12:16:29 -05002004 *error_msg = error_str.str();
2005 return false;
2006 }
2007 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002008 // Verify consecutive bindings match (if needed)
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06002009 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002010 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07002011 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Dave Houlton00c154e2018-05-24 13:20:50 -06002012 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002013 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002014 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002015 // Update is within bounds and consistent so last step is to validate update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06002016 if (!VerifyWriteUpdateContents(update, start_idx, func_name, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002017 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002018 error_str << "Write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002019 << " failed with error message: " << error_msg->c_str();
2020 *error_msg = error_str.str();
2021 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002022 }
2023 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002024 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06002025}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002026// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002027// 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 -07002028bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06002029 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002030 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06002031 auto usage = buffer_node->createInfo.usage;
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002032 const char *error_usage_bit = nullptr;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002033 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002034 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2035 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002036 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00334";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002037 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
2038 }
2039 break;
2040 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2041 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002042 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00335";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002043 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
2044 }
2045 break;
2046 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2047 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2048 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002049 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00330";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002050 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
2051 }
2052 break;
2053 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2054 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
2055 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002056 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00331";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002057 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
2058 }
2059 break;
2060 default:
2061 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002062 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002063 if (error_usage_bit) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002064 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002065 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
2066 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
2067 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002068 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002069 return false;
2070 }
2071 return true;
2072}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002073// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
2074// 1. buffer is valid
2075// 2. buffer was created with correct usage flags
2076// 3. offset is less than buffer size
2077// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002078// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002079// 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 -06002080bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
John Zulaufb45fdc32018-10-12 15:14:17 -06002081 const char *func_name, std::string *error_code,
2082 std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002083 // First make sure that buffer is valid
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002084 auto buffer_node = device_data_->GetBufferState(buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07002085 // Any invalid buffer should already be caught by object_tracker
2086 assert(buffer_node);
Mark Lobodzinski0ebe9712019-03-07 13:39:07 -07002087 if (device_data_->ValidateMemoryIsBoundToBuffer(buffer_node, func_name, "VUID-VkWriteDescriptorSet-descriptorType-00329")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002088 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00329";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002089 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06002090 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06002091 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002092 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002093 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
2094 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002095 return false;
2096 }
2097 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002098 if (buffer_info->offset >= buffer_node->createInfo.size) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002099 *error_code = "VUID-VkDescriptorBufferInfo-offset-00340";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002100 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002101 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
2102 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002103 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002104 return false;
2105 }
2106 if (buffer_info->range != VK_WHOLE_SIZE) {
2107 // Range must be VK_WHOLE_SIZE or > 0
2108 if (!buffer_info->range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002109 *error_code = "VUID-VkDescriptorBufferInfo-range-00341";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002110 std::stringstream error_str;
2111 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002112 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002113 return false;
2114 }
2115 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
2116 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002117 *error_code = "VUID-VkDescriptorBufferInfo-range-00342";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002118 std::stringstream error_str;
2119 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
2120 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002121 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002122 return false;
2123 }
2124 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002125 // Check buffer update sizes against device limits
2126 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
2127 auto max_ub_range = limits_.maxUniformBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002128 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002129 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002130 std::stringstream error_str;
2131 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2132 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
2133 *error_msg = error_str.str();
2134 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002135 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_ub_range) {
2136 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
2137 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002138 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2139 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002140 << "maxUniformBufferRange (" << max_ub_range << ")";
2141 *error_msg = error_str.str();
2142 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002143 }
2144 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
2145 auto max_sb_range = limits_.maxStorageBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002146 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002147 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002148 std::stringstream error_str;
2149 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2150 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
2151 *error_msg = error_str.str();
2152 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002153 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_sb_range) {
2154 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
2155 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002156 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2157 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002158 << "maxStorageBufferRange (" << max_sb_range << ")";
2159 *error_msg = error_str.str();
2160 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002161 }
2162 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002163 return true;
2164}
2165
Tobin Ehlis300888c2016-05-18 13:43:26 -06002166// Verify that the contents of the update are ok, but don't perform actual update
2167bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
John Zulaufb45fdc32018-10-12 15:14:17 -06002168 const char *func_name, std::string *error_code,
2169 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002170 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002171 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
2172 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2173 // Validate image
2174 auto image_view = update->pImageInfo[di].imageView;
2175 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002176 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2177 error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002178 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002179 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
2180 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002181 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002182 return false;
2183 }
Mark Lobodzinski60e79032019-03-07 10:22:31 -07002184 if (device_data_->GetDeviceExtensions()->vk_khr_sampler_ycbcr_conversion) {
Tony-LunarG352f08f2019-01-03 10:11:24 -07002185 ImageSamplerDescriptor *desc = (ImageSamplerDescriptor *)descriptors_[index].get();
2186 if (desc->IsImmutableSampler()) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07002187 auto sampler_state = device_data_->GetSamplerState(desc->GetSampler());
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07002188 auto iv_state = device_data_->GetImageViewState(image_view);
Tony-LunarG352f08f2019-01-03 10:11:24 -07002189 if (iv_state && sampler_state) {
2190 if (iv_state->samplerConversion != sampler_state->samplerConversion) {
2191 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01948";
2192 std::stringstream error_str;
Tony-LunarG8a51f0a2019-01-04 16:14:14 -07002193 error_str << "Attempted write update to combined image sampler and image view and sampler ycbcr "
2194 "conversions are not identical, sampler: "
Tony-LunarG352f08f2019-01-03 10:11:24 -07002195 << desc->GetSampler() << " image view: " << iv_state->image_view << ".";
2196 *error_msg = error_str.str();
2197 return false;
2198 }
2199 }
2200 }
2201 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002202 }
2203 }
Tobin Ehlis648b1cf2018-04-13 15:24:13 -06002204 // fall through
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002205 case VK_DESCRIPTOR_TYPE_SAMPLER: {
2206 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2207 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
2208 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002209 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002210 std::stringstream error_str;
2211 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
2212 << update->pImageInfo[di].sampler << ".";
2213 *error_msg = error_str.str();
2214 return false;
2215 }
2216 } else {
2217 // TODO : Warn here
2218 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002219 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002220 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002221 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002222 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2223 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2224 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2225 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2226 auto image_view = update->pImageInfo[di].imageView;
2227 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002228 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2229 error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002230 std::stringstream error_str;
2231 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
2232 *error_msg = error_str.str();
2233 return false;
2234 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002235 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002236 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002237 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002238 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2239 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
2240 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2241 auto buffer_view = update->pTexelBufferView[di];
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07002242 auto bv_state = device_data_->GetBufferViewState(buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002243 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002244 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002245 std::stringstream error_str;
2246 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2247 *error_msg = error_str.str();
2248 return false;
2249 }
2250 auto buffer = bv_state->create_info.buffer;
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002251 auto buffer_state = device_data_->GetBufferState(buffer);
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002252 // Verify that buffer underlying the view hasn't been destroyed prematurely
2253 if (!buffer_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002254 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002255 std::stringstream error_str;
2256 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
2257 << ") has been destroyed: " << error_msg->c_str();
2258 *error_msg = error_str.str();
2259 return false;
2260 } else if (!ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002261 std::stringstream error_str;
2262 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
2263 *error_msg = error_str.str();
2264 return false;
2265 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002266 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002267 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002268 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002269 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2270 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2271 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2272 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2273 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufb45fdc32018-10-12 15:14:17 -06002274 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, func_name, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002275 std::stringstream error_str;
2276 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
2277 *error_msg = error_str.str();
2278 return false;
2279 }
2280 }
2281 break;
2282 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002283 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
2284 break;
Eric Werness30127fd2018-10-31 21:01:03 -07002285 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002286 // XXX TODO
2287 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002288 default:
2289 assert(0); // We've already verified update type so should never get here
2290 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002291 }
2292 // All checks passed so update contents are good
2293 return true;
2294}
2295// Verify that the contents of the update are ok, but don't perform actual update
2296bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
John Zulaufb45fdc32018-10-12 15:14:17 -06002297 VkDescriptorType type, uint32_t index, const char *func_name,
2298 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002299 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
2300 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06002301 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002302 case PlainSampler: {
2303 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002304 const auto src_desc = src_set->descriptors_[index + di].get();
2305 if (!src_desc->updated) continue;
2306 if (!src_desc->IsImmutableSampler()) {
2307 auto update_sampler = static_cast<SamplerDescriptor *>(src_desc)->GetSampler();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002308 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002309 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002310 std::stringstream error_str;
2311 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2312 *error_msg = error_str.str();
2313 return false;
2314 }
2315 } else {
2316 // TODO : Warn here
2317 }
2318 }
2319 break;
2320 }
2321 case ImageSampler: {
2322 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002323 const auto src_desc = src_set->descriptors_[index + di].get();
2324 if (!src_desc->updated) continue;
2325 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002326 // First validate sampler
2327 if (!img_samp_desc->IsImmutableSampler()) {
2328 auto update_sampler = img_samp_desc->GetSampler();
2329 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002330 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002331 std::stringstream error_str;
2332 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2333 *error_msg = error_str.str();
2334 return false;
2335 }
2336 } else {
2337 // TODO : Warn here
2338 }
2339 // Validate image
2340 auto image_view = img_samp_desc->GetImageView();
2341 auto image_layout = img_samp_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002342 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002343 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002344 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002345 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002346 return false;
2347 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002348 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002349 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002350 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002351 case Image: {
2352 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002353 const auto src_desc = src_set->descriptors_[index + di].get();
2354 if (!src_desc->updated) continue;
2355 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002356 auto image_view = img_desc->GetImageView();
2357 auto image_layout = img_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002358 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002359 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002360 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002361 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002362 return false;
2363 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002364 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002365 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002366 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002367 case TexelBuffer: {
2368 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002369 const auto src_desc = src_set->descriptors_[index + di].get();
2370 if (!src_desc->updated) continue;
2371 auto buffer_view = static_cast<TexelDescriptor *>(src_desc)->GetBufferView();
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07002372 auto bv_state = device_data_->GetBufferViewState(buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002373 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002374 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002375 std::stringstream error_str;
2376 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2377 *error_msg = error_str.str();
2378 return false;
2379 }
2380 auto buffer = bv_state->create_info.buffer;
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002381 if (!ValidateBufferUsage(device_data_->GetBufferState(buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002382 std::stringstream error_str;
2383 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
2384 *error_msg = error_str.str();
2385 return false;
2386 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002387 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002388 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002389 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002390 case GeneralBuffer: {
2391 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002392 const auto src_desc = src_set->descriptors_[index + di].get();
2393 if (!src_desc->updated) continue;
2394 auto buffer = static_cast<BufferDescriptor *>(src_desc)->GetBuffer();
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002395 if (!ValidateBufferUsage(device_data_->GetBufferState(buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002396 std::stringstream error_str;
2397 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
2398 *error_msg = error_str.str();
2399 return false;
2400 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06002401 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002402 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002403 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002404 case InlineUniform:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002405 case AccelerationStructure:
Jeff Bolze54ae892018-09-08 12:16:29 -05002406 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002407 default:
2408 assert(0); // We've already verified update type so should never get here
2409 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002410 }
2411 // All checks passed so update contents are good
2412 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12002413}
Tobin Ehlisf320b192017-03-14 11:22:50 -06002414// Update the common AllocateDescriptorSetsData
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002415void CoreChecks::UpdateAllocateDescriptorSetsData(const VkDescriptorSetAllocateInfo *p_alloc_info,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002416 cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisf320b192017-03-14 11:22:50 -06002417 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002418 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
Tobin Ehlisf320b192017-03-14 11:22:50 -06002419 if (layout) {
2420 ds_data->layout_nodes[i] = layout;
2421 // Count total descriptors required per type
2422 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
2423 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
2424 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
2425 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
2426 }
2427 }
2428 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
2429 }
Petr Kraus13c98a62017-12-09 00:22:39 +01002430}
Tobin Ehlisee471462016-05-26 11:21:59 -06002431// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002432bool CoreChecks::ValidateAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002433 const cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002434 bool skip = false;
Mark Lobodzinski7804bd42019-03-06 11:28:48 -07002435 auto pool_state = GetDescriptorPoolState(p_alloc_info->descriptorPool);
Tobin Ehlisee471462016-05-26 11:21:59 -06002436
2437 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002438 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07002439 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
John Zulauf1d27e0a2018-11-05 10:12:48 -07002440 if (layout->IsPushDescriptor()) {
John Zulauf5562d062018-01-24 11:54:05 -07002441 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 -06002442 HandleToUint64(p_alloc_info->pSetLayouts[i]), "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-00308",
Lockeca0d9792019-03-03 23:48:13 -07002443 "Layout %s specified at pSetLayouts[%" PRIu32
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002444 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
Lockeca0d9792019-03-03 23:48:13 -07002445 report_data->FormatHandle(p_alloc_info->pSetLayouts[i]).c_str(), i,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002446 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07002447 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002448 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT &&
2449 !(pool_state->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
2450 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 -06002451 0, "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-03044",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002452 "Descriptor set layout create flags and pool create flags mismatch for index (%d)", i);
2453 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002454 }
2455 }
Mark Lobodzinski60e79032019-03-07 10:22:31 -07002456 if (!GetDeviceExtensions()->vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002457 // Track number of descriptorSets allowable in this pool
2458 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002459 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 -06002460 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorSetCount-00306",
Lockeca0d9792019-03-03 23:48:13 -07002461 "Unable to allocate %u descriptorSets from pool %s"
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002462 ". This pool only has %d descriptorSets remaining.",
Lockeca0d9792019-03-03 23:48:13 -07002463 p_alloc_info->descriptorSetCount, report_data->FormatHandle(pool_state->pool).c_str(),
2464 pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002465 }
2466 // Determine whether descriptor counts are satisfiable
Jeff Bolze54ae892018-09-08 12:16:29 -05002467 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2468 if (ds_data->required_descriptors_by_type.at(it->first) > pool_state->availableDescriptorTypeCount[it->first]) {
Lockeca0d9792019-03-03 23:48:13 -07002469 skip |= log_msg(
2470 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
2471 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307",
2472 "Unable to allocate %u descriptors of type %s from pool %s"
2473 ". This pool only has %d descriptors of this type remaining.",
2474 ds_data->required_descriptors_by_type.at(it->first), string_VkDescriptorType(VkDescriptorType(it->first)),
2475 report_data->FormatHandle(pool_state->pool).c_str(), pool_state->availableDescriptorTypeCount[it->first]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002476 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002477 }
2478 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06002479
Jeff Bolzfdf96072018-04-10 14:32:18 -05002480 const auto *count_allocate_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2481
2482 if (count_allocate_info) {
2483 if (count_allocate_info->descriptorSetCount != 0 &&
2484 count_allocate_info->descriptorSetCount != p_alloc_info->descriptorSetCount) {
2485 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 -06002486 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-descriptorSetCount-03045",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002487 "VkDescriptorSetAllocateInfo::descriptorSetCount (%d) != "
2488 "VkDescriptorSetVariableDescriptorCountAllocateInfoEXT::descriptorSetCount (%d)",
2489 p_alloc_info->descriptorSetCount, count_allocate_info->descriptorSetCount);
2490 }
2491 if (count_allocate_info->descriptorSetCount == p_alloc_info->descriptorSetCount) {
2492 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002493 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002494 if (count_allocate_info->pDescriptorCounts[i] > layout->GetDescriptorCountFromBinding(layout->GetMaxBinding())) {
2495 skip |= log_msg(
2496 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 -06002497 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-pSetLayouts-03046",
2498 "pDescriptorCounts[%d] = (%d), binding's descriptorCount = (%d)", i,
Jeff Bolzfdf96072018-04-10 14:32:18 -05002499 count_allocate_info->pDescriptorCounts[i], layout->GetDescriptorCountFromBinding(layout->GetMaxBinding()));
2500 }
2501 }
2502 }
2503 }
2504
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002505 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06002506}
2507// Decrement allocated sets from the pool and insert new sets into set_map
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002508void CoreChecks::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
2509 const VkDescriptorSet *descriptor_sets,
2510 const cvdescriptorset::AllocateDescriptorSetsData *ds_data,
2511 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Mark Lobodzinski254c8512019-03-09 12:21:15 -07002512 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map) {
Tobin Ehlisee471462016-05-26 11:21:59 -06002513 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002514 // Account for sets and individual descriptors allocated from pool
Tobin Ehlisee471462016-05-26 11:21:59 -06002515 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Jeff Bolze54ae892018-09-08 12:16:29 -05002516 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2517 pool_state->availableDescriptorTypeCount[it->first] -= ds_data->required_descriptors_by_type.at(it->first);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06002518 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002519
2520 const auto *variable_count_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2521 bool variable_count_valid = variable_count_info && variable_count_info->descriptorSetCount == p_alloc_info->descriptorSetCount;
2522
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002523 // Create tracking object for each descriptor set; insert into global map and the pool's set.
Tobin Ehlisee471462016-05-26 11:21:59 -06002524 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -05002525 uint32_t variable_count = variable_count_valid ? variable_count_info->pDescriptorCounts[i] : 0;
2526
Tobin Ehlis93f22372016-10-12 14:34:12 -06002527 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i],
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002528 variable_count, this);
Tobin Ehlisee471462016-05-26 11:21:59 -06002529
2530 pool_state->sets.insert(new_ds);
2531 new_ds->in_use.store(0);
2532 (*set_map)[descriptor_sets[i]] = new_ds;
2533 }
2534}
John Zulauf48a6a702017-12-22 17:14:54 -07002535
2536cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2537 GLOBAL_CB_NODE *cb_state)
2538 : filtered_map_(), orig_map_(in_map) {
2539 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2540 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2541 ds.FilterAndTrackBindingReqs(cb_state, orig_map_, filtered_map_.get());
2542 }
2543}
2544cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2545 GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline)
2546 : filtered_map_(), orig_map_(in_map) {
2547 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2548 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2549 ds.FilterAndTrackBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
2550 }
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01002551}