blob: de349457d3f2678fdb21a42a508f1fe0c9d54762 [file] [log] [blame]
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
4 * Copyright (C) 2015-2016 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Tobin Ehlis <tobine@google.com>
19 */
20
Tobin Ehlisf922ef82016-11-30 10:19:14 -070021// Allow use of STL min and max functions in Windows
22#define NOMINMAX
23
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060024#include "descriptor_sets.h"
25#include "vk_enum_string_helper.h"
26#include "vk_safe_struct.h"
Tobin Ehlisc8266452017-04-07 12:20:30 -060027#include "buffer_validation.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060028#include <sstream>
Mark Lobodzinski2eee5d82016-12-02 15:33:18 -070029#include <algorithm>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060030
31// Construct DescriptorSetLayout instance from given create info
Tobin Ehlis154c2692016-10-25 09:36:53 -060032cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060033 const VkDescriptorSetLayout layout)
34 : layout_(layout), binding_count_(p_create_info->bindingCount), descriptor_count_(0), dynamic_descriptor_count_(0) {
Tobin Ehlisa3525e02016-11-17 10:50:52 -070035 // Dyn array indicies are ordered by binding # and array index of any array within the binding
36 // so we store up bindings w/ count in ordered map in order to create dyn array mappings below
37 std::map<uint32_t, uint32_t> binding_to_dyn_count;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060038 for (uint32_t i = 0; i < binding_count_; ++i) {
Tobin Ehlis9637fb22016-12-12 15:59:34 -070039 auto binding_num = p_create_info->pBindings[i].binding;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060040 descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070041 uint32_t insert_index = 0; // Track vector index where we insert element
Tobin Ehlis9637fb22016-12-12 15:59:34 -070042 if (bindings_.empty() || binding_num > bindings_.back().binding) {
43 bindings_.push_back(safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i]));
Jamie Madill3f5fd492016-12-19 15:59:18 -050044 insert_index = static_cast<uint32_t>(bindings_.size()) - 1;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070045 } else { // out-of-order binding number, need to insert into vector in-order
Tobin Ehlis9637fb22016-12-12 15:59:34 -070046 auto it = bindings_.begin();
47 // Find currently binding's spot in vector
48 while (binding_num > it->binding) {
49 assert(it != bindings_.end());
50 ++insert_index;
51 ++it;
52 }
53 bindings_.insert(it, safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i]));
54 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060055 // In cases where we should ignore pImmutableSamplers make sure it's NULL
56 if ((p_create_info->pBindings[i].pImmutableSamplers) &&
57 ((p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_SAMPLER) &&
58 (p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER))) {
Tobin Ehlis9637fb22016-12-12 15:59:34 -070059 bindings_[insert_index].pImmutableSamplers = nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060060 }
61 if (p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
62 p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
Tobin Ehlisa3525e02016-11-17 10:50:52 -070063 binding_to_dyn_count[p_create_info->pBindings[i].binding] = p_create_info->pBindings[i].descriptorCount;
Tobin Ehlisef0de162016-06-20 13:07:34 -060064 dynamic_descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060065 }
66 }
Tobin Ehlis9637fb22016-12-12 15:59:34 -070067 assert(bindings_.size() == binding_count_);
68 uint32_t global_index = 0;
69 // Vector order is finalized so create maps of bindings to indices
70 for (uint32_t i = 0; i < binding_count_; ++i) {
71 auto binding_num = bindings_[i].binding;
72 binding_to_index_map_[binding_num] = i;
73 binding_to_global_start_index_map_[binding_num] = global_index;
74 global_index += bindings_[i].descriptorCount ? bindings_[i].descriptorCount - 1 : 0;
75 binding_to_global_end_index_map_[binding_num] = global_index;
76 global_index += bindings_[i].descriptorCount ? 1 : 0;
77 }
Tobin Ehlisa3525e02016-11-17 10:50:52 -070078 // Now create dyn offset array mapping for any dynamic descriptors
79 uint32_t dyn_array_idx = 0;
80 for (const auto &bc_pair : binding_to_dyn_count) {
81 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
82 dyn_array_idx += bc_pair.second;
83 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060084}
Tobin Ehlis154c2692016-10-25 09:36:53 -060085
86// Validate descriptor set layout create info
87bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(debug_report_data *report_data,
88 const VkDescriptorSetLayoutCreateInfo *create_info) {
89 bool skip = false;
90 std::unordered_set<uint32_t> bindings;
91 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
Tobin Ehlisfdcb63f2016-10-25 20:56:47 -060092 if (!bindings.insert(create_info->pBindings[i].binding).second) {
Tobin Ehlis154c2692016-10-25 09:36:53 -060093 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
94 VALIDATION_ERROR_02345, "DS", "duplicated binding number in VkDescriptorSetLayoutBinding. %s",
95 validation_error_map[VALIDATION_ERROR_02345]);
96 }
Tobin Ehlis154c2692016-10-25 09:36:53 -060097 }
98 return skip;
99}
100
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600101// put all bindings into the given set
102void cvdescriptorset::DescriptorSetLayout::FillBindingSet(std::unordered_set<uint32_t> *binding_set) const {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700103 for (auto binding_index_pair : binding_to_index_map_) binding_set->insert(binding_index_pair.first);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600104}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600105
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700106VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(
107 const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600108 const auto &bi_itr = binding_to_index_map_.find(binding);
109 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600110 return bindings_[bi_itr->second].ptr();
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600111 }
112 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600113}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700114VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(
115 const uint32_t index) const {
116 if (index >= bindings_.size()) return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600117 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600118}
119// Return descriptorCount for given binding, 0 if index is unavailable
120uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600121 const auto &bi_itr = binding_to_index_map_.find(binding);
122 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600123 return bindings_[bi_itr->second].descriptorCount;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600124 }
125 return 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600126}
127// Return descriptorCount for given index, 0 if index is unavailable
128uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700129 if (index >= bindings_.size()) return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600130 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600131}
132// For the given binding, return descriptorType
133VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const {
134 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600135 const auto &bi_itr = binding_to_index_map_.find(binding);
136 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600137 return bindings_[bi_itr->second].descriptorType;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600138 }
139 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600140}
141// For the given index, return descriptorType
142VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
143 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600144 return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600145}
146// For the given global index, return descriptorType
147// Currently just counting up through bindings_, may improve this in future
148VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const {
149 uint32_t global_offset = 0;
150 for (auto binding : bindings_) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600151 global_offset += binding.descriptorCount;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700152 if (index < global_offset) return binding.descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600153 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700154 assert(0); // requested global index is out of bounds
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600155 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
156}
157// For the given binding, return stageFlags
158VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const {
159 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600160 const auto &bi_itr = binding_to_index_map_.find(binding);
161 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600162 return bindings_[bi_itr->second].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600163 }
164 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600165}
166// For the given binding, return start index
167uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const {
168 assert(binding_to_global_start_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600169 const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding);
170 if (btgsi_itr != binding_to_global_start_index_map_.end()) {
171 return btgsi_itr->second;
172 }
173 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600174 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600175 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600176}
177// For the given binding, return end index
178uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
179 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600180 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
181 if (btgei_itr != binding_to_global_end_index_map_.end()) {
182 return btgei_itr->second;
183 }
184 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600185 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600186 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600187}
188// For given binding, return ptr to ImmutableSampler array
189VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
190 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600191 const auto &bi_itr = binding_to_index_map_.find(binding);
192 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600193 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600194 }
195 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600196}
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -0700197// Move to next valid binding having a non-zero binding count
198uint32_t cvdescriptorset::DescriptorSetLayout::GetNextValidBinding(const uint32_t binding) const {
199 uint32_t new_binding = binding;
200 do {
201 new_binding++;
202 } while (GetDescriptorCountFromBinding(new_binding) == 0);
203 return new_binding;
204}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600205// For given index, return ptr to ImmutableSampler array
206VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
207 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600208 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600209}
210// If our layout is compatible with rh_ds_layout, return true,
211// else return false and fill in error_msg will description of what causes incompatibility
212bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
213 // Trivial case
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700214 if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) return true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600215 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
216 std::stringstream error_str;
217 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
218 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
219 *error_msg = error_str.str();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700220 return false; // trivial fail case
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600221 }
222 // Descriptor counts match so need to go through bindings one-by-one
223 // and verify that type and stageFlags match
224 for (auto binding : bindings_) {
225 // TODO : Do we also need to check immutable samplers?
226 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600227 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600228 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600229 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
230 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600231 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600232 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600233 *error_msg = error_str.str();
234 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600235 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600236 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600237 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
238 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600239 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600240 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600241 *error_msg = error_str.str();
242 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600243 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600244 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600245 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
246 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600247 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600248 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600249 *error_msg = error_str.str();
250 return false;
251 }
252 }
253 return true;
254}
255
256bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700257 if (!binding_to_index_map_.count(binding + 1)) return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600258 auto const &bi_itr = binding_to_index_map_.find(binding);
259 if (bi_itr != binding_to_index_map_.end()) {
260 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
261 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600262 auto type = bindings_[bi_itr->second].descriptorType;
263 auto stage_flags = bindings_[bi_itr->second].stageFlags;
264 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
265 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
266 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
267 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600268 return false;
269 }
270 return true;
271 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600272 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600273 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600274}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600275// Starting at offset descriptor of given binding, parse over update_count
276// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
277// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
278// If so, return true. If not, fill in error_msg and return false
279bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
280 const char *type, const VkDescriptorSet set,
281 std::string *error_msg) const {
282 // Verify consecutive bindings match (if needed)
283 auto orig_binding = current_binding;
284 // Track count of descriptors in the current_bindings that are remaining to be updated
285 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
286 // First, it's legal to offset beyond your own binding so handle that case
287 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
288 while (offset >= binding_remaining) {
289 // Advance to next binding, decrement offset by binding size
290 offset -= binding_remaining;
291 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
292 }
293 binding_remaining -= offset;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700294 while (update_count > binding_remaining) { // While our updates overstep current binding
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600295 // Verify next consecutive binding matches type, stage flags & immutable sampler use
296 if (!IsNextBindingConsistent(current_binding++)) {
297 std::stringstream error_str;
298 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
299 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
300 "next binding is not consistent with current binding so this update is invalid.";
301 *error_msg = error_str.str();
302 return false;
303 }
304 // For sake of this check consider the bindings updated and grab count for next binding
305 update_count -= binding_remaining;
306 binding_remaining = GetDescriptorCountFromBinding(current_binding);
307 }
308 return true;
309}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600310
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600311cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
312 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
313
Tobin Ehlis93f22372016-10-12 14:34:12 -0600314cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700315 const DescriptorSetLayout *layout, const layer_data *dev_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700316 : some_update_(false),
317 set_(set),
318 pool_state_(nullptr),
319 p_layout_(layout),
320 device_data_(dev_data),
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700321 limits_(GetPhysDevProperties(dev_data)->properties.limits) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700322 pool_state_ = GetDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600323 // Foreach binding, create default descriptors of given type
324 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
325 auto type = p_layout_->GetTypeFromIndex(i);
326 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700327 case VK_DESCRIPTOR_TYPE_SAMPLER: {
328 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
329 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
330 if (immut_sampler)
331 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
332 else
333 descriptors_.emplace_back(new SamplerDescriptor());
334 }
335 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600336 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700337 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
338 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
339 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
340 if (immut)
341 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
342 else
343 descriptors_.emplace_back(new ImageSamplerDescriptor());
344 }
345 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600346 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700347 // ImageDescriptors
348 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
349 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
350 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
351 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
352 descriptors_.emplace_back(new ImageDescriptor(type));
353 break;
354 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
355 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
356 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
357 descriptors_.emplace_back(new TexelDescriptor(type));
358 break;
359 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
360 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
361 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
362 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
363 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
364 descriptors_.emplace_back(new BufferDescriptor(type));
365 break;
366 default:
367 assert(0); // Bad descriptor type specified
368 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600369 }
370 }
371}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600372
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700373cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200374
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700375static std::string string_descriptor_req_view_type(descriptor_req req) {
376 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200377 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
378 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700379 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700380 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200381 }
382 }
383
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700384 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700385
386 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200387}
388
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600389// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
390bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
391 return layout->IsCompatible(p_layout_, error);
392}
Chris Forbes57989132016-07-26 17:06:10 +1200393
Tobin Ehlis3066db62016-08-22 08:12:23 -0600394// 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 -0600395// This includes validating that all descriptors in the given bindings are updated,
396// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
397// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600398bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600399 const std::vector<uint32_t> &dynamic_offsets, const GLOBAL_CB_NODE *cb_node,
400 const char *caller, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200401 for (auto binding_pair : bindings) {
402 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600403 if (!p_layout_->HasBinding(binding)) {
404 std::stringstream error_str;
405 error_str << "Attempting to validate DrawState for binding #" << binding
406 << " which is an invalid binding for this descriptor set.";
407 *error = error_str.str();
408 return false;
409 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600410 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600411 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600412 // Nothing to do for strictly immutable sampler
413 } else {
414 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700415 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700416 for (uint32_t i = start_idx; i <= end_idx; ++i, ++array_idx) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600417 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600418 std::stringstream error_str;
419 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
420 << " is being used in draw but has not been updated.";
421 *error = error_str.str();
422 return false;
423 } else {
Chris Forbes57989132016-07-26 17:06:10 +1200424 auto descriptor_class = descriptors_[i]->GetClass();
425 if (descriptor_class == GeneralBuffer) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600426 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600427 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700428 auto buffer_node = GetBufferState(device_data_, buffer);
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600429 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600430 std::stringstream error_str;
431 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
432 << " references invalid buffer " << buffer << ".";
433 *error = error_str.str();
434 return false;
435 } else {
Tobin Ehlis640a81c2016-11-15 15:37:18 -0700436 for (auto mem_binding : buffer_node->GetBoundMemory()) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700437 if (!GetMemObjInfo(device_data_, mem_binding)) {
Tobin Ehlis640a81c2016-11-15 15:37:18 -0700438 std::stringstream error_str;
439 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
440 << " uses buffer " << buffer << " that references invalid memory " << mem_binding
441 << ".";
442 *error = error_str.str();
443 return false;
444 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600445 }
446 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600447 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600448 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600449 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600450 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
451 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700452 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600453 if (VK_WHOLE_SIZE == range) {
454 if ((dyn_offset + desc_offset) > buffer_size) {
455 std::stringstream error_str;
456 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
457 << " uses buffer " << buffer
458 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
459 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
460 << buffer_size << ".";
461 *error = error_str.str();
462 return false;
463 }
464 } else {
465 if ((dyn_offset + desc_offset + range) > buffer_size) {
466 std::stringstream error_str;
467 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
468 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
469 << " combined with offset " << desc_offset << " and range " << range
470 << " that oversteps the buffer size of " << buffer_size << ".";
471 *error = error_str.str();
472 return false;
473 }
474 }
475 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700476 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
Tobin Ehlisc8266452017-04-07 12:20:30 -0600477 VkImageView image_view;
478 VkImageLayout image_layout;
479 if (descriptor_class == ImageSampler) {
480 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
481 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
482 } else {
483 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
484 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
485 }
Chris Forbes57989132016-07-26 17:06:10 +1200486 auto reqs = binding_pair.second;
487
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700488 auto image_view_state = GetImageViewState(device_data_, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600489 assert(image_view_state);
490 auto image_view_ci = image_view_state->create_info;
Chris Forbes57989132016-07-26 17:06:10 +1200491
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600492 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
Chris Forbes57989132016-07-26 17:06:10 +1200493 // bad view type
494 std::stringstream error_str;
495 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700496 << " requires an image view of type " << string_descriptor_req_view_type(reqs) << " but got "
497 << string_VkImageViewType(image_view_ci.viewType) << ".";
Chris Forbes57989132016-07-26 17:06:10 +1200498 *error = error_str.str();
499 return false;
500 }
501
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700502 auto image_node = GetImageState(device_data_, image_view_ci.image);
Chris Forbes57989132016-07-26 17:06:10 +1200503 assert(image_node);
Tobin Ehlisc8266452017-04-07 12:20:30 -0600504 // Verify Image Layout
505 // TODO: VALIDATION_ERROR_02981 is the error physically closest to the spec language of interest, however
506 // there is no VUID for the actual spec language. Need to file a spec MR to add VU language for:
507 // imageLayout is the layout that the image subresources accessible from imageView will be in at the time
508 // this descriptor is accessed.
509 // Copy first mip level into sub_layers and loop over each mip level to verify layout
510 VkImageSubresourceLayers sub_layers;
511 sub_layers.aspectMask = image_view_ci.subresourceRange.aspectMask;
512 sub_layers.baseArrayLayer = image_view_ci.subresourceRange.baseArrayLayer;
513 sub_layers.layerCount = image_view_ci.subresourceRange.layerCount;
514 bool hit_error = false;
515 for (auto cur_level = image_view_ci.subresourceRange.baseMipLevel;
516 cur_level < image_view_ci.subresourceRange.levelCount; ++cur_level) {
517 sub_layers.mipLevel = cur_level;
518 VerifyImageLayout(device_data_, cb_node, image_node, sub_layers, image_layout,
519 VK_IMAGE_LAYOUT_UNDEFINED, caller, VALIDATION_ERROR_02981, &hit_error);
520 if (hit_error) {
521 *error =
522 "Image layout specified at vkUpdateDescriptorSets() time doesn't match actual image layout at "
523 "time descriptor is used. See previous error callback for specific details.";
524 return false;
525 }
526 }
527 // Verify Sample counts
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700528 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
Chris Forbes57989132016-07-26 17:06:10 +1200529 std::stringstream error_str;
530 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
531 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
532 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
533 *error = error_str.str();
534 return false;
535 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700536 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
Chris Forbes57989132016-07-26 17:06:10 +1200537 std::stringstream error_str;
538 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
539 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
540 *error = error_str.str();
541 return false;
542 }
543 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600544 }
545 }
546 }
547 }
548 return true;
549}
Chris Forbes57989132016-07-26 17:06:10 +1200550
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600551// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600552uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600553 std::unordered_set<VkBuffer> *buffer_set,
554 std::unordered_set<VkImageView> *image_set) const {
555 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200556 for (auto binding_pair : bindings) {
557 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600558 // If a binding doesn't exist, skip it
559 if (!p_layout_->HasBinding(binding)) {
560 continue;
561 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600562 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600563 if (descriptors_[start_idx]->IsStorage()) {
564 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600565 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600566 if (descriptors_[start_idx + i]->updated) {
567 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600568 num_updates++;
569 }
570 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600571 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600572 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600573 if (descriptors_[start_idx + i]->updated) {
574 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700575 auto bv_state = GetBufferViewState(device_data_, bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600576 if (bv_state) {
577 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600578 num_updates++;
579 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600580 }
581 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600582 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600583 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600584 if (descriptors_[start_idx + i]->updated) {
585 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600586 num_updates++;
587 }
588 }
589 }
590 }
591 }
592 return num_updates;
593}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600594// Set is being deleted or updates so invalidate all bound cmd buffers
595void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Tobin Ehlisfe5731a2016-11-21 08:31:01 -0700596 core_validation::invalidateCommandBuffers(device_data_, cb_bindings,
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600597 {reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600598}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600599// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600600void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700601 // Perform update on a per-binding basis as consecutive updates roll over to next binding
602 auto descriptors_remaining = update->descriptorCount;
603 auto binding_being_updated = update->dstBinding;
604 auto offset = update->dstArrayElement;
605 while (descriptors_remaining) {
606 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
607 auto global_idx = p_layout_->GetGlobalStartIndexFromBinding(binding_being_updated) + offset;
608 // Loop over the updates for a single binding at a time
609 for (uint32_t di = 0; di < update_count; ++di) {
610 descriptors_[global_idx + di]->WriteUpdate(update, di);
611 }
612 // Roll over to next binding in case of consecutive update
613 descriptors_remaining -= update_count;
614 offset = 0;
615 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600616 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700617 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600618
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600619 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600620}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600621// Validate Copy update
622bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600623 const DescriptorSet *src_set, UNIQUE_VALIDATION_ERROR_CODE *error_code,
624 std::string *error_msg) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600625 // Verify idle ds
626 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700627 // TODO : Re-using Free Idle error code, need copy update idle error code
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600628 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600629 std::stringstream error_str;
630 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700631 << " that is in use by a command buffer";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600632 *error_msg = error_str.str();
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600633 return false;
634 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600635 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600636 *error_code = VALIDATION_ERROR_00966;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600637 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700638 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600639 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600640 return false;
641 }
642 if (!src_set->HasBinding(update->srcBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600643 *error_code = VALIDATION_ERROR_00964;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600644 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700645 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600646 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600647 return false;
648 }
649 // src & dst set bindings are valid
650 // Check bounds of src & dst
651 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
652 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
653 // SRC update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600654 *error_code = VALIDATION_ERROR_00965;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600655 std::stringstream error_str;
656 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
657 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
658 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700659 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600660 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600661 return false;
662 }
663 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
664 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
665 // DST update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600666 *error_code = VALIDATION_ERROR_00967;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600667 std::stringstream error_str;
668 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
669 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
670 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700671 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600672 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600673 return false;
674 }
675 // Check that types match
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600676 // TODO : Base default error case going from here is VALIDATION_ERROR_00968 which covers all consistency issues, need more
677 // fine-grained error codes
678 *error_code = VALIDATION_ERROR_00968;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600679 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
680 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
681 if (src_type != dst_type) {
682 std::stringstream error_str;
683 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
684 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700685 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600686 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600687 return false;
688 }
689 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600690 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600691 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600692 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600693 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600694 return false;
695 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600696 // First make sure source descriptors are updated
697 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
698 if (!src_set->descriptors_[src_start_idx + i]) {
699 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700700 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding
701 << " but descriptor at array offset " << update->srcArrayElement + i << " has not been updated";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600702 *error_msg = error_str.str();
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600703 return false;
704 }
705 }
706 // Update parameters all look good and descriptor updated so verify update contents
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700707 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error_code, error_msg)) return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600708
709 // All checks passed so update is good
710 return true;
711}
712// Perform Copy update
713void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600714 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
715 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600716 // Update parameters all look good so perform update
717 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600718 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600719 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700720 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600721
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600722 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600723}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600724
Tobin Ehlisf9519102016-08-17 09:49:13 -0600725// Bind cb_node to this set and this set to cb_node.
726// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
727// to be used in a draw by the given cb_node
Tobin Ehlis276d3d32016-12-21 09:21:06 -0700728void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node,
Tobin Ehlis022528b2016-12-29 12:22:32 -0700729 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600730 // bind cb to this descriptor set
731 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600732 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600733 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 -0600734 pool_state_->cb_bindings.insert(cb_node);
735 cb_node->object_bindings.insert(
736 {reinterpret_cast<uint64_t &>(pool_state_->pool), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT});
Tobin Ehlisf9519102016-08-17 09:49:13 -0600737 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
738 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -0700739 for (auto binding_req_pair : binding_req_map) {
740 auto binding = binding_req_pair.first;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600741 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
742 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
743 for (uint32_t i = start_idx; i <= end_idx; ++i) {
744 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
745 }
746 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600747}
748
Tobin Ehlis300888c2016-05-18 13:43:26 -0600749cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600750 updated = false;
751 descriptor_class = PlainSampler;
752};
753
Tobin Ehlis300888c2016-05-18 13:43:26 -0600754cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600755 updated = false;
756 descriptor_class = PlainSampler;
757 if (immut) {
758 sampler_ = *immut;
759 immutable_ = true;
760 updated = true;
761 }
762}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600763// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700764bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const layer_data *dev_data) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700765 return (GetSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600766}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600767
Tobin Ehlis554bf382016-05-24 11:14:43 -0600768bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700769 const layer_data *dev_data, UNIQUE_VALIDATION_ERROR_CODE *error_code,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600770 std::string *error_msg) {
771 // TODO : Defaulting to 00943 for all cases here. Need to create new error codes for various cases.
772 *error_code = VALIDATION_ERROR_00943;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700773 auto iv_state = GetImageViewState(dev_data, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600774 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600775 std::stringstream error_str;
776 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600777 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600778 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600779 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600780 // 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 -0600781 // Validate that imageLayout is compatible with aspect_mask and image format
782 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600783 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
784 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600785 VkFormat format = VK_FORMAT_MAX_ENUM;
786 VkImageUsageFlags usage = 0;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700787 auto image_node = GetImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600788 if (image_node) {
789 format = image_node->createInfo.format;
790 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600791 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700792 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
793 // the error here occurs is if memory bound to a created imageView has been freed.
Tobin Ehlise1995fc2016-12-22 12:45:09 -0700794 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_02524)) {
Tobin Ehlisde1a0f92016-12-22 12:26:32 -0700795 *error_code = VALIDATION_ERROR_02524;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600796 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600797 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -0600798 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600799 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600800 // Also need to check the swapchains.
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700801 auto swapchain = GetSwapchainFromImage(dev_data, image);
Tobin Ehlis969a5262016-06-02 12:13:32 -0600802 if (swapchain) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700803 auto swapchain_node = GetSwapchainNode(dev_data, swapchain);
Tobin Ehlis4e380592016-06-02 12:41:47 -0600804 if (swapchain_node) {
805 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600806 }
807 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600808 }
809 // First validate that format and layout are compatible
810 if (format == VK_FORMAT_MAX_ENUM) {
811 std::stringstream error_str;
812 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600813 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600814 return false;
815 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600816 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
817 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -0600818 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -0600819 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700820 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
821 // Only Color bit must be set
822 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600823 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700824 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
825 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600826 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600827 return false;
828 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700829 // format must NOT be DS
830 if (ds) {
831 std::stringstream error_str;
832 error_str << "ImageView (" << image_view
833 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
834 << string_VkFormat(format) << " which is not a color format.";
835 *error_msg = error_str.str();
836 return false;
837 }
838 break;
839 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
840 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
841 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600842 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
843 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
844 // both must NOT be set
845 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700846 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600847 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600848 return false;
849 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700850 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
851 // Neither were set
852 std::stringstream error_str;
853 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
854 << " but does not have STENCIL or DEPTH aspects set";
855 *error_msg = error_str.str();
856 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600857 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700858 // format must be DS
859 if (!ds) {
860 std::stringstream error_str;
861 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
862 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
863 *error_msg = error_str.str();
864 return false;
865 }
866 break;
867 default:
868 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
869 if (ds) {
870 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
871 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
872 // both must NOT be set
873 std::stringstream error_str;
874 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
875 << " and is using depth/stencil image of format " << string_VkFormat(format)
876 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
877 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
878 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
879 "reads respectively.";
880 *error_msg = error_str.str();
881 return false;
882 }
883 }
884 }
885 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600886 }
887 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -0600888 // 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 -0600889 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
890 // under vkCreateImage()
891 // TODO : Need to also validate case VALIDATION_ERROR_00952 where STORAGE_IMAGE & INPUT_ATTACH types must have been created with
892 // identify swizzle
Tobin Ehlis1809f912016-05-25 09:24:36 -0600893 std::string error_usage_bit;
894 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700895 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
896 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
897 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
898 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
899 }
900 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600901 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700902 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
903 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
904 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
905 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
906 std::stringstream error_str;
907 // TODO : Need to create custom enum error code for this case
908 error_str
909 << "ImageView (" << image_view << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
910 << string_VkImageLayout(image_layout)
911 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage images can "
912 "only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
913 *error_msg = error_str.str();
914 return false;
915 }
916 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600917 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700918 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
919 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
920 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
921 }
922 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600923 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700924 default:
925 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600926 }
927 if (!error_usage_bit.empty()) {
928 std::stringstream error_str;
929 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
930 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
931 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600932 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600933 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600934 }
935 return true;
936}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600937
Tobin Ehlis300888c2016-05-18 13:43:26 -0600938void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
939 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600940 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600941}
942
Tobin Ehlis300888c2016-05-18 13:43:26 -0600943void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600944 if (!immutable_) {
945 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600946 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600947 }
948 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600949}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600950
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700951void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600952 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700953 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700954 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600955 }
956}
957
Tobin Ehlis300888c2016-05-18 13:43:26 -0600958cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
959 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600960 updated = false;
961 descriptor_class = ImageSampler;
962}
963
Tobin Ehlis300888c2016-05-18 13:43:26 -0600964cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
965 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600966 updated = false;
967 descriptor_class = ImageSampler;
968 if (immut) {
969 sampler_ = *immut;
970 immutable_ = true;
971 updated = true;
972 }
973}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600974
Tobin Ehlis300888c2016-05-18 13:43:26 -0600975void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600976 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600977 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600978 sampler_ = image_info.sampler;
979 image_view_ = image_info.imageView;
980 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600981}
982
Tobin Ehlis300888c2016-05-18 13:43:26 -0600983void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600984 if (!immutable_) {
985 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600986 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600987 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600988 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
989 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600990 updated = true;
991 image_view_ = image_view;
992 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600993}
994
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700995void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600996 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600997 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700998 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700999 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001000 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001001 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001002 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001003 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001004 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001005 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001006}
1007
Tobin Ehlis300888c2016-05-18 13:43:26 -06001008cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1009 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001010 updated = false;
1011 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001012 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001013};
1014
Tobin Ehlis300888c2016-05-18 13:43:26 -06001015void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001016 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001017 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001018 image_view_ = image_info.imageView;
1019 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001020}
1021
Tobin Ehlis300888c2016-05-18 13:43:26 -06001022void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001023 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1024 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001025 updated = true;
1026 image_view_ = image_view;
1027 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001028}
1029
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001030void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001031 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001032 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001033 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001034 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001035 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001036}
1037
Tobin Ehlis300888c2016-05-18 13:43:26 -06001038cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1039 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001040 updated = false;
1041 descriptor_class = GeneralBuffer;
1042 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1043 dynamic_ = true;
1044 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1045 storage_ = true;
1046 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1047 dynamic_ = true;
1048 storage_ = true;
1049 }
1050}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001051void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001052 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001053 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001054 buffer_ = buffer_info.buffer;
1055 offset_ = buffer_info.offset;
1056 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001057}
1058
Tobin Ehlis300888c2016-05-18 13:43:26 -06001059void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1060 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001061 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001062 buffer_ = buff_desc->buffer_;
1063 offset_ = buff_desc->offset_;
1064 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001065}
1066
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001067void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001068 auto buffer_node = GetBufferState(dev_data, buffer_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001069 if (buffer_node) core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001070}
1071
Tobin Ehlis300888c2016-05-18 13:43:26 -06001072cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001073 updated = false;
1074 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001075 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001076};
Tobin Ehlis56a30942016-05-19 08:00:00 -06001077
Tobin Ehlis300888c2016-05-18 13:43:26 -06001078void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001079 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001080 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001081}
1082
Tobin Ehlis300888c2016-05-18 13:43:26 -06001083void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1084 updated = true;
1085 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1086}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001087
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001088void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001089 auto bv_state = GetBufferViewState(dev_data, buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001090 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001091 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001092 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001093}
1094
Tobin Ehlis300888c2016-05-18 13:43:26 -06001095// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1096// sets, and then calls their respective Validate[Write|Copy]Update functions.
1097// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1098// be skipped, then true is returned.
1099// If there is no issue with the update, then false is returned.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001100bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data, const layer_data *dev_data,
1101 uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001102 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001103 bool skip_call = false;
1104 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001105 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001106 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001107 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001108 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001109 skip_call |=
1110 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 -06001111 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001112 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
1113 reinterpret_cast<uint64_t &>(dest_set));
1114 } else {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001115 UNIQUE_VALIDATION_ERROR_CODE error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001116 std::string error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001117 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_code, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001118 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 -06001119 reinterpret_cast<uint64_t &>(dest_set), __LINE__, error_code, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001120 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001121 " with error: %s. %s",
1122 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str(), validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001123 }
1124 }
1125 }
1126 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001127 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001128 auto dst_set = p_cds[i].dstSet;
1129 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001130 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1131 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001132 // Object_tracker verifies that src & dest descriptor set are valid
1133 assert(src_node);
1134 assert(dst_node);
1135 UNIQUE_VALIDATION_ERROR_CODE error_code;
1136 std::string error_str;
1137 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_code, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001138 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlisa1712752017-01-04 09:41:47 -07001139 reinterpret_cast<uint64_t &>(dst_set), __LINE__, error_code, "DS",
1140 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
1141 " to Descriptor Set 0x%" PRIx64 " with error: %s. %s",
1142 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str(),
1143 validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001144 }
1145 }
1146 return skip_call;
1147}
1148// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1149// sets, and then calls their respective Perform[Write|Copy]Update functions.
1150// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1151// with the same set of updates.
1152// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1153// calling down the chain.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001154void cvdescriptorset::PerformUpdateDescriptorSets(const layer_data *dev_data, uint32_t write_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001155 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1156 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001157 // Write updates first
1158 uint32_t i = 0;
1159 for (i = 0; i < write_count; ++i) {
1160 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001161 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001162 if (set_node) {
1163 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001164 }
1165 }
1166 // Now copy updates
1167 for (i = 0; i < copy_count; ++i) {
1168 auto dst_set = p_cds[i].dstSet;
1169 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001170 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1171 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001172 if (src_node && dst_node) {
1173 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001174 }
1175 }
1176}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001177// This helper function carries out the state updates for descriptor updates peformed via update templates. It basically collects
1178// data and leverages the PerformUpdateDescriptor helper functions to do this.
1179void cvdescriptorset::PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet,
1180 std::unique_ptr<TEMPLATE_STATE> const &template_state,
1181 const void *pData) {
1182 auto const &create_info = template_state->create_info;
1183
1184 // Create a vector of write structs
1185 std::vector<VkWriteDescriptorSet> desc_writes;
1186 auto layout_obj = GetDescriptorSetLayout(device_data, create_info.descriptorSetLayout);
1187
1188 // Create a WriteDescriptorSet struct for each template update entry
1189 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1190 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1191 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1192 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1193
1194 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1195 desc_writes.emplace_back();
1196 auto &write_entry = desc_writes.back();
1197
1198 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1199 char *update_entry = (char *)(pData) + offset;
1200
1201 if (dst_array_element >= binding_count) {
1202 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001203 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001204 }
1205
1206 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1207 write_entry.pNext = NULL;
1208 write_entry.dstSet = descriptorSet;
1209 write_entry.dstBinding = binding_being_updated;
1210 write_entry.dstArrayElement = dst_array_element;
1211 write_entry.descriptorCount = 1;
1212 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1213
1214 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1215 case VK_DESCRIPTOR_TYPE_SAMPLER:
1216 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1217 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1218 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1219 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1220 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1221 break;
1222
1223 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1224 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1225 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1226 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1227 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1228 break;
1229
1230 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1231 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1232 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1233 break;
1234 default:
1235 assert(0);
1236 break;
1237 }
1238 dst_array_element++;
1239 }
1240 }
1241 PerformUpdateDescriptorSets(device_data, static_cast<uint32_t>(desc_writes.size()), desc_writes.data(), 0, NULL);
1242}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001243// Validate the state for a given write update but don't actually perform the update
1244// If an error would occur for this update, return false and fill in details in error_msg string
1245bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001246 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001247 // Verify idle ds
1248 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001249 // TODO : Re-using Free Idle error code, need write update idle error code
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001250 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001251 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001252 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001253 << " that is in use by a command buffer";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001254 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001255 return false;
1256 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001257 // Verify dst binding exists
1258 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001259 *error_code = VALIDATION_ERROR_00936;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001260 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001261 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001262 *error_msg = error_str.str();
1263 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001264 } else {
1265 // Make sure binding isn't empty
1266 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
1267 *error_code = VALIDATION_ERROR_02348;
1268 std::stringstream error_str;
1269 error_str << "DescriptorSet " << set_ << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
1270 *error_msg = error_str.str();
1271 return false;
1272 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001273 }
1274 // We know that binding is valid, verify update and do update on each descriptor
1275 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
1276 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
1277 if (type != update->descriptorType) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001278 *error_code = VALIDATION_ERROR_00937;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001279 std::stringstream error_str;
1280 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1281 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1282 *error_msg = error_str.str();
1283 return false;
1284 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001285 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001286 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001287 std::stringstream error_str;
1288 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001289 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001290 << " descriptors in that binding and all successive bindings of the set, but update of "
1291 << update->descriptorCount << " descriptors combined with update array element offset of "
1292 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001293 *error_msg = error_str.str();
1294 return false;
1295 }
1296 // Verify consecutive bindings match (if needed)
1297 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001298 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07001299 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001300 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001301 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001302 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001303 // Update is within bounds and consistent so last step is to validate update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001304 if (!VerifyWriteUpdateContents(update, start_idx, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001305 std::stringstream error_str;
1306 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1307 << " failed with error message: " << error_msg->c_str();
1308 *error_msg = error_str.str();
1309 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001310 }
1311 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001312 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001313}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001314// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001315// If there's an error, update the error_msg string with details and return false, else return true
Tobin Ehlis4668dce2016-11-16 09:30:23 -07001316bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001317 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001318 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001319 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001320 std::string error_usage_bit;
1321 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001322 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1323 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
1324 *error_code = VALIDATION_ERROR_00950;
1325 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1326 }
1327 break;
1328 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1329 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1330 *error_code = VALIDATION_ERROR_00951;
1331 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1332 }
1333 break;
1334 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1335 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1336 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1337 *error_code = VALIDATION_ERROR_00946;
1338 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1339 }
1340 break;
1341 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1342 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1343 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1344 *error_code = VALIDATION_ERROR_00947;
1345 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1346 }
1347 break;
1348 default:
1349 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001350 }
1351 if (!error_usage_bit.empty()) {
1352 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001353 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1354 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1355 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001356 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001357 return false;
1358 }
1359 return true;
1360}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001361// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1362// 1. buffer is valid
1363// 2. buffer was created with correct usage flags
1364// 3. offset is less than buffer size
1365// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001366// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001367// 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 -06001368bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001369 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001370 // First make sure that buffer is valid
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001371 auto buffer_node = GetBufferState(device_data_, buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07001372 // Any invalid buffer should already be caught by object_tracker
1373 assert(buffer_node);
Tobin Ehlise1995fc2016-12-22 12:45:09 -07001374 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_02525)) {
Tobin Ehlisde1a0f92016-12-22 12:26:32 -07001375 *error_code = VALIDATION_ERROR_02525;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001376 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001377 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001378 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001379 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001380 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1381 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001382 return false;
1383 }
1384 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001385 if (buffer_info->offset >= buffer_node->createInfo.size) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001386 *error_code = VALIDATION_ERROR_00959;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001387 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001388 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
1389 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001390 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001391 return false;
1392 }
1393 if (buffer_info->range != VK_WHOLE_SIZE) {
1394 // Range must be VK_WHOLE_SIZE or > 0
1395 if (!buffer_info->range) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001396 *error_code = VALIDATION_ERROR_00960;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001397 std::stringstream error_str;
1398 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001399 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001400 return false;
1401 }
1402 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1403 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001404 *error_code = VALIDATION_ERROR_00961;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001405 std::stringstream error_str;
1406 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1407 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001408 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001409 return false;
1410 }
1411 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001412 // Check buffer update sizes against device limits
1413 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1414 auto max_ub_range = limits_.maxUniformBufferRange;
1415 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1416 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
1417 *error_code = VALIDATION_ERROR_00948;
1418 std::stringstream error_str;
1419 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1420 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
1421 *error_msg = error_str.str();
1422 return false;
1423 }
1424 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1425 auto max_sb_range = limits_.maxStorageBufferRange;
1426 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1427 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
1428 *error_code = VALIDATION_ERROR_00949;
1429 std::stringstream error_str;
1430 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1431 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
1432 *error_msg = error_str.str();
1433 return false;
1434 }
1435 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001436 return true;
1437}
1438
Tobin Ehlis300888c2016-05-18 13:43:26 -06001439// Verify that the contents of the update are ok, but don't perform actual update
1440bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001441 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1442 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001443 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001444 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1445 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1446 // Validate image
1447 auto image_view = update->pImageInfo[di].imageView;
1448 auto image_layout = update->pImageInfo[di].imageLayout;
1449 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001450 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001451 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
1452 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001453 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001454 return false;
1455 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001456 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001457 // Intentional fall-through to validate sampler
Tobin Ehlis300888c2016-05-18 13:43:26 -06001458 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001459 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1460 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1461 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
1462 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
1463 *error_code = VALIDATION_ERROR_00942;
1464 std::stringstream error_str;
1465 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1466 << update->pImageInfo[di].sampler << ".";
1467 *error_msg = error_str.str();
1468 return false;
1469 }
1470 } else {
1471 // TODO : Warn here
1472 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001473 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001474 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001475 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001476 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1477 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1478 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1479 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1480 auto image_view = update->pImageInfo[di].imageView;
1481 auto image_layout = update->pImageInfo[di].imageLayout;
1482 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
1483 std::stringstream error_str;
1484 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
1485 *error_msg = error_str.str();
1486 return false;
1487 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001488 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001489 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001490 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001491 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1492 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1493 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1494 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001495 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001496 if (!bv_state) {
1497 *error_code = VALIDATION_ERROR_00940;
1498 std::stringstream error_str;
1499 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1500 *error_msg = error_str.str();
1501 return false;
1502 }
1503 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001504 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001505 std::stringstream error_str;
1506 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
1507 *error_msg = error_str.str();
1508 return false;
1509 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001510 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001511 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001512 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001513 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1514 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1515 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1516 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1517 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1518 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error_code, error_msg)) {
1519 std::stringstream error_str;
1520 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
1521 *error_msg = error_str.str();
1522 return false;
1523 }
1524 }
1525 break;
1526 }
1527 default:
1528 assert(0); // We've already verified update type so should never get here
1529 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001530 }
1531 // All checks passed so update contents are good
1532 return true;
1533}
1534// Verify that the contents of the update are ok, but don't perform actual update
1535bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001536 VkDescriptorType type, uint32_t index,
1537 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1538 std::string *error_msg) const {
1539 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
1540 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06001541 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001542 case PlainSampler: {
1543 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1544 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1545 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
1546 if (!ValidateSampler(update_sampler, device_data_)) {
1547 *error_code = VALIDATION_ERROR_00942;
1548 std::stringstream error_str;
1549 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1550 *error_msg = error_str.str();
1551 return false;
1552 }
1553 } else {
1554 // TODO : Warn here
1555 }
1556 }
1557 break;
1558 }
1559 case ImageSampler: {
1560 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1561 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1562 // First validate sampler
1563 if (!img_samp_desc->IsImmutableSampler()) {
1564 auto update_sampler = img_samp_desc->GetSampler();
1565 if (!ValidateSampler(update_sampler, device_data_)) {
1566 *error_code = VALIDATION_ERROR_00942;
1567 std::stringstream error_str;
1568 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1569 *error_msg = error_str.str();
1570 return false;
1571 }
1572 } else {
1573 // TODO : Warn here
1574 }
1575 // Validate image
1576 auto image_view = img_samp_desc->GetImageView();
1577 auto image_layout = img_samp_desc->GetImageLayout();
1578 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001579 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001580 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001581 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001582 return false;
1583 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001584 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001585 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001586 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001587 case Image: {
1588 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1589 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1590 auto image_view = img_desc->GetImageView();
1591 auto image_layout = img_desc->GetImageLayout();
1592 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001593 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001594 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001595 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001596 return false;
1597 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001598 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001599 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001600 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001601 case TexelBuffer: {
1602 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1603 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001604 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001605 if (!bv_state) {
1606 *error_code = VALIDATION_ERROR_00940;
1607 std::stringstream error_str;
1608 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1609 *error_msg = error_str.str();
1610 return false;
1611 }
1612 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001613 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001614 std::stringstream error_str;
1615 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
1616 *error_msg = error_str.str();
1617 return false;
1618 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001619 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001620 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001621 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001622 case GeneralBuffer: {
1623 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1624 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001625 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001626 std::stringstream error_str;
1627 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
1628 *error_msg = error_str.str();
1629 return false;
1630 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001631 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001632 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001633 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001634 default:
1635 assert(0); // We've already verified update type so should never get here
1636 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001637 }
1638 // All checks passed so update contents are good
1639 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001640}
Tobin Ehlisf320b192017-03-14 11:22:50 -06001641// Update the common AllocateDescriptorSetsData
1642void cvdescriptorset::UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
1643 AllocateDescriptorSetsData *ds_data) {
1644 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
1645 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1646 if (layout) {
1647 ds_data->layout_nodes[i] = layout;
1648 // Count total descriptors required per type
1649 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1650 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
1651 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1652 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1653 }
1654 }
1655 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
1656 }
1657};
Tobin Ehlisee471462016-05-26 11:21:59 -06001658// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlisf320b192017-03-14 11:22:50 -06001659bool cvdescriptorset::ValidateAllocateDescriptorSets(const core_validation::layer_data *dev_data,
1660 const VkDescriptorSetAllocateInfo *p_alloc_info,
1661 const AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001662 bool skip_call = false;
Tobin Ehlisf320b192017-03-14 11:22:50 -06001663 auto report_data = core_validation::GetReportData(dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001664
1665 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001666 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
Tobin Ehlis815e8132016-06-02 13:02:17 -06001667 if (!layout) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001668 skip_call |=
1669 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1670 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1671 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1672 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlisee471462016-05-26 11:21:59 -06001673 }
1674 }
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001675 if (!GetDeviceExtensions(dev_data)->khr_maintenance1_enabled) {
1676 auto pool_state = GetDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
1677 // Track number of descriptorSets allowable in this pool
1678 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001679 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001680 reinterpret_cast<uint64_t &>(pool_state->pool), __LINE__, VALIDATION_ERROR_00911, "DS",
1681 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1682 ". This pool only has %d descriptorSets remaining. %s",
1683 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_state->pool),
1684 pool_state->availableSets, validation_error_map[VALIDATION_ERROR_00911]);
1685 }
1686 // Determine whether descriptor counts are satisfiable
1687 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1688 if (ds_data->required_descriptors_by_type[i] > pool_state->availableDescriptorTypeCount[i]) {
1689 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1690 reinterpret_cast<const uint64_t &>(pool_state->pool), __LINE__, VALIDATION_ERROR_00912, "DS",
1691 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1692 ". This pool only has %d descriptors of this type remaining. %s",
1693 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
1694 reinterpret_cast<uint64_t &>(pool_state->pool), pool_state->availableDescriptorTypeCount[i],
1695 validation_error_map[VALIDATION_ERROR_00912]);
1696 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001697 }
1698 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001699
Tobin Ehlisee471462016-05-26 11:21:59 -06001700 return skip_call;
1701}
1702// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001703void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1704 const VkDescriptorSet *descriptor_sets,
1705 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06001706 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06001707 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001708 const layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001709 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001710 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001711 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001712 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1713 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1714 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001715 /* Create tracking object for each descriptor set; insert into
1716 * global map and the pool's set.
1717 */
1718 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis93f22372016-10-12 14:34:12 -06001719 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i],
1720 dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001721
1722 pool_state->sets.insert(new_ds);
1723 new_ds->in_use.store(0);
1724 (*set_map)[descriptor_sets[i]] = new_ds;
1725 }
1726}