blob: a3b7030ffce2c83ec23f73e2634b7f3a45d418d3 [file] [log] [blame]
Dave Houlton51653902018-06-22 17:32:13 -06001/* Copyright (c) 2015-2018 The Khronos Group Inc.
2 * Copyright (c) 2015-2018 Valve Corporation
3 * Copyright (c) 2015-2018 LunarG, Inc.
4 * Copyright (C) 2015-2018 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 Zulauf1f8174b2018-02-16 12:58:37 -070033#include <memory>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060034
Jeff Bolzfdf96072018-04-10 14:32:18 -050035// ExtendedBinding collects a VkDescriptorSetLayoutBinding and any extended
36// state that comes from a different array/structure so they can stay together
37// while being sorted by binding number.
38struct ExtendedBinding {
39 ExtendedBinding(const VkDescriptorSetLayoutBinding *l, VkDescriptorBindingFlagsEXT f) : layout_binding(l), binding_flags(f) {}
40
41 const VkDescriptorSetLayoutBinding *layout_binding;
42 VkDescriptorBindingFlagsEXT binding_flags;
43};
44
John Zulauf508d13a2018-01-05 15:10:34 -070045struct BindingNumCmp {
Jeff Bolzfdf96072018-04-10 14:32:18 -050046 bool operator()(const ExtendedBinding &a, const ExtendedBinding &b) const {
47 return a.layout_binding->binding < b.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070048 }
49};
50
John Zulaufd47d0612018-02-16 13:00:34 -070051using DescriptorSetLayoutDef = cvdescriptorset::DescriptorSetLayoutDef;
52using DescriptorSetLayoutId = cvdescriptorset::DescriptorSetLayoutId;
53
John Zulauf34ebf272018-02-16 13:08:47 -070054// Canonical dictionary of DescriptorSetLayoutDef (without any handle/device specific information)
55cvdescriptorset::DescriptorSetLayoutDict descriptor_set_layout_dict;
John Zulaufd47d0612018-02-16 13:00:34 -070056
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060057DescriptorSetLayoutId GetCanonicalId(const VkDescriptorSetLayoutCreateInfo *p_create_info) {
John Zulauf34ebf272018-02-16 13:08:47 -070058 return descriptor_set_layout_dict.look_up(DescriptorSetLayoutDef(p_create_info));
John Zulaufd47d0612018-02-16 13:00:34 -070059}
John Zulauf34ebf272018-02-16 13:08:47 -070060
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060061// Construct DescriptorSetLayout instance from given create info
John Zulauf48a6a702017-12-22 17:14:54 -070062// Proactively reserve and resize as possible, as the reallocation was visible in profiling
John Zulauf1f8174b2018-02-16 12:58:37 -070063cvdescriptorset::DescriptorSetLayoutDef::DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info)
64 : flags_(p_create_info->flags), binding_count_(0), descriptor_count_(0), dynamic_descriptor_count_(0) {
Jeff Bolzfdf96072018-04-10 14:32:18 -050065 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(p_create_info->pNext);
66
John Zulauf48a6a702017-12-22 17:14:54 -070067 binding_type_stats_ = {0, 0, 0};
Jeff Bolzfdf96072018-04-10 14:32:18 -050068 std::set<ExtendedBinding, BindingNumCmp> sorted_bindings;
John Zulauf508d13a2018-01-05 15:10:34 -070069 const uint32_t input_bindings_count = p_create_info->bindingCount;
70 // Sort the input bindings in binding number order, eliminating duplicates
71 for (uint32_t i = 0; i < input_bindings_count; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -050072 VkDescriptorBindingFlagsEXT flags = 0;
73 if (flags_create_info && flags_create_info->bindingCount == p_create_info->bindingCount) {
74 flags = flags_create_info->pBindingFlags[i];
75 }
76 sorted_bindings.insert(ExtendedBinding(p_create_info->pBindings + i, flags));
John Zulaufb6d71202017-12-22 16:47:09 -070077 }
78
79 // Store the create info in the sorted order from above
Tobin Ehlisa3525e02016-11-17 10:50:52 -070080 std::map<uint32_t, uint32_t> binding_to_dyn_count;
John Zulauf508d13a2018-01-05 15:10:34 -070081 uint32_t index = 0;
82 binding_count_ = static_cast<uint32_t>(sorted_bindings.size());
83 bindings_.reserve(binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -050084 binding_flags_.reserve(binding_count_);
John Zulauf508d13a2018-01-05 15:10:34 -070085 binding_to_index_map_.reserve(binding_count_);
86 for (auto input_binding : sorted_bindings) {
87 // Add to binding and map, s.t. it is robust to invalid duplication of binding_num
Jeff Bolzfdf96072018-04-10 14:32:18 -050088 const auto binding_num = input_binding.layout_binding->binding;
John Zulauf508d13a2018-01-05 15:10:34 -070089 binding_to_index_map_[binding_num] = index++;
Jeff Bolzfdf96072018-04-10 14:32:18 -050090 bindings_.emplace_back(input_binding.layout_binding);
John Zulauf508d13a2018-01-05 15:10:34 -070091 auto &binding_info = bindings_.back();
Jeff Bolzfdf96072018-04-10 14:32:18 -050092 binding_flags_.emplace_back(input_binding.binding_flags);
John Zulauf508d13a2018-01-05 15:10:34 -070093
John Zulaufb6d71202017-12-22 16:47:09 -070094 descriptor_count_ += binding_info.descriptorCount;
95 if (binding_info.descriptorCount > 0) {
96 non_empty_bindings_.insert(binding_num);
Tobin Ehlis9637fb22016-12-12 15:59:34 -070097 }
John Zulaufb6d71202017-12-22 16:47:09 -070098
99 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
100 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
101 binding_to_dyn_count[binding_num] = binding_info.descriptorCount;
102 dynamic_descriptor_count_ += binding_info.descriptorCount;
John Zulauf48a6a702017-12-22 17:14:54 -0700103 binding_type_stats_.dynamic_buffer_count++;
104 } else if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
105 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
106 binding_type_stats_.non_dynamic_buffer_count++;
107 } else {
108 binding_type_stats_.image_sampler_count++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600109 }
110 }
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700111 assert(bindings_.size() == binding_count_);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500112 assert(binding_flags_.size() == binding_count_);
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700113 uint32_t global_index = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700114 binding_to_global_index_range_map_.reserve(binding_count_);
115 // Vector order is finalized so create maps of bindings to descriptors and descriptors to indices
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700116 for (uint32_t i = 0; i < binding_count_; ++i) {
117 auto binding_num = bindings_[i].binding;
John Zulaufc483f442017-12-15 14:02:06 -0700118 auto final_index = global_index + bindings_[i].descriptorCount;
119 binding_to_global_index_range_map_[binding_num] = IndexRange(global_index, final_index);
John Zulaufb6d71202017-12-22 16:47:09 -0700120 if (final_index != global_index) {
121 global_start_to_index_map_[global_index] = i;
122 }
John Zulaufc483f442017-12-15 14:02:06 -0700123 global_index = final_index;
Tobin Ehlis9637fb22016-12-12 15:59:34 -0700124 }
John Zulaufb6d71202017-12-22 16:47:09 -0700125
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700126 // Now create dyn offset array mapping for any dynamic descriptors
127 uint32_t dyn_array_idx = 0;
John Zulaufb6d71202017-12-22 16:47:09 -0700128 binding_to_dynamic_array_idx_map_.reserve(binding_to_dyn_count.size());
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700129 for (const auto &bc_pair : binding_to_dyn_count) {
130 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
131 dyn_array_idx += bc_pair.second;
132 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600133}
Tobin Ehlis154c2692016-10-25 09:36:53 -0600134
John Zulaufd47d0612018-02-16 13:00:34 -0700135size_t cvdescriptorset::DescriptorSetLayoutDef::hash() const {
136 hash_util::HashCombiner hc;
137 hc << flags_;
138 hc.Combine(bindings_);
139 return hc.Value();
140}
141//
142
John Zulauf1f8174b2018-02-16 12:58:37 -0700143// Return valid index or "end" i.e. binding_count_;
144// The asserts in "Get" are reduced to the set where no valid answer(like null or 0) could be given
145// Common code for all binding lookups.
146uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromBinding(uint32_t binding) const {
147 const auto &bi_itr = binding_to_index_map_.find(binding);
148 if (bi_itr != binding_to_index_map_.cend()) return bi_itr->second;
149 return GetBindingCount();
150}
151VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorSetLayoutBindingPtrFromIndex(
152 const uint32_t index) const {
153 if (index >= bindings_.size()) return nullptr;
154 return bindings_[index].ptr();
155}
156// Return descriptorCount for given index, 0 if index is unavailable
157uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorCountFromIndex(const uint32_t index) const {
158 if (index >= bindings_.size()) return 0;
159 return bindings_[index].descriptorCount;
160}
161// For the given index, return descriptorType
162VkDescriptorType cvdescriptorset::DescriptorSetLayoutDef::GetTypeFromIndex(const uint32_t index) const {
163 assert(index < bindings_.size());
164 if (index < bindings_.size()) return bindings_[index].descriptorType;
165 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
166}
167// For the given index, return stageFlags
168VkShaderStageFlags cvdescriptorset::DescriptorSetLayoutDef::GetStageFlagsFromIndex(const uint32_t index) const {
169 assert(index < bindings_.size());
170 if (index < bindings_.size()) return bindings_[index].stageFlags;
171 return VkShaderStageFlags(0);
172}
Jeff Bolzfdf96072018-04-10 14:32:18 -0500173// Return binding flags for given index, 0 if index is unavailable
174VkDescriptorBindingFlagsEXT cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorBindingFlagsFromIndex(
175 const uint32_t index) const {
176 if (index >= binding_flags_.size()) return 0;
177 return binding_flags_[index];
178}
John Zulauf1f8174b2018-02-16 12:58:37 -0700179
180// For the given global index, return index
181uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromGlobalIndex(const uint32_t global_index) const {
182 auto start_it = global_start_to_index_map_.upper_bound(global_index);
183 uint32_t index = binding_count_;
184 assert(start_it != global_start_to_index_map_.cbegin());
185 if (start_it != global_start_to_index_map_.cbegin()) {
186 --start_it;
187 index = start_it->second;
188#ifndef NDEBUG
189 const auto &range = GetGlobalIndexRangeFromBinding(bindings_[index].binding);
190 assert(range.start <= global_index && global_index < range.end);
191#endif
192 }
193 return index;
194}
195
196// For the given binding, return the global index range
197// As start and end are often needed in pairs, get both with a single hash lookup.
198const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromBinding(
199 const uint32_t binding) const {
200 assert(binding_to_global_index_range_map_.count(binding));
201 // In error case max uint32_t so index is out of bounds to break ASAP
202 const static IndexRange kInvalidRange = {0xFFFFFFFF, 0xFFFFFFFF};
203 const auto &range_it = binding_to_global_index_range_map_.find(binding);
204 if (range_it != binding_to_global_index_range_map_.end()) {
205 return range_it->second;
206 }
207 return kInvalidRange;
208}
209
210// For given binding, return ptr to ImmutableSampler array
211VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
212 const auto &bi_itr = binding_to_index_map_.find(binding);
213 if (bi_itr != binding_to_index_map_.end()) {
214 return bindings_[bi_itr->second].pImmutableSamplers;
215 }
216 return nullptr;
217}
218// Move to next valid binding having a non-zero binding count
219uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetNextValidBinding(const uint32_t binding) const {
220 auto it = non_empty_bindings_.upper_bound(binding);
221 assert(it != non_empty_bindings_.cend());
222 if (it != non_empty_bindings_.cend()) return *it;
223 return GetMaxBinding() + 1;
224}
225// For given index, return ptr to ImmutableSampler array
226VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
227 if (index < bindings_.size()) {
228 return bindings_[index].pImmutableSamplers;
229 }
230 return nullptr;
231}
232// If our layout is compatible with rh_ds_layout, return true,
233// else return false and fill in error_msg will description of what causes incompatibility
234bool cvdescriptorset::DescriptorSetLayout::IsCompatible(DescriptorSetLayout const *const rh_ds_layout,
235 std::string *error_msg) const {
236 // Trivial case
237 if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) return true;
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600238 if (GetLayoutDef() == rh_ds_layout->GetLayoutDef()) return true;
John Zulaufd47d0612018-02-16 13:00:34 -0700239 bool detailed_compat_check =
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600240 GetLayoutDef()->IsCompatible(layout_, rh_ds_layout->GetDescriptorSetLayout(), rh_ds_layout->GetLayoutDef(), error_msg);
John Zulaufd47d0612018-02-16 13:00:34 -0700241 // The detailed check should never tell us mismatching DSL are compatible
242 assert(!detailed_compat_check);
243 return detailed_compat_check;
John Zulauf1f8174b2018-02-16 12:58:37 -0700244}
245
John Zulaufdf3c5c12018-03-06 16:44:43 -0700246// Do a detailed compatibility check of this def (referenced by ds_layout), vs. the rhs (layout and def)
247// Should only be called if trivial accept has failed, and in that context should return false.
John Zulauf1f8174b2018-02-16 12:58:37 -0700248bool cvdescriptorset::DescriptorSetLayoutDef::IsCompatible(VkDescriptorSetLayout ds_layout, VkDescriptorSetLayout rh_ds_layout,
249 DescriptorSetLayoutDef const *const rh_ds_layout_def,
250 std::string *error_msg) const {
251 if (descriptor_count_ != rh_ds_layout_def->descriptor_count_) {
252 std::stringstream error_str;
253 error_str << "DescriptorSetLayout " << ds_layout << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
254 << rh_ds_layout << ", which comes from pipelineLayout, has " << rh_ds_layout_def->descriptor_count_
255 << " descriptors.";
256 *error_msg = error_str.str();
257 return false; // trivial fail case
258 }
John Zulaufd47d0612018-02-16 13:00:34 -0700259
John Zulauf1f8174b2018-02-16 12:58:37 -0700260 // Descriptor counts match so need to go through bindings one-by-one
261 // and verify that type and stageFlags match
262 for (auto binding : bindings_) {
263 // TODO : Do we also need to check immutable samplers?
264 // VkDescriptorSetLayoutBinding *rh_binding;
265 if (binding.descriptorCount != rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding)) {
266 std::stringstream error_str;
267 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has a descriptorCount of "
268 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
269 << rh_ds_layout << ", which comes from pipelineLayout, has a descriptorCount of "
270 << rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding);
271 *error_msg = error_str.str();
272 return false;
273 } else if (binding.descriptorType != rh_ds_layout_def->GetTypeFromBinding(binding.binding)) {
274 std::stringstream error_str;
275 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " is type '"
276 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
277 << " for DescriptorSetLayout " << rh_ds_layout << ", which comes from pipelineLayout, is type '"
278 << string_VkDescriptorType(rh_ds_layout_def->GetTypeFromBinding(binding.binding)) << "'";
279 *error_msg = error_str.str();
280 return false;
281 } else if (binding.stageFlags != rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding)) {
282 std::stringstream error_str;
283 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has stageFlags "
284 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout " << rh_ds_layout
285 << ", which comes from pipelineLayout, has stageFlags "
286 << rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding);
287 *error_msg = error_str.str();
288 return false;
289 }
290 }
291 return true;
292}
293
294bool cvdescriptorset::DescriptorSetLayoutDef::IsNextBindingConsistent(const uint32_t binding) const {
295 if (!binding_to_index_map_.count(binding + 1)) return false;
296 auto const &bi_itr = binding_to_index_map_.find(binding);
297 if (bi_itr != binding_to_index_map_.end()) {
298 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
299 if (next_bi_itr != binding_to_index_map_.end()) {
300 auto type = bindings_[bi_itr->second].descriptorType;
301 auto stage_flags = bindings_[bi_itr->second].stageFlags;
302 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
Jeff Bolzfdf96072018-04-10 14:32:18 -0500303 auto flags = binding_flags_[bi_itr->second];
John Zulauf1f8174b2018-02-16 12:58:37 -0700304 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
305 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
Jeff Bolzfdf96072018-04-10 14:32:18 -0500306 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false)) ||
307 (flags != binding_flags_[next_bi_itr->second])) {
John Zulauf1f8174b2018-02-16 12:58:37 -0700308 return false;
309 }
310 return true;
311 }
312 }
313 return false;
314}
315// Starting at offset descriptor of given binding, parse over update_count
316// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
317// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
318// If so, return true. If not, fill in error_msg and return false
319bool cvdescriptorset::DescriptorSetLayoutDef::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset,
320 uint32_t update_count, const char *type,
321 const VkDescriptorSet set, std::string *error_msg) const {
322 // Verify consecutive bindings match (if needed)
323 auto orig_binding = current_binding;
324 // Track count of descriptors in the current_bindings that are remaining to be updated
325 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
326 // First, it's legal to offset beyond your own binding so handle that case
327 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
328 while (offset >= binding_remaining) {
329 // Advance to next binding, decrement offset by binding size
330 offset -= binding_remaining;
331 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
332 }
333 binding_remaining -= offset;
334 while (update_count > binding_remaining) { // While our updates overstep current binding
335 // Verify next consecutive binding matches type, stage flags & immutable sampler use
336 if (!IsNextBindingConsistent(current_binding++)) {
337 std::stringstream error_str;
338 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
339 << update_count
340 << " descriptors being updated but this update oversteps the bounds of this binding and the next binding is "
341 "not consistent with current binding so this update is invalid.";
342 *error_msg = error_str.str();
343 return false;
344 }
345 // For sake of this check consider the bindings updated and grab count for next binding
346 update_count -= binding_remaining;
347 binding_remaining = GetDescriptorCountFromBinding(current_binding);
348 }
349 return true;
350}
351
352// The DescriptorSetLayout stores the per handle data for a descriptor set layout, and references the common defintion for the
353// handle invariant portion
354cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
355 const VkDescriptorSetLayout layout)
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600356 : layout_(layout), layout_destroyed_(false), layout_id_(GetCanonicalId(p_create_info)) {}
John Zulauf1f8174b2018-02-16 12:58:37 -0700357
Tobin Ehlis154c2692016-10-25 09:36:53 -0600358// Validate descriptor set layout create info
Jeff Bolzfdf96072018-04-10 14:32:18 -0500359bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(
360 const debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *create_info, const bool push_descriptor_ext,
361 const uint32_t max_push_descriptors, const bool descriptor_indexing_ext,
Jeff Bolze54ae892018-09-08 12:16:29 -0500362 const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features,
363 const VkPhysicalDeviceInlineUniformBlockFeaturesEXT *inline_uniform_block_features,
364 const VkPhysicalDeviceInlineUniformBlockPropertiesEXT *inline_uniform_block_props) {
Tobin Ehlis154c2692016-10-25 09:36:53 -0600365 bool skip = false;
366 std::unordered_set<uint32_t> bindings;
John Zulauf0fdeab32018-01-23 11:27:35 -0700367 uint64_t total_descriptors = 0;
368
Jeff Bolzfdf96072018-04-10 14:32:18 -0500369 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(create_info->pNext);
370
371 const bool push_descriptor_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR);
John Zulauf0fdeab32018-01-23 11:27:35 -0700372 if (push_descriptor_set && !push_descriptor_ext) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600373 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 -0600374 kVUID_Core_DrawState_ExtensionNotEnabled,
Mark Lobodzinskifb5a3e62018-04-13 10:46:48 -0600375 "Attempted to use %s in %s but its required extension %s has not been enabled.\n",
John Zulauf0fdeab32018-01-23 11:27:35 -0700376 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR", "VkDescriptorSetLayoutCreateInfo::flags",
377 VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
378 }
379
Jeff Bolzfdf96072018-04-10 14:32:18 -0500380 const bool update_after_bind_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT);
381 if (update_after_bind_set && !descriptor_indexing_ext) {
382 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -0600383 kVUID_Core_DrawState_ExtensionNotEnabled,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500384 "Attemped to use %s in %s but its required extension %s has not been enabled.\n",
385 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT", "VkDescriptorSetLayoutCreateInfo::flags",
386 VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
387 }
388
John Zulauf0fdeab32018-01-23 11:27:35 -0700389 auto valid_type = [push_descriptor_set](const VkDescriptorType type) {
390 return !push_descriptor_set ||
Dave Houlton142c4cb2018-10-17 15:04:41 -0600391 ((type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && (type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) &&
Jeff Bolze54ae892018-09-08 12:16:29 -0500392 (type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT));
John Zulauf0fdeab32018-01-23 11:27:35 -0700393 };
394
Jeff Bolzfdf96072018-04-10 14:32:18 -0500395 uint32_t max_binding = 0;
396
Tobin Ehlis154c2692016-10-25 09:36:53 -0600397 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
John Zulauf0fdeab32018-01-23 11:27:35 -0700398 const auto &binding_info = create_info->pBindings[i];
Jeff Bolzfdf96072018-04-10 14:32:18 -0500399 max_binding = std::max(max_binding, binding_info.binding);
400
John Zulauf0fdeab32018-01-23 11:27:35 -0700401 if (!bindings.insert(binding_info.binding).second) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600402 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 -0600403 "VUID-VkDescriptorSetLayoutCreateInfo-binding-00279",
404 "duplicated binding number in VkDescriptorSetLayoutBinding.");
Tobin Ehlis154c2692016-10-25 09:36:53 -0600405 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700406 if (!valid_type(binding_info.descriptorType)) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600407 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 -0600408 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
409 ? "VUID-VkDescriptorSetLayoutCreateInfo-flags-02208"
410 : "VUID-VkDescriptorSetLayoutCreateInfo-flags-00280",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600411 "invalid type %s ,for push descriptors in VkDescriptorSetLayoutBinding entry %" PRIu32 ".",
412 string_VkDescriptorType(binding_info.descriptorType), i);
John Zulauf0fdeab32018-01-23 11:27:35 -0700413 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500414
415 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
416 if ((binding_info.descriptorCount % 4) != 0) {
417 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
418 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02209",
Dave Houlton142c4cb2018-10-17 15:04:41 -0600419 "descriptorCount =(%" PRIu32 ") must be a multiple of 4", binding_info.descriptorCount);
Jeff Bolze54ae892018-09-08 12:16:29 -0500420 }
421 if (binding_info.descriptorCount > inline_uniform_block_props->maxInlineUniformBlockSize) {
422 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
423 "VUID-VkDescriptorSetLayoutBinding-descriptorType-02210",
424 "descriptorCount =(%" PRIu32 ") must be less than or equal to maxInlineUniformBlockSize",
425 binding_info.descriptorCount);
426 }
427 }
428
John Zulauf0fdeab32018-01-23 11:27:35 -0700429 total_descriptors += binding_info.descriptorCount;
Tobin Ehlis154c2692016-10-25 09:36:53 -0600430 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700431
Jeff Bolzfdf96072018-04-10 14:32:18 -0500432 if (flags_create_info) {
433 if (flags_create_info->bindingCount != 0 && flags_create_info->bindingCount != create_info->bindingCount) {
434 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 -0600435 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-bindingCount-03002",
Jeff Bolzfdf96072018-04-10 14:32:18 -0500436 "VkDescriptorSetLayoutCreateInfo::bindingCount (%d) != "
437 "VkDescriptorSetLayoutBindingFlagsCreateInfoEXT::bindingCount (%d)",
438 create_info->bindingCount, flags_create_info->bindingCount);
439 }
440
441 if (flags_create_info->bindingCount == create_info->bindingCount) {
442 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
443 const auto &binding_info = create_info->pBindings[i];
444
445 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) {
446 if (!update_after_bind_set) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600447 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
448 "VUID-VkDescriptorSetLayoutCreateInfo-flags-03000",
449 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500450 }
451
452 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER &&
453 !descriptor_indexing_features->descriptorBindingUniformBufferUpdateAfterBind) {
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-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
456 "descriptorBindingUniformBufferUpdateAfterBind-03005",
457 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500458 }
459 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
460 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
461 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) &&
462 !descriptor_indexing_features->descriptorBindingSampledImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600463 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
464 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
465 "descriptorBindingSampledImageUpdateAfterBind-03006",
466 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500467 }
468 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE &&
469 !descriptor_indexing_features->descriptorBindingStorageImageUpdateAfterBind) {
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 "descriptorBindingStorageImageUpdateAfterBind-03007",
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_BUFFER &&
476 !descriptor_indexing_features->descriptorBindingStorageBufferUpdateAfterBind) {
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 "descriptorBindingStorageBufferUpdateAfterBind-03008",
480 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500481 }
482 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
483 !descriptor_indexing_features->descriptorBindingUniformTexelBufferUpdateAfterBind) {
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 "descriptorBindingUniformTexelBufferUpdateAfterBind-03009",
487 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500488 }
489 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER &&
490 !descriptor_indexing_features->descriptorBindingStorageTexelBufferUpdateAfterBind) {
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 "descriptorBindingStorageTexelBufferUpdateAfterBind-03010",
494 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500495 }
496 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT ||
497 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
498 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600499 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
500 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-None-03011",
501 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500502 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500503
504 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT &&
505 !inline_uniform_block_features->descriptorBindingInlineUniformBlockUpdateAfterBind) {
506 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 -0600507 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
508 "descriptorBindingInlineUniformBlockUpdateAfterBind-02211",
509 "Invalid flags (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) for "
510 "VkDescriptorSetLayoutBinding entry %" PRIu32
511 " with descriptorBindingInlineUniformBlockUpdateAfterBind not enabled",
512 i);
Jeff Bolze54ae892018-09-08 12:16:29 -0500513 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500514 }
515
516 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT) {
517 if (!descriptor_indexing_features->descriptorBindingUpdateUnusedWhilePending) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600518 skip |= log_msg(
519 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
520 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingUpdateUnusedWhilePending-03012",
521 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500522 }
523 }
524
525 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) {
526 if (!descriptor_indexing_features->descriptorBindingPartiallyBound) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600527 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
528 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingPartiallyBound-03013",
529 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500530 }
531 }
532
533 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT) {
534 if (binding_info.binding != max_binding) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600535 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
536 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03004",
537 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500538 }
539
540 if (!descriptor_indexing_features->descriptorBindingVariableDescriptorCount) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600541 skip |= log_msg(
542 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
543 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingVariableDescriptorCount-03014",
544 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500545 }
546 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
547 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600548 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
549 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03015",
550 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500551 }
552 }
553
554 if (push_descriptor_set &&
555 (flags_create_info->pBindingFlags[i] &
556 (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
557 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT))) {
558 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 -0600559 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-flags-03003",
560 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500561 }
562 }
563 }
564 }
565
John Zulauf0fdeab32018-01-23 11:27:35 -0700566 if ((push_descriptor_set) && (total_descriptors > max_push_descriptors)) {
567 const char *undefined = push_descriptor_ext ? "" : " -- undefined";
Dave Houltond8ed0212018-05-16 17:18:24 -0600568 skip |=
569 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
570 "VUID-VkDescriptorSetLayoutCreateInfo-flags-00281",
571 "for push descriptor, total descriptor count in layout (%" PRIu64
572 ") must not be greater than VkPhysicalDevicePushDescriptorPropertiesKHR::maxPushDescriptors (%" PRIu32 "%s).",
573 total_descriptors, max_push_descriptors, undefined);
John Zulauf0fdeab32018-01-23 11:27:35 -0700574 }
575
Tobin Ehlis154c2692016-10-25 09:36:53 -0600576 return skip;
577}
578
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600579cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
580 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
581
Tobin Ehlis93f22372016-10-12 14:34:12 -0600582cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500583 const std::shared_ptr<DescriptorSetLayout const> &layout, uint32_t variable_count,
584 layer_data *dev_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700585 : some_update_(false),
586 set_(set),
587 pool_state_(nullptr),
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600588 p_layout_(layout),
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700589 device_data_(dev_data),
Jeff Bolzfdf96072018-04-10 14:32:18 -0500590 limits_(GetPhysDevProperties(dev_data)->properties.limits),
591 variable_count_(variable_count) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700592 pool_state_ = GetDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600593 // Foreach binding, create default descriptors of given type
John Zulaufb6d71202017-12-22 16:47:09 -0700594 descriptors_.reserve(p_layout_->GetTotalDescriptorCount());
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600595 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
596 auto type = p_layout_->GetTypeFromIndex(i);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600597 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700598 case VK_DESCRIPTOR_TYPE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600599 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
600 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600601 if (immut_sampler) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700602 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600603 some_update_ = true; // Immutable samplers are updated at creation
604 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700605 descriptors_.emplace_back(new SamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700606 }
607 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600608 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700609 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600610 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
611 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600612 if (immut) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700613 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600614 some_update_ = true; // Immutable samplers are updated at creation
615 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700616 descriptors_.emplace_back(new ImageSamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700617 }
618 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600619 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700620 // ImageDescriptors
621 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
622 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
623 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600624 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700625 descriptors_.emplace_back(new ImageDescriptor(type));
626 break;
627 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
628 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600629 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700630 descriptors_.emplace_back(new TexelDescriptor(type));
631 break;
632 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
633 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
634 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
635 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
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 BufferDescriptor(type));
638 break;
Jeff Bolze54ae892018-09-08 12:16:29 -0500639 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
640 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
641 descriptors_.emplace_back(new InlineUniformDescriptor(type));
642 break;
Jeff Bolzfbe51582018-09-13 10:01:35 -0500643 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NVX:
644 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
645 descriptors_.emplace_back(new AccelerationStructureDescriptor(type));
646 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700647 default:
648 assert(0); // Bad descriptor type specified
649 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600650 }
651 }
652}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600653
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700654cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200655
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600656static std::string StringDescriptorReqViewType(descriptor_req req) {
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700657 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200658 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
659 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700660 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700661 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200662 }
663 }
664
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700665 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700666
667 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200668}
669
Chris Forbesda01e8d2018-08-27 15:36:57 -0700670static char const *StringDescriptorReqComponentType(descriptor_req req) {
671 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_SINT) return "SINT";
672 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_UINT) return "UINT";
673 if (req & DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT) return "FLOAT";
674 return "(none)";
675}
676
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600677// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600678bool cvdescriptorset::DescriptorSet::IsCompatible(DescriptorSetLayout const *const layout, std::string *error) const {
679 return layout->IsCompatible(p_layout_.get(), error);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600680}
Chris Forbes57989132016-07-26 17:06:10 +1200681
Chris Forbesda01e8d2018-08-27 15:36:57 -0700682static unsigned DescriptorRequirementsBitsFromFormat(VkFormat fmt) {
683 if (FormatIsSInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
684 if (FormatIsUInt(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
685 if (FormatIsDepthAndStencil(fmt)) return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT | DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
686 if (fmt == VK_FORMAT_UNDEFINED) return 0;
687 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
688 return DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
689}
690
Tobin Ehlis3066db62016-08-22 08:12:23 -0600691// 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 -0600692// This includes validating that all descriptors in the given bindings are updated,
693// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
694// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600695bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
John Zulauf48a6a702017-12-22 17:14:54 -0700696 const std::vector<uint32_t> &dynamic_offsets, GLOBAL_CB_NODE *cb_node,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600697 const char *caller, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200698 for (auto binding_pair : bindings) {
699 auto binding = binding_pair.first;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600700 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600701 std::stringstream error_str;
702 error_str << "Attempting to validate DrawState for binding #" << binding
703 << " which is an invalid binding for this descriptor set.";
704 *error = error_str.str();
705 return false;
706 }
John Zulaufc483f442017-12-15 14:02:06 -0700707 IndexRange index_range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700708 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Jeff Bolzfdf96072018-04-10 14:32:18 -0500709
710 if (IsVariableDescriptorCount(binding)) {
711 // Only validate the first N descriptors if it uses variable_count
712 index_range.end = index_range.start + GetVariableDescriptorCount();
713 }
714
John Zulaufc483f442017-12-15 14:02:06 -0700715 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500716 if ((p_layout_->GetDescriptorBindingFlagsFromBinding(binding) & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) ||
717 descriptors_[i]->GetClass() == InlineUniform) {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500718 // Can't validate the descriptor because it may not have been updated,
719 // or the view could have been destroyed
720 continue;
721 } else if (!descriptors_[i]->updated) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700722 std::stringstream error_str;
723 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
724 << " is being used in draw but has not been updated.";
725 *error = error_str.str();
726 return false;
727 } else {
728 auto descriptor_class = descriptors_[i]->GetClass();
729 if (descriptor_class == GeneralBuffer) {
730 // Verify that buffers are valid
731 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
732 auto buffer_node = GetBufferState(device_data_, buffer);
733 if (!buffer_node) {
734 std::stringstream error_str;
735 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
736 << " references invalid buffer " << buffer << ".";
737 *error = error_str.str();
738 return false;
John Zulauf48a6a702017-12-22 17:14:54 -0700739 } else if (!buffer_node->sparse) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700740 for (auto mem_binding : buffer_node->GetBoundMemory()) {
741 if (!GetMemObjInfo(device_data_, mem_binding)) {
742 std::stringstream error_str;
743 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
744 << " uses buffer " << buffer << " that references invalid memory " << mem_binding << ".";
745 *error = error_str.str();
Tobin Ehlisc8266452017-04-07 12:20:30 -0600746 return false;
747 }
748 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700749 }
750 if (descriptors_[i]->IsDynamic()) {
751 // Validate that dynamic offsets are within the buffer
752 auto buffer_size = buffer_node->createInfo.size;
753 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
754 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
755 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
756 if (VK_WHOLE_SIZE == range) {
757 if ((dyn_offset + desc_offset) > buffer_size) {
758 std::stringstream error_str;
759 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
760 << " uses buffer " << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset "
761 << dyn_offset << " combined with offset " << desc_offset
762 << " that oversteps the buffer size of " << buffer_size << ".";
763 *error = error_str.str();
764 return false;
765 }
766 } else {
767 if ((dyn_offset + desc_offset + range) > buffer_size) {
768 std::stringstream error_str;
769 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
770 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
771 << " combined with offset " << desc_offset << " and range " << range
772 << " that oversteps the buffer size of " << buffer_size << ".";
773 *error = error_str.str();
774 return false;
775 }
776 }
777 }
778 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
779 VkImageView image_view;
780 VkImageLayout image_layout;
781 if (descriptor_class == ImageSampler) {
782 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
783 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
784 } else {
785 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
786 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
787 }
788 auto reqs = binding_pair.second;
789
790 auto image_view_state = GetImageViewState(device_data_, image_view);
Tobin Ehlis836a1372017-07-14 11:25:21 -0600791 if (nullptr == image_view_state) {
792 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
793 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
794 std::stringstream error_str;
795 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
796 << " is using imageView " << image_view << " that has been destroyed.";
797 *error = error_str.str();
798 return false;
799 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700800 auto image_view_ci = image_view_state->create_info;
801
802 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
803 // bad view type
804 std::stringstream error_str;
805 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600806 << " requires an image view of type " << StringDescriptorReqViewType(reqs) << " but got "
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700807 << string_VkImageViewType(image_view_ci.viewType) << ".";
808 *error = error_str.str();
809 return false;
810 }
811
Chris Forbesda01e8d2018-08-27 15:36:57 -0700812 auto format_bits = DescriptorRequirementsBitsFromFormat(image_view_ci.format);
813 if (!(reqs & format_bits)) {
814 // bad component type
815 std::stringstream error_str;
816 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i << " requires "
817 << StringDescriptorReqComponentType(reqs) << " component type, but bound descriptor format is "
818 << string_VkFormat(image_view_ci.format) << ".";
819 *error = error_str.str();
820 return false;
821 }
822
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700823 auto image_node = GetImageState(device_data_, image_view_ci.image);
824 assert(image_node);
825 // Verify Image Layout
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700826 // Copy first mip level into sub_layers and loop over each mip level to verify layout
827 VkImageSubresourceLayers sub_layers;
828 sub_layers.aspectMask = image_view_ci.subresourceRange.aspectMask;
829 sub_layers.baseArrayLayer = image_view_ci.subresourceRange.baseArrayLayer;
830 sub_layers.layerCount = image_view_ci.subresourceRange.layerCount;
831 bool hit_error = false;
832 for (auto cur_level = image_view_ci.subresourceRange.baseMipLevel;
833 cur_level < image_view_ci.subresourceRange.levelCount; ++cur_level) {
834 sub_layers.mipLevel = cur_level;
Cort Stratton7df30962018-05-17 19:45:57 -0700835 // No "invalid layout" VUID required for this call, since the optimal_layout parameter is UNDEFINED.
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700836 VerifyImageLayout(device_data_, cb_node, image_node, sub_layers, image_layout, VK_IMAGE_LAYOUT_UNDEFINED,
Cort Stratton7df30962018-05-17 19:45:57 -0700837 caller, kVUIDUndefined, "VUID-VkDescriptorImageInfo-imageLayout-00344", &hit_error);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700838 if (hit_error) {
839 *error =
Dave Houltona9df0ce2018-02-07 10:51:23 -0700840 "Image layout specified at vkUpdateDescriptorSets() time doesn't match actual image layout at time "
841 "descriptor is used. See previous error callback for specific details.";
Chris Forbes57989132016-07-26 17:06:10 +1200842 return false;
843 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700844 }
845 // Verify Sample counts
846 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
847 std::stringstream error_str;
848 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
849 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
850 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
851 *error = error_str.str();
852 return false;
853 }
854 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
855 std::stringstream error_str;
856 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
857 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
858 *error = error_str.str();
859 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200860 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600861 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600862 if (descriptor_class == ImageSampler || descriptor_class == PlainSampler) {
863 // Verify Sampler still valid
864 VkSampler sampler;
865 if (descriptor_class == ImageSampler) {
866 sampler = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetSampler();
867 } else {
868 sampler = static_cast<SamplerDescriptor *>(descriptors_[i].get())->GetSampler();
869 }
870 if (!ValidateSampler(sampler, device_data_)) {
871 std::stringstream error_str;
872 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
873 << " is using sampler " << sampler << " that has been destroyed.";
874 *error = error_str.str();
875 return false;
876 }
877 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600878 }
879 }
880 }
881 return true;
882}
Chris Forbes57989132016-07-26 17:06:10 +1200883
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600884// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600885uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600886 std::unordered_set<VkBuffer> *buffer_set,
887 std::unordered_set<VkImageView> *image_set) const {
888 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200889 for (auto binding_pair : bindings) {
890 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600891 // If a binding doesn't exist, skip it
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600892 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600893 continue;
894 }
John Zulaufc483f442017-12-15 14:02:06 -0700895 uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600896 if (descriptors_[start_idx]->IsStorage()) {
897 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600898 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600899 if (descriptors_[start_idx + i]->updated) {
900 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600901 num_updates++;
902 }
903 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600904 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600905 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600906 if (descriptors_[start_idx + i]->updated) {
907 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700908 auto bv_state = GetBufferViewState(device_data_, bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600909 if (bv_state) {
910 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600911 num_updates++;
912 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600913 }
914 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600915 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600916 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600917 if (descriptors_[start_idx + i]->updated) {
918 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600919 num_updates++;
920 }
921 }
922 }
923 }
924 }
925 return num_updates;
926}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600927// Set is being deleted or updates so invalidate all bound cmd buffers
928void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600929 core_validation::InvalidateCommandBuffers(device_data_, cb_bindings, {HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600930}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600931// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600932void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700933 // Perform update on a per-binding basis as consecutive updates roll over to next binding
934 auto descriptors_remaining = update->descriptorCount;
935 auto binding_being_updated = update->dstBinding;
936 auto offset = update->dstArrayElement;
Tobin Ehlise16805c2017-08-09 09:10:37 -0600937 uint32_t update_index = 0;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700938 while (descriptors_remaining) {
939 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
John Zulaufc483f442017-12-15 14:02:06 -0700940 auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700941 // Loop over the updates for a single binding at a time
Tobin Ehlise16805c2017-08-09 09:10:37 -0600942 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
943 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700944 }
945 // Roll over to next binding in case of consecutive update
946 descriptors_remaining -= update_count;
947 offset = 0;
948 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600949 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700950 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600951
Jeff Bolzfdf96072018-04-10 14:32:18 -0500952 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
953 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
954 InvalidateBoundCmdBuffers();
955 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600956}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600957// Validate Copy update
958bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
Dave Houlton00c154e2018-05-24 13:20:50 -0600959 const DescriptorSet *src_set, std::string *error_code,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600960 std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -0700961 // Verify dst layout still valid
962 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600963 *error_code = "VUID-VkCopyDescriptorSet-dstSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -0700964 string_sprintf(error_msg,
965 "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set dstSet 0x%" PRIxLEAST64
966 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
967 HandleToUint64(set_), HandleToUint64(p_layout_->GetDescriptorSetLayout()));
968 return false;
969 }
970
971 // Verify src layout still valid
972 if (src_set->p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600973 *error_code = "VUID-VkCopyDescriptorSet-srcSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -0700974 string_sprintf(
975 error_msg,
976 "Cannot call vkUpdateDescriptorSets() to perform copy update of dstSet 0x%" PRIxLEAST64
977 " from descriptor set srcSet 0x%" PRIxLEAST64 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
978 HandleToUint64(set_), HandleToUint64(src_set->set_), HandleToUint64(src_set->p_layout_->GetDescriptorSetLayout()));
979 return false;
980 }
981
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600982 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600983 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-00347";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600984 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700985 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600986 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600987 return false;
988 }
989 if (!src_set->HasBinding(update->srcBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600990 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-00345";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600991 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700992 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600993 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600994 return false;
995 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500996 // Verify idle ds
997 if (in_use.load() &&
998 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
999 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1000 // TODO : Re-using Free Idle error code, need copy update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001001 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001002 std::stringstream error_str;
1003 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
1004 << " that is in use by a command buffer";
1005 *error_msg = error_str.str();
1006 return false;
1007 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001008 // src & dst set bindings are valid
1009 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -07001010 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001011 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
1012 // SRC update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001013 *error_code = "VUID-VkCopyDescriptorSet-srcArrayElement-00346";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001014 std::stringstream error_str;
1015 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
John Zulaufc483f442017-12-15 14:02:06 -07001016 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001017 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001018 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001019 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001020 return false;
1021 }
John Zulaufc483f442017-12-15 14:02:06 -07001022 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001023 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001024 // DST update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -06001025 *error_code = "VUID-VkCopyDescriptorSet-dstArrayElement-00348";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001026 std::stringstream error_str;
1027 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
John Zulaufc483f442017-12-15 14:02:06 -07001028 << " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001029 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001030 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001031 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001032 return false;
1033 }
1034 // Check that types match
Dave Houltond8ed0212018-05-16 17:18:24 -06001035 // TODO : Base default error case going from here is "VUID-VkAcquireNextImageInfoKHR-semaphore-parameter"2ba which covers all
1036 // consistency issues, need more fine-grained error codes
Dave Houlton00c154e2018-05-24 13:20:50 -06001037 *error_code = "VUID-VkCopyDescriptorSet-srcSet-00349";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001038 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001039 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001040 if (src_type != dst_type) {
1041 std::stringstream error_str;
1042 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
1043 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001044 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001045 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001046 return false;
1047 }
1048 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -06001049 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001050 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001051 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001052 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001053 return false;
1054 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001055
1056 if ((src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1057 !(GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001058 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01918";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001059 std::stringstream error_str;
1060 error_str << "If pname:srcSet's (" << update->srcSet
1061 << ") layout was created with the "
1062 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1063 "set, then pname:dstSet's ("
1064 << update->dstSet
1065 << ") layout must: also have been created with the "
1066 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1067 *error_msg = error_str.str();
1068 return false;
1069 }
1070
1071 if (!(src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1072 (GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001073 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01919";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001074 std::stringstream error_str;
1075 error_str << "If pname:srcSet's (" << update->srcSet
1076 << ") layout was created without the "
1077 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1078 "set, then pname:dstSet's ("
1079 << update->dstSet
1080 << ") layout must: also have been created without the "
1081 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1082 *error_msg = error_str.str();
1083 return false;
1084 }
1085
1086 if ((src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1087 !(GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001088 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01920";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001089 std::stringstream error_str;
1090 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1091 << ") was allocated was created "
1092 "with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1093 "set, then the descriptor pool from which pname:dstSet ("
1094 << update->dstSet
1095 << ") was allocated must: "
1096 "also have been created with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1097 *error_msg = error_str.str();
1098 return false;
1099 }
1100
1101 if (!(src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1102 (GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001103 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01921";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001104 std::stringstream error_str;
1105 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1106 << ") was allocated was created "
1107 "without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1108 "set, then the descriptor pool from which pname:dstSet ("
1109 << update->dstSet
1110 << ") was allocated must: "
1111 "also have been created without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1112 *error_msg = error_str.str();
1113 return false;
1114 }
1115
Jeff Bolze54ae892018-09-08 12:16:29 -05001116 if (src_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1117 if ((update->srcArrayElement % 4) != 0) {
1118 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02223";
1119 std::stringstream error_str;
1120 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1121 << "srcArrayElement " << update->srcArrayElement << " not a multiple of 4";
1122 *error_msg = error_str.str();
1123 return false;
1124 }
1125 if ((update->dstArrayElement % 4) != 0) {
1126 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-02224";
1127 std::stringstream error_str;
1128 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1129 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1130 *error_msg = error_str.str();
1131 return false;
1132 }
1133 if ((update->descriptorCount % 4) != 0) {
1134 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-02225";
1135 std::stringstream error_str;
1136 error_str << "Attempting copy update to VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT binding with "
1137 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1138 *error_msg = error_str.str();
1139 return false;
1140 }
1141 }
1142
Tobin Ehlisd41e7b62016-05-19 07:56:18 -06001143 // Update parameters all look good and descriptor updated so verify update contents
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001144 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error_code, error_msg)) return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001145
1146 // All checks passed so update is good
1147 return true;
1148}
1149// Perform Copy update
1150void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -07001151 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
1152 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001153 // Update parameters all look good so perform update
1154 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001155 auto src = src_set->descriptors_[src_start_idx + di].get();
1156 auto dst = descriptors_[dst_start_idx + di].get();
1157 if (src->updated) {
1158 dst->CopyUpdate(src);
1159 some_update_ = true;
1160 } else {
1161 dst->updated = false;
1162 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001163 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001164
Jeff Bolzfdf96072018-04-10 14:32:18 -05001165 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1166 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1167 InvalidateBoundCmdBuffers();
1168 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001169}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001170
Tobin Ehlisf9519102016-08-17 09:49:13 -06001171// Bind cb_node to this set and this set to cb_node.
1172// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
1173// to be used in a draw by the given cb_node
Tobin Ehlis276d3d32016-12-21 09:21:06 -07001174void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node,
Tobin Ehlis022528b2016-12-29 12:22:32 -07001175 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001176 // bind cb to this descriptor set
1177 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001178 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Petr Krausbc7f5442017-05-14 23:43:38 +02001179 cb_node->object_bindings.insert({HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001180 pool_state_->cb_bindings.insert(cb_node);
Petr Krausbc7f5442017-05-14 23:43:38 +02001181 cb_node->object_bindings.insert({HandleToUint64(pool_state_->pool), kVulkanObjectTypeDescriptorPool});
Tobin Ehlisf9519102016-08-17 09:49:13 -06001182 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
1183 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -07001184 for (auto binding_req_pair : binding_req_map) {
1185 auto binding = binding_req_pair.first;
John Zulaufc483f442017-12-15 14:02:06 -07001186 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
1187 for (uint32_t i = range.start; i < range.end; ++i) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001188 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
1189 }
1190 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001191}
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001192
1193// Update CB layout map with any image/imagesampler descriptor image layouts
1194void cvdescriptorset::DescriptorSet::UpdateDSImageLayoutState(GLOBAL_CB_NODE *cb_state) {
1195 for (auto const &desc : descriptors_) {
Mark Lobodzinskieae49f02018-09-10 11:58:08 -06001196 if (desc->updated && (desc->descriptor_class == ImageSampler || desc->descriptor_class == Image)) {
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001197 VkImageView image_view;
1198 VkImageLayout image_layout;
1199 if (desc->descriptor_class == ImageSampler) {
1200 image_view = static_cast<ImageSamplerDescriptor *>(desc.get())->GetImageView();
1201 image_layout = static_cast<ImageSamplerDescriptor *>(desc.get())->GetImageLayout();
1202 } else {
1203 image_view = static_cast<ImageDescriptor *>(desc.get())->GetImageView();
1204 image_layout = static_cast<ImageDescriptor *>(desc.get())->GetImageLayout();
1205 }
1206 SetImageViewLayout(device_data_, cb_state, image_view, image_layout);
1207 }
1208 }
1209}
1210
John Zulauf48a6a702017-12-22 17:14:54 -07001211void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1212 const BindingReqMap &in_req, BindingReqMap *out_req,
1213 TrackedBindings *bindings) {
1214 assert(out_req);
1215 assert(bindings);
1216 const auto binding = binding_req_pair.first;
1217 // Use insert and look at the boolean ("was inserted") in the returned pair to see if this is a new set member.
1218 // Saves one hash lookup vs. find ... compare w/ end ... insert.
1219 const auto it_bool_pair = bindings->insert(binding);
1220 if (it_bool_pair.second) {
1221 out_req->emplace(binding_req_pair);
1222 }
1223}
Mark Lobodzinski2872f4a2018-09-03 17:00:53 -06001224
John Zulauf48a6a702017-12-22 17:14:54 -07001225void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1226 const BindingReqMap &in_req, BindingReqMap *out_req,
1227 TrackedBindings *bindings, uint32_t limit) {
1228 if (bindings->size() < limit) FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, bindings);
1229}
1230
1231void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, const BindingReqMap &in_req,
1232 BindingReqMap *out_req) {
1233 TrackedBindings &bound = cached_validation_[cb_state].command_binding_and_usage;
1234 if (bound.size() == GetBindingCount()) {
1235 return; // All bindings are bound, out req is empty
1236 }
1237 for (const auto &binding_req_pair : in_req) {
1238 const auto binding = binding_req_pair.first;
1239 // If a binding doesn't exist, or has already been bound, skip it
1240 if (p_layout_->HasBinding(binding)) {
1241 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, &bound);
1242 }
1243 }
1244}
1245
1246void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline,
1247 const BindingReqMap &in_req, BindingReqMap *out_req) {
1248 auto &validated = cached_validation_[cb_state];
1249 auto &image_sample_val = validated.image_samplers[pipeline];
1250 auto *const dynamic_buffers = &validated.dynamic_buffers;
1251 auto *const non_dynamic_buffers = &validated.non_dynamic_buffers;
1252 const auto &stats = p_layout_->GetBindingTypeStats();
1253 for (const auto &binding_req_pair : in_req) {
1254 auto binding = binding_req_pair.first;
1255 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
1256 if (!layout_binding) {
1257 continue;
1258 }
1259 // Caching criteria differs per type.
1260 // If image_layout have changed , the image descriptors need to be validated against them.
1261 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1262 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
1263 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, dynamic_buffers, stats.dynamic_buffer_count);
1264 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1265 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
1266 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
1267 } else {
1268 // This is rather crude, as the changed layouts may not impact the bound descriptors,
1269 // but the simple "versioning" is a simple "dirt" test.
1270 auto &version = image_sample_val[binding]; // Take advantage of default construtor zero initialzing new entries
1271 if (version != cb_state->image_layout_change_count) {
1272 version = cb_state->image_layout_change_count;
1273 out_req->emplace(binding_req_pair);
1274 }
1275 }
1276 }
1277}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001278
Tobin Ehlis300888c2016-05-18 13:43:26 -06001279cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001280 updated = false;
1281 descriptor_class = PlainSampler;
1282 if (immut) {
1283 sampler_ = *immut;
1284 immutable_ = true;
1285 updated = true;
1286 }
1287}
Tobin Ehlise2f80292016-06-02 10:08:53 -06001288// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001289bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const layer_data *dev_data) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001290 return (GetSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001291}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001292
Tobin Ehlis554bf382016-05-24 11:14:43 -06001293bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06001294 const layer_data *dev_data, std::string *error_code, std::string *error_msg) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001295 // TODO : Defaulting to 00943 for all cases here. Need to create new error codes for various cases.
Dave Houlton00c154e2018-05-24 13:20:50 -06001296 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00326";
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001297 auto iv_state = GetImageViewState(dev_data, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001298 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001299 std::stringstream error_str;
1300 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001301 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001302 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001303 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001304 // 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 -06001305 // Validate that imageLayout is compatible with aspect_mask and image format
1306 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001307 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
1308 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001309 VkFormat format = VK_FORMAT_MAX_ENUM;
1310 VkImageUsageFlags usage = 0;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001311 auto image_node = GetImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001312 if (image_node) {
1313 format = image_node->createInfo.format;
1314 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001315 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001316 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
1317 // the error here occurs is if memory bound to a created imageView has been freed.
Dave Houltond8ed0212018-05-16 17:18:24 -06001318 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()",
1319 "VUID-VkImageViewCreateInfo-image-01020")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001320 *error_code = "VUID-VkImageViewCreateInfo-image-01020";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001321 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001322 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001323 }
Chris Forbes67757ff2017-07-21 13:59:01 -07001324
1325 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
1326 // but not binding them to descriptor sets.
1327 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D &&
1328 (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
1329 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001330 *error_code = "VUID-VkDescriptorImageInfo-imageView-00343";
Chris Forbes67757ff2017-07-21 13:59:01 -07001331 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
1332 return false;
1333 }
Tobin Ehlis1809f912016-05-25 09:24:36 -06001334 }
1335 // First validate that format and layout are compatible
1336 if (format == VK_FORMAT_MAX_ENUM) {
1337 std::stringstream error_str;
1338 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001339 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001340 return false;
1341 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001342 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
1343 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -06001344 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -06001345 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001346 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1347 // Only Color bit must be set
1348 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001349 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -07001350 error_str
1351 << "ImageView (" << image_view
1352 << ") 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 -06001353 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001354 return false;
1355 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001356 // format must NOT be DS
1357 if (ds) {
1358 std::stringstream error_str;
1359 error_str << "ImageView (" << image_view
1360 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1361 << string_VkFormat(format) << " which is not a color format.";
1362 *error_msg = error_str.str();
1363 return false;
1364 }
1365 break;
1366 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1367 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1368 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001369 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1370 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1371 // both must NOT be set
1372 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001373 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001374 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001375 return false;
1376 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001377 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1378 // Neither were set
1379 std::stringstream error_str;
1380 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1381 << " but does not have STENCIL or DEPTH aspects set";
1382 *error_msg = error_str.str();
1383 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001384 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001385 // format must be DS
1386 if (!ds) {
1387 std::stringstream error_str;
1388 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1389 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
1390 *error_msg = error_str.str();
1391 return false;
1392 }
1393 break;
1394 default:
1395 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1396 if (ds) {
1397 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1398 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1399 // both must NOT be set
1400 std::stringstream error_str;
1401 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1402 << " and is using depth/stencil image of format " << string_VkFormat(format)
1403 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1404 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1405 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1406 "reads respectively.";
1407 *error_msg = error_str.str();
1408 return false;
1409 }
1410 }
1411 }
1412 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001413 }
1414 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001415 // 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 -06001416 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1417 // under vkCreateImage()
Dave Houltond8ed0212018-05-16 17:18:24 -06001418 // TODO : Need to also validate case "VUID-VkWriteDescriptorSet-descriptorType-00336" where STORAGE_IMAGE & INPUT_ATTACH types
1419 // must have been created with identify swizzle
Tobin Ehlis1809f912016-05-25 09:24:36 -06001420 std::string error_usage_bit;
1421 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001422 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1423 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1424 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1425 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
1426 }
1427 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001428 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001429 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1430 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1431 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
1432 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
1433 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001434 // TODO : Need to create custom enum error codes for these cases
1435 if (image_node->shared_presentable) {
1436 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001437 error_str << "ImageView (" << image_view
1438 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
1439 "layout "
1440 << string_VkImageLayout(image_layout)
1441 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report "
1442 "support for VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the "
1443 "VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001444 *error_msg = error_str.str();
1445 return false;
1446 }
1447 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001448 error_str << "ImageView (" << image_view
1449 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
1450 << string_VkImageLayout(image_layout)
1451 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage "
1452 "images can only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001453 *error_msg = error_str.str();
1454 return false;
1455 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001456 }
1457 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001458 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001459 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
1460 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
1461 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
1462 }
1463 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001464 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001465 default:
1466 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001467 }
1468 if (!error_usage_bit.empty()) {
1469 std::stringstream error_str;
1470 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
1471 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1472 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001473 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001474 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001475 }
1476 return true;
1477}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001478
Tobin Ehlis300888c2016-05-18 13:43:26 -06001479void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Chris Forbesfea2c542018-04-13 09:34:15 -07001480 if (!immutable_) {
1481 sampler_ = update->pImageInfo[index].sampler;
1482 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001483 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001484}
1485
Tobin Ehlis300888c2016-05-18 13:43:26 -06001486void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001487 if (!immutable_) {
1488 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001489 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001490 }
1491 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001492}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001493
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001494void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001495 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001496 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001497 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001498 }
1499}
1500
Tobin Ehlis300888c2016-05-18 13:43:26 -06001501cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001502 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001503 updated = false;
1504 descriptor_class = ImageSampler;
1505 if (immut) {
1506 sampler_ = *immut;
1507 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001508 }
1509}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001510
Tobin Ehlis300888c2016-05-18 13:43:26 -06001511void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001512 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001513 const auto &image_info = update->pImageInfo[index];
Chris Forbesfea2c542018-04-13 09:34:15 -07001514 if (!immutable_) {
1515 sampler_ = image_info.sampler;
1516 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001517 image_view_ = image_info.imageView;
1518 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001519}
1520
Tobin Ehlis300888c2016-05-18 13:43:26 -06001521void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001522 if (!immutable_) {
1523 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001524 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001525 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001526 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1527 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001528 updated = true;
1529 image_view_ = image_view;
1530 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001531}
1532
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001533void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001534 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001535 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001536 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001537 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001538 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001539 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001540 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001541 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001542 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001543 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001544}
1545
Tobin Ehlis300888c2016-05-18 13:43:26 -06001546cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1547 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001548 updated = false;
1549 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001550 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001551}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001552
Tobin Ehlis300888c2016-05-18 13:43:26 -06001553void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001554 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001555 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001556 image_view_ = image_info.imageView;
1557 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001558}
1559
Tobin Ehlis300888c2016-05-18 13:43:26 -06001560void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001561 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1562 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001563 updated = true;
1564 image_view_ = image_view;
1565 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001566}
1567
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001568void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001569 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001570 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001571 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001572 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001573 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001574}
1575
Tobin Ehlis300888c2016-05-18 13:43:26 -06001576cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1577 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001578 updated = false;
1579 descriptor_class = GeneralBuffer;
1580 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1581 dynamic_ = true;
1582 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1583 storage_ = true;
1584 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1585 dynamic_ = true;
1586 storage_ = true;
1587 }
1588}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001589void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001590 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001591 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001592 buffer_ = buffer_info.buffer;
1593 offset_ = buffer_info.offset;
1594 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001595}
1596
Tobin Ehlis300888c2016-05-18 13:43:26 -06001597void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1598 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001599 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001600 buffer_ = buff_desc->buffer_;
1601 offset_ = buff_desc->offset_;
1602 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001603}
1604
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001605void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001606 auto buffer_node = GetBufferState(dev_data, buffer_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001607 if (buffer_node) core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001608}
1609
Tobin Ehlis300888c2016-05-18 13:43:26 -06001610cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001611 updated = false;
1612 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001613 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001614}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001615
Tobin Ehlis300888c2016-05-18 13:43:26 -06001616void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001617 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001618 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001619}
1620
Tobin Ehlis300888c2016-05-18 13:43:26 -06001621void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1622 updated = true;
1623 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1624}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001625
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001626void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001627 auto bv_state = GetBufferViewState(dev_data, buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001628 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001629 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001630 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001631}
1632
Tobin Ehlis300888c2016-05-18 13:43:26 -06001633// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1634// sets, and then calls their respective Validate[Write|Copy]Update functions.
1635// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1636// be skipped, then true is returned.
1637// If there is no issue with the update, then false is returned.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001638bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data, const layer_data *dev_data,
1639 uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001640 const VkCopyDescriptorSet *p_cds) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001641 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001642 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001643 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001644 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001645 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001646 if (!set_node) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001647 skip |=
Tobin Ehlis300888c2016-05-18 13:43:26 -06001648 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Dave Houlton51653902018-06-22 17:32:13 -06001649 HandleToUint64(dest_set), kVUID_Core_DrawState_InvalidDescriptorSet,
Tobin Ehlis300888c2016-05-18 13:43:26 -06001650 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
Petr Krausbc7f5442017-05-14 23:43:38 +02001651 HandleToUint64(dest_set));
Tobin Ehlis300888c2016-05-18 13:43:26 -06001652 } else {
Dave Houltond8ed0212018-05-16 17:18:24 -06001653 std::string error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001654 std::string error_str;
Dave Houlton00c154e2018-05-24 13:20:50 -06001655 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001656 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 -06001657 HandleToUint64(dest_set), error_code,
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01001658 "vkUpdateDescriptorSets() failed write update validation for Descriptor Set 0x%" PRIx64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001659 " with error: %s.",
1660 HandleToUint64(dest_set), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001661 }
1662 }
1663 }
1664 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001665 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001666 auto dst_set = p_cds[i].dstSet;
1667 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001668 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1669 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001670 // Object_tracker verifies that src & dest descriptor set are valid
1671 assert(src_node);
1672 assert(dst_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001673 std::string error_code;
Tobin Ehlisa1712752017-01-04 09:41:47 -07001674 std::string error_str;
Dave Houlton00c154e2018-05-24 13:20:50 -06001675 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001676 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 -06001677 HandleToUint64(dst_set), error_code,
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01001678 "vkUpdateDescriptorSets() failed copy update from Descriptor Set 0x%" PRIx64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001679 " to Descriptor Set 0x%" PRIx64 " with error: %s.",
1680 HandleToUint64(src_set), HandleToUint64(dst_set), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001681 }
1682 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001683 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001684}
1685// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1686// sets, and then calls their respective Perform[Write|Copy]Update functions.
1687// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1688// with the same set of updates.
1689// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1690// calling down the chain.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001691void cvdescriptorset::PerformUpdateDescriptorSets(const layer_data *dev_data, uint32_t write_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001692 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1693 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001694 // Write updates first
1695 uint32_t i = 0;
1696 for (i = 0; i < write_count; ++i) {
1697 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001698 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001699 if (set_node) {
1700 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001701 }
1702 }
1703 // Now copy updates
1704 for (i = 0; i < copy_count; ++i) {
1705 auto dst_set = p_cds[i].dstSet;
1706 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001707 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1708 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001709 if (src_node && dst_node) {
1710 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001711 }
1712 }
1713}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001714// This helper function carries out the state updates for descriptor updates peformed via update templates. It basically collects
1715// data and leverages the PerformUpdateDescriptor helper functions to do this.
1716void cvdescriptorset::PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet,
1717 std::unique_ptr<TEMPLATE_STATE> const &template_state,
1718 const void *pData) {
1719 auto const &create_info = template_state->create_info;
1720
1721 // Create a vector of write structs
1722 std::vector<VkWriteDescriptorSet> desc_writes;
Jeff Bolze54ae892018-09-08 12:16:29 -05001723 std::vector<VkWriteDescriptorSetInlineUniformBlockEXT> inline_infos(create_info.descriptorUpdateEntryCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001724 auto layout_obj = GetDescriptorSetLayout(device_data, create_info.descriptorSetLayout);
1725
1726 // Create a WriteDescriptorSet struct for each template update entry
1727 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1728 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1729 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1730 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1731
John Zulaufb6d71202017-12-22 16:47:09 -07001732 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001733 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1734 desc_writes.emplace_back();
1735 auto &write_entry = desc_writes.back();
1736
1737 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1738 char *update_entry = (char *)(pData) + offset;
1739
1740 if (dst_array_element >= binding_count) {
1741 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001742 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001743 }
1744
1745 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1746 write_entry.pNext = NULL;
1747 write_entry.dstSet = descriptorSet;
1748 write_entry.dstBinding = binding_being_updated;
1749 write_entry.dstArrayElement = dst_array_element;
1750 write_entry.descriptorCount = 1;
1751 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1752
1753 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1754 case VK_DESCRIPTOR_TYPE_SAMPLER:
1755 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1756 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1757 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1758 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1759 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1760 break;
1761
1762 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1763 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1764 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1765 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1766 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1767 break;
1768
1769 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1770 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1771 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1772 break;
Dave Houlton142c4cb2018-10-17 15:04:41 -06001773 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: {
1774 VkWriteDescriptorSetInlineUniformBlockEXT *inline_info = &inline_infos[i];
1775 inline_info->sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK_EXT;
1776 inline_info->pNext = nullptr;
1777 inline_info->dataSize = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1778 inline_info->pData = update_entry;
1779 write_entry.pNext = inline_info;
1780 // skip the rest of the array, they just represent bytes in the update
1781 j = create_info.pDescriptorUpdateEntries[i].descriptorCount;
1782 break;
1783 }
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001784 default:
1785 assert(0);
1786 break;
1787 }
1788 dst_array_element++;
1789 }
1790 }
1791 PerformUpdateDescriptorSets(device_data, static_cast<uint32_t>(desc_writes.size()), desc_writes.data(), 0, NULL);
1792}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001793// Validate the state for a given write update but don't actually perform the update
1794// If an error would occur for this update, return false and fill in details in error_msg string
1795bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
Dave Houlton00c154e2018-05-24 13:20:50 -06001796 std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001797 // Verify dst layout still valid
1798 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001799 *error_code = "VUID-VkWriteDescriptorSet-dstSet-00320";
John Zulauf5dfd45c2018-01-17 11:06:34 -07001800 string_sprintf(error_msg,
1801 "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set 0x%" PRIxLEAST64
1802 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
1803 HandleToUint64(set_), HandleToUint64(p_layout_->GetDescriptorSetLayout()));
1804 return false;
1805 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001806 // Verify dst binding exists
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001807 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001808 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00315";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001809 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001810 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001811 *error_msg = error_str.str();
1812 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001813 } else {
1814 // Make sure binding isn't empty
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001815 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001816 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00316";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001817 std::stringstream error_str;
1818 error_str << "DescriptorSet " << set_ << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
1819 *error_msg = error_str.str();
1820 return false;
1821 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001822 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001823 // Verify idle ds
1824 if (in_use.load() &&
1825 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1826 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1827 // TODO : Re-using Free Idle error code, need write update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001828 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001829 std::stringstream error_str;
1830 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
1831 << " that is in use by a command buffer";
1832 *error_msg = error_str.str();
1833 return false;
1834 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001835 // We know that binding is valid, verify update and do update on each descriptor
John Zulaufc483f442017-12-15 14:02:06 -07001836 auto start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001837 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001838 if (type != update->descriptorType) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001839 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00319";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001840 std::stringstream error_str;
1841 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1842 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1843 *error_msg = error_str.str();
1844 return false;
1845 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001846 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001847 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001848 std::stringstream error_str;
1849 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001850 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001851 << " descriptors in that binding and all successive bindings of the set, but update of "
1852 << update->descriptorCount << " descriptors combined with update array element offset of "
1853 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001854 *error_msg = error_str.str();
1855 return false;
1856 }
Jeff Bolze54ae892018-09-08 12:16:29 -05001857 if (type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1858 if ((update->dstArrayElement % 4) != 0) {
1859 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02219";
1860 std::stringstream error_str;
1861 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
1862 << "dstArrayElement " << update->dstArrayElement << " not a multiple of 4";
1863 *error_msg = error_str.str();
1864 return false;
1865 }
1866 if ((update->descriptorCount % 4) != 0) {
1867 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02220";
1868 std::stringstream error_str;
1869 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
1870 << "descriptorCount " << update->descriptorCount << " not a multiple of 4";
1871 *error_msg = error_str.str();
1872 return false;
1873 }
1874 const auto *write_inline_info = lvl_find_in_chain<VkWriteDescriptorSetInlineUniformBlockEXT>(update->pNext);
1875 if (!write_inline_info || write_inline_info->dataSize != update->descriptorCount) {
1876 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-02221";
1877 std::stringstream error_str;
1878 if (!write_inline_info) {
1879 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
1880 << "VkWriteDescriptorSetInlineUniformBlockEXT missing";
1881 } else {
1882 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06001883 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
1884 << " not equal to "
Jeff Bolze54ae892018-09-08 12:16:29 -05001885 << "VkWriteDescriptorSet descriptorCount " << update->descriptorCount;
1886 }
1887 *error_msg = error_str.str();
1888 return false;
1889 }
1890 // This error is probably unreachable due to the previous two errors
1891 if (write_inline_info && (write_inline_info->dataSize % 4) != 0) {
1892 *error_code = "VUID-VkWriteDescriptorSetInlineUniformBlockEXT-dataSize-02222";
1893 std::stringstream error_str;
1894 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Dave Houlton142c4cb2018-10-17 15:04:41 -06001895 << "VkWriteDescriptorSetInlineUniformBlockEXT dataSize " << write_inline_info->dataSize
1896 << " not a multiple of 4";
Jeff Bolze54ae892018-09-08 12:16:29 -05001897 *error_msg = error_str.str();
1898 return false;
1899 }
1900 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001901 // Verify consecutive bindings match (if needed)
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001902 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001903 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07001904 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Dave Houlton00c154e2018-05-24 13:20:50 -06001905 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001906 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001907 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001908 // Update is within bounds and consistent so last step is to validate update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001909 if (!VerifyWriteUpdateContents(update, start_idx, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001910 std::stringstream error_str;
1911 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1912 << " failed with error message: " << error_msg->c_str();
1913 *error_msg = error_str.str();
1914 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001915 }
1916 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001917 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001918}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001919// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001920// 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 -07001921bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06001922 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001923 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001924 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001925 std::string error_usage_bit;
1926 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001927 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1928 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001929 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00334";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001930 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1931 }
1932 break;
1933 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1934 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001935 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00335";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001936 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1937 }
1938 break;
1939 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1940 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1941 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001942 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00330";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001943 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1944 }
1945 break;
1946 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1947 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1948 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001949 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00331";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001950 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1951 }
1952 break;
1953 default:
1954 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001955 }
1956 if (!error_usage_bit.empty()) {
1957 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001958 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1959 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1960 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001961 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001962 return false;
1963 }
1964 return true;
1965}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001966// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1967// 1. buffer is valid
1968// 2. buffer was created with correct usage flags
1969// 3. offset is less than buffer size
1970// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001971// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001972// 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 -06001973bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06001974 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001975 // First make sure that buffer is valid
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001976 auto buffer_node = GetBufferState(device_data_, buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07001977 // Any invalid buffer should already be caught by object_tracker
1978 assert(buffer_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001979 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()",
1980 "VUID-VkWriteDescriptorSet-descriptorType-00329")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001981 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00329";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001982 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001983 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001984 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001985 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001986 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1987 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001988 return false;
1989 }
1990 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001991 if (buffer_info->offset >= buffer_node->createInfo.size) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001992 *error_code = "VUID-VkDescriptorBufferInfo-offset-00340";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001993 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001994 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
1995 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001996 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001997 return false;
1998 }
1999 if (buffer_info->range != VK_WHOLE_SIZE) {
2000 // Range must be VK_WHOLE_SIZE or > 0
2001 if (!buffer_info->range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002002 *error_code = "VUID-VkDescriptorBufferInfo-range-00341";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002003 std::stringstream error_str;
2004 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002005 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002006 return false;
2007 }
2008 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
2009 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002010 *error_code = "VUID-VkDescriptorBufferInfo-range-00342";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002011 std::stringstream error_str;
2012 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
2013 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002014 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002015 return false;
2016 }
2017 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002018 // Check buffer update sizes against device limits
2019 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
2020 auto max_ub_range = limits_.maxUniformBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002021 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002022 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002023 std::stringstream error_str;
2024 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2025 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
2026 *error_msg = error_str.str();
2027 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002028 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_ub_range) {
2029 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
2030 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002031 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2032 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002033 << "maxUniformBufferRange (" << max_ub_range << ")";
2034 *error_msg = error_str.str();
2035 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002036 }
2037 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
2038 auto max_sb_range = limits_.maxStorageBufferRange;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002039 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002040 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002041 std::stringstream error_str;
2042 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
2043 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
2044 *error_msg = error_str.str();
2045 return false;
Peter Kohaut2794a292018-07-13 11:13:47 +02002046 } else if (buffer_info->range == VK_WHOLE_SIZE && (buffer_node->createInfo.size - buffer_info->offset) > max_sb_range) {
2047 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
2048 std::stringstream error_str;
Peter Kohaut18f413d2018-07-16 13:15:42 +02002049 error_str << "VkDescriptorBufferInfo range is VK_WHOLE_SIZE but effective range "
2050 << "(" << (buffer_node->createInfo.size - buffer_info->offset) << ") is greater than this device's "
Peter Kohaut2794a292018-07-13 11:13:47 +02002051 << "maxStorageBufferRange (" << max_sb_range << ")";
2052 *error_msg = error_str.str();
2053 return false;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07002054 }
2055 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06002056 return true;
2057}
2058
Tobin Ehlis300888c2016-05-18 13:43:26 -06002059// Verify that the contents of the update are ok, but don't perform actual update
2060bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
Dave Houlton00c154e2018-05-24 13:20:50 -06002061 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002062 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002063 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
2064 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2065 // Validate image
2066 auto image_view = update->pImageInfo[di].imageView;
2067 auto image_layout = update->pImageInfo[di].imageLayout;
2068 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002069 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002070 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
2071 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002072 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002073 return false;
2074 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002075 }
2076 }
Tobin Ehlis648b1cf2018-04-13 15:24:13 -06002077 // fall through
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002078 case VK_DESCRIPTOR_TYPE_SAMPLER: {
2079 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2080 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
2081 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002082 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002083 std::stringstream error_str;
2084 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
2085 << update->pImageInfo[di].sampler << ".";
2086 *error_msg = error_str.str();
2087 return false;
2088 }
2089 } else {
2090 // TODO : Warn here
2091 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002092 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002093 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002094 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002095 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
2096 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
2097 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
2098 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2099 auto image_view = update->pImageInfo[di].imageView;
2100 auto image_layout = update->pImageInfo[di].imageLayout;
2101 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
2102 std::stringstream error_str;
2103 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
2104 *error_msg = error_str.str();
2105 return false;
2106 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002107 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002108 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002109 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002110 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
2111 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
2112 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2113 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002114 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002115 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002116 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002117 std::stringstream error_str;
2118 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2119 *error_msg = error_str.str();
2120 return false;
2121 }
2122 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002123 auto buffer_state = GetBufferState(device_data_, buffer);
2124 // Verify that buffer underlying the view hasn't been destroyed prematurely
2125 if (!buffer_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002126 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06002127 std::stringstream error_str;
2128 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
2129 << ") has been destroyed: " << error_msg->c_str();
2130 *error_msg = error_str.str();
2131 return false;
2132 } else if (!ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002133 std::stringstream error_str;
2134 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
2135 *error_msg = error_str.str();
2136 return false;
2137 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002138 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002139 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002140 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002141 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
2142 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
2143 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
2144 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
2145 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
2146 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error_code, error_msg)) {
2147 std::stringstream error_str;
2148 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
2149 *error_msg = error_str.str();
2150 return false;
2151 }
2152 }
2153 break;
2154 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002155 case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:
2156 break;
Jeff Bolzfbe51582018-09-13 10:01:35 -05002157 case VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NVX:
2158 // XXX TODO
2159 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002160 default:
2161 assert(0); // We've already verified update type so should never get here
2162 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002163 }
2164 // All checks passed so update contents are good
2165 return true;
2166}
2167// Verify that the contents of the update are ok, but don't perform actual update
2168bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Dave Houlton00c154e2018-05-24 13:20:50 -06002169 VkDescriptorType type, uint32_t index, std::string *error_code,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002170 std::string *error_msg) const {
2171 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
2172 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06002173 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002174 case PlainSampler: {
2175 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002176 const auto src_desc = src_set->descriptors_[index + di].get();
2177 if (!src_desc->updated) continue;
2178 if (!src_desc->IsImmutableSampler()) {
2179 auto update_sampler = static_cast<SamplerDescriptor *>(src_desc)->GetSampler();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002180 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002181 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002182 std::stringstream error_str;
2183 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2184 *error_msg = error_str.str();
2185 return false;
2186 }
2187 } else {
2188 // TODO : Warn here
2189 }
2190 }
2191 break;
2192 }
2193 case ImageSampler: {
2194 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002195 const auto src_desc = src_set->descriptors_[index + di].get();
2196 if (!src_desc->updated) continue;
2197 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002198 // First validate sampler
2199 if (!img_samp_desc->IsImmutableSampler()) {
2200 auto update_sampler = img_samp_desc->GetSampler();
2201 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002202 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002203 std::stringstream error_str;
2204 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2205 *error_msg = error_str.str();
2206 return false;
2207 }
2208 } else {
2209 // TODO : Warn here
2210 }
2211 // Validate image
2212 auto image_view = img_samp_desc->GetImageView();
2213 auto image_layout = img_samp_desc->GetImageLayout();
2214 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002215 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002216 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002217 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002218 return false;
2219 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002220 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002221 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002222 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002223 case Image: {
2224 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002225 const auto src_desc = src_set->descriptors_[index + di].get();
2226 if (!src_desc->updated) continue;
2227 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002228 auto image_view = img_desc->GetImageView();
2229 auto image_layout = img_desc->GetImageLayout();
2230 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002231 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002232 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002233 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002234 return false;
2235 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002236 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002237 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002238 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002239 case TexelBuffer: {
2240 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002241 const auto src_desc = src_set->descriptors_[index + di].get();
2242 if (!src_desc->updated) continue;
2243 auto buffer_view = static_cast<TexelDescriptor *>(src_desc)->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002244 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002245 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002246 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002247 std::stringstream error_str;
2248 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2249 *error_msg = error_str.str();
2250 return false;
2251 }
2252 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002253 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002254 std::stringstream error_str;
2255 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
2256 *error_msg = error_str.str();
2257 return false;
2258 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002259 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002260 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002261 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002262 case GeneralBuffer: {
2263 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002264 const auto src_desc = src_set->descriptors_[index + di].get();
2265 if (!src_desc->updated) continue;
2266 auto buffer = static_cast<BufferDescriptor *>(src_desc)->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002267 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002268 std::stringstream error_str;
2269 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
2270 *error_msg = error_str.str();
2271 return false;
2272 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06002273 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002274 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002275 }
Jeff Bolze54ae892018-09-08 12:16:29 -05002276 case InlineUniform:
Jeff Bolzfbe51582018-09-13 10:01:35 -05002277 case AccelerationStructure:
Jeff Bolze54ae892018-09-08 12:16:29 -05002278 break;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002279 default:
2280 assert(0); // We've already verified update type so should never get here
2281 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002282 }
2283 // All checks passed so update contents are good
2284 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12002285}
Tobin Ehlisf320b192017-03-14 11:22:50 -06002286// Update the common AllocateDescriptorSetsData
2287void cvdescriptorset::UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
2288 AllocateDescriptorSetsData *ds_data) {
2289 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
2290 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
2291 if (layout) {
2292 ds_data->layout_nodes[i] = layout;
2293 // Count total descriptors required per type
2294 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
2295 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
2296 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
2297 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
2298 }
2299 }
2300 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
2301 }
Petr Kraus13c98a62017-12-09 00:22:39 +01002302}
Tobin Ehlisee471462016-05-26 11:21:59 -06002303// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlisf320b192017-03-14 11:22:50 -06002304bool cvdescriptorset::ValidateAllocateDescriptorSets(const core_validation::layer_data *dev_data,
2305 const VkDescriptorSetAllocateInfo *p_alloc_info,
2306 const AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002307 bool skip = false;
Tobin Ehlisf320b192017-03-14 11:22:50 -06002308 auto report_data = core_validation::GetReportData(dev_data);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002309 auto pool_state = GetDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlisee471462016-05-26 11:21:59 -06002310
2311 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002312 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07002313 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
2314 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR) {
2315 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 -06002316 HandleToUint64(p_alloc_info->pSetLayouts[i]), "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-00308",
John Zulauf5562d062018-01-24 11:54:05 -07002317 "Layout 0x%" PRIxLEAST64 " specified at pSetLayouts[%" PRIu32
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002318 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
John Zulauf5562d062018-01-24 11:54:05 -07002319 HandleToUint64(p_alloc_info->pSetLayouts[i]), i,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002320 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07002321 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002322 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT &&
2323 !(pool_state->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
2324 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 -06002325 0, "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-03044",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002326 "Descriptor set layout create flags and pool create flags mismatch for index (%d)", i);
2327 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002328 }
2329 }
Mark Lobodzinski28426ae2017-06-01 07:56:38 -06002330 if (!GetDeviceExtensions(dev_data)->vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002331 // Track number of descriptorSets allowable in this pool
2332 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002333 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 -06002334 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorSetCount-00306",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002335 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002336 ". This pool only has %d descriptorSets remaining.",
2337 p_alloc_info->descriptorSetCount, HandleToUint64(pool_state->pool), pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002338 }
2339 // Determine whether descriptor counts are satisfiable
Jeff Bolze54ae892018-09-08 12:16:29 -05002340 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2341 if (ds_data->required_descriptors_by_type.at(it->first) > pool_state->availableDescriptorTypeCount[it->first]) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002342 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 -06002343 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002344 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002345 ". This pool only has %d descriptors of this type remaining.",
Dave Houlton142c4cb2018-10-17 15:04:41 -06002346 ds_data->required_descriptors_by_type.at(it->first),
2347 string_VkDescriptorType(VkDescriptorType(it->first)), HandleToUint64(pool_state->pool),
2348 pool_state->availableDescriptorTypeCount[it->first]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002349 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002350 }
2351 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06002352
Jeff Bolzfdf96072018-04-10 14:32:18 -05002353 const auto *count_allocate_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2354
2355 if (count_allocate_info) {
2356 if (count_allocate_info->descriptorSetCount != 0 &&
2357 count_allocate_info->descriptorSetCount != p_alloc_info->descriptorSetCount) {
2358 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 -06002359 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-descriptorSetCount-03045",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002360 "VkDescriptorSetAllocateInfo::descriptorSetCount (%d) != "
2361 "VkDescriptorSetVariableDescriptorCountAllocateInfoEXT::descriptorSetCount (%d)",
2362 p_alloc_info->descriptorSetCount, count_allocate_info->descriptorSetCount);
2363 }
2364 if (count_allocate_info->descriptorSetCount == p_alloc_info->descriptorSetCount) {
2365 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
2366 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
2367 if (count_allocate_info->pDescriptorCounts[i] > layout->GetDescriptorCountFromBinding(layout->GetMaxBinding())) {
2368 skip |= log_msg(
2369 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 -06002370 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-pSetLayouts-03046",
2371 "pDescriptorCounts[%d] = (%d), binding's descriptorCount = (%d)", i,
Jeff Bolzfdf96072018-04-10 14:32:18 -05002372 count_allocate_info->pDescriptorCounts[i], layout->GetDescriptorCountFromBinding(layout->GetMaxBinding()));
2373 }
2374 }
2375 }
2376 }
2377
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002378 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06002379}
2380// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06002381void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
2382 const VkDescriptorSet *descriptor_sets,
2383 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06002384 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06002385 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
John Zulauf48a6a702017-12-22 17:14:54 -07002386 layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06002387 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002388 // Account for sets and individual descriptors allocated from pool
Tobin Ehlisee471462016-05-26 11:21:59 -06002389 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Jeff Bolze54ae892018-09-08 12:16:29 -05002390 for (auto it = ds_data->required_descriptors_by_type.begin(); it != ds_data->required_descriptors_by_type.end(); ++it) {
2391 pool_state->availableDescriptorTypeCount[it->first] -= ds_data->required_descriptors_by_type.at(it->first);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06002392 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002393
2394 const auto *variable_count_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2395 bool variable_count_valid = variable_count_info && variable_count_info->descriptorSetCount == p_alloc_info->descriptorSetCount;
2396
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002397 // Create tracking object for each descriptor set; insert into global map and the pool's set.
Tobin Ehlisee471462016-05-26 11:21:59 -06002398 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -05002399 uint32_t variable_count = variable_count_valid ? variable_count_info->pDescriptorCounts[i] : 0;
2400
Tobin Ehlis93f22372016-10-12 14:34:12 -06002401 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 -05002402 variable_count, dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06002403
2404 pool_state->sets.insert(new_ds);
2405 new_ds->in_use.store(0);
2406 (*set_map)[descriptor_sets[i]] = new_ds;
2407 }
2408}
John Zulauf48a6a702017-12-22 17:14:54 -07002409
2410cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2411 GLOBAL_CB_NODE *cb_state)
2412 : filtered_map_(), orig_map_(in_map) {
2413 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2414 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2415 ds.FilterAndTrackBindingReqs(cb_state, orig_map_, filtered_map_.get());
2416 }
2417}
2418cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2419 GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline)
2420 : filtered_map_(), orig_map_(in_map) {
2421 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2422 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2423 ds.FilterAndTrackBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
2424 }
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01002425}