blob: b79e5a0eeb3e8cb0c00b8d54170420a4dfb7fe11 [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,
Mark Lobodzinski33a34b82019-04-25 11:38:36 -0600706 const std::vector<uint32_t> &dynamic_offsets, CMD_BUFFER_STATE *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 Lobodzinski0a41e0e2019-04-25 12:12:40 -0600754 if (!device_data_->GetDevMemState(mem_binding)) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700755 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600756 error_str << "Descriptor in binding #" << binding << " index " << index << " uses buffer " << buffer
757 << " that references invalid memory " << mem_binding << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700758 *error = error_str.str();
Tobin Ehlisc8266452017-04-07 12:20:30 -0600759 return false;
760 }
761 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700762 }
763 if (descriptors_[i]->IsDynamic()) {
764 // Validate that dynamic offsets are within the buffer
765 auto buffer_size = buffer_node->createInfo.size;
766 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
767 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
768 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
769 if (VK_WHOLE_SIZE == range) {
770 if ((dyn_offset + desc_offset) > buffer_size) {
771 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600772 error_str << "Dynamic descriptor in binding #" << binding << " index " << index << " uses buffer "
773 << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
774 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
775 << buffer_size << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700776 *error = error_str.str();
777 return false;
778 }
779 } else {
780 if ((dyn_offset + desc_offset + range) > buffer_size) {
781 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600782 error_str << "Dynamic descriptor in binding #" << binding << " index " << index << " uses buffer "
783 << buffer << " with dynamic offset " << dyn_offset << " combined with offset "
784 << desc_offset << " and range " << range << " that oversteps the buffer size of "
785 << buffer_size << ".";
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700786 *error = error_str.str();
787 return false;
788 }
789 }
790 }
791 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
792 VkImageView image_view;
793 VkImageLayout image_layout;
794 if (descriptor_class == ImageSampler) {
795 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
796 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
797 } else {
798 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
799 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
800 }
801 auto reqs = binding_pair.second;
802
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -0700803 auto image_view_state = device_data_->GetImageViewState(image_view);
Tobin Ehlis836a1372017-07-14 11:25:21 -0600804 if (nullptr == image_view_state) {
805 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
806 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
807 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600808 error_str << "Descriptor in binding #" << binding << " index " << index << " is using imageView "
809 << image_view << " that has been destroyed.";
Tobin Ehlis836a1372017-07-14 11:25:21 -0600810 *error = error_str.str();
811 return false;
812 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700813 auto image_view_ci = image_view_state->create_info;
814
815 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
816 // bad view type
817 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600818 error_str << "Descriptor in binding #" << binding << " index " << index
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600819 << " requires an image view of type " << StringDescriptorReqViewType(reqs) << " but got "
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700820 << string_VkImageViewType(image_view_ci.viewType) << ".";
821 *error = error_str.str();
822 return false;
823 }
824
Chris Forbesda01e8d2018-08-27 15:36:57 -0700825 auto format_bits = DescriptorRequirementsBitsFromFormat(image_view_ci.format);
826 if (!(reqs & format_bits)) {
827 // bad component type
828 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600829 error_str << "Descriptor in binding #" << binding << " index " << index << " requires "
Chris Forbesda01e8d2018-08-27 15:36:57 -0700830 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
831 << string_VkFormat(image_view_ci.format) << ".";
832 *error = error_str.str();
833 return false;
834 }
835
Mark Lobodzinski8ddc23f2019-03-06 11:48:49 -0700836 auto image_node = device_data_->GetImageState(image_view_ci.image);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700837 assert(image_node);
838 // Verify Image Layout
John Zulauff660ad62019-03-23 07:16:05 -0600839 // No "invalid layout" VUID required for this call, since the optimal_layout parameter is UNDEFINED.
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700840 bool hit_error = false;
John Zulauff660ad62019-03-23 07:16:05 -0600841 device_data_->VerifyImageLayout(cb_node, image_node, image_view_state->normalized_subresource_range,
John Zulaufabcc8292019-04-08 18:07:44 -0600842 image_view_ci.subresourceRange.aspectMask, image_layout,
843 VK_IMAGE_LAYOUT_UNDEFINED, caller, kVUIDUndefined,
John Zulauff660ad62019-03-23 07:16:05 -0600844 "VUID-VkDescriptorImageInfo-imageLayout-00344", &hit_error);
845 if (hit_error) {
846 *error =
847 "Image layout specified at vkUpdateDescriptorSet* or vkCmdPushDescriptorSet* time "
848 "doesn't match actual image layout at time descriptor is used. See previous error callback for "
849 "specific details.";
850 return false;
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700851 }
John Zulauff660ad62019-03-23 07:16:05 -0600852
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700853 // Verify Sample counts
854 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
855 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600856 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700857 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
858 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
859 *error = error_str.str();
860 return false;
861 }
862 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
863 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600864 error_str << "Descriptor in binding #" << binding << " index " << index
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700865 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
866 *error = error_str.str();
867 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200868 }
Chris Forbese92dd1d2019-01-21 15:58:57 -0800869 } else if (descriptor_class == TexelBuffer) {
870 auto texel_buffer = static_cast<TexelDescriptor *>(descriptors_[i].get());
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -0700871 auto buffer_view = device_data_->GetBufferViewState(texel_buffer->GetBufferView());
Chris Forbese92dd1d2019-01-21 15:58:57 -0800872
Locke2d9c3dd2019-04-08 16:29:09 -0600873 if (nullptr == buffer_view) {
874 std::stringstream error_str;
875 error_str << "Descriptor in binding #" << binding << " index " << index << " is using bufferView "
876 << buffer_view << " that has been destroyed.";
877 *error = error_str.str();
878 return false;
879 }
880 auto buffer = buffer_view->create_info.buffer;
881 auto buffer_state = device_data_->GetBufferState(buffer);
882 if (!buffer_state) {
883 std::stringstream error_str;
884 error_str << "Descriptor in binding #" << binding << " index " << index << " is using buffer "
885 << buffer_state << " that has been destroyed.";
886 *error = error_str.str();
887 return false;
888 }
Chris Forbese92dd1d2019-01-21 15:58:57 -0800889 auto reqs = binding_pair.second;
890 auto format_bits = DescriptorRequirementsBitsFromFormat(buffer_view->create_info.format);
891
892 if (!(reqs & format_bits)) {
893 // bad component type
894 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600895 error_str << "Descriptor in binding #" << binding << " index " << index << " requires "
Chris Forbese92dd1d2019-01-21 15:58:57 -0800896 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
897 << string_VkFormat(buffer_view->create_info.format) << ".";
898 *error = error_str.str();
899 return false;
900 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600901 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600902 if (descriptor_class == ImageSampler || descriptor_class == PlainSampler) {
903 // Verify Sampler still valid
904 VkSampler sampler;
905 if (descriptor_class == ImageSampler) {
906 sampler = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetSampler();
907 } else {
908 sampler = static_cast<SamplerDescriptor *>(descriptors_[i].get())->GetSampler();
909 }
910 if (!ValidateSampler(sampler, device_data_)) {
911 std::stringstream error_str;
Lockeb994adf2019-03-29 23:52:31 -0600912 error_str << "Descriptor in binding #" << binding << " index " << index << " is using sampler " << sampler
913 << " that has been destroyed.";
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600914 *error = error_str.str();
915 return false;
Lockea223c102019-04-05 00:38:24 -0600916 } else {
917 SAMPLER_STATE *sampler_state = device_data_->GetSamplerState(sampler);
918 if (sampler_state->samplerConversion && !descriptors_[i].get()->IsImmutableSampler()) {
919 std::stringstream error_str;
920 error_str << "sampler (" << sampler << ") in the descriptor set (" << set_
921 << ") contains a YCBCR conversion (" << sampler_state->samplerConversion
922 << ") , then the sampler MUST also exists as an immutable sampler.";
923 *error = error_str.str();
924 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600925 }
926 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600927 }
928 }
929 }
930 return true;
931}
Chris Forbes57989132016-07-26 17:06:10 +1200932
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600933// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600934uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600935 std::unordered_set<VkBuffer> *buffer_set,
936 std::unordered_set<VkImageView> *image_set) const {
937 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200938 for (auto binding_pair : bindings) {
939 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600940 // If a binding doesn't exist, skip it
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600941 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600942 continue;
943 }
John Zulaufc483f442017-12-15 14:02:06 -0700944 uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600945 if (descriptors_[start_idx]->IsStorage()) {
946 if (Image == 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 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600950 num_updates++;
951 }
952 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600953 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600954 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600955 if (descriptors_[start_idx + i]->updated) {
956 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -0700957 auto bv_state = device_data_->GetBufferViewState(bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600958 if (bv_state) {
959 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600960 num_updates++;
961 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600962 }
963 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600964 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600965 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600966 if (descriptors_[start_idx + i]->updated) {
967 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600968 num_updates++;
969 }
970 }
971 }
972 }
973 }
974 return num_updates;
975}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600976// Set is being deleted or updates so invalidate all bound cmd buffers
977void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Mark Lobodzinski9ddb7182019-03-08 17:31:09 -0700978 device_data_->InvalidateCommandBuffers(cb_bindings, {HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600979}
John Zulauf1d27e0a2018-11-05 10:12:48 -0700980
981// Loop through the write updates to do for a push descriptor set, ignoring dstSet
982void cvdescriptorset::DescriptorSet::PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds) {
983 assert(IsPushDescriptor());
984 for (uint32_t i = 0; i < write_count; i++) {
985 PerformWriteUpdate(&p_wds[i]);
986 }
987}
988
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600989// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600990void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700991 // Perform update on a per-binding basis as consecutive updates roll over to next binding
992 auto descriptors_remaining = update->descriptorCount;
993 auto binding_being_updated = update->dstBinding;
994 auto offset = update->dstArrayElement;
Tobin Ehlise16805c2017-08-09 09:10:37 -0600995 uint32_t update_index = 0;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700996 while (descriptors_remaining) {
997 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
John Zulaufc483f442017-12-15 14:02:06 -0700998 auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700999 // Loop over the updates for a single binding at a time
Tobin Ehlise16805c2017-08-09 09:10:37 -06001000 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
1001 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001002 }
1003 // Roll over to next binding in case of consecutive update
1004 descriptors_remaining -= update_count;
1005 offset = 0;
1006 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001007 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001008 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001009
Jeff Bolzfdf96072018-04-10 14:32:18 -05001010 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1011 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1012 InvalidateBoundCmdBuffers();
1013 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001014}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001015// Validate Copy update
1016bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -06001017 const DescriptorSet *src_set, const char *func_name,
1018 std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001019 // Verify dst layout still valid
1020 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001021 *error_code = "VUID-VkCopyDescriptorSet-dstSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -07001022 string_sprintf(error_msg,
Lockeca0d9792019-03-03 23:48:13 -07001023 "Cannot call %s to perform copy update on descriptor set dstSet %s"
1024 " created with destroyed VkDescriptorSetLayout %s.",
1025 func_name, report_data->FormatHandle(set_).c_str(),
1026 report_data->FormatHandle(p_layout_->GetDescriptorSetLayout()).c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001027 return false;
1028 }
1029
1030 // Verify src layout still valid
1031 if (src_set->p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001032 *error_code = "VUID-VkCopyDescriptorSet-srcSet-parameter";
John Zulaufb45fdc32018-10-12 15:14:17 -06001033 string_sprintf(error_msg,
Lockeca0d9792019-03-03 23:48:13 -07001034 "Cannot call %s to perform copy update of dstSet %s"
1035 " from descriptor set srcSet %s"
1036 " created with destroyed VkDescriptorSetLayout %s.",
1037 func_name, report_data->FormatHandle(set_).c_str(), report_data->FormatHandle(src_set->set_).c_str(),
1038 report_data->FormatHandle(src_set->p_layout_->GetDescriptorSetLayout()).c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001039 return false;
1040 }
1041
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001042 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001043 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-00347";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001044 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001045 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001046 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001047 return false;
1048 }
1049 if (!src_set->HasBinding(update->srcBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001050 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-00345";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001051 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001052 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001053 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001054 return false;
1055 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001056 // Verify idle ds
1057 if (in_use.load() &&
1058 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1059 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1060 // TODO : Re-using Free Idle error code, need copy update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001061 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001062 std::stringstream error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001063 error_str << "Cannot call " << func_name << " to perform copy update on descriptor set " << set_
Jeff Bolzfdf96072018-04-10 14:32:18 -05001064 << " that is in use by a command buffer";
1065 *error_msg = error_str.str();
1066 return false;
1067 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001068 // src & dst set bindings are valid
1069 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -07001070 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001071 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
1072 // SRC update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001073 *error_code = "VUID-VkCopyDescriptorSet-srcArrayElement-00346";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001074 std::stringstream error_str;
1075 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
John Zulaufc483f442017-12-15 14:02:06 -07001076 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001077 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001078 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001079 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001080 return false;
1081 }
John Zulaufc483f442017-12-15 14:02:06 -07001082 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001083 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001084 // DST update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001085 *error_code = "VUID-VkCopyDescriptorSet-dstArrayElement-00348";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001086 std::stringstream error_str;
1087 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
John Zulaufc483f442017-12-15 14:02:06 -07001088 << " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001089 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001090 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001091 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001092 return false;
1093 }
1094 // Check that types match
Dave Houltond8ed0212018-05-16 17:18:24 -06001095 // TODO : Base default error case going from here is "VUID-VkAcquireNextImageInfoKHR-semaphore-parameter"2ba which covers all
1096 // consistency issues, need more fine-grained error codes
Dave Houlton00c154e2018-05-24 13:20:50 -06001097 *error_code = "VUID-VkCopyDescriptorSet-srcSet-00349";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001098 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001099 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001100 if (src_type != dst_type) {
1101 std::stringstream error_str;
1102 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
1103 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001104 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001105 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001106 return false;
1107 }
1108 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -06001109 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001110 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001111 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001112 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001113 return false;
1114 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001115
1116 if ((src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1117 !(GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001118 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01918";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001119 std::stringstream error_str;
1120 error_str << "If pname:srcSet's (" << update->srcSet
1121 << ") layout was created with the "
1122 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1123 "set, then pname:dstSet's ("
1124 << update->dstSet
1125 << ") layout must: also have been created with the "
1126 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1127 *error_msg = error_str.str();
1128 return false;
1129 }
1130
1131 if (!(src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1132 (GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001133 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01919";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001134 std::stringstream error_str;
1135 error_str << "If pname:srcSet's (" << update->srcSet
1136 << ") layout was created without the "
1137 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1138 "set, then pname:dstSet's ("
1139 << update->dstSet
1140 << ") layout must: also have been created without the "
1141 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1142 *error_msg = error_str.str();
1143 return false;
1144 }
1145
1146 if ((src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1147 !(GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001148 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01920";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001149 std::stringstream error_str;
1150 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1151 << ") was allocated was created "
1152 "with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1153 "set, then the descriptor pool from which pname:dstSet ("
1154 << update->dstSet
1155 << ") was allocated must: "
1156 "also have been created with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1157 *error_msg = error_str.str();
1158 return false;
1159 }
1160
1161 if (!(src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1162 (GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001163 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01921";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001164 std::stringstream error_str;
1165 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1166 << ") was allocated was created "
1167 "without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1168 "set, then the descriptor pool from which pname:dstSet ("
1169 << update->dstSet
1170 << ") was allocated must: "
1171 "also have been created without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1172 *error_msg = error_str.str();
1173 return false;
1174 }
1175
Jeff Bolze54ae892018-09-08 12:16:29 -05001176 if (src_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1177 if ((update->srcArrayElement % 4) != 0) {
1178 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02223";
1179 std::stringstream error_str;
1180 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1181 << "srcArrayElement " << update->srcArrayElement << " not a multiple of 4";
1182 *error_msg = error_str.str();
1183 return false;
1184 }
1185 if ((update->dstArrayElement % 4) != 0) {
1186 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02224";
1187 std::stringstream error_str;
1188 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1189 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1190 *error_msg = error_str.str();
1191 return false;
1192 }
1193 if ((update->descriptorCount % 4) != 0) {
1194 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02225";
1195 std::stringstream error_str;
1196 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1197 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1198 *error_msg = error_str.str();
1199 return false;
1200 }
1201 }
1202
Tobin Ehlisd41e7b62016-05-19 07:56:18 -06001203 // Update parameters all look good and descriptor updated so verify update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06001204 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 -06001205
1206 // All checks passed so update is good
1207 return true;
1208}
1209// Perform Copy update
1210void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -07001211 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
1212 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001213 // Update parameters all look good so perform update
1214 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001215 auto src = src_set->descriptors_[src_start_idx + di].get();
1216 auto dst = descriptors_[dst_start_idx + di].get();
1217 if (src->updated) {
1218 dst->CopyUpdate(src);
1219 some_update_ = true;
1220 } else {
1221 dst->updated = false;
1222 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001223 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001224
Jeff Bolzfdf96072018-04-10 14:32:18 -05001225 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1226 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1227 InvalidateBoundCmdBuffers();
1228 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001229}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001230
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001231// Update the drawing state for the affected descriptors.
1232// Set cb_node to this set and this set to cb_node.
1233// Add the bindings of the descriptor
1234// Set the layout based on the current descriptor layout (will mask subsequent layer mismatch errors)
1235// TODO: Modify the UpdateDrawState virtural functions to *only* set initial layout and not change layouts
Tobin Ehlisf9519102016-08-17 09:49:13 -06001236// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
1237// to be used in a draw by the given cb_node
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001238void cvdescriptorset::DescriptorSet::UpdateDrawState(CoreChecks *device_data, CMD_BUFFER_STATE *cb_node,
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001239 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001240 // bind cb to this descriptor set
1241 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001242 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Petr Krausbc7f5442017-05-14 23:43:38 +02001243 cb_node->object_bindings.insert({HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001244 pool_state_->cb_bindings.insert(cb_node);
Petr Krausbc7f5442017-05-14 23:43:38 +02001245 cb_node->object_bindings.insert({HandleToUint64(pool_state_->pool), kVulkanObjectTypeDescriptorPool});
Tobin Ehlisf9519102016-08-17 09:49:13 -06001246 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
1247 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -07001248 for (auto binding_req_pair : binding_req_map) {
1249 auto binding = binding_req_pair.first;
Tony-LunarG62c5dba2018-12-20 14:27:23 -07001250 // We aren't validating descriptors created with PARTIALLY_BOUND or UPDATE_AFTER_BIND, so don't record state
1251 if (p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
1252 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) {
1253 continue;
1254 }
John Zulaufc483f442017-12-15 14:02:06 -07001255 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
1256 for (uint32_t i = range.start; i < range.end; ++i) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001257 descriptors_[i]->UpdateDrawState(device_data, cb_node);
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001258 }
1259 }
1260}
1261
John Zulauf48a6a702017-12-22 17:14:54 -07001262void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1263 const BindingReqMap &in_req, BindingReqMap *out_req,
1264 TrackedBindings *bindings) {
1265 assert(out_req);
1266 assert(bindings);
1267 const auto binding = binding_req_pair.first;
1268 // Use insert and look at the boolean ("was inserted") in the returned pair to see if this is a new set member.
1269 // Saves one hash lookup vs. find ... compare w/ end ... insert.
1270 const auto it_bool_pair = bindings->insert(binding);
1271 if (it_bool_pair.second) {
1272 out_req->emplace(binding_req_pair);
1273 }
1274}
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001275
John Zulauf48a6a702017-12-22 17:14:54 -07001276void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1277 const BindingReqMap &in_req, BindingReqMap *out_req,
1278 TrackedBindings *bindings, uint32_t limit) {
1279 if (bindings->size() < limit) FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, bindings);
1280}
1281
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001282void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(CMD_BUFFER_STATE *cb_state, const BindingReqMap &in_req,
John Zulauf48a6a702017-12-22 17:14:54 -07001283 BindingReqMap *out_req) {
1284 TrackedBindings &bound = cached_validation_[cb_state].command_binding_and_usage;
1285 if (bound.size() == GetBindingCount()) {
1286 return; // All bindings are bound, out req is empty
1287 }
1288 for (const auto &binding_req_pair : in_req) {
1289 const auto binding = binding_req_pair.first;
1290 // If a binding doesn't exist, or has already been bound, skip it
1291 if (p_layout_->HasBinding(binding)) {
1292 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, &bound);
1293 }
1294 }
1295}
1296
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001297void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(CMD_BUFFER_STATE *cb_state, PIPELINE_STATE *pipeline,
John Zulauf48a6a702017-12-22 17:14:54 -07001298 const BindingReqMap &in_req, BindingReqMap *out_req) {
1299 auto &validated = cached_validation_[cb_state];
1300 auto &image_sample_val = validated.image_samplers[pipeline];
1301 auto *const dynamic_buffers = &validated.dynamic_buffers;
1302 auto *const non_dynamic_buffers = &validated.non_dynamic_buffers;
1303 const auto &stats = p_layout_->GetBindingTypeStats();
1304 for (const auto &binding_req_pair : in_req) {
1305 auto binding = binding_req_pair.first;
1306 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
1307 if (!layout_binding) {
1308 continue;
1309 }
1310 // Caching criteria differs per type.
1311 // If image_layout have changed , the image descriptors need to be validated against them.
1312 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1313 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
1314 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, dynamic_buffers, stats.dynamic_buffer_count);
1315 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1316 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
1317 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
1318 } else {
1319 // This is rather crude, as the changed layouts may not impact the bound descriptors,
1320 // but the simple "versioning" is a simple "dirt" test.
1321 auto &version = image_sample_val[binding]; // Take advantage of default construtor zero initialzing new entries
1322 if (version != cb_state->image_layout_change_count) {
1323 version = cb_state->image_layout_change_count;
1324 out_req->emplace(binding_req_pair);
1325 }
1326 }
1327 }
1328}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001329
Tobin Ehlis300888c2016-05-18 13:43:26 -06001330cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001331 updated = false;
1332 descriptor_class = PlainSampler;
1333 if (immut) {
1334 sampler_ = *immut;
1335 immutable_ = true;
1336 updated = true;
1337 }
1338}
Tobin Ehlise2f80292016-06-02 10:08:53 -06001339// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001340bool cvdescriptorset::ValidateSampler(const VkSampler sampler, CoreChecks *dev_data) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001341 return (dev_data->GetSamplerState(sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001342}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001343
Tobin Ehlis554bf382016-05-24 11:14:43 -06001344bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001345 CoreChecks *dev_data, const char *func_name, std::string *error_code,
John Zulaufb45fdc32018-10-12 15:14:17 -06001346 std::string *error_msg) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001347 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00326";
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001348 auto iv_state = dev_data->GetImageViewState(image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001349 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001350 std::stringstream error_str;
1351 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001352 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001353 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001354 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001355 // 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 -06001356 // Validate that imageLayout is compatible with aspect_mask and image format
1357 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001358 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
1359 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001360 VkFormat format = VK_FORMAT_MAX_ENUM;
1361 VkImageUsageFlags usage = 0;
Mark Lobodzinski8ddc23f2019-03-06 11:48:49 -07001362 auto image_node = dev_data->GetImageState(image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001363 if (image_node) {
1364 format = image_node->createInfo.format;
1365 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001366 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001367 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
1368 // the error here occurs is if memory bound to a created imageView has been freed.
Mark Lobodzinski0ebe9712019-03-07 13:39:07 -07001369 if (dev_data->ValidateMemoryIsBoundToImage(image_node, func_name, "VUID-VkImageViewCreateInfo-image-01020")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001370 *error_code = "VUID-VkImageViewCreateInfo-image-01020";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001371 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001372 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001373 }
Chris Forbes67757ff2017-07-21 13:59:01 -07001374
1375 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
1376 // but not binding them to descriptor sets.
1377 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D &&
1378 (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
1379 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001380 *error_code = "VUID-VkDescriptorImageInfo-imageView-00343";
Chris Forbes67757ff2017-07-21 13:59:01 -07001381 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
1382 return false;
1383 }
Tobin Ehlis1809f912016-05-25 09:24:36 -06001384 }
1385 // First validate that format and layout are compatible
1386 if (format == VK_FORMAT_MAX_ENUM) {
1387 std::stringstream error_str;
1388 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001389 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001390 return false;
1391 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001392 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
1393 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -06001394 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -06001395 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001396 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1397 // Only Color bit must be set
1398 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001399 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -07001400 error_str
1401 << "ImageView (" << image_view
1402 << ") 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 -06001403 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001404 return false;
1405 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001406 // format must NOT be DS
1407 if (ds) {
1408 std::stringstream error_str;
1409 error_str << "ImageView (" << image_view
1410 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1411 << string_VkFormat(format) << " which is not a color format.";
1412 *error_msg = error_str.str();
1413 return false;
1414 }
1415 break;
1416 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1417 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1418 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001419 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1420 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1421 // both must NOT be set
1422 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001423 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001424 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001425 return false;
1426 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001427 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1428 // Neither were set
1429 std::stringstream error_str;
1430 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1431 << " but does not have STENCIL or DEPTH aspects set";
1432 *error_msg = error_str.str();
1433 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001434 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001435 // format must be DS
1436 if (!ds) {
1437 std::stringstream error_str;
1438 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1439 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
1440 *error_msg = error_str.str();
1441 return false;
1442 }
1443 break;
1444 default:
1445 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1446 if (ds) {
1447 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1448 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1449 // both must NOT be set
1450 std::stringstream error_str;
1451 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1452 << " and is using depth/stencil image of format " << string_VkFormat(format)
1453 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1454 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1455 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1456 "reads respectively.";
1457 *error_msg = error_str.str();
1458 return false;
1459 }
1460 }
1461 }
1462 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001463 }
1464 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001465 // 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 -06001466 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1467 // under vkCreateImage()
Dave Houltond8ed0212018-05-16 17:18:24 -06001468 // TODO : Need to also validate case "VUID-VkWriteDescriptorSet-descriptorType-00336" where STORAGE_IMAGE & INPUT_ATTACH types
1469 // must have been created with identify swizzle
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001470 const char *error_usage_bit = nullptr;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001471 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001472 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1473 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1474 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1475 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
1476 }
1477 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001478 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001479 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1480 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1481 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
1482 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
1483 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001484 // TODO : Need to create custom enum error codes for these cases
1485 if (image_node->shared_presentable) {
1486 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001487 error_str << "ImageView (" << image_view
1488 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
1489 "layout "
1490 << string_VkImageLayout(image_layout)
1491 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report "
1492 "support for VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the "
1493 "VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001494 *error_msg = error_str.str();
1495 return false;
1496 }
1497 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001498 error_str << "ImageView (" << image_view
1499 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
1500 << string_VkImageLayout(image_layout)
1501 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage "
1502 "images can only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001503 *error_msg = error_str.str();
1504 return false;
1505 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001506 }
1507 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001508 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001509 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
1510 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
1511 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
1512 }
1513 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001514 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001515 default:
1516 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001517 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001518 if (error_usage_bit) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001519 std::stringstream error_str;
1520 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
1521 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1522 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001523 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001524 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001525 }
John Zulauff4c07882019-01-24 14:03:36 -07001526
1527 if ((type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) || (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)) {
1528 // Test that the layout is compatible with the descriptorType for the two sampled image types
1529 const static std::array<VkImageLayout, 3> valid_layouts = {
1530 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL};
1531
1532 struct ExtensionLayout {
1533 VkImageLayout layout;
1534 bool DeviceExtensions::*extension;
1535 };
1536
1537 const static std::array<ExtensionLayout, 3> extended_layouts{
1538 {// Note double brace req'd for aggregate initialization
1539 {VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR, &DeviceExtensions::vk_khr_shared_presentable_image},
1540 {VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2},
1541 {VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2}}};
1542 auto is_layout = [image_layout, dev_data](const ExtensionLayout &ext_layout) {
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001543 return dev_data->device_extensions.*(ext_layout.extension) && (ext_layout.layout == image_layout);
John Zulauff4c07882019-01-24 14:03:36 -07001544 };
1545
1546 bool valid_layout = (std::find(valid_layouts.cbegin(), valid_layouts.cend(), image_layout) != valid_layouts.cend()) ||
1547 std::any_of(extended_layouts.cbegin(), extended_layouts.cend(), is_layout);
1548
1549 if (!valid_layout) {
1550 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01403";
1551 std::stringstream error_str;
1552 error_str << "Descriptor update with descriptorType " << string_VkDescriptorType(type)
1553 << " is being updated with invalid imageLayout " << string_VkImageLayout(image_layout)
1554 << ". Allowed layouts are: VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, "
1555 << "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL";
1556 for (auto &ext_layout : extended_layouts) {
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001557 if (dev_data->device_extensions.*(ext_layout.extension)) {
John Zulauff4c07882019-01-24 14:03:36 -07001558 error_str << ", " << string_VkImageLayout(ext_layout.layout);
1559 }
1560 }
1561 *error_msg = error_str.str();
1562 return false;
1563 }
1564 }
1565
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001566 return true;
1567}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001568
Tobin Ehlis300888c2016-05-18 13:43:26 -06001569void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Chris Forbesfea2c542018-04-13 09:34:15 -07001570 if (!immutable_) {
1571 sampler_ = update->pImageInfo[index].sampler;
1572 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001573 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001574}
1575
Tobin Ehlis300888c2016-05-18 13:43:26 -06001576void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001577 if (!immutable_) {
1578 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001579 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001580 }
1581 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001582}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001583
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001584void cvdescriptorset::SamplerDescriptor::UpdateDrawState(CoreChecks *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001585 if (!immutable_) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001586 auto sampler_state = dev_data->GetSamplerState(sampler_);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001587 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001588 }
1589}
1590
Tobin Ehlis300888c2016-05-18 13:43:26 -06001591cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001592 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001593 updated = false;
1594 descriptor_class = ImageSampler;
1595 if (immut) {
1596 sampler_ = *immut;
1597 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001598 }
1599}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001600
Tobin Ehlis300888c2016-05-18 13:43:26 -06001601void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001602 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001603 const auto &image_info = update->pImageInfo[index];
Chris Forbesfea2c542018-04-13 09:34:15 -07001604 if (!immutable_) {
1605 sampler_ = image_info.sampler;
1606 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001607 image_view_ = image_info.imageView;
1608 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001609}
1610
Tobin Ehlis300888c2016-05-18 13:43:26 -06001611void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001612 if (!immutable_) {
1613 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001614 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001615 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001616 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1617 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001618 updated = true;
1619 image_view_ = image_view;
1620 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001621}
1622
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001623void cvdescriptorset::ImageSamplerDescriptor::UpdateDrawState(CoreChecks *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001624 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001625 if (!immutable_) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07001626 auto sampler_state = dev_data->GetSamplerState(sampler_);
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001627 if (sampler_state) dev_data->AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001628 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001629 // Add binding for image
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001630 auto iv_state = dev_data->GetImageViewState(image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001631 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001632 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
John Zulauff660ad62019-03-23 07:16:05 -06001633 dev_data->SetImageViewInitialLayout(cb_node, *iv_state, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06001634 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001635}
1636
Tobin Ehlis300888c2016-05-18 13:43:26 -06001637cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1638 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001639 updated = false;
1640 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001641 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001642}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001643
Tobin Ehlis300888c2016-05-18 13:43:26 -06001644void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001645 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001646 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001647 image_view_ = image_info.imageView;
1648 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001649}
1650
Tobin Ehlis300888c2016-05-18 13:43:26 -06001651void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001652 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1653 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001654 updated = true;
1655 image_view_ = image_view;
1656 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001657}
1658
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001659void cvdescriptorset::ImageDescriptor::UpdateDrawState(CoreChecks *dev_data, CMD_BUFFER_STATE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001660 // Add binding for image
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07001661 auto iv_state = dev_data->GetImageViewState(image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001662 if (iv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001663 dev_data->AddCommandBufferBindingImageView(cb_node, iv_state);
John Zulauff660ad62019-03-23 07:16:05 -06001664 dev_data->SetImageViewInitialLayout(cb_node, *iv_state, image_layout_);
Jeff Bolz148d94e2018-12-13 21:25:56 -06001665 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001666}
1667
Tobin Ehlis300888c2016-05-18 13:43:26 -06001668cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1669 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001670 updated = false;
1671 descriptor_class = GeneralBuffer;
1672 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1673 dynamic_ = true;
1674 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1675 storage_ = true;
1676 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1677 dynamic_ = true;
1678 storage_ = true;
1679 }
1680}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001681void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001682 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001683 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001684 buffer_ = buffer_info.buffer;
1685 offset_ = buffer_info.offset;
1686 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001687}
1688
Tobin Ehlis300888c2016-05-18 13:43:26 -06001689void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1690 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001691 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001692 buffer_ = buff_desc->buffer_;
1693 offset_ = buff_desc->offset_;
1694 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001695}
1696
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001697void cvdescriptorset::BufferDescriptor::UpdateDrawState(CoreChecks *dev_data, CMD_BUFFER_STATE *cb_node) {
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07001698 auto buffer_node = dev_data->GetBufferState(buffer_);
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001699 if (buffer_node) dev_data->AddCommandBufferBindingBuffer(cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001700}
1701
Tobin Ehlis300888c2016-05-18 13:43:26 -06001702cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001703 updated = false;
1704 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001705 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001706}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001707
Tobin Ehlis300888c2016-05-18 13:43:26 -06001708void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001709 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001710 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001711}
1712
Tobin Ehlis300888c2016-05-18 13:43:26 -06001713void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1714 updated = true;
1715 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1716}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001717
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06001718void cvdescriptorset::TexelDescriptor::UpdateDrawState(CoreChecks *dev_data, CMD_BUFFER_STATE *cb_node) {
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07001719 auto bv_state = dev_data->GetBufferViewState(buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001720 if (bv_state) {
Mark Lobodzinskifae179e2019-03-08 16:47:08 -07001721 dev_data->AddCommandBufferBindingBufferView(cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001722 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001723}
1724
Tobin Ehlis300888c2016-05-18 13:43:26 -06001725// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1726// sets, and then calls their respective Validate[Write|Copy]Update functions.
1727// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1728// be skipped, then true is returned.
1729// If there is no issue with the update, then false is returned.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001730bool CoreChecks::ValidateUpdateDescriptorSets(uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001731 const VkCopyDescriptorSet *p_cds, const char *func_name) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001732 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001733 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001734 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001735 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001736 auto set_node = GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001737 if (!set_node) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001738 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1739 HandleToUint64(dest_set), kVUID_Core_DrawState_InvalidDescriptorSet,
Lockeca0d9792019-03-03 23:48:13 -07001740 "Cannot call %s on descriptor set %s that has not been allocated.", func_name,
1741 report_data->FormatHandle(dest_set).c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001742 } else {
Dave Houltond8ed0212018-05-16 17:18:24 -06001743 std::string error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001744 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001745 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], func_name, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001746 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 -06001747 HandleToUint64(dest_set), error_code,
Lockeca0d9792019-03-03 23:48:13 -07001748 "%s failed write update validation for Descriptor Set %s with error: %s.", func_name,
1749 report_data->FormatHandle(dest_set).c_str(), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001750 }
1751 }
1752 }
1753 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001754 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001755 auto dst_set = p_cds[i].dstSet;
1756 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001757 auto src_node = GetSetNode(src_set);
1758 auto dst_node = GetSetNode(dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001759 // Object_tracker verifies that src & dest descriptor set are valid
1760 assert(src_node);
1761 assert(dst_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001762 std::string error_code;
Tobin Ehlisa1712752017-01-04 09:41:47 -07001763 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001764 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, func_name, &error_code, &error_str)) {
Lockeca0d9792019-03-03 23:48:13 -07001765 skip |= log_msg(
1766 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT, HandleToUint64(dst_set),
1767 error_code, "%s failed copy update from Descriptor Set %s to Descriptor Set %s with error: %s.", func_name,
1768 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 -06001769 }
1770 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001771 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001772}
1773// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1774// sets, and then calls their respective Perform[Write|Copy]Update functions.
1775// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1776// with the same set of updates.
1777// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1778// calling down the chain.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001779void cvdescriptorset::PerformUpdateDescriptorSets(CoreChecks *dev_data, uint32_t write_count, const VkWriteDescriptorSet *p_wds,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001780 uint32_t copy_count, const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001781 // Write updates first
1782 uint32_t i = 0;
1783 for (i = 0; i < write_count; ++i) {
1784 auto dest_set = p_wds[i].dstSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001785 auto set_node = dev_data->GetSetNode(dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001786 if (set_node) {
1787 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001788 }
1789 }
1790 // Now copy updates
1791 for (i = 0; i < copy_count; ++i) {
1792 auto dst_set = p_cds[i].dstSet;
1793 auto src_set = p_cds[i].srcSet;
Mark Lobodzinskifc2f0d32019-03-06 11:25:39 -07001794 auto src_node = dev_data->GetSetNode(src_set);
1795 auto dst_node = dev_data->GetSetNode(dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001796 if (src_node && dst_node) {
1797 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001798 }
1799 }
1800}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001801
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001802cvdescriptorset::DecodedTemplateUpdate::DecodedTemplateUpdate(CoreChecks *device_data, VkDescriptorSet descriptorSet,
John Zulauf1d27e0a2018-11-05 10:12:48 -07001803 const TEMPLATE_STATE *template_state, const void *pData,
1804 VkDescriptorSetLayout push_layout) {
John Zulaufb845eb22018-10-12 11:41:06 -06001805 auto const &create_info = template_state->create_info;
1806 inline_infos.resize(create_info.descriptorUpdateEntryCount); // Make sure we have one if we need it
1807 desc_writes.reserve(create_info.descriptorUpdateEntryCount); // emplaced, so reserved without initialization
John Zulauf1d27e0a2018-11-05 10:12:48 -07001808 VkDescriptorSetLayout effective_dsl = create_info.templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
1809 ? create_info.descriptorSetLayout
1810 : push_layout;
1811 auto layout_obj = GetDescriptorSetLayout(device_data, effective_dsl);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001812
1813 // Create a WriteDescriptorSet struct for each template update entry
1814 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1815 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1816 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1817 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1818
John Zulaufb6d71202017-12-22 16:47:09 -07001819 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001820 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1821 desc_writes.emplace_back();
1822 auto &write_entry = desc_writes.back();
1823
1824 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1825 char *update_entry = (char *)(pData) + offset;
1826
1827 if (dst_array_element >= binding_count) {
1828 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001829 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001830 }
1831
1832 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1833 write_entry.pNext = NULL;
1834 write_entry.dstSet = descriptorSet;
1835 write_entry.dstBinding = binding_being_updated;
1836 write_entry.dstArrayElement = dst_array_element;
1837 write_entry.descriptorCount = 1;
1838 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1839
1840 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1841 case VK_DESCRIPTOR_TYPE_SAMPLER:
1842 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1843 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1844 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1845 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1846 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1847 break;
1848
1849 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1850 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1851 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1852 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1853 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1854 break;
1855
1856 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1857 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1858 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1859 break;
Dave Houlton142c4cb2018-10-17 15:04:41 -06001860 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1861 VkWriteDescriptorSetInlineUniformBlockEXT *inline_info = &inline_infos[i];
1862 inline_info->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT;
1863 inline_info->pNext = nullptr;
1864 inline_info->dataSize = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1865 inline_info->pData = update_entry;
1866 write_entry.pNext = inline_info;
1867 // skip the rest of the array, they just represent bytes in the update
1868 j = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1869 break;
1870 }
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001871 default:
1872 assert(0);
1873 break;
1874 }
1875 dst_array_element++;
1876 }
1877 }
John Zulaufb845eb22018-10-12 11:41:06 -06001878}
John Zulaufb45fdc32018-10-12 15:14:17 -06001879// These helper functions carry out the validate and record descriptor updates peformed via update templates. They decode
1880// the templatized data and leverage the non-template UpdateDescriptor helper functions.
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001881bool CoreChecks::ValidateUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
1882 const void *pData) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001883 // Translate the templated update into a normal update for validation...
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07001884 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
1885 return ValidateUpdateDescriptorSets(static_cast<uint32_t>(decoded_update.desc_writes.size()), decoded_update.desc_writes.data(),
1886 0, NULL, "vkUpdateDescriptorSetWithTemplate()");
John Zulaufb45fdc32018-10-12 15:14:17 -06001887}
John Zulaufb845eb22018-10-12 11:41:06 -06001888
Mark Lobodzinski254c8512019-03-09 12:21:15 -07001889void CoreChecks::PerformUpdateDescriptorSetsWithTemplateKHR(VkDescriptorSet descriptorSet, const TEMPLATE_STATE *template_state,
1890 const void *pData) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001891 // Translate the templated update into a normal update for validation...
Mark Lobodzinski254c8512019-03-09 12:21:15 -07001892 cvdescriptorset::DecodedTemplateUpdate decoded_update(this, descriptorSet, template_state, pData);
1893 cvdescriptorset::PerformUpdateDescriptorSets(this, static_cast<uint32_t>(decoded_update.desc_writes.size()),
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07001894 decoded_update.desc_writes.data(), 0, NULL);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001895}
John Zulauf4e7bcb52018-11-02 10:46:30 -06001896
1897std::string cvdescriptorset::DescriptorSet::StringifySetAndLayout() const {
1898 std::string out;
1899 uint64_t layout_handle = HandleToUint64(p_layout_->GetDescriptorSetLayout());
1900 if (IsPushDescriptor()) {
1901 string_sprintf(&out, "Push Descriptors defined with VkDescriptorSetLayout 0x%" PRIxLEAST64, layout_handle);
John Zulauf4e7bcb52018-11-02 10:46:30 -06001902 } else {
Bob Ellisonaf2f8532019-03-12 11:05:47 -06001903 string_sprintf(&out, "VkDescriptorSet 0x%" PRIxLEAST64 " allocated with VkDescriptorSetLayout 0x%" PRIxLEAST64,
John Zulauf4e7bcb52018-11-02 10:46:30 -06001904 HandleToUint64(set_), layout_handle);
1905 }
1906 return out;
1907};
1908
John Zulauf1d27e0a2018-11-05 10:12:48 -07001909// Loop through the write updates to validate for a push descriptor set, ignoring dstSet
1910bool cvdescriptorset::DescriptorSet::ValidatePushDescriptorsUpdate(const debug_report_data *report_data, uint32_t write_count,
1911 const VkWriteDescriptorSet *p_wds, const char *func_name) {
1912 assert(IsPushDescriptor());
1913 bool skip = false;
1914 for (uint32_t i = 0; i < write_count; i++) {
1915 std::string error_code;
1916 std::string error_str;
1917 if (!ValidateWriteUpdate(report_data, &p_wds[i], func_name, &error_code, &error_str)) {
1918 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1919 HandleToUint64(p_layout_->GetDescriptorSetLayout()), error_code, "%s failed update validation: %s.",
1920 func_name, error_str.c_str());
1921 }
1922 }
1923 return skip;
1924}
1925
Tobin Ehlis300888c2016-05-18 13:43:26 -06001926// Validate the state for a given write update but don't actually perform the update
1927// If an error would occur for this update, return false and fill in details in error_msg string
1928bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -06001929 const char *func_name, std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001930 // Verify dst layout still valid
1931 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001932 *error_code = "VUID-VkWriteDescriptorSet-dstSet-00320";
John Zulauf4e7bcb52018-11-02 10:46:30 -06001933 string_sprintf(error_msg, "Cannot call %s to perform write update on %s which has been destroyed", func_name,
1934 StringifySetAndLayout().c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001935 return false;
1936 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001937 // Verify dst binding exists
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001938 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001939 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00315";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001940 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001941 error_str << StringifySetAndLayout() << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001942 *error_msg = error_str.str();
1943 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001944 } else {
1945 // Make sure binding isn't empty
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001946 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001947 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00316";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001948 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001949 error_str << StringifySetAndLayout() << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001950 *error_msg = error_str.str();
1951 return false;
1952 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001953 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001954 // Verify idle ds
1955 if (in_use.load() &&
1956 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1957 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1958 // TODO : Re-using Free Idle error code, need write update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001959 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001960 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001961 error_str << "Cannot call " << func_name << " to perform write update on " << StringifySetAndLayout()
Jeff Bolzfdf96072018-04-10 14:32:18 -05001962 << " that is in use by a command buffer";
1963 *error_msg = error_str.str();
1964 return false;
1965 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001966 // We know that binding is valid, verify update and do update on each descriptor
John Zulaufc483f442017-12-15 14:02:06 -07001967 auto start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001968 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001969 if (type != update->descriptorType) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001970 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00319";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001971 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001972 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with type "
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001973 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1974 *error_msg = error_str.str();
1975 return false;
1976 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001977 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001978 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001979 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001980 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001981 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001982 << " descriptors in that binding and all successive bindings of the set, but update of "
1983 << update->descriptorCount << " descriptors combined with update array element offset of "
1984 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001985 *error_msg = error_str.str();
1986 return false;
1987 }
Jeff Bolze54ae892018-09-08 12:16:29 -05001988 if (type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1989 if ((update->dstArrayElement % 4) != 0) {
1990 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02219";
1991 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001992 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001993 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1994 *error_msg = error_str.str();
1995 return false;
1996 }
1997 if ((update->descriptorCount % 4) != 0) {
1998 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02220";
1999 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002000 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05002001 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
2002 *error_msg = error_str.str();
2003 return false;
2004 }
2005 const auto *write_inline_info = lvl_find_in_chain<VkWriteDescriptorSetInlineUniformBlockEXT>(update->pNext);
2006 if (!write_inline_info || write_inline_info->dataSize != update->descriptorCount) {
2007 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02221";
2008 std::stringstream error_str;
2009 if (!write_inline_info) {
John Zulauf4e7bcb52018-11-02 10:46:30 -06002010 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
2011 << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05002012 << "VkWriteDescriptorSetInlineUniformBlockEXT missing";
2013 } else {
John Zulauf4e7bcb52018-11-02 10:46:30 -06002014 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
2015 << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06002016 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2017 << " not equal to "
Jeff Bolze54ae892018-09-08 12:16:29 -05002018 << "VkWriteDescriptorSet descriptorCount " << update->descriptorCount;
2019 }
2020 *error_msg = error_str.str();
2021 return false;
2022 }
2023 // This error is probably unreachable due to the previous two errors
2024 if (write_inline_info && (write_inline_info->dataSize % 4) != 0) {
2025 *error_code = "VUID-VkWriteDescriptorSetInlineUniformBlockEXT-dataSize-02222";
2026 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002027 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06002028 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2029 << " not a multiple of 4";
Jeff Bolze54ae892018-09-08 12:16:29 -05002030 *error_msg = error_str.str();
2031 return false;
2032 }
2033 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002034 // Verify consecutive bindings match (if needed)
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06002035 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002036 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07002037 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Dave Houlton00c154e2018-05-24 13:20:50 -06002038 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002039 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002040 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002041 // Update is within bounds and consistent so last step is to validate update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06002042 if (!VerifyWriteUpdateContents(update, start_idx, func_name, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002043 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002044 error_str << "Write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002045 << " failed with error message: " << error_msg->c_str();
2046 *error_msg = error_str.str();
2047 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002048 }
2049 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002050 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06002051}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002052// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002053// 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 -07002054bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06002055 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002056 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06002057 auto usage = buffer_node->createInfo.usage;
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002058 const char *error_usage_bit = nullptr;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002059 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002060 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2061 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002062 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00334";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002063 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
2064 }
2065 break;
2066 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2067 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002068 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00335";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002069 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
2070 }
2071 break;
2072 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2073 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2074 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002075 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00330";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002076 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
2077 }
2078 break;
2079 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2080 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
2081 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002082 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00331";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002083 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
2084 }
2085 break;
2086 default:
2087 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002088 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002089 if (error_usage_bit) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002090 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002091 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
2092 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
2093 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002094 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002095 return false;
2096 }
2097 return true;
2098}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002099// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
2100// 1. buffer is valid
2101// 2. buffer was created with correct usage flags
2102// 3. offset is less than buffer size
2103// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002104// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002105// 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 -06002106bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
John Zulaufb45fdc32018-10-12 15:14:17 -06002107 const char *func_name, std::string *error_code,
2108 std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002109 // First make sure that buffer is valid
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002110 auto buffer_node = device_data_->GetBufferState(buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07002111 // Any invalid buffer should already be caught by object_tracker
2112 assert(buffer_node);
Mark Lobodzinski0ebe9712019-03-07 13:39:07 -07002113 if (device_data_->ValidateMemoryIsBoundToBuffer(buffer_node, func_name, "VUID-VkWriteDescriptorSet-descriptorType-00329")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002114 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00329";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002115 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06002116 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06002117 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002118 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002119 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
2120 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002121 return false;
2122 }
2123 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002124 if (buffer_info->offset >= buffer_node->createInfo.size) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002125 *error_code = "VUID-VkDescriptorBufferInfo-offset-00340";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002126 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002127 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
2128 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002129 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002130 return false;
2131 }
2132 if (buffer_info->range != VK_WHOLE_SIZE) {
2133 // Range must be VK_WHOLE_SIZE or > 0
2134 if (!buffer_info->range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002135 *error_code = "VUID-VkDescriptorBufferInfo-range-00341";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002136 std::stringstream error_str;
2137 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002138 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002139 return false;
2140 }
2141 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
2142 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002143 *error_code = "VUID-VkDescriptorBufferInfo-range-00342";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002144 std::stringstream error_str;
2145 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
2146 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002147 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002148 return false;
2149 }
2150 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002151 // Check buffer update sizes against device limits
2152 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
2153 auto max_ub_range = limits_.maxUniformBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002154 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002155 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002156 std::stringstream error_str;
2157 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2158 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
2159 *error_msg = error_str.str();
2160 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002161 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_ub_range) {
2162 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
2163 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002164 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2165 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002166 << "maxUniformBufferRange (" << max_ub_range << ")";
2167 *error_msg = error_str.str();
2168 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002169 }
2170 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
2171 auto max_sb_range = limits_.maxStorageBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002172 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002173 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002174 std::stringstream error_str;
2175 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2176 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
2177 *error_msg = error_str.str();
2178 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002179 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_sb_range) {
2180 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
2181 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002182 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2183 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002184 << "maxStorageBufferRange (" << max_sb_range << ")";
2185 *error_msg = error_str.str();
2186 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002187 }
2188 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002189 return true;
2190}
2191
Tobin Ehlis300888c2016-05-18 13:43:26 -06002192// Verify that the contents of the update are ok, but don't perform actual update
2193bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
John Zulaufb45fdc32018-10-12 15:14:17 -06002194 const char *func_name, std::string *error_code,
2195 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002196 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002197 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
2198 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2199 // Validate image
2200 auto image_view = update->pImageInfo[di].imageView;
2201 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002202 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2203 error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002204 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002205 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
2206 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002207 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002208 return false;
2209 }
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002210 if (device_data_->device_extensions.vk_khr_sampler_ycbcr_conversion) {
Juan A. Suarez Romerob6809602019-04-24 07:52:26 +00002211 ImageSamplerDescriptor *desc = (ImageSamplerDescriptor *)descriptors_[index + di].get();
Tony-LunarG352f08f2019-01-03 10:11:24 -07002212 if (desc->IsImmutableSampler()) {
Mark Lobodzinskif8d1ef92019-03-06 11:53:27 -07002213 auto sampler_state = device_data_->GetSamplerState(desc->GetSampler());
Mark Lobodzinskia3a230b2019-03-06 15:35:13 -07002214 auto iv_state = device_data_->GetImageViewState(image_view);
Tony-LunarG352f08f2019-01-03 10:11:24 -07002215 if (iv_state && sampler_state) {
2216 if (iv_state->samplerConversion != sampler_state->samplerConversion) {
2217 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01948";
2218 std::stringstream error_str;
Tony-LunarG8a51f0a2019-01-04 16:14:14 -07002219 error_str << "Attempted write update to combined image sampler and image view and sampler ycbcr "
2220 "conversions are not identical, sampler: "
Tony-LunarG352f08f2019-01-03 10:11:24 -07002221 << desc->GetSampler() << " image view: " << iv_state->image_view << ".";
2222 *error_msg = error_str.str();
2223 return false;
2224 }
2225 }
Lockea223c102019-04-05 00:38:24 -06002226 } else {
2227 auto iv_state = device_data_->GetImageViewState(image_view);
Locke5ce35a72019-05-06 14:24:36 -06002228 if (iv_state && (iv_state->samplerConversion != VK_NULL_HANDLE)) {
2229 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01947";
2230 std::stringstream error_str;
2231 error_str << "Because dstSet (" << update->dstSet << ") is bound to image view ("
2232 << iv_state->image_view
2233 << ") that includes a YCBCR conversion, it must have been allocated with a layout that "
2234 "includes an immutable sampler.";
2235 *error_msg = error_str.str();
2236 return false;
Lockea223c102019-04-05 00:38:24 -06002237 }
Tony-LunarG352f08f2019-01-03 10:11:24 -07002238 }
2239 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002240 }
2241 }
Tobin Ehlis648b1cf2018-04-13 15:24:13 -06002242 // fall through
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002243 case VK_DESCRIPTOR_TYPE_SAMPLER: {
2244 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2245 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
2246 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002247 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002248 std::stringstream error_str;
2249 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
2250 << update->pImageInfo[di].sampler << ".";
2251 *error_msg = error_str.str();
2252 return false;
2253 }
2254 } else {
2255 // TODO : Warn here
2256 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002257 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002258 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002259 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002260 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2261 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2262 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2263 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2264 auto image_view = update->pImageInfo[di].imageView;
2265 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002266 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2267 error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002268 std::stringstream error_str;
2269 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
2270 *error_msg = error_str.str();
2271 return false;
2272 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002273 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002274 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002275 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002276 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2277 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
2278 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2279 auto buffer_view = update->pTexelBufferView[di];
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07002280 auto bv_state = device_data_->GetBufferViewState(buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002281 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002282 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002283 std::stringstream error_str;
2284 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2285 *error_msg = error_str.str();
2286 return false;
2287 }
2288 auto buffer = bv_state->create_info.buffer;
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002289 auto buffer_state = device_data_->GetBufferState(buffer);
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002290 // Verify that buffer underlying the view hasn't been destroyed prematurely
2291 if (!buffer_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002292 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002293 std::stringstream error_str;
2294 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
2295 << ") has been destroyed: " << error_msg->c_str();
2296 *error_msg = error_str.str();
2297 return false;
2298 } else if (!ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002299 std::stringstream error_str;
2300 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
2301 *error_msg = error_str.str();
2302 return false;
2303 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002304 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002305 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002306 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002307 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2308 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2309 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2310 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2311 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufb45fdc32018-10-12 15:14:17 -06002312 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, func_name, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002313 std::stringstream error_str;
2314 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
2315 *error_msg = error_str.str();
2316 return false;
2317 }
2318 }
2319 break;
2320 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002321 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
2322 break;
Eric Werness30127fd2018-10-31 21:01:03 -07002323 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002324 // XXX TODO
2325 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002326 default:
2327 assert(0); // We've already verified update type so should never get here
2328 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002329 }
2330 // All checks passed so update contents are good
2331 return true;
2332}
2333// Verify that the contents of the update are ok, but don't perform actual update
2334bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
John Zulaufb45fdc32018-10-12 15:14:17 -06002335 VkDescriptorType type, uint32_t index, const char *func_name,
2336 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002337 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
2338 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06002339 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002340 case PlainSampler: {
2341 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002342 const auto src_desc = src_set->descriptors_[index + di].get();
2343 if (!src_desc->updated) continue;
2344 if (!src_desc->IsImmutableSampler()) {
2345 auto update_sampler = static_cast<SamplerDescriptor *>(src_desc)->GetSampler();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002346 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002347 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002348 std::stringstream error_str;
2349 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2350 *error_msg = error_str.str();
2351 return false;
2352 }
2353 } else {
2354 // TODO : Warn here
2355 }
2356 }
2357 break;
2358 }
2359 case ImageSampler: {
2360 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002361 const auto src_desc = src_set->descriptors_[index + di].get();
2362 if (!src_desc->updated) continue;
2363 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002364 // First validate sampler
2365 if (!img_samp_desc->IsImmutableSampler()) {
2366 auto update_sampler = img_samp_desc->GetSampler();
2367 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002368 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002369 std::stringstream error_str;
2370 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2371 *error_msg = error_str.str();
2372 return false;
2373 }
2374 } else {
2375 // TODO : Warn here
2376 }
2377 // Validate image
2378 auto image_view = img_samp_desc->GetImageView();
2379 auto image_layout = img_samp_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002380 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002381 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002382 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002383 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002384 return false;
2385 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002386 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002387 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002388 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002389 case Image: {
2390 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002391 const auto src_desc = src_set->descriptors_[index + di].get();
2392 if (!src_desc->updated) continue;
2393 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002394 auto image_view = img_desc->GetImageView();
2395 auto image_layout = img_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002396 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002397 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002398 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002399 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002400 return false;
2401 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002402 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002403 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002404 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002405 case TexelBuffer: {
2406 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002407 const auto src_desc = src_set->descriptors_[index + di].get();
2408 if (!src_desc->updated) continue;
2409 auto buffer_view = static_cast<TexelDescriptor *>(src_desc)->GetBufferView();
Mark Lobodzinski31aa9b02019-03-06 11:51:37 -07002410 auto bv_state = device_data_->GetBufferViewState(buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002411 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002412 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002413 std::stringstream error_str;
2414 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2415 *error_msg = error_str.str();
2416 return false;
2417 }
2418 auto buffer = bv_state->create_info.buffer;
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002419 if (!ValidateBufferUsage(device_data_->GetBufferState(buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002420 std::stringstream error_str;
2421 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
2422 *error_msg = error_str.str();
2423 return false;
2424 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002425 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002426 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002427 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002428 case GeneralBuffer: {
2429 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002430 const auto src_desc = src_set->descriptors_[index + di].get();
2431 if (!src_desc->updated) continue;
2432 auto buffer = static_cast<BufferDescriptor *>(src_desc)->GetBuffer();
Mark Lobodzinski6ed74142019-03-06 11:35:39 -07002433 if (!ValidateBufferUsage(device_data_->GetBufferState(buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002434 std::stringstream error_str;
2435 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
2436 *error_msg = error_str.str();
2437 return false;
2438 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06002439 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002440 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002441 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002442 case InlineUniform:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002443 case AccelerationStructure:
Jeff Bolze54ae892018-09-08 12:16:29 -05002444 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002445 default:
2446 assert(0); // We've already verified update type so should never get here
2447 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002448 }
2449 // All checks passed so update contents are good
2450 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12002451}
Tobin Ehlisf320b192017-03-14 11:22:50 -06002452// Update the common AllocateDescriptorSetsData
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002453void CoreChecks::UpdateAllocateDescriptorSetsData(const VkDescriptorSetAllocateInfo *p_alloc_info,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002454 cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisf320b192017-03-14 11:22:50 -06002455 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002456 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
Tobin Ehlisf320b192017-03-14 11:22:50 -06002457 if (layout) {
2458 ds_data->layout_nodes[i] = layout;
2459 // Count total descriptors required per type
2460 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
2461 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
2462 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
2463 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
2464 }
2465 }
2466 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
2467 }
Petr Kraus13c98a62017-12-09 00:22:39 +01002468}
Tobin Ehlisee471462016-05-26 11:21:59 -06002469// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002470bool CoreChecks::ValidateAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002471 const cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002472 bool skip = false;
Mark Lobodzinski7804bd42019-03-06 11:28:48 -07002473 auto pool_state = GetDescriptorPoolState(p_alloc_info->descriptorPool);
Tobin Ehlisee471462016-05-26 11:21:59 -06002474
2475 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002476 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07002477 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
John Zulauf1d27e0a2018-11-05 10:12:48 -07002478 if (layout->IsPushDescriptor()) {
John Zulauf5562d062018-01-24 11:54:05 -07002479 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 -06002480 HandleToUint64(p_alloc_info->pSetLayouts[i]), "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-00308",
Lockeca0d9792019-03-03 23:48:13 -07002481 "Layout %s specified at pSetLayouts[%" PRIu32
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002482 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
Lockeca0d9792019-03-03 23:48:13 -07002483 report_data->FormatHandle(p_alloc_info->pSetLayouts[i]).c_str(), i,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002484 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07002485 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002486 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT &&
2487 !(pool_state->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
2488 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 -06002489 0, "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-03044",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002490 "Descriptor set layout create flags and pool create flags mismatch for index (%d)", i);
2491 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002492 }
2493 }
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002494 if (!device_extensions.vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002495 // Track number of descriptorSets allowable in this pool
2496 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002497 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 -06002498 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorSetCount-00306",
Lockeca0d9792019-03-03 23:48:13 -07002499 "Unable to allocate %u descriptorSets from pool %s"
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002500 ". This pool only has %d descriptorSets remaining.",
Lockeca0d9792019-03-03 23:48:13 -07002501 p_alloc_info->descriptorSetCount, report_data->FormatHandle(pool_state->pool).c_str(),
2502 pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002503 }
2504 // Determine whether descriptor counts are satisfiable
Jeff Bolze54ae892018-09-08 12:16:29 -05002505 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2506 if (ds_data->required_descriptors_by_type.at(it->first) > pool_state->availableDescriptorTypeCount[it->first]) {
Lockeca0d9792019-03-03 23:48:13 -07002507 skip |= log_msg(
2508 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
2509 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307",
2510 "Unable to allocate %u descriptors of type %s from pool %s"
2511 ". This pool only has %d descriptors of this type remaining.",
2512 ds_data->required_descriptors_by_type.at(it->first), string_VkDescriptorType(VkDescriptorType(it->first)),
2513 report_data->FormatHandle(pool_state->pool).c_str(), pool_state->availableDescriptorTypeCount[it->first]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002514 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002515 }
2516 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06002517
Jeff Bolzfdf96072018-04-10 14:32:18 -05002518 const auto *count_allocate_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2519
2520 if (count_allocate_info) {
2521 if (count_allocate_info->descriptorSetCount != 0 &&
2522 count_allocate_info->descriptorSetCount != p_alloc_info->descriptorSetCount) {
2523 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 -06002524 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-descriptorSetCount-03045",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002525 "VkDescriptorSetAllocateInfo::descriptorSetCount (%d) != "
2526 "VkDescriptorSetVariableDescriptorCountAllocateInfoEXT::descriptorSetCount (%d)",
2527 p_alloc_info->descriptorSetCount, count_allocate_info->descriptorSetCount);
2528 }
2529 if (count_allocate_info->descriptorSetCount == p_alloc_info->descriptorSetCount) {
2530 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Mark Lobodzinski3840ca02019-03-08 18:36:11 -07002531 auto layout = GetDescriptorSetLayout(this, p_alloc_info->pSetLayouts[i]);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002532 if (count_allocate_info->pDescriptorCounts[i] > layout->GetDescriptorCountFromBinding(layout->GetMaxBinding())) {
2533 skip |= log_msg(
2534 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 -06002535 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-pSetLayouts-03046",
2536 "pDescriptorCounts[%d] = (%d), binding's descriptorCount = (%d)", i,
Jeff Bolzfdf96072018-04-10 14:32:18 -05002537 count_allocate_info->pDescriptorCounts[i], layout->GetDescriptorCountFromBinding(layout->GetMaxBinding()));
2538 }
2539 }
2540 }
2541 }
2542
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002543 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06002544}
2545// Decrement allocated sets from the pool and insert new sets into set_map
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002546void CoreChecks::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
2547 const VkDescriptorSet *descriptor_sets,
Mark Lobodzinskia33af952019-04-25 14:59:05 -06002548 const cvdescriptorset::AllocateDescriptorSetsData *ds_data) {
2549 auto pool_state = descriptorPoolMap[p_alloc_info->descriptorPool].get();
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002550 // Account for sets and individual descriptors allocated from pool
Tobin Ehlisee471462016-05-26 11:21:59 -06002551 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Jeff Bolze54ae892018-09-08 12:16:29 -05002552 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2553 pool_state->availableDescriptorTypeCount[it->first] -= ds_data->required_descriptors_by_type.at(it->first);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06002554 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002555
2556 const auto *variable_count_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2557 bool variable_count_valid = variable_count_info && variable_count_info->descriptorSetCount == p_alloc_info->descriptorSetCount;
2558
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002559 // Create tracking object for each descriptor set; insert into global map and the pool's set.
Tobin Ehlisee471462016-05-26 11:21:59 -06002560 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -05002561 uint32_t variable_count = variable_count_valid ? variable_count_info->pDescriptorCounts[i] : 0;
2562
Mark Lobodzinski14112ab2019-04-25 15:29:34 -06002563 std::unique_ptr<cvdescriptorset::DescriptorSet> new_ds(new cvdescriptorset::DescriptorSet(
2564 descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i], variable_count, this));
2565 pool_state->sets.insert(new_ds.get());
Tobin Ehlisee471462016-05-26 11:21:59 -06002566 new_ds->in_use.store(0);
Mark Lobodzinski14112ab2019-04-25 15:29:34 -06002567 setMap[descriptor_sets[i]] = std::move(new_ds);
Tobin Ehlisee471462016-05-26 11:21:59 -06002568 }
2569}
John Zulauf48a6a702017-12-22 17:14:54 -07002570
2571cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06002572 CMD_BUFFER_STATE *cb_state)
John Zulauf48a6a702017-12-22 17:14:54 -07002573 : filtered_map_(), orig_map_(in_map) {
2574 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2575 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2576 ds.FilterAndTrackBindingReqs(cb_state, orig_map_, filtered_map_.get());
2577 }
2578}
2579cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
Mark Lobodzinski33a34b82019-04-25 11:38:36 -06002580 CMD_BUFFER_STATE *cb_state, PIPELINE_STATE *pipeline)
John Zulauf48a6a702017-12-22 17:14:54 -07002581 : filtered_map_(), orig_map_(in_map) {
2582 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2583 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2584 ds.FilterAndTrackBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
2585 }
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01002586}