blob: d2315438525762deb9c7b823dfb1adb7bd767621 [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
21#include "descriptor_sets.h"
22#include "vk_enum_string_helper.h"
23#include "vk_safe_struct.h"
24#include <sstream>
25
26// Construct DescriptorSetLayout instance from given create info
Tobin Ehlis154c2692016-10-25 09:36:53 -060027cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060028 const VkDescriptorSetLayout layout)
29 : layout_(layout), binding_count_(p_create_info->bindingCount), descriptor_count_(0), dynamic_descriptor_count_(0) {
30 uint32_t global_index = 0;
Tobin Ehlisa3525e02016-11-17 10:50:52 -070031 // Dyn array indicies are ordered by binding # and array index of any array within the binding
32 // so we store up bindings w/ count in ordered map in order to create dyn array mappings below
33 std::map<uint32_t, uint32_t> binding_to_dyn_count;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060034 for (uint32_t i = 0; i < binding_count_; ++i) {
35 descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
Tobin Ehlisfdcb63f2016-10-25 20:56:47 -060036 binding_to_index_map_[p_create_info->pBindings[i].binding] = i;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060037 binding_to_global_start_index_map_[p_create_info->pBindings[i].binding] = global_index;
38 global_index += p_create_info->pBindings[i].descriptorCount ? p_create_info->pBindings[i].descriptorCount - 1 : 0;
39 binding_to_global_end_index_map_[p_create_info->pBindings[i].binding] = global_index;
Tobin Ehlis789c4ed2016-10-17 13:51:57 -060040 global_index += p_create_info->pBindings[i].descriptorCount ? 1 : 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -060041 bindings_.push_back(safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i]));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060042 // In cases where we should ignore pImmutableSamplers make sure it's NULL
43 if ((p_create_info->pBindings[i].pImmutableSamplers) &&
44 ((p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_SAMPLER) &&
45 (p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER))) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060046 bindings_.back().pImmutableSamplers = nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060047 }
48 if (p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
49 p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
Tobin Ehlisa3525e02016-11-17 10:50:52 -070050 binding_to_dyn_count[p_create_info->pBindings[i].binding] = p_create_info->pBindings[i].descriptorCount;
Tobin Ehlisef0de162016-06-20 13:07:34 -060051 dynamic_descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060052 }
53 }
Tobin Ehlisa3525e02016-11-17 10:50:52 -070054 // Now create dyn offset array mapping for any dynamic descriptors
55 uint32_t dyn_array_idx = 0;
56 for (const auto &bc_pair : binding_to_dyn_count) {
57 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
58 dyn_array_idx += bc_pair.second;
59 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060060}
Tobin Ehlis154c2692016-10-25 09:36:53 -060061
62// Validate descriptor set layout create info
63bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(debug_report_data *report_data,
64 const VkDescriptorSetLayoutCreateInfo *create_info) {
65 bool skip = false;
66 std::unordered_set<uint32_t> bindings;
67 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
Tobin Ehlisfdcb63f2016-10-25 20:56:47 -060068 if (!bindings.insert(create_info->pBindings[i].binding).second) {
Tobin Ehlis154c2692016-10-25 09:36:53 -060069 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
70 VALIDATION_ERROR_02345, "DS", "duplicated binding number in VkDescriptorSetLayoutBinding. %s",
71 validation_error_map[VALIDATION_ERROR_02345]);
72 }
Tobin Ehlis154c2692016-10-25 09:36:53 -060073 }
74 return skip;
75}
76
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060077// put all bindings into the given set
78void cvdescriptorset::DescriptorSetLayout::FillBindingSet(std::unordered_set<uint32_t> *binding_set) const {
79 for (auto binding_index_pair : binding_to_index_map_)
80 binding_set->insert(binding_index_pair.first);
81}
Tobin Ehlis56a30942016-05-19 08:00:00 -060082
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060083VkDescriptorSetLayoutBinding const *
84cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060085 const auto &bi_itr = binding_to_index_map_.find(binding);
86 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060087 return bindings_[bi_itr->second].ptr();
Tobin Ehlis0bc30632016-05-05 10:16:02 -060088 }
89 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060090}
91VkDescriptorSetLayoutBinding const *
92cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const {
93 if (index >= bindings_.size())
94 return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -060095 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060096}
97// Return descriptorCount for given binding, 0 if index is unavailable
98uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060099 const auto &bi_itr = binding_to_index_map_.find(binding);
100 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600101 return bindings_[bi_itr->second].descriptorCount;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600102 }
103 return 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600104}
105// Return descriptorCount for given index, 0 if index is unavailable
106uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
107 if (index >= bindings_.size())
108 return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600109 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600110}
111// For the given binding, return descriptorType
112VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const {
113 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600114 const auto &bi_itr = binding_to_index_map_.find(binding);
115 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600116 return bindings_[bi_itr->second].descriptorType;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600117 }
118 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600119}
120// For the given index, return descriptorType
121VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
122 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600123 return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600124}
125// For the given global index, return descriptorType
126// Currently just counting up through bindings_, may improve this in future
127VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const {
128 uint32_t global_offset = 0;
129 for (auto binding : bindings_) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600130 global_offset += binding.descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600131 if (index < global_offset)
Tobin Ehlis664e6012016-05-05 11:04:44 -0600132 return binding.descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600133 }
134 assert(0); // requested global index is out of bounds
135 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
136}
137// For the given binding, return stageFlags
138VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const {
139 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600140 const auto &bi_itr = binding_to_index_map_.find(binding);
141 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600142 return bindings_[bi_itr->second].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600143 }
144 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600145}
146// For the given binding, return start index
147uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const {
148 assert(binding_to_global_start_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600149 const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding);
150 if (btgsi_itr != binding_to_global_start_index_map_.end()) {
151 return btgsi_itr->second;
152 }
153 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600154 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600155 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600156}
157// For the given binding, return end index
158uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
159 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600160 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
161 if (btgei_itr != binding_to_global_end_index_map_.end()) {
162 return btgei_itr->second;
163 }
164 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600165 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600166 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600167}
168// For given binding, return ptr to ImmutableSampler array
169VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
170 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600171 const auto &bi_itr = binding_to_index_map_.find(binding);
172 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600173 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600174 }
175 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600176}
177// For given index, return ptr to ImmutableSampler array
178VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
179 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600180 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600181}
182// If our layout is compatible with rh_ds_layout, return true,
183// else return false and fill in error_msg will description of what causes incompatibility
184bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
185 // Trivial case
186 if (layout_ == rh_ds_layout->GetDescriptorSetLayout())
187 return true;
188 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
189 std::stringstream error_str;
190 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
191 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
192 *error_msg = error_str.str();
193 return false; // trivial fail case
194 }
195 // Descriptor counts match so need to go through bindings one-by-one
196 // and verify that type and stageFlags match
197 for (auto binding : bindings_) {
198 // TODO : Do we also need to check immutable samplers?
199 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600200 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600201 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600202 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
203 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600204 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600205 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600206 *error_msg = error_str.str();
207 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600208 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600209 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600210 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
211 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600212 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600213 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600214 *error_msg = error_str.str();
215 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600216 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600217 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600218 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
219 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600220 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600221 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600222 *error_msg = error_str.str();
223 return false;
224 }
225 }
226 return true;
227}
228
229bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
230 if (!binding_to_index_map_.count(binding + 1))
231 return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600232 auto const &bi_itr = binding_to_index_map_.find(binding);
233 if (bi_itr != binding_to_index_map_.end()) {
234 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
235 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600236 auto type = bindings_[bi_itr->second].descriptorType;
237 auto stage_flags = bindings_[bi_itr->second].stageFlags;
238 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
239 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
240 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
241 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600242 return false;
243 }
244 return true;
245 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600246 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600247 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600248}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600249// Starting at offset descriptor of given binding, parse over update_count
250// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
251// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
252// If so, return true. If not, fill in error_msg and return false
253bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
254 const char *type, const VkDescriptorSet set,
255 std::string *error_msg) const {
256 // Verify consecutive bindings match (if needed)
257 auto orig_binding = current_binding;
258 // Track count of descriptors in the current_bindings that are remaining to be updated
259 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
260 // First, it's legal to offset beyond your own binding so handle that case
261 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
262 while (offset >= binding_remaining) {
263 // Advance to next binding, decrement offset by binding size
264 offset -= binding_remaining;
265 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
266 }
267 binding_remaining -= offset;
268 while (update_count > binding_remaining) { // While our updates overstep current binding
269 // Verify next consecutive binding matches type, stage flags & immutable sampler use
270 if (!IsNextBindingConsistent(current_binding++)) {
271 std::stringstream error_str;
272 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
273 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
274 "next binding is not consistent with current binding so this update is invalid.";
275 *error_msg = error_str.str();
276 return false;
277 }
278 // For sake of this check consider the bindings updated and grab count for next binding
279 update_count -= binding_remaining;
280 binding_remaining = GetDescriptorCountFromBinding(current_binding);
281 }
282 return true;
283}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600284
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600285cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
286 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
287
Tobin Ehlis93f22372016-10-12 14:34:12 -0600288cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
289 const DescriptorSetLayout *layout, const core_validation::layer_data *dev_data)
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600290 : some_update_(false), set_(set), pool_state_(nullptr), p_layout_(layout), device_data_(dev_data) {
291 pool_state_ = getDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600292 // Foreach binding, create default descriptors of given type
293 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
294 auto type = p_layout_->GetTypeFromIndex(i);
295 switch (type) {
296 case VK_DESCRIPTOR_TYPE_SAMPLER: {
297 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
298 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
299 if (immut_sampler)
Chris Forbescb621ea2016-05-30 11:47:31 +1200300 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600301 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200302 descriptors_.emplace_back(new SamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600303 }
304 break;
305 }
306 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
307 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
308 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
309 if (immut)
Chris Forbescb621ea2016-05-30 11:47:31 +1200310 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600311 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200312 descriptors_.emplace_back(new ImageSamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600313 }
314 break;
315 }
316 // ImageDescriptors
317 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
318 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
319 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
320 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200321 descriptors_.emplace_back(new ImageDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600322 break;
323 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
324 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
325 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200326 descriptors_.emplace_back(new TexelDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600327 break;
328 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
329 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
330 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
331 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
332 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200333 descriptors_.emplace_back(new BufferDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600334 break;
335 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600336 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600337 break;
338 }
339 }
340}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600341
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600342cvdescriptorset::DescriptorSet::~DescriptorSet() {
343 InvalidateBoundCmdBuffers();
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600344}
Chris Forbes57989132016-07-26 17:06:10 +1200345
346
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700347static std::string string_descriptor_req_view_type(descriptor_req req) {
348 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200349 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
350 if (req & (1 << i)) {
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700351 if (result.size()) result += ", ";
352 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200353 }
354 }
355
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700356 if (!result.size())
357 result = "(none)";
358
359 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200360}
361
362
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600363// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
364bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
365 return layout->IsCompatible(p_layout_, error);
366}
Chris Forbes57989132016-07-26 17:06:10 +1200367
Tobin Ehlis3066db62016-08-22 08:12:23 -0600368// 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 -0600369// This includes validating that all descriptors in the given bindings are updated,
370// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
371// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600372bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600373 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200374 for (auto binding_pair : bindings) {
375 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600376 if (!p_layout_->HasBinding(binding)) {
377 std::stringstream error_str;
378 error_str << "Attempting to validate DrawState for binding #" << binding
379 << " which is an invalid binding for this descriptor set.";
380 *error = error_str.str();
381 return false;
382 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600383 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600384 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600385 // Nothing to do for strictly immutable sampler
386 } else {
387 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700388 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
389 for (uint32_t i = start_idx; i <= end_idx; ++i, ++array_idx) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600390 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600391 std::stringstream error_str;
392 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
393 << " is being used in draw but has not been updated.";
394 *error = error_str.str();
395 return false;
396 } else {
Chris Forbes57989132016-07-26 17:06:10 +1200397 auto descriptor_class = descriptors_[i]->GetClass();
398 if (descriptor_class == GeneralBuffer) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600399 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600400 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600401 auto buffer_node = getBufferNode(device_data_, buffer);
402 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600403 std::stringstream error_str;
404 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
405 << " references invalid buffer " << buffer << ".";
406 *error = error_str.str();
407 return false;
408 } else {
Tobin Ehlis54108272016-10-11 14:26:49 -0600409 auto mem_entry = getMemObjInfo(device_data_, buffer_node->binding.mem);
Tobin Ehlis997b2582016-06-02 08:43:37 -0600410 if (!mem_entry) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600411 std::stringstream error_str;
412 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Tobin Ehlis54108272016-10-11 14:26:49 -0600413 << " uses buffer " << buffer << " that references invalid memory "
414 << buffer_node->binding.mem << ".";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600415 *error = error_str.str();
416 return false;
417 }
418 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600419 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600420 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600421 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600422 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
423 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700424 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600425 if (VK_WHOLE_SIZE == range) {
426 if ((dyn_offset + desc_offset) > buffer_size) {
427 std::stringstream error_str;
428 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
429 << " uses buffer " << buffer
430 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
431 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
432 << buffer_size << ".";
433 *error = error_str.str();
434 return false;
435 }
436 } else {
437 if ((dyn_offset + desc_offset + range) > buffer_size) {
438 std::stringstream error_str;
439 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
440 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
441 << " combined with offset " << desc_offset << " and range " << range
442 << " that oversteps the buffer size of " << buffer_size << ".";
443 *error = error_str.str();
444 return false;
445 }
446 }
447 }
448 }
Chris Forbes57989132016-07-26 17:06:10 +1200449 else if (descriptor_class == ImageSampler || descriptor_class == Image) {
450 auto image_view = (descriptor_class == ImageSampler)
451 ? static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView()
452 : static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
453 auto reqs = binding_pair.second;
454
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600455 auto image_view_state = getImageViewState(device_data_, image_view);
456 assert(image_view_state);
457 auto image_view_ci = image_view_state->create_info;
Chris Forbes57989132016-07-26 17:06:10 +1200458
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600459 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
Chris Forbes57989132016-07-26 17:06:10 +1200460 // bad view type
461 std::stringstream error_str;
462 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
463 << " requires an image view of type " << string_descriptor_req_view_type(reqs)
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600464 << " but got " << string_VkImageViewType(image_view_ci.viewType) << ".";
Chris Forbes57989132016-07-26 17:06:10 +1200465 *error = error_str.str();
466 return false;
467 }
468
Tobin Ehlis30df15c2016-10-12 17:17:57 -0600469 auto image_node = getImageState(device_data_, image_view_ci.image);
Chris Forbes57989132016-07-26 17:06:10 +1200470 assert(image_node);
471
472 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) &&
473 image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
474 std::stringstream error_str;
475 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
476 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
477 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
478 *error = error_str.str();
479 return false;
480 }
481
482 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) &&
483 image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
484 std::stringstream error_str;
485 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
486 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
487 *error = error_str.str();
488 return false;
489 }
490 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600491 }
492 }
493 }
494 }
495 return true;
496}
Chris Forbes57989132016-07-26 17:06:10 +1200497
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600498// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600499uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600500 std::unordered_set<VkBuffer> *buffer_set,
501 std::unordered_set<VkImageView> *image_set) const {
502 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200503 for (auto binding_pair : bindings) {
504 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600505 // If a binding doesn't exist, skip it
506 if (!p_layout_->HasBinding(binding)) {
507 continue;
508 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600509 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600510 if (descriptors_[start_idx]->IsStorage()) {
511 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600512 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600513 if (descriptors_[start_idx + i]->updated) {
514 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600515 num_updates++;
516 }
517 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600518 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600519 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600520 if (descriptors_[start_idx + i]->updated) {
521 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis8b872462016-09-14 08:12:08 -0600522 auto bv_state = getBufferViewState(device_data_, bufferview);
523 if (bv_state) {
524 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600525 num_updates++;
526 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600527 }
528 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600529 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600530 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600531 if (descriptors_[start_idx + i]->updated) {
532 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600533 num_updates++;
534 }
535 }
536 }
537 }
538 }
539 return num_updates;
540}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600541// Set is being deleted or updates so invalidate all bound cmd buffers
542void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600543 core_validation::invalidateCommandBuffers(cb_bindings,
544 {reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600545}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600546// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600547void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600548 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
549 // perform update
550 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
551 descriptors_[start_idx + di]->WriteUpdate(update, di);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600552 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600553 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600554 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600555
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600556 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600557}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600558// Validate Copy update
559bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600560 const DescriptorSet *src_set, UNIQUE_VALIDATION_ERROR_CODE *error_code,
561 std::string *error_msg) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600562 // Verify idle ds
563 if (in_use.load()) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600564 // TODO : Re-using Allocate Idle error code, need copy update idle error code
565 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600566 std::stringstream error_str;
567 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
568 << " that is in use by a command buffer.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600569 *error_msg = error_str.str();
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600570 return false;
571 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600572 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600573 *error_code = VALIDATION_ERROR_00966;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600574 std::stringstream error_str;
575 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600576 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600577 return false;
578 }
579 if (!src_set->HasBinding(update->srcBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600580 *error_code = VALIDATION_ERROR_00964;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600581 std::stringstream error_str;
582 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600583 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600584 return false;
585 }
586 // src & dst set bindings are valid
587 // Check bounds of src & dst
588 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
589 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
590 // SRC update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600591 *error_code = VALIDATION_ERROR_00965;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600592 std::stringstream error_str;
593 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
594 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
595 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
596 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600597 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600598 return false;
599 }
600 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
601 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
602 // DST update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600603 *error_code = VALIDATION_ERROR_00967;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600604 std::stringstream error_str;
605 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
606 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
607 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
608 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600609 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600610 return false;
611 }
612 // Check that types match
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600613 // TODO : Base default error case going from here is VALIDATION_ERROR_00968 which covers all consistency issues, need more
614 // fine-grained error codes
615 *error_code = VALIDATION_ERROR_00968;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600616 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
617 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
618 if (src_type != dst_type) {
619 std::stringstream error_str;
620 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
621 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
622 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600623 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600624 return false;
625 }
626 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600627 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600628 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600629 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600630 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600631 return false;
632 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600633 // First make sure source descriptors are updated
634 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
635 if (!src_set->descriptors_[src_start_idx + i]) {
636 std::stringstream error_str;
637 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
638 << update->srcArrayElement + i << " has not been updated.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600639 *error_msg = error_str.str();
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600640 return false;
641 }
642 }
643 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600644 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error_code, error_msg))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600645 return false;
646
647 // All checks passed so update is good
648 return true;
649}
650// Perform Copy update
651void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600652 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
653 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600654 // Update parameters all look good so perform update
655 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600656 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600657 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600658 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600659 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600660
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600661 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600662}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600663
Tobin Ehlisf9519102016-08-17 09:49:13 -0600664// Bind cb_node to this set and this set to cb_node.
665// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
666// to be used in a draw by the given cb_node
Tobin Ehlisf9519102016-08-17 09:49:13 -0600667void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node, const std::unordered_set<uint32_t> &bindings) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600668 // bind cb to this descriptor set
669 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600670 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600671 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 -0600672 pool_state_->cb_bindings.insert(cb_node);
673 cb_node->object_bindings.insert(
674 {reinterpret_cast<uint64_t &>(pool_state_->pool), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT});
Tobin Ehlisf9519102016-08-17 09:49:13 -0600675 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
676 // resources
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600677 for (auto binding : bindings) {
678 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
679 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
680 for (uint32_t i = start_idx; i <= end_idx; ++i) {
681 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
682 }
683 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600684}
685
Tobin Ehlis300888c2016-05-18 13:43:26 -0600686cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600687 updated = false;
688 descriptor_class = PlainSampler;
689};
690
Tobin Ehlis300888c2016-05-18 13:43:26 -0600691cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600692 updated = false;
693 descriptor_class = PlainSampler;
694 if (immut) {
695 sampler_ = *immut;
696 immutable_ = true;
697 updated = true;
698 }
699}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600700// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
701bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
Tobin Ehlisfad7adf2016-10-20 06:50:37 -0600702 return (getSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600703}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600704
Tobin Ehlis554bf382016-05-24 11:14:43 -0600705bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600706 const core_validation::layer_data *dev_data, UNIQUE_VALIDATION_ERROR_CODE *error_code,
707 std::string *error_msg) {
708 // TODO : Defaulting to 00943 for all cases here. Need to create new error codes for various cases.
709 *error_code = VALIDATION_ERROR_00943;
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600710 auto iv_state = getImageViewState(dev_data, image_view);
711 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600712 std::stringstream error_str;
713 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600714 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600715 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600716 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600717 // 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 -0600718 // Validate that imageLayout is compatible with aspect_mask and image format
719 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600720 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
721 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600722 VkFormat format = VK_FORMAT_MAX_ENUM;
723 VkImageUsageFlags usage = 0;
Tobin Ehlis30df15c2016-10-12 17:17:57 -0600724 auto image_node = getImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600725 if (image_node) {
726 format = image_node->createInfo.format;
727 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600728 // Validate that memory is bound to image
Tobin Ehlisfed999f2016-09-21 15:09:45 -0600729 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()")) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600730 // TODO : Need new code(s) for language in 11.6 Memory Association
731 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600732 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -0600733 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600734 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600735 // Also need to check the swapchains.
Tobin Ehlis969a5262016-06-02 12:13:32 -0600736 auto swapchain = getSwapchainFromImage(dev_data, image);
737 if (swapchain) {
Tobin Ehlis4e380592016-06-02 12:41:47 -0600738 auto swapchain_node = getSwapchainNode(dev_data, swapchain);
739 if (swapchain_node) {
740 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600741 }
742 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600743 }
744 // First validate that format and layout are compatible
745 if (format == VK_FORMAT_MAX_ENUM) {
746 std::stringstream error_str;
747 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600748 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600749 return false;
750 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600751 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
752 // vkCreateImageView(). What's the best way to create unique id for these cases?
Tobin Ehlis1809f912016-05-25 09:24:36 -0600753 bool ds = vk_format_is_depth_or_stencil(format);
754 switch (image_layout) {
755 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
756 // Only Color bit must be set
757 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600758 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600759 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
760 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600761 *error_msg = error_str.str();
Tobin Ehlis554bf382016-05-24 11:14:43 -0600762 return false;
763 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600764 // format must NOT be DS
765 if (ds) {
766 std::stringstream error_str;
767 error_str << "ImageView (" << image_view
768 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
769 << string_VkFormat(format) << " which is not a color format.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600770 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600771 return false;
772 }
773 break;
774 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
775 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
776 // Depth or stencil bit must be set, but both must NOT be set
777 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
778 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
779 // both must NOT be set
780 std::stringstream error_str;
781 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600782 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600783 return false;
784 }
785 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
786 // Neither were set
787 std::stringstream error_str;
788 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
789 << " but does not have STENCIL or DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600790 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600791 return false;
792 }
793 // format must be DS
794 if (!ds) {
795 std::stringstream error_str;
796 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
797 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600798 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600799 return false;
800 }
801 break;
802 default:
Mike Weiblencce7ec72016-10-17 19:33:05 -0600803 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600804 if (ds) {
805 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
806 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
807 // both must NOT be set
808 std::stringstream error_str;
809 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
810 << " and is using depth/stencil image of format " << string_VkFormat(format)
811 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
812 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
813 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
814 "reads respectively.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600815 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600816 return false;
817 }
818 }
819 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600820 break;
821 }
822 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -0600823 // 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 -0600824 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
825 // under vkCreateImage()
826 // TODO : Need to also validate case VALIDATION_ERROR_00952 where STORAGE_IMAGE & INPUT_ATTACH types must have been created with
827 // identify swizzle
Tobin Ehlis1809f912016-05-25 09:24:36 -0600828 std::string error_usage_bit;
829 switch (type) {
830 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
831 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
832 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
833 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
834 }
835 break;
836 }
837 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
838 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
839 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
Tobin Ehlisfb4cf712016-10-10 14:02:48 -0600840 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
841 std::stringstream error_str;
842 // TODO : Need to create custom enum error code for this case
843 error_str << "ImageView (" << image_view << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
844 << string_VkImageLayout(image_layout)
845 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage images can "
846 "only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
847 *error_msg = error_str.str();
848 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600849 }
850 break;
851 }
852 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
853 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
854 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
855 }
856 break;
857 }
858 default:
859 break;
860 }
861 if (!error_usage_bit.empty()) {
862 std::stringstream error_str;
863 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
864 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
865 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600866 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600867 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600868 }
869 return true;
870}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600871
Tobin Ehlis300888c2016-05-18 13:43:26 -0600872void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
873 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600874 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600875}
876
Tobin Ehlis300888c2016-05-18 13:43:26 -0600877void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600878 if (!immutable_) {
879 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600880 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600881 }
882 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600883}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600884
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600885void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
886 if (!immutable_) {
Tobin Ehlisfad7adf2016-10-20 06:50:37 -0600887 auto sampler_state = getSamplerState(dev_data, sampler_);
888 if (sampler_state)
889 core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600890 }
891}
892
Tobin Ehlis300888c2016-05-18 13:43:26 -0600893cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
894 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600895 updated = false;
896 descriptor_class = ImageSampler;
897}
898
Tobin Ehlis300888c2016-05-18 13:43:26 -0600899cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
900 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600901 updated = false;
902 descriptor_class = ImageSampler;
903 if (immut) {
904 sampler_ = *immut;
905 immutable_ = true;
906 updated = true;
907 }
908}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600909
Tobin Ehlis300888c2016-05-18 13:43:26 -0600910void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600911 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600912 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600913 sampler_ = image_info.sampler;
914 image_view_ = image_info.imageView;
915 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600916}
917
Tobin Ehlis300888c2016-05-18 13:43:26 -0600918void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600919 if (!immutable_) {
920 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600921 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600922 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600923 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
924 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600925 updated = true;
926 image_view_ = image_view;
927 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600928}
929
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600930void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data,
931 GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600932 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600933 if (!immutable_) {
Tobin Ehlisfad7adf2016-10-20 06:50:37 -0600934 auto sampler_state = getSamplerState(dev_data, sampler_);
935 if (sampler_state)
936 core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600937 }
Tobin Ehlis81e46372016-08-17 13:33:44 -0600938 // Add binding for image
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600939 auto iv_state = getImageViewState(dev_data, image_view_);
940 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -0600941 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -0600942 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600943}
944
Tobin Ehlis300888c2016-05-18 13:43:26 -0600945cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
946 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600947 updated = false;
948 descriptor_class = Image;
949 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
950 storage_ = true;
951};
952
Tobin Ehlis300888c2016-05-18 13:43:26 -0600953void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600954 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600955 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600956 image_view_ = image_info.imageView;
957 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600958}
959
Tobin Ehlis300888c2016-05-18 13:43:26 -0600960void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600961 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
962 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600963 updated = true;
964 image_view_ = image_view;
965 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600966}
967
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600968void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600969 // Add binding for image
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600970 auto iv_state = getImageViewState(dev_data, image_view_);
971 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -0600972 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -0600973 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600974}
975
Tobin Ehlis300888c2016-05-18 13:43:26 -0600976cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
977 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600978 updated = false;
979 descriptor_class = GeneralBuffer;
980 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
981 dynamic_ = true;
982 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
983 storage_ = true;
984 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
985 dynamic_ = true;
986 storage_ = true;
987 }
988}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600989void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600990 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600991 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600992 buffer_ = buffer_info.buffer;
993 offset_ = buffer_info.offset;
994 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600995}
996
Tobin Ehlis300888c2016-05-18 13:43:26 -0600997void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
998 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600999 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001000 buffer_ = buff_desc->buffer_;
1001 offset_ = buff_desc->offset_;
1002 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001003}
1004
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001005void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001006 auto buffer_node = getBufferNode(dev_data, buffer_);
1007 if (buffer_node)
1008 core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001009}
1010
Tobin Ehlis300888c2016-05-18 13:43:26 -06001011cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001012 updated = false;
1013 descriptor_class = TexelBuffer;
1014 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
1015 storage_ = true;
1016};
Tobin Ehlis56a30942016-05-19 08:00:00 -06001017
Tobin Ehlis300888c2016-05-18 13:43:26 -06001018void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001019 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001020 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001021}
1022
Tobin Ehlis300888c2016-05-18 13:43:26 -06001023void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1024 updated = true;
1025 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1026}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001027
1028void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8b872462016-09-14 08:12:08 -06001029 auto bv_state = getBufferViewState(dev_data, buffer_view_);
1030 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001031 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001032 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001033}
1034
Tobin Ehlis300888c2016-05-18 13:43:26 -06001035// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1036// sets, and then calls their respective Validate[Write|Copy]Update functions.
1037// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1038// be skipped, then true is returned.
1039// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001040bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
1041 const core_validation::layer_data *dev_data, uint32_t write_count,
1042 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1043 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001044 bool skip_call = false;
1045 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001046 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001047 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001048 auto set_node = core_validation::getSetNode(dev_data, dest_set);
1049 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001050 skip_call |=
1051 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 -06001052 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001053 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
1054 reinterpret_cast<uint64_t &>(dest_set));
1055 } else {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001056 UNIQUE_VALIDATION_ERROR_CODE error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001057 std::string error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001058 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_code, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001059 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 -06001060 reinterpret_cast<uint64_t &>(dest_set), __LINE__, error_code, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001061 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001062 " with error: %s. %s",
1063 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str(), validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001064 }
1065 }
1066 }
1067 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001068 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001069 auto dst_set = p_cds[i].dstSet;
1070 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001071 auto src_node = core_validation::getSetNode(dev_data, src_set);
1072 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
1073 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001074 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 -06001075 reinterpret_cast<uint64_t &>(src_set), __LINE__, VALIDATION_ERROR_00971, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001076 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001077 " that has not been allocated. %s",
1078 reinterpret_cast<uint64_t &>(src_set), validation_error_map[VALIDATION_ERROR_00971]);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001079 } else if (!dst_node) {
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 &>(dst_set), __LINE__, VALIDATION_ERROR_00972, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001082 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001083 " that has not been allocated. %s",
1084 reinterpret_cast<uint64_t &>(dst_set), validation_error_map[VALIDATION_ERROR_00972]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001085 } else {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001086 UNIQUE_VALIDATION_ERROR_CODE error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001087 std::string error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001088 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_code, &error_str)) {
1089 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1090 reinterpret_cast<uint64_t &>(dst_set), __LINE__, error_code, "DS",
1091 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
1092 " to Descriptor Set 0x%" PRIx64 " with error: %s. %s",
1093 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set),
1094 error_str.c_str(), validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001095 }
1096 }
1097 }
1098 return skip_call;
1099}
1100// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1101// sets, and then calls their respective Perform[Write|Copy]Update functions.
1102// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1103// with the same set of updates.
1104// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1105// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001106void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
1107 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1108 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001109 // Write updates first
1110 uint32_t i = 0;
1111 for (i = 0; i < write_count; ++i) {
1112 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001113 auto set_node = core_validation::getSetNode(dev_data, dest_set);
1114 if (set_node) {
1115 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001116 }
1117 }
1118 // Now copy updates
1119 for (i = 0; i < copy_count; ++i) {
1120 auto dst_set = p_cds[i].dstSet;
1121 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001122 auto src_node = core_validation::getSetNode(dev_data, src_set);
1123 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
1124 if (src_node && dst_node) {
1125 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001126 }
1127 }
1128}
1129// Validate the state for a given write update but don't actually perform the update
1130// If an error would occur for this update, return false and fill in details in error_msg string
1131bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001132 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001133 // Verify idle ds
1134 if (in_use.load()) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001135 // TODO : Re-using Allocate Idle error code, need write update idle error code
1136 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001137 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001138 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
1139 << " that is in use by a command buffer.";
1140 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001141 return false;
1142 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001143 // Verify dst binding exists
1144 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001145 *error_code = VALIDATION_ERROR_00936;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001146 std::stringstream error_str;
1147 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
1148 *error_msg = error_str.str();
1149 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001150 }
1151 // We know that binding is valid, verify update and do update on each descriptor
1152 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
1153 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
1154 if (type != update->descriptorType) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001155 *error_code = VALIDATION_ERROR_00937;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001156 std::stringstream error_str;
1157 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1158 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1159 *error_msg = error_str.str();
1160 return false;
1161 }
1162 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001163 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001164 std::stringstream error_str;
1165 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
1166 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
1167 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
1168 << " combined with update array element offset of " << update->dstArrayElement
1169 << " oversteps the size of this descriptor set.";
1170 *error_msg = error_str.str();
1171 return false;
1172 }
1173 // Verify consecutive bindings match (if needed)
1174 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001175 set_, error_msg)) {
1176 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001177 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001178 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001179 // Update is within bounds and consistent so last step is to validate update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001180 if (!VerifyWriteUpdateContents(update, start_idx, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001181 std::stringstream error_str;
1182 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1183 << " failed with error message: " << error_msg->c_str();
1184 *error_msg = error_str.str();
1185 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001186 }
1187 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001188 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001189}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001190// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001191// 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 -06001192bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_NODE const *buffer_node, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001193 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001194 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001195 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001196 std::string error_usage_bit;
1197 switch (type) {
1198 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1199 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001200 *error_code = VALIDATION_ERROR_00950;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001201 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1202 }
1203 break;
1204 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1205 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001206 *error_code = VALIDATION_ERROR_00951;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001207 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1208 }
1209 break;
1210 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1211 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1212 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001213 *error_code = VALIDATION_ERROR_00946;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001214 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1215 }
1216 break;
1217 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1218 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1219 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001220 *error_code = VALIDATION_ERROR_00947;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001221 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1222 }
1223 break;
1224 default:
1225 break;
1226 }
1227 if (!error_usage_bit.empty()) {
1228 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001229 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1230 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1231 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001232 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001233 return false;
1234 }
1235 return true;
1236}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001237// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1238// 1. buffer is valid
1239// 2. buffer was created with correct usage flags
1240// 3. offset is less than buffer size
1241// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001242// 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 -06001243bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001244 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
1245 // TODO : Defaulting to 00962 for all cases here. Need to create new error codes for a few cases below.
1246 *error_code = VALIDATION_ERROR_00962;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001247 // First make sure that buffer is valid
1248 auto buffer_node = getBufferNode(device_data_, buffer_info->buffer);
1249 if (!buffer_node) {
1250 std::stringstream error_str;
1251 error_str << "Invalid VkBuffer: " << buffer_info->buffer;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001252 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001253 return false;
1254 }
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001255 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()")) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001256 // TODO : This is a repeat code, need new code(s) for language in 11.6 Memory Association
1257 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001258 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001259 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001260 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001261 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1262 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001263 return false;
1264 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001265 // TODO : Need to also validate device limit offset requirements captured in VALIDATION_ERROR_00944,945
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001266 // offset must be less than buffer size
1267 if (buffer_info->offset > buffer_node->createInfo.size) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001268 *error_code = VALIDATION_ERROR_00959;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001269 std::stringstream error_str;
1270 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than buffer " << buffer_node->buffer
1271 << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001272 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001273 return false;
1274 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001275 // TODO : Need to also validate device limit range requirements captured in VALIDATION_ERROR_00948,949
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001276 if (buffer_info->range != VK_WHOLE_SIZE) {
1277 // Range must be VK_WHOLE_SIZE or > 0
1278 if (!buffer_info->range) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001279 *error_code = VALIDATION_ERROR_00960;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001280 std::stringstream error_str;
1281 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001282 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001283 return false;
1284 }
1285 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1286 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001287 *error_code = VALIDATION_ERROR_00961;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001288 std::stringstream error_str;
1289 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1290 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001291 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001292 return false;
1293 }
1294 }
1295 return true;
1296}
1297
Tobin Ehlis300888c2016-05-18 13:43:26 -06001298// Verify that the contents of the update are ok, but don't perform actual update
1299bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001300 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1301 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001302 switch (update->descriptorType) {
1303 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1304 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1305 // Validate image
1306 auto image_view = update->pImageInfo[di].imageView;
1307 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001308 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001309 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001310 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error_msg->c_str();
1311 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001312 return false;
1313 }
1314 }
1315 // Intentional fall-through to validate sampler
1316 }
1317 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1318 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1319 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001320 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001321 *error_code = VALIDATION_ERROR_00942;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001322 std::stringstream error_str;
1323 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1324 << update->pImageInfo[di].sampler << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001325 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001326 return false;
1327 }
1328 } else {
1329 // TODO : Warn here
1330 }
1331 }
1332 break;
1333 }
1334 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1335 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1336 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1337 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1338 auto image_view = update->pImageInfo[di].imageView;
1339 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001340 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001341 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001342 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
1343 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001344 return false;
1345 }
1346 }
1347 break;
1348 }
1349 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1350 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1351 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1352 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis8b872462016-09-14 08:12:08 -06001353 auto bv_state = getBufferViewState(device_data_, buffer_view);
1354 if (!bv_state) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001355 *error_code = VALIDATION_ERROR_00940;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001356 std::stringstream error_str;
1357 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001358 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001359 return false;
1360 }
Tobin Ehlis8b872462016-09-14 08:12:08 -06001361 auto buffer = bv_state->create_info.buffer;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001362 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), update->descriptorType, error_code, error_msg)) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001363 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001364 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
1365 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001366 return false;
1367 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001368 }
1369 break;
1370 }
1371 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1372 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1373 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1374 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1375 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001376 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001377 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001378 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
1379 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001380 return false;
1381 }
1382 }
1383 break;
1384 }
1385 default:
1386 assert(0); // We've already verified update type so should never get here
1387 break;
1388 }
1389 // All checks passed so update contents are good
1390 return true;
1391}
1392// Verify that the contents of the update are ok, but don't perform actual update
1393bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001394 VkDescriptorType type, uint32_t index,
1395 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1396 std::string *error_msg) const {
1397 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
1398 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06001399 switch (src_set->descriptors_[index]->descriptor_class) {
1400 case PlainSampler: {
1401 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1402 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1403 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001404 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001405 *error_code = VALIDATION_ERROR_00942;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001406 std::stringstream error_str;
1407 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001408 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001409 return false;
1410 }
1411 } else {
1412 // TODO : Warn here
1413 }
1414 }
1415 break;
1416 }
1417 case ImageSampler: {
1418 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1419 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1420 // First validate sampler
1421 if (!img_samp_desc->IsImmutableSampler()) {
1422 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001423 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001424 *error_code = VALIDATION_ERROR_00942;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001425 std::stringstream error_str;
1426 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001427 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001428 return false;
1429 }
1430 } else {
1431 // TODO : Warn here
1432 }
1433 // Validate image
1434 auto image_view = img_samp_desc->GetImageView();
1435 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001436 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001437 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001438 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
1439 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001440 return false;
1441 }
1442 }
Alex Smithd8f14792016-09-23 12:18:51 +01001443 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001444 }
1445 case Image: {
1446 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1447 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1448 auto image_view = img_desc->GetImageView();
1449 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001450 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001451 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001452 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
1453 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001454 return false;
1455 }
1456 }
1457 break;
1458 }
1459 case TexelBuffer: {
1460 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1461 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis8b872462016-09-14 08:12:08 -06001462 auto bv_state = getBufferViewState(device_data_, buffer_view);
1463 if (!bv_state) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001464 *error_code = VALIDATION_ERROR_00940;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001465 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001466 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001467 *error_msg = error_str.str();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001468 return false;
1469 }
Tobin Ehlis8b872462016-09-14 08:12:08 -06001470 auto buffer = bv_state->create_info.buffer;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001471 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), type, error_code, error_msg)) {
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001472 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001473 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
1474 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001475 return false;
1476 }
1477 }
1478 break;
1479 }
1480 case GeneralBuffer: {
1481 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1482 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001483 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), type, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001484 std::stringstream error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001485 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
1486 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001487 return false;
1488 }
1489 }
1490 break;
1491 }
1492 default:
1493 assert(0); // We've already verified update type so should never get here
1494 break;
1495 }
1496 // All checks passed so update contents are good
1497 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001498}
Tobin Ehlisee471462016-05-26 11:21:59 -06001499// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlis815e8132016-06-02 13:02:17 -06001500bool cvdescriptorset::ValidateAllocateDescriptorSets(const debug_report_data *report_data,
1501 const VkDescriptorSetAllocateInfo *p_alloc_info,
1502 const core_validation::layer_data *dev_data,
1503 AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001504 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001505
1506 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001507 auto layout = getDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1508 if (!layout) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001509 skip_call |=
1510 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1511 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1512 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1513 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001514 } else {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001515 ds_data->layout_nodes[i] = layout;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001516 // Count total descriptors required per type
Tobin Ehlis815e8132016-06-02 13:02:17 -06001517 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1518 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001519 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1520 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1521 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001522 }
1523 }
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001524 auto pool_state = getDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001525 // Track number of descriptorSets allowable in this pool
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001526 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001527 skip_call |= log_msg(
1528 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001529 reinterpret_cast<uint64_t &>(pool_state->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY, "DS",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001530 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64 ". This pool only has %d descriptorSets remaining.",
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001531 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_state->pool), pool_state->availableSets);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001532 }
1533 // Determine whether descriptor counts are satisfiable
1534 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001535 if (ds_data->required_descriptors_by_type[i] > pool_state->availableDescriptorTypeCount[i]) {
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001536 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001537 reinterpret_cast<const uint64_t &>(pool_state->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001538 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1539 ". This pool only has %d descriptors of this type remaining.",
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001540 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Tobin Ehlis15e6f792016-10-12 15:01:39 -06001541 reinterpret_cast<uint64_t &>(pool_state->pool), pool_state->availableDescriptorTypeCount[i]);
Tobin Ehlisee471462016-05-26 11:21:59 -06001542 }
1543 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001544
Tobin Ehlisee471462016-05-26 11:21:59 -06001545 return skip_call;
1546}
1547// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001548void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1549 const VkDescriptorSet *descriptor_sets,
1550 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06001551 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06001552 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
1553 const core_validation::layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001554 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001555 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001556 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001557 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1558 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1559 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001560 /* Create tracking object for each descriptor set; insert into
1561 * global map and the pool's set.
1562 */
1563 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis93f22372016-10-12 14:34:12 -06001564 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i],
1565 dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001566
1567 pool_state->sets.insert(new_ds);
1568 new_ds->in_use.store(0);
1569 (*set_map)[descriptor_sets[i]] = new_ds;
1570 }
1571}