blob: 4b748381647454f9b45264470b1820fd4452ef52 [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>
19 */
20
Tobin Ehlisf922ef82016-11-30 10:19:14 -070021// Allow use of STL min and max functions in Windows
22#define NOMINMAX
23
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060024#include "descriptor_sets.h"
25#include "vk_enum_string_helper.h"
26#include "vk_safe_struct.h"
27#include <sstream>
Mark Lobodzinski2eee5d82016-12-02 15:33:18 -070028#include <algorithm>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060029
30// Construct DescriptorSetLayout instance from given create info
Tobin Ehlis154c2692016-10-25 09:36:53 -060031cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060032 const VkDescriptorSetLayout layout)
33 : layout_(layout), binding_count_(p_create_info->bindingCount), descriptor_count_(0), dynamic_descriptor_count_(0) {
Tobin Ehlisa3525e02016-11-17 10:50:52 -070034 // Dyn array indicies are ordered by binding # and array index of any array within the binding
35 // so we store up bindings w/ count in ordered map in order to create dyn array mappings below
36 std::map<uint32_t, uint32_t> binding_to_dyn_count;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060037 for (uint32_t i = 0; i < binding_count_; ++i) {
Tobin Ehlis9637fb22016-12-12 15:59:34 -070038 auto binding_num = p_create_info->pBindings[i].binding;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060039 descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
Tobin Ehlis9637fb22016-12-12 15:59:34 -070040 uint32_t insert_index = 0; // Track vector index where we insert element
41 if (bindings_.empty() || binding_num > bindings_.back().binding) {
42 bindings_.push_back(safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i]));
43 insert_index = bindings_.size() - 1;
44 } else { // out-of-order binding number, need to insert into vector in-order
45 auto it = bindings_.begin();
46 // Find currently binding's spot in vector
47 while (binding_num > it->binding) {
48 assert(it != bindings_.end());
49 ++insert_index;
50 ++it;
51 }
52 bindings_.insert(it, safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i]));
53 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060054 // In cases where we should ignore pImmutableSamplers make sure it's NULL
55 if ((p_create_info->pBindings[i].pImmutableSamplers) &&
56 ((p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_SAMPLER) &&
57 (p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER))) {
Tobin Ehlis9637fb22016-12-12 15:59:34 -070058 bindings_[insert_index].pImmutableSamplers = nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060059 }
60 if (p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
61 p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
Tobin Ehlisa3525e02016-11-17 10:50:52 -070062 binding_to_dyn_count[p_create_info->pBindings[i].binding] = p_create_info->pBindings[i].descriptorCount;
Tobin Ehlisef0de162016-06-20 13:07:34 -060063 dynamic_descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060064 }
65 }
Tobin Ehlis9637fb22016-12-12 15:59:34 -070066 assert(bindings_.size() == binding_count_);
67 uint32_t global_index = 0;
68 // Vector order is finalized so create maps of bindings to indices
69 for (uint32_t i = 0; i < binding_count_; ++i) {
70 auto binding_num = bindings_[i].binding;
71 binding_to_index_map_[binding_num] = i;
72 binding_to_global_start_index_map_[binding_num] = global_index;
73 global_index += bindings_[i].descriptorCount ? bindings_[i].descriptorCount - 1 : 0;
74 binding_to_global_end_index_map_[binding_num] = global_index;
75 global_index += bindings_[i].descriptorCount ? 1 : 0;
76 }
Tobin Ehlisa3525e02016-11-17 10:50:52 -070077 // Now create dyn offset array mapping for any dynamic descriptors
78 uint32_t dyn_array_idx = 0;
79 for (const auto &bc_pair : binding_to_dyn_count) {
80 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
81 dyn_array_idx += bc_pair.second;
82 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060083}
Tobin Ehlis154c2692016-10-25 09:36:53 -060084
85// Validate descriptor set layout create info
86bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(debug_report_data *report_data,
87 const VkDescriptorSetLayoutCreateInfo *create_info) {
88 bool skip = false;
89 std::unordered_set<uint32_t> bindings;
90 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
Tobin Ehlisfdcb63f2016-10-25 20:56:47 -060091 if (!bindings.insert(create_info->pBindings[i].binding).second) {
Tobin Ehlis154c2692016-10-25 09:36:53 -060092 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
93 VALIDATION_ERROR_02345, "DS", "duplicated binding number in VkDescriptorSetLayoutBinding. %s",
94 validation_error_map[VALIDATION_ERROR_02345]);
95 }
Tobin Ehlis154c2692016-10-25 09:36:53 -060096 }
97 return skip;
98}
99
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700100// Return the number of descriptors for the given binding and all successive bindings
101uint32_t cvdescriptorset::DescriptorSetLayout::GetConsecutiveDescriptorCountFromBinding(uint32_t binding) const {
102 // If binding is invalid we'll return 0
103 uint32_t binding_count = 0;
104 auto bi_itr = binding_to_index_map_.find(binding);
105 while (bi_itr != binding_to_index_map_.end()) {
106 binding_count += bindings_[bi_itr->second].descriptorCount;
107 bi_itr++;
108 }
109 return binding_count;
110}
111
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600112// put all bindings into the given set
113void cvdescriptorset::DescriptorSetLayout::FillBindingSet(std::unordered_set<uint32_t> *binding_set) const {
114 for (auto binding_index_pair : binding_to_index_map_)
115 binding_set->insert(binding_index_pair.first);
116}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600117
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600118VkDescriptorSetLayoutBinding const *
119cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600120 const auto &bi_itr = binding_to_index_map_.find(binding);
121 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600122 return bindings_[bi_itr->second].ptr();
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600123 }
124 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600125}
126VkDescriptorSetLayoutBinding const *
127cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const {
128 if (index >= bindings_.size())
129 return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600130 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600131}
132// Return descriptorCount for given binding, 0 if index is unavailable
133uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600134 const auto &bi_itr = binding_to_index_map_.find(binding);
135 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600136 return bindings_[bi_itr->second].descriptorCount;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600137 }
138 return 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600139}
140// Return descriptorCount for given index, 0 if index is unavailable
141uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
142 if (index >= bindings_.size())
143 return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600144 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600145}
146// For the given binding, return descriptorType
147VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const {
148 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600149 const auto &bi_itr = binding_to_index_map_.find(binding);
150 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600151 return bindings_[bi_itr->second].descriptorType;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600152 }
153 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600154}
155// For the given index, return descriptorType
156VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
157 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600158 return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600159}
160// For the given global index, return descriptorType
161// Currently just counting up through bindings_, may improve this in future
162VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const {
163 uint32_t global_offset = 0;
164 for (auto binding : bindings_) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600165 global_offset += binding.descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600166 if (index < global_offset)
Tobin Ehlis664e6012016-05-05 11:04:44 -0600167 return binding.descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600168 }
169 assert(0); // requested global index is out of bounds
170 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
171}
172// For the given binding, return stageFlags
173VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const {
174 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600175 const auto &bi_itr = binding_to_index_map_.find(binding);
176 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600177 return bindings_[bi_itr->second].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600178 }
179 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600180}
181// For the given binding, return start index
182uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const {
183 assert(binding_to_global_start_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600184 const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding);
185 if (btgsi_itr != binding_to_global_start_index_map_.end()) {
186 return btgsi_itr->second;
187 }
188 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600189 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600190 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600191}
192// For the given binding, return end index
193uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
194 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600195 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
196 if (btgei_itr != binding_to_global_end_index_map_.end()) {
197 return btgei_itr->second;
198 }
199 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600200 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600201 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600202}
203// For given binding, return ptr to ImmutableSampler array
204VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
205 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600206 const auto &bi_itr = binding_to_index_map_.find(binding);
207 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600208 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600209 }
210 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600211}
212// For given index, return ptr to ImmutableSampler array
213VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
214 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600215 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600216}
217// If our layout is compatible with rh_ds_layout, return true,
218// else return false and fill in error_msg will description of what causes incompatibility
219bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
220 // Trivial case
221 if (layout_ == rh_ds_layout->GetDescriptorSetLayout())
222 return true;
223 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
224 std::stringstream error_str;
225 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
226 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
227 *error_msg = error_str.str();
228 return false; // trivial fail case
229 }
230 // Descriptor counts match so need to go through bindings one-by-one
231 // and verify that type and stageFlags match
232 for (auto binding : bindings_) {
233 // TODO : Do we also need to check immutable samplers?
234 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600235 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600236 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600237 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
238 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600239 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600240 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600241 *error_msg = error_str.str();
242 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600243 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600244 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600245 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
246 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600247 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600248 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600249 *error_msg = error_str.str();
250 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600251 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600252 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600253 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
254 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600255 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600256 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600257 *error_msg = error_str.str();
258 return false;
259 }
260 }
261 return true;
262}
263
264bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
265 if (!binding_to_index_map_.count(binding + 1))
266 return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600267 auto const &bi_itr = binding_to_index_map_.find(binding);
268 if (bi_itr != binding_to_index_map_.end()) {
269 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
270 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600271 auto type = bindings_[bi_itr->second].descriptorType;
272 auto stage_flags = bindings_[bi_itr->second].stageFlags;
273 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
274 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
275 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
276 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600277 return false;
278 }
279 return true;
280 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600281 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600282 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600283}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600284// Starting at offset descriptor of given binding, parse over update_count
285// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
286// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
287// If so, return true. If not, fill in error_msg and return false
288bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
289 const char *type, const VkDescriptorSet set,
290 std::string *error_msg) const {
291 // Verify consecutive bindings match (if needed)
292 auto orig_binding = current_binding;
293 // Track count of descriptors in the current_bindings that are remaining to be updated
294 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
295 // First, it's legal to offset beyond your own binding so handle that case
296 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
297 while (offset >= binding_remaining) {
298 // Advance to next binding, decrement offset by binding size
299 offset -= binding_remaining;
300 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
301 }
302 binding_remaining -= offset;
303 while (update_count > binding_remaining) { // While our updates overstep current binding
304 // Verify next consecutive binding matches type, stage flags & immutable sampler use
305 if (!IsNextBindingConsistent(current_binding++)) {
306 std::stringstream error_str;
307 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
308 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
309 "next binding is not consistent with current binding so this update is invalid.";
310 *error_msg = error_str.str();
311 return false;
312 }
313 // For sake of this check consider the bindings updated and grab count for next binding
314 update_count -= binding_remaining;
315 binding_remaining = GetDescriptorCountFromBinding(current_binding);
316 }
317 return true;
318}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600319
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600320cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
321 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
322
Tobin Ehlis93f22372016-10-12 14:34:12 -0600323cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
324 const DescriptorSetLayout *layout, const core_validation::layer_data *dev_data)
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600325 : some_update_(false), set_(set), pool_state_(nullptr), p_layout_(layout), device_data_(dev_data) {
326 pool_state_ = getDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600327 // Foreach binding, create default descriptors of given type
328 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
329 auto type = p_layout_->GetTypeFromIndex(i);
330 switch (type) {
331 case VK_DESCRIPTOR_TYPE_SAMPLER: {
332 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
333 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
334 if (immut_sampler)
Chris Forbescb621ea2016-05-30 11:47:31 +1200335 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600336 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200337 descriptors_.emplace_back(new SamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600338 }
339 break;
340 }
341 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
342 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
343 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
344 if (immut)
Chris Forbescb621ea2016-05-30 11:47:31 +1200345 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600346 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200347 descriptors_.emplace_back(new ImageSamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600348 }
349 break;
350 }
351 // ImageDescriptors
352 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
353 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
354 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
355 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200356 descriptors_.emplace_back(new ImageDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600357 break;
358 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
359 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
360 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200361 descriptors_.emplace_back(new TexelDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600362 break;
363 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
364 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
365 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
366 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
367 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200368 descriptors_.emplace_back(new BufferDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600369 break;
370 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600371 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600372 break;
373 }
374 }
375}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600376
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600377cvdescriptorset::DescriptorSet::~DescriptorSet() {
378 InvalidateBoundCmdBuffers();
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600379}
Chris Forbes57989132016-07-26 17:06:10 +1200380
381
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700382static std::string string_descriptor_req_view_type(descriptor_req req) {
383 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200384 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
385 if (req & (1 << i)) {
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700386 if (result.size()) result += ", ";
387 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200388 }
389 }
390
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700391 if (!result.size())
392 result = "(none)";
393
394 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200395}
396
397
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600398// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
399bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
400 return layout->IsCompatible(p_layout_, error);
401}
Chris Forbes57989132016-07-26 17:06:10 +1200402
Tobin Ehlis3066db62016-08-22 08:12:23 -0600403// 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 -0600404// This includes validating that all descriptors in the given bindings are updated,
405// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
406// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600407bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600408 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200409 for (auto binding_pair : bindings) {
410 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600411 if (!p_layout_->HasBinding(binding)) {
412 std::stringstream error_str;
413 error_str << "Attempting to validate DrawState for binding #" << binding
414 << " which is an invalid binding for this descriptor set.";
415 *error = error_str.str();
416 return false;
417 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600418 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600419 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600420 // Nothing to do for strictly immutable sampler
421 } else {
422 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700423 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
424 for (uint32_t i = start_idx; i <= end_idx; ++i, ++array_idx) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600425 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600426 std::stringstream error_str;
427 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
428 << " is being used in draw but has not been updated.";
429 *error = error_str.str();
430 return false;
431 } else {
Chris Forbes57989132016-07-26 17:06:10 +1200432 auto descriptor_class = descriptors_[i]->GetClass();
433 if (descriptor_class == GeneralBuffer) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600434 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600435 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis4668dce2016-11-16 09:30:23 -0700436 auto buffer_node = getBufferState(device_data_, buffer);
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600437 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600438 std::stringstream error_str;
439 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
440 << " references invalid buffer " << buffer << ".";
441 *error = error_str.str();
442 return false;
443 } else {
Tobin Ehlis640a81c2016-11-15 15:37:18 -0700444 for (auto mem_binding : buffer_node->GetBoundMemory()) {
445 if (!getMemObjInfo(device_data_, mem_binding)) {
446 std::stringstream error_str;
447 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
448 << " uses buffer " << buffer << " that references invalid memory " << mem_binding
449 << ".";
450 *error = error_str.str();
451 return false;
452 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600453 }
454 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600455 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600456 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600457 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600458 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
459 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700460 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600461 if (VK_WHOLE_SIZE == range) {
462 if ((dyn_offset + desc_offset) > buffer_size) {
463 std::stringstream error_str;
464 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
465 << " uses buffer " << buffer
466 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
467 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
468 << buffer_size << ".";
469 *error = error_str.str();
470 return false;
471 }
472 } else {
473 if ((dyn_offset + desc_offset + range) > buffer_size) {
474 std::stringstream error_str;
475 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
476 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
477 << " combined with offset " << desc_offset << " and range " << range
478 << " that oversteps the buffer size of " << buffer_size << ".";
479 *error = error_str.str();
480 return false;
481 }
482 }
483 }
484 }
Chris Forbes57989132016-07-26 17:06:10 +1200485 else if (descriptor_class == ImageSampler || descriptor_class == Image) {
486 auto image_view = (descriptor_class == ImageSampler)
487 ? static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView()
488 : static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
489 auto reqs = binding_pair.second;
490
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600491 auto image_view_state = getImageViewState(device_data_, image_view);
492 assert(image_view_state);
493 auto image_view_ci = image_view_state->create_info;
Chris Forbes57989132016-07-26 17:06:10 +1200494
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600495 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
Chris Forbes57989132016-07-26 17:06:10 +1200496 // bad view type
497 std::stringstream error_str;
498 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
499 << " requires an image view of type " << string_descriptor_req_view_type(reqs)
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600500 << " but got " << string_VkImageViewType(image_view_ci.viewType) << ".";
Chris Forbes57989132016-07-26 17:06:10 +1200501 *error = error_str.str();
502 return false;
503 }
504
Tobin Ehlis30df15c2016-10-12 17:17:57 -0600505 auto image_node = getImageState(device_data_, image_view_ci.image);
Chris Forbes57989132016-07-26 17:06:10 +1200506 assert(image_node);
507
508 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) &&
509 image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
510 std::stringstream error_str;
511 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
512 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
513 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
514 *error = error_str.str();
515 return false;
516 }
517
518 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) &&
519 image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
520 std::stringstream error_str;
521 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
522 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
523 *error = error_str.str();
524 return false;
525 }
526 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600527 }
528 }
529 }
530 }
531 return true;
532}
Chris Forbes57989132016-07-26 17:06:10 +1200533
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600534// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600535uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600536 std::unordered_set<VkBuffer> *buffer_set,
537 std::unordered_set<VkImageView> *image_set) const {
538 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200539 for (auto binding_pair : bindings) {
540 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600541 // If a binding doesn't exist, skip it
542 if (!p_layout_->HasBinding(binding)) {
543 continue;
544 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600545 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600546 if (descriptors_[start_idx]->IsStorage()) {
547 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600548 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600549 if (descriptors_[start_idx + i]->updated) {
550 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600551 num_updates++;
552 }
553 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600554 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600555 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600556 if (descriptors_[start_idx + i]->updated) {
557 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis8b872462016-09-14 08:12:08 -0600558 auto bv_state = getBufferViewState(device_data_, bufferview);
559 if (bv_state) {
560 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600561 num_updates++;
562 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600563 }
564 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600565 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600566 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600567 if (descriptors_[start_idx + i]->updated) {
568 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600569 num_updates++;
570 }
571 }
572 }
573 }
574 }
575 return num_updates;
576}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600577// Set is being deleted or updates so invalidate all bound cmd buffers
578void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Tobin Ehlisfe5731a2016-11-21 08:31:01 -0700579 core_validation::invalidateCommandBuffers(device_data_, cb_bindings,
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600580 {reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600581}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600582// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600583void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700584 // Perform update on a per-binding basis as consecutive updates roll over to next binding
585 auto descriptors_remaining = update->descriptorCount;
586 auto binding_being_updated = update->dstBinding;
587 auto offset = update->dstArrayElement;
588 while (descriptors_remaining) {
589 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
590 auto global_idx = p_layout_->GetGlobalStartIndexFromBinding(binding_being_updated) + offset;
591 // Loop over the updates for a single binding at a time
592 for (uint32_t di = 0; di < update_count; ++di) {
593 descriptors_[global_idx + di]->WriteUpdate(update, di);
594 }
595 // Roll over to next binding in case of consecutive update
596 descriptors_remaining -= update_count;
597 offset = 0;
598 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600599 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600600 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600601 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600602
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600603 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600604}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600605// Validate Copy update
606bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600607 const DescriptorSet *src_set, UNIQUE_VALIDATION_ERROR_CODE *error_code,
608 std::string *error_msg) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600609 // Verify idle ds
610 if (in_use.load()) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600611 // TODO : Re-using Allocate Idle error code, need copy update idle error code
612 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600613 std::stringstream error_str;
614 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700615 << " that is in use by a command buffer";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600616 *error_msg = error_str.str();
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600617 return false;
618 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600619 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600620 *error_code = VALIDATION_ERROR_00966;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600621 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700622 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600623 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600624 return false;
625 }
626 if (!src_set->HasBinding(update->srcBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600627 *error_code = VALIDATION_ERROR_00964;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600628 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700629 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600630 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600631 return false;
632 }
633 // src & dst set bindings are valid
634 // Check bounds of src & dst
635 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
636 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
637 // SRC update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600638 *error_code = VALIDATION_ERROR_00965;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600639 std::stringstream error_str;
640 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
641 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
642 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700643 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600644 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600645 return false;
646 }
647 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
648 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
649 // DST update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600650 *error_code = VALIDATION_ERROR_00967;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600651 std::stringstream error_str;
652 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
653 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
654 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700655 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600656 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600657 return false;
658 }
659 // Check that types match
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600660 // TODO : Base default error case going from here is VALIDATION_ERROR_00968 which covers all consistency issues, need more
661 // fine-grained error codes
662 *error_code = VALIDATION_ERROR_00968;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600663 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
664 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
665 if (src_type != dst_type) {
666 std::stringstream error_str;
667 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
668 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700669 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600670 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600671 return false;
672 }
673 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600674 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600675 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600676 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600677 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600678 return false;
679 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600680 // First make sure source descriptors are updated
681 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
682 if (!src_set->descriptors_[src_start_idx + i]) {
683 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700684 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding
685 << " but descriptor at array offset " << update->srcArrayElement + i << " has not been updated";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600686 *error_msg = error_str.str();
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600687 return false;
688 }
689 }
690 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600691 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error_code, error_msg))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600692 return false;
693
694 // All checks passed so update is good
695 return true;
696}
697// Perform Copy update
698void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600699 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
700 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600701 // Update parameters all look good so perform update
702 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600703 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600704 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600705 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600706 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600707
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600708 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600709}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600710
Tobin Ehlisf9519102016-08-17 09:49:13 -0600711// Bind cb_node to this set and this set to cb_node.
712// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
713// to be used in a draw by the given cb_node
Tobin Ehlisf9519102016-08-17 09:49:13 -0600714void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node, const std::unordered_set<uint32_t> &bindings) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600715 // bind cb to this descriptor set
716 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600717 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600718 cb_node->object_bindings.insert({reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600719 pool_state_->cb_bindings.insert(cb_node);
720 cb_node->object_bindings.insert(
721 {reinterpret_cast<uint64_t &>(pool_state_->pool), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT});
Tobin Ehlisf9519102016-08-17 09:49:13 -0600722 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
723 // resources
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600724 for (auto binding : bindings) {
725 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
726 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
727 for (uint32_t i = start_idx; i <= end_idx; ++i) {
728 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
729 }
730 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600731}
732
Tobin Ehlis300888c2016-05-18 13:43:26 -0600733cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600734 updated = false;
735 descriptor_class = PlainSampler;
736};
737
Tobin Ehlis300888c2016-05-18 13:43:26 -0600738cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600739 updated = false;
740 descriptor_class = PlainSampler;
741 if (immut) {
742 sampler_ = *immut;
743 immutable_ = true;
744 updated = true;
745 }
746}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600747// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
748bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
Tobin Ehlisfad7adf2016-10-20 06:50:37 -0600749 return (getSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600750}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600751
Tobin Ehlis554bf382016-05-24 11:14:43 -0600752bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600753 const core_validation::layer_data *dev_data, UNIQUE_VALIDATION_ERROR_CODE *error_code,
754 std::string *error_msg) {
755 // TODO : Defaulting to 00943 for all cases here. Need to create new error codes for various cases.
756 *error_code = VALIDATION_ERROR_00943;
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600757 auto iv_state = getImageViewState(dev_data, image_view);
758 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600759 std::stringstream error_str;
760 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600761 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600762 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600763 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600764 // 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 -0600765 // Validate that imageLayout is compatible with aspect_mask and image format
766 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600767 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
768 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600769 VkFormat format = VK_FORMAT_MAX_ENUM;
770 VkImageUsageFlags usage = 0;
Tobin Ehlis30df15c2016-10-12 17:17:57 -0600771 auto image_node = getImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600772 if (image_node) {
773 format = image_node->createInfo.format;
774 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600775 // Validate that memory is bound to image
Tobin Ehlisfed999f2016-09-21 15:09:45 -0600776 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()")) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600777 // TODO : Need new code(s) for language in 11.6 Memory Association
778 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600779 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -0600780 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600781 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600782 // Also need to check the swapchains.
Tobin Ehlis969a5262016-06-02 12:13:32 -0600783 auto swapchain = getSwapchainFromImage(dev_data, image);
784 if (swapchain) {
Tobin Ehlis4e380592016-06-02 12:41:47 -0600785 auto swapchain_node = getSwapchainNode(dev_data, swapchain);
786 if (swapchain_node) {
787 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600788 }
789 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600790 }
791 // First validate that format and layout are compatible
792 if (format == VK_FORMAT_MAX_ENUM) {
793 std::stringstream error_str;
794 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600795 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600796 return false;
797 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600798 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
799 // vkCreateImageView(). What's the best way to create unique id for these cases?
Tobin Ehlis1809f912016-05-25 09:24:36 -0600800 bool ds = vk_format_is_depth_or_stencil(format);
801 switch (image_layout) {
802 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
803 // Only Color bit must be set
804 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600805 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600806 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
807 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600808 *error_msg = error_str.str();
Tobin Ehlis554bf382016-05-24 11:14:43 -0600809 return false;
810 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600811 // format must NOT be DS
812 if (ds) {
813 std::stringstream error_str;
814 error_str << "ImageView (" << image_view
815 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
816 << string_VkFormat(format) << " which is not a color format.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600817 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600818 return false;
819 }
820 break;
821 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
822 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
823 // Depth or stencil bit must be set, but both must NOT be set
824 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
825 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
826 // both must NOT be set
827 std::stringstream error_str;
828 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600829 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600830 return false;
831 }
832 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
833 // Neither were set
834 std::stringstream error_str;
835 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
836 << " but does not have STENCIL or DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600837 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600838 return false;
839 }
840 // format must be DS
841 if (!ds) {
842 std::stringstream error_str;
843 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
844 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600845 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600846 return false;
847 }
848 break;
849 default:
Mike Weiblencce7ec72016-10-17 19:33:05 -0600850 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600851 if (ds) {
852 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
853 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
854 // both must NOT be set
855 std::stringstream error_str;
856 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
857 << " and is using depth/stencil image of format " << string_VkFormat(format)
858 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
859 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
860 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
861 "reads respectively.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600862 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600863 return false;
864 }
865 }
866 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600867 break;
868 }
869 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -0600870 // 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 -0600871 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
872 // under vkCreateImage()
873 // TODO : Need to also validate case VALIDATION_ERROR_00952 where STORAGE_IMAGE & INPUT_ATTACH types must have been created with
874 // identify swizzle
Tobin Ehlis1809f912016-05-25 09:24:36 -0600875 std::string error_usage_bit;
876 switch (type) {
877 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
878 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
879 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
880 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
881 }
882 break;
883 }
884 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
885 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
886 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
Tobin Ehlisfb4cf712016-10-10 14:02:48 -0600887 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
888 std::stringstream error_str;
889 // TODO : Need to create custom enum error code for this case
890 error_str << "ImageView (" << image_view << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
891 << string_VkImageLayout(image_layout)
892 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage images can "
893 "only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
894 *error_msg = error_str.str();
895 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600896 }
897 break;
898 }
899 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
900 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
901 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
902 }
903 break;
904 }
905 default:
906 break;
907 }
908 if (!error_usage_bit.empty()) {
909 std::stringstream error_str;
910 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
911 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
912 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600913 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600914 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600915 }
916 return true;
917}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600918
Tobin Ehlis300888c2016-05-18 13:43:26 -0600919void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
920 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600921 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600922}
923
Tobin Ehlis300888c2016-05-18 13:43:26 -0600924void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600925 if (!immutable_) {
926 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600927 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600928 }
929 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600930}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600931
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600932void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
933 if (!immutable_) {
Tobin Ehlisfad7adf2016-10-20 06:50:37 -0600934 auto sampler_state = getSamplerState(dev_data, sampler_);
935 if (sampler_state)
936 core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600937 }
938}
939
Tobin Ehlis300888c2016-05-18 13:43:26 -0600940cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
941 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600942 updated = false;
943 descriptor_class = ImageSampler;
944}
945
Tobin Ehlis300888c2016-05-18 13:43:26 -0600946cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
947 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600948 updated = false;
949 descriptor_class = ImageSampler;
950 if (immut) {
951 sampler_ = *immut;
952 immutable_ = true;
953 updated = true;
954 }
955}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600956
Tobin Ehlis300888c2016-05-18 13:43:26 -0600957void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600958 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600959 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600960 sampler_ = image_info.sampler;
961 image_view_ = image_info.imageView;
962 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600963}
964
Tobin Ehlis300888c2016-05-18 13:43:26 -0600965void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600966 if (!immutable_) {
967 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600968 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600969 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600970 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
971 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600972 updated = true;
973 image_view_ = image_view;
974 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600975}
976
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600977void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data,
978 GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600979 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600980 if (!immutable_) {
Tobin Ehlisfad7adf2016-10-20 06:50:37 -0600981 auto sampler_state = getSamplerState(dev_data, sampler_);
982 if (sampler_state)
983 core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600984 }
Tobin Ehlis81e46372016-08-17 13:33:44 -0600985 // Add binding for image
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600986 auto iv_state = getImageViewState(dev_data, image_view_);
987 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -0600988 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -0600989 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600990}
991
Tobin Ehlis300888c2016-05-18 13:43:26 -0600992cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
993 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600994 updated = false;
995 descriptor_class = Image;
996 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
997 storage_ = true;
998};
999
Tobin Ehlis300888c2016-05-18 13:43:26 -06001000void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001001 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001002 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001003 image_view_ = image_info.imageView;
1004 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001005}
1006
Tobin Ehlis300888c2016-05-18 13:43:26 -06001007void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001008 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1009 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001010 updated = true;
1011 image_view_ = image_view;
1012 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001013}
1014
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001015void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001016 // Add binding for image
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001017 auto iv_state = getImageViewState(dev_data, image_view_);
1018 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001019 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001020 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001021}
1022
Tobin Ehlis300888c2016-05-18 13:43:26 -06001023cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1024 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001025 updated = false;
1026 descriptor_class = GeneralBuffer;
1027 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1028 dynamic_ = true;
1029 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1030 storage_ = true;
1031 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1032 dynamic_ = true;
1033 storage_ = true;
1034 }
1035}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001036void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001037 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001038 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001039 buffer_ = buffer_info.buffer;
1040 offset_ = buffer_info.offset;
1041 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001042}
1043
Tobin Ehlis300888c2016-05-18 13:43:26 -06001044void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1045 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001046 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001047 buffer_ = buff_desc->buffer_;
1048 offset_ = buff_desc->offset_;
1049 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001050}
1051
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001052void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001053 auto buffer_node = getBufferState(dev_data, buffer_);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001054 if (buffer_node)
1055 core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001056}
1057
Tobin Ehlis300888c2016-05-18 13:43:26 -06001058cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001059 updated = false;
1060 descriptor_class = TexelBuffer;
1061 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
1062 storage_ = true;
1063};
Tobin Ehlis56a30942016-05-19 08:00:00 -06001064
Tobin Ehlis300888c2016-05-18 13:43:26 -06001065void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001066 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001067 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001068}
1069
Tobin Ehlis300888c2016-05-18 13:43:26 -06001070void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1071 updated = true;
1072 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1073}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001074
1075void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8b872462016-09-14 08:12:08 -06001076 auto bv_state = getBufferViewState(dev_data, buffer_view_);
1077 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001078 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001079 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001080}
1081
Tobin Ehlis300888c2016-05-18 13:43:26 -06001082// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1083// sets, and then calls their respective Validate[Write|Copy]Update functions.
1084// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1085// be skipped, then true is returned.
1086// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001087bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
1088 const core_validation::layer_data *dev_data, uint32_t write_count,
1089 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1090 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001091 bool skip_call = false;
1092 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001093 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001094 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001095 auto set_node = core_validation::getSetNode(dev_data, dest_set);
1096 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001097 skip_call |=
1098 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis56a30942016-05-19 08:00:00 -06001099 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001100 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
1101 reinterpret_cast<uint64_t &>(dest_set));
1102 } else {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001103 UNIQUE_VALIDATION_ERROR_CODE error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001104 std::string error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001105 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_code, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001106 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001107 reinterpret_cast<uint64_t &>(dest_set), __LINE__, error_code, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001108 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001109 " with error: %s. %s",
1110 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str(), validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001111 }
1112 }
1113 }
1114 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001115 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001116 auto dst_set = p_cds[i].dstSet;
1117 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001118 auto src_node = core_validation::getSetNode(dev_data, src_set);
1119 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
1120 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001121 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001122 reinterpret_cast<uint64_t &>(src_set), __LINE__, VALIDATION_ERROR_00971, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001123 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001124 " that has not been allocated. %s",
1125 reinterpret_cast<uint64_t &>(src_set), validation_error_map[VALIDATION_ERROR_00971]);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001126 } else if (!dst_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001127 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001128 reinterpret_cast<uint64_t &>(dst_set), __LINE__, VALIDATION_ERROR_00972, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001129 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001130 " that has not been allocated. %s",
1131 reinterpret_cast<uint64_t &>(dst_set), validation_error_map[VALIDATION_ERROR_00972]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001132 } else {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001133 UNIQUE_VALIDATION_ERROR_CODE error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001134 std::string error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001135 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_code, &error_str)) {
1136 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1137 reinterpret_cast<uint64_t &>(dst_set), __LINE__, error_code, "DS",
1138 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
1139 " to Descriptor Set 0x%" PRIx64 " with error: %s. %s",
1140 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set),
1141 error_str.c_str(), validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001142 }
1143 }
1144 }
1145 return skip_call;
1146}
1147// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1148// sets, and then calls their respective Perform[Write|Copy]Update functions.
1149// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1150// with the same set of updates.
1151// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1152// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001153void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
1154 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1155 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001156 // Write updates first
1157 uint32_t i = 0;
1158 for (i = 0; i < write_count; ++i) {
1159 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001160 auto set_node = core_validation::getSetNode(dev_data, dest_set);
1161 if (set_node) {
1162 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001163 }
1164 }
1165 // Now copy updates
1166 for (i = 0; i < copy_count; ++i) {
1167 auto dst_set = p_cds[i].dstSet;
1168 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001169 auto src_node = core_validation::getSetNode(dev_data, src_set);
1170 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
1171 if (src_node && dst_node) {
1172 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001173 }
1174 }
1175}
1176// Validate the state for a given write update but don't actually perform the update
1177// If an error would occur for this update, return false and fill in details in error_msg string
1178bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001179 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001180 // Verify idle ds
1181 if (in_use.load()) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001182 // TODO : Re-using Allocate Idle error code, need write update idle error code
1183 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001184 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001185 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001186 << " that is in use by a command buffer";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001187 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001188 return false;
1189 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001190 // Verify dst binding exists
1191 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001192 *error_code = VALIDATION_ERROR_00936;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001193 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001194 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001195 *error_msg = error_str.str();
1196 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001197 } else {
1198 // Make sure binding isn't empty
1199 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
1200 *error_code = VALIDATION_ERROR_02348;
1201 std::stringstream error_str;
1202 error_str << "DescriptorSet " << set_ << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
1203 *error_msg = error_str.str();
1204 return false;
1205 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001206 }
1207 // We know that binding is valid, verify update and do update on each descriptor
1208 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
1209 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
1210 if (type != update->descriptorType) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001211 *error_code = VALIDATION_ERROR_00937;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001212 std::stringstream error_str;
1213 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1214 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1215 *error_msg = error_str.str();
1216 return false;
1217 }
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001218 if (update->descriptorCount >
1219 (p_layout_->GetConsecutiveDescriptorCountFromBinding(update->dstBinding) - update->dstArrayElement)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001220 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001221 std::stringstream error_str;
1222 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001223 << p_layout_->GetConsecutiveDescriptorCountFromBinding(update->dstBinding)
1224 << " descriptors in that binding and all successive bindings of the set, but update of "
1225 << update->descriptorCount << " descriptors combined with update array element offset of "
1226 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001227 *error_msg = error_str.str();
1228 return false;
1229 }
1230 // Verify consecutive bindings match (if needed)
1231 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001232 set_, error_msg)) {
1233 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001234 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001235 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001236 // Update is within bounds and consistent so last step is to validate update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001237 if (!VerifyWriteUpdateContents(update, start_idx, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001238 std::stringstream error_str;
1239 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1240 << " failed with error message: " << error_msg->c_str();
1241 *error_msg = error_str.str();
1242 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001243 }
1244 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001245 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001246}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001247// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001248// 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 -07001249bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001250 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001251 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001252 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001253 std::string error_usage_bit;
1254 switch (type) {
1255 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1256 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001257 *error_code = VALIDATION_ERROR_00950;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001258 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1259 }
1260 break;
1261 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1262 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001263 *error_code = VALIDATION_ERROR_00951;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001264 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1265 }
1266 break;
1267 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1268 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1269 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001270 *error_code = VALIDATION_ERROR_00946;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001271 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1272 }
1273 break;
1274 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1275 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1276 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001277 *error_code = VALIDATION_ERROR_00947;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001278 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1279 }
1280 break;
1281 default:
1282 break;
1283 }
1284 if (!error_usage_bit.empty()) {
1285 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001286 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1287 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1288 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001289 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001290 return false;
1291 }
1292 return true;
1293}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001294// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1295// 1. buffer is valid
1296// 2. buffer was created with correct usage flags
1297// 3. offset is less than buffer size
1298// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001299// 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 -06001300bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001301 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
1302 // TODO : Defaulting to 00962 for all cases here. Need to create new error codes for a few cases below.
1303 *error_code = VALIDATION_ERROR_00962;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001304 // First make sure that buffer is valid
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001305 auto buffer_node = getBufferState(device_data_, buffer_info->buffer);
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001306 if (!buffer_node) {
1307 std::stringstream error_str;
1308 error_str << "Invalid VkBuffer: " << buffer_info->buffer;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001309 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001310 return false;
1311 }
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001312 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()")) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001313 // TODO : This is a repeat code, need new code(s) for language in 11.6 Memory Association
1314 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001315 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001316 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001317 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001318 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1319 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001320 return false;
1321 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001322 // TODO : Need to also validate device limit offset requirements captured in VALIDATION_ERROR_00944,945
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001323 // offset must be less than buffer size
1324 if (buffer_info->offset > buffer_node->createInfo.size) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001325 *error_code = VALIDATION_ERROR_00959;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001326 std::stringstream error_str;
1327 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than buffer " << buffer_node->buffer
1328 << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001329 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001330 return false;
1331 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001332 // TODO : Need to also validate device limit range requirements captured in VALIDATION_ERROR_00948,949
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001333 if (buffer_info->range != VK_WHOLE_SIZE) {
1334 // Range must be VK_WHOLE_SIZE or > 0
1335 if (!buffer_info->range) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001336 *error_code = VALIDATION_ERROR_00960;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001337 std::stringstream error_str;
1338 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001339 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001340 return false;
1341 }
1342 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1343 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001344 *error_code = VALIDATION_ERROR_00961;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001345 std::stringstream error_str;
1346 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1347 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001348 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001349 return false;
1350 }
1351 }
1352 return true;
1353}
1354
Tobin Ehlis300888c2016-05-18 13:43:26 -06001355// Verify that the contents of the update are ok, but don't perform actual update
1356bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001357 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1358 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001359 switch (update->descriptorType) {
1360 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1361 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1362 // Validate image
1363 auto image_view = update->pImageInfo[di].imageView;
1364 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001365 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001366 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001367 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error_msg->c_str();
1368 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001369 return false;
1370 }
1371 }
1372 // Intentional fall-through to validate sampler
1373 }
1374 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1375 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1376 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001377 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001378 *error_code = VALIDATION_ERROR_00942;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001379 std::stringstream error_str;
1380 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1381 << update->pImageInfo[di].sampler << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001382 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001383 return false;
1384 }
1385 } else {
1386 // TODO : Warn here
1387 }
1388 }
1389 break;
1390 }
1391 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1392 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1393 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1394 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1395 auto image_view = update->pImageInfo[di].imageView;
1396 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001397 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001398 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001399 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
1400 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001401 return false;
1402 }
1403 }
1404 break;
1405 }
1406 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1407 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1408 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1409 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis8b872462016-09-14 08:12:08 -06001410 auto bv_state = getBufferViewState(device_data_, buffer_view);
1411 if (!bv_state) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001412 *error_code = VALIDATION_ERROR_00940;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001413 std::stringstream error_str;
1414 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001415 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001416 return false;
1417 }
Tobin Ehlis8b872462016-09-14 08:12:08 -06001418 auto buffer = bv_state->create_info.buffer;
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001419 if (!ValidateBufferUsage(getBufferState(device_data_, buffer), update->descriptorType, error_code, error_msg)) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001420 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001421 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
1422 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001423 return false;
1424 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001425 }
1426 break;
1427 }
1428 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1429 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1430 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1431 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1432 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001433 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001434 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001435 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
1436 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001437 return false;
1438 }
1439 }
1440 break;
1441 }
1442 default:
1443 assert(0); // We've already verified update type so should never get here
1444 break;
1445 }
1446 // All checks passed so update contents are good
1447 return true;
1448}
1449// Verify that the contents of the update are ok, but don't perform actual update
1450bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001451 VkDescriptorType type, uint32_t index,
1452 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1453 std::string *error_msg) const {
1454 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
1455 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06001456 switch (src_set->descriptors_[index]->descriptor_class) {
1457 case PlainSampler: {
1458 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1459 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1460 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001461 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001462 *error_code = VALIDATION_ERROR_00942;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001463 std::stringstream error_str;
1464 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001465 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001466 return false;
1467 }
1468 } else {
1469 // TODO : Warn here
1470 }
1471 }
1472 break;
1473 }
1474 case ImageSampler: {
1475 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1476 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1477 // First validate sampler
1478 if (!img_samp_desc->IsImmutableSampler()) {
1479 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001480 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001481 *error_code = VALIDATION_ERROR_00942;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001482 std::stringstream error_str;
1483 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001484 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001485 return false;
1486 }
1487 } else {
1488 // TODO : Warn here
1489 }
1490 // Validate image
1491 auto image_view = img_samp_desc->GetImageView();
1492 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001493 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001494 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001495 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
1496 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001497 return false;
1498 }
1499 }
Alex Smithd8f14792016-09-23 12:18:51 +01001500 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001501 }
1502 case Image: {
1503 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1504 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1505 auto image_view = img_desc->GetImageView();
1506 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001507 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001508 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001509 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
1510 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001511 return false;
1512 }
1513 }
1514 break;
1515 }
1516 case TexelBuffer: {
1517 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1518 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis8b872462016-09-14 08:12:08 -06001519 auto bv_state = getBufferViewState(device_data_, buffer_view);
1520 if (!bv_state) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001521 *error_code = VALIDATION_ERROR_00940;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001522 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001523 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001524 *error_msg = error_str.str();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001525 return false;
1526 }
Tobin Ehlis8b872462016-09-14 08:12:08 -06001527 auto buffer = bv_state->create_info.buffer;
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001528 if (!ValidateBufferUsage(getBufferState(device_data_, buffer), type, error_code, error_msg)) {
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001529 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001530 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
1531 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001532 return false;
1533 }
1534 }
1535 break;
1536 }
1537 case GeneralBuffer: {
1538 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1539 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001540 if (!ValidateBufferUsage(getBufferState(device_data_, buffer), type, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001541 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001542 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
1543 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001544 return false;
1545 }
1546 }
1547 break;
1548 }
1549 default:
1550 assert(0); // We've already verified update type so should never get here
1551 break;
1552 }
1553 // All checks passed so update contents are good
1554 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001555}
Tobin Ehlisee471462016-05-26 11:21:59 -06001556// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlis815e8132016-06-02 13:02:17 -06001557bool cvdescriptorset::ValidateAllocateDescriptorSets(const debug_report_data *report_data,
1558 const VkDescriptorSetAllocateInfo *p_alloc_info,
1559 const core_validation::layer_data *dev_data,
1560 AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001561 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001562
1563 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001564 auto layout = getDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1565 if (!layout) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001566 skip_call |=
1567 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1568 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1569 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1570 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001571 } else {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001572 ds_data->layout_nodes[i] = layout;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001573 // Count total descriptors required per type
Tobin Ehlis815e8132016-06-02 13:02:17 -06001574 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1575 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001576 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1577 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1578 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001579 }
1580 }
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001581 auto pool_state = getDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001582 // Track number of descriptorSets allowable in this pool
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001583 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Tobin Ehlisc4c4bed2016-11-23 12:23:32 -07001584 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1585 reinterpret_cast<uint64_t &>(pool_state->pool), __LINE__, VALIDATION_ERROR_00911, "DS",
1586 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1587 ". This pool only has %d descriptorSets remaining. %s",
1588 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_state->pool),
1589 pool_state->availableSets, validation_error_map[VALIDATION_ERROR_00911]);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001590 }
1591 // Determine whether descriptor counts are satisfiable
1592 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001593 if (ds_data->required_descriptors_by_type[i] > pool_state->availableDescriptorTypeCount[i]) {
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001594 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
Tobin Ehlisc4c4bed2016-11-23 12:23:32 -07001595 reinterpret_cast<const uint64_t &>(pool_state->pool), __LINE__, VALIDATION_ERROR_00912, "DS",
1596 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1597 ". This pool only has %d descriptors of this type remaining. %s",
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001598 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Tobin Ehlisc4c4bed2016-11-23 12:23:32 -07001599 reinterpret_cast<uint64_t &>(pool_state->pool), pool_state->availableDescriptorTypeCount[i],
1600 validation_error_map[VALIDATION_ERROR_00912]);
Tobin Ehlisee471462016-05-26 11:21:59 -06001601 }
1602 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001603
Tobin Ehlisee471462016-05-26 11:21:59 -06001604 return skip_call;
1605}
1606// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001607void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1608 const VkDescriptorSet *descriptor_sets,
1609 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06001610 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06001611 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
1612 const core_validation::layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001613 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001614 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001615 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001616 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1617 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1618 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001619 /* Create tracking object for each descriptor set; insert into
1620 * global map and the pool's set.
1621 */
1622 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis93f22372016-10-12 14:34:12 -06001623 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i],
1624 dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001625
1626 pool_state->sets.insert(new_ds);
1627 new_ds->in_use.store(0);
1628 (*set_map)[descriptor_sets[i]] = new_ds;
1629 }
1630}