blob: e3398edbf829dea210caacfb73b139e254baf2ae [file] [log] [blame]
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
4 * Copyright (C) 2015-2016 Google Inc.
5 *
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
John Zulauf34ebf272018-02-16 13:08:47 -070057DescriptorSetLayoutId get_canonical_id(const VkDescriptorSetLayoutCreateInfo *p_create_info) {
58 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;
John Zulaufd47d0612018-02-16 13:00:34 -0700238 if (get_layout_def() == rh_ds_layout->get_layout_def()) return true;
239 bool detailed_compat_check =
240 get_layout_def()->IsCompatible(layout_, rh_ds_layout->GetDescriptorSetLayout(), rh_ds_layout->get_layout_def(), error_msg);
241 // 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)
John Zulauf34ebf272018-02-16 13:08:47 -0700356 : layout_(layout), layout_destroyed_(false), layout_id_(get_canonical_id(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,
362 const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features) {
Tobin Ehlis154c2692016-10-25 09:36:53 -0600363 bool skip = false;
364 std::unordered_set<uint32_t> bindings;
John Zulauf0fdeab32018-01-23 11:27:35 -0700365 uint64_t total_descriptors = 0;
366
Jeff Bolzfdf96072018-04-10 14:32:18 -0500367 const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(create_info->pNext);
368
369 const bool push_descriptor_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR);
John Zulauf0fdeab32018-01-23 11:27:35 -0700370 if (push_descriptor_set && !push_descriptor_ext) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600371 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Mark Lobodzinski88529492018-04-01 10:38:15 -0600372 DRAWSTATE_EXTENSION_NOT_ENABLED,
Mark Lobodzinskifb5a3e62018-04-13 10:46:48 -0600373 "Attempted to use %s in %s but its required extension %s has not been enabled.\n",
John Zulauf0fdeab32018-01-23 11:27:35 -0700374 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR", "VkDescriptorSetLayoutCreateInfo::flags",
375 VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
376 }
377
Jeff Bolzfdf96072018-04-10 14:32:18 -0500378 const bool update_after_bind_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT);
379 if (update_after_bind_set && !descriptor_indexing_ext) {
380 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
381 DRAWSTATE_EXTENSION_NOT_ENABLED,
382 "Attemped to use %s in %s but its required extension %s has not been enabled.\n",
383 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT", "VkDescriptorSetLayoutCreateInfo::flags",
384 VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
385 }
386
John Zulauf0fdeab32018-01-23 11:27:35 -0700387 auto valid_type = [push_descriptor_set](const VkDescriptorType type) {
388 return !push_descriptor_set ||
389 ((type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && (type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC));
390 };
391
Jeff Bolzfdf96072018-04-10 14:32:18 -0500392 uint32_t max_binding = 0;
393
Tobin Ehlis154c2692016-10-25 09:36:53 -0600394 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
John Zulauf0fdeab32018-01-23 11:27:35 -0700395 const auto &binding_info = create_info->pBindings[i];
Jeff Bolzfdf96072018-04-10 14:32:18 -0500396 max_binding = std::max(max_binding, binding_info.binding);
397
John Zulauf0fdeab32018-01-23 11:27:35 -0700398 if (!bindings.insert(binding_info.binding).second) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600399 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 -0600400 "VUID-VkDescriptorSetLayoutCreateInfo-binding-00279",
401 "duplicated binding number in VkDescriptorSetLayoutBinding.");
Tobin Ehlis154c2692016-10-25 09:36:53 -0600402 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700403 if (!valid_type(binding_info.descriptorType)) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600404 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 -0600405 "VUID-VkDescriptorSetLayoutCreateInfo-flags-00280",
Mark Lobodzinski487a0d12018-03-30 10:09:03 -0600406 "invalid type %s ,for push descriptors in VkDescriptorSetLayoutBinding entry %" PRIu32 ".",
407 string_VkDescriptorType(binding_info.descriptorType), i);
John Zulauf0fdeab32018-01-23 11:27:35 -0700408 }
409 total_descriptors += binding_info.descriptorCount;
Tobin Ehlis154c2692016-10-25 09:36:53 -0600410 }
John Zulauf0fdeab32018-01-23 11:27:35 -0700411
Jeff Bolzfdf96072018-04-10 14:32:18 -0500412 if (flags_create_info) {
413 if (flags_create_info->bindingCount != 0 && flags_create_info->bindingCount != create_info->bindingCount) {
414 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 -0600415 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-bindingCount-03002",
Jeff Bolzfdf96072018-04-10 14:32:18 -0500416 "VkDescriptorSetLayoutCreateInfo::bindingCount (%d) != "
417 "VkDescriptorSetLayoutBindingFlagsCreateInfoEXT::bindingCount (%d)",
418 create_info->bindingCount, flags_create_info->bindingCount);
419 }
420
421 if (flags_create_info->bindingCount == create_info->bindingCount) {
422 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
423 const auto &binding_info = create_info->pBindings[i];
424
425 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) {
426 if (!update_after_bind_set) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600427 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
428 "VUID-VkDescriptorSetLayoutCreateInfo-flags-03000",
429 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500430 }
431
432 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER &&
433 !descriptor_indexing_features->descriptorBindingUniformBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600434 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
435 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
436 "descriptorBindingUniformBufferUpdateAfterBind-03005",
437 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500438 }
439 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
440 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
441 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) &&
442 !descriptor_indexing_features->descriptorBindingSampledImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600443 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
444 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
445 "descriptorBindingSampledImageUpdateAfterBind-03006",
446 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500447 }
448 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE &&
449 !descriptor_indexing_features->descriptorBindingStorageImageUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600450 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
451 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
452 "descriptorBindingStorageImageUpdateAfterBind-03007",
453 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500454 }
455 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER &&
456 !descriptor_indexing_features->descriptorBindingStorageBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600457 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
458 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
459 "descriptorBindingStorageBufferUpdateAfterBind-03008",
460 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500461 }
462 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
463 !descriptor_indexing_features->descriptorBindingUniformTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600464 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
465 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
466 "descriptorBindingUniformTexelBufferUpdateAfterBind-03009",
467 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500468 }
469 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER &&
470 !descriptor_indexing_features->descriptorBindingStorageTexelBufferUpdateAfterBind) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600471 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
472 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-"
473 "descriptorBindingStorageTexelBufferUpdateAfterBind-03010",
474 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500475 }
476 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT ||
477 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
478 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600479 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
480 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-None-03011",
481 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500482 }
483 }
484
485 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT) {
486 if (!descriptor_indexing_features->descriptorBindingUpdateUnusedWhilePending) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600487 skip |= log_msg(
488 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
489 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingUpdateUnusedWhilePending-03012",
490 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500491 }
492 }
493
494 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) {
495 if (!descriptor_indexing_features->descriptorBindingPartiallyBound) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600496 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
497 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingPartiallyBound-03013",
498 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500499 }
500 }
501
502 if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT) {
503 if (binding_info.binding != max_binding) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600504 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
505 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03004",
506 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500507 }
508
509 if (!descriptor_indexing_features->descriptorBindingVariableDescriptorCount) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600510 skip |= log_msg(
511 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
512 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-descriptorBindingVariableDescriptorCount-03014",
513 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500514 }
515 if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
516 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
Dave Houltond8ed0212018-05-16 17:18:24 -0600517 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
518 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-pBindingFlags-03015",
519 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500520 }
521 }
522
523 if (push_descriptor_set &&
524 (flags_create_info->pBindingFlags[i] &
525 (VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
526 VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT))) {
527 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 -0600528 "VUID-VkDescriptorSetLayoutBindingFlagsCreateInfoEXT-flags-03003",
529 "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
Jeff Bolzfdf96072018-04-10 14:32:18 -0500530 }
531 }
532 }
533 }
534
John Zulauf0fdeab32018-01-23 11:27:35 -0700535 if ((push_descriptor_set) && (total_descriptors > max_push_descriptors)) {
536 const char *undefined = push_descriptor_ext ? "" : " -- undefined";
Dave Houltond8ed0212018-05-16 17:18:24 -0600537 skip |=
538 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
539 "VUID-VkDescriptorSetLayoutCreateInfo-flags-00281",
540 "for push descriptor, total descriptor count in layout (%" PRIu64
541 ") must not be greater than VkPhysicalDevicePushDescriptorPropertiesKHR::maxPushDescriptors (%" PRIu32 "%s).",
542 total_descriptors, max_push_descriptors, undefined);
John Zulauf0fdeab32018-01-23 11:27:35 -0700543 }
544
Tobin Ehlis154c2692016-10-25 09:36:53 -0600545 return skip;
546}
547
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600548cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
549 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
550
Tobin Ehlis93f22372016-10-12 14:34:12 -0600551cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
Jeff Bolzfdf96072018-04-10 14:32:18 -0500552 const std::shared_ptr<DescriptorSetLayout const> &layout, uint32_t variable_count,
553 layer_data *dev_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700554 : some_update_(false),
555 set_(set),
556 pool_state_(nullptr),
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600557 p_layout_(layout),
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700558 device_data_(dev_data),
Jeff Bolzfdf96072018-04-10 14:32:18 -0500559 limits_(GetPhysDevProperties(dev_data)->properties.limits),
560 variable_count_(variable_count) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700561 pool_state_ = GetDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600562 // Foreach binding, create default descriptors of given type
John Zulaufb6d71202017-12-22 16:47:09 -0700563 descriptors_.reserve(p_layout_->GetTotalDescriptorCount());
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600564 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
565 auto type = p_layout_->GetTypeFromIndex(i);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600566 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700567 case VK_DESCRIPTOR_TYPE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600568 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
569 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600570 if (immut_sampler) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700571 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600572 some_update_ = true; // Immutable samplers are updated at creation
573 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700574 descriptors_.emplace_back(new SamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700575 }
576 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600577 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700578 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600579 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
580 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600581 if (immut) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700582 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600583 some_update_ = true; // Immutable samplers are updated at creation
584 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700585 descriptors_.emplace_back(new ImageSamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700586 }
587 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600588 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700589 // ImageDescriptors
590 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
591 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
592 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600593 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700594 descriptors_.emplace_back(new ImageDescriptor(type));
595 break;
596 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
597 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600598 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700599 descriptors_.emplace_back(new TexelDescriptor(type));
600 break;
601 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
602 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
603 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
604 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600605 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700606 descriptors_.emplace_back(new BufferDescriptor(type));
607 break;
608 default:
609 assert(0); // Bad descriptor type specified
610 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600611 }
612 }
613}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600614
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700615cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200616
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700617static std::string string_descriptor_req_view_type(descriptor_req req) {
618 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200619 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
620 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700621 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700622 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200623 }
624 }
625
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700626 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700627
628 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200629}
630
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600631// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600632bool cvdescriptorset::DescriptorSet::IsCompatible(DescriptorSetLayout const *const layout, std::string *error) const {
633 return layout->IsCompatible(p_layout_.get(), error);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600634}
Chris Forbes57989132016-07-26 17:06:10 +1200635
Tobin Ehlis3066db62016-08-22 08:12:23 -0600636// 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 -0600637// This includes validating that all descriptors in the given bindings are updated,
638// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
639// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600640bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
John Zulauf48a6a702017-12-22 17:14:54 -0700641 const std::vector<uint32_t> &dynamic_offsets, GLOBAL_CB_NODE *cb_node,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600642 const char *caller, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200643 for (auto binding_pair : bindings) {
644 auto binding = binding_pair.first;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600645 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600646 std::stringstream error_str;
647 error_str << "Attempting to validate DrawState for binding #" << binding
648 << " which is an invalid binding for this descriptor set.";
649 *error = error_str.str();
650 return false;
651 }
John Zulaufc483f442017-12-15 14:02:06 -0700652 IndexRange index_range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700653 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Jeff Bolzfdf96072018-04-10 14:32:18 -0500654
655 if (IsVariableDescriptorCount(binding)) {
656 // Only validate the first N descriptors if it uses variable_count
657 index_range.end = index_range.start + GetVariableDescriptorCount();
658 }
659
John Zulaufc483f442017-12-15 14:02:06 -0700660 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
Jeff Bolzfdf96072018-04-10 14:32:18 -0500661 if (p_layout_->GetDescriptorBindingFlagsFromBinding(binding) & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) {
662 // Can't validate the descriptor because it may not have been updated,
663 // or the view could have been destroyed
664 continue;
665 } else if (!descriptors_[i]->updated) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700666 std::stringstream error_str;
667 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
668 << " is being used in draw but has not been updated.";
669 *error = error_str.str();
670 return false;
671 } else {
672 auto descriptor_class = descriptors_[i]->GetClass();
673 if (descriptor_class == GeneralBuffer) {
674 // Verify that buffers are valid
675 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
676 auto buffer_node = GetBufferState(device_data_, buffer);
677 if (!buffer_node) {
678 std::stringstream error_str;
679 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
680 << " references invalid buffer " << buffer << ".";
681 *error = error_str.str();
682 return false;
John Zulauf48a6a702017-12-22 17:14:54 -0700683 } else if (!buffer_node->sparse) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700684 for (auto mem_binding : buffer_node->GetBoundMemory()) {
685 if (!GetMemObjInfo(device_data_, mem_binding)) {
686 std::stringstream error_str;
687 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
688 << " uses buffer " << buffer << " that references invalid memory " << mem_binding << ".";
689 *error = error_str.str();
Tobin Ehlisc8266452017-04-07 12:20:30 -0600690 return false;
691 }
692 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700693 }
694 if (descriptors_[i]->IsDynamic()) {
695 // Validate that dynamic offsets are within the buffer
696 auto buffer_size = buffer_node->createInfo.size;
697 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
698 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
699 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
700 if (VK_WHOLE_SIZE == range) {
701 if ((dyn_offset + desc_offset) > buffer_size) {
702 std::stringstream error_str;
703 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
704 << " uses buffer " << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset "
705 << dyn_offset << " combined with offset " << desc_offset
706 << " that oversteps the buffer size of " << buffer_size << ".";
707 *error = error_str.str();
708 return false;
709 }
710 } else {
711 if ((dyn_offset + desc_offset + range) > buffer_size) {
712 std::stringstream error_str;
713 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
714 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
715 << " combined with offset " << desc_offset << " and range " << range
716 << " that oversteps the buffer size of " << buffer_size << ".";
717 *error = error_str.str();
718 return false;
719 }
720 }
721 }
722 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
723 VkImageView image_view;
724 VkImageLayout image_layout;
725 if (descriptor_class == ImageSampler) {
726 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
727 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
728 } else {
729 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
730 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
731 }
732 auto reqs = binding_pair.second;
733
734 auto image_view_state = GetImageViewState(device_data_, image_view);
Tobin Ehlis836a1372017-07-14 11:25:21 -0600735 if (nullptr == image_view_state) {
736 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
737 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
738 std::stringstream error_str;
739 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
740 << " is using imageView " << image_view << " that has been destroyed.";
741 *error = error_str.str();
742 return false;
743 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700744 auto image_view_ci = image_view_state->create_info;
745
746 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
747 // bad view type
748 std::stringstream error_str;
749 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
750 << " requires an image view of type " << string_descriptor_req_view_type(reqs) << " but got "
751 << string_VkImageViewType(image_view_ci.viewType) << ".";
752 *error = error_str.str();
753 return false;
754 }
755
756 auto image_node = GetImageState(device_data_, image_view_ci.image);
757 assert(image_node);
758 // Verify Image Layout
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700759 // Copy first mip level into sub_layers and loop over each mip level to verify layout
760 VkImageSubresourceLayers sub_layers;
761 sub_layers.aspectMask = image_view_ci.subresourceRange.aspectMask;
762 sub_layers.baseArrayLayer = image_view_ci.subresourceRange.baseArrayLayer;
763 sub_layers.layerCount = image_view_ci.subresourceRange.layerCount;
764 bool hit_error = false;
765 for (auto cur_level = image_view_ci.subresourceRange.baseMipLevel;
766 cur_level < image_view_ci.subresourceRange.levelCount; ++cur_level) {
767 sub_layers.mipLevel = cur_level;
768 VerifyImageLayout(device_data_, cb_node, image_node, sub_layers, image_layout, VK_IMAGE_LAYOUT_UNDEFINED,
Dave Houltond8ed0212018-05-16 17:18:24 -0600769 caller, "VUID-VkDescriptorImageInfo-imageLayout-00344", &hit_error);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700770 if (hit_error) {
771 *error =
Dave Houltona9df0ce2018-02-07 10:51:23 -0700772 "Image layout specified at vkUpdateDescriptorSets() time doesn't match actual image layout at time "
773 "descriptor is used. See previous error callback for specific details.";
Chris Forbes57989132016-07-26 17:06:10 +1200774 return false;
775 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700776 }
777 // Verify Sample counts
778 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
779 std::stringstream error_str;
780 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
781 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
782 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
783 *error = error_str.str();
784 return false;
785 }
786 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
787 std::stringstream error_str;
788 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
789 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
790 *error = error_str.str();
791 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200792 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600793 }
Tobin Ehlisb1a2e4b2018-03-16 07:54:24 -0600794 if (descriptor_class == ImageSampler || descriptor_class == PlainSampler) {
795 // Verify Sampler still valid
796 VkSampler sampler;
797 if (descriptor_class == ImageSampler) {
798 sampler = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetSampler();
799 } else {
800 sampler = static_cast<SamplerDescriptor *>(descriptors_[i].get())->GetSampler();
801 }
802 if (!ValidateSampler(sampler, device_data_)) {
803 std::stringstream error_str;
804 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
805 << " is using sampler " << sampler << " that has been destroyed.";
806 *error = error_str.str();
807 return false;
808 }
809 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600810 }
811 }
812 }
813 return true;
814}
Chris Forbes57989132016-07-26 17:06:10 +1200815
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600816// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600817uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600818 std::unordered_set<VkBuffer> *buffer_set,
819 std::unordered_set<VkImageView> *image_set) const {
820 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200821 for (auto binding_pair : bindings) {
822 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600823 // If a binding doesn't exist, skip it
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600824 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600825 continue;
826 }
John Zulaufc483f442017-12-15 14:02:06 -0700827 uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600828 if (descriptors_[start_idx]->IsStorage()) {
829 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600830 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600831 if (descriptors_[start_idx + i]->updated) {
832 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600833 num_updates++;
834 }
835 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600836 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600837 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600838 if (descriptors_[start_idx + i]->updated) {
839 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700840 auto bv_state = GetBufferViewState(device_data_, bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600841 if (bv_state) {
842 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600843 num_updates++;
844 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600845 }
846 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600847 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600848 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600849 if (descriptors_[start_idx + i]->updated) {
850 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600851 num_updates++;
852 }
853 }
854 }
855 }
856 }
857 return num_updates;
858}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600859// Set is being deleted or updates so invalidate all bound cmd buffers
860void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Petr Krausbc7f5442017-05-14 23:43:38 +0200861 core_validation::invalidateCommandBuffers(device_data_, cb_bindings, {HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600862}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600863// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600864void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700865 // Perform update on a per-binding basis as consecutive updates roll over to next binding
866 auto descriptors_remaining = update->descriptorCount;
867 auto binding_being_updated = update->dstBinding;
868 auto offset = update->dstArrayElement;
Tobin Ehlise16805c2017-08-09 09:10:37 -0600869 uint32_t update_index = 0;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700870 while (descriptors_remaining) {
871 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
John Zulaufc483f442017-12-15 14:02:06 -0700872 auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700873 // Loop over the updates for a single binding at a time
Tobin Ehlise16805c2017-08-09 09:10:37 -0600874 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
875 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700876 }
877 // Roll over to next binding in case of consecutive update
878 descriptors_remaining -= update_count;
879 offset = 0;
880 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600881 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700882 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600883
Jeff Bolzfdf96072018-04-10 14:32:18 -0500884 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
885 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
886 InvalidateBoundCmdBuffers();
887 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600888}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600889// Validate Copy update
890bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
Dave Houlton00c154e2018-05-24 13:20:50 -0600891 const DescriptorSet *src_set, std::string *error_code,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600892 std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -0700893 // Verify dst layout still valid
894 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600895 *error_code = "VUID-VkCopyDescriptorSet-dstSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -0700896 string_sprintf(error_msg,
897 "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set dstSet 0x%" PRIxLEAST64
898 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
899 HandleToUint64(set_), HandleToUint64(p_layout_->GetDescriptorSetLayout()));
900 return false;
901 }
902
903 // Verify src layout still valid
904 if (src_set->p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600905 *error_code = "VUID-VkCopyDescriptorSet-srcSet-parameter";
John Zulauf5dfd45c2018-01-17 11:06:34 -0700906 string_sprintf(
907 error_msg,
908 "Cannot call vkUpdateDescriptorSets() to perform copy update of dstSet 0x%" PRIxLEAST64
909 " from descriptor set srcSet 0x%" PRIxLEAST64 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
910 HandleToUint64(set_), HandleToUint64(src_set->set_), HandleToUint64(src_set->p_layout_->GetDescriptorSetLayout()));
911 return false;
912 }
913
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600914 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600915 *error_code = "VUID-VkCopyDescriptorSet-dstBinding-00347";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600916 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700917 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600918 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600919 return false;
920 }
921 if (!src_set->HasBinding(update->srcBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600922 *error_code = "VUID-VkCopyDescriptorSet-srcBinding-00345";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600923 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700924 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600925 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600926 return false;
927 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500928 // Verify idle ds
929 if (in_use.load() &&
930 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
931 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
932 // TODO : Re-using Free Idle error code, need copy update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -0600933 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -0500934 std::stringstream error_str;
935 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
936 << " that is in use by a command buffer";
937 *error_msg = error_str.str();
938 return false;
939 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600940 // src & dst set bindings are valid
941 // Check bounds of src & dst
John Zulaufc483f442017-12-15 14:02:06 -0700942 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600943 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
944 // SRC update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -0600945 *error_code = "VUID-VkCopyDescriptorSet-srcArrayElement-00346";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600946 std::stringstream error_str;
947 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
John Zulaufc483f442017-12-15 14:02:06 -0700948 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600949 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700950 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600951 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600952 return false;
953 }
John Zulaufc483f442017-12-15 14:02:06 -0700954 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600955 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600956 // DST update out of bounds
Dave Houlton00c154e2018-05-24 13:20:50 -0600957 *error_code = "VUID-VkCopyDescriptorSet-dstArrayElement-00348";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600958 std::stringstream error_str;
959 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
John Zulaufc483f442017-12-15 14:02:06 -0700960 << " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600961 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600962 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600963 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600964 return false;
965 }
966 // Check that types match
Dave Houltond8ed0212018-05-16 17:18:24 -0600967 // TODO : Base default error case going from here is "VUID-VkAcquireNextImageInfoKHR-semaphore-parameter"2ba which covers all
968 // consistency issues, need more fine-grained error codes
Dave Houlton00c154e2018-05-24 13:20:50 -0600969 *error_code = "VUID-VkCopyDescriptorSet-srcSet-00349";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600970 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600971 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600972 if (src_type != dst_type) {
973 std::stringstream error_str;
974 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
975 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700976 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600977 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600978 return false;
979 }
980 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600981 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600982 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600983 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600984 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600985 return false;
986 }
Jeff Bolzfdf96072018-04-10 14:32:18 -0500987
988 if ((src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
989 !(GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -0600990 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01918";
Jeff Bolzfdf96072018-04-10 14:32:18 -0500991 std::stringstream error_str;
992 error_str << "If pname:srcSet's (" << update->srcSet
993 << ") layout was created with the "
994 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
995 "set, then pname:dstSet's ("
996 << update->dstSet
997 << ") layout must: also have been created with the "
998 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
999 *error_msg = error_str.str();
1000 return false;
1001 }
1002
1003 if (!(src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
1004 (GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001005 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01919";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001006 std::stringstream error_str;
1007 error_str << "If pname:srcSet's (" << update->srcSet
1008 << ") layout was created without the "
1009 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
1010 "set, then pname:dstSet's ("
1011 << update->dstSet
1012 << ") layout must: also have been created without the "
1013 "ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
1014 *error_msg = error_str.str();
1015 return false;
1016 }
1017
1018 if ((src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1019 !(GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001020 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01920";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001021 std::stringstream error_str;
1022 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1023 << ") was allocated was created "
1024 "with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1025 "set, then the descriptor pool from which pname:dstSet ("
1026 << update->dstSet
1027 << ") was allocated must: "
1028 "also have been created with the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1029 *error_msg = error_str.str();
1030 return false;
1031 }
1032
1033 if (!(src_set->GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT) &&
1034 (GetPoolState()->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001035 *error_code = "VUID-VkCopyDescriptorSet-srcSet-01921";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001036 std::stringstream error_str;
1037 error_str << "If the descriptor pool from which pname:srcSet (" << update->srcSet
1038 << ") was allocated was created "
1039 "without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag "
1040 "set, then the descriptor pool from which pname:dstSet ("
1041 << update->dstSet
1042 << ") was allocated must: "
1043 "also have been created without the ename:VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT flag set";
1044 *error_msg = error_str.str();
1045 return false;
1046 }
1047
Tobin Ehlisd41e7b62016-05-19 07:56:18 -06001048 // Update parameters all look good and descriptor updated so verify update contents
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001049 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error_code, error_msg)) return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001050
1051 // All checks passed so update is good
1052 return true;
1053}
1054// Perform Copy update
1055void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -07001056 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
1057 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001058 // Update parameters all look good so perform update
1059 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001060 auto src = src_set->descriptors_[src_start_idx + di].get();
1061 auto dst = descriptors_[dst_start_idx + di].get();
1062 if (src->updated) {
1063 dst->CopyUpdate(src);
1064 some_update_ = true;
1065 } else {
1066 dst->updated = false;
1067 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001068 }
Tobin Ehlis56a30942016-05-19 08:00:00 -06001069
Jeff Bolzfdf96072018-04-10 14:32:18 -05001070 if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1071 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1072 InvalidateBoundCmdBuffers();
1073 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001074}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001075
Tobin Ehlisf9519102016-08-17 09:49:13 -06001076// Bind cb_node to this set and this set to cb_node.
1077// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
1078// to be used in a draw by the given cb_node
Tobin Ehlis276d3d32016-12-21 09:21:06 -07001079void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node,
Tobin Ehlis022528b2016-12-29 12:22:32 -07001080 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001081 // bind cb to this descriptor set
1082 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001083 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Petr Krausbc7f5442017-05-14 23:43:38 +02001084 cb_node->object_bindings.insert({HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis7ca20be2016-10-12 15:09:16 -06001085 pool_state_->cb_bindings.insert(cb_node);
Petr Krausbc7f5442017-05-14 23:43:38 +02001086 cb_node->object_bindings.insert({HandleToUint64(pool_state_->pool), kVulkanObjectTypeDescriptorPool});
Tobin Ehlisf9519102016-08-17 09:49:13 -06001087 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
1088 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -07001089 for (auto binding_req_pair : binding_req_map) {
1090 auto binding = binding_req_pair.first;
John Zulaufc483f442017-12-15 14:02:06 -07001091 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
1092 for (uint32_t i = range.start; i < range.end; ++i) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001093 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
1094 }
1095 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001096}
John Zulauf48a6a702017-12-22 17:14:54 -07001097void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1098 const BindingReqMap &in_req, BindingReqMap *out_req,
1099 TrackedBindings *bindings) {
1100 assert(out_req);
1101 assert(bindings);
1102 const auto binding = binding_req_pair.first;
1103 // Use insert and look at the boolean ("was inserted") in the returned pair to see if this is a new set member.
1104 // Saves one hash lookup vs. find ... compare w/ end ... insert.
1105 const auto it_bool_pair = bindings->insert(binding);
1106 if (it_bool_pair.second) {
1107 out_req->emplace(binding_req_pair);
1108 }
1109}
1110void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
1111 const BindingReqMap &in_req, BindingReqMap *out_req,
1112 TrackedBindings *bindings, uint32_t limit) {
1113 if (bindings->size() < limit) FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, bindings);
1114}
1115
1116void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, const BindingReqMap &in_req,
1117 BindingReqMap *out_req) {
1118 TrackedBindings &bound = cached_validation_[cb_state].command_binding_and_usage;
1119 if (bound.size() == GetBindingCount()) {
1120 return; // All bindings are bound, out req is empty
1121 }
1122 for (const auto &binding_req_pair : in_req) {
1123 const auto binding = binding_req_pair.first;
1124 // If a binding doesn't exist, or has already been bound, skip it
1125 if (p_layout_->HasBinding(binding)) {
1126 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, &bound);
1127 }
1128 }
1129}
1130
1131void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline,
1132 const BindingReqMap &in_req, BindingReqMap *out_req) {
1133 auto &validated = cached_validation_[cb_state];
1134 auto &image_sample_val = validated.image_samplers[pipeline];
1135 auto *const dynamic_buffers = &validated.dynamic_buffers;
1136 auto *const non_dynamic_buffers = &validated.non_dynamic_buffers;
1137 const auto &stats = p_layout_->GetBindingTypeStats();
1138 for (const auto &binding_req_pair : in_req) {
1139 auto binding = binding_req_pair.first;
1140 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
1141 if (!layout_binding) {
1142 continue;
1143 }
1144 // Caching criteria differs per type.
1145 // If image_layout have changed , the image descriptors need to be validated against them.
1146 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
1147 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
1148 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, dynamic_buffers, stats.dynamic_buffer_count);
1149 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
1150 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
1151 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
1152 } else {
1153 // This is rather crude, as the changed layouts may not impact the bound descriptors,
1154 // but the simple "versioning" is a simple "dirt" test.
1155 auto &version = image_sample_val[binding]; // Take advantage of default construtor zero initialzing new entries
1156 if (version != cb_state->image_layout_change_count) {
1157 version = cb_state->image_layout_change_count;
1158 out_req->emplace(binding_req_pair);
1159 }
1160 }
1161 }
1162}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -06001163
Tobin Ehlis300888c2016-05-18 13:43:26 -06001164cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001165 updated = false;
1166 descriptor_class = PlainSampler;
1167 if (immut) {
1168 sampler_ = *immut;
1169 immutable_ = true;
1170 updated = true;
1171 }
1172}
Tobin Ehlise2f80292016-06-02 10:08:53 -06001173// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001174bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const layer_data *dev_data) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001175 return (GetSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001176}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001177
Tobin Ehlis554bf382016-05-24 11:14:43 -06001178bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06001179 const layer_data *dev_data, std::string *error_code, std::string *error_msg) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001180 // TODO : Defaulting to 00943 for all cases here. Need to create new error codes for various cases.
Dave Houlton00c154e2018-05-24 13:20:50 -06001181 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00326";
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001182 auto iv_state = GetImageViewState(dev_data, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001183 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001184 std::stringstream error_str;
1185 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001186 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001187 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001188 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001189 // 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 -06001190 // Validate that imageLayout is compatible with aspect_mask and image format
1191 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001192 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
1193 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001194 VkFormat format = VK_FORMAT_MAX_ENUM;
1195 VkImageUsageFlags usage = 0;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001196 auto image_node = GetImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001197 if (image_node) {
1198 format = image_node->createInfo.format;
1199 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001200 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001201 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
1202 // the error here occurs is if memory bound to a created imageView has been freed.
Dave Houltond8ed0212018-05-16 17:18:24 -06001203 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()",
1204 "VUID-VkImageViewCreateInfo-image-01020")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001205 *error_code = "VUID-VkImageViewCreateInfo-image-01020";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001206 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -06001207 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001208 }
Chris Forbes67757ff2017-07-21 13:59:01 -07001209
1210 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
1211 // but not binding them to descriptor sets.
1212 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D &&
1213 (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
1214 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001215 *error_code = "VUID-VkDescriptorImageInfo-imageView-00343";
Chris Forbes67757ff2017-07-21 13:59:01 -07001216 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
1217 return false;
1218 }
Tobin Ehlis1809f912016-05-25 09:24:36 -06001219 }
1220 // First validate that format and layout are compatible
1221 if (format == VK_FORMAT_MAX_ENUM) {
1222 std::stringstream error_str;
1223 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001224 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001225 return false;
1226 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001227 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
1228 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -06001229 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -06001230 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001231 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1232 // Only Color bit must be set
1233 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -06001234 std::stringstream error_str;
Dave Houltona9df0ce2018-02-07 10:51:23 -07001235 error_str
1236 << "ImageView (" << image_view
1237 << ") 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 -06001238 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001239 return false;
1240 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001241 // format must NOT be DS
1242 if (ds) {
1243 std::stringstream error_str;
1244 error_str << "ImageView (" << image_view
1245 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
1246 << string_VkFormat(format) << " which is not a color format.";
1247 *error_msg = error_str.str();
1248 return false;
1249 }
1250 break;
1251 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1252 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1253 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001254 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1255 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1256 // both must NOT be set
1257 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001258 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001259 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001260 return false;
1261 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001262 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1263 // Neither were set
1264 std::stringstream error_str;
1265 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1266 << " but does not have STENCIL or DEPTH aspects set";
1267 *error_msg = error_str.str();
1268 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -06001269 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001270 // format must be DS
1271 if (!ds) {
1272 std::stringstream error_str;
1273 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1274 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
1275 *error_msg = error_str.str();
1276 return false;
1277 }
1278 break;
1279 default:
1280 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
1281 if (ds) {
1282 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
1283 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
1284 // both must NOT be set
1285 std::stringstream error_str;
1286 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
1287 << " and is using depth/stencil image of format " << string_VkFormat(format)
1288 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
1289 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
1290 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
1291 "reads respectively.";
1292 *error_msg = error_str.str();
1293 return false;
1294 }
1295 }
1296 }
1297 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001298 }
1299 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -06001300 // 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 -06001301 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
1302 // under vkCreateImage()
Dave Houltond8ed0212018-05-16 17:18:24 -06001303 // TODO : Need to also validate case "VUID-VkWriteDescriptorSet-descriptorType-00336" where STORAGE_IMAGE & INPUT_ATTACH types
1304 // must have been created with identify swizzle
Tobin Ehlis1809f912016-05-25 09:24:36 -06001305 std::string error_usage_bit;
1306 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001307 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1308 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1309 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
1310 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
1311 }
1312 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001313 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001314 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1315 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1316 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
1317 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
1318 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001319 // TODO : Need to create custom enum error codes for these cases
1320 if (image_node->shared_presentable) {
1321 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001322 error_str << "ImageView (" << image_view
1323 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
1324 "layout "
1325 << string_VkImageLayout(image_layout)
1326 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report "
1327 "support for VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the "
1328 "VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001329 *error_msg = error_str.str();
1330 return false;
1331 }
1332 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001333 error_str << "ImageView (" << image_view
1334 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
1335 << string_VkImageLayout(image_layout)
1336 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage "
1337 "images can only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -06001338 *error_msg = error_str.str();
1339 return false;
1340 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001341 }
1342 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001343 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001344 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
1345 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
1346 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
1347 }
1348 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001349 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001350 default:
1351 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -06001352 }
1353 if (!error_usage_bit.empty()) {
1354 std::stringstream error_str;
1355 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
1356 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1357 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001358 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001359 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001360 }
1361 return true;
1362}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001363
Tobin Ehlis300888c2016-05-18 13:43:26 -06001364void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Chris Forbesfea2c542018-04-13 09:34:15 -07001365 if (!immutable_) {
1366 sampler_ = update->pImageInfo[index].sampler;
1367 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001368 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001369}
1370
Tobin Ehlis300888c2016-05-18 13:43:26 -06001371void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001372 if (!immutable_) {
1373 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001374 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001375 }
1376 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001377}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001378
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001379void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001380 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001381 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001382 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001383 }
1384}
1385
Tobin Ehlis300888c2016-05-18 13:43:26 -06001386cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001387 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001388 updated = false;
1389 descriptor_class = ImageSampler;
1390 if (immut) {
1391 sampler_ = *immut;
1392 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001393 }
1394}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001395
Tobin Ehlis300888c2016-05-18 13:43:26 -06001396void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001397 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001398 const auto &image_info = update->pImageInfo[index];
Chris Forbesfea2c542018-04-13 09:34:15 -07001399 if (!immutable_) {
1400 sampler_ = image_info.sampler;
1401 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001402 image_view_ = image_info.imageView;
1403 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001404}
1405
Tobin Ehlis300888c2016-05-18 13:43:26 -06001406void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001407 if (!immutable_) {
1408 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001409 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001410 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001411 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1412 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001413 updated = true;
1414 image_view_ = image_view;
1415 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001416}
1417
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001418void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001419 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001420 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001421 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001422 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001423 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001424 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001425 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001426 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001427 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001428 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001429}
1430
Tobin Ehlis300888c2016-05-18 13:43:26 -06001431cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1432 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001433 updated = false;
1434 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001435 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001436}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001437
Tobin Ehlis300888c2016-05-18 13:43:26 -06001438void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001439 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001440 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001441 image_view_ = image_info.imageView;
1442 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001443}
1444
Tobin Ehlis300888c2016-05-18 13:43:26 -06001445void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001446 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1447 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001448 updated = true;
1449 image_view_ = image_view;
1450 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001451}
1452
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001453void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001454 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001455 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001456 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001457 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001458 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001459}
1460
Tobin Ehlis300888c2016-05-18 13:43:26 -06001461cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1462 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001463 updated = false;
1464 descriptor_class = GeneralBuffer;
1465 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1466 dynamic_ = true;
1467 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1468 storage_ = true;
1469 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1470 dynamic_ = true;
1471 storage_ = true;
1472 }
1473}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001474void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001475 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001476 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001477 buffer_ = buffer_info.buffer;
1478 offset_ = buffer_info.offset;
1479 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001480}
1481
Tobin Ehlis300888c2016-05-18 13:43:26 -06001482void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1483 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001484 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001485 buffer_ = buff_desc->buffer_;
1486 offset_ = buff_desc->offset_;
1487 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001488}
1489
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001490void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001491 auto buffer_node = GetBufferState(dev_data, buffer_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001492 if (buffer_node) core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001493}
1494
Tobin Ehlis300888c2016-05-18 13:43:26 -06001495cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001496 updated = false;
1497 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001498 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001499}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001500
Tobin Ehlis300888c2016-05-18 13:43:26 -06001501void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001502 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001503 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001504}
1505
Tobin Ehlis300888c2016-05-18 13:43:26 -06001506void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1507 updated = true;
1508 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1509}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001510
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001511void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001512 auto bv_state = GetBufferViewState(dev_data, buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001513 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001514 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001515 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001516}
1517
Tobin Ehlis300888c2016-05-18 13:43:26 -06001518// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1519// sets, and then calls their respective Validate[Write|Copy]Update functions.
1520// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1521// be skipped, then true is returned.
1522// If there is no issue with the update, then false is returned.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001523bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data, const layer_data *dev_data,
1524 uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001525 const VkCopyDescriptorSet *p_cds) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001526 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001527 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001528 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001529 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001530 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001531 if (!set_node) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001532 skip |=
Tobin Ehlis300888c2016-05-18 13:43:26 -06001533 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 -06001534 HandleToUint64(dest_set), DRAWSTATE_INVALID_DESCRIPTOR_SET,
Tobin Ehlis300888c2016-05-18 13:43:26 -06001535 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
Petr Krausbc7f5442017-05-14 23:43:38 +02001536 HandleToUint64(dest_set));
Tobin Ehlis300888c2016-05-18 13:43:26 -06001537 } else {
Dave Houltond8ed0212018-05-16 17:18:24 -06001538 std::string error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001539 std::string error_str;
Dave Houlton00c154e2018-05-24 13:20:50 -06001540 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001541 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 -06001542 HandleToUint64(dest_set), error_code,
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01001543 "vkUpdateDescriptorSets() failed write update validation for Descriptor Set 0x%" PRIx64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001544 " with error: %s.",
1545 HandleToUint64(dest_set), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001546 }
1547 }
1548 }
1549 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001550 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001551 auto dst_set = p_cds[i].dstSet;
1552 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001553 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1554 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001555 // Object_tracker verifies that src & dest descriptor set are valid
1556 assert(src_node);
1557 assert(dst_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001558 std::string error_code;
Tobin Ehlisa1712752017-01-04 09:41:47 -07001559 std::string error_str;
Dave Houlton00c154e2018-05-24 13:20:50 -06001560 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001561 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 -06001562 HandleToUint64(dst_set), error_code,
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01001563 "vkUpdateDescriptorSets() failed copy update from Descriptor Set 0x%" PRIx64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001564 " to Descriptor Set 0x%" PRIx64 " with error: %s.",
1565 HandleToUint64(src_set), HandleToUint64(dst_set), error_str.c_str());
Tobin Ehlis300888c2016-05-18 13:43:26 -06001566 }
1567 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001568 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001569}
1570// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1571// sets, and then calls their respective Perform[Write|Copy]Update functions.
1572// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1573// with the same set of updates.
1574// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1575// calling down the chain.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001576void cvdescriptorset::PerformUpdateDescriptorSets(const layer_data *dev_data, uint32_t write_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001577 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1578 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001579 // Write updates first
1580 uint32_t i = 0;
1581 for (i = 0; i < write_count; ++i) {
1582 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001583 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001584 if (set_node) {
1585 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001586 }
1587 }
1588 // Now copy updates
1589 for (i = 0; i < copy_count; ++i) {
1590 auto dst_set = p_cds[i].dstSet;
1591 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001592 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1593 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001594 if (src_node && dst_node) {
1595 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001596 }
1597 }
1598}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001599// This helper function carries out the state updates for descriptor updates peformed via update templates. It basically collects
1600// data and leverages the PerformUpdateDescriptor helper functions to do this.
1601void cvdescriptorset::PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet,
1602 std::unique_ptr<TEMPLATE_STATE> const &template_state,
1603 const void *pData) {
1604 auto const &create_info = template_state->create_info;
1605
1606 // Create a vector of write structs
1607 std::vector<VkWriteDescriptorSet> desc_writes;
1608 auto layout_obj = GetDescriptorSetLayout(device_data, create_info.descriptorSetLayout);
1609
1610 // Create a WriteDescriptorSet struct for each template update entry
1611 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1612 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1613 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1614 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1615
John Zulaufb6d71202017-12-22 16:47:09 -07001616 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001617 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1618 desc_writes.emplace_back();
1619 auto &write_entry = desc_writes.back();
1620
1621 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1622 char *update_entry = (char *)(pData) + offset;
1623
1624 if (dst_array_element >= binding_count) {
1625 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001626 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001627 }
1628
1629 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1630 write_entry.pNext = NULL;
1631 write_entry.dstSet = descriptorSet;
1632 write_entry.dstBinding = binding_being_updated;
1633 write_entry.dstArrayElement = dst_array_element;
1634 write_entry.descriptorCount = 1;
1635 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1636
1637 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1638 case VK_DESCRIPTOR_TYPE_SAMPLER:
1639 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1640 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1641 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1642 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1643 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1644 break;
1645
1646 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1647 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1648 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1649 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1650 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1651 break;
1652
1653 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1654 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1655 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1656 break;
1657 default:
1658 assert(0);
1659 break;
1660 }
1661 dst_array_element++;
1662 }
1663 }
1664 PerformUpdateDescriptorSets(device_data, static_cast<uint32_t>(desc_writes.size()), desc_writes.data(), 0, NULL);
1665}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001666// Validate the state for a given write update but don't actually perform the update
1667// If an error would occur for this update, return false and fill in details in error_msg string
1668bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
Dave Houlton00c154e2018-05-24 13:20:50 -06001669 std::string *error_code, std::string *error_msg) {
John Zulauf5dfd45c2018-01-17 11:06:34 -07001670 // Verify dst layout still valid
1671 if (p_layout_->IsDestroyed()) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001672 *error_code = "VUID-VkWriteDescriptorSet-dstSet-00320";
John Zulauf5dfd45c2018-01-17 11:06:34 -07001673 string_sprintf(error_msg,
1674 "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set 0x%" PRIxLEAST64
1675 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
1676 HandleToUint64(set_), HandleToUint64(p_layout_->GetDescriptorSetLayout()));
1677 return false;
1678 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001679 // Verify dst binding exists
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001680 if (!p_layout_->HasBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001681 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00315";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001682 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001683 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001684 *error_msg = error_str.str();
1685 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001686 } else {
1687 // Make sure binding isn't empty
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001688 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001689 *error_code = "VUID-VkWriteDescriptorSet-dstBinding-00316";
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001690 std::stringstream error_str;
1691 error_str << "DescriptorSet " << set_ << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
1692 *error_msg = error_str.str();
1693 return false;
1694 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001695 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05001696 // Verify idle ds
1697 if (in_use.load() &&
1698 !(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
1699 (VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
1700 // TODO : Re-using Free Idle error code, need write update idle error code
Dave Houlton00c154e2018-05-24 13:20:50 -06001701 *error_code = "VUID-vkFreeDescriptorSets-pDescriptorSets-00309";
Jeff Bolzfdf96072018-04-10 14:32:18 -05001702 std::stringstream error_str;
1703 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
1704 << " that is in use by a command buffer";
1705 *error_msg = error_str.str();
1706 return false;
1707 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001708 // We know that binding is valid, verify update and do update on each descriptor
John Zulaufc483f442017-12-15 14:02:06 -07001709 auto start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001710 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001711 if (type != update->descriptorType) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001712 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00319";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001713 std::stringstream error_str;
1714 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1715 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1716 *error_msg = error_str.str();
1717 return false;
1718 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001719 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001720 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001721 std::stringstream error_str;
1722 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001723 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001724 << " descriptors in that binding and all successive bindings of the set, but update of "
1725 << update->descriptorCount << " descriptors combined with update array element offset of "
1726 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001727 *error_msg = error_str.str();
1728 return false;
1729 }
1730 // Verify consecutive bindings match (if needed)
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001731 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001732 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07001733 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Dave Houlton00c154e2018-05-24 13:20:50 -06001734 *error_code = "VUID-VkWriteDescriptorSet-dstArrayElement-00321";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001735 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001736 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001737 // Update is within bounds and consistent so last step is to validate update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001738 if (!VerifyWriteUpdateContents(update, start_idx, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001739 std::stringstream error_str;
1740 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1741 << " failed with error message: " << error_msg->c_str();
1742 *error_msg = error_str.str();
1743 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001744 }
1745 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001746 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001747}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001748// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001749// 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 -07001750bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06001751 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001752 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001753 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001754 std::string error_usage_bit;
1755 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001756 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1757 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001758 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00334";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001759 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1760 }
1761 break;
1762 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1763 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001764 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00335";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001765 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1766 }
1767 break;
1768 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1769 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1770 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001771 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00330";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001772 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1773 }
1774 break;
1775 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1776 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1777 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001778 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00331";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001779 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1780 }
1781 break;
1782 default:
1783 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001784 }
1785 if (!error_usage_bit.empty()) {
1786 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001787 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1788 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1789 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001790 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001791 return false;
1792 }
1793 return true;
1794}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001795// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1796// 1. buffer is valid
1797// 2. buffer was created with correct usage flags
1798// 3. offset is less than buffer size
1799// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001800// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001801// 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 -06001802bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
Dave Houlton00c154e2018-05-24 13:20:50 -06001803 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001804 // First make sure that buffer is valid
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001805 auto buffer_node = GetBufferState(device_data_, buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07001806 // Any invalid buffer should already be caught by object_tracker
1807 assert(buffer_node);
Dave Houltond8ed0212018-05-16 17:18:24 -06001808 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()",
1809 "VUID-VkWriteDescriptorSet-descriptorType-00329")) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001810 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00329";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001811 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001812 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001813 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001814 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001815 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1816 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001817 return false;
1818 }
1819 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001820 if (buffer_info->offset >= buffer_node->createInfo.size) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001821 *error_code = "VUID-VkDescriptorBufferInfo-offset-00340";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001822 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001823 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
1824 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001825 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001826 return false;
1827 }
1828 if (buffer_info->range != VK_WHOLE_SIZE) {
1829 // Range must be VK_WHOLE_SIZE or > 0
1830 if (!buffer_info->range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001831 *error_code = "VUID-VkDescriptorBufferInfo-range-00341";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001832 std::stringstream error_str;
1833 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001834 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001835 return false;
1836 }
1837 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1838 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001839 *error_code = "VUID-VkDescriptorBufferInfo-range-00342";
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001840 std::stringstream error_str;
1841 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1842 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001843 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001844 return false;
1845 }
1846 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001847 // Check buffer update sizes against device limits
1848 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1849 auto max_ub_range = limits_.maxUniformBufferRange;
1850 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1851 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001852 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00332";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001853 std::stringstream error_str;
1854 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1855 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
1856 *error_msg = error_str.str();
1857 return false;
1858 }
1859 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1860 auto max_sb_range = limits_.maxStorageBufferRange;
1861 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1862 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001863 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00333";
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001864 std::stringstream error_str;
1865 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1866 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
1867 *error_msg = error_str.str();
1868 return false;
1869 }
1870 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001871 return true;
1872}
1873
Tobin Ehlis300888c2016-05-18 13:43:26 -06001874// Verify that the contents of the update are ok, but don't perform actual update
1875bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
Dave Houlton00c154e2018-05-24 13:20:50 -06001876 std::string *error_code, std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001877 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001878 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1879 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1880 // Validate image
1881 auto image_view = update->pImageInfo[di].imageView;
1882 auto image_layout = update->pImageInfo[di].imageLayout;
1883 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001884 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001885 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
1886 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001887 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001888 return false;
1889 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001890 }
1891 }
Tobin Ehlis648b1cf2018-04-13 15:24:13 -06001892 // fall through
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001893 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1894 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1895 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
1896 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001897 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001898 std::stringstream error_str;
1899 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1900 << update->pImageInfo[di].sampler << ".";
1901 *error_msg = error_str.str();
1902 return false;
1903 }
1904 } else {
1905 // TODO : Warn here
1906 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001907 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001908 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001909 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001910 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1911 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1912 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1913 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1914 auto image_view = update->pImageInfo[di].imageView;
1915 auto image_layout = update->pImageInfo[di].imageLayout;
1916 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
1917 std::stringstream error_str;
1918 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
1919 *error_msg = error_str.str();
1920 return false;
1921 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001922 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001923 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001924 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001925 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1926 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1927 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1928 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001929 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001930 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001931 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001932 std::stringstream error_str;
1933 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1934 *error_msg = error_str.str();
1935 return false;
1936 }
1937 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06001938 auto buffer_state = GetBufferState(device_data_, buffer);
1939 // Verify that buffer underlying the view hasn't been destroyed prematurely
1940 if (!buffer_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001941 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06001942 std::stringstream error_str;
1943 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
1944 << ") has been destroyed: " << error_msg->c_str();
1945 *error_msg = error_str.str();
1946 return false;
1947 } else if (!ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001948 std::stringstream error_str;
1949 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
1950 *error_msg = error_str.str();
1951 return false;
1952 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001953 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001954 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001955 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001956 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1957 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1958 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1959 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1960 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1961 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error_code, error_msg)) {
1962 std::stringstream error_str;
1963 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
1964 *error_msg = error_str.str();
1965 return false;
1966 }
1967 }
1968 break;
1969 }
1970 default:
1971 assert(0); // We've already verified update type so should never get here
1972 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001973 }
1974 // All checks passed so update contents are good
1975 return true;
1976}
1977// Verify that the contents of the update are ok, but don't perform actual update
1978bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Dave Houlton00c154e2018-05-24 13:20:50 -06001979 VkDescriptorType type, uint32_t index, std::string *error_code,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001980 std::string *error_msg) const {
1981 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
1982 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06001983 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001984 case PlainSampler: {
1985 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001986 const auto src_desc = src_set->descriptors_[index + di].get();
1987 if (!src_desc->updated) continue;
1988 if (!src_desc->IsImmutableSampler()) {
1989 auto update_sampler = static_cast<SamplerDescriptor *>(src_desc)->GetSampler();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001990 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06001991 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001992 std::stringstream error_str;
1993 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1994 *error_msg = error_str.str();
1995 return false;
1996 }
1997 } else {
1998 // TODO : Warn here
1999 }
2000 }
2001 break;
2002 }
2003 case ImageSampler: {
2004 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002005 const auto src_desc = src_set->descriptors_[index + di].get();
2006 if (!src_desc->updated) continue;
2007 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002008 // First validate sampler
2009 if (!img_samp_desc->IsImmutableSampler()) {
2010 auto update_sampler = img_samp_desc->GetSampler();
2011 if (!ValidateSampler(update_sampler, device_data_)) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002012 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00325";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002013 std::stringstream error_str;
2014 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
2015 *error_msg = error_str.str();
2016 return false;
2017 }
2018 } else {
2019 // TODO : Warn here
2020 }
2021 // Validate image
2022 auto image_view = img_samp_desc->GetImageView();
2023 auto image_layout = img_samp_desc->GetImageLayout();
2024 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002025 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002026 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002027 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002028 return false;
2029 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002030 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002031 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002032 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002033 case Image: {
2034 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002035 const auto src_desc = src_set->descriptors_[index + di].get();
2036 if (!src_desc->updated) continue;
2037 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002038 auto image_view = img_desc->GetImageView();
2039 auto image_layout = img_desc->GetImageLayout();
2040 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06002041 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002042 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06002043 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06002044 return false;
2045 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002046 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002047 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002048 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002049 case TexelBuffer: {
2050 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002051 const auto src_desc = src_set->descriptors_[index + di].get();
2052 if (!src_desc->updated) continue;
2053 auto buffer_view = static_cast<TexelDescriptor *>(src_desc)->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002054 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002055 if (!bv_state) {
Dave Houlton00c154e2018-05-24 13:20:50 -06002056 *error_code = "VUID-VkWriteDescriptorSet-descriptorType-00323";
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002057 std::stringstream error_str;
2058 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
2059 *error_msg = error_str.str();
2060 return false;
2061 }
2062 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002063 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002064 std::stringstream error_str;
2065 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
2066 *error_msg = error_str.str();
2067 return false;
2068 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06002069 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002070 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002071 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002072 case GeneralBuffer: {
2073 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02002074 const auto src_desc = src_set->descriptors_[index + di].get();
2075 if (!src_desc->updated) continue;
2076 auto buffer = static_cast<BufferDescriptor *>(src_desc)->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002077 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002078 std::stringstream error_str;
2079 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
2080 *error_msg = error_str.str();
2081 return false;
2082 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06002083 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002084 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002085 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07002086 default:
2087 assert(0); // We've already verified update type so should never get here
2088 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06002089 }
2090 // All checks passed so update contents are good
2091 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12002092}
Tobin Ehlisf320b192017-03-14 11:22:50 -06002093// Update the common AllocateDescriptorSetsData
2094void cvdescriptorset::UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
2095 AllocateDescriptorSetsData *ds_data) {
2096 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
2097 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
2098 if (layout) {
2099 ds_data->layout_nodes[i] = layout;
2100 // Count total descriptors required per type
2101 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
2102 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
2103 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
2104 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
2105 }
2106 }
2107 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
2108 }
Petr Kraus13c98a62017-12-09 00:22:39 +01002109}
Tobin Ehlisee471462016-05-26 11:21:59 -06002110// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlisf320b192017-03-14 11:22:50 -06002111bool cvdescriptorset::ValidateAllocateDescriptorSets(const core_validation::layer_data *dev_data,
2112 const VkDescriptorSetAllocateInfo *p_alloc_info,
2113 const AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002114 bool skip = false;
Tobin Ehlisf320b192017-03-14 11:22:50 -06002115 auto report_data = core_validation::GetReportData(dev_data);
Jeff Bolzfdf96072018-04-10 14:32:18 -05002116 auto pool_state = GetDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlisee471462016-05-26 11:21:59 -06002117
2118 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07002119 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
John Zulauf5562d062018-01-24 11:54:05 -07002120 if (layout) { // nullptr layout indicates no valid layout handle for this device, validated/logged in object_tracker
2121 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR) {
2122 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 -06002123 HandleToUint64(p_alloc_info->pSetLayouts[i]), "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-00308",
John Zulauf5562d062018-01-24 11:54:05 -07002124 "Layout 0x%" PRIxLEAST64 " specified at pSetLayouts[%" PRIu32
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002125 "] in vkAllocateDescriptorSets() was created with invalid flag %s set.",
John Zulauf5562d062018-01-24 11:54:05 -07002126 HandleToUint64(p_alloc_info->pSetLayouts[i]), i,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002127 "VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR");
John Zulauf5562d062018-01-24 11:54:05 -07002128 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002129 if (layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT &&
2130 !(pool_state->createInfo.flags & VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT)) {
2131 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 -06002132 0, "VUID-VkDescriptorSetAllocateInfo-pSetLayouts-03044",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002133 "Descriptor set layout create flags and pool create flags mismatch for index (%d)", i);
2134 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002135 }
2136 }
Mark Lobodzinski28426ae2017-06-01 07:56:38 -06002137 if (!GetDeviceExtensions(dev_data)->vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002138 // Track number of descriptorSets allowable in this pool
2139 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002140 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 -06002141 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorSetCount-00306",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002142 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002143 ". This pool only has %d descriptorSets remaining.",
2144 p_alloc_info->descriptorSetCount, HandleToUint64(pool_state->pool), pool_state->availableSets);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002145 }
2146 // Determine whether descriptor counts are satisfiable
2147 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
2148 if (ds_data->required_descriptors_by_type[i] > pool_state->availableDescriptorTypeCount[i]) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002149 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 -06002150 HandleToUint64(pool_state->pool), "VUID-VkDescriptorSetAllocateInfo-descriptorPool-00307",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002151 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002152 ". This pool only has %d descriptors of this type remaining.",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002153 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06002154 HandleToUint64(pool_state->pool), pool_state->availableDescriptorTypeCount[i]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002155 }
Tobin Ehlisee471462016-05-26 11:21:59 -06002156 }
2157 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06002158
Jeff Bolzfdf96072018-04-10 14:32:18 -05002159 const auto *count_allocate_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2160
2161 if (count_allocate_info) {
2162 if (count_allocate_info->descriptorSetCount != 0 &&
2163 count_allocate_info->descriptorSetCount != p_alloc_info->descriptorSetCount) {
2164 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 -06002165 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-descriptorSetCount-03045",
Jeff Bolzfdf96072018-04-10 14:32:18 -05002166 "VkDescriptorSetAllocateInfo::descriptorSetCount (%d) != "
2167 "VkDescriptorSetVariableDescriptorCountAllocateInfoEXT::descriptorSetCount (%d)",
2168 p_alloc_info->descriptorSetCount, count_allocate_info->descriptorSetCount);
2169 }
2170 if (count_allocate_info->descriptorSetCount == p_alloc_info->descriptorSetCount) {
2171 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
2172 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
2173 if (count_allocate_info->pDescriptorCounts[i] > layout->GetDescriptorCountFromBinding(layout->GetMaxBinding())) {
2174 skip |= log_msg(
2175 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 -06002176 "VUID-VkDescriptorSetVariableDescriptorCountAllocateInfoEXT-pSetLayouts-03046",
2177 "pDescriptorCounts[%d] = (%d), binding's descriptorCount = (%d)", i,
Jeff Bolzfdf96072018-04-10 14:32:18 -05002178 count_allocate_info->pDescriptorCounts[i], layout->GetDescriptorCountFromBinding(layout->GetMaxBinding()));
2179 }
2180 }
2181 }
2182 }
2183
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06002184 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06002185}
2186// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06002187void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
2188 const VkDescriptorSet *descriptor_sets,
2189 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06002190 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06002191 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
John Zulauf48a6a702017-12-22 17:14:54 -07002192 layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06002193 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002194 // Account for sets and individual descriptors allocated from pool
Tobin Ehlisee471462016-05-26 11:21:59 -06002195 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06002196 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
2197 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
2198 }
Jeff Bolzfdf96072018-04-10 14:32:18 -05002199
2200 const auto *variable_count_info = lvl_find_in_chain<VkDescriptorSetVariableDescriptorCountAllocateInfoEXT>(p_alloc_info->pNext);
2201 bool variable_count_valid = variable_count_info && variable_count_info->descriptorSetCount == p_alloc_info->descriptorSetCount;
2202
Mark Lobodzinskic9430182017-06-13 13:00:05 -06002203 // Create tracking object for each descriptor set; insert into global map and the pool's set.
Tobin Ehlisee471462016-05-26 11:21:59 -06002204 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Jeff Bolzfdf96072018-04-10 14:32:18 -05002205 uint32_t variable_count = variable_count_valid ? variable_count_info->pDescriptorCounts[i] : 0;
2206
Tobin Ehlis93f22372016-10-12 14:34:12 -06002207 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 -05002208 variable_count, dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06002209
2210 pool_state->sets.insert(new_ds);
2211 new_ds->in_use.store(0);
2212 (*set_map)[descriptor_sets[i]] = new_ds;
2213 }
2214}
John Zulauf48a6a702017-12-22 17:14:54 -07002215
2216cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2217 GLOBAL_CB_NODE *cb_state)
2218 : filtered_map_(), orig_map_(in_map) {
2219 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2220 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2221 ds.FilterAndTrackBindingReqs(cb_state, orig_map_, filtered_map_.get());
2222 }
2223}
2224cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
2225 GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline)
2226 : filtered_map_(), orig_map_(in_map) {
2227 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
2228 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
2229 ds.FilterAndTrackBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
2230 }
Artem Kharytoniuk2456f992018-01-12 14:17:41 +01002231}