blob: d15f18b0e5faf576ea2d7f9d3aec51ffbd9f46aa [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
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700839 // Copy first mip level into sub_layers and loop over each mip level to verify layout
840 VkImageSubresourceLayers sub_layers;
841 sub_layers.aspectMask = image_view_ci.subresourceRange.aspectMask;
842 sub_layers.baseArrayLayer = image_view_ci.subresourceRange.baseArrayLayer;
843 sub_layers.layerCount = image_view_ci.subresourceRange.layerCount;
844 bool hit_error = false;
845 for (auto cur_level = image_view_ci.subresourceRange.baseMipLevel;
846 cur_level < image_view_ci.subresourceRange.levelCount; ++cur_level) {
847 sub_layers.mipLevel = cur_level;
Cort Stratton7df30962018-05-17 19:45:57 -0700848 // No "invalid layout" VUID required for this call, since the optimal_layout parameter is UNDEFINED.
Mark Lobodzinski8564e442019-03-07 12:49:41 -0700849 device_data_->VerifyImageLayout(cb_node, image_node, sub_layers, image_layout, VK_IMAGE_LAYOUT_UNDEFINED,
850 caller, kVUIDUndefined, "VUID-VkDescriptorImageInfo-imageLayout-00344",
851 &hit_error);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700852 if (hit_error) {
853 *error =
John Zulauf1d27e0a2018-11-05 10:12:48 -0700854 "Image layout specified at vkUpdateDescriptorSet* or vkCmdPushDescriptorSet* time "
John Zulaufb45fdc32018-10-12 15:14:17 -0600855 "doesn't match actual image layout at time descriptor is used. See previous error callback for "
856 "specific details.";
Chris Forbes57989132016-07-26 17:06:10 +1200857 return false;
858 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700859 }
860 // Verify Sample counts
861 if ((reqs & DESCRIPTOR_REQ_SINGLE_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 VK_SAMPLE_COUNT_1_BIT but got "
865 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
866 *error = error_str.str();
867 return false;
868 }
869 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
870 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600871 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700872 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
873 *error = error_str.str();
874 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200875 }
Chris Forbese92dd1d2019-01-21 15:58:57 -0800876 } else if (descriptor_class == TexelBuffer) {
877 auto texel_buffer = static_cast<TexelDescriptor *>(descriptors_[i].get());
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -0700878 auto buffer_view = device_data_->GetBufferViewState(texel_buffer->GetBufferView());
Chris Forbese92dd1d2019-01-21 15:58:57 -0800879
880 auto reqs = binding_pair.second;
881 auto format_bits = DescriptorRequirementsBitsFromFormat(buffer_view->create_info.format);
882
883 if (!(reqs & format_bits)) {
884 // bad component type
885 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600886 error_str << "Descriptor in binding #" << binding << " index " << index << " requires "
Chris Forbese92dd1d2019-01-21 15:58:57 -0800887 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
888 << string_VkFormat(buffer_view->create_info.format) << ".";
889 *error = error_str.str();
890 return false;
891 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600892 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600893 if (descriptor_class == ImageSampler || descriptor_class == PlainSampler) {
894 // Verify Sampler still valid
895 VkSampler sampler;
896 if (descriptor_class == ImageSampler) {
897 sampler = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetSampler();
898 } else {
899 sampler = static_cast<SamplerDescriptor *>(descriptors_[i].get())->GetSampler();
900 }
901 if (!ValidateSampler(sampler, device_data_)) {
902 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600903 error_str << "Descriptor in binding #" << binding << " index " << index << " is using sampler " << sampler
904 << " that has been destroyed.";
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600905 *error = error_str.str();
906 return false;
907 }
908 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600909 }
910 }
911 }
912 return true;
913}
Chris Forbes57989132016-07-26 17:06:10 +1200914
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600915// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600916uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600917 std::unordered_set<VkBuffer> *buffer_set,
918 std::unordered_set<VkImageView> *image_set) const {
919 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200920 for (auto binding_pair : bindings) {
921 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600922 // If a binding doesn't exist, skip it
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600923 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600924 continue;
925 }
John Zulaufc483f442017-12-15 14:02:06 -0700926 uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600927 if (descriptors_[start_idx]->IsStorage()) {
928 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600929 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600930 if (descriptors_[start_idx + i]->updated) {
931 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600932 num_updates++;
933 }
934 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600935 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600936 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600937 if (descriptors_[start_idx + i]->updated) {
938 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -0700939 auto bv_state = device_data_->GetBufferViewState(bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600940 if (bv_state) {
941 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600942 num_updates++;
943 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600944 }
945 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600946 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600947 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600948 if (descriptors_[start_idx + i]->updated) {
949 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600950 num_updates++;
951 }
952 }
953 }
954 }
955 }
956 return num_updates;
957}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600958// Set is being deleted or updates so invalidate all bound cmd buffers
959void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Mark Lobodzinski9ddb7182019-03-08 17:31:09 -0700960 device_data_->InvalidateCommandBuffers(cb_bindings, {HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600961}
John Zulauf1d27e0a2018-11-05 10:12:48 -0700962
963// Loop through the write updates to do for a push descriptor set, ignoring dstSet
964void cvdescriptorset::DescriptorSet::PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds) {
965 assert(IsPushDescriptor());
966 for (uint32_t i = 0; i < write_count; i++) {
967 PerformWriteUpdate(&p_wds[i]);
968 }
969}
970
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600971// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600972void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700973 // Perform update on a per-binding basis as consecutive updates roll over to next binding
974 auto descriptors_remaining = update->descriptorCount;
975 auto binding_being_updated = update->dstBinding;
976 auto offset = update->dstArrayElement;
Tobin Ehlise16805c2017-08-09 09:10:37 -0600977 uint32_t update_index = 0;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700978 while (descriptors_remaining) {
979 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
John Zulaufc483f442017-12-15 14:02:06 -0700980 auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700981 // Loop over the updates for a single binding at a time
Tobin Ehlise16805c2017-08-09 09:10:37 -0600982 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
983 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700984 }
985 // Roll over to next binding in case of consecutive update
986 descriptors_remaining -= update_count;
987 offset = 0;
988 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600989 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700990 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600991
Jeff Bolzfdf96072018-04-10 14:32:18 -0500992 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
993 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
994 InvalidateBoundCmdBuffers();
995 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600996}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600997// Validate Copy update
998bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -0600999 const DescriptorSet *src_set, const char *func_name,
1000 std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001001 // Verify dst layout still valid
1002 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001003 *error_code = "VUID-VkCopyDescriptorSet-dstSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -07001004 string_sprintf(error_msg,
Lockeca0d9792019-03-03 23:48:13 -07001005 "Cannot call %s to perform copy update on descriptor set dstSet %s"
1006 " created with destroyed VkDescriptorSetLayout %s.",
1007 func_name, report_data->FormatHandle(set_).c_str(),
1008 report_data->FormatHandle(p_layout_->GetDescriptorSetLayout()).c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001009 return false;
1010 }
1011
1012 // Verify src layout still valid
1013 if (src_set->p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001014 *error_code = "VUID-VkCopyDescriptorSet-srcSet-parameter";
John Zulaufb45fdc32018-10-12 15:14:17 -06001015 string_sprintf(error_msg,
Lockeca0d9792019-03-03 23:48:13 -07001016 "Cannot call %s to perform copy update of dstSet %s"
1017 " from descriptor set srcSet %s"
1018 " created with destroyed VkDescriptorSetLayout %s.",
1019 func_name, report_data->FormatHandle(set_).c_str(), report_data->FormatHandle(src_set->set_).c_str(),
1020 report_data->FormatHandle(src_set->p_layout_->GetDescriptorSetLayout()).c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001021 return false;
1022 }
1023
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001024 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001025 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-00347";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001026 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001027 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001028 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001029 return false;
1030 }
1031 if (!src_set->HasBinding(update->srcBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001032 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-00345";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001033 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001034 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001035 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001036 return false;
1037 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001038 // Verify idle ds
1039 if (in_use.load() &&
1040 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1041 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1042 // TODO : Re-using Free Idle error code, need copy update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001043 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001044 std::stringstream error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001045 error_str << "Cannot call " << func_name << " to perform copy update on descriptor set " << set_
Jeff Bolzfdf96072018-04-10 14:32:18 -05001046 << " that is in use by a command buffer";
1047 *error_msg = error_str.str();
1048 return false;
1049 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001050 // src & dst set bindings are valid
1051 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -07001052 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001053 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
1054 // SRC update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001055 *error_code = "VUID-VkCopyDescriptorSet-srcArrayElement-00346";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001056 std::stringstream error_str;
1057 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
John Zulaufc483f442017-12-15 14:02:06 -07001058 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001059 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001060 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001061 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001062 return false;
1063 }
John Zulaufc483f442017-12-15 14:02:06 -07001064 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001065 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001066 // DST update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001067 *error_code = "VUID-VkCopyDescriptorSet-dstArrayElement-00348";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001068 std::stringstream error_str;
1069 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
John Zulaufc483f442017-12-15 14:02:06 -07001070 << " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001071 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001072 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001073 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001074 return false;
1075 }
1076 // Check that types match
Dave Houltond8ed0212018-05-16 17:18:24 -06001077 // TODO : Base default error case going from here is "VUID-VkAcquireNextImageInfoKHR-semaphore-parameter"2ba which covers all
1078 // consistency issues, need more fine-grained error codes
Dave Houlton00c154e2018-05-24 13:20:50 -06001079 *error_code = "VUID-VkCopyDescriptorSet-srcSet-00349";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001080 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001081 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001082 if (src_type != dst_type) {
1083 std::stringstream error_str;
1084 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
1085 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001086 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001087 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001088 return false;
1089 }
1090 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -06001091 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001092 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001093 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001094 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001095 return false;
1096 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001097
1098 if ((src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1099 !(GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001100 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01918";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001101 std::stringstream error_str;
1102 error_str << "If pname:srcSet's (" << update->srcSet
1103 << ") layout was created with the "
1104 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1105 "set, then pname:dstSet's ("
1106 << update->dstSet
1107 << ") layout must: also have been created with the "
1108 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1109 *error_msg = error_str.str();
1110 return false;
1111 }
1112
1113 if (!(src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1114 (GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001115 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01919";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001116 std::stringstream error_str;
1117 error_str << "If pname:srcSet's (" << update->srcSet
1118 << ") layout was created without the "
1119 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1120 "set, then pname:dstSet's ("
1121 << update->dstSet
1122 << ") layout must: also have been created without the "
1123 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1124 *error_msg = error_str.str();
1125 return false;
1126 }
1127
1128 if ((src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1129 !(GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001130 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01920";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001131 std::stringstream error_str;
1132 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1133 << ") was allocated was created "
1134 "with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1135 "set, then the descriptor pool from which pname:dstSet ("
1136 << update->dstSet
1137 << ") was allocated must: "
1138 "also have been created with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1139 *error_msg = error_str.str();
1140 return false;
1141 }
1142
1143 if (!(src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1144 (GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001145 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01921";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001146 std::stringstream error_str;
1147 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1148 << ") was allocated was created "
1149 "without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1150 "set, then the descriptor pool from which pname:dstSet ("
1151 << update->dstSet
1152 << ") was allocated must: "
1153 "also have been created without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1154 *error_msg = error_str.str();
1155 return false;
1156 }
1157
Jeff Bolze54ae892018-09-08 12:16:29 -05001158 if (src_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1159 if ((update->srcArrayElement % 4) != 0) {
1160 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02223";
1161 std::stringstream error_str;
1162 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1163 << "srcArrayElement " << update->srcArrayElement << " not a multiple of 4";
1164 *error_msg = error_str.str();
1165 return false;
1166 }
1167 if ((update->dstArrayElement % 4) != 0) {
1168 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02224";
1169 std::stringstream error_str;
1170 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1171 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1172 *error_msg = error_str.str();
1173 return false;
1174 }
1175 if ((update->descriptorCount % 4) != 0) {
1176 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02225";
1177 std::stringstream error_str;
1178 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1179 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1180 *error_msg = error_str.str();
1181 return false;
1182 }
1183 }
1184
Tobin Ehlisd41e7b62016-05-19 07:56:18 -06001185 // Update parameters all look good and descriptor updated so verify update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06001186 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 -06001187
1188 // All checks passed so update is good
1189 return true;
1190}
1191// Perform Copy update
1192void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -07001193 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
1194 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001195 // Update parameters all look good so perform update
1196 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001197 auto src = src_set->descriptors_[src_start_idx + di].get();
1198 auto dst = descriptors_[dst_start_idx + di].get();
1199 if (src->updated) {
1200 dst->CopyUpdate(src);
1201 some_update_ = true;
1202 } else {
1203 dst->updated = false;
1204 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001205 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001206
Jeff Bolzfdf96072018-04-10 14:32:18 -05001207 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1208 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1209 InvalidateBoundCmdBuffers();
1210 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001211}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001212
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001213// Update the drawing state for the affected descriptors.
1214// Set cb_node to this set and this set to cb_node.
1215// Add the bindings of the descriptor
1216// Set the layout based on the current descriptor layout (will mask subsequent layer mismatch errors)
1217// TODO: Modify the UpdateDrawState virtural functions to *only* set initial layout and not change layouts
Tobin Ehlisf9519102016-08-17 09:49:13 -06001218// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
1219// to be used in a draw by the given cb_node
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001220void cvdescriptorset::DescriptorSet::UpdateDrawState(CoreChecks *device_data, GLOBAL_CB_NODE *cb_node,
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001221 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001222 // bind cb to this descriptor set
1223 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001224 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Petr Krausbc7f5442017-05-14 23:43:38 +02001225 cb_node->object_bindings.insert({HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001226 pool_state_->cb_bindings.insert(cb_node);
Petr Krausbc7f5442017-05-14 23:43:38 +02001227 cb_node->object_bindings.insert({HandleToUint64(pool_state_->pool), kVulkanObjectTypeDescriptorPool});
Tobin Ehlisf9519102016-08-17 09:49:13 -06001228 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
1229 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -07001230 for (auto binding_req_pair : binding_req_map) {
1231 auto binding = binding_req_pair.first;
Tony-LunarG62c5dba2018-12-20 14:27:23 -07001232 // We aren't validating descriptors created with PARTIALLY_BOUND or UPDATE_AFTER_BIND, so don't record state
1233 if (p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
1234 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) {
1235 continue;
1236 }
John Zulaufc483f442017-12-15 14:02:06 -07001237 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
1238 for (uint32_t i = range.start; i < range.end; ++i) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001239 descriptors_[i]->UpdateDrawState(device_data, cb_node);
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001240 }
1241 }
1242}
1243
John Zulauf48a6a702017-12-22 17:14:54 -07001244void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1245 const BindingReqMap &in_req, BindingReqMap *out_req,
1246 TrackedBindings *bindings) {
1247 assert(out_req);
1248 assert(bindings);
1249 const auto binding = binding_req_pair.first;
1250 // Use insert and look at the boolean ("was inserted") in the returned pair to see if this is a new set member.
1251 // Saves one hash lookup vs. find ... compare w/ end ... insert.
1252 const auto it_bool_pair = bindings->insert(binding);
1253 if (it_bool_pair.second) {
1254 out_req->emplace(binding_req_pair);
1255 }
1256}
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001257
John Zulauf48a6a702017-12-22 17:14:54 -07001258void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1259 const BindingReqMap &in_req, BindingReqMap *out_req,
1260 TrackedBindings *bindings, uint32_t limit) {
1261 if (bindings->size() < limit) FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, bindings);
1262}
1263
1264void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, const BindingReqMap &in_req,
1265 BindingReqMap *out_req) {
1266 TrackedBindings &bound = cached_validation_[cb_state].command_binding_and_usage;
1267 if (bound.size() == GetBindingCount()) {
1268 return; // All bindings are bound, out req is empty
1269 }
1270 for (const auto &binding_req_pair : in_req) {
1271 const auto binding = binding_req_pair.first;
1272 // If a binding doesn't exist, or has already been bound, skip it
1273 if (p_layout_->HasBinding(binding)) {
1274 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, &bound);
1275 }
1276 }
1277}
1278
1279void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline,
1280 const BindingReqMap &in_req, BindingReqMap *out_req) {
1281 auto &validated = cached_validation_[cb_state];
1282 auto &image_sample_val = validated.image_samplers[pipeline];
1283 auto *const dynamic_buffers = &validated.dynamic_buffers;
1284 auto *const non_dynamic_buffers = &validated.non_dynamic_buffers;
1285 const auto &stats = p_layout_->GetBindingTypeStats();
1286 for (const auto &binding_req_pair : in_req) {
1287 auto binding = binding_req_pair.first;
1288 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
1289 if (!layout_binding) {
1290 continue;
1291 }
1292 // Caching criteria differs per type.
1293 // If image_layout have changed , the image descriptors need to be validated against them.
1294 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1295 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
1296 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, dynamic_buffers, stats.dynamic_buffer_count);
1297 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1298 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
1299 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
1300 } else {
1301 // This is rather crude, as the changed layouts may not impact the bound descriptors,
1302 // but the simple "versioning" is a simple "dirt" test.
1303 auto &version = image_sample_val[binding]; // Take advantage of default construtor zero initialzing new entries
1304 if (version != cb_state->image_layout_change_count) {
1305 version = cb_state->image_layout_change_count;
1306 out_req->emplace(binding_req_pair);
1307 }
1308 }
1309 }
1310}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001311
Tobin Ehlis300888c2016-05-18 13:43:26 -06001312cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001313 updated = false;
1314 descriptor_class = PlainSampler;
1315 if (immut) {
1316 sampler_ = *immut;
1317 immutable_ = true;
1318 updated = true;
1319 }
1320}
Tobin Ehlise2f80292016-06-02 10:08:53 -06001321// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001322bool cvdescriptorset::ValidateSampler(const VkSampler sampler, CoreChecks *dev_data) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001323 return (dev_data->GetSamplerState(sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001324}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001325
Tobin Ehlis554bf382016-05-24 11:14:43 -06001326bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001327 CoreChecks *dev_data, const char *func_name, std::string *error_code,
John Zulaufb45fdc32018-10-12 15:14:17 -06001328 std::string *error_msg) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001329 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00326";
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001330 auto iv_state = dev_data->GetImageViewState(image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001331 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001332 std::stringstream error_str;
1333 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001334 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001335 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001336 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001337 // 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 -06001338 // Validate that imageLayout is compatible with aspect_mask and image format
1339 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001340 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
1341 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001342 VkFormat format = VK_FORMAT_MAX_ENUM;
1343 VkImageUsageFlags usage = 0;
Mark Lobodzinski8ddc23f2019-03-06 11:48:49 -07001344 auto image_node = dev_data->GetImageState(image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001345 if (image_node) {
1346 format = image_node->createInfo.format;
1347 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001348 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001349 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
1350 // the error here occurs is if memory bound to a created imageView has been freed.
Mark Lobodzinski0ebe9712019-03-07 13:39:07 -07001351 if (dev_data->ValidateMemoryIsBoundToImage(image_node, func_name, "VUID-VkImageViewCreateInfo-image-01020")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001352 *error_code = "VUID-VkImageViewCreateInfo-image-01020";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001353 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001354 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001355 }
Chris Forbes67757ff2017-07-21 13:59:01 -07001356
1357 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
1358 // but not binding them to descriptor sets.
1359 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D &&
1360 (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
1361 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001362 *error_code = "VUID-VkDescriptorImageInfo-imageView-00343";
Chris Forbes67757ff2017-07-21 13:59:01 -07001363 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
1364 return false;
1365 }
Tobin Ehlis1809f912016-05-25 09:24:36 -06001366 }
1367 // First validate that format and layout are compatible
1368 if (format == VK_FORMAT_MAX_ENUM) {
1369 std::stringstream error_str;
1370 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001371 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001372 return false;
1373 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001374 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
1375 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -06001376 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -06001377 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001378 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1379 // Only Color bit must be set
1380 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001381 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -07001382 error_str
1383 << "ImageView (" << image_view
1384 << ") 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 -06001385 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001386 return false;
1387 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001388 // format must NOT be DS
1389 if (ds) {
1390 std::stringstream error_str;
1391 error_str << "ImageView (" << image_view
1392 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1393 << string_VkFormat(format) << " which is not a color format.";
1394 *error_msg = error_str.str();
1395 return false;
1396 }
1397 break;
1398 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1399 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1400 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001401 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1402 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1403 // both must NOT be set
1404 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001405 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001406 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001407 return false;
1408 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001409 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1410 // Neither were set
1411 std::stringstream error_str;
1412 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1413 << " but does not have STENCIL or DEPTH aspects set";
1414 *error_msg = error_str.str();
1415 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001416 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001417 // format must be DS
1418 if (!ds) {
1419 std::stringstream error_str;
1420 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1421 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
1422 *error_msg = error_str.str();
1423 return false;
1424 }
1425 break;
1426 default:
1427 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1428 if (ds) {
1429 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1430 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1431 // both must NOT be set
1432 std::stringstream error_str;
1433 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1434 << " and is using depth/stencil image of format " << string_VkFormat(format)
1435 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1436 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1437 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1438 "reads respectively.";
1439 *error_msg = error_str.str();
1440 return false;
1441 }
1442 }
1443 }
1444 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001445 }
1446 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001447 // 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 -06001448 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1449 // under vkCreateImage()
Dave Houltond8ed0212018-05-16 17:18:24 -06001450 // TODO : Need to also validate case "VUID-VkWriteDescriptorSet-descriptorType-00336" where STORAGE_IMAGE & INPUT_ATTACH types
1451 // must have been created with identify swizzle
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001452 const char *error_usage_bit = nullptr;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001453 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001454 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1455 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1456 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1457 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
1458 }
1459 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001460 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001461 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1462 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1463 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
1464 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
1465 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001466 // TODO : Need to create custom enum error codes for these cases
1467 if (image_node->shared_presentable) {
1468 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001469 error_str << "ImageView (" << image_view
1470 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
1471 "layout "
1472 << string_VkImageLayout(image_layout)
1473 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report "
1474 "support for VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the "
1475 "VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001476 *error_msg = error_str.str();
1477 return false;
1478 }
1479 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001480 error_str << "ImageView (" << image_view
1481 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
1482 << string_VkImageLayout(image_layout)
1483 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage "
1484 "images can only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001485 *error_msg = error_str.str();
1486 return false;
1487 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001488 }
1489 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001490 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001491 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
1492 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
1493 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
1494 }
1495 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001496 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001497 default:
1498 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001499 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001500 if (error_usage_bit) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001501 std::stringstream error_str;
1502 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
1503 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1504 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001505 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001506 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001507 }
John Zulauff4c07882019-01-24 14:03:36 -07001508
1509 if ((type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) || (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)) {
1510 // Test that the layout is compatible with the descriptorType for the two sampled image types
1511 const static std::array<VkImageLayout, 3> valid_layouts = {
1512 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL};
1513
1514 struct ExtensionLayout {
1515 VkImageLayout layout;
1516 bool DeviceExtensions::*extension;
1517 };
1518
1519 const static std::array<ExtensionLayout, 3> extended_layouts{
1520 {// Note double brace req'd for aggregate initialization
1521 {VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR, &DeviceExtensions::vk_khr_shared_presentable_image},
1522 {VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2},
1523 {VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2}}};
1524 auto is_layout = [image_layout, dev_data](const ExtensionLayout &ext_layout) {
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001525 return dev_data->device_extensions.*(ext_layout.extension) && (ext_layout.layout == image_layout);
John Zulauff4c07882019-01-24 14:03:36 -07001526 };
1527
1528 bool valid_layout = (std::find(valid_layouts.cbegin(), valid_layouts.cend(), image_layout) != valid_layouts.cend()) ||
1529 std::any_of(extended_layouts.cbegin(), extended_layouts.cend(), is_layout);
1530
1531 if (!valid_layout) {
1532 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01403";
1533 std::stringstream error_str;
1534 error_str << "Descriptor update with descriptorType " << string_VkDescriptorType(type)
1535 << " is being updated with invalid imageLayout " << string_VkImageLayout(image_layout)
1536 << ". Allowed layouts are: VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, "
1537 << "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL";
1538 for (auto &ext_layout : extended_layouts) {
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001539 if (dev_data->device_extensions.*(ext_layout.extension)) {
John Zulauff4c07882019-01-24 14:03:36 -07001540 error_str << ", " << string_VkImageLayout(ext_layout.layout);
1541 }
1542 }
1543 *error_msg = error_str.str();
1544 return false;
1545 }
1546 }
1547
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001548 return true;
1549}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001550
Tobin Ehlis300888c2016-05-18 13:43:26 -06001551void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Chris Forbesfea2c542018-04-13 09:34:15 -07001552 if (!immutable_) {
1553 sampler_ = update->pImageInfo[index].sampler;
1554 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001555 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001556}
1557
Tobin Ehlis300888c2016-05-18 13:43:26 -06001558void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001559 if (!immutable_) {
1560 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001561 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001562 }
1563 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001564}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001565
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001566void cvdescriptorset::SamplerDescriptor::UpdateDrawState(CoreChecks *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001567 if (!immutable_) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001568 auto sampler_state = dev_data->GetSamplerState(sampler_);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001569 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001570 }
1571}
1572
Tobin Ehlis300888c2016-05-18 13:43:26 -06001573cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001574 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001575 updated = false;
1576 descriptor_class = ImageSampler;
1577 if (immut) {
1578 sampler_ = *immut;
1579 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001580 }
1581}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001582
Tobin Ehlis300888c2016-05-18 13:43:26 -06001583void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001584 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001585 const auto &image_info = update->pImageInfo[index];
Chris Forbesfea2c542018-04-13 09:34:15 -07001586 if (!immutable_) {
1587 sampler_ = image_info.sampler;
1588 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001589 image_view_ = image_info.imageView;
1590 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001591}
1592
Tobin Ehlis300888c2016-05-18 13:43:26 -06001593void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001594 if (!immutable_) {
1595 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001596 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001597 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001598 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1599 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001600 updated = true;
1601 image_view_ = image_view;
1602 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001603}
1604
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001605void cvdescriptorset::ImageSamplerDescriptor::UpdateDrawState(CoreChecks *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001606 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001607 if (!immutable_) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001608 auto sampler_state = dev_data->GetSamplerState(sampler_);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001609 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001610 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001611 // Add binding for image
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001612 auto iv_state = dev_data->GetImageViewState(image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001613 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001614 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001615 }
Jeff Bolz148d94e2018-12-13 21:25:56 -06001616 if (image_view_) {
Mark Lobodzinski8564e442019-03-07 12:49:41 -07001617 dev_data->SetImageViewLayout(cb_node, image_view_, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06001618 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001619}
1620
Tobin Ehlis300888c2016-05-18 13:43:26 -06001621cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1622 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001623 updated = false;
1624 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001625 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001626}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001627
Tobin Ehlis300888c2016-05-18 13:43:26 -06001628void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001629 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001630 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001631 image_view_ = image_info.imageView;
1632 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001633}
1634
Tobin Ehlis300888c2016-05-18 13:43:26 -06001635void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001636 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1637 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001638 updated = true;
1639 image_view_ = image_view;
1640 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001641}
1642
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001643void cvdescriptorset::ImageDescriptor::UpdateDrawState(CoreChecks *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001644 // Add binding for image
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001645 auto iv_state = dev_data->GetImageViewState(image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001646 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001647 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001648 }
Jeff Bolz148d94e2018-12-13 21:25:56 -06001649 if (image_view_) {
Mark Lobodzinski8564e442019-03-07 12:49:41 -07001650 dev_data->SetImageViewLayout(cb_node, image_view_, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06001651 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001652}
1653
Tobin Ehlis300888c2016-05-18 13:43:26 -06001654cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1655 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001656 updated = false;
1657 descriptor_class = GeneralBuffer;
1658 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1659 dynamic_ = true;
1660 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1661 storage_ = true;
1662 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1663 dynamic_ = true;
1664 storage_ = true;
1665 }
1666}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001667void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001668 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001669 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001670 buffer_ = buffer_info.buffer;
1671 offset_ = buffer_info.offset;
1672 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001673}
1674
Tobin Ehlis300888c2016-05-18 13:43:26 -06001675void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1676 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001677 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001678 buffer_ = buff_desc->buffer_;
1679 offset_ = buff_desc->offset_;
1680 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001681}
1682
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001683void cvdescriptorset::BufferDescriptor::UpdateDrawState(CoreChecks *dev_data, GLOBAL_CB_NODE *cb_node) {
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07001684 auto buffer_node = dev_data->GetBufferState(buffer_);
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001685 if (buffer_node) dev_data->AddCommandBufferBindingBuffer(cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001686}
1687
Tobin Ehlis300888c2016-05-18 13:43:26 -06001688cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001689 updated = false;
1690 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001691 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001692}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001693
Tobin Ehlis300888c2016-05-18 13:43:26 -06001694void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001695 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001696 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001697}
1698
Tobin Ehlis300888c2016-05-18 13:43:26 -06001699void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1700 updated = true;
1701 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1702}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001703
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001704void cvdescriptorset::TexelDescriptor::UpdateDrawState(CoreChecks *dev_data, GLOBAL_CB_NODE *cb_node) {
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07001705 auto bv_state = dev_data->GetBufferViewState(buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001706 if (bv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001707 dev_data->AddCommandBufferBindingBufferView(cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001708 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001709}
1710
Tobin Ehlis300888c2016-05-18 13:43:26 -06001711// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1712// sets, and then calls their respective Validate[Write|Copy]Update functions.
1713// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1714// be skipped, then true is returned.
1715// If there is no issue with the update, then false is returned.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001716bool CoreChecks::ValidateUpdateDescriptorSets(uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001717 const VkCopyDescriptorSet *p_cds, const char *func_name) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001718 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001719 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001720 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001721 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001722 auto set_node = GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001723 if (!set_node) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001724 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1725 HandleToUint64(dest_set), kVUID_Core_DrawState_InvalidDescriptorSet,
Lockeca0d9792019-03-03 23:48:13 -07001726 "Cannot call %s on descriptor set %s that has not been allocated.", func_name,
1727 report_data->FormatHandle(dest_set).c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001728 } else {
Dave Houltond8ed0212018-05-16 17:18:24 -06001729 std::string error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001730 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001731 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], func_name, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001732 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 -06001733 HandleToUint64(dest_set), error_code,
Lockeca0d9792019-03-03 23:48:13 -07001734 "%s failed write update validation for Descriptor Set %s with error: %s.", func_name,
1735 report_data->FormatHandle(dest_set).c_str(), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001736 }
1737 }
1738 }
1739 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001740 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001741 auto dst_set = p_cds[i].dstSet;
1742 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001743 auto src_node = GetSetNode(src_set);
1744 auto dst_node = GetSetNode(dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001745 // Object_tracker verifies that src & dest descriptor set are valid
1746 assert(src_node);
1747 assert(dst_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001748 std::string error_code;
Tobin Ehlisa1712752017-01-04 09:41:47 -07001749 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001750 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, func_name, &error_code, &error_str)) {
Lockeca0d9792019-03-03 23:48:13 -07001751 skip |= log_msg(
1752 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, HandleToUint64(dst_set),
1753 error_code, "%s failed copy update from Descriptor Set %s to Descriptor Set %s with error: %s.", func_name,
1754 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 -06001755 }
1756 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001757 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001758}
1759// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1760// sets, and then calls their respective Perform[Write|Copy]Update functions.
1761// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1762// with the same set of updates.
1763// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1764// calling down the chain.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001765void cvdescriptorset::PerformUpdateDescriptorSets(CoreChecks *dev_data, uint32_t write_count, const VkWriteDescriptorSet *p_wds,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001766 uint32_t copy_count, const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001767 // Write updates first
1768 uint32_t i = 0;
1769 for (i = 0; i < write_count; ++i) {
1770 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001771 auto set_node = dev_data->GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001772 if (set_node) {
1773 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001774 }
1775 }
1776 // Now copy updates
1777 for (i = 0; i < copy_count; ++i) {
1778 auto dst_set = p_cds[i].dstSet;
1779 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001780 auto src_node = dev_data->GetSetNode(src_set);
1781 auto dst_node = dev_data->GetSetNode(dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001782 if (src_node && dst_node) {
1783 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001784 }
1785 }
1786}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001787
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001788cvdescriptorset::DecodedTemplateUpdate::DecodedTemplateUpdate(CoreChecks *device_data, VkDescriptorSet descriptorSet,
John Zulauf1d27e0a2018-11-05 10:12:48 -07001789 const TEMPLATE_STATE *template_state, const void *pData,
1790 VkDescriptorSetLayout push_layout) {
John Zulaufb845eb22018-10-12 11:41:06 -06001791 auto const &create_info = template_state->create_info;
1792 inline_infos.resize(create_info.descriptorUpdateEntryCount); // Make sure we have one if we need it
1793 desc_writes.reserve(create_info.descriptorUpdateEntryCount); // emplaced, so reserved without initialization
John Zulauf1d27e0a2018-11-05 10:12:48 -07001794 VkDescriptorSetLayout effective_dsl = create_info.templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
1795 ? create_info.descriptorSetLayout
1796 : push_layout;
1797 auto layout_obj = GetDescriptorSetLayout(device_data, effective_dsl);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001798
1799 // Create a WriteDescriptorSet struct for each template update entry
1800 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1801 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1802 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1803 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1804
John Zulaufb6d71202017-12-22 16:47:09 -07001805 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001806 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1807 desc_writes.emplace_back();
1808 auto &write_entry = desc_writes.back();
1809
1810 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1811 char *update_entry = (char *)(pData) + offset;
1812
1813 if (dst_array_element >= binding_count) {
1814 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001815 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001816 }
1817
1818 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1819 write_entry.pNext = NULL;
1820 write_entry.dstSet = descriptorSet;
1821 write_entry.dstBinding = binding_being_updated;
1822 write_entry.dstArrayElement = dst_array_element;
1823 write_entry.descriptorCount = 1;
1824 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1825
1826 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1827 case VK_DESCRIPTOR_TYPE_SAMPLER:
1828 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1829 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1830 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1831 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1832 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1833 break;
1834
1835 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1836 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1837 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1838 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1839 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1840 break;
1841
1842 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1843 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1844 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1845 break;
Dave Houlton142c4cb2018-10-17 15:04:41 -06001846 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1847 VkWriteDescriptorSetInlineUniformBlockEXT *inline_info = &inline_infos[i];
1848 inline_info->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT;
1849 inline_info->pNext = nullptr;
1850 inline_info->dataSize = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1851 inline_info->pData = update_entry;
1852 write_entry.pNext = inline_info;
1853 // skip the rest of the array, they just represent bytes in the update
1854 j = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1855 break;
1856 }
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001857 default:
1858 assert(0);
1859 break;
1860 }
1861 dst_array_element++;
1862 }
1863 }
John Zulaufb845eb22018-10-12 11:41:06 -06001864}
John Zulaufb45fdc32018-10-12 15:14:17 -06001865// These helper functions carry out the validate and record descriptor updates peformed via update templates. They decode
1866// the templatized data and leverage the non-template UpdateDescriptor helper functions.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001867bool CoreChecks::ValidateUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
1868 const void *pData) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001869 // Translate the templated update into a normal update for validation...
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001870 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
1871 return ValidateUpdateDescriptorSets(static_cast<uint32_t>(decoded_update.desc_writes.size()), decoded_update.desc_writes.data(),
1872 0, NULL, "vkUpdateDescriptorSetWithTemplate()");
John Zulaufb45fdc32018-10-12 15:14:17 -06001873}
John Zulaufb845eb22018-10-12 11:41:06 -06001874
Mark Lobodzinski254c8512019-03-09 12:21:15 -07001875void CoreChecks::PerformUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
1876 const void *pData) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001877 // Translate the templated update into a normal update for validation...
Mark Lobodzinski254c8512019-03-09 12:21:15 -07001878 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
1879 cvdescriptorset::PerformUpdateDescriptorSets(this, static_cast<uint32_t>(decoded_update.desc_writes.size()),
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001880 decoded_update.desc_writes.data(), 0, NULL);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001881}
John Zulauf4e7bcb52018-11-02 10:46:30 -06001882
1883std::string cvdescriptorset::DescriptorSet::StringifySetAndLayout() const {
1884 std::string out;
1885 uint64_t layout_handle = HandleToUint64(p_layout_->GetDescriptorSetLayout());
1886 if (IsPushDescriptor()) {
1887 string_sprintf(&out, "Push Descriptors defined with VkDescriptorSetLayout 0x%" PRIxLEAST64, layout_handle);
John Zulauf4e7bcb52018-11-02 10:46:30 -06001888 } else {
Bob Ellisonaf2f8532019-03-12 11:05:47 -06001889 string_sprintf(&out, "VkDescriptorSet 0x%" PRIxLEAST64 " allocated with VkDescriptorSetLayout 0x%" PRIxLEAST64,
John Zulauf4e7bcb52018-11-02 10:46:30 -06001890 HandleToUint64(set_), layout_handle);
1891 }
1892 return out;
1893};
1894
John Zulauf1d27e0a2018-11-05 10:12:48 -07001895// Loop through the write updates to validate for a push descriptor set, ignoring dstSet
1896bool cvdescriptorset::DescriptorSet::ValidatePushDescriptorsUpdate(const debug_report_data *report_data, uint32_t write_count,
1897 const VkWriteDescriptorSet *p_wds, const char *func_name) {
1898 assert(IsPushDescriptor());
1899 bool skip = false;
1900 for (uint32_t i = 0; i < write_count; i++) {
1901 std::string error_code;
1902 std::string error_str;
1903 if (!ValidateWriteUpdate(report_data, &p_wds[i], func_name, &error_code, &error_str)) {
1904 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1905 HandleToUint64(p_layout_->GetDescriptorSetLayout()), error_code, "%s failed update validation: %s.",
1906 func_name, error_str.c_str());
1907 }
1908 }
1909 return skip;
1910}
1911
Tobin Ehlis300888c2016-05-18 13:43:26 -06001912// Validate the state for a given write update but don't actually perform the update
1913// If an error would occur for this update, return false and fill in details in error_msg string
1914bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -06001915 const char *func_name, std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001916 // Verify dst layout still valid
1917 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001918 *error_code = "VUID-VkWriteDescriptorSet-dstSet-00320";
John Zulauf4e7bcb52018-11-02 10:46:30 -06001919 string_sprintf(error_msg, "Cannot call %s to perform write update on %s which has been destroyed", func_name,
1920 StringifySetAndLayout().c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001921 return false;
1922 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001923 // Verify dst binding exists
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001924 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001925 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00315";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001926 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001927 error_str << StringifySetAndLayout() << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001928 *error_msg = error_str.str();
1929 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001930 } else {
1931 // Make sure binding isn't empty
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001932 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001933 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00316";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001934 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001935 error_str << StringifySetAndLayout() << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001936 *error_msg = error_str.str();
1937 return false;
1938 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001939 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001940 // Verify idle ds
1941 if (in_use.load() &&
1942 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1943 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1944 // TODO : Re-using Free Idle error code, need write update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001945 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001946 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001947 error_str << "Cannot call " << func_name << " to perform write update on " << StringifySetAndLayout()
Jeff Bolzfdf96072018-04-10 14:32:18 -05001948 << " that is in use by a command buffer";
1949 *error_msg = error_str.str();
1950 return false;
1951 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001952 // We know that binding is valid, verify update and do update on each descriptor
John Zulaufc483f442017-12-15 14:02:06 -07001953 auto start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001954 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001955 if (type != update->descriptorType) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001956 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00319";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001957 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001958 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with type "
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001959 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1960 *error_msg = error_str.str();
1961 return false;
1962 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001963 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001964 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001965 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001966 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001967 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001968 << " descriptors in that binding and all successive bindings of the set, but update of "
1969 << update->descriptorCount << " descriptors combined with update array element offset of "
1970 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001971 *error_msg = error_str.str();
1972 return false;
1973 }
Jeff Bolze54ae892018-09-08 12:16:29 -05001974 if (type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1975 if ((update->dstArrayElement % 4) != 0) {
1976 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02219";
1977 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001978 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001979 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1980 *error_msg = error_str.str();
1981 return false;
1982 }
1983 if ((update->descriptorCount % 4) != 0) {
1984 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02220";
1985 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001986 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001987 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1988 *error_msg = error_str.str();
1989 return false;
1990 }
1991 const auto *write_inline_info = lvl_find_in_chain<VkWriteDescriptorSetInlineUniformBlockEXT>(update->pNext);
1992 if (!write_inline_info || write_inline_info->dataSize != update->descriptorCount) {
1993 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02221";
1994 std::stringstream error_str;
1995 if (!write_inline_info) {
John Zulauf4e7bcb52018-11-02 10:46:30 -06001996 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
1997 << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001998 << "VkWriteDescriptorSetInlineUniformBlockEXT missing";
1999 } else {
John Zulauf4e7bcb52018-11-02 10:46:30 -06002000 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
2001 << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06002002 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2003 << " not equal to "
Jeff Bolze54ae892018-09-08 12:16:29 -05002004 << "VkWriteDescriptorSet descriptorCount " << update->descriptorCount;
2005 }
2006 *error_msg = error_str.str();
2007 return false;
2008 }
2009 // This error is probably unreachable due to the previous two errors
2010 if (write_inline_info && (write_inline_info->dataSize % 4) != 0) {
2011 *error_code = "VUID-VkWriteDescriptorSetInlineUniformBlockEXT-dataSize-02222";
2012 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002013 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06002014 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2015 << " not a multiple of 4";
Jeff Bolze54ae892018-09-08 12:16:29 -05002016 *error_msg = error_str.str();
2017 return false;
2018 }
2019 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002020 // Verify consecutive bindings match (if needed)
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06002021 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002022 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07002023 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Dave Houlton00c154e2018-05-24 13:20:50 -06002024 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002025 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002026 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002027 // Update is within bounds and consistent so last step is to validate update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06002028 if (!VerifyWriteUpdateContents(update, start_idx, func_name, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002029 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002030 error_str << "Write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002031 << " failed with error message: " << error_msg->c_str();
2032 *error_msg = error_str.str();
2033 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002034 }
2035 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002036 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06002037}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002038// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002039// 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 -07002040bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06002041 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002042 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06002043 auto usage = buffer_node->createInfo.usage;
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002044 const char *error_usage_bit = nullptr;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002045 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002046 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2047 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002048 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00334";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002049 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
2050 }
2051 break;
2052 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2053 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002054 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00335";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002055 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
2056 }
2057 break;
2058 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2059 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2060 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002061 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00330";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002062 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
2063 }
2064 break;
2065 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2066 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
2067 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002068 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00331";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002069 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
2070 }
2071 break;
2072 default:
2073 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002074 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002075 if (error_usage_bit) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002076 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002077 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
2078 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
2079 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002080 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002081 return false;
2082 }
2083 return true;
2084}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002085// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
2086// 1. buffer is valid
2087// 2. buffer was created with correct usage flags
2088// 3. offset is less than buffer size
2089// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002090// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002091// 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 -06002092bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
John Zulaufb45fdc32018-10-12 15:14:17 -06002093 const char *func_name, std::string *error_code,
2094 std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002095 // First make sure that buffer is valid
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002096 auto buffer_node = device_data_->GetBufferState(buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07002097 // Any invalid buffer should already be caught by object_tracker
2098 assert(buffer_node);
Mark Lobodzinski0ebe9712019-03-07 13:39:07 -07002099 if (device_data_->ValidateMemoryIsBoundToBuffer(buffer_node, func_name, "VUID-VkWriteDescriptorSet-descriptorType-00329")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002100 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00329";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002101 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06002102 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06002103 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002104 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002105 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
2106 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002107 return false;
2108 }
2109 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002110 if (buffer_info->offset >= buffer_node->createInfo.size) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002111 *error_code = "VUID-VkDescriptorBufferInfo-offset-00340";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002112 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002113 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
2114 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002115 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002116 return false;
2117 }
2118 if (buffer_info->range != VK_WHOLE_SIZE) {
2119 // Range must be VK_WHOLE_SIZE or > 0
2120 if (!buffer_info->range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002121 *error_code = "VUID-VkDescriptorBufferInfo-range-00341";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002122 std::stringstream error_str;
2123 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002124 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002125 return false;
2126 }
2127 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
2128 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002129 *error_code = "VUID-VkDescriptorBufferInfo-range-00342";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002130 std::stringstream error_str;
2131 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
2132 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002133 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002134 return false;
2135 }
2136 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002137 // Check buffer update sizes against device limits
2138 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
2139 auto max_ub_range = limits_.maxUniformBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002140 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002141 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002142 std::stringstream error_str;
2143 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2144 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
2145 *error_msg = error_str.str();
2146 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002147 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_ub_range) {
2148 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
2149 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002150 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2151 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002152 << "maxUniformBufferRange (" << max_ub_range << ")";
2153 *error_msg = error_str.str();
2154 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002155 }
2156 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
2157 auto max_sb_range = limits_.maxStorageBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002158 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002159 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002160 std::stringstream error_str;
2161 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2162 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
2163 *error_msg = error_str.str();
2164 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002165 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_sb_range) {
2166 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
2167 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002168 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2169 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002170 << "maxStorageBufferRange (" << max_sb_range << ")";
2171 *error_msg = error_str.str();
2172 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002173 }
2174 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002175 return true;
2176}
2177
Tobin Ehlis300888c2016-05-18 13:43:26 -06002178// Verify that the contents of the update are ok, but don't perform actual update
2179bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
John Zulaufb45fdc32018-10-12 15:14:17 -06002180 const char *func_name, std::string *error_code,
2181 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002182 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002183 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
2184 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2185 // Validate image
2186 auto image_view = update->pImageInfo[di].imageView;
2187 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002188 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2189 error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002190 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002191 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
2192 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002193 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002194 return false;
2195 }
Mark Lobodzinski60e79032019-03-07 10:22:31 -07002196 if (device_data_->GetDeviceExtensions()->vk_khr_sampler_ycbcr_conversion) {
Tony-LunarG352f08f2019-01-03 10:11:24 -07002197 ImageSamplerDescriptor *desc = (ImageSamplerDescriptor *)descriptors_[index].get();
2198 if (desc->IsImmutableSampler()) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07002199 auto sampler_state = device_data_->GetSamplerState(desc->GetSampler());
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07002200 auto iv_state = device_data_->GetImageViewState(image_view);
Tony-LunarG352f08f2019-01-03 10:11:24 -07002201 if (iv_state && sampler_state) {
2202 if (iv_state->samplerConversion != sampler_state->samplerConversion) {
2203 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01948";
2204 std::stringstream error_str;
Tony-LunarG8a51f0a2019-01-04 16:14:14 -07002205 error_str << "Attempted write update to combined image sampler and image view and sampler ycbcr "
2206 "conversions are not identical, sampler: "
Tony-LunarG352f08f2019-01-03 10:11:24 -07002207 << desc->GetSampler() << " image view: " << iv_state->image_view << ".";
2208 *error_msg = error_str.str();
2209 return false;
2210 }
2211 }
2212 }
2213 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002214 }
2215 }
Tobin Ehlis648b1cf2018-04-13 15:24:13 -06002216 // fall through
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002217 case VK_DESCRIPTOR_TYPE_SAMPLER: {
2218 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2219 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
2220 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002221 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002222 std::stringstream error_str;
2223 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
2224 << update->pImageInfo[di].sampler << ".";
2225 *error_msg = error_str.str();
2226 return false;
2227 }
2228 } else {
2229 // TODO : Warn here
2230 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002231 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002232 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002233 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002234 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2235 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2236 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2237 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2238 auto image_view = update->pImageInfo[di].imageView;
2239 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002240 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2241 error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002242 std::stringstream error_str;
2243 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
2244 *error_msg = error_str.str();
2245 return false;
2246 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002247 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002248 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002249 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002250 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2251 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
2252 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2253 auto buffer_view = update->pTexelBufferView[di];
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07002254 auto bv_state = device_data_->GetBufferViewState(buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002255 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002256 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002257 std::stringstream error_str;
2258 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2259 *error_msg = error_str.str();
2260 return false;
2261 }
2262 auto buffer = bv_state->create_info.buffer;
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002263 auto buffer_state = device_data_->GetBufferState(buffer);
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002264 // Verify that buffer underlying the view hasn't been destroyed prematurely
2265 if (!buffer_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002266 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002267 std::stringstream error_str;
2268 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
2269 << ") has been destroyed: " << error_msg->c_str();
2270 *error_msg = error_str.str();
2271 return false;
2272 } else if (!ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002273 std::stringstream error_str;
2274 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
2275 *error_msg = error_str.str();
2276 return false;
2277 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002278 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002279 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002280 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002281 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2282 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2283 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2284 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2285 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufb45fdc32018-10-12 15:14:17 -06002286 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, func_name, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002287 std::stringstream error_str;
2288 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
2289 *error_msg = error_str.str();
2290 return false;
2291 }
2292 }
2293 break;
2294 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002295 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
2296 break;
Eric Werness30127fd2018-10-31 21:01:03 -07002297 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002298 // XXX TODO
2299 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002300 default:
2301 assert(0); // We've already verified update type so should never get here
2302 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002303 }
2304 // All checks passed so update contents are good
2305 return true;
2306}
2307// Verify that the contents of the update are ok, but don't perform actual update
2308bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
John Zulaufb45fdc32018-10-12 15:14:17 -06002309 VkDescriptorType type, uint32_t index, const char *func_name,
2310 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002311 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
2312 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06002313 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002314 case PlainSampler: {
2315 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002316 const auto src_desc = src_set->descriptors_[index + di].get();
2317 if (!src_desc->updated) continue;
2318 if (!src_desc->IsImmutableSampler()) {
2319 auto update_sampler = static_cast<SamplerDescriptor *>(src_desc)->GetSampler();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002320 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002321 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002322 std::stringstream error_str;
2323 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2324 *error_msg = error_str.str();
2325 return false;
2326 }
2327 } else {
2328 // TODO : Warn here
2329 }
2330 }
2331 break;
2332 }
2333 case ImageSampler: {
2334 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002335 const auto src_desc = src_set->descriptors_[index + di].get();
2336 if (!src_desc->updated) continue;
2337 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002338 // First validate sampler
2339 if (!img_samp_desc->IsImmutableSampler()) {
2340 auto update_sampler = img_samp_desc->GetSampler();
2341 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002342 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002343 std::stringstream error_str;
2344 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2345 *error_msg = error_str.str();
2346 return false;
2347 }
2348 } else {
2349 // TODO : Warn here
2350 }
2351 // Validate image
2352 auto image_view = img_samp_desc->GetImageView();
2353 auto image_layout = img_samp_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002354 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002355 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002356 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002357 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002358 return false;
2359 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002360 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002361 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002362 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002363 case Image: {
2364 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002365 const auto src_desc = src_set->descriptors_[index + di].get();
2366 if (!src_desc->updated) continue;
2367 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002368 auto image_view = img_desc->GetImageView();
2369 auto image_layout = img_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002370 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002371 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002372 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002373 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002374 return false;
2375 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002376 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002377 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002378 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002379 case TexelBuffer: {
2380 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002381 const auto src_desc = src_set->descriptors_[index + di].get();
2382 if (!src_desc->updated) continue;
2383 auto buffer_view = static_cast<TexelDescriptor *>(src_desc)->GetBufferView();
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07002384 auto bv_state = device_data_->GetBufferViewState(buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002385 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002386 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002387 std::stringstream error_str;
2388 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2389 *error_msg = error_str.str();
2390 return false;
2391 }
2392 auto buffer = bv_state->create_info.buffer;
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002393 if (!ValidateBufferUsage(device_data_->GetBufferState(buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002394 std::stringstream error_str;
2395 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
2396 *error_msg = error_str.str();
2397 return false;
2398 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002399 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002400 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002401 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002402 case GeneralBuffer: {
2403 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002404 const auto src_desc = src_set->descriptors_[index + di].get();
2405 if (!src_desc->updated) continue;
2406 auto buffer = static_cast<BufferDescriptor *>(src_desc)->GetBuffer();
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002407 if (!ValidateBufferUsage(device_data_->GetBufferState(buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002408 std::stringstream error_str;
2409 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
2410 *error_msg = error_str.str();
2411 return false;
2412 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06002413 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002414 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002415 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002416 case InlineUniform:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002417 case AccelerationStructure:
Jeff Bolze54ae892018-09-08 12:16:29 -05002418 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002419 default:
2420 assert(0); // We've already verified update type so should never get here
2421 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002422 }
2423 // All checks passed so update contents are good
2424 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12002425}
Tobin Ehlisf320b192017-03-14 11:22:50 -06002426// Update the common AllocateDescriptorSetsData
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002427void CoreChecks::UpdateAllocateDescriptorSetsData(const VkDescriptorSetAllocateInfo *p_alloc_info,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002428 cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisf320b192017-03-14 11:22:50 -06002429 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002430 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
Tobin Ehlisf320b192017-03-14 11:22:50 -06002431 if (layout) {
2432 ds_data->layout_nodes[i] = layout;
2433 // Count total descriptors required per type
2434 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
2435 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
2436 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
2437 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
2438 }
2439 }
2440 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
2441 }
Petr Kraus13c98a62017-12-09 00:22:39 +01002442}
Tobin Ehlisee471462016-05-26 11:21:59 -06002443// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002444bool CoreChecks::ValidateAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002445 const cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002446 bool skip = false;
Mark Lobodzinski7804bd42019-03-06 11:28:48 -07002447 auto pool_state = GetDescriptorPoolState(p_alloc_info->descriptorPool);
Tobin Ehlisee471462016-05-26 11:21:59 -06002448
2449 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002450 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07002451 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
John Zulauf1d27e0a2018-11-05 10:12:48 -07002452 if (layout->IsPushDescriptor()) {
John Zulauf5562d062018-01-24 11:54:05 -07002453 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 -06002454 HandleToUint64(p_alloc_info->pSetLayouts[i]), "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-00308",
Lockeca0d9792019-03-03 23:48:13 -07002455 "Layout %s specified at pSetLayouts[%" PRIu32
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002456 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
Lockeca0d9792019-03-03 23:48:13 -07002457 report_data->FormatHandle(p_alloc_info->pSetLayouts[i]).c_str(), i,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002458 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07002459 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002460 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT &&
2461 !(pool_state->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
2462 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 -06002463 0, "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-03044",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002464 "Descriptor set layout create flags and pool create flags mismatch for index (%d)", i);
2465 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002466 }
2467 }
Mark Lobodzinski60e79032019-03-07 10:22:31 -07002468 if (!GetDeviceExtensions()->vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002469 // Track number of descriptorSets allowable in this pool
2470 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002471 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 -06002472 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorSetCount-00306",
Lockeca0d9792019-03-03 23:48:13 -07002473 "Unable to allocate %u descriptorSets from pool %s"
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002474 ". This pool only has %d descriptorSets remaining.",
Lockeca0d9792019-03-03 23:48:13 -07002475 p_alloc_info->descriptorSetCount, report_data->FormatHandle(pool_state->pool).c_str(),
2476 pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002477 }
2478 // Determine whether descriptor counts are satisfiable
Jeff Bolze54ae892018-09-08 12:16:29 -05002479 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2480 if (ds_data->required_descriptors_by_type.at(it->first) > pool_state->availableDescriptorTypeCount[it->first]) {
Lockeca0d9792019-03-03 23:48:13 -07002481 skip |= log_msg(
2482 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
2483 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307",
2484 "Unable to allocate %u descriptors of type %s from pool %s"
2485 ". This pool only has %d descriptors of this type remaining.",
2486 ds_data->required_descriptors_by_type.at(it->first), string_VkDescriptorType(VkDescriptorType(it->first)),
2487 report_data->FormatHandle(pool_state->pool).c_str(), pool_state->availableDescriptorTypeCount[it->first]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002488 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002489 }
2490 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06002491
Jeff Bolzfdf96072018-04-10 14:32:18 -05002492 const auto *count_allocate_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2493
2494 if (count_allocate_info) {
2495 if (count_allocate_info->descriptorSetCount != 0 &&
2496 count_allocate_info->descriptorSetCount != p_alloc_info->descriptorSetCount) {
2497 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 -06002498 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-descriptorSetCount-03045",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002499 "VkDescriptorSetAllocateInfo::descriptorSetCount (%d) != "
2500 "VkDescriptorSetVariableDescriptorCountAllocateInfoEXT::descriptorSetCount (%d)",
2501 p_alloc_info->descriptorSetCount, count_allocate_info->descriptorSetCount);
2502 }
2503 if (count_allocate_info->descriptorSetCount == p_alloc_info->descriptorSetCount) {
2504 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002505 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002506 if (count_allocate_info->pDescriptorCounts[i] > layout->GetDescriptorCountFromBinding(layout->GetMaxBinding())) {
2507 skip |= log_msg(
2508 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 -06002509 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-pSetLayouts-03046",
2510 "pDescriptorCounts[%d] = (%d), binding's descriptorCount = (%d)", i,
Jeff Bolzfdf96072018-04-10 14:32:18 -05002511 count_allocate_info->pDescriptorCounts[i], layout->GetDescriptorCountFromBinding(layout->GetMaxBinding()));
2512 }
2513 }
2514 }
2515 }
2516
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002517 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06002518}
2519// Decrement allocated sets from the pool and insert new sets into set_map
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002520void CoreChecks::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
2521 const VkDescriptorSet *descriptor_sets,
2522 const cvdescriptorset::AllocateDescriptorSetsData *ds_data,
2523 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Mark Lobodzinski254c8512019-03-09 12:21:15 -07002524 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map) {
Tobin Ehlisee471462016-05-26 11:21:59 -06002525 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002526 // Account for sets and individual descriptors allocated from pool
Tobin Ehlisee471462016-05-26 11:21:59 -06002527 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Jeff Bolze54ae892018-09-08 12:16:29 -05002528 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2529 pool_state->availableDescriptorTypeCount[it->first] -= ds_data->required_descriptors_by_type.at(it->first);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06002530 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002531
2532 const auto *variable_count_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2533 bool variable_count_valid = variable_count_info && variable_count_info->descriptorSetCount == p_alloc_info->descriptorSetCount;
2534
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002535 // Create tracking object for each descriptor set; insert into global map and the pool's set.
Tobin Ehlisee471462016-05-26 11:21:59 -06002536 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -05002537 uint32_t variable_count = variable_count_valid ? variable_count_info->pDescriptorCounts[i] : 0;
2538
Tobin Ehlis93f22372016-10-12 14:34:12 -06002539 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 -07002540 variable_count, this);
Tobin Ehlisee471462016-05-26 11:21:59 -06002541
2542 pool_state->sets.insert(new_ds);
2543 new_ds->in_use.store(0);
2544 (*set_map)[descriptor_sets[i]] = new_ds;
2545 }
2546}
John Zulauf48a6a702017-12-22 17:14:54 -07002547
2548cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2549 GLOBAL_CB_NODE *cb_state)
2550 : filtered_map_(), orig_map_(in_map) {
2551 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2552 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2553 ds.FilterAndTrackBindingReqs(cb_state, orig_map_, filtered_map_.get());
2554 }
2555}
2556cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2557 GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline)
2558 : filtered_map_(), orig_map_(in_map) {
2559 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2560 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2561 ds.FilterAndTrackBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
2562 }
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01002563}