blob: 2744fcc59b55f199707df8c8ae9c71981d73ced8 [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 Lobodzinski76d76662019-02-14 14:38:21 -070025#include "core_validation_error_enums.h"
26#include "core_validation.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060027#include "descriptor_sets.h"
John Zulaufd47d0612018-02-16 13:00:34 -070028#include "hash_vk_types.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060029#include "vk_enum_string_helper.h"
30#include "vk_safe_struct.h"
Jeff Bolzfdf96072018-04-10 14:32:18 -050031#include "vk_typemap_helper.h"
Tobin Ehlisc8266452017-04-07 12:20:30 -060032#include "buffer_validation.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060033#include <sstream>
Mark Lobodzinski2eee5d82016-12-02 15:33:18 -070034#include <algorithm>
John Zulauff4c07882019-01-24 14:03:36 -070035#include <array>
John Zulauf1f8174b2018-02-16 12:58:37 -070036#include <memory>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060037
Jeff Bolzfdf96072018-04-10 14:32:18 -050038// ExtendedBinding collects a VkDescriptorSetLayoutBinding and any extended
39// state that comes from a different array/structure so they can stay together
40// while being sorted by binding number.
41struct ExtendedBinding {
42 ExtendedBinding(const VkDescriptorSetLayoutBinding *l, VkDescriptorBindingFlagsEXT f) : layout_binding(l), binding_flags(f) {}
43
44 const VkDescriptorSetLayoutBinding *layout_binding;
45 VkDescriptorBindingFlagsEXT binding_flags;
46};
47
John Zulauf508d13a2018-01-05 15:10:34 -070048struct BindingNumCmp {
Jeff Bolzfdf96072018-04-10 14:32:18 -050049 bool operator()(const ExtendedBinding &a, const ExtendedBinding &b) const {
50 return a.layout_binding->binding < b.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070051 }
52};
53
John Zulaufd47d0612018-02-16 13:00:34 -070054using DescriptorSetLayoutDef = cvdescriptorset::DescriptorSetLayoutDef;
55using DescriptorSetLayoutId = cvdescriptorset::DescriptorSetLayoutId;
56
John Zulauf34ebf272018-02-16 13:08:47 -070057// Canonical dictionary of DescriptorSetLayoutDef (without any handle/device specific information)
58cvdescriptorset::DescriptorSetLayoutDict descriptor_set_layout_dict;
John Zulaufd47d0612018-02-16 13:00:34 -070059
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060060DescriptorSetLayoutId GetCanonicalId(const VkDescriptorSetLayoutCreateInfo *p_create_info) {
John Zulauf34ebf272018-02-16 13:08:47 -070061 return descriptor_set_layout_dict.look_up(DescriptorSetLayoutDef(p_create_info));
John Zulaufd47d0612018-02-16 13:00:34 -070062}
John Zulauf34ebf272018-02-16 13:08:47 -070063
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060064// Construct DescriptorSetLayout instance from given create info
John Zulauf48a6a702017-12-22 17:14:54 -070065// Proactively reserve and resize as possible, as the reallocation was visible in profiling
John Zulauf1f8174b2018-02-16 12:58:37 -070066cvdescriptorset::DescriptorSetLayoutDef::DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info)
67 : flags_(p_create_info->flags), binding_count_(0), descriptor_count_(0), dynamic_descriptor_count_(0) {
Jeff Bolzfdf96072018-04-10 14:32:18 -050068 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(p_create_info->pNext);
69
John Zulauf48a6a702017-12-22 17:14:54 -070070 binding_type_stats_ = {0, 0, 0};
Jeff Bolzfdf96072018-04-10 14:32:18 -050071 std::set<ExtendedBinding, BindingNumCmp> sorted_bindings;
John Zulauf508d13a2018-01-05 15:10:34 -070072 const uint32_t input_bindings_count = p_create_info->bindingCount;
73 // Sort the input bindings in binding number order, eliminating duplicates
74 for (uint32_t i = 0; i < input_bindings_count; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -050075 VkDescriptorBindingFlagsEXT flags = 0;
76 if (flags_create_info && flags_create_info->bindingCount == p_create_info->bindingCount) {
77 flags = flags_create_info->pBindingFlags[i];
78 }
79 sorted_bindings.insert(ExtendedBinding(p_create_info->pBindings + i, flags));
John Zulaufb6d71202017-12-22 16:47:09 -070080 }
81
82 // Store the create info in the sorted order from above
Tobin Ehlisa3525e02016-11-17 10:50:52 -070083 std::map<uint32_t, uint32_t> binding_to_dyn_count;
John Zulauf508d13a2018-01-05 15:10:34 -070084 uint32_t index = 0;
85 binding_count_ = static_cast<uint32_t>(sorted_bindings.size());
86 bindings_.reserve(binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -050087 binding_flags_.reserve(binding_count_);
John Zulauf508d13a2018-01-05 15:10:34 -070088 binding_to_index_map_.reserve(binding_count_);
89 for (auto input_binding : sorted_bindings) {
90 // Add to binding and map, s.t. it is robust to invalid duplication of binding_num
Jeff Bolzfdf96072018-04-10 14:32:18 -050091 const auto binding_num = input_binding.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070092 binding_to_index_map_[binding_num] = index++;
Jeff Bolzfdf96072018-04-10 14:32:18 -050093 bindings_.emplace_back(input_binding.layout_binding);
John Zulauf508d13a2018-01-05 15:10:34 -070094 auto &binding_info = bindings_.back();
Jeff Bolzfdf96072018-04-10 14:32:18 -050095 binding_flags_.emplace_back(input_binding.binding_flags);
John Zulauf508d13a2018-01-05 15:10:34 -070096
John Zulaufb6d71202017-12-22 16:47:09 -070097 descriptor_count_ += binding_info.descriptorCount;
98 if (binding_info.descriptorCount > 0) {
99 non_empty_bindings_.insert(binding_num);
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700100 }
John Zulaufb6d71202017-12-22 16:47:09 -0700101
102 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
103 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
104 binding_to_dyn_count[binding_num] = binding_info.descriptorCount;
105 dynamic_descriptor_count_ += binding_info.descriptorCount;
John Zulauf48a6a702017-12-22 17:14:54 -0700106 binding_type_stats_.dynamic_buffer_count++;
107 } else if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
108 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
109 binding_type_stats_.non_dynamic_buffer_count++;
110 } else {
111 binding_type_stats_.image_sampler_count++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600112 }
113 }
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700114 assert(bindings_.size() == binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500115 assert(binding_flags_.size() == binding_count_);
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700116 uint32_t global_index = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700117 binding_to_global_index_range_map_.reserve(binding_count_);
118 // Vector order is finalized so create maps of bindings to descriptors and descriptors to indices
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700119 for (uint32_t i = 0; i < binding_count_; ++i) {
120 auto binding_num = bindings_[i].binding;
John Zulaufc483f442017-12-15 14:02:06 -0700121 auto final_index = global_index + bindings_[i].descriptorCount;
122 binding_to_global_index_range_map_[binding_num] = IndexRange(global_index, final_index);
John Zulaufb6d71202017-12-22 16:47:09 -0700123 if (final_index != global_index) {
124 global_start_to_index_map_[global_index] = i;
125 }
John Zulaufc483f442017-12-15 14:02:06 -0700126 global_index = final_index;
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700127 }
John Zulaufb6d71202017-12-22 16:47:09 -0700128
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700129 // Now create dyn offset array mapping for any dynamic descriptors
130 uint32_t dyn_array_idx = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700131 binding_to_dynamic_array_idx_map_.reserve(binding_to_dyn_count.size());
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700132 for (const auto &bc_pair : binding_to_dyn_count) {
133 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
134 dyn_array_idx += bc_pair.second;
135 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600136}
Tobin Ehlis154c2692016-10-25 09:36:53 -0600137
John Zulaufd47d0612018-02-16 13:00:34 -0700138size_t cvdescriptorset::DescriptorSetLayoutDef::hash() const {
139 hash_util::HashCombiner hc;
140 hc << flags_;
141 hc.Combine(bindings_);
John Zulauf223b69d2018-11-09 16:00:59 -0700142 hc.Combine(binding_flags_);
John Zulaufd47d0612018-02-16 13:00:34 -0700143 return hc.Value();
144}
145//
146
John Zulauf1f8174b2018-02-16 12:58:37 -0700147// Return valid index or "end" i.e. binding_count_;
148// The asserts in "Get" are reduced to the set where no valid answer(like null or 0) could be given
149// Common code for all binding lookups.
150uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromBinding(uint32_t binding) const {
151 const auto &bi_itr = binding_to_index_map_.find(binding);
152 if (bi_itr != binding_to_index_map_.cend()) return bi_itr->second;
153 return GetBindingCount();
154}
155VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorSetLayoutBindingPtrFromIndex(
156 const uint32_t index) const {
157 if (index >= bindings_.size()) return nullptr;
158 return bindings_[index].ptr();
159}
160// Return descriptorCount for given index, 0 if index is unavailable
161uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorCountFromIndex(const uint32_t index) const {
162 if (index >= bindings_.size()) return 0;
163 return bindings_[index].descriptorCount;
164}
165// For the given index, return descriptorType
166VkDescriptorType cvdescriptorset::DescriptorSetLayoutDef::GetTypeFromIndex(const uint32_t index) const {
167 assert(index < bindings_.size());
168 if (index < bindings_.size()) return bindings_[index].descriptorType;
169 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
170}
171// For the given index, return stageFlags
172VkShaderStageFlags cvdescriptorset::DescriptorSetLayoutDef::GetStageFlagsFromIndex(const uint32_t index) const {
173 assert(index < bindings_.size());
174 if (index < bindings_.size()) return bindings_[index].stageFlags;
175 return VkShaderStageFlags(0);
176}
Jeff Bolzfdf96072018-04-10 14:32:18 -0500177// Return binding flags for given index, 0 if index is unavailable
178VkDescriptorBindingFlagsEXT cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorBindingFlagsFromIndex(
179 const uint32_t index) const {
180 if (index >= binding_flags_.size()) return 0;
181 return binding_flags_[index];
182}
John Zulauf1f8174b2018-02-16 12:58:37 -0700183
184// For the given global index, return index
185uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromGlobalIndex(const uint32_t global_index) const {
186 auto start_it = global_start_to_index_map_.upper_bound(global_index);
187 uint32_t index = binding_count_;
188 assert(start_it != global_start_to_index_map_.cbegin());
189 if (start_it != global_start_to_index_map_.cbegin()) {
190 --start_it;
191 index = start_it->second;
192#ifndef NDEBUG
193 const auto &range = GetGlobalIndexRangeFromBinding(bindings_[index].binding);
194 assert(range.start <= global_index && global_index < range.end);
195#endif
196 }
197 return index;
198}
199
200// For the given binding, return the global index range
201// As start and end are often needed in pairs, get both with a single hash lookup.
202const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromBinding(
203 const uint32_t binding) const {
204 assert(binding_to_global_index_range_map_.count(binding));
205 // In error case max uint32_t so index is out of bounds to break ASAP
206 const static IndexRange kInvalidRange = {0xFFFFFFFF, 0xFFFFFFFF};
207 const auto &range_it = binding_to_global_index_range_map_.find(binding);
208 if (range_it != binding_to_global_index_range_map_.end()) {
209 return range_it->second;
210 }
211 return kInvalidRange;
212}
213
214// For given binding, return ptr to ImmutableSampler array
215VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
216 const auto &bi_itr = binding_to_index_map_.find(binding);
217 if (bi_itr != binding_to_index_map_.end()) {
218 return bindings_[bi_itr->second].pImmutableSamplers;
219 }
220 return nullptr;
221}
222// Move to next valid binding having a non-zero binding count
223uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetNextValidBinding(const uint32_t binding) const {
224 auto it = non_empty_bindings_.upper_bound(binding);
225 assert(it != non_empty_bindings_.cend());
226 if (it != non_empty_bindings_.cend()) return *it;
227 return GetMaxBinding() + 1;
228}
229// For given index, return ptr to ImmutableSampler array
230VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
231 if (index < bindings_.size()) {
232 return bindings_[index].pImmutableSamplers;
233 }
234 return nullptr;
235}
236// If our layout is compatible with rh_ds_layout, return true,
237// else return false and fill in error_msg will description of what causes incompatibility
238bool cvdescriptorset::DescriptorSetLayout::IsCompatible(DescriptorSetLayout const *const rh_ds_layout,
239 std::string *error_msg) const {
240 // Trivial case
241 if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) return true;
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600242 if (GetLayoutDef() == rh_ds_layout->GetLayoutDef()) return true;
John Zulaufd47d0612018-02-16 13:00:34 -0700243 bool detailed_compat_check =
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600244 GetLayoutDef()->IsCompatible(layout_, rh_ds_layout->GetDescriptorSetLayout(), rh_ds_layout->GetLayoutDef(), error_msg);
John Zulaufd47d0612018-02-16 13:00:34 -0700245 // The detailed check should never tell us mismatching DSL are compatible
246 assert(!detailed_compat_check);
247 return detailed_compat_check;
John Zulauf1f8174b2018-02-16 12:58:37 -0700248}
249
John Zulaufdf3c5c12018-03-06 16:44:43 -0700250// Do a detailed compatibility check of this def (referenced by ds_layout), vs. the rhs (layout and def)
251// Should only be called if trivial accept has failed, and in that context should return false.
John Zulauf1f8174b2018-02-16 12:58:37 -0700252bool cvdescriptorset::DescriptorSetLayoutDef::IsCompatible(VkDescriptorSetLayout ds_layout, VkDescriptorSetLayout rh_ds_layout,
253 DescriptorSetLayoutDef const *const rh_ds_layout_def,
254 std::string *error_msg) const {
255 if (descriptor_count_ != rh_ds_layout_def->descriptor_count_) {
256 std::stringstream error_str;
257 error_str << "DescriptorSetLayout " << ds_layout << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
258 << rh_ds_layout << ", which comes from pipelineLayout, has " << rh_ds_layout_def->descriptor_count_
259 << " descriptors.";
260 *error_msg = error_str.str();
261 return false; // trivial fail case
262 }
John Zulaufd47d0612018-02-16 13:00:34 -0700263
John Zulauf1f8174b2018-02-16 12:58:37 -0700264 // Descriptor counts match so need to go through bindings one-by-one
265 // and verify that type and stageFlags match
266 for (auto binding : bindings_) {
267 // TODO : Do we also need to check immutable samplers?
268 // VkDescriptorSetLayoutBinding *rh_binding;
269 if (binding.descriptorCount != rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding)) {
270 std::stringstream error_str;
271 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has a descriptorCount of "
272 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
273 << rh_ds_layout << ", which comes from pipelineLayout, has a descriptorCount of "
274 << rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding);
275 *error_msg = error_str.str();
276 return false;
277 } else if (binding.descriptorType != rh_ds_layout_def->GetTypeFromBinding(binding.binding)) {
278 std::stringstream error_str;
279 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " is type '"
280 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
281 << " for DescriptorSetLayout " << rh_ds_layout << ", which comes from pipelineLayout, is type '"
282 << string_VkDescriptorType(rh_ds_layout_def->GetTypeFromBinding(binding.binding)) << "'";
283 *error_msg = error_str.str();
284 return false;
285 } else if (binding.stageFlags != rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding)) {
286 std::stringstream error_str;
287 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has stageFlags "
288 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout " << rh_ds_layout
289 << ", which comes from pipelineLayout, has stageFlags "
290 << rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding);
291 *error_msg = error_str.str();
292 return false;
293 }
294 }
295 return true;
296}
297
298bool cvdescriptorset::DescriptorSetLayoutDef::IsNextBindingConsistent(const uint32_t binding) const {
299 if (!binding_to_index_map_.count(binding + 1)) return false;
300 auto const &bi_itr = binding_to_index_map_.find(binding);
301 if (bi_itr != binding_to_index_map_.end()) {
302 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
303 if (next_bi_itr != binding_to_index_map_.end()) {
304 auto type = bindings_[bi_itr->second].descriptorType;
305 auto stage_flags = bindings_[bi_itr->second].stageFlags;
306 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500307 auto flags = binding_flags_[bi_itr->second];
John Zulauf1f8174b2018-02-16 12:58:37 -0700308 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
309 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
Jeff Bolzfdf96072018-04-10 14:32:18 -0500310 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false)) ||
311 (flags != binding_flags_[next_bi_itr->second])) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700312 return false;
313 }
314 return true;
315 }
316 }
317 return false;
318}
319// Starting at offset descriptor of given binding, parse over update_count
320// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
321// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
322// If so, return true. If not, fill in error_msg and return false
323bool cvdescriptorset::DescriptorSetLayoutDef::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset,
324 uint32_t update_count, const char *type,
325 const VkDescriptorSet set, std::string *error_msg) const {
326 // Verify consecutive bindings match (if needed)
327 auto orig_binding = current_binding;
328 // Track count of descriptors in the current_bindings that are remaining to be updated
329 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
330 // First, it's legal to offset beyond your own binding so handle that case
331 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
332 while (offset >= binding_remaining) {
333 // Advance to next binding, decrement offset by binding size
334 offset -= binding_remaining;
335 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
336 }
337 binding_remaining -= offset;
338 while (update_count > binding_remaining) { // While our updates overstep current binding
339 // Verify next consecutive binding matches type, stage flags & immutable sampler use
340 if (!IsNextBindingConsistent(current_binding++)) {
341 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -0600342 error_str << "Attempting " << type;
343 if (IsPushDescriptor()) {
344 error_str << " push descriptors";
345 } else {
346 error_str << " descriptor set " << set;
347 }
348 error_str << " binding #" << orig_binding << " with #" << update_count
John Zulauf1f8174b2018-02-16 12:58:37 -0700349 << " descriptors being updated but this update oversteps the bounds of this binding and the next binding is "
350 "not consistent with current binding so this update is invalid.";
351 *error_msg = error_str.str();
352 return false;
353 }
354 // For sake of this check consider the bindings updated and grab count for next binding
355 update_count -= binding_remaining;
356 binding_remaining = GetDescriptorCountFromBinding(current_binding);
357 }
358 return true;
359}
360
361// The DescriptorSetLayout stores the per handle data for a descriptor set layout, and references the common defintion for the
362// handle invariant portion
363cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
364 const VkDescriptorSetLayout layout)
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600365 : layout_(layout), layout_destroyed_(false), layout_id_(GetCanonicalId(p_create_info)) {}
John Zulauf1f8174b2018-02-16 12:58:37 -0700366
Tobin Ehlis154c2692016-10-25 09:36:53 -0600367// Validate descriptor set layout create info
Jeff Bolzfdf96072018-04-10 14:32:18 -0500368bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(
369 const debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *create_info, const bool push_descriptor_ext,
370 const uint32_t max_push_descriptors, const bool descriptor_indexing_ext,
Jeff Bolze54ae892018-09-08 12:16:29 -0500371 const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features,
372 const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features,
373 const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props) {
Tobin Ehlis154c2692016-10-25 09:36:53 -0600374 bool skip = false;
375 std::unordered_set<uint32_t> bindings;
John Zulauf0fdeab32018-01-23 11:27:35 -0700376 uint64_t total_descriptors = 0;
377
Jeff Bolzfdf96072018-04-10 14:32:18 -0500378 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(create_info->pNext);
379
380 const bool push_descriptor_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR);
John Zulauf0fdeab32018-01-23 11:27:35 -0700381 if (push_descriptor_set && !push_descriptor_ext) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600382 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 -0600383 kVUID_Core_DrawState_ExtensionNotEnabled,
Mark Lobodzinskifb5a3e62018-04-13 10:46:48 -0600384 "Attempted to use %s in %s but its required extension %s has not been enabled.\n",
John Zulauf0fdeab32018-01-23 11:27:35 -0700385 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR", "VkDescriptorSetLayoutCreateInfo::flags",
386 VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
387 }
388
Jeff Bolzfdf96072018-04-10 14:32:18 -0500389 const bool update_after_bind_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT);
390 if (update_after_bind_set && !descriptor_indexing_ext) {
391 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 -0600392 kVUID_Core_DrawState_ExtensionNotEnabled,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500393 "Attemped to use %s in %s but its required extension %s has not been enabled.\n",
394 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT", "VkDescriptorSetLayoutCreateInfo::flags",
395 VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
396 }
397
John Zulauf0fdeab32018-01-23 11:27:35 -0700398 auto valid_type = [push_descriptor_set](const VkDescriptorType type) {
399 return !push_descriptor_set ||
Dave Houlton142c4cb2018-10-17 15:04:41 -0600400 ((type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && (type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) &&
Jeff Bolze54ae892018-09-08 12:16:29 -0500401 (type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT));
John Zulauf0fdeab32018-01-23 11:27:35 -0700402 };
403
Jeff Bolzfdf96072018-04-10 14:32:18 -0500404 uint32_t max_binding = 0;
405
Tobin Ehlis154c2692016-10-25 09:36:53 -0600406 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
John Zulauf0fdeab32018-01-23 11:27:35 -0700407 const auto &binding_info = create_info->pBindings[i];
Jeff Bolzfdf96072018-04-10 14:32:18 -0500408 max_binding = std::max(max_binding, binding_info.binding);
409
John Zulauf0fdeab32018-01-23 11:27:35 -0700410 if (!bindings.insert(binding_info.binding).second) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600411 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 -0600412 "VUID-VkDescriptorSetLayoutCreateInfo-binding-00279",
413 "duplicated binding number in VkDescriptorSetLayoutBinding.");
Tobin Ehlis154c2692016-10-25 09:36:53 -0600414 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700415 if (!valid_type(binding_info.descriptorType)) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600416 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 -0600417 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
418 ? "VUID-VkDescriptorSetLayoutCreateInfo-flags-02208"
419 : "VUID-VkDescriptorSetLayoutCreateInfo-flags-00280",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600420 "invalid type %s ,for push descriptors in VkDescriptorSetLayoutBinding entry %" PRIu32 ".",
421 string_VkDescriptorType(binding_info.descriptorType), i);
John Zulauf0fdeab32018-01-23 11:27:35 -0700422 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500423
424 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
425 if ((binding_info.descriptorCount % 4) != 0) {
426 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
427 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02209",
Dave Houlton142c4cb2018-10-17 15:04:41 -0600428 "descriptorCount =(%" PRIu32 ") must be a multiple of 4", binding_info.descriptorCount);
Jeff Bolze54ae892018-09-08 12:16:29 -0500429 }
430 if (binding_info.descriptorCount > inline_uniform_block_props->maxInlineUniformBlockSize) {
431 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
432 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02210",
433 "descriptorCount =(%" PRIu32 ") must be less than or equal to maxInlineUniformBlockSize",
434 binding_info.descriptorCount);
435 }
436 }
437
John Zulauf0fdeab32018-01-23 11:27:35 -0700438 total_descriptors += binding_info.descriptorCount;
Tobin Ehlis154c2692016-10-25 09:36:53 -0600439 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700440
Jeff Bolzfdf96072018-04-10 14:32:18 -0500441 if (flags_create_info) {
442 if (flags_create_info->bindingCount != 0 && flags_create_info->bindingCount != create_info->bindingCount) {
443 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 -0600444 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-bindingCount-03002",
Jeff Bolzfdf96072018-04-10 14:32:18 -0500445 "VkDescriptorSetLayoutCreateInfo::bindingCount (%d) != "
446 "VkDescriptorSetLayoutBindingFlagsCreateInfoEXT::bindingCount (%d)",
447 create_info->bindingCount, flags_create_info->bindingCount);
448 }
449
450 if (flags_create_info->bindingCount == create_info->bindingCount) {
451 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
452 const auto &binding_info = create_info->pBindings[i];
453
454 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) {
455 if (!update_after_bind_set) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600456 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
457 "VUID-VkDescriptorSetLayoutCreateInfo-flags-03000",
458 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500459 }
460
461 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER &&
462 !descriptor_indexing_features->descriptorBindingUniformBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600463 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
464 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
465 "descriptorBindingUniformBufferUpdateAfterBind-03005",
466 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500467 }
468 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
469 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
470 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) &&
471 !descriptor_indexing_features->descriptorBindingSampledImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600472 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
473 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
474 "descriptorBindingSampledImageUpdateAfterBind-03006",
475 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500476 }
477 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE &&
478 !descriptor_indexing_features->descriptorBindingStorageImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600479 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
480 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
481 "descriptorBindingStorageImageUpdateAfterBind-03007",
482 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500483 }
484 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER &&
485 !descriptor_indexing_features->descriptorBindingStorageBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600486 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
487 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
488 "descriptorBindingStorageBufferUpdateAfterBind-03008",
489 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500490 }
491 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
492 !descriptor_indexing_features->descriptorBindingUniformTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600493 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
494 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
495 "descriptorBindingUniformTexelBufferUpdateAfterBind-03009",
496 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500497 }
498 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER &&
499 !descriptor_indexing_features->descriptorBindingStorageTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600500 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
501 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
502 "descriptorBindingStorageTexelBufferUpdateAfterBind-03010",
503 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500504 }
505 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT ||
506 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
507 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600508 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
509 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-None-03011",
510 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500511 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500512
513 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT &&
514 !inline_uniform_block_features->descriptorBindingInlineUniformBlockUpdateAfterBind) {
515 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 -0600516 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
517 "descriptorBindingInlineUniformBlockUpdateAfterBind-02211",
518 "Invalid flags (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) for "
519 "VkDescriptorSetLayoutBinding entry %" PRIu32
520 " with descriptorBindingInlineUniformBlockUpdateAfterBind not enabled",
521 i);
Jeff Bolze54ae892018-09-08 12:16:29 -0500522 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500523 }
524
525 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT) {
526 if (!descriptor_indexing_features->descriptorBindingUpdateUnusedWhilePending) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600527 skip |= log_msg(
528 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
529 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingUpdateUnusedWhilePending-03012",
530 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500531 }
532 }
533
534 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) {
535 if (!descriptor_indexing_features->descriptorBindingPartiallyBound) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600536 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
537 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingPartiallyBound-03013",
538 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500539 }
540 }
541
542 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT) {
543 if (binding_info.binding != max_binding) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600544 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
545 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03004",
546 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500547 }
548
549 if (!descriptor_indexing_features->descriptorBindingVariableDescriptorCount) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600550 skip |= log_msg(
551 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
552 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingVariableDescriptorCount-03014",
553 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500554 }
555 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
556 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600557 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
558 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03015",
559 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500560 }
561 }
562
563 if (push_descriptor_set &&
564 (flags_create_info->pBindingFlags[i] &
565 (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
566 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT))) {
567 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 -0600568 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-flags-03003",
569 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500570 }
571 }
572 }
573 }
574
John Zulauf0fdeab32018-01-23 11:27:35 -0700575 if ((push_descriptor_set) && (total_descriptors > max_push_descriptors)) {
576 const char *undefined = push_descriptor_ext ? "" : " -- undefined";
Dave Houltond8ed0212018-05-16 17:18:24 -0600577 skip |=
578 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
579 "VUID-VkDescriptorSetLayoutCreateInfo-flags-00281",
580 "for push descriptor, total descriptor count in layout (%" PRIu64
581 ") must not be greater than VkPhysicalDevicePushDescriptorPropertiesKHR::maxPushDescriptors (%" PRIu32 "%s).",
582 total_descriptors, max_push_descriptors, undefined);
John Zulauf0fdeab32018-01-23 11:27:35 -0700583 }
584
Tobin Ehlis154c2692016-10-25 09:36:53 -0600585 return skip;
586}
587
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600588cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
589 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
590
Tobin Ehlis93f22372016-10-12 14:34:12 -0600591cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500592 const std::shared_ptr<DescriptorSetLayout const> &layout, uint32_t variable_count,
593 layer_data *dev_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700594 : some_update_(false),
595 set_(set),
596 pool_state_(nullptr),
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600597 p_layout_(layout),
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700598 device_data_(dev_data),
Mark Lobodzinski57a44272019-02-27 12:40:50 -0700599 limits_(dev_data->phys_dev_props.limits),
Jeff Bolzfdf96072018-04-10 14:32:18 -0500600 variable_count_(variable_count) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700601 pool_state_ = GetDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600602 // Foreach binding, create default descriptors of given type
John Zulaufb6d71202017-12-22 16:47:09 -0700603 descriptors_.reserve(p_layout_->GetTotalDescriptorCount());
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600604 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
605 auto type = p_layout_->GetTypeFromIndex(i);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600606 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700607 case VK_DESCRIPTOR_TYPE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600608 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
609 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600610 if (immut_sampler) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700611 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600612 some_update_ = true; // Immutable samplers are updated at creation
613 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700614 descriptors_.emplace_back(new SamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700615 }
616 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600617 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700618 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600619 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
620 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600621 if (immut) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700622 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600623 some_update_ = true; // Immutable samplers are updated at creation
624 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700625 descriptors_.emplace_back(new ImageSamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700626 }
627 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600628 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700629 // ImageDescriptors
630 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
631 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
632 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600633 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700634 descriptors_.emplace_back(new ImageDescriptor(type));
635 break;
636 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
637 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600638 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700639 descriptors_.emplace_back(new TexelDescriptor(type));
640 break;
641 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
642 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
643 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
644 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600645 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700646 descriptors_.emplace_back(new BufferDescriptor(type));
647 break;
Jeff Bolze54ae892018-09-08 12:16:29 -0500648 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
649 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
650 descriptors_.emplace_back(new InlineUniformDescriptor(type));
651 break;
Eric Werness30127fd2018-10-31 21:01:03 -0700652 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -0500653 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
654 descriptors_.emplace_back(new AccelerationStructureDescriptor(type));
655 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700656 default:
657 assert(0); // Bad descriptor type specified
658 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600659 }
660 }
661}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600662
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700663cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200664
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600665static std::string StringDescriptorReqViewType(descriptor_req req) {
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700666 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200667 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
668 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700669 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700670 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200671 }
672 }
673
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700674 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700675
676 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200677}
678
Chris Forbesda01e8d2018-08-27 15:36:57 -0700679static char const *StringDescriptorReqComponentType(descriptor_req req) {
680 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_SINT) return "SINT";
681 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_UINT) return "UINT";
682 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT) return "FLOAT";
683 return "(none)";
684}
685
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600686// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600687bool cvdescriptorset::DescriptorSet::IsCompatible(DescriptorSetLayout const *const layout, std::string *error) const {
688 return layout->IsCompatible(p_layout_.get(), error);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600689}
Chris Forbes57989132016-07-26 17:06:10 +1200690
Chris Forbesda01e8d2018-08-27 15:36:57 -0700691static unsigned DescriptorRequirementsBitsFromFormat(VkFormat fmt) {
692 if (FormatIsSInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
693 if (FormatIsUInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
694 if (FormatIsDepthAndStencil(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT | DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
695 if (fmt == VK_FORMAT_UNDEFINED) return 0;
696 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
697 return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
698}
699
Tobin Ehlis3066db62016-08-22 08:12:23 -0600700// 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 -0600701// This includes validating that all descriptors in the given bindings are updated,
702// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
703// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600704bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
John Zulauf48a6a702017-12-22 17:14:54 -0700705 const std::vector<uint32_t> &dynamic_offsets, GLOBAL_CB_NODE *cb_node,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600706 const char *caller, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200707 for (auto binding_pair : bindings) {
708 auto binding = binding_pair.first;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600709 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600710 std::stringstream error_str;
711 error_str << "Attempting to validate DrawState for binding #" << binding
712 << " which is an invalid binding for this descriptor set.";
713 *error = error_str.str();
714 return false;
715 }
John Zulaufc483f442017-12-15 14:02:06 -0700716 IndexRange index_range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700717 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Jeff Bolzfdf96072018-04-10 14:32:18 -0500718
719 if (IsVariableDescriptorCount(binding)) {
720 // Only validate the first N descriptors if it uses variable_count
721 index_range.end = index_range.start + GetVariableDescriptorCount();
722 }
723
John Zulaufc483f442017-12-15 14:02:06 -0700724 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
Tobin Ehlisda77de72018-12-13 08:53:28 -0700725 if ((p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
726 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) ||
Jeff Bolze54ae892018-09-08 12:16:29 -0500727 descriptors_[i]->GetClass() == InlineUniform) {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500728 // Can't validate the descriptor because it may not have been updated,
729 // or the view could have been destroyed
730 continue;
731 } else if (!descriptors_[i]->updated) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700732 std::stringstream error_str;
733 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
734 << " is being used in draw but has not been updated.";
735 *error = error_str.str();
736 return false;
737 } else {
738 auto descriptor_class = descriptors_[i]->GetClass();
739 if (descriptor_class == GeneralBuffer) {
740 // Verify that buffers are valid
741 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
742 auto buffer_node = GetBufferState(device_data_, buffer);
743 if (!buffer_node) {
744 std::stringstream error_str;
745 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
746 << " references invalid buffer " << buffer << ".";
747 *error = error_str.str();
748 return false;
John Zulauf48a6a702017-12-22 17:14:54 -0700749 } else if (!buffer_node->sparse) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700750 for (auto mem_binding : buffer_node->GetBoundMemory()) {
751 if (!GetMemObjInfo(device_data_, mem_binding)) {
752 std::stringstream error_str;
753 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
754 << " uses buffer " << buffer << " that references invalid memory " << mem_binding << ".";
755 *error = error_str.str();
Tobin Ehlisc8266452017-04-07 12:20:30 -0600756 return false;
757 }
758 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700759 }
760 if (descriptors_[i]->IsDynamic()) {
761 // Validate that dynamic offsets are within the buffer
762 auto buffer_size = buffer_node->createInfo.size;
763 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
764 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
765 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
766 if (VK_WHOLE_SIZE == range) {
767 if ((dyn_offset + desc_offset) > buffer_size) {
768 std::stringstream error_str;
769 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
770 << " uses buffer " << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset "
771 << dyn_offset << " combined with offset " << desc_offset
772 << " that oversteps the buffer size of " << buffer_size << ".";
773 *error = error_str.str();
774 return false;
775 }
776 } else {
777 if ((dyn_offset + desc_offset + range) > buffer_size) {
778 std::stringstream error_str;
779 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
780 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
781 << " combined with offset " << desc_offset << " and range " << range
782 << " that oversteps the buffer size of " << buffer_size << ".";
783 *error = error_str.str();
784 return false;
785 }
786 }
787 }
788 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
789 VkImageView image_view;
790 VkImageLayout image_layout;
791 if (descriptor_class == ImageSampler) {
792 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
793 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
794 } else {
795 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
796 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
797 }
798 auto reqs = binding_pair.second;
799
800 auto image_view_state = GetImageViewState(device_data_, image_view);
Tobin Ehlis836a1372017-07-14 11:25:21 -0600801 if (nullptr == image_view_state) {
802 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
803 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
804 std::stringstream error_str;
805 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
806 << " is using imageView " << image_view << " that has been destroyed.";
807 *error = error_str.str();
808 return false;
809 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700810 auto image_view_ci = image_view_state->create_info;
811
812 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
813 // bad view type
814 std::stringstream error_str;
815 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600816 << " requires an image view of type " << StringDescriptorReqViewType(reqs) << " but got "
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700817 << string_VkImageViewType(image_view_ci.viewType) << ".";
818 *error = error_str.str();
819 return false;
820 }
821
Chris Forbesda01e8d2018-08-27 15:36:57 -0700822 auto format_bits = DescriptorRequirementsBitsFromFormat(image_view_ci.format);
823 if (!(reqs & format_bits)) {
824 // bad component type
825 std::stringstream error_str;
826 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i << " requires "
827 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
828 << string_VkFormat(image_view_ci.format) << ".";
829 *error = error_str.str();
830 return false;
831 }
832
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700833 auto image_node = GetImageState(device_data_, image_view_ci.image);
834 assert(image_node);
835 // Verify Image Layout
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700836 // Copy first mip level into sub_layers and loop over each mip level to verify layout
837 VkImageSubresourceLayers sub_layers;
838 sub_layers.aspectMask = image_view_ci.subresourceRange.aspectMask;
839 sub_layers.baseArrayLayer = image_view_ci.subresourceRange.baseArrayLayer;
840 sub_layers.layerCount = image_view_ci.subresourceRange.layerCount;
841 bool hit_error = false;
842 for (auto cur_level = image_view_ci.subresourceRange.baseMipLevel;
843 cur_level < image_view_ci.subresourceRange.levelCount; ++cur_level) {
844 sub_layers.mipLevel = cur_level;
Cort Stratton7df30962018-05-17 19:45:57 -0700845 // No "invalid layout" VUID required for this call, since the optimal_layout parameter is UNDEFINED.
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700846 VerifyImageLayout(device_data_, cb_node, image_node, sub_layers, image_layout, VK_IMAGE_LAYOUT_UNDEFINED,
Cort Stratton7df30962018-05-17 19:45:57 -0700847 caller, kVUIDUndefined, "VUID-VkDescriptorImageInfo-imageLayout-00344", &hit_error);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700848 if (hit_error) {
849 *error =
John Zulauf1d27e0a2018-11-05 10:12:48 -0700850 "Image layout specified at vkUpdateDescriptorSet* or vkCmdPushDescriptorSet* time "
John Zulaufb45fdc32018-10-12 15:14:17 -0600851 "doesn't match actual image layout at time descriptor is used. See previous error callback for "
852 "specific details.";
Chris Forbes57989132016-07-26 17:06:10 +1200853 return false;
854 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700855 }
856 // Verify Sample counts
857 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
858 std::stringstream error_str;
859 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
860 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
861 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
862 *error = error_str.str();
863 return false;
864 }
865 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
866 std::stringstream error_str;
867 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
868 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
869 *error = error_str.str();
870 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200871 }
Chris Forbese92dd1d2019-01-21 15:58:57 -0800872 } else if (descriptor_class == TexelBuffer) {
873 auto texel_buffer = static_cast<TexelDescriptor *>(descriptors_[i].get());
874 auto buffer_view = GetBufferViewState(device_data_, texel_buffer->GetBufferView());
875
876 auto reqs = binding_pair.second;
877 auto format_bits = DescriptorRequirementsBitsFromFormat(buffer_view->create_info.format);
878
879 if (!(reqs & format_bits)) {
880 // bad component type
881 std::stringstream error_str;
882 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i << " requires "
883 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
884 << string_VkFormat(buffer_view->create_info.format) << ".";
885 *error = error_str.str();
886 return false;
887 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600888 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600889 if (descriptor_class == ImageSampler || descriptor_class == PlainSampler) {
890 // Verify Sampler still valid
891 VkSampler sampler;
892 if (descriptor_class == ImageSampler) {
893 sampler = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetSampler();
894 } else {
895 sampler = static_cast<SamplerDescriptor *>(descriptors_[i].get())->GetSampler();
896 }
897 if (!ValidateSampler(sampler, device_data_)) {
898 std::stringstream error_str;
899 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
900 << " is using sampler " << sampler << " that has been destroyed.";
901 *error = error_str.str();
902 return false;
903 }
904 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600905 }
906 }
907 }
908 return true;
909}
Chris Forbes57989132016-07-26 17:06:10 +1200910
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600911// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600912uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600913 std::unordered_set<VkBuffer> *buffer_set,
914 std::unordered_set<VkImageView> *image_set) const {
915 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200916 for (auto binding_pair : bindings) {
917 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600918 // If a binding doesn't exist, skip it
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600919 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600920 continue;
921 }
John Zulaufc483f442017-12-15 14:02:06 -0700922 uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600923 if (descriptors_[start_idx]->IsStorage()) {
924 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600925 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600926 if (descriptors_[start_idx + i]->updated) {
927 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600928 num_updates++;
929 }
930 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600931 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600932 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600933 if (descriptors_[start_idx + i]->updated) {
934 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700935 auto bv_state = GetBufferViewState(device_data_, bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600936 if (bv_state) {
937 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600938 num_updates++;
939 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600940 }
941 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600942 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600943 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600944 if (descriptors_[start_idx + i]->updated) {
945 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600946 num_updates++;
947 }
948 }
949 }
950 }
951 }
952 return num_updates;
953}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600954// Set is being deleted or updates so invalidate all bound cmd buffers
955void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600956 core_validation::InvalidateCommandBuffers(device_data_, cb_bindings, {HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600957}
John Zulauf1d27e0a2018-11-05 10:12:48 -0700958
959// Loop through the write updates to do for a push descriptor set, ignoring dstSet
960void cvdescriptorset::DescriptorSet::PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds) {
961 assert(IsPushDescriptor());
962 for (uint32_t i = 0; i < write_count; i++) {
963 PerformWriteUpdate(&p_wds[i]);
964 }
965}
966
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600967// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600968void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700969 // Perform update on a per-binding basis as consecutive updates roll over to next binding
970 auto descriptors_remaining = update->descriptorCount;
971 auto binding_being_updated = update->dstBinding;
972 auto offset = update->dstArrayElement;
Tobin Ehlise16805c2017-08-09 09:10:37 -0600973 uint32_t update_index = 0;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700974 while (descriptors_remaining) {
975 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
John Zulaufc483f442017-12-15 14:02:06 -0700976 auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700977 // Loop over the updates for a single binding at a time
Tobin Ehlise16805c2017-08-09 09:10:37 -0600978 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
979 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700980 }
981 // Roll over to next binding in case of consecutive update
982 descriptors_remaining -= update_count;
983 offset = 0;
984 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600985 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700986 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600987
Jeff Bolzfdf96072018-04-10 14:32:18 -0500988 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
989 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
990 InvalidateBoundCmdBuffers();
991 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600992}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600993// Validate Copy update
994bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -0600995 const DescriptorSet *src_set, const char *func_name,
996 std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -0700997 // Verify dst layout still valid
998 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600999 *error_code = "VUID-VkCopyDescriptorSet-dstSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -07001000 string_sprintf(error_msg,
John Zulaufb45fdc32018-10-12 15:14:17 -06001001 "Cannot call %s to perform copy update on descriptor set dstSet 0x%" PRIxLEAST64
John Zulauf5dfd45c2018-01-17 11:06:34 -07001002 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
John Zulaufb45fdc32018-10-12 15:14:17 -06001003 func_name, HandleToUint64(set_), HandleToUint64(p_layout_->GetDescriptorSetLayout()));
John Zulauf5dfd45c2018-01-17 11:06:34 -07001004 return false;
1005 }
1006
1007 // Verify src layout still valid
1008 if (src_set->p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001009 *error_code = "VUID-VkCopyDescriptorSet-srcSet-parameter";
John Zulaufb45fdc32018-10-12 15:14:17 -06001010 string_sprintf(error_msg,
1011 "Cannot call %s to perform copy update of dstSet 0x%" PRIxLEAST64
1012 " from descriptor set srcSet 0x%" PRIxLEAST64
1013 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
1014 func_name, HandleToUint64(set_), HandleToUint64(src_set->set_),
1015 HandleToUint64(src_set->p_layout_->GetDescriptorSetLayout()));
John Zulauf5dfd45c2018-01-17 11:06:34 -07001016 return false;
1017 }
1018
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001019 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001020 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-00347";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001021 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001022 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001023 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001024 return false;
1025 }
1026 if (!src_set->HasBinding(update->srcBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001027 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-00345";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001028 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001029 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001030 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001031 return false;
1032 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001033 // Verify idle ds
1034 if (in_use.load() &&
1035 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1036 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1037 // TODO : Re-using Free Idle error code, need copy update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001038 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001039 std::stringstream error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001040 error_str << "Cannot call " << func_name << " to perform copy update on descriptor set " << set_
Jeff Bolzfdf96072018-04-10 14:32:18 -05001041 << " that is in use by a command buffer";
1042 *error_msg = error_str.str();
1043 return false;
1044 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001045 // src & dst set bindings are valid
1046 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -07001047 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001048 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
1049 // SRC update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001050 *error_code = "VUID-VkCopyDescriptorSet-srcArrayElement-00346";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001051 std::stringstream error_str;
1052 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
John Zulaufc483f442017-12-15 14:02:06 -07001053 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001054 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001055 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001056 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001057 return false;
1058 }
John Zulaufc483f442017-12-15 14:02:06 -07001059 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001060 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001061 // DST update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001062 *error_code = "VUID-VkCopyDescriptorSet-dstArrayElement-00348";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001063 std::stringstream error_str;
1064 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
John Zulaufc483f442017-12-15 14:02:06 -07001065 << " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001066 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001067 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001068 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001069 return false;
1070 }
1071 // Check that types match
Dave Houltond8ed0212018-05-16 17:18:24 -06001072 // TODO : Base default error case going from here is "VUID-VkAcquireNextImageInfoKHR-semaphore-parameter"2ba which covers all
1073 // consistency issues, need more fine-grained error codes
Dave Houlton00c154e2018-05-24 13:20:50 -06001074 *error_code = "VUID-VkCopyDescriptorSet-srcSet-00349";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001075 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001076 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001077 if (src_type != dst_type) {
1078 std::stringstream error_str;
1079 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
1080 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001081 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001082 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001083 return false;
1084 }
1085 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -06001086 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001087 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001088 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001089 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001090 return false;
1091 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001092
1093 if ((src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1094 !(GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001095 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01918";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001096 std::stringstream error_str;
1097 error_str << "If pname:srcSet's (" << update->srcSet
1098 << ") layout was created with the "
1099 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1100 "set, then pname:dstSet's ("
1101 << update->dstSet
1102 << ") layout must: also have been created with the "
1103 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1104 *error_msg = error_str.str();
1105 return false;
1106 }
1107
1108 if (!(src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1109 (GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001110 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01919";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001111 std::stringstream error_str;
1112 error_str << "If pname:srcSet's (" << update->srcSet
1113 << ") layout was created without the "
1114 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1115 "set, then pname:dstSet's ("
1116 << update->dstSet
1117 << ") layout must: also have been created without the "
1118 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1119 *error_msg = error_str.str();
1120 return false;
1121 }
1122
1123 if ((src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1124 !(GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001125 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01920";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001126 std::stringstream error_str;
1127 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1128 << ") was allocated was created "
1129 "with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1130 "set, then the descriptor pool from which pname:dstSet ("
1131 << update->dstSet
1132 << ") was allocated must: "
1133 "also have been created with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1134 *error_msg = error_str.str();
1135 return false;
1136 }
1137
1138 if (!(src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1139 (GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001140 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01921";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001141 std::stringstream error_str;
1142 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1143 << ") was allocated was created "
1144 "without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1145 "set, then the descriptor pool from which pname:dstSet ("
1146 << update->dstSet
1147 << ") was allocated must: "
1148 "also have been created without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1149 *error_msg = error_str.str();
1150 return false;
1151 }
1152
Jeff Bolze54ae892018-09-08 12:16:29 -05001153 if (src_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1154 if ((update->srcArrayElement % 4) != 0) {
1155 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02223";
1156 std::stringstream error_str;
1157 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1158 << "srcArrayElement " << update->srcArrayElement << " not a multiple of 4";
1159 *error_msg = error_str.str();
1160 return false;
1161 }
1162 if ((update->dstArrayElement % 4) != 0) {
1163 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02224";
1164 std::stringstream error_str;
1165 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1166 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1167 *error_msg = error_str.str();
1168 return false;
1169 }
1170 if ((update->descriptorCount % 4) != 0) {
1171 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02225";
1172 std::stringstream error_str;
1173 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1174 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1175 *error_msg = error_str.str();
1176 return false;
1177 }
1178 }
1179
Tobin Ehlisd41e7b62016-05-19 07:56:18 -06001180 // Update parameters all look good and descriptor updated so verify update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06001181 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 -06001182
1183 // All checks passed so update is good
1184 return true;
1185}
1186// Perform Copy update
1187void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -07001188 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
1189 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001190 // Update parameters all look good so perform update
1191 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001192 auto src = src_set->descriptors_[src_start_idx + di].get();
1193 auto dst = descriptors_[dst_start_idx + di].get();
1194 if (src->updated) {
1195 dst->CopyUpdate(src);
1196 some_update_ = true;
1197 } else {
1198 dst->updated = false;
1199 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001200 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001201
Jeff Bolzfdf96072018-04-10 14:32:18 -05001202 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1203 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1204 InvalidateBoundCmdBuffers();
1205 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001206}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001207
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001208// Update the drawing state for the affected descriptors.
1209// Set cb_node to this set and this set to cb_node.
1210// Add the bindings of the descriptor
1211// Set the layout based on the current descriptor layout (will mask subsequent layer mismatch errors)
1212// TODO: Modify the UpdateDrawState virtural functions to *only* set initial layout and not change layouts
Tobin Ehlisf9519102016-08-17 09:49:13 -06001213// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
1214// to be used in a draw by the given cb_node
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001215void cvdescriptorset::DescriptorSet::UpdateDrawState(GLOBAL_CB_NODE *cb_node,
1216 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001217 // bind cb to this descriptor set
1218 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001219 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Petr Krausbc7f5442017-05-14 23:43:38 +02001220 cb_node->object_bindings.insert({HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001221 pool_state_->cb_bindings.insert(cb_node);
Petr Krausbc7f5442017-05-14 23:43:38 +02001222 cb_node->object_bindings.insert({HandleToUint64(pool_state_->pool), kVulkanObjectTypeDescriptorPool});
Tobin Ehlisf9519102016-08-17 09:49:13 -06001223 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
1224 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -07001225 for (auto binding_req_pair : binding_req_map) {
1226 auto binding = binding_req_pair.first;
Tony-LunarG62c5dba2018-12-20 14:27:23 -07001227 // We aren't validating descriptors created with PARTIALLY_BOUND or UPDATE_AFTER_BIND, so don't record state
1228 if (p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
1229 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) {
1230 continue;
1231 }
John Zulaufc483f442017-12-15 14:02:06 -07001232 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
1233 for (uint32_t i = range.start; i < range.end; ++i) {
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001234 descriptors_[i]->UpdateDrawState(device_data_, cb_node);
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001235 }
1236 }
1237}
1238
John Zulauf48a6a702017-12-22 17:14:54 -07001239void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1240 const BindingReqMap &in_req, BindingReqMap *out_req,
1241 TrackedBindings *bindings) {
1242 assert(out_req);
1243 assert(bindings);
1244 const auto binding = binding_req_pair.first;
1245 // Use insert and look at the boolean ("was inserted") in the returned pair to see if this is a new set member.
1246 // Saves one hash lookup vs. find ... compare w/ end ... insert.
1247 const auto it_bool_pair = bindings->insert(binding);
1248 if (it_bool_pair.second) {
1249 out_req->emplace(binding_req_pair);
1250 }
1251}
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001252
John Zulauf48a6a702017-12-22 17:14:54 -07001253void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1254 const BindingReqMap &in_req, BindingReqMap *out_req,
1255 TrackedBindings *bindings, uint32_t limit) {
1256 if (bindings->size() < limit) FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, bindings);
1257}
1258
1259void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, const BindingReqMap &in_req,
1260 BindingReqMap *out_req) {
1261 TrackedBindings &bound = cached_validation_[cb_state].command_binding_and_usage;
1262 if (bound.size() == GetBindingCount()) {
1263 return; // All bindings are bound, out req is empty
1264 }
1265 for (const auto &binding_req_pair : in_req) {
1266 const auto binding = binding_req_pair.first;
1267 // If a binding doesn't exist, or has already been bound, skip it
1268 if (p_layout_->HasBinding(binding)) {
1269 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, &bound);
1270 }
1271 }
1272}
1273
1274void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline,
1275 const BindingReqMap &in_req, BindingReqMap *out_req) {
1276 auto &validated = cached_validation_[cb_state];
1277 auto &image_sample_val = validated.image_samplers[pipeline];
1278 auto *const dynamic_buffers = &validated.dynamic_buffers;
1279 auto *const non_dynamic_buffers = &validated.non_dynamic_buffers;
1280 const auto &stats = p_layout_->GetBindingTypeStats();
1281 for (const auto &binding_req_pair : in_req) {
1282 auto binding = binding_req_pair.first;
1283 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
1284 if (!layout_binding) {
1285 continue;
1286 }
1287 // Caching criteria differs per type.
1288 // If image_layout have changed , the image descriptors need to be validated against them.
1289 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1290 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
1291 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, dynamic_buffers, stats.dynamic_buffer_count);
1292 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1293 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
1294 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
1295 } else {
1296 // This is rather crude, as the changed layouts may not impact the bound descriptors,
1297 // but the simple "versioning" is a simple "dirt" test.
1298 auto &version = image_sample_val[binding]; // Take advantage of default construtor zero initialzing new entries
1299 if (version != cb_state->image_layout_change_count) {
1300 version = cb_state->image_layout_change_count;
1301 out_req->emplace(binding_req_pair);
1302 }
1303 }
1304 }
1305}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001306
Tobin Ehlis300888c2016-05-18 13:43:26 -06001307cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001308 updated = false;
1309 descriptor_class = PlainSampler;
1310 if (immut) {
1311 sampler_ = *immut;
1312 immutable_ = true;
1313 updated = true;
1314 }
1315}
Tobin Ehlise2f80292016-06-02 10:08:53 -06001316// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001317bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const layer_data *dev_data) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001318 return (GetSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001319}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001320
Tobin Ehlis554bf382016-05-24 11:14:43 -06001321bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
John Zulaufb45fdc32018-10-12 15:14:17 -06001322 const layer_data *dev_data, const char *func_name, std::string *error_code,
1323 std::string *error_msg) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001324 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00326";
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001325 auto iv_state = GetImageViewState(dev_data, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001326 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001327 std::stringstream error_str;
1328 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001329 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001330 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001331 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001332 // 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 -06001333 // Validate that imageLayout is compatible with aspect_mask and image format
1334 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001335 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
1336 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001337 VkFormat format = VK_FORMAT_MAX_ENUM;
1338 VkImageUsageFlags usage = 0;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001339 auto image_node = GetImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001340 if (image_node) {
1341 format = image_node->createInfo.format;
1342 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001343 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001344 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
1345 // the error here occurs is if memory bound to a created imageView has been freed.
John Zulaufb45fdc32018-10-12 15:14:17 -06001346 if (ValidateMemoryIsBoundToImage(dev_data, image_node, func_name, "VUID-VkImageViewCreateInfo-image-01020")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001347 *error_code = "VUID-VkImageViewCreateInfo-image-01020";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001348 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001349 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001350 }
Chris Forbes67757ff2017-07-21 13:59:01 -07001351
1352 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
1353 // but not binding them to descriptor sets.
1354 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D &&
1355 (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
1356 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001357 *error_code = "VUID-VkDescriptorImageInfo-imageView-00343";
Chris Forbes67757ff2017-07-21 13:59:01 -07001358 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
1359 return false;
1360 }
Tobin Ehlis1809f912016-05-25 09:24:36 -06001361 }
1362 // First validate that format and layout are compatible
1363 if (format == VK_FORMAT_MAX_ENUM) {
1364 std::stringstream error_str;
1365 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001366 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001367 return false;
1368 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001369 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
1370 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -06001371 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -06001372 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001373 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1374 // Only Color bit must be set
1375 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001376 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -07001377 error_str
1378 << "ImageView (" << image_view
1379 << ") 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 -06001380 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001381 return false;
1382 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001383 // format must NOT be DS
1384 if (ds) {
1385 std::stringstream error_str;
1386 error_str << "ImageView (" << image_view
1387 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1388 << string_VkFormat(format) << " which is not a color format.";
1389 *error_msg = error_str.str();
1390 return false;
1391 }
1392 break;
1393 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1394 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1395 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001396 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1397 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1398 // both must NOT be set
1399 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001400 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001401 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001402 return false;
1403 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001404 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1405 // Neither were set
1406 std::stringstream error_str;
1407 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1408 << " but does not have STENCIL or DEPTH aspects set";
1409 *error_msg = error_str.str();
1410 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001411 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001412 // format must be DS
1413 if (!ds) {
1414 std::stringstream error_str;
1415 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1416 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
1417 *error_msg = error_str.str();
1418 return false;
1419 }
1420 break;
1421 default:
1422 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1423 if (ds) {
1424 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1425 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1426 // both must NOT be set
1427 std::stringstream error_str;
1428 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1429 << " and is using depth/stencil image of format " << string_VkFormat(format)
1430 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1431 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1432 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1433 "reads respectively.";
1434 *error_msg = error_str.str();
1435 return false;
1436 }
1437 }
1438 }
1439 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001440 }
1441 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001442 // 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 -06001443 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1444 // under vkCreateImage()
Dave Houltond8ed0212018-05-16 17:18:24 -06001445 // TODO : Need to also validate case "VUID-VkWriteDescriptorSet-descriptorType-00336" where STORAGE_IMAGE & INPUT_ATTACH types
1446 // must have been created with identify swizzle
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001447 const char *error_usage_bit = nullptr;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001448 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001449 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1450 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1451 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1452 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
1453 }
1454 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001455 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001456 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1457 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1458 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
1459 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
1460 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001461 // TODO : Need to create custom enum error codes for these cases
1462 if (image_node->shared_presentable) {
1463 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001464 error_str << "ImageView (" << image_view
1465 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
1466 "layout "
1467 << string_VkImageLayout(image_layout)
1468 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report "
1469 "support for VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the "
1470 "VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001471 *error_msg = error_str.str();
1472 return false;
1473 }
1474 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001475 error_str << "ImageView (" << image_view
1476 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
1477 << string_VkImageLayout(image_layout)
1478 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage "
1479 "images can only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001480 *error_msg = error_str.str();
1481 return false;
1482 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001483 }
1484 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001485 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001486 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
1487 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
1488 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
1489 }
1490 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001491 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001492 default:
1493 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001494 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001495 if (error_usage_bit) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001496 std::stringstream error_str;
1497 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
1498 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1499 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001500 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001501 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001502 }
John Zulauff4c07882019-01-24 14:03:36 -07001503
1504 if ((type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) || (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)) {
1505 // Test that the layout is compatible with the descriptorType for the two sampled image types
1506 const static std::array<VkImageLayout, 3> valid_layouts = {
1507 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL};
1508
1509 struct ExtensionLayout {
1510 VkImageLayout layout;
1511 bool DeviceExtensions::*extension;
1512 };
1513
1514 const static std::array<ExtensionLayout, 3> extended_layouts{
1515 {// Note double brace req'd for aggregate initialization
1516 {VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR, &DeviceExtensions::vk_khr_shared_presentable_image},
1517 {VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2},
1518 {VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2}}};
1519 auto is_layout = [image_layout, dev_data](const ExtensionLayout &ext_layout) {
1520 return dev_data->extensions.*(ext_layout.extension) && (ext_layout.layout == image_layout);
1521 };
1522
1523 bool valid_layout = (std::find(valid_layouts.cbegin(), valid_layouts.cend(), image_layout) != valid_layouts.cend()) ||
1524 std::any_of(extended_layouts.cbegin(), extended_layouts.cend(), is_layout);
1525
1526 if (!valid_layout) {
1527 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01403";
1528 std::stringstream error_str;
1529 error_str << "Descriptor update with descriptorType " << string_VkDescriptorType(type)
1530 << " is being updated with invalid imageLayout " << string_VkImageLayout(image_layout)
1531 << ". Allowed layouts are: VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, "
1532 << "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL";
1533 for (auto &ext_layout : extended_layouts) {
1534 if (dev_data->extensions.*(ext_layout.extension)) {
1535 error_str << ", " << string_VkImageLayout(ext_layout.layout);
1536 }
1537 }
1538 *error_msg = error_str.str();
1539 return false;
1540 }
1541 }
1542
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001543 return true;
1544}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001545
Tobin Ehlis300888c2016-05-18 13:43:26 -06001546void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Chris Forbesfea2c542018-04-13 09:34:15 -07001547 if (!immutable_) {
1548 sampler_ = update->pImageInfo[index].sampler;
1549 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001550 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001551}
1552
Tobin Ehlis300888c2016-05-18 13:43:26 -06001553void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001554 if (!immutable_) {
1555 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001556 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001557 }
1558 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001559}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001560
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001561void cvdescriptorset::SamplerDescriptor::UpdateDrawState(layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001562 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001563 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001564 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001565 }
1566}
1567
Tobin Ehlis300888c2016-05-18 13:43:26 -06001568cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001569 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001570 updated = false;
1571 descriptor_class = ImageSampler;
1572 if (immut) {
1573 sampler_ = *immut;
1574 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001575 }
1576}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001577
Tobin Ehlis300888c2016-05-18 13:43:26 -06001578void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001579 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001580 const auto &image_info = update->pImageInfo[index];
Chris Forbesfea2c542018-04-13 09:34:15 -07001581 if (!immutable_) {
1582 sampler_ = image_info.sampler;
1583 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001584 image_view_ = image_info.imageView;
1585 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001586}
1587
Tobin Ehlis300888c2016-05-18 13:43:26 -06001588void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001589 if (!immutable_) {
1590 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001591 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001592 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001593 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1594 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001595 updated = true;
1596 image_view_ = image_view;
1597 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001598}
1599
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001600void cvdescriptorset::ImageSamplerDescriptor::UpdateDrawState(layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001601 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001602 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001603 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001604 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001605 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001606 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001607 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001608 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001609 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001610 }
Jeff Bolz148d94e2018-12-13 21:25:56 -06001611 if (image_view_) {
1612 SetImageViewLayout(dev_data, cb_node, image_view_, image_layout_);
1613 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001614}
1615
Tobin Ehlis300888c2016-05-18 13:43:26 -06001616cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1617 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001618 updated = false;
1619 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001620 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001621}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001622
Tobin Ehlis300888c2016-05-18 13:43:26 -06001623void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001624 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001625 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001626 image_view_ = image_info.imageView;
1627 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001628}
1629
Tobin Ehlis300888c2016-05-18 13:43:26 -06001630void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001631 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1632 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001633 updated = true;
1634 image_view_ = image_view;
1635 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001636}
1637
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001638void cvdescriptorset::ImageDescriptor::UpdateDrawState(layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001639 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001640 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001641 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001642 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001643 }
Jeff Bolz148d94e2018-12-13 21:25:56 -06001644 if (image_view_) {
1645 SetImageViewLayout(dev_data, cb_node, image_view_, image_layout_);
1646 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001647}
1648
Tobin Ehlis300888c2016-05-18 13:43:26 -06001649cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1650 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001651 updated = false;
1652 descriptor_class = GeneralBuffer;
1653 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1654 dynamic_ = true;
1655 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1656 storage_ = true;
1657 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1658 dynamic_ = true;
1659 storage_ = true;
1660 }
1661}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001662void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001663 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001664 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001665 buffer_ = buffer_info.buffer;
1666 offset_ = buffer_info.offset;
1667 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001668}
1669
Tobin Ehlis300888c2016-05-18 13:43:26 -06001670void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1671 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001672 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001673 buffer_ = buff_desc->buffer_;
1674 offset_ = buff_desc->offset_;
1675 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001676}
1677
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001678void cvdescriptorset::BufferDescriptor::UpdateDrawState(layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001679 auto buffer_node = GetBufferState(dev_data, buffer_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001680 if (buffer_node) core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001681}
1682
Tobin Ehlis300888c2016-05-18 13:43:26 -06001683cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001684 updated = false;
1685 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001686 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001687}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001688
Tobin Ehlis300888c2016-05-18 13:43:26 -06001689void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001690 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001691 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001692}
1693
Tobin Ehlis300888c2016-05-18 13:43:26 -06001694void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1695 updated = true;
1696 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1697}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001698
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001699void cvdescriptorset::TexelDescriptor::UpdateDrawState(layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001700 auto bv_state = GetBufferViewState(dev_data, buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001701 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001702 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001703 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001704}
1705
Tobin Ehlis300888c2016-05-18 13:43:26 -06001706// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1707// sets, and then calls their respective Validate[Write|Copy]Update functions.
1708// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1709// be skipped, then true is returned.
1710// If there is no issue with the update, then false is returned.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001711bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data, const layer_data *dev_data,
1712 uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
John Zulaufb45fdc32018-10-12 15:14:17 -06001713 const VkCopyDescriptorSet *p_cds, const char *func_name) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001714 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001715 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001716 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001717 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001718 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001719 if (!set_node) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001720 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1721 HandleToUint64(dest_set), kVUID_Core_DrawState_InvalidDescriptorSet,
1722 "Cannot call %s on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.", func_name,
1723 HandleToUint64(dest_set));
Tobin Ehlis300888c2016-05-18 13:43:26 -06001724 } else {
Dave Houltond8ed0212018-05-16 17:18:24 -06001725 std::string error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001726 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001727 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], func_name, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001728 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 -06001729 HandleToUint64(dest_set), error_code,
John Zulaufb45fdc32018-10-12 15:14:17 -06001730 "%s failed write update validation for Descriptor Set 0x%" PRIx64 " with error: %s.", func_name,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001731 HandleToUint64(dest_set), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001732 }
1733 }
1734 }
1735 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001736 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001737 auto dst_set = p_cds[i].dstSet;
1738 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001739 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1740 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001741 // Object_tracker verifies that src & dest descriptor set are valid
1742 assert(src_node);
1743 assert(dst_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001744 std::string error_code;
Tobin Ehlisa1712752017-01-04 09:41:47 -07001745 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001746 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, func_name, &error_code, &error_str)) {
1747 skip |=
1748 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1749 HandleToUint64(dst_set), error_code,
1750 "%s failed copy update from Descriptor Set 0x%" PRIx64 " to Descriptor Set 0x%" PRIx64 " with error: %s.",
1751 func_name, HandleToUint64(src_set), HandleToUint64(dst_set), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001752 }
1753 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001754 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001755}
1756// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1757// sets, and then calls their respective Perform[Write|Copy]Update functions.
1758// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1759// with the same set of updates.
1760// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1761// calling down the chain.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001762void cvdescriptorset::PerformUpdateDescriptorSets(const layer_data *dev_data, uint32_t write_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001763 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1764 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001765 // Write updates first
1766 uint32_t i = 0;
1767 for (i = 0; i < write_count; ++i) {
1768 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001769 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001770 if (set_node) {
1771 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001772 }
1773 }
1774 // Now copy updates
1775 for (i = 0; i < copy_count; ++i) {
1776 auto dst_set = p_cds[i].dstSet;
1777 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001778 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1779 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001780 if (src_node && dst_node) {
1781 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001782 }
1783 }
1784}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001785
John Zulaufb845eb22018-10-12 11:41:06 -06001786cvdescriptorset::DecodedTemplateUpdate::DecodedTemplateUpdate(layer_data *device_data, VkDescriptorSet descriptorSet,
John Zulauf1d27e0a2018-11-05 10:12:48 -07001787 const TEMPLATE_STATE *template_state, const void *pData,
1788 VkDescriptorSetLayout push_layout) {
John Zulaufb845eb22018-10-12 11:41:06 -06001789 auto const &create_info = template_state->create_info;
1790 inline_infos.resize(create_info.descriptorUpdateEntryCount); // Make sure we have one if we need it
1791 desc_writes.reserve(create_info.descriptorUpdateEntryCount); // emplaced, so reserved without initialization
John Zulauf1d27e0a2018-11-05 10:12:48 -07001792 VkDescriptorSetLayout effective_dsl = create_info.templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
1793 ? create_info.descriptorSetLayout
1794 : push_layout;
1795 auto layout_obj = GetDescriptorSetLayout(device_data, effective_dsl);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001796
1797 // Create a WriteDescriptorSet struct for each template update entry
1798 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1799 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1800 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1801 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1802
John Zulaufb6d71202017-12-22 16:47:09 -07001803 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001804 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1805 desc_writes.emplace_back();
1806 auto &write_entry = desc_writes.back();
1807
1808 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1809 char *update_entry = (char *)(pData) + offset;
1810
1811 if (dst_array_element >= binding_count) {
1812 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001813 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001814 }
1815
1816 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1817 write_entry.pNext = NULL;
1818 write_entry.dstSet = descriptorSet;
1819 write_entry.dstBinding = binding_being_updated;
1820 write_entry.dstArrayElement = dst_array_element;
1821 write_entry.descriptorCount = 1;
1822 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1823
1824 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1825 case VK_DESCRIPTOR_TYPE_SAMPLER:
1826 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1827 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1828 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1829 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1830 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1831 break;
1832
1833 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1834 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1835 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1836 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1837 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1838 break;
1839
1840 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1841 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1842 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1843 break;
Dave Houlton142c4cb2018-10-17 15:04:41 -06001844 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1845 VkWriteDescriptorSetInlineUniformBlockEXT *inline_info = &inline_infos[i];
1846 inline_info->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT;
1847 inline_info->pNext = nullptr;
1848 inline_info->dataSize = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1849 inline_info->pData = update_entry;
1850 write_entry.pNext = inline_info;
1851 // skip the rest of the array, they just represent bytes in the update
1852 j = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1853 break;
1854 }
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001855 default:
1856 assert(0);
1857 break;
1858 }
1859 dst_array_element++;
1860 }
1861 }
John Zulaufb845eb22018-10-12 11:41:06 -06001862}
John Zulaufb45fdc32018-10-12 15:14:17 -06001863// These helper functions carry out the validate and record descriptor updates peformed via update templates. They decode
1864// the templatized data and leverage the non-template UpdateDescriptor helper functions.
1865bool cvdescriptorset::ValidateUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet,
1866 const TEMPLATE_STATE *template_state, const void *pData) {
1867 // Translate the templated update into a normal update for validation...
1868 DecodedTemplateUpdate decoded_update(device_data, descriptorSet, template_state, pData);
1869 return ValidateUpdateDescriptorSets(GetReportData(device_data), device_data,
1870 static_cast<uint32_t>(decoded_update.desc_writes.size()), decoded_update.desc_writes.data(),
1871 0, NULL, "vkUpdateDescriptorSetWithTemplate()");
1872}
John Zulaufb845eb22018-10-12 11:41:06 -06001873
John Zulaufb845eb22018-10-12 11:41:06 -06001874void cvdescriptorset::PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet,
John Zulaufb45fdc32018-10-12 15:14:17 -06001875 const TEMPLATE_STATE *template_state, const void *pData) {
1876 // Translate the templated update into a normal update for validation...
1877 DecodedTemplateUpdate decoded_update(device_data, descriptorSet, template_state, pData);
John Zulaufb845eb22018-10-12 11:41:06 -06001878 PerformUpdateDescriptorSets(device_data, static_cast<uint32_t>(decoded_update.desc_writes.size()),
1879 decoded_update.desc_writes.data(), 0, NULL);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001880}
John Zulauf4e7bcb52018-11-02 10:46:30 -06001881
1882std::string cvdescriptorset::DescriptorSet::StringifySetAndLayout() const {
1883 std::string out;
1884 uint64_t layout_handle = HandleToUint64(p_layout_->GetDescriptorSetLayout());
1885 if (IsPushDescriptor()) {
1886 string_sprintf(&out, "Push Descriptors defined with VkDescriptorSetLayout 0x%" PRIxLEAST64, layout_handle);
John Zulauf4e7bcb52018-11-02 10:46:30 -06001887 } else {
1888 string_sprintf(&out, "VkDescriptorSet 0x%" PRIxLEAST64 "allocated with VkDescriptorSetLayout 0x%" PRIxLEAST64,
1889 HandleToUint64(set_), layout_handle);
1890 }
1891 return out;
1892};
1893
John Zulauf1d27e0a2018-11-05 10:12:48 -07001894// Loop through the write updates to validate for a push descriptor set, ignoring dstSet
1895bool cvdescriptorset::DescriptorSet::ValidatePushDescriptorsUpdate(const debug_report_data *report_data, uint32_t write_count,
1896 const VkWriteDescriptorSet *p_wds, const char *func_name) {
1897 assert(IsPushDescriptor());
1898 bool skip = false;
1899 for (uint32_t i = 0; i < write_count; i++) {
1900 std::string error_code;
1901 std::string error_str;
1902 if (!ValidateWriteUpdate(report_data, &p_wds[i], func_name, &error_code, &error_str)) {
1903 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1904 HandleToUint64(p_layout_->GetDescriptorSetLayout()), error_code, "%s failed update validation: %s.",
1905 func_name, error_str.c_str());
1906 }
1907 }
1908 return skip;
1909}
1910
Tobin Ehlis300888c2016-05-18 13:43:26 -06001911// Validate the state for a given write update but don't actually perform the update
1912// If an error would occur for this update, return false and fill in details in error_msg string
1913bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -06001914 const char *func_name, std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001915 // Verify dst layout still valid
1916 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001917 *error_code = "VUID-VkWriteDescriptorSet-dstSet-00320";
John Zulauf4e7bcb52018-11-02 10:46:30 -06001918 string_sprintf(error_msg, "Cannot call %s to perform write update on %s which has been destroyed", func_name,
1919 StringifySetAndLayout().c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001920 return false;
1921 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001922 // Verify dst binding exists
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001923 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001924 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00315";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001925 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001926 error_str << StringifySetAndLayout() << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001927 *error_msg = error_str.str();
1928 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001929 } else {
1930 // Make sure binding isn't empty
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001931 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001932 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00316";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001933 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001934 error_str << StringifySetAndLayout() << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001935 *error_msg = error_str.str();
1936 return false;
1937 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001938 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001939 // Verify idle ds
1940 if (in_use.load() &&
1941 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1942 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1943 // TODO : Re-using Free Idle error code, need write update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001944 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001945 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001946 error_str << "Cannot call " << func_name << " to perform write update on " << StringifySetAndLayout()
Jeff Bolzfdf96072018-04-10 14:32:18 -05001947 << " that is in use by a command buffer";
1948 *error_msg = error_str.str();
1949 return false;
1950 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001951 // We know that binding is valid, verify update and do update on each descriptor
John Zulaufc483f442017-12-15 14:02:06 -07001952 auto start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001953 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001954 if (type != update->descriptorType) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001955 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00319";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001956 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001957 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with type "
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001958 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1959 *error_msg = error_str.str();
1960 return false;
1961 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001962 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001963 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001964 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001965 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001966 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001967 << " descriptors in that binding and all successive bindings of the set, but update of "
1968 << update->descriptorCount << " descriptors combined with update array element offset of "
1969 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001970 *error_msg = error_str.str();
1971 return false;
1972 }
Jeff Bolze54ae892018-09-08 12:16:29 -05001973 if (type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1974 if ((update->dstArrayElement % 4) != 0) {
1975 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02219";
1976 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001977 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001978 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1979 *error_msg = error_str.str();
1980 return false;
1981 }
1982 if ((update->descriptorCount % 4) != 0) {
1983 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02220";
1984 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001985 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001986 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1987 *error_msg = error_str.str();
1988 return false;
1989 }
1990 const auto *write_inline_info = lvl_find_in_chain<VkWriteDescriptorSetInlineUniformBlockEXT>(update->pNext);
1991 if (!write_inline_info || write_inline_info->dataSize != update->descriptorCount) {
1992 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02221";
1993 std::stringstream error_str;
1994 if (!write_inline_info) {
John Zulauf4e7bcb52018-11-02 10:46:30 -06001995 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
1996 << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001997 << "VkWriteDescriptorSetInlineUniformBlockEXT missing";
1998 } else {
John Zulauf4e7bcb52018-11-02 10:46:30 -06001999 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
2000 << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06002001 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2002 << " not equal to "
Jeff Bolze54ae892018-09-08 12:16:29 -05002003 << "VkWriteDescriptorSet descriptorCount " << update->descriptorCount;
2004 }
2005 *error_msg = error_str.str();
2006 return false;
2007 }
2008 // This error is probably unreachable due to the previous two errors
2009 if (write_inline_info && (write_inline_info->dataSize % 4) != 0) {
2010 *error_code = "VUID-VkWriteDescriptorSetInlineUniformBlockEXT-dataSize-02222";
2011 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002012 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06002013 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2014 << " not a multiple of 4";
Jeff Bolze54ae892018-09-08 12:16:29 -05002015 *error_msg = error_str.str();
2016 return false;
2017 }
2018 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002019 // Verify consecutive bindings match (if needed)
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06002020 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002021 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07002022 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Dave Houlton00c154e2018-05-24 13:20:50 -06002023 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002024 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002025 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002026 // Update is within bounds and consistent so last step is to validate update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06002027 if (!VerifyWriteUpdateContents(update, start_idx, func_name, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002028 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002029 error_str << "Write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002030 << " failed with error message: " << error_msg->c_str();
2031 *error_msg = error_str.str();
2032 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002033 }
2034 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002035 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06002036}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002037// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002038// 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 -07002039bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06002040 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002041 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06002042 auto usage = buffer_node->createInfo.usage;
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002043 const char *error_usage_bit = nullptr;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002044 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002045 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2046 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002047 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00334";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002048 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
2049 }
2050 break;
2051 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2052 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002053 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00335";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002054 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
2055 }
2056 break;
2057 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2058 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2059 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002060 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00330";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002061 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
2062 }
2063 break;
2064 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2065 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
2066 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002067 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00331";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002068 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
2069 }
2070 break;
2071 default:
2072 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002073 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002074 if (error_usage_bit) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002075 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002076 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
2077 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
2078 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002079 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002080 return false;
2081 }
2082 return true;
2083}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002084// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
2085// 1. buffer is valid
2086// 2. buffer was created with correct usage flags
2087// 3. offset is less than buffer size
2088// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002089// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002090// 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 -06002091bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
John Zulaufb45fdc32018-10-12 15:14:17 -06002092 const char *func_name, std::string *error_code,
2093 std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002094 // First make sure that buffer is valid
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002095 auto buffer_node = GetBufferState(device_data_, buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07002096 // Any invalid buffer should already be caught by object_tracker
2097 assert(buffer_node);
John Zulaufb45fdc32018-10-12 15:14:17 -06002098 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, func_name, "VUID-VkWriteDescriptorSet-descriptorType-00329")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002099 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00329";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002100 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06002101 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06002102 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002103 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002104 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
2105 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002106 return false;
2107 }
2108 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002109 if (buffer_info->offset >= buffer_node->createInfo.size) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002110 *error_code = "VUID-VkDescriptorBufferInfo-offset-00340";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002111 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002112 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
2113 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002114 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002115 return false;
2116 }
2117 if (buffer_info->range != VK_WHOLE_SIZE) {
2118 // Range must be VK_WHOLE_SIZE or > 0
2119 if (!buffer_info->range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002120 *error_code = "VUID-VkDescriptorBufferInfo-range-00341";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002121 std::stringstream error_str;
2122 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002123 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002124 return false;
2125 }
2126 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
2127 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002128 *error_code = "VUID-VkDescriptorBufferInfo-range-00342";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002129 std::stringstream error_str;
2130 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
2131 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002132 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002133 return false;
2134 }
2135 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002136 // Check buffer update sizes against device limits
2137 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
2138 auto max_ub_range = limits_.maxUniformBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002139 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002140 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002141 std::stringstream error_str;
2142 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2143 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
2144 *error_msg = error_str.str();
2145 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002146 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_ub_range) {
2147 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
2148 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002149 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2150 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002151 << "maxUniformBufferRange (" << max_ub_range << ")";
2152 *error_msg = error_str.str();
2153 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002154 }
2155 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
2156 auto max_sb_range = limits_.maxStorageBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002157 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002158 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002159 std::stringstream error_str;
2160 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2161 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
2162 *error_msg = error_str.str();
2163 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002164 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_sb_range) {
2165 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
2166 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002167 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2168 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002169 << "maxStorageBufferRange (" << max_sb_range << ")";
2170 *error_msg = error_str.str();
2171 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002172 }
2173 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002174 return true;
2175}
2176
Tobin Ehlis300888c2016-05-18 13:43:26 -06002177// Verify that the contents of the update are ok, but don't perform actual update
2178bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
John Zulaufb45fdc32018-10-12 15:14:17 -06002179 const char *func_name, std::string *error_code,
2180 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002181 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002182 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
2183 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2184 // Validate image
2185 auto image_view = update->pImageInfo[di].imageView;
2186 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002187 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2188 error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002189 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002190 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
2191 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002192 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002193 return false;
2194 }
Tony-LunarG352f08f2019-01-03 10:11:24 -07002195 if (GetDeviceExtensions(device_data_)->vk_khr_sampler_ycbcr_conversion) {
2196 ImageSamplerDescriptor *desc = (ImageSamplerDescriptor *)descriptors_[index].get();
2197 if (desc->IsImmutableSampler()) {
2198 auto sampler_state = GetSamplerState(device_data_, desc->GetSampler());
2199 auto iv_state = GetImageViewState(device_data_, image_view);
2200 if (iv_state && sampler_state) {
2201 if (iv_state->samplerConversion != sampler_state->samplerConversion) {
2202 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01948";
2203 std::stringstream error_str;
Tony-LunarG8a51f0a2019-01-04 16:14:14 -07002204 error_str << "Attempted write update to combined image sampler and image view and sampler ycbcr "
2205 "conversions are not identical, sampler: "
Tony-LunarG352f08f2019-01-03 10:11:24 -07002206 << desc->GetSampler() << " image view: " << iv_state->image_view << ".";
2207 *error_msg = error_str.str();
2208 return false;
2209 }
2210 }
2211 }
2212 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002213 }
2214 }
Tobin Ehlis648b1cf2018-04-13 15:24:13 -06002215 // fall through
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002216 case VK_DESCRIPTOR_TYPE_SAMPLER: {
2217 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2218 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
2219 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002220 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002221 std::stringstream error_str;
2222 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
2223 << update->pImageInfo[di].sampler << ".";
2224 *error_msg = error_str.str();
2225 return false;
2226 }
2227 } else {
2228 // TODO : Warn here
2229 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002230 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002231 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002232 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002233 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2234 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2235 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2236 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2237 auto image_view = update->pImageInfo[di].imageView;
2238 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002239 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2240 error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002241 std::stringstream error_str;
2242 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
2243 *error_msg = error_str.str();
2244 return false;
2245 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002246 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002247 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002248 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002249 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2250 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
2251 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2252 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002253 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002254 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002255 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002256 std::stringstream error_str;
2257 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2258 *error_msg = error_str.str();
2259 return false;
2260 }
2261 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002262 auto buffer_state = GetBufferState(device_data_, buffer);
2263 // Verify that buffer underlying the view hasn't been destroyed prematurely
2264 if (!buffer_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002265 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002266 std::stringstream error_str;
2267 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
2268 << ") has been destroyed: " << error_msg->c_str();
2269 *error_msg = error_str.str();
2270 return false;
2271 } else if (!ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002272 std::stringstream error_str;
2273 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
2274 *error_msg = error_str.str();
2275 return false;
2276 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002277 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002278 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002279 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002280 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2281 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2282 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2283 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2284 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufb45fdc32018-10-12 15:14:17 -06002285 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, func_name, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002286 std::stringstream error_str;
2287 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
2288 *error_msg = error_str.str();
2289 return false;
2290 }
2291 }
2292 break;
2293 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002294 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
2295 break;
Eric Werness30127fd2018-10-31 21:01:03 -07002296 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002297 // XXX TODO
2298 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002299 default:
2300 assert(0); // We've already verified update type so should never get here
2301 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002302 }
2303 // All checks passed so update contents are good
2304 return true;
2305}
2306// Verify that the contents of the update are ok, but don't perform actual update
2307bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
John Zulaufb45fdc32018-10-12 15:14:17 -06002308 VkDescriptorType type, uint32_t index, const char *func_name,
2309 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002310 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
2311 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06002312 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002313 case PlainSampler: {
2314 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002315 const auto src_desc = src_set->descriptors_[index + di].get();
2316 if (!src_desc->updated) continue;
2317 if (!src_desc->IsImmutableSampler()) {
2318 auto update_sampler = static_cast<SamplerDescriptor *>(src_desc)->GetSampler();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002319 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002320 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002321 std::stringstream error_str;
2322 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2323 *error_msg = error_str.str();
2324 return false;
2325 }
2326 } else {
2327 // TODO : Warn here
2328 }
2329 }
2330 break;
2331 }
2332 case ImageSampler: {
2333 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002334 const auto src_desc = src_set->descriptors_[index + di].get();
2335 if (!src_desc->updated) continue;
2336 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002337 // First validate sampler
2338 if (!img_samp_desc->IsImmutableSampler()) {
2339 auto update_sampler = img_samp_desc->GetSampler();
2340 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002341 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002342 std::stringstream error_str;
2343 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2344 *error_msg = error_str.str();
2345 return false;
2346 }
2347 } else {
2348 // TODO : Warn here
2349 }
2350 // Validate image
2351 auto image_view = img_samp_desc->GetImageView();
2352 auto image_layout = img_samp_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002353 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002354 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002355 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002356 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002357 return false;
2358 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002359 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002360 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002361 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002362 case Image: {
2363 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002364 const auto src_desc = src_set->descriptors_[index + di].get();
2365 if (!src_desc->updated) continue;
2366 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002367 auto image_view = img_desc->GetImageView();
2368 auto image_layout = img_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002369 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002370 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002371 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002372 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002373 return false;
2374 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002375 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002376 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002377 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002378 case TexelBuffer: {
2379 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002380 const auto src_desc = src_set->descriptors_[index + di].get();
2381 if (!src_desc->updated) continue;
2382 auto buffer_view = static_cast<TexelDescriptor *>(src_desc)->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002383 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002384 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002385 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002386 std::stringstream error_str;
2387 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2388 *error_msg = error_str.str();
2389 return false;
2390 }
2391 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002392 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002393 std::stringstream error_str;
2394 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
2395 *error_msg = error_str.str();
2396 return false;
2397 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002398 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002399 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002400 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002401 case GeneralBuffer: {
2402 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002403 const auto src_desc = src_set->descriptors_[index + di].get();
2404 if (!src_desc->updated) continue;
2405 auto buffer = static_cast<BufferDescriptor *>(src_desc)->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002406 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002407 std::stringstream error_str;
2408 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
2409 *error_msg = error_str.str();
2410 return false;
2411 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06002412 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002413 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002414 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002415 case InlineUniform:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002416 case AccelerationStructure:
Jeff Bolze54ae892018-09-08 12:16:29 -05002417 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002418 default:
2419 assert(0); // We've already verified update type so should never get here
2420 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002421 }
2422 // All checks passed so update contents are good
2423 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12002424}
Tobin Ehlisf320b192017-03-14 11:22:50 -06002425// Update the common AllocateDescriptorSetsData
2426void cvdescriptorset::UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
2427 AllocateDescriptorSetsData *ds_data) {
2428 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
2429 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
2430 if (layout) {
2431 ds_data->layout_nodes[i] = layout;
2432 // Count total descriptors required per type
2433 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
2434 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
2435 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
2436 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
2437 }
2438 }
2439 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
2440 }
Petr Kraus13c98a62017-12-09 00:22:39 +01002441}
Tobin Ehlisee471462016-05-26 11:21:59 -06002442// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlisf320b192017-03-14 11:22:50 -06002443bool cvdescriptorset::ValidateAllocateDescriptorSets(const core_validation::layer_data *dev_data,
2444 const VkDescriptorSetAllocateInfo *p_alloc_info,
2445 const AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002446 bool skip = false;
Tobin Ehlisf320b192017-03-14 11:22:50 -06002447 auto report_data = core_validation::GetReportData(dev_data);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002448 auto pool_state = GetDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlisee471462016-05-26 11:21:59 -06002449
2450 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002451 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07002452 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
John Zulauf1d27e0a2018-11-05 10:12:48 -07002453 if (layout->IsPushDescriptor()) {
John Zulauf5562d062018-01-24 11:54:05 -07002454 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 -06002455 HandleToUint64(p_alloc_info->pSetLayouts[i]), "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-00308",
John Zulauf5562d062018-01-24 11:54:05 -07002456 "Layout 0x%" PRIxLEAST64 " specified at pSetLayouts[%" PRIu32
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002457 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
John Zulauf5562d062018-01-24 11:54:05 -07002458 HandleToUint64(p_alloc_info->pSetLayouts[i]), i,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002459 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07002460 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002461 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT &&
2462 !(pool_state->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
2463 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 -06002464 0, "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-03044",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002465 "Descriptor set layout create flags and pool create flags mismatch for index (%d)", i);
2466 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002467 }
2468 }
Mark Lobodzinski28426ae2017-06-01 07:56:38 -06002469 if (!GetDeviceExtensions(dev_data)->vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002470 // Track number of descriptorSets allowable in this pool
2471 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002472 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 -06002473 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorSetCount-00306",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002474 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002475 ". This pool only has %d descriptorSets remaining.",
2476 p_alloc_info->descriptorSetCount, HandleToUint64(pool_state->pool), pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002477 }
2478 // Determine whether descriptor counts are satisfiable
Jeff Bolze54ae892018-09-08 12:16:29 -05002479 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2480 if (ds_data->required_descriptors_by_type.at(it->first) > pool_state->availableDescriptorTypeCount[it->first]) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002481 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 -06002482 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002483 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002484 ". This pool only has %d descriptors of this type remaining.",
Dave Houlton142c4cb2018-10-17 15:04:41 -06002485 ds_data->required_descriptors_by_type.at(it->first),
2486 string_VkDescriptorType(VkDescriptorType(it->first)), HandleToUint64(pool_state->pool),
2487 pool_state->availableDescriptorTypeCount[it->first]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002488 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002489 }
2490 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06002491
Jeff Bolzfdf96072018-04-10 14:32:18 -05002492 const auto *count_allocate_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2493
2494 if (count_allocate_info) {
2495 if (count_allocate_info->descriptorSetCount != 0 &&
2496 count_allocate_info->descriptorSetCount != p_alloc_info->descriptorSetCount) {
2497 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, 0,
Dave Houltond8ed0212018-05-16 17:18:24 -06002498 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-descriptorSetCount-03045",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002499 "VkDescriptorSetAllocateInfo::descriptorSetCount (%d) != "
2500 "VkDescriptorSetVariableDescriptorCountAllocateInfoEXT::descriptorSetCount (%d)",
2501 p_alloc_info->descriptorSetCount, count_allocate_info->descriptorSetCount);
2502 }
2503 if (count_allocate_info->descriptorSetCount == p_alloc_info->descriptorSetCount) {
2504 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
2505 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
2506 if (count_allocate_info->pDescriptorCounts[i] > layout->GetDescriptorCountFromBinding(layout->GetMaxBinding())) {
2507 skip |= log_msg(
2508 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT, 0,
Dave Houltond8ed0212018-05-16 17:18:24 -06002509 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-pSetLayouts-03046",
2510 "pDescriptorCounts[%d] = (%d), binding's descriptorCount = (%d)", i,
Jeff Bolzfdf96072018-04-10 14:32:18 -05002511 count_allocate_info->pDescriptorCounts[i], layout->GetDescriptorCountFromBinding(layout->GetMaxBinding()));
2512 }
2513 }
2514 }
2515 }
2516
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002517 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06002518}
2519// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06002520void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
2521 const VkDescriptorSet *descriptor_sets,
2522 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06002523 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06002524 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
John Zulauf48a6a702017-12-22 17:14:54 -07002525 layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06002526 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002527 // Account for sets and individual descriptors allocated from pool
Tobin Ehlisee471462016-05-26 11:21:59 -06002528 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Jeff Bolze54ae892018-09-08 12:16:29 -05002529 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2530 pool_state->availableDescriptorTypeCount[it->first] -= ds_data->required_descriptors_by_type.at(it->first);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06002531 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002532
2533 const auto *variable_count_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2534 bool variable_count_valid = variable_count_info && variable_count_info->descriptorSetCount == p_alloc_info->descriptorSetCount;
2535
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002536 // Create tracking object for each descriptor set; insert into global map and the pool's set.
Tobin Ehlisee471462016-05-26 11:21:59 -06002537 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -05002538 uint32_t variable_count = variable_count_valid ? variable_count_info->pDescriptorCounts[i] : 0;
2539
Tobin Ehlis93f22372016-10-12 14:34:12 -06002540 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i],
Jeff Bolzfdf96072018-04-10 14:32:18 -05002541 variable_count, dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06002542
2543 pool_state->sets.insert(new_ds);
2544 new_ds->in_use.store(0);
2545 (*set_map)[descriptor_sets[i]] = new_ds;
2546 }
2547}
John Zulauf48a6a702017-12-22 17:14:54 -07002548
2549cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2550 GLOBAL_CB_NODE *cb_state)
2551 : filtered_map_(), orig_map_(in_map) {
2552 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2553 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2554 ds.FilterAndTrackBindingReqs(cb_state, orig_map_, filtered_map_.get());
2555 }
2556}
2557cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2558 GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline)
2559 : filtered_map_(), orig_map_(in_map) {
2560 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2561 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2562 ds.FilterAndTrackBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
2563 }
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01002564}