blob: 44769ced4fe35eb6a7490fe09e1f38f18bace116 [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;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070040 uint32_t insert_index = 0; // Track vector index where we insert element
Tobin Ehlis9637fb22016-12-12 15:59:34 -070041 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;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070044 } else { // out-of-order binding number, need to insert into vector in-order
Tobin Ehlis9637fb22016-12-12 15:59:34 -070045 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 {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700102 for (auto binding_index_pair : binding_to_index_map_) binding_set->insert(binding_index_pair.first);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600103}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600104
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700105VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(
106 const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600107 const auto &bi_itr = binding_to_index_map_.find(binding);
108 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600109 return bindings_[bi_itr->second].ptr();
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600110 }
111 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600112}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700113VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(
114 const uint32_t index) const {
115 if (index >= bindings_.size()) return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600116 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600117}
118// Return descriptorCount for given binding, 0 if index is unavailable
119uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600120 const auto &bi_itr = binding_to_index_map_.find(binding);
121 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600122 return bindings_[bi_itr->second].descriptorCount;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600123 }
124 return 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600125}
126// Return descriptorCount for given index, 0 if index is unavailable
127uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700128 if (index >= bindings_.size()) return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600129 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600130}
131// For the given binding, return descriptorType
132VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const {
133 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600134 const auto &bi_itr = binding_to_index_map_.find(binding);
135 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600136 return bindings_[bi_itr->second].descriptorType;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600137 }
138 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600139}
140// For the given index, return descriptorType
141VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
142 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600143 return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600144}
145// For the given global index, return descriptorType
146// Currently just counting up through bindings_, may improve this in future
147VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const {
148 uint32_t global_offset = 0;
149 for (auto binding : bindings_) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600150 global_offset += binding.descriptorCount;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700151 if (index < global_offset) return binding.descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600152 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700153 assert(0); // requested global index is out of bounds
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600154 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
155}
156// For the given binding, return stageFlags
157VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const {
158 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600159 const auto &bi_itr = binding_to_index_map_.find(binding);
160 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600161 return bindings_[bi_itr->second].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600162 }
163 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600164}
165// For the given binding, return start index
166uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const {
167 assert(binding_to_global_start_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600168 const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding);
169 if (btgsi_itr != binding_to_global_start_index_map_.end()) {
170 return btgsi_itr->second;
171 }
172 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600173 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600174 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600175}
176// For the given binding, return end index
177uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
178 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600179 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
180 if (btgei_itr != binding_to_global_end_index_map_.end()) {
181 return btgei_itr->second;
182 }
183 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600184 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600185 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600186}
187// For given binding, return ptr to ImmutableSampler array
188VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
189 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600190 const auto &bi_itr = binding_to_index_map_.find(binding);
191 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600192 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600193 }
194 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600195}
196// For given index, return ptr to ImmutableSampler array
197VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
198 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600199 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600200}
201// If our layout is compatible with rh_ds_layout, return true,
202// else return false and fill in error_msg will description of what causes incompatibility
203bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
204 // Trivial case
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700205 if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) return true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600206 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
207 std::stringstream error_str;
208 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
209 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
210 *error_msg = error_str.str();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700211 return false; // trivial fail case
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600212 }
213 // Descriptor counts match so need to go through bindings one-by-one
214 // and verify that type and stageFlags match
215 for (auto binding : bindings_) {
216 // TODO : Do we also need to check immutable samplers?
217 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600218 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600219 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600220 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
221 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600222 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600223 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600224 *error_msg = error_str.str();
225 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600226 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600227 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600228 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
229 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600230 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600231 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600232 *error_msg = error_str.str();
233 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600234 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600235 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600236 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
237 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600238 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600239 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600240 *error_msg = error_str.str();
241 return false;
242 }
243 }
244 return true;
245}
246
247bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700248 if (!binding_to_index_map_.count(binding + 1)) return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600249 auto const &bi_itr = binding_to_index_map_.find(binding);
250 if (bi_itr != binding_to_index_map_.end()) {
251 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
252 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600253 auto type = bindings_[bi_itr->second].descriptorType;
254 auto stage_flags = bindings_[bi_itr->second].stageFlags;
255 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
256 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
257 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
258 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600259 return false;
260 }
261 return true;
262 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600263 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600264 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600265}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600266// Starting at offset descriptor of given binding, parse over update_count
267// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
268// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
269// If so, return true. If not, fill in error_msg and return false
270bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
271 const char *type, const VkDescriptorSet set,
272 std::string *error_msg) const {
273 // Verify consecutive bindings match (if needed)
274 auto orig_binding = current_binding;
275 // Track count of descriptors in the current_bindings that are remaining to be updated
276 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
277 // First, it's legal to offset beyond your own binding so handle that case
278 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
279 while (offset >= binding_remaining) {
280 // Advance to next binding, decrement offset by binding size
281 offset -= binding_remaining;
282 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
283 }
284 binding_remaining -= offset;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700285 while (update_count > binding_remaining) { // While our updates overstep current binding
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600286 // Verify next consecutive binding matches type, stage flags & immutable sampler use
287 if (!IsNextBindingConsistent(current_binding++)) {
288 std::stringstream error_str;
289 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
290 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
291 "next binding is not consistent with current binding so this update is invalid.";
292 *error_msg = error_str.str();
293 return false;
294 }
295 // For sake of this check consider the bindings updated and grab count for next binding
296 update_count -= binding_remaining;
297 binding_remaining = GetDescriptorCountFromBinding(current_binding);
298 }
299 return true;
300}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600301
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600302cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
303 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
304
Tobin Ehlis93f22372016-10-12 14:34:12 -0600305cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700306 const DescriptorSetLayout *layout, const layer_data *dev_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700307 : some_update_(false),
308 set_(set),
309 pool_state_(nullptr),
310 p_layout_(layout),
311 device_data_(dev_data),
312 limits_(GetPhysicalDeviceLimits(dev_data)) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700313 pool_state_ = GetDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600314 // Foreach binding, create default descriptors of given type
315 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
316 auto type = p_layout_->GetTypeFromIndex(i);
317 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700318 case VK_DESCRIPTOR_TYPE_SAMPLER: {
319 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
320 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
321 if (immut_sampler)
322 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
323 else
324 descriptors_.emplace_back(new SamplerDescriptor());
325 }
326 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600327 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700328 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
329 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
330 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
331 if (immut)
332 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
333 else
334 descriptors_.emplace_back(new ImageSamplerDescriptor());
335 }
336 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600337 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700338 // ImageDescriptors
339 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
340 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
341 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
342 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
343 descriptors_.emplace_back(new ImageDescriptor(type));
344 break;
345 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
346 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
347 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
348 descriptors_.emplace_back(new TexelDescriptor(type));
349 break;
350 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
351 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
352 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
353 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
354 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
355 descriptors_.emplace_back(new BufferDescriptor(type));
356 break;
357 default:
358 assert(0); // Bad descriptor type specified
359 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600360 }
361 }
362}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600363
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700364cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200365
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700366static std::string string_descriptor_req_view_type(descriptor_req req) {
367 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200368 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
369 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700370 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700371 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200372 }
373 }
374
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700375 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700376
377 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200378}
379
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600380// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
381bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
382 return layout->IsCompatible(p_layout_, error);
383}
Chris Forbes57989132016-07-26 17:06:10 +1200384
Tobin Ehlis3066db62016-08-22 08:12:23 -0600385// 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 -0600386// This includes validating that all descriptors in the given bindings are updated,
387// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
388// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600389bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600390 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200391 for (auto binding_pair : bindings) {
392 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600393 if (!p_layout_->HasBinding(binding)) {
394 std::stringstream error_str;
395 error_str << "Attempting to validate DrawState for binding #" << binding
396 << " which is an invalid binding for this descriptor set.";
397 *error = error_str.str();
398 return false;
399 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600400 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600401 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600402 // Nothing to do for strictly immutable sampler
403 } else {
404 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700405 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700406 for (uint32_t i = start_idx; i <= end_idx; ++i, ++array_idx) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600407 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600408 std::stringstream error_str;
409 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
410 << " is being used in draw but has not been updated.";
411 *error = error_str.str();
412 return false;
413 } else {
Chris Forbes57989132016-07-26 17:06:10 +1200414 auto descriptor_class = descriptors_[i]->GetClass();
415 if (descriptor_class == GeneralBuffer) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600416 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600417 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700418 auto buffer_node = GetBufferState(device_data_, buffer);
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600419 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600420 std::stringstream error_str;
421 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
422 << " references invalid buffer " << buffer << ".";
423 *error = error_str.str();
424 return false;
425 } else {
Tobin Ehlis640a81c2016-11-15 15:37:18 -0700426 for (auto mem_binding : buffer_node->GetBoundMemory()) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700427 if (!GetMemObjInfo(device_data_, mem_binding)) {
Tobin Ehlis640a81c2016-11-15 15:37:18 -0700428 std::stringstream error_str;
429 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
430 << " uses buffer " << buffer << " that references invalid memory " << mem_binding
431 << ".";
432 *error = error_str.str();
433 return false;
434 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600435 }
436 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600437 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600438 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600439 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600440 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
441 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700442 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600443 if (VK_WHOLE_SIZE == range) {
444 if ((dyn_offset + desc_offset) > buffer_size) {
445 std::stringstream error_str;
446 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
447 << " uses buffer " << buffer
448 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
449 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
450 << buffer_size << ".";
451 *error = error_str.str();
452 return false;
453 }
454 } else {
455 if ((dyn_offset + desc_offset + range) > buffer_size) {
456 std::stringstream error_str;
457 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
458 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
459 << " combined with offset " << desc_offset << " and range " << range
460 << " that oversteps the buffer size of " << buffer_size << ".";
461 *error = error_str.str();
462 return false;
463 }
464 }
465 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700466 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
Chris Forbes57989132016-07-26 17:06:10 +1200467 auto image_view = (descriptor_class == ImageSampler)
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700468 ? static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView()
469 : static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
Chris Forbes57989132016-07-26 17:06:10 +1200470 auto reqs = binding_pair.second;
471
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700472 auto image_view_state = GetImageViewState(device_data_, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600473 assert(image_view_state);
474 auto image_view_ci = image_view_state->create_info;
Chris Forbes57989132016-07-26 17:06:10 +1200475
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600476 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
Chris Forbes57989132016-07-26 17:06:10 +1200477 // bad view type
478 std::stringstream error_str;
479 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700480 << " requires an image view of type " << string_descriptor_req_view_type(reqs) << " but got "
481 << string_VkImageViewType(image_view_ci.viewType) << ".";
Chris Forbes57989132016-07-26 17:06:10 +1200482 *error = error_str.str();
483 return false;
484 }
485
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700486 auto image_node = GetImageState(device_data_, image_view_ci.image);
Chris Forbes57989132016-07-26 17:06:10 +1200487 assert(image_node);
488
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700489 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
Chris Forbes57989132016-07-26 17:06:10 +1200490 std::stringstream error_str;
491 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
492 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
493 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
494 *error = error_str.str();
495 return false;
496 }
497
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700498 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
Chris Forbes57989132016-07-26 17:06:10 +1200499 std::stringstream error_str;
500 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
501 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
502 *error = error_str.str();
503 return false;
504 }
505 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600506 }
507 }
508 }
509 }
510 return true;
511}
Chris Forbes57989132016-07-26 17:06:10 +1200512
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600513// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600514uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600515 std::unordered_set<VkBuffer> *buffer_set,
516 std::unordered_set<VkImageView> *image_set) const {
517 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200518 for (auto binding_pair : bindings) {
519 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600520 // If a binding doesn't exist, skip it
521 if (!p_layout_->HasBinding(binding)) {
522 continue;
523 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600524 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600525 if (descriptors_[start_idx]->IsStorage()) {
526 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600527 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600528 if (descriptors_[start_idx + i]->updated) {
529 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600530 num_updates++;
531 }
532 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600533 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600534 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600535 if (descriptors_[start_idx + i]->updated) {
536 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700537 auto bv_state = GetBufferViewState(device_data_, bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600538 if (bv_state) {
539 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600540 num_updates++;
541 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600542 }
543 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600544 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600545 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600546 if (descriptors_[start_idx + i]->updated) {
547 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600548 num_updates++;
549 }
550 }
551 }
552 }
553 }
554 return num_updates;
555}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600556// Set is being deleted or updates so invalidate all bound cmd buffers
557void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Tobin Ehlisfe5731a2016-11-21 08:31:01 -0700558 core_validation::invalidateCommandBuffers(device_data_, cb_bindings,
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600559 {reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600560}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600561// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600562void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700563 // Perform update on a per-binding basis as consecutive updates roll over to next binding
564 auto descriptors_remaining = update->descriptorCount;
565 auto binding_being_updated = update->dstBinding;
566 auto offset = update->dstArrayElement;
567 while (descriptors_remaining) {
568 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
569 auto global_idx = p_layout_->GetGlobalStartIndexFromBinding(binding_being_updated) + offset;
570 // Loop over the updates for a single binding at a time
571 for (uint32_t di = 0; di < update_count; ++di) {
572 descriptors_[global_idx + di]->WriteUpdate(update, di);
573 }
574 // Roll over to next binding in case of consecutive update
575 descriptors_remaining -= update_count;
576 offset = 0;
577 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600578 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700579 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600580
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600581 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600582}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600583// Validate Copy update
584bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600585 const DescriptorSet *src_set, UNIQUE_VALIDATION_ERROR_CODE *error_code,
586 std::string *error_msg) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600587 // Verify idle ds
588 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700589 // TODO : Re-using Free Idle error code, need copy update idle error code
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600590 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600591 std::stringstream error_str;
592 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700593 << " that is in use by a command buffer";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600594 *error_msg = error_str.str();
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600595 return false;
596 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600597 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600598 *error_code = VALIDATION_ERROR_00966;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600599 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700600 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600601 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600602 return false;
603 }
604 if (!src_set->HasBinding(update->srcBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600605 *error_code = VALIDATION_ERROR_00964;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600606 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700607 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600608 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600609 return false;
610 }
611 // src & dst set bindings are valid
612 // Check bounds of src & dst
613 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
614 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
615 // SRC update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600616 *error_code = VALIDATION_ERROR_00965;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600617 std::stringstream error_str;
618 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
619 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
620 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700621 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600622 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600623 return false;
624 }
625 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
626 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
627 // DST update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600628 *error_code = VALIDATION_ERROR_00967;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600629 std::stringstream error_str;
630 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
631 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
632 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700633 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600634 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600635 return false;
636 }
637 // Check that types match
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600638 // TODO : Base default error case going from here is VALIDATION_ERROR_00968 which covers all consistency issues, need more
639 // fine-grained error codes
640 *error_code = VALIDATION_ERROR_00968;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600641 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
642 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
643 if (src_type != dst_type) {
644 std::stringstream error_str;
645 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
646 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700647 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600648 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600649 return false;
650 }
651 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600652 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600653 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600654 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600655 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600656 return false;
657 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600658 // First make sure source descriptors are updated
659 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
660 if (!src_set->descriptors_[src_start_idx + i]) {
661 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700662 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding
663 << " but descriptor at array offset " << update->srcArrayElement + i << " has not been updated";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600664 *error_msg = error_str.str();
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600665 return false;
666 }
667 }
668 // Update parameters all look good and descriptor updated so verify update contents
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700669 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error_code, error_msg)) return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600670
671 // All checks passed so update is good
672 return true;
673}
674// Perform Copy update
675void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600676 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
677 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600678 // Update parameters all look good so perform update
679 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600680 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600681 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700682 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600683
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600684 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600685}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600686
Tobin Ehlisf9519102016-08-17 09:49:13 -0600687// Bind cb_node to this set and this set to cb_node.
688// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
689// to be used in a draw by the given cb_node
Tobin Ehlis276d3d32016-12-21 09:21:06 -0700690void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node,
Tobin Ehlis022528b2016-12-29 12:22:32 -0700691 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600692 // bind cb to this descriptor set
693 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600694 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600695 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 -0600696 pool_state_->cb_bindings.insert(cb_node);
697 cb_node->object_bindings.insert(
698 {reinterpret_cast<uint64_t &>(pool_state_->pool), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT});
Tobin Ehlisf9519102016-08-17 09:49:13 -0600699 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
700 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -0700701 for (auto binding_req_pair : binding_req_map) {
702 auto binding = binding_req_pair.first;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600703 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
704 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
705 for (uint32_t i = start_idx; i <= end_idx; ++i) {
706 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
707 }
708 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600709}
710
Tobin Ehlis300888c2016-05-18 13:43:26 -0600711cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600712 updated = false;
713 descriptor_class = PlainSampler;
714};
715
Tobin Ehlis300888c2016-05-18 13:43:26 -0600716cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600717 updated = false;
718 descriptor_class = PlainSampler;
719 if (immut) {
720 sampler_ = *immut;
721 immutable_ = true;
722 updated = true;
723 }
724}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600725// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700726bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const layer_data *dev_data) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700727 return (GetSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600728}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600729
Tobin Ehlis554bf382016-05-24 11:14:43 -0600730bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700731 const layer_data *dev_data, UNIQUE_VALIDATION_ERROR_CODE *error_code,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600732 std::string *error_msg) {
733 // TODO : Defaulting to 00943 for all cases here. Need to create new error codes for various cases.
734 *error_code = VALIDATION_ERROR_00943;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700735 auto iv_state = GetImageViewState(dev_data, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600736 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600737 std::stringstream error_str;
738 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600739 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600740 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600741 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600742 // 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 -0600743 // Validate that imageLayout is compatible with aspect_mask and image format
744 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600745 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
746 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600747 VkFormat format = VK_FORMAT_MAX_ENUM;
748 VkImageUsageFlags usage = 0;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700749 auto image_node = GetImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600750 if (image_node) {
751 format = image_node->createInfo.format;
752 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600753 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700754 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
755 // the error here occurs is if memory bound to a created imageView has been freed.
Tobin Ehlise1995fc2016-12-22 12:45:09 -0700756 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_02524)) {
Tobin Ehlisde1a0f92016-12-22 12:26:32 -0700757 *error_code = VALIDATION_ERROR_02524;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600758 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600759 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -0600760 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600761 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600762 // Also need to check the swapchains.
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700763 auto swapchain = GetSwapchainFromImage(dev_data, image);
Tobin Ehlis969a5262016-06-02 12:13:32 -0600764 if (swapchain) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700765 auto swapchain_node = GetSwapchainNode(dev_data, swapchain);
Tobin Ehlis4e380592016-06-02 12:41:47 -0600766 if (swapchain_node) {
767 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600768 }
769 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600770 }
771 // First validate that format and layout are compatible
772 if (format == VK_FORMAT_MAX_ENUM) {
773 std::stringstream error_str;
774 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600775 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600776 return false;
777 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600778 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
779 // vkCreateImageView(). What's the best way to create unique id for these cases?
Tobin Ehlis1809f912016-05-25 09:24:36 -0600780 bool ds = vk_format_is_depth_or_stencil(format);
781 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700782 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
783 // Only Color bit must be set
784 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600785 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700786 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
787 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600788 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600789 return false;
790 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700791 // format must NOT be DS
792 if (ds) {
793 std::stringstream error_str;
794 error_str << "ImageView (" << image_view
795 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
796 << string_VkFormat(format) << " which is not a color format.";
797 *error_msg = error_str.str();
798 return false;
799 }
800 break;
801 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
802 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
803 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600804 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
805 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
806 // both must NOT be set
807 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700808 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600809 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600810 return false;
811 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700812 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
813 // Neither were set
814 std::stringstream error_str;
815 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
816 << " but does not have STENCIL or DEPTH aspects set";
817 *error_msg = error_str.str();
818 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600819 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700820 // format must be DS
821 if (!ds) {
822 std::stringstream error_str;
823 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
824 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
825 *error_msg = error_str.str();
826 return false;
827 }
828 break;
829 default:
830 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
831 if (ds) {
832 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
833 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
834 // both must NOT be set
835 std::stringstream error_str;
836 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
837 << " and is using depth/stencil image of format " << string_VkFormat(format)
838 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
839 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
840 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
841 "reads respectively.";
842 *error_msg = error_str.str();
843 return false;
844 }
845 }
846 }
847 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600848 }
849 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -0600850 // 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 -0600851 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
852 // under vkCreateImage()
853 // TODO : Need to also validate case VALIDATION_ERROR_00952 where STORAGE_IMAGE & INPUT_ATTACH types must have been created with
854 // identify swizzle
Tobin Ehlis1809f912016-05-25 09:24:36 -0600855 std::string error_usage_bit;
856 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700857 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
858 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
859 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
860 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
861 }
862 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600863 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700864 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
865 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
866 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
867 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
868 std::stringstream error_str;
869 // TODO : Need to create custom enum error code for this case
870 error_str
871 << "ImageView (" << image_view << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
872 << string_VkImageLayout(image_layout)
873 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage images can "
874 "only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
875 *error_msg = error_str.str();
876 return false;
877 }
878 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600879 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700880 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
881 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
882 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
883 }
884 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600885 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700886 default:
887 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600888 }
889 if (!error_usage_bit.empty()) {
890 std::stringstream error_str;
891 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
892 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
893 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600894 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600895 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600896 }
897 return true;
898}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600899
Tobin Ehlis300888c2016-05-18 13:43:26 -0600900void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
901 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600902 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600903}
904
Tobin Ehlis300888c2016-05-18 13:43:26 -0600905void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600906 if (!immutable_) {
907 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600908 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600909 }
910 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600911}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600912
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700913void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600914 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700915 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700916 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600917 }
918}
919
Tobin Ehlis300888c2016-05-18 13:43:26 -0600920cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
921 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600922 updated = false;
923 descriptor_class = ImageSampler;
924}
925
Tobin Ehlis300888c2016-05-18 13:43:26 -0600926cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
927 : sampler_(VK_NULL_HANDLE), immutable_(true), 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 if (immut) {
931 sampler_ = *immut;
932 immutable_ = true;
933 updated = true;
934 }
935}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600936
Tobin Ehlis300888c2016-05-18 13:43:26 -0600937void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600938 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600939 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600940 sampler_ = image_info.sampler;
941 image_view_ = image_info.imageView;
942 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600943}
944
Tobin Ehlis300888c2016-05-18 13:43:26 -0600945void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600946 if (!immutable_) {
947 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600948 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600949 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600950 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
951 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600952 updated = true;
953 image_view_ = image_view;
954 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600955}
956
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700957void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600958 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600959 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700960 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700961 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600962 }
Tobin Ehlis81e46372016-08-17 13:33:44 -0600963 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700964 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600965 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -0600966 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -0600967 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600968}
969
Tobin Ehlis300888c2016-05-18 13:43:26 -0600970cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
971 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600972 updated = false;
973 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700974 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600975};
976
Tobin Ehlis300888c2016-05-18 13:43:26 -0600977void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600978 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600979 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600980 image_view_ = image_info.imageView;
981 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600982}
983
Tobin Ehlis300888c2016-05-18 13:43:26 -0600984void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600985 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
986 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600987 updated = true;
988 image_view_ = image_view;
989 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600990}
991
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700992void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600993 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700994 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600995 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -0600996 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -0600997 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600998}
999
Tobin Ehlis300888c2016-05-18 13:43:26 -06001000cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1001 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001002 updated = false;
1003 descriptor_class = GeneralBuffer;
1004 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1005 dynamic_ = true;
1006 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1007 storage_ = true;
1008 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1009 dynamic_ = true;
1010 storage_ = true;
1011 }
1012}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001013void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001014 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001015 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001016 buffer_ = buffer_info.buffer;
1017 offset_ = buffer_info.offset;
1018 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001019}
1020
Tobin Ehlis300888c2016-05-18 13:43:26 -06001021void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1022 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001023 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001024 buffer_ = buff_desc->buffer_;
1025 offset_ = buff_desc->offset_;
1026 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001027}
1028
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001029void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001030 auto buffer_node = GetBufferState(dev_data, buffer_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001031 if (buffer_node) core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001032}
1033
Tobin Ehlis300888c2016-05-18 13:43:26 -06001034cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001035 updated = false;
1036 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001037 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001038};
Tobin Ehlis56a30942016-05-19 08:00:00 -06001039
Tobin Ehlis300888c2016-05-18 13:43:26 -06001040void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001041 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001042 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001043}
1044
Tobin Ehlis300888c2016-05-18 13:43:26 -06001045void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1046 updated = true;
1047 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1048}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001049
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001050void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001051 auto bv_state = GetBufferViewState(dev_data, buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001052 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001053 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001054 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001055}
1056
Tobin Ehlis300888c2016-05-18 13:43:26 -06001057// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1058// sets, and then calls their respective Validate[Write|Copy]Update functions.
1059// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1060// be skipped, then true is returned.
1061// If there is no issue with the update, then false is returned.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001062bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data, const layer_data *dev_data,
1063 uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001064 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001065 bool skip_call = false;
1066 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001067 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001068 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001069 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001070 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001071 skip_call |=
1072 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 -06001073 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001074 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
1075 reinterpret_cast<uint64_t &>(dest_set));
1076 } else {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001077 UNIQUE_VALIDATION_ERROR_CODE error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001078 std::string error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001079 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_code, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001080 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 -06001081 reinterpret_cast<uint64_t &>(dest_set), __LINE__, error_code, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001082 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001083 " with error: %s. %s",
1084 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str(), validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001085 }
1086 }
1087 }
1088 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001089 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001090 auto dst_set = p_cds[i].dstSet;
1091 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001092 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1093 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001094 // Object_tracker verifies that src & dest descriptor set are valid
1095 assert(src_node);
1096 assert(dst_node);
1097 UNIQUE_VALIDATION_ERROR_CODE error_code;
1098 std::string error_str;
1099 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_code, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001100 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 -07001101 reinterpret_cast<uint64_t &>(dst_set), __LINE__, error_code, "DS",
1102 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
1103 " to Descriptor Set 0x%" PRIx64 " with error: %s. %s",
1104 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str(),
1105 validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001106 }
1107 }
1108 return skip_call;
1109}
1110// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1111// sets, and then calls their respective Perform[Write|Copy]Update functions.
1112// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1113// with the same set of updates.
1114// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1115// calling down the chain.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001116void cvdescriptorset::PerformUpdateDescriptorSets(const layer_data *dev_data, uint32_t write_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001117 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1118 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001119 // Write updates first
1120 uint32_t i = 0;
1121 for (i = 0; i < write_count; ++i) {
1122 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001123 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001124 if (set_node) {
1125 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001126 }
1127 }
1128 // Now copy updates
1129 for (i = 0; i < copy_count; ++i) {
1130 auto dst_set = p_cds[i].dstSet;
1131 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001132 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1133 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001134 if (src_node && dst_node) {
1135 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001136 }
1137 }
1138}
1139// Validate the state for a given write update but don't actually perform the update
1140// If an error would occur for this update, return false and fill in details in error_msg string
1141bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001142 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001143 // Verify idle ds
1144 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001145 // TODO : Re-using Free Idle error code, need write update idle error code
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001146 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001147 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001148 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001149 << " that is in use by a command buffer";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001150 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001151 return false;
1152 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001153 // Verify dst binding exists
1154 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001155 *error_code = VALIDATION_ERROR_00936;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001156 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001157 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001158 *error_msg = error_str.str();
1159 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001160 } else {
1161 // Make sure binding isn't empty
1162 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
1163 *error_code = VALIDATION_ERROR_02348;
1164 std::stringstream error_str;
1165 error_str << "DescriptorSet " << set_ << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
1166 *error_msg = error_str.str();
1167 return false;
1168 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001169 }
1170 // We know that binding is valid, verify update and do update on each descriptor
1171 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
1172 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
1173 if (type != update->descriptorType) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001174 *error_code = VALIDATION_ERROR_00937;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001175 std::stringstream error_str;
1176 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1177 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1178 *error_msg = error_str.str();
1179 return false;
1180 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001181 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001182 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001183 std::stringstream error_str;
1184 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001185 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001186 << " descriptors in that binding and all successive bindings of the set, but update of "
1187 << update->descriptorCount << " descriptors combined with update array element offset of "
1188 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001189 *error_msg = error_str.str();
1190 return false;
1191 }
1192 // Verify consecutive bindings match (if needed)
1193 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001194 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07001195 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001196 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001197 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001198 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001199 // Update is within bounds and consistent so last step is to validate update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001200 if (!VerifyWriteUpdateContents(update, start_idx, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001201 std::stringstream error_str;
1202 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1203 << " failed with error message: " << error_msg->c_str();
1204 *error_msg = error_str.str();
1205 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001206 }
1207 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001208 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001209}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001210// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001211// 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 -07001212bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001213 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001214 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001215 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001216 std::string error_usage_bit;
1217 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001218 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1219 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
1220 *error_code = VALIDATION_ERROR_00950;
1221 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1222 }
1223 break;
1224 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1225 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1226 *error_code = VALIDATION_ERROR_00951;
1227 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1228 }
1229 break;
1230 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1231 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1232 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1233 *error_code = VALIDATION_ERROR_00946;
1234 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1235 }
1236 break;
1237 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1238 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1239 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1240 *error_code = VALIDATION_ERROR_00947;
1241 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1242 }
1243 break;
1244 default:
1245 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001246 }
1247 if (!error_usage_bit.empty()) {
1248 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001249 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1250 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1251 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001252 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001253 return false;
1254 }
1255 return true;
1256}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001257// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1258// 1. buffer is valid
1259// 2. buffer was created with correct usage flags
1260// 3. offset is less than buffer size
1261// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001262// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001263// 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 -06001264bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001265 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001266 // First make sure that buffer is valid
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001267 auto buffer_node = GetBufferState(device_data_, buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07001268 // Any invalid buffer should already be caught by object_tracker
1269 assert(buffer_node);
Tobin Ehlise1995fc2016-12-22 12:45:09 -07001270 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_02525)) {
Tobin Ehlisde1a0f92016-12-22 12:26:32 -07001271 *error_code = VALIDATION_ERROR_02525;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001272 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001273 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001274 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001275 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001276 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1277 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001278 return false;
1279 }
1280 // offset must be less than buffer size
1281 if (buffer_info->offset > buffer_node->createInfo.size) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001282 *error_code = VALIDATION_ERROR_00959;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001283 std::stringstream error_str;
1284 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than buffer " << buffer_node->buffer
1285 << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001286 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001287 return false;
1288 }
1289 if (buffer_info->range != VK_WHOLE_SIZE) {
1290 // Range must be VK_WHOLE_SIZE or > 0
1291 if (!buffer_info->range) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001292 *error_code = VALIDATION_ERROR_00960;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001293 std::stringstream error_str;
1294 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001295 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001296 return false;
1297 }
1298 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1299 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001300 *error_code = VALIDATION_ERROR_00961;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001301 std::stringstream error_str;
1302 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1303 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001304 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001305 return false;
1306 }
1307 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001308 // Check buffer update sizes against device limits
1309 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1310 auto max_ub_range = limits_.maxUniformBufferRange;
1311 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1312 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
1313 *error_code = VALIDATION_ERROR_00948;
1314 std::stringstream error_str;
1315 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1316 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
1317 *error_msg = error_str.str();
1318 return false;
1319 }
1320 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1321 auto max_sb_range = limits_.maxStorageBufferRange;
1322 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1323 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
1324 *error_code = VALIDATION_ERROR_00949;
1325 std::stringstream error_str;
1326 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1327 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
1328 *error_msg = error_str.str();
1329 return false;
1330 }
1331 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001332 return true;
1333}
1334
Tobin Ehlis300888c2016-05-18 13:43:26 -06001335// Verify that the contents of the update are ok, but don't perform actual update
1336bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001337 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1338 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001339 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001340 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1341 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1342 // Validate image
1343 auto image_view = update->pImageInfo[di].imageView;
1344 auto image_layout = update->pImageInfo[di].imageLayout;
1345 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001346 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001347 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
1348 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001349 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001350 return false;
1351 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001352 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001353 // Intentional fall-through to validate sampler
Tobin Ehlis300888c2016-05-18 13:43:26 -06001354 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001355 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1356 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1357 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
1358 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
1359 *error_code = VALIDATION_ERROR_00942;
1360 std::stringstream error_str;
1361 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1362 << update->pImageInfo[di].sampler << ".";
1363 *error_msg = error_str.str();
1364 return false;
1365 }
1366 } else {
1367 // TODO : Warn here
1368 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001369 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001370 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001371 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001372 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1373 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1374 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1375 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1376 auto image_view = update->pImageInfo[di].imageView;
1377 auto image_layout = update->pImageInfo[di].imageLayout;
1378 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
1379 std::stringstream error_str;
1380 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
1381 *error_msg = error_str.str();
1382 return false;
1383 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001384 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001385 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001386 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001387 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1388 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1389 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1390 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001391 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001392 if (!bv_state) {
1393 *error_code = VALIDATION_ERROR_00940;
1394 std::stringstream error_str;
1395 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1396 *error_msg = error_str.str();
1397 return false;
1398 }
1399 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001400 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001401 std::stringstream error_str;
1402 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
1403 *error_msg = error_str.str();
1404 return false;
1405 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001406 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001407 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001408 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001409 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1410 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1411 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1412 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1413 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1414 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error_code, error_msg)) {
1415 std::stringstream error_str;
1416 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
1417 *error_msg = error_str.str();
1418 return false;
1419 }
1420 }
1421 break;
1422 }
1423 default:
1424 assert(0); // We've already verified update type so should never get here
1425 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001426 }
1427 // All checks passed so update contents are good
1428 return true;
1429}
1430// Verify that the contents of the update are ok, but don't perform actual update
1431bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001432 VkDescriptorType type, uint32_t index,
1433 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1434 std::string *error_msg) const {
1435 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
1436 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06001437 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001438 case PlainSampler: {
1439 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1440 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1441 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
1442 if (!ValidateSampler(update_sampler, device_data_)) {
1443 *error_code = VALIDATION_ERROR_00942;
1444 std::stringstream error_str;
1445 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1446 *error_msg = error_str.str();
1447 return false;
1448 }
1449 } else {
1450 // TODO : Warn here
1451 }
1452 }
1453 break;
1454 }
1455 case ImageSampler: {
1456 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1457 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1458 // First validate sampler
1459 if (!img_samp_desc->IsImmutableSampler()) {
1460 auto update_sampler = img_samp_desc->GetSampler();
1461 if (!ValidateSampler(update_sampler, device_data_)) {
1462 *error_code = VALIDATION_ERROR_00942;
1463 std::stringstream error_str;
1464 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1465 *error_msg = error_str.str();
1466 return false;
1467 }
1468 } else {
1469 // TODO : Warn here
1470 }
1471 // Validate image
1472 auto image_view = img_samp_desc->GetImageView();
1473 auto image_layout = img_samp_desc->GetImageLayout();
1474 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001475 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001476 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001477 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001478 return false;
1479 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001480 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001481 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001482 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001483 case Image: {
1484 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1485 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1486 auto image_view = img_desc->GetImageView();
1487 auto image_layout = img_desc->GetImageLayout();
1488 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001489 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001490 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001491 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001492 return false;
1493 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001494 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001495 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001496 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001497 case TexelBuffer: {
1498 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1499 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001500 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001501 if (!bv_state) {
1502 *error_code = VALIDATION_ERROR_00940;
1503 std::stringstream error_str;
1504 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1505 *error_msg = error_str.str();
1506 return false;
1507 }
1508 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001509 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001510 std::stringstream error_str;
1511 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
1512 *error_msg = error_str.str();
1513 return false;
1514 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001515 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001516 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001517 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001518 case GeneralBuffer: {
1519 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1520 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001521 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001522 std::stringstream error_str;
1523 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
1524 *error_msg = error_str.str();
1525 return false;
1526 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001527 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001528 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001529 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001530 default:
1531 assert(0); // We've already verified update type so should never get here
1532 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001533 }
1534 // All checks passed so update contents are good
1535 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001536}
Tobin Ehlisee471462016-05-26 11:21:59 -06001537// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlis815e8132016-06-02 13:02:17 -06001538bool cvdescriptorset::ValidateAllocateDescriptorSets(const debug_report_data *report_data,
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001539 const VkDescriptorSetAllocateInfo *p_alloc_info, const layer_data *dev_data,
Tobin Ehlis815e8132016-06-02 13:02:17 -06001540 AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001541 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001542
1543 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001544 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
Tobin Ehlis815e8132016-06-02 13:02:17 -06001545 if (!layout) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001546 skip_call |=
1547 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1548 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1549 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1550 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001551 } else {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001552 ds_data->layout_nodes[i] = layout;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001553 // Count total descriptors required per type
Tobin Ehlis815e8132016-06-02 13:02:17 -06001554 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1555 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001556 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1557 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1558 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001559 }
1560 }
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001561 auto pool_state = GetDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001562 // Track number of descriptorSets allowable in this pool
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001563 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Tobin Ehlisc4c4bed2016-11-23 12:23:32 -07001564 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1565 reinterpret_cast<uint64_t &>(pool_state->pool), __LINE__, VALIDATION_ERROR_00911, "DS",
1566 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1567 ". This pool only has %d descriptorSets remaining. %s",
1568 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_state->pool),
1569 pool_state->availableSets, validation_error_map[VALIDATION_ERROR_00911]);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001570 }
1571 // Determine whether descriptor counts are satisfiable
1572 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001573 if (ds_data->required_descriptors_by_type[i] > pool_state->availableDescriptorTypeCount[i]) {
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001574 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 -07001575 reinterpret_cast<const uint64_t &>(pool_state->pool), __LINE__, VALIDATION_ERROR_00912, "DS",
1576 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1577 ". This pool only has %d descriptors of this type remaining. %s",
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001578 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Tobin Ehlisc4c4bed2016-11-23 12:23:32 -07001579 reinterpret_cast<uint64_t &>(pool_state->pool), pool_state->availableDescriptorTypeCount[i],
1580 validation_error_map[VALIDATION_ERROR_00912]);
Tobin Ehlisee471462016-05-26 11:21:59 -06001581 }
1582 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001583
Tobin Ehlisee471462016-05-26 11:21:59 -06001584 return skip_call;
1585}
1586// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001587void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1588 const VkDescriptorSet *descriptor_sets,
1589 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06001590 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06001591 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001592 const layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001593 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001594 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001595 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001596 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1597 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1598 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001599 /* Create tracking object for each descriptor set; insert into
1600 * global map and the pool's set.
1601 */
1602 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis93f22372016-10-12 14:34:12 -06001603 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i],
1604 dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001605
1606 pool_state->sets.insert(new_ds);
1607 new_ds->in_use.store(0);
1608 (*set_map)[descriptor_sets[i]] = new_ds;
1609 }
1610}