blob: 58fc9ac1fd583776ce3ae6cdded981ea7ae934a7 [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) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600330 if (immut_sampler) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700331 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600332 some_update_ = true; // Immutable samplers are updated at creation
333 } else
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700334 descriptors_.emplace_back(new SamplerDescriptor());
335 }
336 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600337 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700338 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
339 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
340 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600341 if (immut) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700342 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600343 some_update_ = true; // Immutable samplers are updated at creation
344 } else
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700345 descriptors_.emplace_back(new ImageSamplerDescriptor());
346 }
347 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600348 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700349 // ImageDescriptors
350 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
351 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
352 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
353 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
354 descriptors_.emplace_back(new ImageDescriptor(type));
355 break;
356 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
357 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
358 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
359 descriptors_.emplace_back(new TexelDescriptor(type));
360 break;
361 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
362 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
363 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
364 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
365 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
366 descriptors_.emplace_back(new BufferDescriptor(type));
367 break;
368 default:
369 assert(0); // Bad descriptor type specified
370 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600371 }
372 }
373}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600374
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700375cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200376
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700377static std::string string_descriptor_req_view_type(descriptor_req req) {
378 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200379 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
380 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700381 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700382 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200383 }
384 }
385
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700386 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700387
388 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200389}
390
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600391// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
392bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
393 return layout->IsCompatible(p_layout_, error);
394}
Chris Forbes57989132016-07-26 17:06:10 +1200395
Tobin Ehlis3066db62016-08-22 08:12:23 -0600396// 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 -0600397// This includes validating that all descriptors in the given bindings are updated,
398// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
399// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600400bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600401 const std::vector<uint32_t> &dynamic_offsets, const GLOBAL_CB_NODE *cb_node,
402 const char *caller, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200403 for (auto binding_pair : bindings) {
404 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600405 if (!p_layout_->HasBinding(binding)) {
406 std::stringstream error_str;
407 error_str << "Attempting to validate DrawState for binding #" << binding
408 << " which is an invalid binding for this descriptor set.";
409 *error = error_str.str();
410 return false;
411 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600412 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600413 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600414 // Nothing to do for strictly immutable sampler
415 } else {
416 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700417 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700418 for (uint32_t i = start_idx; i <= end_idx; ++i, ++array_idx) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600419 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600420 std::stringstream error_str;
421 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
422 << " is being used in draw but has not been updated.";
423 *error = error_str.str();
424 return false;
425 } else {
Chris Forbes57989132016-07-26 17:06:10 +1200426 auto descriptor_class = descriptors_[i]->GetClass();
427 if (descriptor_class == GeneralBuffer) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600428 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600429 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700430 auto buffer_node = GetBufferState(device_data_, buffer);
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600431 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600432 std::stringstream error_str;
433 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
434 << " references invalid buffer " << buffer << ".";
435 *error = error_str.str();
436 return false;
437 } else {
Tobin Ehlis640a81c2016-11-15 15:37:18 -0700438 for (auto mem_binding : buffer_node->GetBoundMemory()) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700439 if (!GetMemObjInfo(device_data_, mem_binding)) {
Tobin Ehlis640a81c2016-11-15 15:37:18 -0700440 std::stringstream error_str;
441 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
442 << " uses buffer " << buffer << " that references invalid memory " << mem_binding
443 << ".";
444 *error = error_str.str();
445 return false;
446 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600447 }
448 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600449 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600450 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600451 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600452 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
453 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlisa3525e02016-11-17 10:50:52 -0700454 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600455 if (VK_WHOLE_SIZE == range) {
456 if ((dyn_offset + desc_offset) > buffer_size) {
457 std::stringstream error_str;
458 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
459 << " uses buffer " << buffer
460 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
461 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
462 << buffer_size << ".";
463 *error = error_str.str();
464 return false;
465 }
466 } else {
467 if ((dyn_offset + desc_offset + range) > buffer_size) {
468 std::stringstream error_str;
469 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
470 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
471 << " combined with offset " << desc_offset << " and range " << range
472 << " that oversteps the buffer size of " << buffer_size << ".";
473 *error = error_str.str();
474 return false;
475 }
476 }
477 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700478 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
Tobin Ehlisc8266452017-04-07 12:20:30 -0600479 VkImageView image_view;
480 VkImageLayout image_layout;
481 if (descriptor_class == ImageSampler) {
482 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
483 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
484 } else {
485 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
486 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
487 }
Chris Forbes57989132016-07-26 17:06:10 +1200488 auto reqs = binding_pair.second;
489
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700490 auto image_view_state = GetImageViewState(device_data_, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600491 assert(image_view_state);
492 auto image_view_ci = image_view_state->create_info;
Chris Forbes57989132016-07-26 17:06:10 +1200493
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600494 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
Chris Forbes57989132016-07-26 17:06:10 +1200495 // bad view type
496 std::stringstream error_str;
497 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700498 << " requires an image view of type " << string_descriptor_req_view_type(reqs) << " but got "
499 << string_VkImageViewType(image_view_ci.viewType) << ".";
Chris Forbes57989132016-07-26 17:06:10 +1200500 *error = error_str.str();
501 return false;
502 }
503
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700504 auto image_node = GetImageState(device_data_, image_view_ci.image);
Chris Forbes57989132016-07-26 17:06:10 +1200505 assert(image_node);
Tobin Ehlisc8266452017-04-07 12:20:30 -0600506 // Verify Image Layout
507 // TODO: VALIDATION_ERROR_02981 is the error physically closest to the spec language of interest, however
508 // there is no VUID for the actual spec language. Need to file a spec MR to add VU language for:
509 // imageLayout is the layout that the image subresources accessible from imageView will be in at the time
510 // this descriptor is accessed.
511 // Copy first mip level into sub_layers and loop over each mip level to verify layout
512 VkImageSubresourceLayers sub_layers;
513 sub_layers.aspectMask = image_view_ci.subresourceRange.aspectMask;
514 sub_layers.baseArrayLayer = image_view_ci.subresourceRange.baseArrayLayer;
515 sub_layers.layerCount = image_view_ci.subresourceRange.layerCount;
516 bool hit_error = false;
517 for (auto cur_level = image_view_ci.subresourceRange.baseMipLevel;
518 cur_level < image_view_ci.subresourceRange.levelCount; ++cur_level) {
519 sub_layers.mipLevel = cur_level;
520 VerifyImageLayout(device_data_, cb_node, image_node, sub_layers, image_layout,
521 VK_IMAGE_LAYOUT_UNDEFINED, caller, VALIDATION_ERROR_02981, &hit_error);
522 if (hit_error) {
523 *error =
524 "Image layout specified at vkUpdateDescriptorSets() time doesn't match actual image layout at "
525 "time descriptor is used. See previous error callback for specific details.";
526 return false;
527 }
528 }
529 // Verify Sample counts
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700530 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
Chris Forbes57989132016-07-26 17:06:10 +1200531 std::stringstream error_str;
532 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
533 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
534 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
535 *error = error_str.str();
536 return false;
537 }
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700538 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
Chris Forbes57989132016-07-26 17:06:10 +1200539 std::stringstream error_str;
540 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
541 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
542 *error = error_str.str();
543 return false;
544 }
545 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600546 }
547 }
548 }
549 }
550 return true;
551}
Chris Forbes57989132016-07-26 17:06:10 +1200552
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600553// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600554uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600555 std::unordered_set<VkBuffer> *buffer_set,
556 std::unordered_set<VkImageView> *image_set) const {
557 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200558 for (auto binding_pair : bindings) {
559 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600560 // If a binding doesn't exist, skip it
561 if (!p_layout_->HasBinding(binding)) {
562 continue;
563 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600564 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600565 if (descriptors_[start_idx]->IsStorage()) {
566 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600567 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600568 if (descriptors_[start_idx + i]->updated) {
569 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600570 num_updates++;
571 }
572 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600573 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600574 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600575 if (descriptors_[start_idx + i]->updated) {
576 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700577 auto bv_state = GetBufferViewState(device_data_, bufferview);
Tobin Ehlis8b872462016-09-14 08:12:08 -0600578 if (bv_state) {
579 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600580 num_updates++;
581 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600582 }
583 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600584 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600585 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600586 if (descriptors_[start_idx + i]->updated) {
587 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600588 num_updates++;
589 }
590 }
591 }
592 }
593 }
594 return num_updates;
595}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600596// Set is being deleted or updates so invalidate all bound cmd buffers
597void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Tobin Ehlisfe5731a2016-11-21 08:31:01 -0700598 core_validation::invalidateCommandBuffers(device_data_, cb_bindings,
Mark Lobodzinski33826372017-04-13 11:10:11 -0600599 {reinterpret_cast<uint64_t &>(set_), kVulkanObjectTypeDescriptorSet });
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600600}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600601// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600602void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700603 // Perform update on a per-binding basis as consecutive updates roll over to next binding
604 auto descriptors_remaining = update->descriptorCount;
605 auto binding_being_updated = update->dstBinding;
606 auto offset = update->dstArrayElement;
607 while (descriptors_remaining) {
608 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
609 auto global_idx = p_layout_->GetGlobalStartIndexFromBinding(binding_being_updated) + offset;
610 // Loop over the updates for a single binding at a time
611 for (uint32_t di = 0; di < update_count; ++di) {
612 descriptors_[global_idx + di]->WriteUpdate(update, di);
613 }
614 // Roll over to next binding in case of consecutive update
615 descriptors_remaining -= update_count;
616 offset = 0;
617 binding_being_updated++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600618 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700619 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600620
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600621 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600622}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600623// Validate Copy update
624bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600625 const DescriptorSet *src_set, UNIQUE_VALIDATION_ERROR_CODE *error_code,
626 std::string *error_msg) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600627 // Verify idle ds
628 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700629 // TODO : Re-using Free Idle error code, need copy update idle error code
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600630 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600631 std::stringstream error_str;
632 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700633 << " that is in use by a command buffer";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600634 *error_msg = error_str.str();
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600635 return false;
636 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600637 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600638 *error_code = VALIDATION_ERROR_00966;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600639 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700640 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600641 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600642 return false;
643 }
644 if (!src_set->HasBinding(update->srcBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600645 *error_code = VALIDATION_ERROR_00964;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600646 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700647 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600648 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600649 return false;
650 }
651 // src & dst set bindings are valid
652 // Check bounds of src & dst
653 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
654 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
655 // SRC update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600656 *error_code = VALIDATION_ERROR_00965;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600657 std::stringstream error_str;
658 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
659 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
660 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700661 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600662 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600663 return false;
664 }
665 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
666 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
667 // DST update out of bounds
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600668 *error_code = VALIDATION_ERROR_00967;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600669 std::stringstream error_str;
670 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
671 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
672 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700673 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600674 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600675 return false;
676 }
677 // Check that types match
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600678 // TODO : Base default error case going from here is VALIDATION_ERROR_00968 which covers all consistency issues, need more
679 // fine-grained error codes
680 *error_code = VALIDATION_ERROR_00968;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600681 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
682 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
683 if (src_type != dst_type) {
684 std::stringstream error_str;
685 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
686 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700687 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600688 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600689 return false;
690 }
691 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600692 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600693 "copy update from", src_set->GetSet(), error_msg)) ||
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600694 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600695 set_, error_msg))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600696 return false;
697 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600698 // First make sure source descriptors are updated
699 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
700 if (!src_set->descriptors_[src_start_idx + i]) {
701 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -0700702 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding
703 << " but descriptor at array offset " << update->srcArrayElement + i << " has not been updated";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600704 *error_msg = error_str.str();
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600705 return false;
706 }
707 }
708 // Update parameters all look good and descriptor updated so verify update contents
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700709 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error_code, error_msg)) return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600710
711 // All checks passed so update is good
712 return true;
713}
714// Perform Copy update
715void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600716 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
717 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600718 // Update parameters all look good so perform update
719 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600720 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600721 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700722 if (update->descriptorCount) some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600723
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600724 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600725}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600726
Tobin Ehlisf9519102016-08-17 09:49:13 -0600727// Bind cb_node to this set and this set to cb_node.
728// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
729// to be used in a draw by the given cb_node
Tobin Ehlis276d3d32016-12-21 09:21:06 -0700730void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node,
Tobin Ehlis022528b2016-12-29 12:22:32 -0700731 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600732 // bind cb to this descriptor set
733 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600734 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Mark Lobodzinski33826372017-04-13 11:10:11 -0600735 cb_node->object_bindings.insert({reinterpret_cast<uint64_t &>(set_), kVulkanObjectTypeDescriptorSet });
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600736 pool_state_->cb_bindings.insert(cb_node);
737 cb_node->object_bindings.insert(
Mark Lobodzinski33826372017-04-13 11:10:11 -0600738 {reinterpret_cast<uint64_t &>(pool_state_->pool), kVulkanObjectTypeDescriptorPool });
Tobin Ehlisf9519102016-08-17 09:49:13 -0600739 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
740 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -0700741 for (auto binding_req_pair : binding_req_map) {
742 auto binding = binding_req_pair.first;
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600743 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
744 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
745 for (uint32_t i = start_idx; i <= end_idx; ++i) {
746 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
747 }
748 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600749}
750
Tobin Ehlis300888c2016-05-18 13:43:26 -0600751cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600752 updated = false;
753 descriptor_class = PlainSampler;
754};
755
Tobin Ehlis300888c2016-05-18 13:43:26 -0600756cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600757 updated = false;
758 descriptor_class = PlainSampler;
759 if (immut) {
760 sampler_ = *immut;
761 immutable_ = true;
762 updated = true;
763 }
764}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600765// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700766bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const layer_data *dev_data) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700767 return (GetSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600768}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600769
Tobin Ehlis554bf382016-05-24 11:14:43 -0600770bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700771 const layer_data *dev_data, UNIQUE_VALIDATION_ERROR_CODE *error_code,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600772 std::string *error_msg) {
773 // TODO : Defaulting to 00943 for all cases here. Need to create new error codes for various cases.
774 *error_code = VALIDATION_ERROR_00943;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700775 auto iv_state = GetImageViewState(dev_data, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600776 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600777 std::stringstream error_str;
778 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600779 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600780 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600781 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600782 // 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 -0600783 // Validate that imageLayout is compatible with aspect_mask and image format
784 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600785 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
786 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600787 VkFormat format = VK_FORMAT_MAX_ENUM;
788 VkImageUsageFlags usage = 0;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700789 auto image_node = GetImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600790 if (image_node) {
791 format = image_node->createInfo.format;
792 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600793 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700794 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
795 // the error here occurs is if memory bound to a created imageView has been freed.
Tobin Ehlise1995fc2016-12-22 12:45:09 -0700796 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_02524)) {
Tobin Ehlisde1a0f92016-12-22 12:26:32 -0700797 *error_code = VALIDATION_ERROR_02524;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600798 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600799 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -0600800 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600801 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600802 // Also need to check the swapchains.
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700803 auto swapchain = GetSwapchainFromImage(dev_data, image);
Tobin Ehlis969a5262016-06-02 12:13:32 -0600804 if (swapchain) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700805 auto swapchain_node = GetSwapchainNode(dev_data, swapchain);
Tobin Ehlis4e380592016-06-02 12:41:47 -0600806 if (swapchain_node) {
807 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600808 }
809 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600810 }
811 // First validate that format and layout are compatible
812 if (format == VK_FORMAT_MAX_ENUM) {
813 std::stringstream error_str;
814 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600815 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600816 return false;
817 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600818 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
819 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -0600820 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -0600821 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700822 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
823 // Only Color bit must be set
824 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600825 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700826 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
827 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600828 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600829 return false;
830 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700831 // format must NOT be DS
832 if (ds) {
833 std::stringstream error_str;
834 error_str << "ImageView (" << image_view
835 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
836 << string_VkFormat(format) << " which is not a color format.";
837 *error_msg = error_str.str();
838 return false;
839 }
840 break;
841 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
842 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
843 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600844 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
845 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
846 // both must NOT be set
847 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700848 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600849 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600850 return false;
851 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700852 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
853 // Neither were set
854 std::stringstream error_str;
855 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
856 << " but does not have STENCIL or DEPTH aspects set";
857 *error_msg = error_str.str();
858 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600859 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700860 // format must be DS
861 if (!ds) {
862 std::stringstream error_str;
863 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
864 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
865 *error_msg = error_str.str();
866 return false;
867 }
868 break;
869 default:
870 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
871 if (ds) {
872 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
873 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
874 // both must NOT be set
875 std::stringstream error_str;
876 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
877 << " and is using depth/stencil image of format " << string_VkFormat(format)
878 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
879 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
880 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
881 "reads respectively.";
882 *error_msg = error_str.str();
883 return false;
884 }
885 }
886 }
887 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600888 }
889 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -0600890 // 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 -0600891 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
892 // under vkCreateImage()
893 // TODO : Need to also validate case VALIDATION_ERROR_00952 where STORAGE_IMAGE & INPUT_ATTACH types must have been created with
894 // identify swizzle
Tobin Ehlis1809f912016-05-25 09:24:36 -0600895 std::string error_usage_bit;
896 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700897 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
898 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
899 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
900 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
901 }
902 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600903 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700904 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
905 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
906 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
907 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
908 std::stringstream error_str;
909 // TODO : Need to create custom enum error code for this case
910 error_str
911 << "ImageView (" << image_view << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
912 << string_VkImageLayout(image_layout)
913 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage images can "
914 "only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
915 *error_msg = error_str.str();
916 return false;
917 }
918 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600919 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700920 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
921 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
922 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
923 }
924 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600925 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700926 default:
927 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600928 }
929 if (!error_usage_bit.empty()) {
930 std::stringstream error_str;
931 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
932 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
933 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600934 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600935 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600936 }
937 return true;
938}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600939
Tobin Ehlis300888c2016-05-18 13:43:26 -0600940void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
941 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600942 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600943}
944
Tobin Ehlis300888c2016-05-18 13:43:26 -0600945void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600946 if (!immutable_) {
947 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600948 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600949 }
950 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600951}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600952
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700953void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600954 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700955 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700956 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600957 }
958}
959
Tobin Ehlis300888c2016-05-18 13:43:26 -0600960cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
961 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600962 updated = false;
963 descriptor_class = ImageSampler;
964}
965
Tobin Ehlis300888c2016-05-18 13:43:26 -0600966cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
967 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600968 updated = false;
969 descriptor_class = ImageSampler;
970 if (immut) {
971 sampler_ = *immut;
972 immutable_ = true;
973 updated = true;
974 }
975}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600976
Tobin Ehlis300888c2016-05-18 13:43:26 -0600977void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600978 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600979 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600980 sampler_ = image_info.sampler;
981 image_view_ = image_info.imageView;
982 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600983}
984
Tobin Ehlis300888c2016-05-18 13:43:26 -0600985void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600986 if (!immutable_) {
987 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600988 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600989 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600990 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
991 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600992 updated = true;
993 image_view_ = image_view;
994 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600995}
996
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700997void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600998 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600999 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001000 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001001 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001002 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001003 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001004 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001005 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001006 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001007 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001008}
1009
Tobin Ehlis300888c2016-05-18 13:43:26 -06001010cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1011 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001012 updated = false;
1013 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001014 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001015};
1016
Tobin Ehlis300888c2016-05-18 13:43:26 -06001017void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001018 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001019 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001020 image_view_ = image_info.imageView;
1021 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001022}
1023
Tobin Ehlis300888c2016-05-18 13:43:26 -06001024void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001025 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1026 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001027 updated = true;
1028 image_view_ = image_view;
1029 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001030}
1031
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001032void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001033 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001034 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001035 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001036 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001037 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001038}
1039
Tobin Ehlis300888c2016-05-18 13:43:26 -06001040cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1041 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001042 updated = false;
1043 descriptor_class = GeneralBuffer;
1044 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1045 dynamic_ = true;
1046 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1047 storage_ = true;
1048 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1049 dynamic_ = true;
1050 storage_ = true;
1051 }
1052}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001053void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001054 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001055 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001056 buffer_ = buffer_info.buffer;
1057 offset_ = buffer_info.offset;
1058 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001059}
1060
Tobin Ehlis300888c2016-05-18 13:43:26 -06001061void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1062 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001063 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001064 buffer_ = buff_desc->buffer_;
1065 offset_ = buff_desc->offset_;
1066 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001067}
1068
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001069void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001070 auto buffer_node = GetBufferState(dev_data, buffer_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001071 if (buffer_node) core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001072}
1073
Tobin Ehlis300888c2016-05-18 13:43:26 -06001074cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001075 updated = false;
1076 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001077 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001078};
Tobin Ehlis56a30942016-05-19 08:00:00 -06001079
Tobin Ehlis300888c2016-05-18 13:43:26 -06001080void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001081 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001082 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001083}
1084
Tobin Ehlis300888c2016-05-18 13:43:26 -06001085void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1086 updated = true;
1087 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1088}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001089
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001090void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001091 auto bv_state = GetBufferViewState(dev_data, buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001092 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001093 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001094 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001095}
1096
Tobin Ehlis300888c2016-05-18 13:43:26 -06001097// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1098// sets, and then calls their respective Validate[Write|Copy]Update functions.
1099// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1100// be skipped, then true is returned.
1101// If there is no issue with the update, then false is returned.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001102bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data, const layer_data *dev_data,
1103 uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001104 const VkCopyDescriptorSet *p_cds) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001105 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001106 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001107 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001108 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001109 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001110 if (!set_node) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001111 skip |=
Tobin Ehlis300888c2016-05-18 13:43:26 -06001112 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 -06001113 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001114 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
1115 reinterpret_cast<uint64_t &>(dest_set));
1116 } else {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001117 UNIQUE_VALIDATION_ERROR_CODE error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001118 std::string error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001119 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001120 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1121 reinterpret_cast<uint64_t &>(dest_set), __LINE__, error_code, "DS",
1122 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
1123 " with error: %s. %s",
1124 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str(), validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001125 }
1126 }
1127 }
1128 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001129 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001130 auto dst_set = p_cds[i].dstSet;
1131 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001132 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1133 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001134 // Object_tracker verifies that src & dest descriptor set are valid
1135 assert(src_node);
1136 assert(dst_node);
1137 UNIQUE_VALIDATION_ERROR_CODE error_code;
1138 std::string error_str;
1139 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001140 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
1141 reinterpret_cast<uint64_t &>(dst_set), __LINE__, error_code, "DS",
1142 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
1143 " to Descriptor Set 0x%" PRIx64 " with error: %s. %s",
1144 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str(),
1145 validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001146 }
1147 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001148 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001149}
1150// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1151// sets, and then calls their respective Perform[Write|Copy]Update functions.
1152// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1153// with the same set of updates.
1154// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1155// calling down the chain.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001156void cvdescriptorset::PerformUpdateDescriptorSets(const layer_data *dev_data, uint32_t write_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001157 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1158 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001159 // Write updates first
1160 uint32_t i = 0;
1161 for (i = 0; i < write_count; ++i) {
1162 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001163 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001164 if (set_node) {
1165 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001166 }
1167 }
1168 // Now copy updates
1169 for (i = 0; i < copy_count; ++i) {
1170 auto dst_set = p_cds[i].dstSet;
1171 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001172 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1173 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001174 if (src_node && dst_node) {
1175 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001176 }
1177 }
1178}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001179// This helper function carries out the state updates for descriptor updates peformed via update templates. It basically collects
1180// data and leverages the PerformUpdateDescriptor helper functions to do this.
1181void cvdescriptorset::PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet,
1182 std::unique_ptr<TEMPLATE_STATE> const &template_state,
1183 const void *pData) {
1184 auto const &create_info = template_state->create_info;
1185
1186 // Create a vector of write structs
1187 std::vector<VkWriteDescriptorSet> desc_writes;
1188 auto layout_obj = GetDescriptorSetLayout(device_data, create_info.descriptorSetLayout);
1189
1190 // Create a WriteDescriptorSet struct for each template update entry
1191 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1192 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1193 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1194 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1195
1196 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1197 desc_writes.emplace_back();
1198 auto &write_entry = desc_writes.back();
1199
1200 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1201 char *update_entry = (char *)(pData) + offset;
1202
1203 if (dst_array_element >= binding_count) {
1204 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001205 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001206 }
1207
1208 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1209 write_entry.pNext = NULL;
1210 write_entry.dstSet = descriptorSet;
1211 write_entry.dstBinding = binding_being_updated;
1212 write_entry.dstArrayElement = dst_array_element;
1213 write_entry.descriptorCount = 1;
1214 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1215
1216 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1217 case VK_DESCRIPTOR_TYPE_SAMPLER:
1218 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1219 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1220 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1221 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1222 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1223 break;
1224
1225 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1226 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1227 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1228 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1229 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1230 break;
1231
1232 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1233 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1234 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1235 break;
1236 default:
1237 assert(0);
1238 break;
1239 }
1240 dst_array_element++;
1241 }
1242 }
1243 PerformUpdateDescriptorSets(device_data, static_cast<uint32_t>(desc_writes.size()), desc_writes.data(), 0, NULL);
1244}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001245// Validate the state for a given write update but don't actually perform the update
1246// If an error would occur for this update, return false and fill in details in error_msg string
1247bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001248 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001249 // Verify idle ds
1250 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001251 // TODO : Re-using Free Idle error code, need write update idle error code
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001252 *error_code = VALIDATION_ERROR_00919;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001253 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001254 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001255 << " that is in use by a command buffer";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001256 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001257 return false;
1258 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001259 // Verify dst binding exists
1260 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001261 *error_code = VALIDATION_ERROR_00936;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001262 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001263 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001264 *error_msg = error_str.str();
1265 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001266 } else {
1267 // Make sure binding isn't empty
1268 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
1269 *error_code = VALIDATION_ERROR_02348;
1270 std::stringstream error_str;
1271 error_str << "DescriptorSet " << set_ << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
1272 *error_msg = error_str.str();
1273 return false;
1274 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001275 }
1276 // We know that binding is valid, verify update and do update on each descriptor
1277 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
1278 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
1279 if (type != update->descriptorType) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001280 *error_code = VALIDATION_ERROR_00937;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001281 std::stringstream error_str;
1282 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1283 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1284 *error_msg = error_str.str();
1285 return false;
1286 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001287 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001288 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001289 std::stringstream error_str;
1290 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001291 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001292 << " descriptors in that binding and all successive bindings of the set, but update of "
1293 << update->descriptorCount << " descriptors combined with update array element offset of "
1294 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001295 *error_msg = error_str.str();
1296 return false;
1297 }
1298 // Verify consecutive bindings match (if needed)
1299 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001300 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07001301 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001302 *error_code = VALIDATION_ERROR_00938;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001303 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001304 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001305 // Update is within bounds and consistent so last step is to validate update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001306 if (!VerifyWriteUpdateContents(update, start_idx, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001307 std::stringstream error_str;
1308 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1309 << " failed with error message: " << error_msg->c_str();
1310 *error_msg = error_str.str();
1311 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001312 }
1313 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001314 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001315}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001316// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001317// 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 -07001318bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001319 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001320 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001321 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001322 std::string error_usage_bit;
1323 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001324 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1325 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
1326 *error_code = VALIDATION_ERROR_00950;
1327 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1328 }
1329 break;
1330 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1331 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1332 *error_code = VALIDATION_ERROR_00951;
1333 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1334 }
1335 break;
1336 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1337 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1338 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1339 *error_code = VALIDATION_ERROR_00946;
1340 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1341 }
1342 break;
1343 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1344 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1345 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1346 *error_code = VALIDATION_ERROR_00947;
1347 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1348 }
1349 break;
1350 default:
1351 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001352 }
1353 if (!error_usage_bit.empty()) {
1354 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001355 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1356 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1357 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001358 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001359 return false;
1360 }
1361 return true;
1362}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001363// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1364// 1. buffer is valid
1365// 2. buffer was created with correct usage flags
1366// 3. offset is less than buffer size
1367// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001368// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001369// 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 -06001370bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001371 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001372 // First make sure that buffer is valid
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001373 auto buffer_node = GetBufferState(device_data_, buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07001374 // Any invalid buffer should already be caught by object_tracker
1375 assert(buffer_node);
Tobin Ehlise1995fc2016-12-22 12:45:09 -07001376 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_02525)) {
Tobin Ehlisde1a0f92016-12-22 12:26:32 -07001377 *error_code = VALIDATION_ERROR_02525;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001378 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001379 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001380 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001381 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001382 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1383 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001384 return false;
1385 }
1386 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001387 if (buffer_info->offset >= buffer_node->createInfo.size) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001388 *error_code = VALIDATION_ERROR_00959;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001389 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001390 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
1391 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001392 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001393 return false;
1394 }
1395 if (buffer_info->range != VK_WHOLE_SIZE) {
1396 // Range must be VK_WHOLE_SIZE or > 0
1397 if (!buffer_info->range) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001398 *error_code = VALIDATION_ERROR_00960;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001399 std::stringstream error_str;
1400 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001401 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001402 return false;
1403 }
1404 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1405 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001406 *error_code = VALIDATION_ERROR_00961;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001407 std::stringstream error_str;
1408 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1409 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001410 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001411 return false;
1412 }
1413 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001414 // Check buffer update sizes against device limits
1415 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1416 auto max_ub_range = limits_.maxUniformBufferRange;
1417 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1418 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
1419 *error_code = VALIDATION_ERROR_00948;
1420 std::stringstream error_str;
1421 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1422 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
1423 *error_msg = error_str.str();
1424 return false;
1425 }
1426 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1427 auto max_sb_range = limits_.maxStorageBufferRange;
1428 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1429 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
1430 *error_code = VALIDATION_ERROR_00949;
1431 std::stringstream error_str;
1432 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1433 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
1434 *error_msg = error_str.str();
1435 return false;
1436 }
1437 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001438 return true;
1439}
1440
Tobin Ehlis300888c2016-05-18 13:43:26 -06001441// Verify that the contents of the update are ok, but don't perform actual update
1442bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001443 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1444 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001445 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001446 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1447 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1448 // Validate image
1449 auto image_view = update->pImageInfo[di].imageView;
1450 auto image_layout = update->pImageInfo[di].imageLayout;
1451 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001452 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001453 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
1454 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001455 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001456 return false;
1457 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001458 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001459 // Intentional fall-through to validate sampler
Tobin Ehlis300888c2016-05-18 13:43:26 -06001460 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001461 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1462 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1463 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
1464 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
1465 *error_code = VALIDATION_ERROR_00942;
1466 std::stringstream error_str;
1467 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1468 << update->pImageInfo[di].sampler << ".";
1469 *error_msg = error_str.str();
1470 return false;
1471 }
1472 } else {
1473 // TODO : Warn here
1474 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001475 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001476 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001477 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001478 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1479 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1480 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1481 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1482 auto image_view = update->pImageInfo[di].imageView;
1483 auto image_layout = update->pImageInfo[di].imageLayout;
1484 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
1485 std::stringstream error_str;
1486 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
1487 *error_msg = error_str.str();
1488 return false;
1489 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001490 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001491 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001492 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001493 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1494 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1495 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1496 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001497 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001498 if (!bv_state) {
1499 *error_code = VALIDATION_ERROR_00940;
1500 std::stringstream error_str;
1501 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1502 *error_msg = error_str.str();
1503 return false;
1504 }
1505 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001506 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001507 std::stringstream error_str;
1508 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
1509 *error_msg = error_str.str();
1510 return false;
1511 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001512 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001513 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001514 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001515 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1516 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1517 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1518 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1519 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1520 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error_code, error_msg)) {
1521 std::stringstream error_str;
1522 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
1523 *error_msg = error_str.str();
1524 return false;
1525 }
1526 }
1527 break;
1528 }
1529 default:
1530 assert(0); // We've already verified update type so should never get here
1531 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001532 }
1533 // All checks passed so update contents are good
1534 return true;
1535}
1536// Verify that the contents of the update are ok, but don't perform actual update
1537bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001538 VkDescriptorType type, uint32_t index,
1539 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1540 std::string *error_msg) const {
1541 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
1542 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06001543 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001544 case PlainSampler: {
1545 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1546 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1547 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
1548 if (!ValidateSampler(update_sampler, device_data_)) {
1549 *error_code = VALIDATION_ERROR_00942;
1550 std::stringstream error_str;
1551 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1552 *error_msg = error_str.str();
1553 return false;
1554 }
1555 } else {
1556 // TODO : Warn here
1557 }
1558 }
1559 break;
1560 }
1561 case ImageSampler: {
1562 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1563 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1564 // First validate sampler
1565 if (!img_samp_desc->IsImmutableSampler()) {
1566 auto update_sampler = img_samp_desc->GetSampler();
1567 if (!ValidateSampler(update_sampler, device_data_)) {
1568 *error_code = VALIDATION_ERROR_00942;
1569 std::stringstream error_str;
1570 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1571 *error_msg = error_str.str();
1572 return false;
1573 }
1574 } else {
1575 // TODO : Warn here
1576 }
1577 // Validate image
1578 auto image_view = img_samp_desc->GetImageView();
1579 auto image_layout = img_samp_desc->GetImageLayout();
1580 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001581 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001582 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001583 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001584 return false;
1585 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001586 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001587 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001588 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001589 case Image: {
1590 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1591 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1592 auto image_view = img_desc->GetImageView();
1593 auto image_layout = img_desc->GetImageLayout();
1594 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001595 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001596 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001597 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001598 return false;
1599 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001600 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001601 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001602 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001603 case TexelBuffer: {
1604 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1605 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001606 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001607 if (!bv_state) {
1608 *error_code = VALIDATION_ERROR_00940;
1609 std::stringstream error_str;
1610 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1611 *error_msg = error_str.str();
1612 return false;
1613 }
1614 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001615 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001616 std::stringstream error_str;
1617 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
1618 *error_msg = error_str.str();
1619 return false;
1620 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001621 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001622 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001623 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001624 case GeneralBuffer: {
1625 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1626 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001627 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001628 std::stringstream error_str;
1629 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
1630 *error_msg = error_str.str();
1631 return false;
1632 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001633 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001634 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001635 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001636 default:
1637 assert(0); // We've already verified update type so should never get here
1638 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001639 }
1640 // All checks passed so update contents are good
1641 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001642}
Tobin Ehlisf320b192017-03-14 11:22:50 -06001643// Update the common AllocateDescriptorSetsData
1644void cvdescriptorset::UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
1645 AllocateDescriptorSetsData *ds_data) {
1646 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
1647 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1648 if (layout) {
1649 ds_data->layout_nodes[i] = layout;
1650 // Count total descriptors required per type
1651 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1652 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
1653 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1654 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1655 }
1656 }
1657 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
1658 }
1659};
Tobin Ehlisee471462016-05-26 11:21:59 -06001660// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlisf320b192017-03-14 11:22:50 -06001661bool cvdescriptorset::ValidateAllocateDescriptorSets(const core_validation::layer_data *dev_data,
1662 const VkDescriptorSetAllocateInfo *p_alloc_info,
1663 const AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001664 bool skip = false;
Tobin Ehlisf320b192017-03-14 11:22:50 -06001665 auto report_data = core_validation::GetReportData(dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001666
1667 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001668 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
Tobin Ehlis815e8132016-06-02 13:02:17 -06001669 if (!layout) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001670 skip |=
Tobin Ehlisee471462016-05-26 11:21:59 -06001671 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1672 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1673 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1674 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlisee471462016-05-26 11:21:59 -06001675 }
1676 }
Chris Forbes481df4f2017-05-02 14:18:07 -07001677 if (!GetDeviceExtensions(dev_data)->khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001678 auto pool_state = GetDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
1679 // Track number of descriptorSets allowable in this pool
1680 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001681 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1682 reinterpret_cast<uint64_t &>(pool_state->pool), __LINE__, VALIDATION_ERROR_00911, "DS",
1683 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1684 ". This pool only has %d descriptorSets remaining. %s",
1685 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_state->pool),
1686 pool_state->availableSets, validation_error_map[VALIDATION_ERROR_00911]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001687 }
1688 // Determine whether descriptor counts are satisfiable
1689 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1690 if (ds_data->required_descriptors_by_type[i] > pool_state->availableDescriptorTypeCount[i]) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001691 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1692 reinterpret_cast<const uint64_t &>(pool_state->pool), __LINE__, VALIDATION_ERROR_00912, "DS",
1693 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1694 ". This pool only has %d descriptors of this type remaining. %s",
1695 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
1696 reinterpret_cast<uint64_t &>(pool_state->pool), pool_state->availableDescriptorTypeCount[i],
1697 validation_error_map[VALIDATION_ERROR_00912]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001698 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001699 }
1700 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001701
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001702 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06001703}
1704// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001705void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1706 const VkDescriptorSet *descriptor_sets,
1707 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06001708 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06001709 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001710 const layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001711 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001712 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001713 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001714 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1715 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1716 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001717 /* Create tracking object for each descriptor set; insert into
1718 * global map and the pool's set.
1719 */
1720 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis93f22372016-10-12 14:34:12 -06001721 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i],
1722 dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001723
1724 pool_state->sets.insert(new_ds);
1725 new_ds->in_use.store(0);
1726 (*set_map)[descriptor_sets[i]] = new_ds;
1727 }
1728}