blob: 6ad8dc029b9da487723f839cd6d48eb9ec341e1c [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]));
Jamie Madill3f5fd492016-12-19 15:59:18 -050043 insert_index = static_cast<uint32_t>(bindings_.size()) - 1;
Tobin Ehlis9637fb22016-12-12 15:59:34 -070044 } 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 Ehlis0a43bde2016-05-03 08:31:08 -0600100// put all bindings into the given set
101void cvdescriptorset::DescriptorSetLayout::FillBindingSet(std::unordered_set<uint32_t> *binding_set) const {
102 for (auto binding_index_pair : binding_to_index_map_)
103 binding_set->insert(binding_index_pair.first);
104}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600105
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600106VkDescriptorSetLayoutBinding const *
107cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600108 const auto &bi_itr = binding_to_index_map_.find(binding);
109 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600110 return bindings_[bi_itr->second].ptr();
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600111 }
112 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600113}
114VkDescriptorSetLayoutBinding const *
115cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const {
116 if (index >= bindings_.size())
117 return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600118 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600119}
120// Return descriptorCount for given binding, 0 if index is unavailable
121uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600122 const auto &bi_itr = binding_to_index_map_.find(binding);
123 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600124 return bindings_[bi_itr->second].descriptorCount;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600125 }
126 return 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600127}
128// Return descriptorCount for given index, 0 if index is unavailable
129uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
130 if (index >= bindings_.size())
131 return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600132 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600133}
134// For the given binding, return descriptorType
135VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const {
136 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600137 const auto &bi_itr = binding_to_index_map_.find(binding);
138 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600139 return bindings_[bi_itr->second].descriptorType;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600140 }
141 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600142}
143// For the given index, return descriptorType
144VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
145 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600146 return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600147}
148// For the given global index, return descriptorType
149// Currently just counting up through bindings_, may improve this in future
150VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const {
151 uint32_t global_offset = 0;
152 for (auto binding : bindings_) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600153 global_offset += binding.descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600154 if (index < global_offset)
Tobin Ehlis664e6012016-05-05 11:04:44 -0600155 return binding.descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600156 }
157 assert(0); // requested global index is out of bounds
158 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
159}
160// For the given binding, return stageFlags
161VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const {
162 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600163 const auto &bi_itr = binding_to_index_map_.find(binding);
164 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600165 return bindings_[bi_itr->second].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600166 }
167 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600168}
169// For the given binding, return start index
170uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const {
171 assert(binding_to_global_start_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600172 const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding);
173 if (btgsi_itr != binding_to_global_start_index_map_.end()) {
174 return btgsi_itr->second;
175 }
176 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600177 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600178 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600179}
180// For the given binding, return end index
181uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
182 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600183 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
184 if (btgei_itr != binding_to_global_end_index_map_.end()) {
185 return btgei_itr->second;
186 }
187 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600188 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600189 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600190}
191// For given binding, return ptr to ImmutableSampler array
192VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
193 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600194 const auto &bi_itr = binding_to_index_map_.find(binding);
195 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600196 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600197 }
198 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600199}
200// For given index, return ptr to ImmutableSampler array
201VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
202 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600203 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600204}
205// If our layout is compatible with rh_ds_layout, return true,
206// else return false and fill in error_msg will description of what causes incompatibility
207bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
208 // Trivial case
209 if (layout_ == rh_ds_layout->GetDescriptorSetLayout())
210 return true;
211 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
212 std::stringstream error_str;
213 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
214 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
215 *error_msg = error_str.str();
216 return false; // trivial fail case
217 }
218 // Descriptor counts match so need to go through bindings one-by-one
219 // and verify that type and stageFlags match
220 for (auto binding : bindings_) {
221 // TODO : Do we also need to check immutable samplers?
222 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600223 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600224 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600225 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
226 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600227 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600228 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600229 *error_msg = error_str.str();
230 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600231 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600232 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600233 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
234 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600235 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600236 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600237 *error_msg = error_str.str();
238 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600239 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600240 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600241 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
242 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600243 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600244 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600245 *error_msg = error_str.str();
246 return false;
247 }
248 }
249 return true;
250}
251
252bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
253 if (!binding_to_index_map_.count(binding + 1))
254 return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600255 auto const &bi_itr = binding_to_index_map_.find(binding);
256 if (bi_itr != binding_to_index_map_.end()) {
257 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
258 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600259 auto type = bindings_[bi_itr->second].descriptorType;
260 auto stage_flags = bindings_[bi_itr->second].stageFlags;
261 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
262 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
263 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
264 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600265 return false;
266 }
267 return true;
268 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600269 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600270 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600271}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600272// Starting at offset descriptor of given binding, parse over update_count
273// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
274// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
275// If so, return true. If not, fill in error_msg and return false
276bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
277 const char *type, const VkDescriptorSet set,
278 std::string *error_msg) const {
279 // Verify consecutive bindings match (if needed)
280 auto orig_binding = current_binding;
281 // Track count of descriptors in the current_bindings that are remaining to be updated
282 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
283 // First, it's legal to offset beyond your own binding so handle that case
284 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
285 while (offset >= binding_remaining) {
286 // Advance to next binding, decrement offset by binding size
287 offset -= binding_remaining;
288 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
289 }
290 binding_remaining -= offset;
291 while (update_count > binding_remaining) { // While our updates overstep current binding
292 // Verify next consecutive binding matches type, stage flags & immutable sampler use
293 if (!IsNextBindingConsistent(current_binding++)) {
294 std::stringstream error_str;
295 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
296 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
297 "next binding is not consistent with current binding so this update is invalid.";
298 *error_msg = error_str.str();
299 return false;
300 }
301 // For sake of this check consider the bindings updated and grab count for next binding
302 update_count -= binding_remaining;
303 binding_remaining = GetDescriptorCountFromBinding(current_binding);
304 }
305 return true;
306}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600307
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600308cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
309 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
310
Tobin Ehlis93f22372016-10-12 14:34:12 -0600311cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
312 const DescriptorSetLayout *layout, const core_validation::layer_data *dev_data)
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600313 : some_update_(false), set_(set), pool_state_(nullptr), p_layout_(layout), device_data_(dev_data) {
314 pool_state_ = getDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600315 // Foreach binding, create default descriptors of given type
316 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
317 auto type = p_layout_->GetTypeFromIndex(i);
318 switch (type) {
319 case VK_DESCRIPTOR_TYPE_SAMPLER: {
320 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
321 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
322 if (immut_sampler)
Chris Forbescb621ea2016-05-30 11:47:31 +1200323 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600324 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200325 descriptors_.emplace_back(new SamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600326 }
327 break;
328 }
329 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
330 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
331 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
332 if (immut)
Chris Forbescb621ea2016-05-30 11:47:31 +1200333 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600334 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200335 descriptors_.emplace_back(new ImageSamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600336 }
337 break;
338 }
339 // ImageDescriptors
340 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
341 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
342 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
343 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200344 descriptors_.emplace_back(new ImageDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600345 break;
346 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
347 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
348 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200349 descriptors_.emplace_back(new TexelDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600350 break;
351 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
352 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
353 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
354 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
355 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200356 descriptors_.emplace_back(new BufferDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600357 break;
358 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600359 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600360 break;
361 }
362 }
363}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600364
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700365cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200366
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700367static std::string string_descriptor_req_view_type(descriptor_req req) {
368 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200369 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
370 if (req & (1 << i)) {
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700371 if (result.size())
372 result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700373 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200374 }
375 }
376
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700377 if (!result.size())
378 result = "(none)";
379
380 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200381}
382
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600383// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
384bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
385 return layout->IsCompatible(p_layout_, error);
386}
Chris Forbes57989132016-07-26 17:06:10 +1200387
Tobin Ehlis3066db62016-08-22 08:12:23 -0600388// 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 -0600389// This includes validating that all descriptors in the given bindings are updated,
390// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
391// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600392bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600393 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200394 for (auto binding_pair : bindings) {
395 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600396 if (!p_layout_->HasBinding(binding)) {
397 std::stringstream error_str;
398 error_str << "Attempting to validate DrawState for binding #" << binding
399 << " which is an invalid binding for this descriptor set.";
400 *error = error_str.str();
401 return false;
402 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600403 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600404 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600405 // Nothing to do for strictly immutable sampler
406 } else {
407 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700408 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
409 for (uint32_t i = start_idx; i <= end_idx; ++i, ++array_idx) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600410 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600411 std::stringstream error_str;
412 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
413 << " is being used in draw but has not been updated.";
414 *error = error_str.str();
415 return false;
416 } else {
Chris Forbes57989132016-07-26 17:06:10 +1200417 auto descriptor_class = descriptors_[i]->GetClass();
418 if (descriptor_class == GeneralBuffer) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600419 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600420 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis4668dce2016-11-16 09:30:23 -0700421 auto buffer_node = getBufferState(device_data_, buffer);
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600422 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600423 std::stringstream error_str;
424 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
425 << " references invalid buffer " << buffer << ".";
426 *error = error_str.str();
427 return false;
428 } else {
Tobin Ehlis640a81c2016-11-15 15:37:18 -0700429 for (auto mem_binding : buffer_node->GetBoundMemory()) {
430 if (!getMemObjInfo(device_data_, mem_binding)) {
431 std::stringstream error_str;
432 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
433 << " uses buffer " << buffer << " that references invalid memory " << mem_binding
434 << ".";
435 *error = error_str.str();
436 return false;
437 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600438 }
439 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600440 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600441 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600442 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600443 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
444 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700445 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600446 if (VK_WHOLE_SIZE == range) {
447 if ((dyn_offset + desc_offset) > buffer_size) {
448 std::stringstream error_str;
449 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
450 << " uses buffer " << buffer
451 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
452 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
453 << buffer_size << ".";
454 *error = error_str.str();
455 return false;
456 }
457 } else {
458 if ((dyn_offset + desc_offset + range) > buffer_size) {
459 std::stringstream error_str;
460 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
461 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
462 << " combined with offset " << desc_offset << " and range " << range
463 << " that oversteps the buffer size of " << buffer_size << ".";
464 *error = error_str.str();
465 return false;
466 }
467 }
468 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700469 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
Chris Forbes57989132016-07-26 17:06:10 +1200470 auto image_view = (descriptor_class == ImageSampler)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700471 ? static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView()
472 : static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
Chris Forbes57989132016-07-26 17:06:10 +1200473 auto reqs = binding_pair.second;
474
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600475 auto image_view_state = getImageViewState(device_data_, image_view);
476 assert(image_view_state);
477 auto image_view_ci = image_view_state->create_info;
Chris Forbes57989132016-07-26 17:06:10 +1200478
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600479 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
Chris Forbes57989132016-07-26 17:06:10 +1200480 // bad view type
481 std::stringstream error_str;
482 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700483 << " requires an image view of type " << string_descriptor_req_view_type(reqs) << " but got "
484 << string_VkImageViewType(image_view_ci.viewType) << ".";
Chris Forbes57989132016-07-26 17:06:10 +1200485 *error = error_str.str();
486 return false;
487 }
488
Tobin Ehlis30df15c2016-10-12 17:17:57 -0600489 auto image_node = getImageState(device_data_, image_view_ci.image);
Chris Forbes57989132016-07-26 17:06:10 +1200490 assert(image_node);
491
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700492 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
Chris Forbes57989132016-07-26 17:06:10 +1200493 std::stringstream error_str;
494 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
495 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
496 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
497 *error = error_str.str();
498 return false;
499 }
500
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700501 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
Chris Forbes57989132016-07-26 17:06:10 +1200502 std::stringstream error_str;
503 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
504 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
505 *error = error_str.str();
506 return false;
507 }
508 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600509 }
510 }
511 }
512 }
513 return true;
514}
Chris Forbes57989132016-07-26 17:06:10 +1200515
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600516// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600517uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600518 std::unordered_set<VkBuffer> *buffer_set,
519 std::unordered_set<VkImageView> *image_set) const {
520 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200521 for (auto binding_pair : bindings) {
522 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600523 // If a binding doesn't exist, skip it
524 if (!p_layout_->HasBinding(binding)) {
525 continue;
526 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600527 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600528 if (descriptors_[start_idx]->IsStorage()) {
529 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600530 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600531 if (descriptors_[start_idx + i]->updated) {
532 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600533 num_updates++;
534 }
535 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600536 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600537 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600538 if (descriptors_[start_idx + i]->updated) {
539 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis8b872462016-09-14 08:12:08 -0600540 auto bv_state = getBufferViewState(device_data_, bufferview);
541 if (bv_state) {
542 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600543 num_updates++;
544 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600545 }
546 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600547 } else if (GeneralBuffer == 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 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600551 num_updates++;
552 }
553 }
554 }
555 }
556 }
557 return num_updates;
558}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600559// Set is being deleted or updates so invalidate all bound cmd buffers
560void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Tobin Ehlisfe5731a2016-11-21 08:31:01 -0700561 core_validation::invalidateCommandBuffers(device_data_, cb_bindings,
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600562 {reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600563}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600564// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600565void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700566 // Perform update on a per-binding basis as consecutive updates roll over to next binding
567 auto descriptors_remaining = update->descriptorCount;
568 auto binding_being_updated = update->dstBinding;
569 auto offset = update->dstArrayElement;
570 while (descriptors_remaining) {
571 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
572 auto global_idx = p_layout_->GetGlobalStartIndexFromBinding(binding_being_updated) + offset;
573 // Loop over the updates for a single binding at a time
574 for (uint32_t di = 0; di < update_count; ++di) {
575 descriptors_[global_idx + di]->WriteUpdate(update, di);
576 }
577 // Roll over to next binding in case of consecutive update
578 descriptors_remaining -= update_count;
579 offset = 0;
580 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600581 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600582 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600583 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600584
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600585 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600586}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600587// Validate Copy update
588bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600589 const DescriptorSet *src_set, UNIQUE_VALIDATION_ERROR_CODE *error_code,
590 std::string *error_msg) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600591 // Verify idle ds
592 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700593 // TODO : Re-using Free Idle error code, need copy update idle error code
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600594 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600595 std::stringstream error_str;
596 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700597 << " that is in use by a command buffer";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600598 *error_msg = error_str.str();
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600599 return false;
600 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600601 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600602 *error_code = VALIDATION_ERROR_00966;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600603 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700604 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600605 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600606 return false;
607 }
608 if (!src_set->HasBinding(update->srcBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600609 *error_code = VALIDATION_ERROR_00964;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600610 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700611 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600612 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600613 return false;
614 }
615 // src & dst set bindings are valid
616 // Check bounds of src & dst
617 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
618 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
619 // SRC update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600620 *error_code = VALIDATION_ERROR_00965;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600621 std::stringstream error_str;
622 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
623 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
624 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700625 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600626 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600627 return false;
628 }
629 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
630 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
631 // DST update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600632 *error_code = VALIDATION_ERROR_00967;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600633 std::stringstream error_str;
634 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
635 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
636 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700637 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600638 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600639 return false;
640 }
641 // Check that types match
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600642 // TODO : Base default error case going from here is VALIDATION_ERROR_00968 which covers all consistency issues, need more
643 // fine-grained error codes
644 *error_code = VALIDATION_ERROR_00968;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600645 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
646 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
647 if (src_type != dst_type) {
648 std::stringstream error_str;
649 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
650 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700651 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600652 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600653 return false;
654 }
655 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600656 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600657 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600658 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600659 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600660 return false;
661 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600662 // First make sure source descriptors are updated
663 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
664 if (!src_set->descriptors_[src_start_idx + i]) {
665 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700666 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding
667 << " but descriptor at array offset " << update->srcArrayElement + i << " has not been updated";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600668 *error_msg = error_str.str();
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600669 return false;
670 }
671 }
672 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600673 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error_code, error_msg))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600674 return false;
675
676 // All checks passed so update is good
677 return true;
678}
679// Perform Copy update
680void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600681 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
682 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600683 // Update parameters all look good so perform update
684 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600685 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600686 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600687 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600688 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600689
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600690 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600691}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600692
Tobin Ehlisf9519102016-08-17 09:49:13 -0600693// Bind cb_node to this set and this set to cb_node.
694// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
695// to be used in a draw by the given cb_node
Tobin Ehlis276d3d32016-12-21 09:21:06 -0700696void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node,
Tobin Ehlis022528b2016-12-29 12:22:32 -0700697 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600698 // bind cb to this descriptor set
699 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600700 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600701 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 -0600702 pool_state_->cb_bindings.insert(cb_node);
703 cb_node->object_bindings.insert(
704 {reinterpret_cast<uint64_t &>(pool_state_->pool), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT});
Tobin Ehlisf9519102016-08-17 09:49:13 -0600705 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
706 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -0700707 for (auto binding_req_pair : binding_req_map) {
708 auto binding = binding_req_pair.first;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600709 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
710 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
711 for (uint32_t i = start_idx; i <= end_idx; ++i) {
712 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
713 }
714 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600715}
716
Tobin Ehlis300888c2016-05-18 13:43:26 -0600717cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600718 updated = false;
719 descriptor_class = PlainSampler;
720};
721
Tobin Ehlis300888c2016-05-18 13:43:26 -0600722cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600723 updated = false;
724 descriptor_class = PlainSampler;
725 if (immut) {
726 sampler_ = *immut;
727 immutable_ = true;
728 updated = true;
729 }
730}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600731// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
732bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
Tobin Ehlisfad7adf2016-10-20 06:50:37 -0600733 return (getSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600734}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600735
Tobin Ehlis554bf382016-05-24 11:14:43 -0600736bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600737 const core_validation::layer_data *dev_data, UNIQUE_VALIDATION_ERROR_CODE *error_code,
738 std::string *error_msg) {
739 // TODO : Defaulting to 00943 for all cases here. Need to create new error codes for various cases.
740 *error_code = VALIDATION_ERROR_00943;
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600741 auto iv_state = getImageViewState(dev_data, image_view);
742 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600743 std::stringstream error_str;
744 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600745 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600746 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600747 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600748 // 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 -0600749 // Validate that imageLayout is compatible with aspect_mask and image format
750 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600751 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
752 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600753 VkFormat format = VK_FORMAT_MAX_ENUM;
754 VkImageUsageFlags usage = 0;
Tobin Ehlis30df15c2016-10-12 17:17:57 -0600755 auto image_node = getImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600756 if (image_node) {
757 format = image_node->createInfo.format;
758 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600759 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700760 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
761 // the error here occurs is if memory bound to a created imageView has been freed.
Tobin Ehlise1995fc2016-12-22 12:45:09 -0700762 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_02524)) {
Tobin Ehlisde1a0f92016-12-22 12:26:32 -0700763 *error_code = VALIDATION_ERROR_02524;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600764 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600765 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -0600766 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600767 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600768 // Also need to check the swapchains.
Tobin Ehlis969a5262016-06-02 12:13:32 -0600769 auto swapchain = getSwapchainFromImage(dev_data, image);
770 if (swapchain) {
Tobin Ehlis4e380592016-06-02 12:41:47 -0600771 auto swapchain_node = getSwapchainNode(dev_data, swapchain);
772 if (swapchain_node) {
773 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600774 }
775 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600776 }
777 // First validate that format and layout are compatible
778 if (format == VK_FORMAT_MAX_ENUM) {
779 std::stringstream error_str;
780 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600781 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600782 return false;
783 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600784 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
785 // vkCreateImageView(). What's the best way to create unique id for these cases?
Tobin Ehlis1809f912016-05-25 09:24:36 -0600786 bool ds = vk_format_is_depth_or_stencil(format);
787 switch (image_layout) {
788 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
789 // Only Color bit must be set
790 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600791 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600792 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
793 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600794 *error_msg = error_str.str();
Tobin Ehlis554bf382016-05-24 11:14:43 -0600795 return false;
796 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600797 // format must NOT be DS
798 if (ds) {
799 std::stringstream error_str;
800 error_str << "ImageView (" << image_view
801 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
802 << string_VkFormat(format) << " which is not a color format.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600803 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600804 return false;
805 }
806 break;
807 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
808 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
809 // Depth or stencil bit must be set, but both must NOT be set
810 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
811 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
812 // both must NOT be set
813 std::stringstream error_str;
814 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600815 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600816 return false;
817 }
818 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
819 // Neither were set
820 std::stringstream error_str;
821 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
822 << " but does not have STENCIL or DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600823 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600824 return false;
825 }
826 // format must be DS
827 if (!ds) {
828 std::stringstream error_str;
829 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
830 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600831 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600832 return false;
833 }
834 break;
835 default:
Mike Weiblencce7ec72016-10-17 19:33:05 -0600836 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600837 if (ds) {
838 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
839 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
840 // both must NOT be set
841 std::stringstream error_str;
842 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
843 << " and is using depth/stencil image of format " << string_VkFormat(format)
844 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
845 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
846 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
847 "reads respectively.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600848 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600849 return false;
850 }
851 }
852 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600853 break;
854 }
855 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -0600856 // 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 -0600857 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
858 // under vkCreateImage()
859 // TODO : Need to also validate case VALIDATION_ERROR_00952 where STORAGE_IMAGE & INPUT_ATTACH types must have been created with
860 // identify swizzle
Tobin Ehlis1809f912016-05-25 09:24:36 -0600861 std::string error_usage_bit;
862 switch (type) {
863 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
864 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
865 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
866 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
867 }
868 break;
869 }
870 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
871 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
872 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
Tobin Ehlisfb4cf712016-10-10 14:02:48 -0600873 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
874 std::stringstream error_str;
875 // TODO : Need to create custom enum error code for this case
876 error_str << "ImageView (" << image_view << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
877 << string_VkImageLayout(image_layout)
878 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage images can "
879 "only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
880 *error_msg = error_str.str();
881 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600882 }
883 break;
884 }
885 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
886 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
887 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
888 }
889 break;
890 }
891 default:
892 break;
893 }
894 if (!error_usage_bit.empty()) {
895 std::stringstream error_str;
896 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
897 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
898 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600899 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600900 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600901 }
902 return true;
903}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600904
Tobin Ehlis300888c2016-05-18 13:43:26 -0600905void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
906 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600907 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600908}
909
Tobin Ehlis300888c2016-05-18 13:43:26 -0600910void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600911 if (!immutable_) {
912 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600913 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600914 }
915 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600916}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600917
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600918void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
919 if (!immutable_) {
Tobin Ehlisfad7adf2016-10-20 06:50:37 -0600920 auto sampler_state = getSamplerState(dev_data, sampler_);
921 if (sampler_state)
922 core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600923 }
924}
925
Tobin Ehlis300888c2016-05-18 13:43:26 -0600926cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
927 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600928 updated = false;
929 descriptor_class = ImageSampler;
930}
931
Tobin Ehlis300888c2016-05-18 13:43:26 -0600932cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
933 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600934 updated = false;
935 descriptor_class = ImageSampler;
936 if (immut) {
937 sampler_ = *immut;
938 immutable_ = true;
939 updated = true;
940 }
941}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600942
Tobin Ehlis300888c2016-05-18 13:43:26 -0600943void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600944 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600945 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600946 sampler_ = image_info.sampler;
947 image_view_ = image_info.imageView;
948 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600949}
950
Tobin Ehlis300888c2016-05-18 13:43:26 -0600951void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600952 if (!immutable_) {
953 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600954 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600955 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600956 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
957 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600958 updated = true;
959 image_view_ = image_view;
960 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600961}
962
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600963void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data,
964 GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600965 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600966 if (!immutable_) {
Tobin Ehlisfad7adf2016-10-20 06:50:37 -0600967 auto sampler_state = getSamplerState(dev_data, sampler_);
968 if (sampler_state)
969 core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600970 }
Tobin Ehlis81e46372016-08-17 13:33:44 -0600971 // Add binding for image
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600972 auto iv_state = getImageViewState(dev_data, image_view_);
973 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -0600974 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -0600975 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600976}
977
Tobin Ehlis300888c2016-05-18 13:43:26 -0600978cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
979 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600980 updated = false;
981 descriptor_class = Image;
982 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
983 storage_ = true;
984};
985
Tobin Ehlis300888c2016-05-18 13:43:26 -0600986void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600987 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600988 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600989 image_view_ = image_info.imageView;
990 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600991}
992
Tobin Ehlis300888c2016-05-18 13:43:26 -0600993void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600994 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
995 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600996 updated = true;
997 image_view_ = image_view;
998 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600999}
1000
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001001void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001002 // Add binding for image
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001003 auto iv_state = getImageViewState(dev_data, image_view_);
1004 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001005 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001006 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001007}
1008
Tobin Ehlis300888c2016-05-18 13:43:26 -06001009cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1010 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001011 updated = false;
1012 descriptor_class = GeneralBuffer;
1013 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1014 dynamic_ = true;
1015 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1016 storage_ = true;
1017 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1018 dynamic_ = true;
1019 storage_ = true;
1020 }
1021}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001022void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001023 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001024 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001025 buffer_ = buffer_info.buffer;
1026 offset_ = buffer_info.offset;
1027 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001028}
1029
Tobin Ehlis300888c2016-05-18 13:43:26 -06001030void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1031 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001032 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001033 buffer_ = buff_desc->buffer_;
1034 offset_ = buff_desc->offset_;
1035 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001036}
1037
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001038void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001039 auto buffer_node = getBufferState(dev_data, buffer_);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001040 if (buffer_node)
1041 core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001042}
1043
Tobin Ehlis300888c2016-05-18 13:43:26 -06001044cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001045 updated = false;
1046 descriptor_class = TexelBuffer;
1047 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
1048 storage_ = true;
1049};
Tobin Ehlis56a30942016-05-19 08:00:00 -06001050
Tobin Ehlis300888c2016-05-18 13:43:26 -06001051void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001052 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001053 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001054}
1055
Tobin Ehlis300888c2016-05-18 13:43:26 -06001056void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1057 updated = true;
1058 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1059}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001060
1061void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8b872462016-09-14 08:12:08 -06001062 auto bv_state = getBufferViewState(dev_data, buffer_view_);
1063 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001064 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001065 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001066}
1067
Tobin Ehlis300888c2016-05-18 13:43:26 -06001068// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1069// sets, and then calls their respective Validate[Write|Copy]Update functions.
1070// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1071// be skipped, then true is returned.
1072// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001073bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
1074 const core_validation::layer_data *dev_data, uint32_t write_count,
1075 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1076 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001077 bool skip_call = false;
1078 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001079 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001080 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001081 auto set_node = core_validation::getSetNode(dev_data, dest_set);
1082 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001083 skip_call |=
1084 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 -06001085 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001086 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
1087 reinterpret_cast<uint64_t &>(dest_set));
1088 } else {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001089 UNIQUE_VALIDATION_ERROR_CODE error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001090 std::string error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001091 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_code, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001092 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 -06001093 reinterpret_cast<uint64_t &>(dest_set), __LINE__, error_code, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001094 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001095 " with error: %s. %s",
1096 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str(), validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001097 }
1098 }
1099 }
1100 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001101 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001102 auto dst_set = p_cds[i].dstSet;
1103 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001104 auto src_node = core_validation::getSetNode(dev_data, src_set);
1105 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001106 // Object_tracker verifies that src & dest descriptor set are valid
1107 assert(src_node);
1108 assert(dst_node);
1109 UNIQUE_VALIDATION_ERROR_CODE error_code;
1110 std::string error_str;
1111 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_code, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001112 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlisa1712752017-01-04 09:41:47 -07001113 reinterpret_cast<uint64_t &>(dst_set), __LINE__, error_code, "DS",
1114 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
1115 " to Descriptor Set 0x%" PRIx64 " with error: %s. %s",
1116 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str(),
1117 validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001118 }
1119 }
1120 return skip_call;
1121}
1122// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1123// sets, and then calls their respective Perform[Write|Copy]Update functions.
1124// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1125// with the same set of updates.
1126// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1127// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001128void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
1129 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1130 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001131 // Write updates first
1132 uint32_t i = 0;
1133 for (i = 0; i < write_count; ++i) {
1134 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001135 auto set_node = core_validation::getSetNode(dev_data, dest_set);
1136 if (set_node) {
1137 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001138 }
1139 }
1140 // Now copy updates
1141 for (i = 0; i < copy_count; ++i) {
1142 auto dst_set = p_cds[i].dstSet;
1143 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001144 auto src_node = core_validation::getSetNode(dev_data, src_set);
1145 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
1146 if (src_node && dst_node) {
1147 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001148 }
1149 }
1150}
1151// Validate the state for a given write update but don't actually perform the update
1152// If an error would occur for this update, return false and fill in details in error_msg string
1153bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001154 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001155 // Verify idle ds
1156 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001157 // TODO : Re-using Free Idle error code, need write update idle error code
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001158 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001159 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001160 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001161 << " that is in use by a command buffer";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001162 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001163 return false;
1164 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001165 // Verify dst binding exists
1166 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001167 *error_code = VALIDATION_ERROR_00936;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001168 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001169 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001170 *error_msg = error_str.str();
1171 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001172 } else {
1173 // Make sure binding isn't empty
1174 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
1175 *error_code = VALIDATION_ERROR_02348;
1176 std::stringstream error_str;
1177 error_str << "DescriptorSet " << set_ << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
1178 *error_msg = error_str.str();
1179 return false;
1180 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001181 }
1182 // We know that binding is valid, verify update and do update on each descriptor
1183 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
1184 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
1185 if (type != update->descriptorType) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001186 *error_code = VALIDATION_ERROR_00937;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001187 std::stringstream error_str;
1188 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1189 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1190 *error_msg = error_str.str();
1191 return false;
1192 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001193 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001194 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001195 std::stringstream error_str;
1196 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001197 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001198 << " descriptors in that binding and all successive bindings of the set, but update of "
1199 << update->descriptorCount << " descriptors combined with update array element offset of "
1200 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001201 *error_msg = error_str.str();
1202 return false;
1203 }
1204 // Verify consecutive bindings match (if needed)
1205 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001206 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07001207 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001208 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001209 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001210 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001211 // Update is within bounds and consistent so last step is to validate update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001212 if (!VerifyWriteUpdateContents(update, start_idx, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001213 std::stringstream error_str;
1214 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1215 << " failed with error message: " << error_msg->c_str();
1216 *error_msg = error_str.str();
1217 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001218 }
1219 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001220 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001221}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001222// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001223// 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 -07001224bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001225 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001226 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001227 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001228 std::string error_usage_bit;
1229 switch (type) {
1230 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1231 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001232 *error_code = VALIDATION_ERROR_00950;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001233 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1234 }
1235 break;
1236 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1237 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001238 *error_code = VALIDATION_ERROR_00951;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001239 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1240 }
1241 break;
1242 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1243 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1244 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001245 *error_code = VALIDATION_ERROR_00946;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001246 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1247 }
1248 break;
1249 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1250 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1251 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001252 *error_code = VALIDATION_ERROR_00947;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001253 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1254 }
1255 break;
1256 default:
1257 break;
1258 }
1259 if (!error_usage_bit.empty()) {
1260 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001261 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1262 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1263 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001264 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001265 return false;
1266 }
1267 return true;
1268}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001269// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1270// 1. buffer is valid
1271// 2. buffer was created with correct usage flags
1272// 3. offset is less than buffer size
1273// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001274// 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 -06001275bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001276 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001277 // First make sure that buffer is valid
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001278 auto buffer_node = getBufferState(device_data_, buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07001279 // Any invalid buffer should already be caught by object_tracker
1280 assert(buffer_node);
Tobin Ehlise1995fc2016-12-22 12:45:09 -07001281 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_02525)) {
Tobin Ehlisde1a0f92016-12-22 12:26:32 -07001282 *error_code = VALIDATION_ERROR_02525;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001283 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001284 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001285 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001286 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001287 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1288 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001289 return false;
1290 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001291 // TODO : Need to also validate device limit offset requirements captured in VALIDATION_ERROR_00944,945
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001292 // offset must be less than buffer size
1293 if (buffer_info->offset > buffer_node->createInfo.size) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001294 *error_code = VALIDATION_ERROR_00959;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001295 std::stringstream error_str;
1296 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than buffer " << buffer_node->buffer
1297 << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001298 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001299 return false;
1300 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001301 // TODO : Need to also validate device limit range requirements captured in VALIDATION_ERROR_00948,949
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001302 if (buffer_info->range != VK_WHOLE_SIZE) {
1303 // Range must be VK_WHOLE_SIZE or > 0
1304 if (!buffer_info->range) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001305 *error_code = VALIDATION_ERROR_00960;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001306 std::stringstream error_str;
1307 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001308 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001309 return false;
1310 }
1311 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1312 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001313 *error_code = VALIDATION_ERROR_00961;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001314 std::stringstream error_str;
1315 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1316 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001317 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001318 return false;
1319 }
1320 }
1321 return true;
1322}
1323
Tobin Ehlis300888c2016-05-18 13:43:26 -06001324// Verify that the contents of the update are ok, but don't perform actual update
1325bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001326 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1327 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001328 switch (update->descriptorType) {
1329 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1330 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1331 // Validate image
1332 auto image_view = update->pImageInfo[di].imageView;
1333 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001334 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001335 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001336 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error_msg->c_str();
1337 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001338 return false;
1339 }
1340 }
1341 // Intentional fall-through to validate sampler
1342 }
1343 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1344 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1345 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001346 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001347 *error_code = VALIDATION_ERROR_00942;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001348 std::stringstream error_str;
1349 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1350 << update->pImageInfo[di].sampler << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001351 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001352 return false;
1353 }
1354 } else {
1355 // TODO : Warn here
1356 }
1357 }
1358 break;
1359 }
1360 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1361 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1362 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1363 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1364 auto image_view = update->pImageInfo[di].imageView;
1365 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001366 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001367 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001368 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
1369 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001370 return false;
1371 }
1372 }
1373 break;
1374 }
1375 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1376 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1377 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1378 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis8b872462016-09-14 08:12:08 -06001379 auto bv_state = getBufferViewState(device_data_, buffer_view);
1380 if (!bv_state) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001381 *error_code = VALIDATION_ERROR_00940;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001382 std::stringstream error_str;
1383 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001384 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001385 return false;
1386 }
Tobin Ehlis8b872462016-09-14 08:12:08 -06001387 auto buffer = bv_state->create_info.buffer;
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001388 if (!ValidateBufferUsage(getBufferState(device_data_, buffer), update->descriptorType, error_code, error_msg)) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001389 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001390 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
1391 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001392 return false;
1393 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001394 }
1395 break;
1396 }
1397 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1398 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1399 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1400 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1401 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001402 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001403 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001404 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
1405 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001406 return false;
1407 }
1408 }
1409 break;
1410 }
1411 default:
1412 assert(0); // We've already verified update type so should never get here
1413 break;
1414 }
1415 // All checks passed so update contents are good
1416 return true;
1417}
1418// Verify that the contents of the update are ok, but don't perform actual update
1419bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001420 VkDescriptorType type, uint32_t index,
1421 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1422 std::string *error_msg) const {
1423 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
1424 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06001425 switch (src_set->descriptors_[index]->descriptor_class) {
1426 case PlainSampler: {
1427 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1428 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1429 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001430 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001431 *error_code = VALIDATION_ERROR_00942;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001432 std::stringstream error_str;
1433 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001434 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001435 return false;
1436 }
1437 } else {
1438 // TODO : Warn here
1439 }
1440 }
1441 break;
1442 }
1443 case ImageSampler: {
1444 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1445 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1446 // First validate sampler
1447 if (!img_samp_desc->IsImmutableSampler()) {
1448 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001449 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001450 *error_code = VALIDATION_ERROR_00942;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001451 std::stringstream error_str;
1452 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001453 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001454 return false;
1455 }
1456 } else {
1457 // TODO : Warn here
1458 }
1459 // Validate image
1460 auto image_view = img_samp_desc->GetImageView();
1461 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001462 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001463 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001464 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
1465 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001466 return false;
1467 }
1468 }
Alex Smithd8f14792016-09-23 12:18:51 +01001469 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001470 }
1471 case Image: {
1472 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1473 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1474 auto image_view = img_desc->GetImageView();
1475 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001476 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001477 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001478 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
1479 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001480 return false;
1481 }
1482 }
1483 break;
1484 }
1485 case TexelBuffer: {
1486 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1487 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis8b872462016-09-14 08:12:08 -06001488 auto bv_state = getBufferViewState(device_data_, buffer_view);
1489 if (!bv_state) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001490 *error_code = VALIDATION_ERROR_00940;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001491 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001492 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001493 *error_msg = error_str.str();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001494 return false;
1495 }
Tobin Ehlis8b872462016-09-14 08:12:08 -06001496 auto buffer = bv_state->create_info.buffer;
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001497 if (!ValidateBufferUsage(getBufferState(device_data_, buffer), type, error_code, error_msg)) {
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001498 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001499 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
1500 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001501 return false;
1502 }
1503 }
1504 break;
1505 }
1506 case GeneralBuffer: {
1507 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1508 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001509 if (!ValidateBufferUsage(getBufferState(device_data_, buffer), type, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001510 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001511 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
1512 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001513 return false;
1514 }
1515 }
1516 break;
1517 }
1518 default:
1519 assert(0); // We've already verified update type so should never get here
1520 break;
1521 }
1522 // All checks passed so update contents are good
1523 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001524}
Tobin Ehlisee471462016-05-26 11:21:59 -06001525// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlis815e8132016-06-02 13:02:17 -06001526bool cvdescriptorset::ValidateAllocateDescriptorSets(const debug_report_data *report_data,
1527 const VkDescriptorSetAllocateInfo *p_alloc_info,
1528 const core_validation::layer_data *dev_data,
1529 AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001530 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001531
1532 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001533 auto layout = getDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1534 if (!layout) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001535 skip_call |=
1536 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1537 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1538 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1539 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001540 } else {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001541 ds_data->layout_nodes[i] = layout;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001542 // Count total descriptors required per type
Tobin Ehlis815e8132016-06-02 13:02:17 -06001543 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1544 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001545 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1546 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1547 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001548 }
1549 }
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001550 auto pool_state = getDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001551 // Track number of descriptorSets allowable in this pool
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001552 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Tobin Ehlisc4c4bed2016-11-23 12:23:32 -07001553 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1554 reinterpret_cast<uint64_t &>(pool_state->pool), __LINE__, VALIDATION_ERROR_00911, "DS",
1555 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1556 ". This pool only has %d descriptorSets remaining. %s",
1557 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_state->pool),
1558 pool_state->availableSets, validation_error_map[VALIDATION_ERROR_00911]);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001559 }
1560 // Determine whether descriptor counts are satisfiable
1561 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001562 if (ds_data->required_descriptors_by_type[i] > pool_state->availableDescriptorTypeCount[i]) {
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001563 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 -07001564 reinterpret_cast<const uint64_t &>(pool_state->pool), __LINE__, VALIDATION_ERROR_00912, "DS",
1565 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1566 ". This pool only has %d descriptors of this type remaining. %s",
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001567 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Tobin Ehlisc4c4bed2016-11-23 12:23:32 -07001568 reinterpret_cast<uint64_t &>(pool_state->pool), pool_state->availableDescriptorTypeCount[i],
1569 validation_error_map[VALIDATION_ERROR_00912]);
Tobin Ehlisee471462016-05-26 11:21:59 -06001570 }
1571 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001572
Tobin Ehlisee471462016-05-26 11:21:59 -06001573 return skip_call;
1574}
1575// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001576void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1577 const VkDescriptorSet *descriptor_sets,
1578 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06001579 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06001580 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
1581 const core_validation::layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001582 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001583 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001584 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001585 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1586 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1587 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001588 /* Create tracking object for each descriptor set; insert into
1589 * global map and the pool's set.
1590 */
1591 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis93f22372016-10-12 14:34:12 -06001592 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i],
1593 dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001594
1595 pool_state->sets.insert(new_ds);
1596 new_ds->in_use.store(0);
1597 (*set_map)[descriptor_sets[i]] = new_ds;
1598 }
1599}