blob: c7ad824b0370ac3c717b16db0f503e623e59b5fc [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
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060025#include "descriptor_sets.h"
John Zulaufd47d0612018-02-16 13:00:34 -070026#include "hash_vk_types.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060027#include "vk_enum_string_helper.h"
28#include "vk_safe_struct.h"
Jeff Bolzfdf96072018-04-10 14:32:18 -050029#include "vk_typemap_helper.h"
Tobin Ehlisc8266452017-04-07 12:20:30 -060030#include "buffer_validation.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060031#include <sstream>
Mark Lobodzinski2eee5d82016-12-02 15:33:18 -070032#include <algorithm>
John Zulauff4c07882019-01-24 14:03:36 -070033#include <array>
John Zulauf1f8174b2018-02-16 12:58:37 -070034#include <memory>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060035
Jeff Bolzfdf96072018-04-10 14:32:18 -050036// ExtendedBinding collects a VkDescriptorSetLayoutBinding and any extended
37// state that comes from a different array/structure so they can stay together
38// while being sorted by binding number.
39struct ExtendedBinding {
40 ExtendedBinding(const VkDescriptorSetLayoutBinding *l, VkDescriptorBindingFlagsEXT f) : layout_binding(l), binding_flags(f) {}
41
42 const VkDescriptorSetLayoutBinding *layout_binding;
43 VkDescriptorBindingFlagsEXT binding_flags;
44};
45
John Zulauf508d13a2018-01-05 15:10:34 -070046struct BindingNumCmp {
Jeff Bolzfdf96072018-04-10 14:32:18 -050047 bool operator()(const ExtendedBinding &a, const ExtendedBinding &b) const {
48 return a.layout_binding->binding < b.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070049 }
50};
51
John Zulaufd47d0612018-02-16 13:00:34 -070052using DescriptorSetLayoutDef = cvdescriptorset::DescriptorSetLayoutDef;
53using DescriptorSetLayoutId = cvdescriptorset::DescriptorSetLayoutId;
54
John Zulauf34ebf272018-02-16 13:08:47 -070055// Canonical dictionary of DescriptorSetLayoutDef (without any handle/device specific information)
56cvdescriptorset::DescriptorSetLayoutDict descriptor_set_layout_dict;
John Zulaufd47d0612018-02-16 13:00:34 -070057
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060058DescriptorSetLayoutId GetCanonicalId(const VkDescriptorSetLayoutCreateInfo *p_create_info) {
John Zulauf34ebf272018-02-16 13:08:47 -070059 return descriptor_set_layout_dict.look_up(DescriptorSetLayoutDef(p_create_info));
John Zulaufd47d0612018-02-16 13:00:34 -070060}
John Zulauf34ebf272018-02-16 13:08:47 -070061
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060062// Construct DescriptorSetLayout instance from given create info
John Zulauf48a6a702017-12-22 17:14:54 -070063// Proactively reserve and resize as possible, as the reallocation was visible in profiling
John Zulauf1f8174b2018-02-16 12:58:37 -070064cvdescriptorset::DescriptorSetLayoutDef::DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info)
65 : flags_(p_create_info->flags), binding_count_(0), descriptor_count_(0), dynamic_descriptor_count_(0) {
Jeff Bolzfdf96072018-04-10 14:32:18 -050066 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(p_create_info->pNext);
67
John Zulauf48a6a702017-12-22 17:14:54 -070068 binding_type_stats_ = {0, 0, 0};
Jeff Bolzfdf96072018-04-10 14:32:18 -050069 std::set<ExtendedBinding, BindingNumCmp> sorted_bindings;
John Zulauf508d13a2018-01-05 15:10:34 -070070 const uint32_t input_bindings_count = p_create_info->bindingCount;
71 // Sort the input bindings in binding number order, eliminating duplicates
72 for (uint32_t i = 0; i < input_bindings_count; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -050073 VkDescriptorBindingFlagsEXT flags = 0;
74 if (flags_create_info && flags_create_info->bindingCount == p_create_info->bindingCount) {
75 flags = flags_create_info->pBindingFlags[i];
76 }
77 sorted_bindings.insert(ExtendedBinding(p_create_info->pBindings + i, flags));
John Zulaufb6d71202017-12-22 16:47:09 -070078 }
79
80 // Store the create info in the sorted order from above
Tobin Ehlisa3525e02016-11-17 10:50:52 -070081 std::map<uint32_t, uint32_t> binding_to_dyn_count;
John Zulauf508d13a2018-01-05 15:10:34 -070082 uint32_t index = 0;
83 binding_count_ = static_cast<uint32_t>(sorted_bindings.size());
84 bindings_.reserve(binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -050085 binding_flags_.reserve(binding_count_);
John Zulauf508d13a2018-01-05 15:10:34 -070086 binding_to_index_map_.reserve(binding_count_);
87 for (auto input_binding : sorted_bindings) {
88 // Add to binding and map, s.t. it is robust to invalid duplication of binding_num
Jeff Bolzfdf96072018-04-10 14:32:18 -050089 const auto binding_num = input_binding.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070090 binding_to_index_map_[binding_num] = index++;
Jeff Bolzfdf96072018-04-10 14:32:18 -050091 bindings_.emplace_back(input_binding.layout_binding);
John Zulauf508d13a2018-01-05 15:10:34 -070092 auto &binding_info = bindings_.back();
Jeff Bolzfdf96072018-04-10 14:32:18 -050093 binding_flags_.emplace_back(input_binding.binding_flags);
John Zulauf508d13a2018-01-05 15:10:34 -070094
John Zulaufb6d71202017-12-22 16:47:09 -070095 descriptor_count_ += binding_info.descriptorCount;
96 if (binding_info.descriptorCount > 0) {
97 non_empty_bindings_.insert(binding_num);
Tobin Ehlis9637fb22016-12-12 15:59:34 -070098 }
John Zulaufb6d71202017-12-22 16:47:09 -070099
100 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
101 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
102 binding_to_dyn_count[binding_num] = binding_info.descriptorCount;
103 dynamic_descriptor_count_ += binding_info.descriptorCount;
John Zulauf48a6a702017-12-22 17:14:54 -0700104 binding_type_stats_.dynamic_buffer_count++;
105 } else if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
106 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
107 binding_type_stats_.non_dynamic_buffer_count++;
108 } else {
109 binding_type_stats_.image_sampler_count++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600110 }
111 }
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700112 assert(bindings_.size() == binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500113 assert(binding_flags_.size() == binding_count_);
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700114 uint32_t global_index = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700115 binding_to_global_index_range_map_.reserve(binding_count_);
116 // Vector order is finalized so create maps of bindings to descriptors and descriptors to indices
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700117 for (uint32_t i = 0; i < binding_count_; ++i) {
118 auto binding_num = bindings_[i].binding;
John Zulaufc483f442017-12-15 14:02:06 -0700119 auto final_index = global_index + bindings_[i].descriptorCount;
120 binding_to_global_index_range_map_[binding_num] = IndexRange(global_index, final_index);
John Zulaufb6d71202017-12-22 16:47:09 -0700121 if (final_index != global_index) {
122 global_start_to_index_map_[global_index] = i;
123 }
John Zulaufc483f442017-12-15 14:02:06 -0700124 global_index = final_index;
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700125 }
John Zulaufb6d71202017-12-22 16:47:09 -0700126
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700127 // Now create dyn offset array mapping for any dynamic descriptors
128 uint32_t dyn_array_idx = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700129 binding_to_dynamic_array_idx_map_.reserve(binding_to_dyn_count.size());
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700130 for (const auto &bc_pair : binding_to_dyn_count) {
131 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
132 dyn_array_idx += bc_pair.second;
133 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600134}
Tobin Ehlis154c2692016-10-25 09:36:53 -0600135
John Zulaufd47d0612018-02-16 13:00:34 -0700136size_t cvdescriptorset::DescriptorSetLayoutDef::hash() const {
137 hash_util::HashCombiner hc;
138 hc << flags_;
139 hc.Combine(bindings_);
John Zulauf223b69d2018-11-09 16:00:59 -0700140 hc.Combine(binding_flags_);
John Zulaufd47d0612018-02-16 13:00:34 -0700141 return hc.Value();
142}
143//
144
John Zulauf1f8174b2018-02-16 12:58:37 -0700145// Return valid index or "end" i.e. binding_count_;
146// The asserts in "Get" are reduced to the set where no valid answer(like null or 0) could be given
147// Common code for all binding lookups.
148uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromBinding(uint32_t binding) const {
149 const auto &bi_itr = binding_to_index_map_.find(binding);
150 if (bi_itr != binding_to_index_map_.cend()) return bi_itr->second;
151 return GetBindingCount();
152}
153VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorSetLayoutBindingPtrFromIndex(
154 const uint32_t index) const {
155 if (index >= bindings_.size()) return nullptr;
156 return bindings_[index].ptr();
157}
158// Return descriptorCount for given index, 0 if index is unavailable
159uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorCountFromIndex(const uint32_t index) const {
160 if (index >= bindings_.size()) return 0;
161 return bindings_[index].descriptorCount;
162}
163// For the given index, return descriptorType
164VkDescriptorType cvdescriptorset::DescriptorSetLayoutDef::GetTypeFromIndex(const uint32_t index) const {
165 assert(index < bindings_.size());
166 if (index < bindings_.size()) return bindings_[index].descriptorType;
167 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
168}
169// For the given index, return stageFlags
170VkShaderStageFlags cvdescriptorset::DescriptorSetLayoutDef::GetStageFlagsFromIndex(const uint32_t index) const {
171 assert(index < bindings_.size());
172 if (index < bindings_.size()) return bindings_[index].stageFlags;
173 return VkShaderStageFlags(0);
174}
Jeff Bolzfdf96072018-04-10 14:32:18 -0500175// Return binding flags for given index, 0 if index is unavailable
176VkDescriptorBindingFlagsEXT cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorBindingFlagsFromIndex(
177 const uint32_t index) const {
178 if (index >= binding_flags_.size()) return 0;
179 return binding_flags_[index];
180}
John Zulauf1f8174b2018-02-16 12:58:37 -0700181
182// For the given global index, return index
183uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromGlobalIndex(const uint32_t global_index) const {
184 auto start_it = global_start_to_index_map_.upper_bound(global_index);
185 uint32_t index = binding_count_;
186 assert(start_it != global_start_to_index_map_.cbegin());
187 if (start_it != global_start_to_index_map_.cbegin()) {
188 --start_it;
189 index = start_it->second;
190#ifndef NDEBUG
191 const auto &range = GetGlobalIndexRangeFromBinding(bindings_[index].binding);
192 assert(range.start <= global_index && global_index < range.end);
193#endif
194 }
195 return index;
196}
197
198// For the given binding, return the global index range
199// As start and end are often needed in pairs, get both with a single hash lookup.
200const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromBinding(
201 const uint32_t binding) const {
202 assert(binding_to_global_index_range_map_.count(binding));
203 // In error case max uint32_t so index is out of bounds to break ASAP
204 const static IndexRange kInvalidRange = {0xFFFFFFFF, 0xFFFFFFFF};
205 const auto &range_it = binding_to_global_index_range_map_.find(binding);
206 if (range_it != binding_to_global_index_range_map_.end()) {
207 return range_it->second;
208 }
209 return kInvalidRange;
210}
211
212// For given binding, return ptr to ImmutableSampler array
213VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
214 const auto &bi_itr = binding_to_index_map_.find(binding);
215 if (bi_itr != binding_to_index_map_.end()) {
216 return bindings_[bi_itr->second].pImmutableSamplers;
217 }
218 return nullptr;
219}
220// Move to next valid binding having a non-zero binding count
221uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetNextValidBinding(const uint32_t binding) const {
222 auto it = non_empty_bindings_.upper_bound(binding);
223 assert(it != non_empty_bindings_.cend());
224 if (it != non_empty_bindings_.cend()) return *it;
225 return GetMaxBinding() + 1;
226}
227// For given index, return ptr to ImmutableSampler array
228VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
229 if (index < bindings_.size()) {
230 return bindings_[index].pImmutableSamplers;
231 }
232 return nullptr;
233}
234// If our layout is compatible with rh_ds_layout, return true,
235// else return false and fill in error_msg will description of what causes incompatibility
236bool cvdescriptorset::DescriptorSetLayout::IsCompatible(DescriptorSetLayout const *const rh_ds_layout,
237 std::string *error_msg) const {
238 // Trivial case
239 if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) return true;
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600240 if (GetLayoutDef() == rh_ds_layout->GetLayoutDef()) return true;
John Zulaufd47d0612018-02-16 13:00:34 -0700241 bool detailed_compat_check =
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600242 GetLayoutDef()->IsCompatible(layout_, rh_ds_layout->GetDescriptorSetLayout(), rh_ds_layout->GetLayoutDef(), error_msg);
John Zulaufd47d0612018-02-16 13:00:34 -0700243 // The detailed check should never tell us mismatching DSL are compatible
244 assert(!detailed_compat_check);
245 return detailed_compat_check;
John Zulauf1f8174b2018-02-16 12:58:37 -0700246}
247
John Zulaufdf3c5c12018-03-06 16:44:43 -0700248// Do a detailed compatibility check of this def (referenced by ds_layout), vs. the rhs (layout and def)
249// Should only be called if trivial accept has failed, and in that context should return false.
John Zulauf1f8174b2018-02-16 12:58:37 -0700250bool cvdescriptorset::DescriptorSetLayoutDef::IsCompatible(VkDescriptorSetLayout ds_layout, VkDescriptorSetLayout rh_ds_layout,
251 DescriptorSetLayoutDef const *const rh_ds_layout_def,
252 std::string *error_msg) const {
253 if (descriptor_count_ != rh_ds_layout_def->descriptor_count_) {
254 std::stringstream error_str;
255 error_str << "DescriptorSetLayout " << ds_layout << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
256 << rh_ds_layout << ", which comes from pipelineLayout, has " << rh_ds_layout_def->descriptor_count_
257 << " descriptors.";
258 *error_msg = error_str.str();
259 return false; // trivial fail case
260 }
John Zulaufd47d0612018-02-16 13:00:34 -0700261
John Zulauf1f8174b2018-02-16 12:58:37 -0700262 // Descriptor counts match so need to go through bindings one-by-one
263 // and verify that type and stageFlags match
264 for (auto binding : bindings_) {
265 // TODO : Do we also need to check immutable samplers?
266 // VkDescriptorSetLayoutBinding *rh_binding;
267 if (binding.descriptorCount != rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding)) {
268 std::stringstream error_str;
269 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has a descriptorCount of "
270 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
271 << rh_ds_layout << ", which comes from pipelineLayout, has a descriptorCount of "
272 << rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding);
273 *error_msg = error_str.str();
274 return false;
275 } else if (binding.descriptorType != rh_ds_layout_def->GetTypeFromBinding(binding.binding)) {
276 std::stringstream error_str;
277 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " is type '"
278 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
279 << " for DescriptorSetLayout " << rh_ds_layout << ", which comes from pipelineLayout, is type '"
280 << string_VkDescriptorType(rh_ds_layout_def->GetTypeFromBinding(binding.binding)) << "'";
281 *error_msg = error_str.str();
282 return false;
283 } else if (binding.stageFlags != rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding)) {
284 std::stringstream error_str;
285 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has stageFlags "
286 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout " << rh_ds_layout
287 << ", which comes from pipelineLayout, has stageFlags "
288 << rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding);
289 *error_msg = error_str.str();
290 return false;
291 }
292 }
293 return true;
294}
295
296bool cvdescriptorset::DescriptorSetLayoutDef::IsNextBindingConsistent(const uint32_t binding) const {
297 if (!binding_to_index_map_.count(binding + 1)) return false;
298 auto const &bi_itr = binding_to_index_map_.find(binding);
299 if (bi_itr != binding_to_index_map_.end()) {
300 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
301 if (next_bi_itr != binding_to_index_map_.end()) {
302 auto type = bindings_[bi_itr->second].descriptorType;
303 auto stage_flags = bindings_[bi_itr->second].stageFlags;
304 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500305 auto flags = binding_flags_[bi_itr->second];
John Zulauf1f8174b2018-02-16 12:58:37 -0700306 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
307 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
Jeff Bolzfdf96072018-04-10 14:32:18 -0500308 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false)) ||
309 (flags != binding_flags_[next_bi_itr->second])) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700310 return false;
311 }
312 return true;
313 }
314 }
315 return false;
316}
317// Starting at offset descriptor of given binding, parse over update_count
318// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
319// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
320// If so, return true. If not, fill in error_msg and return false
321bool cvdescriptorset::DescriptorSetLayoutDef::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset,
322 uint32_t update_count, const char *type,
323 const VkDescriptorSet set, std::string *error_msg) const {
324 // Verify consecutive bindings match (if needed)
325 auto orig_binding = current_binding;
326 // Track count of descriptors in the current_bindings that are remaining to be updated
327 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
328 // First, it's legal to offset beyond your own binding so handle that case
329 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
330 while (offset >= binding_remaining) {
331 // Advance to next binding, decrement offset by binding size
332 offset -= binding_remaining;
333 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
334 }
335 binding_remaining -= offset;
336 while (update_count > binding_remaining) { // While our updates overstep current binding
337 // Verify next consecutive binding matches type, stage flags & immutable sampler use
338 if (!IsNextBindingConsistent(current_binding++)) {
339 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -0600340 error_str << "Attempting " << type;
341 if (IsPushDescriptor()) {
342 error_str << " push descriptors";
343 } else {
344 error_str << " descriptor set " << set;
345 }
346 error_str << " binding #" << orig_binding << " with #" << update_count
John Zulauf1f8174b2018-02-16 12:58:37 -0700347 << " descriptors being updated but this update oversteps the bounds of this binding and the next binding is "
348 "not consistent with current binding so this update is invalid.";
349 *error_msg = error_str.str();
350 return false;
351 }
352 // For sake of this check consider the bindings updated and grab count for next binding
353 update_count -= binding_remaining;
354 binding_remaining = GetDescriptorCountFromBinding(current_binding);
355 }
356 return true;
357}
358
359// The DescriptorSetLayout stores the per handle data for a descriptor set layout, and references the common defintion for the
360// handle invariant portion
361cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
362 const VkDescriptorSetLayout layout)
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600363 : layout_(layout), layout_destroyed_(false), layout_id_(GetCanonicalId(p_create_info)) {}
John Zulauf1f8174b2018-02-16 12:58:37 -0700364
Tobin Ehlis154c2692016-10-25 09:36:53 -0600365// Validate descriptor set layout create info
Jeff Bolzfdf96072018-04-10 14:32:18 -0500366bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(
367 const debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *create_info, const bool push_descriptor_ext,
368 const uint32_t max_push_descriptors, const bool descriptor_indexing_ext,
Jeff Bolze54ae892018-09-08 12:16:29 -0500369 const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features,
370 const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features,
371 const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props) {
Tobin Ehlis154c2692016-10-25 09:36:53 -0600372 bool skip = false;
373 std::unordered_set<uint32_t> bindings;
John Zulauf0fdeab32018-01-23 11:27:35 -0700374 uint64_t total_descriptors = 0;
375
Jeff Bolzfdf96072018-04-10 14:32:18 -0500376 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(create_info->pNext);
377
378 const bool push_descriptor_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR);
John Zulauf0fdeab32018-01-23 11:27:35 -0700379 if (push_descriptor_set && !push_descriptor_ext) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600380 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 -0600381 kVUID_Core_DrawState_ExtensionNotEnabled,
Mark Lobodzinskifb5a3e62018-04-13 10:46:48 -0600382 "Attempted to use %s in %s but its required extension %s has not been enabled.\n",
John Zulauf0fdeab32018-01-23 11:27:35 -0700383 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR", "VkDescriptorSetLayoutCreateInfo::flags",
384 VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
385 }
386
Jeff Bolzfdf96072018-04-10 14:32:18 -0500387 const bool update_after_bind_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT);
388 if (update_after_bind_set && !descriptor_indexing_ext) {
389 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 -0600390 kVUID_Core_DrawState_ExtensionNotEnabled,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500391 "Attemped to use %s in %s but its required extension %s has not been enabled.\n",
392 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT", "VkDescriptorSetLayoutCreateInfo::flags",
393 VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
394 }
395
John Zulauf0fdeab32018-01-23 11:27:35 -0700396 auto valid_type = [push_descriptor_set](const VkDescriptorType type) {
397 return !push_descriptor_set ||
Dave Houlton142c4cb2018-10-17 15:04:41 -0600398 ((type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && (type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) &&
Jeff Bolze54ae892018-09-08 12:16:29 -0500399 (type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT));
John Zulauf0fdeab32018-01-23 11:27:35 -0700400 };
401
Jeff Bolzfdf96072018-04-10 14:32:18 -0500402 uint32_t max_binding = 0;
403
Tobin Ehlis154c2692016-10-25 09:36:53 -0600404 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
John Zulauf0fdeab32018-01-23 11:27:35 -0700405 const auto &binding_info = create_info->pBindings[i];
Jeff Bolzfdf96072018-04-10 14:32:18 -0500406 max_binding = std::max(max_binding, binding_info.binding);
407
John Zulauf0fdeab32018-01-23 11:27:35 -0700408 if (!bindings.insert(binding_info.binding).second) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600409 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 -0600410 "VUID-VkDescriptorSetLayoutCreateInfo-binding-00279",
411 "duplicated binding number in VkDescriptorSetLayoutBinding.");
Tobin Ehlis154c2692016-10-25 09:36:53 -0600412 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700413 if (!valid_type(binding_info.descriptorType)) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600414 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 -0600415 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
416 ? "VUID-VkDescriptorSetLayoutCreateInfo-flags-02208"
417 : "VUID-VkDescriptorSetLayoutCreateInfo-flags-00280",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600418 "invalid type %s ,for push descriptors in VkDescriptorSetLayoutBinding entry %" PRIu32 ".",
419 string_VkDescriptorType(binding_info.descriptorType), i);
John Zulauf0fdeab32018-01-23 11:27:35 -0700420 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500421
422 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
423 if ((binding_info.descriptorCount % 4) != 0) {
424 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
425 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02209",
Dave Houlton142c4cb2018-10-17 15:04:41 -0600426 "descriptorCount =(%" PRIu32 ") must be a multiple of 4", binding_info.descriptorCount);
Jeff Bolze54ae892018-09-08 12:16:29 -0500427 }
428 if (binding_info.descriptorCount > inline_uniform_block_props->maxInlineUniformBlockSize) {
429 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
430 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02210",
431 "descriptorCount =(%" PRIu32 ") must be less than or equal to maxInlineUniformBlockSize",
432 binding_info.descriptorCount);
433 }
434 }
435
John Zulauf0fdeab32018-01-23 11:27:35 -0700436 total_descriptors += binding_info.descriptorCount;
Tobin Ehlis154c2692016-10-25 09:36:53 -0600437 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700438
Jeff Bolzfdf96072018-04-10 14:32:18 -0500439 if (flags_create_info) {
440 if (flags_create_info->bindingCount != 0 && flags_create_info->bindingCount != create_info->bindingCount) {
441 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 -0600442 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-bindingCount-03002",
Jeff Bolzfdf96072018-04-10 14:32:18 -0500443 "VkDescriptorSetLayoutCreateInfo::bindingCount (%d) != "
444 "VkDescriptorSetLayoutBindingFlagsCreateInfoEXT::bindingCount (%d)",
445 create_info->bindingCount, flags_create_info->bindingCount);
446 }
447
448 if (flags_create_info->bindingCount == create_info->bindingCount) {
449 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
450 const auto &binding_info = create_info->pBindings[i];
451
452 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) {
453 if (!update_after_bind_set) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600454 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
455 "VUID-VkDescriptorSetLayoutCreateInfo-flags-03000",
456 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500457 }
458
459 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER &&
460 !descriptor_indexing_features->descriptorBindingUniformBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600461 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
462 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
463 "descriptorBindingUniformBufferUpdateAfterBind-03005",
464 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500465 }
466 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
467 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
468 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) &&
469 !descriptor_indexing_features->descriptorBindingSampledImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600470 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
471 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
472 "descriptorBindingSampledImageUpdateAfterBind-03006",
473 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500474 }
475 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE &&
476 !descriptor_indexing_features->descriptorBindingStorageImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600477 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
478 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
479 "descriptorBindingStorageImageUpdateAfterBind-03007",
480 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500481 }
482 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER &&
483 !descriptor_indexing_features->descriptorBindingStorageBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600484 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
485 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
486 "descriptorBindingStorageBufferUpdateAfterBind-03008",
487 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500488 }
489 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
490 !descriptor_indexing_features->descriptorBindingUniformTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600491 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
492 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
493 "descriptorBindingUniformTexelBufferUpdateAfterBind-03009",
494 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500495 }
496 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER &&
497 !descriptor_indexing_features->descriptorBindingStorageTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600498 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
499 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
500 "descriptorBindingStorageTexelBufferUpdateAfterBind-03010",
501 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500502 }
503 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT ||
504 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
505 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600506 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
507 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-None-03011",
508 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500509 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500510
511 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT &&
512 !inline_uniform_block_features->descriptorBindingInlineUniformBlockUpdateAfterBind) {
513 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 -0600514 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
515 "descriptorBindingInlineUniformBlockUpdateAfterBind-02211",
516 "Invalid flags (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) for "
517 "VkDescriptorSetLayoutBinding entry %" PRIu32
518 " with descriptorBindingInlineUniformBlockUpdateAfterBind not enabled",
519 i);
Jeff Bolze54ae892018-09-08 12:16:29 -0500520 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500521 }
522
523 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT) {
524 if (!descriptor_indexing_features->descriptorBindingUpdateUnusedWhilePending) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600525 skip |= log_msg(
526 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
527 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingUpdateUnusedWhilePending-03012",
528 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500529 }
530 }
531
532 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) {
533 if (!descriptor_indexing_features->descriptorBindingPartiallyBound) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600534 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
535 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingPartiallyBound-03013",
536 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500537 }
538 }
539
540 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT) {
541 if (binding_info.binding != max_binding) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600542 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
543 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03004",
544 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500545 }
546
547 if (!descriptor_indexing_features->descriptorBindingVariableDescriptorCount) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600548 skip |= log_msg(
549 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
550 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingVariableDescriptorCount-03014",
551 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500552 }
553 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
554 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600555 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
556 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03015",
557 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500558 }
559 }
560
561 if (push_descriptor_set &&
562 (flags_create_info->pBindingFlags[i] &
563 (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
564 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT))) {
565 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 -0600566 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-flags-03003",
567 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500568 }
569 }
570 }
571 }
572
John Zulauf0fdeab32018-01-23 11:27:35 -0700573 if ((push_descriptor_set) && (total_descriptors > max_push_descriptors)) {
574 const char *undefined = push_descriptor_ext ? "" : " -- undefined";
Dave Houltond8ed0212018-05-16 17:18:24 -0600575 skip |=
576 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
577 "VUID-VkDescriptorSetLayoutCreateInfo-flags-00281",
578 "for push descriptor, total descriptor count in layout (%" PRIu64
579 ") must not be greater than VkPhysicalDevicePushDescriptorPropertiesKHR::maxPushDescriptors (%" PRIu32 "%s).",
580 total_descriptors, max_push_descriptors, undefined);
John Zulauf0fdeab32018-01-23 11:27:35 -0700581 }
582
Tobin Ehlis154c2692016-10-25 09:36:53 -0600583 return skip;
584}
585
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600586cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
587 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
588
Tobin Ehlis93f22372016-10-12 14:34:12 -0600589cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500590 const std::shared_ptr<DescriptorSetLayout const> &layout, uint32_t variable_count,
591 layer_data *dev_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700592 : some_update_(false),
593 set_(set),
594 pool_state_(nullptr),
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600595 p_layout_(layout),
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700596 device_data_(dev_data),
Jeff Bolzfdf96072018-04-10 14:32:18 -0500597 limits_(GetPhysDevProperties(dev_data)->properties.limits),
598 variable_count_(variable_count) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700599 pool_state_ = GetDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600600 // Foreach binding, create default descriptors of given type
John Zulaufb6d71202017-12-22 16:47:09 -0700601 descriptors_.reserve(p_layout_->GetTotalDescriptorCount());
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600602 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
603 auto type = p_layout_->GetTypeFromIndex(i);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600604 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700605 case VK_DESCRIPTOR_TYPE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600606 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
607 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600608 if (immut_sampler) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700609 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600610 some_update_ = true; // Immutable samplers are updated at creation
611 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700612 descriptors_.emplace_back(new SamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700613 }
614 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600615 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700616 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600617 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
618 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600619 if (immut) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700620 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600621 some_update_ = true; // Immutable samplers are updated at creation
622 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700623 descriptors_.emplace_back(new ImageSamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700624 }
625 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600626 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700627 // ImageDescriptors
628 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
629 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
630 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600631 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700632 descriptors_.emplace_back(new ImageDescriptor(type));
633 break;
634 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
635 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600636 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700637 descriptors_.emplace_back(new TexelDescriptor(type));
638 break;
639 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
640 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
641 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
642 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600643 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700644 descriptors_.emplace_back(new BufferDescriptor(type));
645 break;
Jeff Bolze54ae892018-09-08 12:16:29 -0500646 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
647 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
648 descriptors_.emplace_back(new InlineUniformDescriptor(type));
649 break;
Eric Werness30127fd2018-10-31 21:01:03 -0700650 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -0500651 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
652 descriptors_.emplace_back(new AccelerationStructureDescriptor(type));
653 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700654 default:
655 assert(0); // Bad descriptor type specified
656 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600657 }
658 }
659}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600660
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700661cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200662
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600663static std::string StringDescriptorReqViewType(descriptor_req req) {
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700664 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200665 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
666 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700667 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700668 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200669 }
670 }
671
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700672 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700673
674 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200675}
676
Chris Forbesda01e8d2018-08-27 15:36:57 -0700677static char const *StringDescriptorReqComponentType(descriptor_req req) {
678 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_SINT) return "SINT";
679 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_UINT) return "UINT";
680 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT) return "FLOAT";
681 return "(none)";
682}
683
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600684// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600685bool cvdescriptorset::DescriptorSet::IsCompatible(DescriptorSetLayout const *const layout, std::string *error) const {
686 return layout->IsCompatible(p_layout_.get(), error);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600687}
Chris Forbes57989132016-07-26 17:06:10 +1200688
Chris Forbesda01e8d2018-08-27 15:36:57 -0700689static unsigned DescriptorRequirementsBitsFromFormat(VkFormat fmt) {
690 if (FormatIsSInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
691 if (FormatIsUInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
692 if (FormatIsDepthAndStencil(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT | DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
693 if (fmt == VK_FORMAT_UNDEFINED) return 0;
694 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
695 return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
696}
697
Tobin Ehlis3066db62016-08-22 08:12:23 -0600698// 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 -0600699// This includes validating that all descriptors in the given bindings are updated,
700// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
701// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600702bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
John Zulauf48a6a702017-12-22 17:14:54 -0700703 const std::vector<uint32_t> &dynamic_offsets, GLOBAL_CB_NODE *cb_node,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600704 const char *caller, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200705 for (auto binding_pair : bindings) {
706 auto binding = binding_pair.first;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600707 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600708 std::stringstream error_str;
709 error_str << "Attempting to validate DrawState for binding #" << binding
710 << " which is an invalid binding for this descriptor set.";
711 *error = error_str.str();
712 return false;
713 }
John Zulaufc483f442017-12-15 14:02:06 -0700714 IndexRange index_range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700715 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Jeff Bolzfdf96072018-04-10 14:32:18 -0500716
717 if (IsVariableDescriptorCount(binding)) {
718 // Only validate the first N descriptors if it uses variable_count
719 index_range.end = index_range.start + GetVariableDescriptorCount();
720 }
721
John Zulaufc483f442017-12-15 14:02:06 -0700722 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
Tobin Ehlisda77de72018-12-13 08:53:28 -0700723 if ((p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
724 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) ||
Jeff Bolze54ae892018-09-08 12:16:29 -0500725 descriptors_[i]->GetClass() == InlineUniform) {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500726 // Can't validate the descriptor because it may not have been updated,
727 // or the view could have been destroyed
728 continue;
729 } else if (!descriptors_[i]->updated) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700730 std::stringstream error_str;
731 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
732 << " is being used in draw but has not been updated.";
733 *error = error_str.str();
734 return false;
735 } else {
736 auto descriptor_class = descriptors_[i]->GetClass();
737 if (descriptor_class == GeneralBuffer) {
738 // Verify that buffers are valid
739 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
740 auto buffer_node = GetBufferState(device_data_, buffer);
741 if (!buffer_node) {
742 std::stringstream error_str;
743 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
744 << " references invalid buffer " << buffer << ".";
745 *error = error_str.str();
746 return false;
John Zulauf48a6a702017-12-22 17:14:54 -0700747 } else if (!buffer_node->sparse) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700748 for (auto mem_binding : buffer_node->GetBoundMemory()) {
749 if (!GetMemObjInfo(device_data_, mem_binding)) {
750 std::stringstream error_str;
751 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
752 << " uses buffer " << buffer << " that references invalid memory " << mem_binding << ".";
753 *error = error_str.str();
Tobin Ehlisc8266452017-04-07 12:20:30 -0600754 return false;
755 }
756 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700757 }
758 if (descriptors_[i]->IsDynamic()) {
759 // Validate that dynamic offsets are within the buffer
760 auto buffer_size = buffer_node->createInfo.size;
761 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
762 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
763 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
764 if (VK_WHOLE_SIZE == range) {
765 if ((dyn_offset + desc_offset) > buffer_size) {
766 std::stringstream error_str;
767 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
768 << " uses buffer " << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset "
769 << dyn_offset << " combined with offset " << desc_offset
770 << " that oversteps the buffer size of " << buffer_size << ".";
771 *error = error_str.str();
772 return false;
773 }
774 } else {
775 if ((dyn_offset + desc_offset + range) > buffer_size) {
776 std::stringstream error_str;
777 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
778 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
779 << " combined with offset " << desc_offset << " and range " << range
780 << " that oversteps the buffer size of " << buffer_size << ".";
781 *error = error_str.str();
782 return false;
783 }
784 }
785 }
786 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
787 VkImageView image_view;
788 VkImageLayout image_layout;
789 if (descriptor_class == ImageSampler) {
790 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
791 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
792 } else {
793 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
794 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
795 }
796 auto reqs = binding_pair.second;
797
798 auto image_view_state = GetImageViewState(device_data_, image_view);
Tobin Ehlis836a1372017-07-14 11:25:21 -0600799 if (nullptr == image_view_state) {
800 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
801 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
802 std::stringstream error_str;
803 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
804 << " is using imageView " << image_view << " that has been destroyed.";
805 *error = error_str.str();
806 return false;
807 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700808 auto image_view_ci = image_view_state->create_info;
809
810 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
811 // bad view type
812 std::stringstream error_str;
813 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600814 << " requires an image view of type " << StringDescriptorReqViewType(reqs) << " but got "
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700815 << string_VkImageViewType(image_view_ci.viewType) << ".";
816 *error = error_str.str();
817 return false;
818 }
819
Chris Forbesda01e8d2018-08-27 15:36:57 -0700820 auto format_bits = DescriptorRequirementsBitsFromFormat(image_view_ci.format);
821 if (!(reqs & format_bits)) {
822 // bad component type
823 std::stringstream error_str;
824 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i << " requires "
825 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
826 << string_VkFormat(image_view_ci.format) << ".";
827 *error = error_str.str();
828 return false;
829 }
830
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700831 auto image_node = GetImageState(device_data_, image_view_ci.image);
832 assert(image_node);
833 // Verify Image Layout
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700834 // Copy first mip level into sub_layers and loop over each mip level to verify layout
835 VkImageSubresourceLayers sub_layers;
836 sub_layers.aspectMask = image_view_ci.subresourceRange.aspectMask;
837 sub_layers.baseArrayLayer = image_view_ci.subresourceRange.baseArrayLayer;
838 sub_layers.layerCount = image_view_ci.subresourceRange.layerCount;
839 bool hit_error = false;
840 for (auto cur_level = image_view_ci.subresourceRange.baseMipLevel;
841 cur_level < image_view_ci.subresourceRange.levelCount; ++cur_level) {
842 sub_layers.mipLevel = cur_level;
Cort Stratton7df30962018-05-17 19:45:57 -0700843 // No "invalid layout" VUID required for this call, since the optimal_layout parameter is UNDEFINED.
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700844 VerifyImageLayout(device_data_, cb_node, image_node, sub_layers, image_layout, VK_IMAGE_LAYOUT_UNDEFINED,
Cort Stratton7df30962018-05-17 19:45:57 -0700845 caller, kVUIDUndefined, "VUID-VkDescriptorImageInfo-imageLayout-00344", &hit_error);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700846 if (hit_error) {
847 *error =
John Zulauf1d27e0a2018-11-05 10:12:48 -0700848 "Image layout specified at vkUpdateDescriptorSet* or vkCmdPushDescriptorSet* time "
John Zulaufb45fdc32018-10-12 15:14:17 -0600849 "doesn't match actual image layout at time descriptor is used. See previous error callback for "
850 "specific details.";
Chris Forbes57989132016-07-26 17:06:10 +1200851 return false;
852 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700853 }
854 // Verify Sample counts
855 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
856 std::stringstream error_str;
857 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
858 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
859 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
860 *error = error_str.str();
861 return false;
862 }
863 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
864 std::stringstream error_str;
865 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
866 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
867 *error = error_str.str();
868 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200869 }
Chris Forbese92dd1d2019-01-21 15:58:57 -0800870 } else if (descriptor_class == TexelBuffer) {
871 auto texel_buffer = static_cast<TexelDescriptor *>(descriptors_[i].get());
872 auto buffer_view = GetBufferViewState(device_data_, texel_buffer->GetBufferView());
873
874 auto reqs = binding_pair.second;
875 auto format_bits = DescriptorRequirementsBitsFromFormat(buffer_view->create_info.format);
876
877 if (!(reqs & format_bits)) {
878 // bad component type
879 std::stringstream error_str;
880 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i << " requires "
881 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
882 << string_VkFormat(buffer_view->create_info.format) << ".";
883 *error = error_str.str();
884 return false;
885 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600886 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600887 if (descriptor_class == ImageSampler || descriptor_class == PlainSampler) {
888 // Verify Sampler still valid
889 VkSampler sampler;
890 if (descriptor_class == ImageSampler) {
891 sampler = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetSampler();
892 } else {
893 sampler = static_cast<SamplerDescriptor *>(descriptors_[i].get())->GetSampler();
894 }
895 if (!ValidateSampler(sampler, device_data_)) {
896 std::stringstream error_str;
897 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
898 << " is using sampler " << sampler << " that has been destroyed.";
899 *error = error_str.str();
900 return false;
901 }
902 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600903 }
904 }
905 }
906 return true;
907}
Chris Forbes57989132016-07-26 17:06:10 +1200908
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600909// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600910uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600911 std::unordered_set<VkBuffer> *buffer_set,
912 std::unordered_set<VkImageView> *image_set) const {
913 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200914 for (auto binding_pair : bindings) {
915 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600916 // If a binding doesn't exist, skip it
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600917 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600918 continue;
919 }
John Zulaufc483f442017-12-15 14:02:06 -0700920 uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600921 if (descriptors_[start_idx]->IsStorage()) {
922 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600923 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600924 if (descriptors_[start_idx + i]->updated) {
925 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600926 num_updates++;
927 }
928 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600929 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600930 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600931 if (descriptors_[start_idx + i]->updated) {
932 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700933 auto bv_state = GetBufferViewState(device_data_, bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600934 if (bv_state) {
935 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600936 num_updates++;
937 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600938 }
939 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600940 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600941 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600942 if (descriptors_[start_idx + i]->updated) {
943 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600944 num_updates++;
945 }
946 }
947 }
948 }
949 }
950 return num_updates;
951}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600952// Set is being deleted or updates so invalidate all bound cmd buffers
953void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600954 core_validation::InvalidateCommandBuffers(device_data_, cb_bindings, {HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600955}
John Zulauf1d27e0a2018-11-05 10:12:48 -0700956
957// Loop through the write updates to do for a push descriptor set, ignoring dstSet
958void cvdescriptorset::DescriptorSet::PerformPushDescriptorsUpdate(uint32_t write_count, const VkWriteDescriptorSet *p_wds) {
959 assert(IsPushDescriptor());
960 for (uint32_t i = 0; i < write_count; i++) {
961 PerformWriteUpdate(&p_wds[i]);
962 }
963}
964
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600965// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600966void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700967 // Perform update on a per-binding basis as consecutive updates roll over to next binding
968 auto descriptors_remaining = update->descriptorCount;
969 auto binding_being_updated = update->dstBinding;
970 auto offset = update->dstArrayElement;
Tobin Ehlise16805c2017-08-09 09:10:37 -0600971 uint32_t update_index = 0;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700972 while (descriptors_remaining) {
973 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
John Zulaufc483f442017-12-15 14:02:06 -0700974 auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700975 // Loop over the updates for a single binding at a time
Tobin Ehlise16805c2017-08-09 09:10:37 -0600976 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
977 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700978 }
979 // Roll over to next binding in case of consecutive update
980 descriptors_remaining -= update_count;
981 offset = 0;
982 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600983 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700984 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600985
Jeff Bolzfdf96072018-04-10 14:32:18 -0500986 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
987 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
988 InvalidateBoundCmdBuffers();
989 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600990}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600991// Validate Copy update
992bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -0600993 const DescriptorSet *src_set, const char *func_name,
994 std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -0700995 // Verify dst layout still valid
996 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600997 *error_code = "VUID-VkCopyDescriptorSet-dstSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -0700998 string_sprintf(error_msg,
John Zulaufb45fdc32018-10-12 15:14:17 -0600999 "Cannot call %s to perform copy update on descriptor set dstSet 0x%" PRIxLEAST64
John Zulauf5dfd45c2018-01-17 11:06:34 -07001000 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
John Zulaufb45fdc32018-10-12 15:14:17 -06001001 func_name, HandleToUint64(set_), HandleToUint64(p_layout_->GetDescriptorSetLayout()));
John Zulauf5dfd45c2018-01-17 11:06:34 -07001002 return false;
1003 }
1004
1005 // Verify src layout still valid
1006 if (src_set->p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001007 *error_code = "VUID-VkCopyDescriptorSet-srcSet-parameter";
John Zulaufb45fdc32018-10-12 15:14:17 -06001008 string_sprintf(error_msg,
1009 "Cannot call %s to perform copy update of dstSet 0x%" PRIxLEAST64
1010 " from descriptor set srcSet 0x%" PRIxLEAST64
1011 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
1012 func_name, HandleToUint64(set_), HandleToUint64(src_set->set_),
1013 HandleToUint64(src_set->p_layout_->GetDescriptorSetLayout()));
John Zulauf5dfd45c2018-01-17 11:06:34 -07001014 return false;
1015 }
1016
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001017 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001018 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-00347";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001019 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001020 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001021 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001022 return false;
1023 }
1024 if (!src_set->HasBinding(update->srcBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001025 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-00345";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001026 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001027 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001028 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001029 return false;
1030 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001031 // Verify idle ds
1032 if (in_use.load() &&
1033 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1034 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1035 // TODO : Re-using Free Idle error code, need copy update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001036 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001037 std::stringstream error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001038 error_str << "Cannot call " << func_name << " to perform copy update on descriptor set " << set_
Jeff Bolzfdf96072018-04-10 14:32:18 -05001039 << " that is in use by a command buffer";
1040 *error_msg = error_str.str();
1041 return false;
1042 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001043 // src & dst set bindings are valid
1044 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -07001045 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001046 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
1047 // SRC update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001048 *error_code = "VUID-VkCopyDescriptorSet-srcArrayElement-00346";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001049 std::stringstream error_str;
1050 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
John Zulaufc483f442017-12-15 14:02:06 -07001051 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001052 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001053 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001054 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001055 return false;
1056 }
John Zulaufc483f442017-12-15 14:02:06 -07001057 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001058 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001059 // DST update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001060 *error_code = "VUID-VkCopyDescriptorSet-dstArrayElement-00348";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001061 std::stringstream error_str;
1062 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
John Zulaufc483f442017-12-15 14:02:06 -07001063 << " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001064 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001065 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001066 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001067 return false;
1068 }
1069 // Check that types match
Dave Houltond8ed0212018-05-16 17:18:24 -06001070 // TODO : Base default error case going from here is "VUID-VkAcquireNextImageInfoKHR-semaphore-parameter"2ba which covers all
1071 // consistency issues, need more fine-grained error codes
Dave Houlton00c154e2018-05-24 13:20:50 -06001072 *error_code = "VUID-VkCopyDescriptorSet-srcSet-00349";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001073 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001074 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001075 if (src_type != dst_type) {
1076 std::stringstream error_str;
1077 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
1078 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001079 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001080 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001081 return false;
1082 }
1083 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -06001084 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001085 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001086 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001087 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001088 return false;
1089 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001090
1091 if ((src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1092 !(GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001093 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01918";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001094 std::stringstream error_str;
1095 error_str << "If pname:srcSet's (" << update->srcSet
1096 << ") layout was created with the "
1097 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1098 "set, then pname:dstSet's ("
1099 << update->dstSet
1100 << ") layout must: also have been created with the "
1101 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1102 *error_msg = error_str.str();
1103 return false;
1104 }
1105
1106 if (!(src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1107 (GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001108 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01919";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001109 std::stringstream error_str;
1110 error_str << "If pname:srcSet's (" << update->srcSet
1111 << ") layout was created without the "
1112 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1113 "set, then pname:dstSet's ("
1114 << update->dstSet
1115 << ") layout must: also have been created without the "
1116 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1117 *error_msg = error_str.str();
1118 return false;
1119 }
1120
1121 if ((src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1122 !(GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001123 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01920";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001124 std::stringstream error_str;
1125 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1126 << ") was allocated was created "
1127 "with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1128 "set, then the descriptor pool from which pname:dstSet ("
1129 << update->dstSet
1130 << ") was allocated must: "
1131 "also have been created with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1132 *error_msg = error_str.str();
1133 return false;
1134 }
1135
1136 if (!(src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1137 (GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001138 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01921";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001139 std::stringstream error_str;
1140 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1141 << ") was allocated was created "
1142 "without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1143 "set, then the descriptor pool from which pname:dstSet ("
1144 << update->dstSet
1145 << ") was allocated must: "
1146 "also have been created without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1147 *error_msg = error_str.str();
1148 return false;
1149 }
1150
Jeff Bolze54ae892018-09-08 12:16:29 -05001151 if (src_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1152 if ((update->srcArrayElement % 4) != 0) {
1153 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02223";
1154 std::stringstream error_str;
1155 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1156 << "srcArrayElement " << update->srcArrayElement << " not a multiple of 4";
1157 *error_msg = error_str.str();
1158 return false;
1159 }
1160 if ((update->dstArrayElement % 4) != 0) {
1161 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02224";
1162 std::stringstream error_str;
1163 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1164 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1165 *error_msg = error_str.str();
1166 return false;
1167 }
1168 if ((update->descriptorCount % 4) != 0) {
1169 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02225";
1170 std::stringstream error_str;
1171 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1172 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1173 *error_msg = error_str.str();
1174 return false;
1175 }
1176 }
1177
Tobin Ehlisd41e7b62016-05-19 07:56:18 -06001178 // Update parameters all look good and descriptor updated so verify update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06001179 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 -06001180
1181 // All checks passed so update is good
1182 return true;
1183}
1184// Perform Copy update
1185void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -07001186 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
1187 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001188 // Update parameters all look good so perform update
1189 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001190 auto src = src_set->descriptors_[src_start_idx + di].get();
1191 auto dst = descriptors_[dst_start_idx + di].get();
1192 if (src->updated) {
1193 dst->CopyUpdate(src);
1194 some_update_ = true;
1195 } else {
1196 dst->updated = false;
1197 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001198 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001199
Jeff Bolzfdf96072018-04-10 14:32:18 -05001200 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1201 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1202 InvalidateBoundCmdBuffers();
1203 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001204}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001205
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001206// Update the drawing state for the affected descriptors.
1207// Set cb_node to this set and this set to cb_node.
1208// Add the bindings of the descriptor
1209// Set the layout based on the current descriptor layout (will mask subsequent layer mismatch errors)
1210// TODO: Modify the UpdateDrawState virtural functions to *only* set initial layout and not change layouts
Tobin Ehlisf9519102016-08-17 09:49:13 -06001211// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
1212// to be used in a draw by the given cb_node
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001213void cvdescriptorset::DescriptorSet::UpdateDrawState(GLOBAL_CB_NODE *cb_node,
1214 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001215 // bind cb to this descriptor set
1216 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001217 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Petr Krausbc7f5442017-05-14 23:43:38 +02001218 cb_node->object_bindings.insert({HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001219 pool_state_->cb_bindings.insert(cb_node);
Petr Krausbc7f5442017-05-14 23:43:38 +02001220 cb_node->object_bindings.insert({HandleToUint64(pool_state_->pool), kVulkanObjectTypeDescriptorPool});
Tobin Ehlisf9519102016-08-17 09:49:13 -06001221 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
1222 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -07001223 for (auto binding_req_pair : binding_req_map) {
1224 auto binding = binding_req_pair.first;
Tony-LunarG62c5dba2018-12-20 14:27:23 -07001225 // We aren't validating descriptors created with PARTIALLY_BOUND or UPDATE_AFTER_BIND, so don't record state
1226 if (p_layout_->GetDescriptorBindingFlagsFromBinding(binding) &
1227 (VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT)) {
1228 continue;
1229 }
John Zulaufc483f442017-12-15 14:02:06 -07001230 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
1231 for (uint32_t i = range.start; i < range.end; ++i) {
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001232 descriptors_[i]->UpdateDrawState(device_data_, cb_node);
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001233 }
1234 }
1235}
1236
John Zulauf48a6a702017-12-22 17:14:54 -07001237void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1238 const BindingReqMap &in_req, BindingReqMap *out_req,
1239 TrackedBindings *bindings) {
1240 assert(out_req);
1241 assert(bindings);
1242 const auto binding = binding_req_pair.first;
1243 // Use insert and look at the boolean ("was inserted") in the returned pair to see if this is a new set member.
1244 // Saves one hash lookup vs. find ... compare w/ end ... insert.
1245 const auto it_bool_pair = bindings->insert(binding);
1246 if (it_bool_pair.second) {
1247 out_req->emplace(binding_req_pair);
1248 }
1249}
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001250
John Zulauf48a6a702017-12-22 17:14:54 -07001251void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1252 const BindingReqMap &in_req, BindingReqMap *out_req,
1253 TrackedBindings *bindings, uint32_t limit) {
1254 if (bindings->size() < limit) FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, bindings);
1255}
1256
1257void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, const BindingReqMap &in_req,
1258 BindingReqMap *out_req) {
1259 TrackedBindings &bound = cached_validation_[cb_state].command_binding_and_usage;
1260 if (bound.size() == GetBindingCount()) {
1261 return; // All bindings are bound, out req is empty
1262 }
1263 for (const auto &binding_req_pair : in_req) {
1264 const auto binding = binding_req_pair.first;
1265 // If a binding doesn't exist, or has already been bound, skip it
1266 if (p_layout_->HasBinding(binding)) {
1267 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, &bound);
1268 }
1269 }
1270}
1271
1272void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline,
1273 const BindingReqMap &in_req, BindingReqMap *out_req) {
1274 auto &validated = cached_validation_[cb_state];
1275 auto &image_sample_val = validated.image_samplers[pipeline];
1276 auto *const dynamic_buffers = &validated.dynamic_buffers;
1277 auto *const non_dynamic_buffers = &validated.non_dynamic_buffers;
1278 const auto &stats = p_layout_->GetBindingTypeStats();
1279 for (const auto &binding_req_pair : in_req) {
1280 auto binding = binding_req_pair.first;
1281 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
1282 if (!layout_binding) {
1283 continue;
1284 }
1285 // Caching criteria differs per type.
1286 // If image_layout have changed , the image descriptors need to be validated against them.
1287 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1288 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
1289 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, dynamic_buffers, stats.dynamic_buffer_count);
1290 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1291 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
1292 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
1293 } else {
1294 // This is rather crude, as the changed layouts may not impact the bound descriptors,
1295 // but the simple "versioning" is a simple "dirt" test.
1296 auto &version = image_sample_val[binding]; // Take advantage of default construtor zero initialzing new entries
1297 if (version != cb_state->image_layout_change_count) {
1298 version = cb_state->image_layout_change_count;
1299 out_req->emplace(binding_req_pair);
1300 }
1301 }
1302 }
1303}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001304
Tobin Ehlis300888c2016-05-18 13:43:26 -06001305cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001306 updated = false;
1307 descriptor_class = PlainSampler;
1308 if (immut) {
1309 sampler_ = *immut;
1310 immutable_ = true;
1311 updated = true;
1312 }
1313}
Tobin Ehlise2f80292016-06-02 10:08:53 -06001314// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001315bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const layer_data *dev_data) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001316 return (GetSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001317}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001318
Tobin Ehlis554bf382016-05-24 11:14:43 -06001319bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
John Zulaufb45fdc32018-10-12 15:14:17 -06001320 const layer_data *dev_data, const char *func_name, std::string *error_code,
1321 std::string *error_msg) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001322 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00326";
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001323 auto iv_state = GetImageViewState(dev_data, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001324 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001325 std::stringstream error_str;
1326 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001327 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001328 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001329 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001330 // 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 -06001331 // Validate that imageLayout is compatible with aspect_mask and image format
1332 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001333 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
1334 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001335 VkFormat format = VK_FORMAT_MAX_ENUM;
1336 VkImageUsageFlags usage = 0;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001337 auto image_node = GetImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001338 if (image_node) {
1339 format = image_node->createInfo.format;
1340 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001341 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001342 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
1343 // the error here occurs is if memory bound to a created imageView has been freed.
John Zulaufb45fdc32018-10-12 15:14:17 -06001344 if (ValidateMemoryIsBoundToImage(dev_data, image_node, func_name, "VUID-VkImageViewCreateInfo-image-01020")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001345 *error_code = "VUID-VkImageViewCreateInfo-image-01020";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001346 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001347 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001348 }
Chris Forbes67757ff2017-07-21 13:59:01 -07001349
1350 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
1351 // but not binding them to descriptor sets.
1352 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D &&
1353 (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
1354 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001355 *error_code = "VUID-VkDescriptorImageInfo-imageView-00343";
Chris Forbes67757ff2017-07-21 13:59:01 -07001356 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
1357 return false;
1358 }
Tobin Ehlis1809f912016-05-25 09:24:36 -06001359 }
1360 // First validate that format and layout are compatible
1361 if (format == VK_FORMAT_MAX_ENUM) {
1362 std::stringstream error_str;
1363 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001364 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001365 return false;
1366 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001367 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
1368 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -06001369 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -06001370 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001371 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1372 // Only Color bit must be set
1373 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001374 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -07001375 error_str
1376 << "ImageView (" << image_view
1377 << ") 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 -06001378 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001379 return false;
1380 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001381 // format must NOT be DS
1382 if (ds) {
1383 std::stringstream error_str;
1384 error_str << "ImageView (" << image_view
1385 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1386 << string_VkFormat(format) << " which is not a color format.";
1387 *error_msg = error_str.str();
1388 return false;
1389 }
1390 break;
1391 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1392 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1393 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001394 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1395 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1396 // both must NOT be set
1397 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001398 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001399 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001400 return false;
1401 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001402 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1403 // Neither were set
1404 std::stringstream error_str;
1405 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1406 << " but does not have STENCIL or DEPTH aspects set";
1407 *error_msg = error_str.str();
1408 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001409 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001410 // format must be DS
1411 if (!ds) {
1412 std::stringstream error_str;
1413 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1414 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
1415 *error_msg = error_str.str();
1416 return false;
1417 }
1418 break;
1419 default:
1420 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1421 if (ds) {
1422 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1423 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1424 // both must NOT be set
1425 std::stringstream error_str;
1426 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1427 << " and is using depth/stencil image of format " << string_VkFormat(format)
1428 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1429 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1430 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1431 "reads respectively.";
1432 *error_msg = error_str.str();
1433 return false;
1434 }
1435 }
1436 }
1437 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001438 }
1439 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001440 // 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 -06001441 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1442 // under vkCreateImage()
Dave Houltond8ed0212018-05-16 17:18:24 -06001443 // TODO : Need to also validate case "VUID-VkWriteDescriptorSet-descriptorType-00336" where STORAGE_IMAGE & INPUT_ATTACH types
1444 // must have been created with identify swizzle
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001445 const char *error_usage_bit = nullptr;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001446 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001447 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1448 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1449 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1450 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
1451 }
1452 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001453 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001454 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1455 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1456 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
1457 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
1458 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001459 // TODO : Need to create custom enum error codes for these cases
1460 if (image_node->shared_presentable) {
1461 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001462 error_str << "ImageView (" << image_view
1463 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
1464 "layout "
1465 << string_VkImageLayout(image_layout)
1466 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report "
1467 "support for VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the "
1468 "VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001469 *error_msg = error_str.str();
1470 return false;
1471 }
1472 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001473 error_str << "ImageView (" << image_view
1474 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
1475 << string_VkImageLayout(image_layout)
1476 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage "
1477 "images can only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001478 *error_msg = error_str.str();
1479 return false;
1480 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001481 }
1482 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001483 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001484 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
1485 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
1486 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
1487 }
1488 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001489 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001490 default:
1491 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001492 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06001493 if (error_usage_bit) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001494 std::stringstream error_str;
1495 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
1496 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1497 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001498 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001499 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001500 }
John Zulauff4c07882019-01-24 14:03:36 -07001501
1502 if ((type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) || (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)) {
1503 // Test that the layout is compatible with the descriptorType for the two sampled image types
1504 const static std::array<VkImageLayout, 3> valid_layouts = {
1505 VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL};
1506
1507 struct ExtensionLayout {
1508 VkImageLayout layout;
1509 bool DeviceExtensions::*extension;
1510 };
1511
1512 const static std::array<ExtensionLayout, 3> extended_layouts{
1513 {// Note double brace req'd for aggregate initialization
1514 {VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR, &DeviceExtensions::vk_khr_shared_presentable_image},
1515 {VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2},
1516 {VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL, &DeviceExtensions::vk_khr_maintenance2}}};
1517 auto is_layout = [image_layout, dev_data](const ExtensionLayout &ext_layout) {
1518 return dev_data->extensions.*(ext_layout.extension) && (ext_layout.layout == image_layout);
1519 };
1520
1521 bool valid_layout = (std::find(valid_layouts.cbegin(), valid_layouts.cend(), image_layout) != valid_layouts.cend()) ||
1522 std::any_of(extended_layouts.cbegin(), extended_layouts.cend(), is_layout);
1523
1524 if (!valid_layout) {
1525 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01403";
1526 std::stringstream error_str;
1527 error_str << "Descriptor update with descriptorType " << string_VkDescriptorType(type)
1528 << " is being updated with invalid imageLayout " << string_VkImageLayout(image_layout)
1529 << ". Allowed layouts are: VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, "
1530 << "VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL";
1531 for (auto &ext_layout : extended_layouts) {
1532 if (dev_data->extensions.*(ext_layout.extension)) {
1533 error_str << ", " << string_VkImageLayout(ext_layout.layout);
1534 }
1535 }
1536 *error_msg = error_str.str();
1537 return false;
1538 }
1539 }
1540
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001541 return true;
1542}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001543
Tobin Ehlis300888c2016-05-18 13:43:26 -06001544void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Chris Forbesfea2c542018-04-13 09:34:15 -07001545 if (!immutable_) {
1546 sampler_ = update->pImageInfo[index].sampler;
1547 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001548 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001549}
1550
Tobin Ehlis300888c2016-05-18 13:43:26 -06001551void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001552 if (!immutable_) {
1553 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001554 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001555 }
1556 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001557}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001558
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001559void cvdescriptorset::SamplerDescriptor::UpdateDrawState(layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001560 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001561 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001562 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001563 }
1564}
1565
Tobin Ehlis300888c2016-05-18 13:43:26 -06001566cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001567 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001568 updated = false;
1569 descriptor_class = ImageSampler;
1570 if (immut) {
1571 sampler_ = *immut;
1572 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001573 }
1574}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001575
Tobin Ehlis300888c2016-05-18 13:43:26 -06001576void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001577 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001578 const auto &image_info = update->pImageInfo[index];
Chris Forbesfea2c542018-04-13 09:34:15 -07001579 if (!immutable_) {
1580 sampler_ = image_info.sampler;
1581 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001582 image_view_ = image_info.imageView;
1583 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001584}
1585
Tobin Ehlis300888c2016-05-18 13:43:26 -06001586void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001587 if (!immutable_) {
1588 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001589 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001590 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001591 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1592 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001593 updated = true;
1594 image_view_ = image_view;
1595 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001596}
1597
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001598void cvdescriptorset::ImageSamplerDescriptor::UpdateDrawState(layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001599 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001600 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001601 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001602 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001603 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001604 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001605 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001606 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001607 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001608 }
Jeff Bolz148d94e2018-12-13 21:25:56 -06001609 if (image_view_) {
1610 SetImageViewLayout(dev_data, cb_node, image_view_, image_layout_);
1611 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001612}
1613
Tobin Ehlis300888c2016-05-18 13:43:26 -06001614cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1615 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001616 updated = false;
1617 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001618 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001619}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001620
Tobin Ehlis300888c2016-05-18 13:43:26 -06001621void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001622 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001623 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001624 image_view_ = image_info.imageView;
1625 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001626}
1627
Tobin Ehlis300888c2016-05-18 13:43:26 -06001628void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001629 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1630 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001631 updated = true;
1632 image_view_ = image_view;
1633 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001634}
1635
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001636void cvdescriptorset::ImageDescriptor::UpdateDrawState(layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001637 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001638 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001639 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001640 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001641 }
Jeff Bolz148d94e2018-12-13 21:25:56 -06001642 if (image_view_) {
1643 SetImageViewLayout(dev_data, cb_node, image_view_, image_layout_);
1644 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001645}
1646
Tobin Ehlis300888c2016-05-18 13:43:26 -06001647cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1648 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001649 updated = false;
1650 descriptor_class = GeneralBuffer;
1651 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1652 dynamic_ = true;
1653 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1654 storage_ = true;
1655 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1656 dynamic_ = true;
1657 storage_ = true;
1658 }
1659}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001660void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001661 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001662 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001663 buffer_ = buffer_info.buffer;
1664 offset_ = buffer_info.offset;
1665 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001666}
1667
Tobin Ehlis300888c2016-05-18 13:43:26 -06001668void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1669 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001670 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001671 buffer_ = buff_desc->buffer_;
1672 offset_ = buff_desc->offset_;
1673 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001674}
1675
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001676void cvdescriptorset::BufferDescriptor::UpdateDrawState(layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001677 auto buffer_node = GetBufferState(dev_data, buffer_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001678 if (buffer_node) core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001679}
1680
Tobin Ehlis300888c2016-05-18 13:43:26 -06001681cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001682 updated = false;
1683 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001684 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001685}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001686
Tobin Ehlis300888c2016-05-18 13:43:26 -06001687void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001688 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001689 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001690}
1691
Tobin Ehlis300888c2016-05-18 13:43:26 -06001692void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1693 updated = true;
1694 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1695}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001696
John Zulauf6f3d2bd2018-10-29 17:08:42 -06001697void cvdescriptorset::TexelDescriptor::UpdateDrawState(layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001698 auto bv_state = GetBufferViewState(dev_data, buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001699 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001700 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001701 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001702}
1703
Tobin Ehlis300888c2016-05-18 13:43:26 -06001704// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1705// sets, and then calls their respective Validate[Write|Copy]Update functions.
1706// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1707// be skipped, then true is returned.
1708// If there is no issue with the update, then false is returned.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001709bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data, const layer_data *dev_data,
1710 uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
John Zulaufb45fdc32018-10-12 15:14:17 -06001711 const VkCopyDescriptorSet *p_cds, const char *func_name) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001712 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001713 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001714 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001715 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001716 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001717 if (!set_node) {
John Zulaufb45fdc32018-10-12 15:14:17 -06001718 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1719 HandleToUint64(dest_set), kVUID_Core_DrawState_InvalidDescriptorSet,
1720 "Cannot call %s on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.", func_name,
1721 HandleToUint64(dest_set));
Tobin Ehlis300888c2016-05-18 13:43:26 -06001722 } else {
Dave Houltond8ed0212018-05-16 17:18:24 -06001723 std::string error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001724 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001725 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], func_name, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001726 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 -06001727 HandleToUint64(dest_set), error_code,
John Zulaufb45fdc32018-10-12 15:14:17 -06001728 "%s failed write update validation for Descriptor Set 0x%" PRIx64 " with error: %s.", func_name,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001729 HandleToUint64(dest_set), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001730 }
1731 }
1732 }
1733 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001734 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001735 auto dst_set = p_cds[i].dstSet;
1736 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001737 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1738 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001739 // Object_tracker verifies that src & dest descriptor set are valid
1740 assert(src_node);
1741 assert(dst_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001742 std::string error_code;
Tobin Ehlisa1712752017-01-04 09:41:47 -07001743 std::string error_str;
John Zulaufb45fdc32018-10-12 15:14:17 -06001744 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, func_name, &error_code, &error_str)) {
1745 skip |=
1746 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1747 HandleToUint64(dst_set), error_code,
1748 "%s failed copy update from Descriptor Set 0x%" PRIx64 " to Descriptor Set 0x%" PRIx64 " with error: %s.",
1749 func_name, HandleToUint64(src_set), HandleToUint64(dst_set), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001750 }
1751 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001752 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001753}
1754// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1755// sets, and then calls their respective Perform[Write|Copy]Update functions.
1756// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1757// with the same set of updates.
1758// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1759// calling down the chain.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001760void cvdescriptorset::PerformUpdateDescriptorSets(const layer_data *dev_data, uint32_t write_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001761 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1762 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001763 // Write updates first
1764 uint32_t i = 0;
1765 for (i = 0; i < write_count; ++i) {
1766 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001767 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001768 if (set_node) {
1769 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001770 }
1771 }
1772 // Now copy updates
1773 for (i = 0; i < copy_count; ++i) {
1774 auto dst_set = p_cds[i].dstSet;
1775 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001776 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1777 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001778 if (src_node && dst_node) {
1779 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001780 }
1781 }
1782}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001783
John Zulaufb845eb22018-10-12 11:41:06 -06001784cvdescriptorset::DecodedTemplateUpdate::DecodedTemplateUpdate(layer_data *device_data, VkDescriptorSet descriptorSet,
John Zulauf1d27e0a2018-11-05 10:12:48 -07001785 const TEMPLATE_STATE *template_state, const void *pData,
1786 VkDescriptorSetLayout push_layout) {
John Zulaufb845eb22018-10-12 11:41:06 -06001787 auto const &create_info = template_state->create_info;
1788 inline_infos.resize(create_info.descriptorUpdateEntryCount); // Make sure we have one if we need it
1789 desc_writes.reserve(create_info.descriptorUpdateEntryCount); // emplaced, so reserved without initialization
John Zulauf1d27e0a2018-11-05 10:12:48 -07001790 VkDescriptorSetLayout effective_dsl = create_info.templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET
1791 ? create_info.descriptorSetLayout
1792 : push_layout;
1793 auto layout_obj = GetDescriptorSetLayout(device_data, effective_dsl);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001794
1795 // Create a WriteDescriptorSet struct for each template update entry
1796 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1797 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1798 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1799 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1800
John Zulaufb6d71202017-12-22 16:47:09 -07001801 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001802 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1803 desc_writes.emplace_back();
1804 auto &write_entry = desc_writes.back();
1805
1806 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1807 char *update_entry = (char *)(pData) + offset;
1808
1809 if (dst_array_element >= binding_count) {
1810 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001811 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001812 }
1813
1814 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1815 write_entry.pNext = NULL;
1816 write_entry.dstSet = descriptorSet;
1817 write_entry.dstBinding = binding_being_updated;
1818 write_entry.dstArrayElement = dst_array_element;
1819 write_entry.descriptorCount = 1;
1820 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1821
1822 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1823 case VK_DESCRIPTOR_TYPE_SAMPLER:
1824 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1825 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1826 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1827 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1828 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1829 break;
1830
1831 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1832 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1833 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1834 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1835 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1836 break;
1837
1838 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1839 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1840 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1841 break;
Dave Houlton142c4cb2018-10-17 15:04:41 -06001842 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1843 VkWriteDescriptorSetInlineUniformBlockEXT *inline_info = &inline_infos[i];
1844 inline_info->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT;
1845 inline_info->pNext = nullptr;
1846 inline_info->dataSize = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1847 inline_info->pData = update_entry;
1848 write_entry.pNext = inline_info;
1849 // skip the rest of the array, they just represent bytes in the update
1850 j = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1851 break;
1852 }
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001853 default:
1854 assert(0);
1855 break;
1856 }
1857 dst_array_element++;
1858 }
1859 }
John Zulaufb845eb22018-10-12 11:41:06 -06001860}
John Zulaufb45fdc32018-10-12 15:14:17 -06001861// These helper functions carry out the validate and record descriptor updates peformed via update templates. They decode
1862// the templatized data and leverage the non-template UpdateDescriptor helper functions.
1863bool cvdescriptorset::ValidateUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet,
1864 const TEMPLATE_STATE *template_state, const void *pData) {
1865 // Translate the templated update into a normal update for validation...
1866 DecodedTemplateUpdate decoded_update(device_data, descriptorSet, template_state, pData);
1867 return ValidateUpdateDescriptorSets(GetReportData(device_data), device_data,
1868 static_cast<uint32_t>(decoded_update.desc_writes.size()), decoded_update.desc_writes.data(),
1869 0, NULL, "vkUpdateDescriptorSetWithTemplate()");
1870}
John Zulaufb845eb22018-10-12 11:41:06 -06001871
John Zulaufb845eb22018-10-12 11:41:06 -06001872void cvdescriptorset::PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet,
John Zulaufb45fdc32018-10-12 15:14:17 -06001873 const TEMPLATE_STATE *template_state, const void *pData) {
1874 // Translate the templated update into a normal update for validation...
1875 DecodedTemplateUpdate decoded_update(device_data, descriptorSet, template_state, pData);
John Zulaufb845eb22018-10-12 11:41:06 -06001876 PerformUpdateDescriptorSets(device_data, static_cast<uint32_t>(decoded_update.desc_writes.size()),
1877 decoded_update.desc_writes.data(), 0, NULL);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001878}
John Zulauf4e7bcb52018-11-02 10:46:30 -06001879
1880std::string cvdescriptorset::DescriptorSet::StringifySetAndLayout() const {
1881 std::string out;
1882 uint64_t layout_handle = HandleToUint64(p_layout_->GetDescriptorSetLayout());
1883 if (IsPushDescriptor()) {
1884 string_sprintf(&out, "Push Descriptors defined with VkDescriptorSetLayout 0x%" PRIxLEAST64, layout_handle);
John Zulauf4e7bcb52018-11-02 10:46:30 -06001885 } else {
1886 string_sprintf(&out, "VkDescriptorSet 0x%" PRIxLEAST64 "allocated with VkDescriptorSetLayout 0x%" PRIxLEAST64,
1887 HandleToUint64(set_), layout_handle);
1888 }
1889 return out;
1890};
1891
John Zulauf1d27e0a2018-11-05 10:12:48 -07001892// Loop through the write updates to validate for a push descriptor set, ignoring dstSet
1893bool cvdescriptorset::DescriptorSet::ValidatePushDescriptorsUpdate(const debug_report_data *report_data, uint32_t write_count,
1894 const VkWriteDescriptorSet *p_wds, const char *func_name) {
1895 assert(IsPushDescriptor());
1896 bool skip = false;
1897 for (uint32_t i = 0; i < write_count; i++) {
1898 std::string error_code;
1899 std::string error_str;
1900 if (!ValidateWriteUpdate(report_data, &p_wds[i], func_name, &error_code, &error_str)) {
1901 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1902 HandleToUint64(p_layout_->GetDescriptorSetLayout()), error_code, "%s failed update validation: %s.",
1903 func_name, error_str.c_str());
1904 }
1905 }
1906 return skip;
1907}
1908
Tobin Ehlis300888c2016-05-18 13:43:26 -06001909// Validate the state for a given write update but don't actually perform the update
1910// If an error would occur for this update, return false and fill in details in error_msg string
1911bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
John Zulaufb45fdc32018-10-12 15:14:17 -06001912 const char *func_name, std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001913 // Verify dst layout still valid
1914 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001915 *error_code = "VUID-VkWriteDescriptorSet-dstSet-00320";
John Zulauf4e7bcb52018-11-02 10:46:30 -06001916 string_sprintf(error_msg, "Cannot call %s to perform write update on %s which has been destroyed", func_name,
1917 StringifySetAndLayout().c_str());
John Zulauf5dfd45c2018-01-17 11:06:34 -07001918 return false;
1919 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001920 // Verify dst binding exists
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001921 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001922 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00315";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001923 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001924 error_str << StringifySetAndLayout() << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001925 *error_msg = error_str.str();
1926 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001927 } else {
1928 // Make sure binding isn't empty
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001929 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001930 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00316";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001931 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001932 error_str << StringifySetAndLayout() << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001933 *error_msg = error_str.str();
1934 return false;
1935 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001936 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001937 // Verify idle ds
1938 if (in_use.load() &&
1939 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1940 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1941 // TODO : Re-using Free Idle error code, need write update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001942 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001943 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001944 error_str << "Cannot call " << func_name << " to perform write update on " << StringifySetAndLayout()
Jeff Bolzfdf96072018-04-10 14:32:18 -05001945 << " that is in use by a command buffer";
1946 *error_msg = error_str.str();
1947 return false;
1948 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001949 // We know that binding is valid, verify update and do update on each descriptor
John Zulaufc483f442017-12-15 14:02:06 -07001950 auto start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001951 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001952 if (type != update->descriptorType) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001953 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00319";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001954 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001955 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with type "
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001956 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1957 *error_msg = error_str.str();
1958 return false;
1959 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001960 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001961 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001962 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001963 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001964 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001965 << " descriptors in that binding and all successive bindings of the set, but update of "
1966 << update->descriptorCount << " descriptors combined with update array element offset of "
1967 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001968 *error_msg = error_str.str();
1969 return false;
1970 }
Jeff Bolze54ae892018-09-08 12:16:29 -05001971 if (type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1972 if ((update->dstArrayElement % 4) != 0) {
1973 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02219";
1974 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001975 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001976 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1977 *error_msg = error_str.str();
1978 return false;
1979 }
1980 if ((update->descriptorCount % 4) != 0) {
1981 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02220";
1982 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06001983 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001984 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1985 *error_msg = error_str.str();
1986 return false;
1987 }
1988 const auto *write_inline_info = lvl_find_in_chain<VkWriteDescriptorSetInlineUniformBlockEXT>(update->pNext);
1989 if (!write_inline_info || write_inline_info->dataSize != update->descriptorCount) {
1990 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02221";
1991 std::stringstream error_str;
1992 if (!write_inline_info) {
John Zulauf4e7bcb52018-11-02 10:46:30 -06001993 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
1994 << " with "
Jeff Bolze54ae892018-09-08 12:16:29 -05001995 << "VkWriteDescriptorSetInlineUniformBlockEXT missing";
1996 } else {
John Zulauf4e7bcb52018-11-02 10:46:30 -06001997 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
1998 << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06001999 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2000 << " not equal to "
Jeff Bolze54ae892018-09-08 12:16:29 -05002001 << "VkWriteDescriptorSet descriptorCount " << update->descriptorCount;
2002 }
2003 *error_msg = error_str.str();
2004 return false;
2005 }
2006 // This error is probably unreachable due to the previous two errors
2007 if (write_inline_info && (write_inline_info->dataSize % 4) != 0) {
2008 *error_code = "VUID-VkWriteDescriptorSetInlineUniformBlockEXT-dataSize-02222";
2009 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002010 error_str << "Attempting write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06002011 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
2012 << " not a multiple of 4";
Jeff Bolze54ae892018-09-08 12:16:29 -05002013 *error_msg = error_str.str();
2014 return false;
2015 }
2016 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002017 // Verify consecutive bindings match (if needed)
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06002018 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002019 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07002020 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Dave Houlton00c154e2018-05-24 13:20:50 -06002021 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002022 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002023 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002024 // Update is within bounds and consistent so last step is to validate update contents
John Zulaufb45fdc32018-10-12 15:14:17 -06002025 if (!VerifyWriteUpdateContents(update, start_idx, func_name, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002026 std::stringstream error_str;
John Zulauf4e7bcb52018-11-02 10:46:30 -06002027 error_str << "Write update to " << StringifySetAndLayout() << " binding #" << update->dstBinding
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06002028 << " failed with error message: " << error_msg->c_str();
2029 *error_msg = error_str.str();
2030 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002031 }
2032 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06002033 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06002034}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002035// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002036// 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 -07002037bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06002038 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002039 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06002040 auto usage = buffer_node->createInfo.usage;
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002041 const char *error_usage_bit = nullptr;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002042 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002043 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2044 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002045 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00334";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002046 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
2047 }
2048 break;
2049 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
2050 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002051 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00335";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002052 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
2053 }
2054 break;
2055 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2056 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2057 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002058 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00330";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002059 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
2060 }
2061 break;
2062 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2063 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
2064 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002065 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00331";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002066 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
2067 }
2068 break;
2069 default:
2070 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002071 }
Jeff Bolz6d3beaa2019-02-09 21:00:05 -06002072 if (error_usage_bit) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002073 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002074 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
2075 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
2076 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002077 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06002078 return false;
2079 }
2080 return true;
2081}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002082// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
2083// 1. buffer is valid
2084// 2. buffer was created with correct usage flags
2085// 3. offset is less than buffer size
2086// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002087// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002088// 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 -06002089bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
John Zulaufb45fdc32018-10-12 15:14:17 -06002090 const char *func_name, std::string *error_code,
2091 std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002092 // First make sure that buffer is valid
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002093 auto buffer_node = GetBufferState(device_data_, buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07002094 // Any invalid buffer should already be caught by object_tracker
2095 assert(buffer_node);
John Zulaufb45fdc32018-10-12 15:14:17 -06002096 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, func_name, "VUID-VkWriteDescriptorSet-descriptorType-00329")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002097 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00329";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002098 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06002099 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06002100 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002101 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002102 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
2103 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002104 return false;
2105 }
2106 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002107 if (buffer_info->offset >= buffer_node->createInfo.size) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002108 *error_code = "VUID-VkDescriptorBufferInfo-offset-00340";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002109 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07002110 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
2111 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002112 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002113 return false;
2114 }
2115 if (buffer_info->range != VK_WHOLE_SIZE) {
2116 // Range must be VK_WHOLE_SIZE or > 0
2117 if (!buffer_info->range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002118 *error_code = "VUID-VkDescriptorBufferInfo-range-00341";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002119 std::stringstream error_str;
2120 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002121 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002122 return false;
2123 }
2124 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
2125 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002126 *error_code = "VUID-VkDescriptorBufferInfo-range-00342";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002127 std::stringstream error_str;
2128 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
2129 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002130 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002131 return false;
2132 }
2133 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002134 // Check buffer update sizes against device limits
2135 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
2136 auto max_ub_range = limits_.maxUniformBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002137 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002138 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002139 std::stringstream error_str;
2140 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2141 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
2142 *error_msg = error_str.str();
2143 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002144 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_ub_range) {
2145 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
2146 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002147 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2148 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002149 << "maxUniformBufferRange (" << max_ub_range << ")";
2150 *error_msg = error_str.str();
2151 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002152 }
2153 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
2154 auto max_sb_range = limits_.maxStorageBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002155 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002156 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002157 std::stringstream error_str;
2158 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2159 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
2160 *error_msg = error_str.str();
2161 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002162 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_sb_range) {
2163 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
2164 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002165 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2166 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002167 << "maxStorageBufferRange (" << max_sb_range << ")";
2168 *error_msg = error_str.str();
2169 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002170 }
2171 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002172 return true;
2173}
2174
Tobin Ehlis300888c2016-05-18 13:43:26 -06002175// Verify that the contents of the update are ok, but don't perform actual update
2176bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
John Zulaufb45fdc32018-10-12 15:14:17 -06002177 const char *func_name, std::string *error_code,
2178 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002179 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002180 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
2181 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2182 // Validate image
2183 auto image_view = update->pImageInfo[di].imageView;
2184 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002185 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2186 error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002187 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002188 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
2189 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002190 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002191 return false;
2192 }
Tony-LunarG352f08f2019-01-03 10:11:24 -07002193 if (GetDeviceExtensions(device_data_)->vk_khr_sampler_ycbcr_conversion) {
2194 ImageSamplerDescriptor *desc = (ImageSamplerDescriptor *)descriptors_[index].get();
2195 if (desc->IsImmutableSampler()) {
2196 auto sampler_state = GetSamplerState(device_data_, desc->GetSampler());
2197 auto iv_state = GetImageViewState(device_data_, image_view);
2198 if (iv_state && sampler_state) {
2199 if (iv_state->samplerConversion != sampler_state->samplerConversion) {
2200 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-01948";
2201 std::stringstream error_str;
Tony-LunarG8a51f0a2019-01-04 16:14:14 -07002202 error_str << "Attempted write update to combined image sampler and image view and sampler ycbcr "
2203 "conversions are not identical, sampler: "
Tony-LunarG352f08f2019-01-03 10:11:24 -07002204 << desc->GetSampler() << " image view: " << iv_state->image_view << ".";
2205 *error_msg = error_str.str();
2206 return false;
2207 }
2208 }
2209 }
2210 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002211 }
2212 }
Tobin Ehlis648b1cf2018-04-13 15:24:13 -06002213 // fall through
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002214 case VK_DESCRIPTOR_TYPE_SAMPLER: {
2215 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2216 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
2217 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002218 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002219 std::stringstream error_str;
2220 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
2221 << update->pImageInfo[di].sampler << ".";
2222 *error_msg = error_str.str();
2223 return false;
2224 }
2225 } else {
2226 // TODO : Warn here
2227 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002228 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002229 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002230 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002231 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2232 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2233 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2234 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2235 auto image_view = update->pImageInfo[di].imageView;
2236 auto image_layout = update->pImageInfo[di].imageLayout;
John Zulaufb45fdc32018-10-12 15:14:17 -06002237 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, func_name, error_code,
2238 error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002239 std::stringstream error_str;
2240 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
2241 *error_msg = error_str.str();
2242 return false;
2243 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002244 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002245 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002246 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002247 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2248 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
2249 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2250 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002251 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002252 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002253 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002254 std::stringstream error_str;
2255 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2256 *error_msg = error_str.str();
2257 return false;
2258 }
2259 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002260 auto buffer_state = GetBufferState(device_data_, buffer);
2261 // Verify that buffer underlying the view hasn't been destroyed prematurely
2262 if (!buffer_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002263 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002264 std::stringstream error_str;
2265 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
2266 << ") has been destroyed: " << error_msg->c_str();
2267 *error_msg = error_str.str();
2268 return false;
2269 } else if (!ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002270 std::stringstream error_str;
2271 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
2272 *error_msg = error_str.str();
2273 return false;
2274 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002275 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002276 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002277 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002278 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2279 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2280 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2281 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2282 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
John Zulaufb45fdc32018-10-12 15:14:17 -06002283 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, func_name, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002284 std::stringstream error_str;
2285 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
2286 *error_msg = error_str.str();
2287 return false;
2288 }
2289 }
2290 break;
2291 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002292 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
2293 break;
Eric Werness30127fd2018-10-31 21:01:03 -07002294 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002295 // XXX TODO
2296 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002297 default:
2298 assert(0); // We've already verified update type so should never get here
2299 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002300 }
2301 // All checks passed so update contents are good
2302 return true;
2303}
2304// Verify that the contents of the update are ok, but don't perform actual update
2305bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
John Zulaufb45fdc32018-10-12 15:14:17 -06002306 VkDescriptorType type, uint32_t index, const char *func_name,
2307 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002308 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
2309 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06002310 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002311 case PlainSampler: {
2312 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002313 const auto src_desc = src_set->descriptors_[index + di].get();
2314 if (!src_desc->updated) continue;
2315 if (!src_desc->IsImmutableSampler()) {
2316 auto update_sampler = static_cast<SamplerDescriptor *>(src_desc)->GetSampler();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002317 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002318 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002319 std::stringstream error_str;
2320 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2321 *error_msg = error_str.str();
2322 return false;
2323 }
2324 } else {
2325 // TODO : Warn here
2326 }
2327 }
2328 break;
2329 }
2330 case ImageSampler: {
2331 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002332 const auto src_desc = src_set->descriptors_[index + di].get();
2333 if (!src_desc->updated) continue;
2334 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002335 // First validate sampler
2336 if (!img_samp_desc->IsImmutableSampler()) {
2337 auto update_sampler = img_samp_desc->GetSampler();
2338 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002339 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002340 std::stringstream error_str;
2341 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2342 *error_msg = error_str.str();
2343 return false;
2344 }
2345 } else {
2346 // TODO : Warn here
2347 }
2348 // Validate image
2349 auto image_view = img_samp_desc->GetImageView();
2350 auto image_layout = img_samp_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002351 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002352 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002353 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002354 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002355 return false;
2356 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002357 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002358 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002359 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002360 case Image: {
2361 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002362 const auto src_desc = src_set->descriptors_[index + di].get();
2363 if (!src_desc->updated) continue;
2364 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002365 auto image_view = img_desc->GetImageView();
2366 auto image_layout = img_desc->GetImageLayout();
John Zulaufb45fdc32018-10-12 15:14:17 -06002367 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, func_name, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002368 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002369 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002370 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002371 return false;
2372 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002373 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002374 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002375 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002376 case TexelBuffer: {
2377 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002378 const auto src_desc = src_set->descriptors_[index + di].get();
2379 if (!src_desc->updated) continue;
2380 auto buffer_view = static_cast<TexelDescriptor *>(src_desc)->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002381 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002382 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002383 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002384 std::stringstream error_str;
2385 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2386 *error_msg = error_str.str();
2387 return false;
2388 }
2389 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002390 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002391 std::stringstream error_str;
2392 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
2393 *error_msg = error_str.str();
2394 return false;
2395 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002396 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002397 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002398 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002399 case GeneralBuffer: {
2400 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002401 const auto src_desc = src_set->descriptors_[index + di].get();
2402 if (!src_desc->updated) continue;
2403 auto buffer = static_cast<BufferDescriptor *>(src_desc)->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002404 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002405 std::stringstream error_str;
2406 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
2407 *error_msg = error_str.str();
2408 return false;
2409 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06002410 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002411 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002412 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002413 case InlineUniform:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002414 case AccelerationStructure:
Jeff Bolze54ae892018-09-08 12:16:29 -05002415 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002416 default:
2417 assert(0); // We've already verified update type so should never get here
2418 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002419 }
2420 // All checks passed so update contents are good
2421 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12002422}
Tobin Ehlisf320b192017-03-14 11:22:50 -06002423// Update the common AllocateDescriptorSetsData
2424void cvdescriptorset::UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
2425 AllocateDescriptorSetsData *ds_data) {
2426 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
2427 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
2428 if (layout) {
2429 ds_data->layout_nodes[i] = layout;
2430 // Count total descriptors required per type
2431 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
2432 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
2433 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
2434 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
2435 }
2436 }
2437 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
2438 }
Petr Kraus13c98a62017-12-09 00:22:39 +01002439}
Tobin Ehlisee471462016-05-26 11:21:59 -06002440// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlisf320b192017-03-14 11:22:50 -06002441bool cvdescriptorset::ValidateAllocateDescriptorSets(const core_validation::layer_data *dev_data,
2442 const VkDescriptorSetAllocateInfo *p_alloc_info,
2443 const AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002444 bool skip = false;
Tobin Ehlisf320b192017-03-14 11:22:50 -06002445 auto report_data = core_validation::GetReportData(dev_data);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002446 auto pool_state = GetDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlisee471462016-05-26 11:21:59 -06002447
2448 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002449 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07002450 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
John Zulauf1d27e0a2018-11-05 10:12:48 -07002451 if (layout->IsPushDescriptor()) {
John Zulauf5562d062018-01-24 11:54:05 -07002452 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 -06002453 HandleToUint64(p_alloc_info->pSetLayouts[i]), "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-00308",
John Zulauf5562d062018-01-24 11:54:05 -07002454 "Layout 0x%" PRIxLEAST64 " specified at pSetLayouts[%" PRIu32
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002455 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
John Zulauf5562d062018-01-24 11:54:05 -07002456 HandleToUint64(p_alloc_info->pSetLayouts[i]), i,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002457 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07002458 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002459 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT &&
2460 !(pool_state->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
2461 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 -06002462 0, "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-03044",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002463 "Descriptor set layout create flags and pool create flags mismatch for index (%d)", i);
2464 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002465 }
2466 }
Mark Lobodzinski28426ae2017-06-01 07:56:38 -06002467 if (!GetDeviceExtensions(dev_data)->vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002468 // Track number of descriptorSets allowable in this pool
2469 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002470 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 -06002471 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorSetCount-00306",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002472 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002473 ". This pool only has %d descriptorSets remaining.",
2474 p_alloc_info->descriptorSetCount, HandleToUint64(pool_state->pool), pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002475 }
2476 // Determine whether descriptor counts are satisfiable
Jeff Bolze54ae892018-09-08 12:16:29 -05002477 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2478 if (ds_data->required_descriptors_by_type.at(it->first) > pool_state->availableDescriptorTypeCount[it->first]) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002479 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 -06002480 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002481 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002482 ". This pool only has %d descriptors of this type remaining.",
Dave Houlton142c4cb2018-10-17 15:04:41 -06002483 ds_data->required_descriptors_by_type.at(it->first),
2484 string_VkDescriptorType(VkDescriptorType(it->first)), HandleToUint64(pool_state->pool),
2485 pool_state->availableDescriptorTypeCount[it->first]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002486 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002487 }
2488 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06002489
Jeff Bolzfdf96072018-04-10 14:32:18 -05002490 const auto *count_allocate_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2491
2492 if (count_allocate_info) {
2493 if (count_allocate_info->descriptorSetCount != 0 &&
2494 count_allocate_info->descriptorSetCount != p_alloc_info->descriptorSetCount) {
2495 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 -06002496 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-descriptorSetCount-03045",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002497 "VkDescriptorSetAllocateInfo::descriptorSetCount (%d) != "
2498 "VkDescriptorSetVariableDescriptorCountAllocateInfoEXT::descriptorSetCount (%d)",
2499 p_alloc_info->descriptorSetCount, count_allocate_info->descriptorSetCount);
2500 }
2501 if (count_allocate_info->descriptorSetCount == p_alloc_info->descriptorSetCount) {
2502 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
2503 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
2504 if (count_allocate_info->pDescriptorCounts[i] > layout->GetDescriptorCountFromBinding(layout->GetMaxBinding())) {
2505 skip |= log_msg(
2506 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 -06002507 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-pSetLayouts-03046",
2508 "pDescriptorCounts[%d] = (%d), binding's descriptorCount = (%d)", i,
Jeff Bolzfdf96072018-04-10 14:32:18 -05002509 count_allocate_info->pDescriptorCounts[i], layout->GetDescriptorCountFromBinding(layout->GetMaxBinding()));
2510 }
2511 }
2512 }
2513 }
2514
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002515 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06002516}
2517// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06002518void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
2519 const VkDescriptorSet *descriptor_sets,
2520 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06002521 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06002522 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
John Zulauf48a6a702017-12-22 17:14:54 -07002523 layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06002524 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002525 // Account for sets and individual descriptors allocated from pool
Tobin Ehlisee471462016-05-26 11:21:59 -06002526 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Jeff Bolze54ae892018-09-08 12:16:29 -05002527 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2528 pool_state->availableDescriptorTypeCount[it->first] -= ds_data->required_descriptors_by_type.at(it->first);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06002529 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002530
2531 const auto *variable_count_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2532 bool variable_count_valid = variable_count_info && variable_count_info->descriptorSetCount == p_alloc_info->descriptorSetCount;
2533
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002534 // Create tracking object for each descriptor set; insert into global map and the pool's set.
Tobin Ehlisee471462016-05-26 11:21:59 -06002535 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -05002536 uint32_t variable_count = variable_count_valid ? variable_count_info->pDescriptorCounts[i] : 0;
2537
Tobin Ehlis93f22372016-10-12 14:34:12 -06002538 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 -05002539 variable_count, dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06002540
2541 pool_state->sets.insert(new_ds);
2542 new_ds->in_use.store(0);
2543 (*set_map)[descriptor_sets[i]] = new_ds;
2544 }
2545}
John Zulauf48a6a702017-12-22 17:14:54 -07002546
2547cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2548 GLOBAL_CB_NODE *cb_state)
2549 : filtered_map_(), orig_map_(in_map) {
2550 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2551 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2552 ds.FilterAndTrackBindingReqs(cb_state, orig_map_, filtered_map_.get());
2553 }
2554}
2555cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2556 GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline)
2557 : filtered_map_(), orig_map_(in_map) {
2558 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2559 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2560 ds.FilterAndTrackBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
2561 }
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01002562}