blob: 4c1275efd910a525292babb171e89d26af72c80e [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>
John Zulaufc483f442017-12-15 14:02:06 -070019 * John Zulauf <jzulauf@lunarg.com>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060020 */
21
Tobin Ehlisf922ef82016-11-30 10:19:14 -070022// Allow use of STL min and max functions in Windows
23#define NOMINMAX
24
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060025#include "descriptor_sets.h"
26#include "vk_enum_string_helper.h"
27#include "vk_safe_struct.h"
Tobin Ehlisc8266452017-04-07 12:20:30 -060028#include "buffer_validation.h"
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060029#include <sstream>
Mark Lobodzinski2eee5d82016-12-02 15:33:18 -070030#include <algorithm>
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060031
John Zulauf508d13a2018-01-05 15:10:34 -070032struct BindingNumCmp {
33 bool operator()(const VkDescriptorSetLayoutBinding *a, const VkDescriptorSetLayoutBinding *b) {
34 return a->binding < b->binding;
35 }
36};
37
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060038// Construct DescriptorSetLayout instance from given create info
John Zulauf48a6a702017-12-22 17:14:54 -070039// Proactively reserve and resize as possible, as the reallocation was visible in profiling
Tobin Ehlis154c2692016-10-25 09:36:53 -060040cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060041 const VkDescriptorSetLayout layout)
John Zulauf508d13a2018-01-05 15:10:34 -070042 : layout_(layout), flags_(p_create_info->flags), binding_count_(0), descriptor_count_(0), dynamic_descriptor_count_(0) {
John Zulauf48a6a702017-12-22 17:14:54 -070043 binding_type_stats_ = {0, 0, 0};
John Zulauf508d13a2018-01-05 15:10:34 -070044 std::set<const VkDescriptorSetLayoutBinding *, BindingNumCmp> sorted_bindings;
45 const uint32_t input_bindings_count = p_create_info->bindingCount;
46 // Sort the input bindings in binding number order, eliminating duplicates
47 for (uint32_t i = 0; i < input_bindings_count; i++) {
48 sorted_bindings.insert(p_create_info->pBindings + i);
John Zulaufb6d71202017-12-22 16:47:09 -070049 }
50
51 // Store the create info in the sorted order from above
Tobin Ehlisa3525e02016-11-17 10:50:52 -070052 std::map<uint32_t, uint32_t> binding_to_dyn_count;
John Zulauf508d13a2018-01-05 15:10:34 -070053 uint32_t index = 0;
54 binding_count_ = static_cast<uint32_t>(sorted_bindings.size());
55 bindings_.reserve(binding_count_);
56 binding_to_index_map_.reserve(binding_count_);
57 for (auto input_binding : sorted_bindings) {
58 // Add to binding and map, s.t. it is robust to invalid duplication of binding_num
59 const auto binding_num = input_binding->binding;
60 binding_to_index_map_[binding_num] = index++;
61 bindings_.emplace_back(input_binding);
62 auto &binding_info = bindings_.back();
63
John Zulaufb6d71202017-12-22 16:47:09 -070064 descriptor_count_ += binding_info.descriptorCount;
65 if (binding_info.descriptorCount > 0) {
66 non_empty_bindings_.insert(binding_num);
Tobin Ehlis9637fb22016-12-12 15:59:34 -070067 }
John Zulaufb6d71202017-12-22 16:47:09 -070068
69 if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
70 binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
71 binding_to_dyn_count[binding_num] = binding_info.descriptorCount;
72 dynamic_descriptor_count_ += binding_info.descriptorCount;
John Zulauf48a6a702017-12-22 17:14:54 -070073 binding_type_stats_.dynamic_buffer_count++;
74 } else if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
75 (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
76 binding_type_stats_.non_dynamic_buffer_count++;
77 } else {
78 binding_type_stats_.image_sampler_count++;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060079 }
80 }
Tobin Ehlis9637fb22016-12-12 15:59:34 -070081 assert(bindings_.size() == binding_count_);
82 uint32_t global_index = 0;
John Zulaufb6d71202017-12-22 16:47:09 -070083 binding_to_global_index_range_map_.reserve(binding_count_);
84 // Vector order is finalized so create maps of bindings to descriptors and descriptors to indices
Tobin Ehlis9637fb22016-12-12 15:59:34 -070085 for (uint32_t i = 0; i < binding_count_; ++i) {
86 auto binding_num = bindings_[i].binding;
John Zulaufc483f442017-12-15 14:02:06 -070087 auto final_index = global_index + bindings_[i].descriptorCount;
88 binding_to_global_index_range_map_[binding_num] = IndexRange(global_index, final_index);
John Zulaufb6d71202017-12-22 16:47:09 -070089 if (final_index != global_index) {
90 global_start_to_index_map_[global_index] = i;
91 }
John Zulaufc483f442017-12-15 14:02:06 -070092 global_index = final_index;
Tobin Ehlis9637fb22016-12-12 15:59:34 -070093 }
John Zulaufb6d71202017-12-22 16:47:09 -070094
Tobin Ehlisa3525e02016-11-17 10:50:52 -070095 // Now create dyn offset array mapping for any dynamic descriptors
96 uint32_t dyn_array_idx = 0;
John Zulaufb6d71202017-12-22 16:47:09 -070097 binding_to_dynamic_array_idx_map_.reserve(binding_to_dyn_count.size());
Tobin Ehlisa3525e02016-11-17 10:50:52 -070098 for (const auto &bc_pair : binding_to_dyn_count) {
99 binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
100 dyn_array_idx += bc_pair.second;
101 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600102}
Tobin Ehlis154c2692016-10-25 09:36:53 -0600103
104// Validate descriptor set layout create info
105bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(debug_report_data *report_data,
106 const VkDescriptorSetLayoutCreateInfo *create_info) {
107 bool skip = false;
108 std::unordered_set<uint32_t> bindings;
109 for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
Tobin Ehlisfdcb63f2016-10-25 20:56:47 -0600110 if (!bindings.insert(create_info->pBindings[i].binding).second) {
Tobin Ehlis154c2692016-10-25 09:36:53 -0600111 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600112 VALIDATION_ERROR_0500022e, "DS", "duplicated binding number in VkDescriptorSetLayoutBinding. %s",
113 validation_error_map[VALIDATION_ERROR_0500022e]);
Tobin Ehlis154c2692016-10-25 09:36:53 -0600114 }
Tobin Ehlis154c2692016-10-25 09:36:53 -0600115 }
116 return skip;
117}
118
John Zulaufb6d71202017-12-22 16:47:09 -0700119// Return valid index or "end" i.e. binding_count_;
120// The asserts in "Get" are reduced to the set where no valid answer(like null or 0) could be given
121// Common code for all binding lookups.
122uint32_t cvdescriptorset::DescriptorSetLayout::GetIndexFromBinding(uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600123 const auto &bi_itr = binding_to_index_map_.find(binding);
John Zulaufb6d71202017-12-22 16:47:09 -0700124 if (bi_itr != binding_to_index_map_.cend()) return bi_itr->second;
125 return GetBindingCount();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600126}
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700127VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(
128 const uint32_t index) const {
129 if (index >= bindings_.size()) return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600130 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600131}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600132// Return descriptorCount for given index, 0 if index is unavailable
133uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700134 if (index >= bindings_.size()) return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600135 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600136}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600137// For the given index, return descriptorType
138VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
139 assert(index < bindings_.size());
John Zulaufb6d71202017-12-22 16:47:09 -0700140 if (index < bindings_.size()) return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600141 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
142}
John Zulaufb6d71202017-12-22 16:47:09 -0700143// For the given index, return stageFlags
144VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromIndex(const uint32_t index) const {
145 assert(index < bindings_.size());
146 if (index < bindings_.size()) return bindings_[index].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600147 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600148}
John Zulaufc483f442017-12-15 14:02:06 -0700149
John Zulaufb6d71202017-12-22 16:47:09 -0700150// For the given global index, return index
151uint32_t cvdescriptorset::DescriptorSetLayout::GetIndexFromGlobalIndex(const uint32_t global_index) const {
152 auto start_it = global_start_to_index_map_.upper_bound(global_index);
153 uint32_t index = binding_count_;
154 assert(start_it != global_start_to_index_map_.cbegin());
155 if (start_it != global_start_to_index_map_.cbegin()) {
156 --start_it;
157 index = start_it->second;
158#ifndef NDEBUG
159 const auto &range = GetGlobalIndexRangeFromBinding(bindings_[index].binding);
160 assert(range.start <= global_index && global_index < range.end);
161#endif
162 }
163 return index;
164}
165
166// For the given binding, return the global index range
John Zulaufc483f442017-12-15 14:02:06 -0700167// As start and end are often needed in pairs, get both with a single hash lookup.
168const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayout::GetGlobalIndexRangeFromBinding(
169 const uint32_t binding) const {
170 assert(binding_to_global_index_range_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600171 // In error case max uint32_t so index is out of bounds to break ASAP
John Zulaufc483f442017-12-15 14:02:06 -0700172 const static IndexRange kInvalidRange = {0xFFFFFFFF, 0xFFFFFFFF};
173 const auto &range_it = binding_to_global_index_range_map_.find(binding);
174 if (range_it != binding_to_global_index_range_map_.end()) {
175 return range_it->second;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600176 }
John Zulaufc483f442017-12-15 14:02:06 -0700177 return kInvalidRange;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600178}
John Zulaufc483f442017-12-15 14:02:06 -0700179
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600180// For given binding, return ptr to ImmutableSampler array
181VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600182 const auto &bi_itr = binding_to_index_map_.find(binding);
183 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600184 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600185 }
186 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600187}
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -0700188// Move to next valid binding having a non-zero binding count
189uint32_t cvdescriptorset::DescriptorSetLayout::GetNextValidBinding(const uint32_t binding) const {
John Zulaufb6d71202017-12-22 16:47:09 -0700190 auto it = non_empty_bindings_.upper_bound(binding);
191 assert(it != non_empty_bindings_.cend());
192 if (it != non_empty_bindings_.cend()) return *it;
193 return GetMaxBinding() + 1;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -0700194}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600195// For given index, return ptr to ImmutableSampler array
196VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
John Zulaufb6d71202017-12-22 16:47:09 -0700197 if (index < bindings_.size()) {
198 return bindings_[index].pImmutableSamplers;
199 }
200 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600201}
202// If our layout is compatible with rh_ds_layout, return true,
203// else return false and fill in error_msg will description of what causes incompatibility
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600204bool cvdescriptorset::DescriptorSetLayout::IsCompatible(DescriptorSetLayout const *const rh_ds_layout,
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600205 std::string *error_msg) const {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600206 // Trivial case
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700207 if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) return true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600208 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
209 std::stringstream error_str;
210 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
Tobin Ehlisb1861d92017-07-06 16:50:10 -0600211 << rh_ds_layout->GetDescriptorSetLayout() << ", which comes from pipelineLayout, has "
212 << rh_ds_layout->descriptor_count_ << " descriptors.";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600213 *error_msg = error_str.str();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700214 return false; // trivial fail case
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600215 }
216 // Descriptor counts match so need to go through bindings one-by-one
217 // and verify that type and stageFlags match
218 for (auto binding : bindings_) {
219 // TODO : Do we also need to check immutable samplers?
220 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600221 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600222 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600223 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
224 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlisb1861d92017-07-06 16:50:10 -0600225 << rh_ds_layout->GetDescriptorSetLayout() << ", which comes from pipelineLayout, has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600226 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600227 *error_msg = error_str.str();
228 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600229 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600230 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600231 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
232 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlisb1861d92017-07-06 16:50:10 -0600233 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout()
234 << ", which comes from pipelineLayout, is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600235 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600236 *error_msg = error_str.str();
237 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600238 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600239 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600240 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
241 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlisb1861d92017-07-06 16:50:10 -0600242 << rh_ds_layout->GetDescriptorSetLayout() << ", which comes from pipelineLayout, has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600243 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600244 *error_msg = error_str.str();
245 return false;
246 }
247 }
248 return true;
249}
250
251bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700252 if (!binding_to_index_map_.count(binding + 1)) return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600253 auto const &bi_itr = binding_to_index_map_.find(binding);
254 if (bi_itr != binding_to_index_map_.end()) {
255 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
256 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600257 auto type = bindings_[bi_itr->second].descriptorType;
258 auto stage_flags = bindings_[bi_itr->second].stageFlags;
259 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
260 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
261 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
262 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600263 return false;
264 }
265 return true;
266 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600267 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600268 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600269}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600270// Starting at offset descriptor of given binding, parse over update_count
271// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
272// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
273// If so, return true. If not, fill in error_msg and return false
274bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
275 const char *type, const VkDescriptorSet set,
276 std::string *error_msg) const {
277 // Verify consecutive bindings match (if needed)
278 auto orig_binding = current_binding;
279 // Track count of descriptors in the current_bindings that are remaining to be updated
280 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
281 // First, it's legal to offset beyond your own binding so handle that case
282 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
283 while (offset >= binding_remaining) {
284 // Advance to next binding, decrement offset by binding size
285 offset -= binding_remaining;
286 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
287 }
288 binding_remaining -= offset;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700289 while (update_count > binding_remaining) { // While our updates overstep current binding
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600290 // Verify next consecutive binding matches type, stage flags & immutable sampler use
291 if (!IsNextBindingConsistent(current_binding++)) {
292 std::stringstream error_str;
293 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
294 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
295 "next binding is not consistent with current binding so this update is invalid.";
296 *error_msg = error_str.str();
297 return false;
298 }
299 // For sake of this check consider the bindings updated and grab count for next binding
300 update_count -= binding_remaining;
301 binding_remaining = GetDescriptorCountFromBinding(current_binding);
302 }
303 return true;
304}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600305
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600306cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
307 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
308
Tobin Ehlis93f22372016-10-12 14:34:12 -0600309cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
John Zulauf48a6a702017-12-22 17:14:54 -0700310 const std::shared_ptr<DescriptorSetLayout const> &layout, layer_data *dev_data)
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700311 : some_update_(false),
312 set_(set),
313 pool_state_(nullptr),
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600314 p_layout_(layout),
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -0700315 device_data_(dev_data),
Józef Kucia1bb00972017-09-10 11:24:08 +0200316 limits_(GetPhysDevProperties(dev_data)->properties.limits) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700317 pool_state_ = GetDescriptorPoolState(dev_data, pool);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600318 // Foreach binding, create default descriptors of given type
John Zulaufb6d71202017-12-22 16:47:09 -0700319 descriptors_.reserve(p_layout_->GetTotalDescriptorCount());
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600320 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
321 auto type = p_layout_->GetTypeFromIndex(i);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600322 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700323 case VK_DESCRIPTOR_TYPE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600324 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
325 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600326 if (immut_sampler) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700327 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600328 some_update_ = true; // Immutable samplers are updated at creation
329 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700330 descriptors_.emplace_back(new SamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700331 }
332 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600333 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700334 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600335 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
336 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
Tobin Ehlis082c7512017-05-08 11:24:57 -0600337 if (immut) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700338 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis082c7512017-05-08 11:24:57 -0600339 some_update_ = true; // Immutable samplers are updated at creation
340 } else
Chris Forbes9f340852017-05-09 08:51:38 -0700341 descriptors_.emplace_back(new ImageSamplerDescriptor(nullptr));
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700342 }
343 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600344 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700345 // ImageDescriptors
346 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
347 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
348 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600349 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700350 descriptors_.emplace_back(new ImageDescriptor(type));
351 break;
352 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
353 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600354 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700355 descriptors_.emplace_back(new TexelDescriptor(type));
356 break;
357 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
358 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
359 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
360 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600361 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700362 descriptors_.emplace_back(new BufferDescriptor(type));
363 break;
364 default:
365 assert(0); // Bad descriptor type specified
366 break;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600367 }
368 }
369}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600370
Mark Lobodzinski729a8d32017-01-26 12:16:30 -0700371cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
Chris Forbes57989132016-07-26 17:06:10 +1200372
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700373static std::string string_descriptor_req_view_type(descriptor_req req) {
374 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200375 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
376 if (req & (1 << i)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700377 if (result.size()) result += ", ";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700378 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200379 }
380 }
381
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700382 if (!result.size()) result = "(none)";
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700383
384 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200385}
386
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600387// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
Tobin Ehlis6dc57dd2017-06-21 10:08:52 -0600388bool cvdescriptorset::DescriptorSet::IsCompatible(DescriptorSetLayout const *const layout, std::string *error) const {
389 return layout->IsCompatible(p_layout_.get(), error);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600390}
Chris Forbes57989132016-07-26 17:06:10 +1200391
Tobin Ehlis3066db62016-08-22 08:12:23 -0600392// 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 -0600393// This includes validating that all descriptors in the given bindings are updated,
394// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
395// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600396bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
John Zulauf48a6a702017-12-22 17:14:54 -0700397 const std::vector<uint32_t> &dynamic_offsets, GLOBAL_CB_NODE *cb_node,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600398 const char *caller, std::string *error) const {
Chris Forbesc7090a82016-07-25 18:10:41 +1200399 for (auto binding_pair : bindings) {
400 auto binding = binding_pair.first;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600401 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600402 std::stringstream error_str;
403 error_str << "Attempting to validate DrawState for binding #" << binding
404 << " which is an invalid binding for this descriptor set.";
405 *error = error_str.str();
406 return false;
407 }
John Zulaufc483f442017-12-15 14:02:06 -0700408 IndexRange index_range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700409 auto array_idx = 0; // Track array idx if we're dealing with array descriptors
John Zulaufc483f442017-12-15 14:02:06 -0700410 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700411 if (!descriptors_[i]->updated) {
412 std::stringstream error_str;
413 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
414 << " is being used in draw but has not been updated.";
415 *error = error_str.str();
416 return false;
417 } else {
418 auto descriptor_class = descriptors_[i]->GetClass();
419 if (descriptor_class == GeneralBuffer) {
420 // Verify that buffers are valid
421 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
422 auto buffer_node = GetBufferState(device_data_, buffer);
423 if (!buffer_node) {
424 std::stringstream error_str;
425 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
426 << " references invalid buffer " << buffer << ".";
427 *error = error_str.str();
428 return false;
John Zulauf48a6a702017-12-22 17:14:54 -0700429 } else if (!buffer_node->sparse) {
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700430 for (auto mem_binding : buffer_node->GetBoundMemory()) {
431 if (!GetMemObjInfo(device_data_, mem_binding)) {
432 std::stringstream error_str;
433 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
434 << " uses buffer " << buffer << " that references invalid memory " << mem_binding << ".";
435 *error = error_str.str();
Tobin Ehlisc8266452017-04-07 12:20:30 -0600436 return false;
437 }
438 }
John Zulauf48a6a702017-12-22 17:14:54 -0700439 } else {
440 // Enqueue sparse resource validation, as these can only be validated at submit time
441 auto device_data_copy = device_data_; // Cannot capture members by value, so make capturable copy.
442 std::function<bool(void)> function = [device_data_copy, caller, buffer_node]() {
443 return core_validation::ValidateBufferMemoryIsValid(device_data_copy, buffer_node, caller);
444 };
445 cb_node->queue_submit_functions.push_back(function);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700446 }
447 if (descriptors_[i]->IsDynamic()) {
448 // Validate that dynamic offsets are within the buffer
449 auto buffer_size = buffer_node->createInfo.size;
450 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
451 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
452 auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
453 if (VK_WHOLE_SIZE == range) {
454 if ((dyn_offset + desc_offset) > buffer_size) {
455 std::stringstream error_str;
456 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
457 << " uses buffer " << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset "
458 << dyn_offset << " combined with offset " << desc_offset
459 << " that oversteps the buffer size of " << buffer_size << ".";
460 *error = error_str.str();
461 return false;
462 }
463 } else {
464 if ((dyn_offset + desc_offset + range) > buffer_size) {
465 std::stringstream error_str;
466 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
467 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
468 << " combined with offset " << desc_offset << " and range " << range
469 << " that oversteps the buffer size of " << buffer_size << ".";
470 *error = error_str.str();
471 return false;
472 }
473 }
474 }
475 } else if (descriptor_class == ImageSampler || descriptor_class == Image) {
476 VkImageView image_view;
477 VkImageLayout image_layout;
478 if (descriptor_class == ImageSampler) {
479 image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
480 image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
481 } else {
482 image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
483 image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
484 }
485 auto reqs = binding_pair.second;
486
487 auto image_view_state = GetImageViewState(device_data_, image_view);
Tobin Ehlis836a1372017-07-14 11:25:21 -0600488 if (nullptr == image_view_state) {
489 // Image view must have been destroyed since initial update. Could potentially flag the descriptor
490 // as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
491 std::stringstream error_str;
492 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
493 << " is using imageView " << image_view << " that has been destroyed.";
494 *error = error_str.str();
495 return false;
496 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700497 auto image_view_ci = image_view_state->create_info;
498
499 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
500 // bad view type
501 std::stringstream error_str;
502 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
503 << " requires an image view of type " << string_descriptor_req_view_type(reqs) << " but got "
504 << string_VkImageViewType(image_view_ci.viewType) << ".";
505 *error = error_str.str();
506 return false;
507 }
508
509 auto image_node = GetImageState(device_data_, image_view_ci.image);
510 assert(image_node);
511 // Verify Image Layout
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700512 // Copy first mip level into sub_layers and loop over each mip level to verify layout
513 VkImageSubresourceLayers sub_layers;
514 sub_layers.aspectMask = image_view_ci.subresourceRange.aspectMask;
515 sub_layers.baseArrayLayer = image_view_ci.subresourceRange.baseArrayLayer;
516 sub_layers.layerCount = image_view_ci.subresourceRange.layerCount;
517 bool hit_error = false;
518 for (auto cur_level = image_view_ci.subresourceRange.baseMipLevel;
519 cur_level < image_view_ci.subresourceRange.levelCount; ++cur_level) {
520 sub_layers.mipLevel = cur_level;
521 VerifyImageLayout(device_data_, cb_node, image_node, sub_layers, image_layout, VK_IMAGE_LAYOUT_UNDEFINED,
Chris Forbesc6bb6ce2017-07-21 14:03:06 -0700522 caller, VALIDATION_ERROR_046002b0, &hit_error);
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700523 if (hit_error) {
524 *error =
525 "Image layout specified at vkUpdateDescriptorSets() time doesn't match actual image layout at "
526 "time descriptor is used. See previous error callback for specific details.";
Chris Forbes57989132016-07-26 17:06:10 +1200527 return false;
528 }
Chris Forbes1f7f3ca2017-05-08 13:54:50 -0700529 }
530 // Verify Sample counts
531 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
532 std::stringstream error_str;
533 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
534 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
535 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
536 *error = error_str.str();
537 return false;
538 }
539 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
540 std::stringstream error_str;
541 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
542 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
543 *error = error_str.str();
544 return false;
Chris Forbes57989132016-07-26 17:06:10 +1200545 }
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
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600561 if (!p_layout_->HasBinding(binding)) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600562 continue;
563 }
John Zulaufc483f442017-12-15 14:02:06 -0700564 uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600565 if (descriptors_[start_idx]->IsStorage()) {
566 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis7cd8c792017-06-20 08:30:39 -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 Ehlis7cd8c792017-06-20 08:30:39 -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 Ehlis7cd8c792017-06-20 08:30:39 -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() {
Petr Krausbc7f5442017-05-14 23:43:38 +0200598 core_validation::invalidateCommandBuffers(device_data_, cb_bindings, {HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600599}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600600// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600601void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700602 // Perform update on a per-binding basis as consecutive updates roll over to next binding
603 auto descriptors_remaining = update->descriptorCount;
604 auto binding_being_updated = update->dstBinding;
605 auto offset = update->dstArrayElement;
Tobin Ehlise16805c2017-08-09 09:10:37 -0600606 uint32_t update_index = 0;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700607 while (descriptors_remaining) {
608 uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
John Zulaufc483f442017-12-15 14:02:06 -0700609 auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700610 // Loop over the updates for a single binding at a time
Tobin Ehlise16805c2017-08-09 09:10:37 -0600611 for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
612 descriptors_[global_idx + di]->WriteUpdate(update, update_index);
Tobin Ehlisf922ef82016-11-30 10:19:14 -0700613 }
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 Ehlis3c37fb32017-05-24 09:31:13 -0600630 *error_code = VALIDATION_ERROR_2860026a;
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 Ehlis7cd8c792017-06-20 08:30:39 -0600637 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600638 *error_code = VALIDATION_ERROR_032002b6;
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 Ehlis3c37fb32017-05-24 09:31:13 -0600645 *error_code = VALIDATION_ERROR_032002b2;
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
John Zulaufc483f442017-12-15 14:02:06 -0700653 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600654 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
655 // SRC update out of bounds
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600656 *error_code = VALIDATION_ERROR_032002b4;
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
John Zulaufc483f442017-12-15 14:02:06 -0700659 << " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600660 << " 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 }
John Zulaufc483f442017-12-15 14:02:06 -0700665 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600666 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600667 // DST update out of bounds
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600668 *error_code = VALIDATION_ERROR_032002b8;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600669 std::stringstream error_str;
670 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
John Zulaufc483f442017-12-15 14:02:06 -0700671 << " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600672 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600673 << " 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 Ehlis3c37fb32017-05-24 09:31:13 -0600678 // TODO : Base default error case going from here is VALIDATION_ERROR_0002b8012ba which covers all consistency issues, need more
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600679 // fine-grained error codes
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600680 *error_code = VALIDATION_ERROR_032002ba;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600681 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
Tobin Ehlis7cd8c792017-06-20 08:30:39 -0600682 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600683 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 Ehlis7cd8c792017-06-20 08:30:39 -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 // Update parameters all look good and descriptor updated so verify update contents
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700699 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error_code, error_msg)) return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600700
701 // All checks passed so update is good
702 return true;
703}
704// Perform Copy update
705void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
John Zulaufc483f442017-12-15 14:02:06 -0700706 auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
707 auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600708 // Update parameters all look good so perform update
709 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +0200710 auto src = src_set->descriptors_[src_start_idx + di].get();
711 auto dst = descriptors_[dst_start_idx + di].get();
712 if (src->updated) {
713 dst->CopyUpdate(src);
714 some_update_ = true;
715 } else {
716 dst->updated = false;
717 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600718 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600719
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600720 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600721}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600722
Tobin Ehlisf9519102016-08-17 09:49:13 -0600723// Bind cb_node to this set and this set to cb_node.
724// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
725// to be used in a draw by the given cb_node
Tobin Ehlis276d3d32016-12-21 09:21:06 -0700726void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node,
Tobin Ehlis022528b2016-12-29 12:22:32 -0700727 const std::map<uint32_t, descriptor_req> &binding_req_map) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600728 // bind cb to this descriptor set
729 cb_bindings.insert(cb_node);
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600730 // Add bindings for descriptor set, the set's pool, and individual objects in the set
Petr Krausbc7f5442017-05-14 23:43:38 +0200731 cb_node->object_bindings.insert({HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
Tobin Ehlis7ca20be2016-10-12 15:09:16 -0600732 pool_state_->cb_bindings.insert(cb_node);
Petr Krausbc7f5442017-05-14 23:43:38 +0200733 cb_node->object_bindings.insert({HandleToUint64(pool_state_->pool), kVulkanObjectTypeDescriptorPool});
Tobin Ehlisf9519102016-08-17 09:49:13 -0600734 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
735 // resources
Tobin Ehlis022528b2016-12-29 12:22:32 -0700736 for (auto binding_req_pair : binding_req_map) {
737 auto binding = binding_req_pair.first;
John Zulaufc483f442017-12-15 14:02:06 -0700738 auto range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
739 for (uint32_t i = range.start; i < range.end; ++i) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600740 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
741 }
742 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600743}
John Zulauf48a6a702017-12-22 17:14:54 -0700744void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
745 const BindingReqMap &in_req, BindingReqMap *out_req,
746 TrackedBindings *bindings) {
747 assert(out_req);
748 assert(bindings);
749 const auto binding = binding_req_pair.first;
750 // Use insert and look at the boolean ("was inserted") in the returned pair to see if this is a new set member.
751 // Saves one hash lookup vs. find ... compare w/ end ... insert.
752 const auto it_bool_pair = bindings->insert(binding);
753 if (it_bool_pair.second) {
754 out_req->emplace(binding_req_pair);
755 }
756}
757void cvdescriptorset::DescriptorSet::FilterAndTrackOneBindingReq(const BindingReqMap::value_type &binding_req_pair,
758 const BindingReqMap &in_req, BindingReqMap *out_req,
759 TrackedBindings *bindings, uint32_t limit) {
760 if (bindings->size() < limit) FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, bindings);
761}
762
763void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, const BindingReqMap &in_req,
764 BindingReqMap *out_req) {
765 TrackedBindings &bound = cached_validation_[cb_state].command_binding_and_usage;
766 if (bound.size() == GetBindingCount()) {
767 return; // All bindings are bound, out req is empty
768 }
769 for (const auto &binding_req_pair : in_req) {
770 const auto binding = binding_req_pair.first;
771 // If a binding doesn't exist, or has already been bound, skip it
772 if (p_layout_->HasBinding(binding)) {
773 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, &bound);
774 }
775 }
776}
777
778void cvdescriptorset::DescriptorSet::FilterAndTrackBindingReqs(GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline,
779 const BindingReqMap &in_req, BindingReqMap *out_req) {
780 auto &validated = cached_validation_[cb_state];
781 auto &image_sample_val = validated.image_samplers[pipeline];
782 auto *const dynamic_buffers = &validated.dynamic_buffers;
783 auto *const non_dynamic_buffers = &validated.non_dynamic_buffers;
784 const auto &stats = p_layout_->GetBindingTypeStats();
785 for (const auto &binding_req_pair : in_req) {
786 auto binding = binding_req_pair.first;
787 VkDescriptorSetLayoutBinding const *layout_binding = p_layout_->GetDescriptorSetLayoutBindingPtrFromBinding(binding);
788 if (!layout_binding) {
789 continue;
790 }
791 // Caching criteria differs per type.
792 // If image_layout have changed , the image descriptors need to be validated against them.
793 if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
794 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
795 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, dynamic_buffers, stats.dynamic_buffer_count);
796 } else if ((layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
797 (layout_binding->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
798 FilterAndTrackOneBindingReq(binding_req_pair, in_req, out_req, non_dynamic_buffers, stats.non_dynamic_buffer_count);
799 } else {
800 // This is rather crude, as the changed layouts may not impact the bound descriptors,
801 // but the simple "versioning" is a simple "dirt" test.
802 auto &version = image_sample_val[binding]; // Take advantage of default construtor zero initialzing new entries
803 if (version != cb_state->image_layout_change_count) {
804 version = cb_state->image_layout_change_count;
805 out_req->emplace(binding_req_pair);
806 }
807 }
808 }
809}
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600810
Tobin Ehlis300888c2016-05-18 13:43:26 -0600811cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600812 updated = false;
813 descriptor_class = PlainSampler;
814 if (immut) {
815 sampler_ = *immut;
816 immutable_ = true;
817 updated = true;
818 }
819}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600820// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700821bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const layer_data *dev_data) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700822 return (GetSamplerState(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600823}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600824
Tobin Ehlis554bf382016-05-24 11:14:43 -0600825bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700826 const layer_data *dev_data, UNIQUE_VALIDATION_ERROR_CODE *error_code,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600827 std::string *error_msg) {
828 // TODO : Defaulting to 00943 for all cases here. Need to create new error codes for various cases.
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600829 *error_code = VALIDATION_ERROR_15c0028c;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700830 auto iv_state = GetImageViewState(dev_data, image_view);
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600831 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600832 std::stringstream error_str;
833 error_str << "Invalid VkImageView: " << image_view;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600834 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600835 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600836 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600837 // 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 -0600838 // Validate that imageLayout is compatible with aspect_mask and image format
839 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600840 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
841 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600842 VkFormat format = VK_FORMAT_MAX_ENUM;
843 VkImageUsageFlags usage = 0;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700844 auto image_node = GetImageState(dev_data, image);
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600845 if (image_node) {
846 format = image_node->createInfo.format;
847 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600848 // Validate that memory is bound to image
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -0700849 // TODO: This should have its own valid usage id apart from 2524 which is from CreateImageView case. The only
850 // the error here occurs is if memory bound to a created imageView has been freed.
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600851 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_0ac007f8)) {
852 *error_code = VALIDATION_ERROR_0ac007f8;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600853 *error_msg = "No memory bound to image.";
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600854 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -0600855 }
Chris Forbes67757ff2017-07-21 13:59:01 -0700856
857 // KHR_maintenance1 allows rendering into 2D or 2DArray views which slice a 3D image,
858 // but not binding them to descriptor sets.
859 if (image_node->createInfo.imageType == VK_IMAGE_TYPE_3D &&
860 (iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D ||
861 iv_state->create_info.viewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY)) {
862 *error_code = VALIDATION_ERROR_046002ae;
863 *error_msg = "ImageView must not be a 2D or 2DArray view of a 3D image";
864 return false;
865 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600866 }
867 // First validate that format and layout are compatible
868 if (format == VK_FORMAT_MAX_ENUM) {
869 std::stringstream error_str;
870 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600871 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600872 return false;
873 }
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600874 // TODO : The various image aspect and format checks here are based on general spec language in 11.5 Image Views section under
875 // vkCreateImageView(). What's the best way to create unique id for these cases?
Dave Houlton1d2022c2017-03-29 11:43:58 -0600876 bool ds = FormatIsDepthOrStencil(format);
Tobin Ehlis1809f912016-05-25 09:24:36 -0600877 switch (image_layout) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700878 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
879 // Only Color bit must be set
880 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600881 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700882 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
883 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600884 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -0600885 return false;
886 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700887 // format must NOT be DS
888 if (ds) {
889 std::stringstream error_str;
890 error_str << "ImageView (" << image_view
891 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
892 << string_VkFormat(format) << " which is not a color format.";
893 *error_msg = error_str.str();
894 return false;
895 }
896 break;
897 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
898 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
899 // Depth or stencil bit must be set, but both must NOT be set
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600900 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
901 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
902 // both must NOT be set
903 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700904 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -0600905 *error_msg = error_str.str();
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600906 return false;
907 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700908 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
909 // Neither were set
910 std::stringstream error_str;
911 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
912 << " but does not have STENCIL or DEPTH aspects set";
913 *error_msg = error_str.str();
914 return false;
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600915 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700916 // format must be DS
917 if (!ds) {
918 std::stringstream error_str;
919 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
920 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
921 *error_msg = error_str.str();
922 return false;
923 }
924 break;
925 default:
926 // For other layouts if the source is depth/stencil image, both aspect bits must not be set
927 if (ds) {
928 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
929 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
930 // both must NOT be set
931 std::stringstream error_str;
932 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
933 << " and is using depth/stencil image of format " << string_VkFormat(format)
934 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
935 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
936 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
937 "reads respectively.";
938 *error_msg = error_str.str();
939 return false;
940 }
941 }
942 }
943 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600944 }
945 // Now validate that usage flags are correctly set for given type of update
Tobin Ehlisfb4cf712016-10-10 14:02:48 -0600946 // 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 -0600947 // TODO : The various image usage bit requirements are in general spec language for VkImageUsageFlags bit block in 11.3 Images
948 // under vkCreateImage()
Tobin Ehlis3c37fb32017-05-24 09:31:13 -0600949 // TODO : Need to also validate case VALIDATION_ERROR_15c002a0 where STORAGE_IMAGE & INPUT_ATTACH types must have been created
950 // with identify swizzle
Tobin Ehlis1809f912016-05-25 09:24:36 -0600951 std::string error_usage_bit;
952 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700953 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
954 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
955 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
956 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
957 }
958 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600959 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700960 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
961 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
962 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
963 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
964 std::stringstream error_str;
Tobin Ehlisbb03e5f2017-05-11 08:52:51 -0600965 // TODO : Need to create custom enum error codes for these cases
966 if (image_node->shared_presentable) {
967 if (VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR != image_layout) {
968 error_str
969 << "ImageView (" << image_view
970 << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type with a front-buffered image is being updated with "
971 "layout "
972 << string_VkImageLayout(image_layout)
973 << " but according to spec section 13.1 Descriptor Types, 'Front-buffered images that report support "
974 "for "
975 "VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT must be in the VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR layout.'";
976 *error_msg = error_str.str();
977 return false;
978 }
979 } else if (VK_IMAGE_LAYOUT_GENERAL != image_layout) {
980 error_str
981 << "ImageView (" << image_view << ") of VK_DESCRIPTOR_TYPE_STORAGE_IMAGE type is being updated with layout "
982 << string_VkImageLayout(image_layout)
983 << " but according to spec section 13.1 Descriptor Types, 'Load and store operations on storage images can "
984 "only be done on images in VK_IMAGE_LAYOUT_GENERAL layout.'";
985 *error_msg = error_str.str();
986 return false;
987 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700988 }
989 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600990 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700991 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
992 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
993 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
994 }
995 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600996 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700997 default:
998 break;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600999 }
1000 if (!error_usage_bit.empty()) {
1001 std::stringstream error_str;
1002 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
1003 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1004 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001005 *error_msg = error_str.str();
Tobin Ehlis1809f912016-05-25 09:24:36 -06001006 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001007 }
1008 return true;
1009}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001010
Tobin Ehlis300888c2016-05-18 13:43:26 -06001011void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
1012 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001013 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001014}
1015
Tobin Ehlis300888c2016-05-18 13:43:26 -06001016void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001017 if (!immutable_) {
1018 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001019 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001020 }
1021 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001022}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001023
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001024void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001025 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001026 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001027 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001028 }
1029}
1030
Tobin Ehlis300888c2016-05-18 13:43:26 -06001031cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
Chris Forbes9f340852017-05-09 08:51:38 -07001032 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001033 updated = false;
1034 descriptor_class = ImageSampler;
1035 if (immut) {
1036 sampler_ = *immut;
1037 immutable_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001038 }
1039}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001040
Tobin Ehlis300888c2016-05-18 13:43:26 -06001041void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001042 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001043 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001044 sampler_ = image_info.sampler;
1045 image_view_ = image_info.imageView;
1046 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001047}
1048
Tobin Ehlis300888c2016-05-18 13:43:26 -06001049void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001050 if (!immutable_) {
1051 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001052 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001053 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001054 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
1055 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001056 updated = true;
1057 image_view_ = image_view;
1058 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001059}
1060
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001061void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001062 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001063 if (!immutable_) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001064 auto sampler_state = GetSamplerState(dev_data, sampler_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001065 if (sampler_state) core_validation::AddCommandBufferBindingSampler(cb_node, sampler_state);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001066 }
Tobin Ehlis81e46372016-08-17 13:33:44 -06001067 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001068 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001069 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001070 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001071 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001072}
1073
Tobin Ehlis300888c2016-05-18 13:43:26 -06001074cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
1075 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001076 updated = false;
1077 descriptor_class = Image;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001078 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001079}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001080
Tobin Ehlis300888c2016-05-18 13:43:26 -06001081void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001082 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001083 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001084 image_view_ = image_info.imageView;
1085 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001086}
1087
Tobin Ehlis300888c2016-05-18 13:43:26 -06001088void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001089 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
1090 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001091 updated = true;
1092 image_view_ = image_view;
1093 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001094}
1095
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001096void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -06001097 // Add binding for image
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001098 auto iv_state = GetImageViewState(dev_data, image_view_);
Tobin Ehlis8b26a382016-09-14 08:02:49 -06001099 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -06001100 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001101 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001102}
1103
Tobin Ehlis300888c2016-05-18 13:43:26 -06001104cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
1105 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001106 updated = false;
1107 descriptor_class = GeneralBuffer;
1108 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1109 dynamic_ = true;
1110 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
1111 storage_ = true;
1112 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1113 dynamic_ = true;
1114 storage_ = true;
1115 }
1116}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001117void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001118 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -06001119 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -06001120 buffer_ = buffer_info.buffer;
1121 offset_ = buffer_info.offset;
1122 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001123}
1124
Tobin Ehlis300888c2016-05-18 13:43:26 -06001125void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
1126 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001127 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001128 buffer_ = buff_desc->buffer_;
1129 offset_ = buff_desc->offset_;
1130 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001131}
1132
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001133void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001134 auto buffer_node = GetBufferState(dev_data, buffer_);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001135 if (buffer_node) core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001136}
1137
Tobin Ehlis300888c2016-05-18 13:43:26 -06001138cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001139 updated = false;
1140 descriptor_class = TexelBuffer;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001141 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type) storage_ = true;
Petr Kraus13c98a62017-12-09 00:22:39 +01001142}
Tobin Ehlis56a30942016-05-19 08:00:00 -06001143
Tobin Ehlis300888c2016-05-18 13:43:26 -06001144void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001145 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001146 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001147}
1148
Tobin Ehlis300888c2016-05-18 13:43:26 -06001149void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
1150 updated = true;
1151 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
1152}
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001153
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001154void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001155 auto bv_state = GetBufferViewState(dev_data, buffer_view_);
Tobin Ehlis8b872462016-09-14 08:12:08 -06001156 if (bv_state) {
Tobin Ehlis2515c0e2016-09-28 07:12:28 -06001157 core_validation::AddCommandBufferBindingBufferView(dev_data, cb_node, bv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -06001158 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -06001159}
1160
Tobin Ehlis300888c2016-05-18 13:43:26 -06001161// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1162// sets, and then calls their respective Validate[Write|Copy]Update functions.
1163// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
1164// be skipped, then true is returned.
1165// If there is no issue with the update, then false is returned.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001166bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data, const layer_data *dev_data,
1167 uint32_t write_count, const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001168 const VkCopyDescriptorSet *p_cds) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001169 bool skip = false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001170 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001171 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001172 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001173 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001174 if (!set_node) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001175 skip |=
Tobin Ehlis300888c2016-05-18 13:43:26 -06001176 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Petr Krausbc7f5442017-05-14 23:43:38 +02001177 HandleToUint64(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001178 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
Petr Krausbc7f5442017-05-14 23:43:38 +02001179 HandleToUint64(dest_set));
Tobin Ehlis300888c2016-05-18 13:43:26 -06001180 } else {
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001181 UNIQUE_VALIDATION_ERROR_CODE error_code;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001182 std::string error_str;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001183 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001184 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Petr Krausbc7f5442017-05-14 23:43:38 +02001185 HandleToUint64(dest_set), __LINE__, error_code, "DS",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001186 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
1187 " with error: %s. %s",
Petr Krausbc7f5442017-05-14 23:43:38 +02001188 HandleToUint64(dest_set), error_str.c_str(), validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001189 }
1190 }
1191 }
1192 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001193 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001194 auto dst_set = p_cds[i].dstSet;
1195 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001196 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1197 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlisa1712752017-01-04 09:41:47 -07001198 // Object_tracker verifies that src & dest descriptor set are valid
1199 assert(src_node);
1200 assert(dst_node);
1201 UNIQUE_VALIDATION_ERROR_CODE error_code;
1202 std::string error_str;
1203 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_code, &error_str)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001204 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Petr Krausbc7f5442017-05-14 23:43:38 +02001205 HandleToUint64(dst_set), __LINE__, error_code, "DS",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001206 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
1207 " to Descriptor Set 0x%" PRIx64 " with error: %s. %s",
Petr Krausbc7f5442017-05-14 23:43:38 +02001208 HandleToUint64(src_set), HandleToUint64(dst_set), error_str.c_str(), validation_error_map[error_code]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001209 }
1210 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001211 return skip;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001212}
1213// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1214// sets, and then calls their respective Perform[Write|Copy]Update functions.
1215// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1216// with the same set of updates.
1217// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1218// calling down the chain.
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001219void cvdescriptorset::PerformUpdateDescriptorSets(const layer_data *dev_data, uint32_t write_count,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001220 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1221 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001222 // Write updates first
1223 uint32_t i = 0;
1224 for (i = 0; i < write_count; ++i) {
1225 auto dest_set = p_wds[i].dstSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001226 auto set_node = core_validation::GetSetNode(dev_data, dest_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001227 if (set_node) {
1228 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001229 }
1230 }
1231 // Now copy updates
1232 for (i = 0; i < copy_count; ++i) {
1233 auto dst_set = p_cds[i].dstSet;
1234 auto src_set = p_cds[i].srcSet;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001235 auto src_node = core_validation::GetSetNode(dev_data, src_set);
1236 auto dst_node = core_validation::GetSetNode(dev_data, dst_set);
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001237 if (src_node && dst_node) {
1238 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001239 }
1240 }
1241}
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001242// This helper function carries out the state updates for descriptor updates peformed via update templates. It basically collects
1243// data and leverages the PerformUpdateDescriptor helper functions to do this.
1244void cvdescriptorset::PerformUpdateDescriptorSetsWithTemplateKHR(layer_data *device_data, VkDescriptorSet descriptorSet,
1245 std::unique_ptr<TEMPLATE_STATE> const &template_state,
1246 const void *pData) {
1247 auto const &create_info = template_state->create_info;
1248
1249 // Create a vector of write structs
1250 std::vector<VkWriteDescriptorSet> desc_writes;
1251 auto layout_obj = GetDescriptorSetLayout(device_data, create_info.descriptorSetLayout);
1252
1253 // Create a WriteDescriptorSet struct for each template update entry
1254 for (uint32_t i = 0; i < create_info.descriptorUpdateEntryCount; i++) {
1255 auto binding_count = layout_obj->GetDescriptorCountFromBinding(create_info.pDescriptorUpdateEntries[i].dstBinding);
1256 auto binding_being_updated = create_info.pDescriptorUpdateEntries[i].dstBinding;
1257 auto dst_array_element = create_info.pDescriptorUpdateEntries[i].dstArrayElement;
1258
John Zulaufb6d71202017-12-22 16:47:09 -07001259 desc_writes.reserve(desc_writes.size() + create_info.pDescriptorUpdateEntries[i].descriptorCount);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001260 for (uint32_t j = 0; j < create_info.pDescriptorUpdateEntries[i].descriptorCount; j++) {
1261 desc_writes.emplace_back();
1262 auto &write_entry = desc_writes.back();
1263
1264 size_t offset = create_info.pDescriptorUpdateEntries[i].offset + j * create_info.pDescriptorUpdateEntries[i].stride;
1265 char *update_entry = (char *)(pData) + offset;
1266
1267 if (dst_array_element >= binding_count) {
1268 dst_array_element = 0;
Mark Lobodzinski4aa479d2017-03-10 09:14:00 -07001269 binding_being_updated = layout_obj->GetNextValidBinding(binding_being_updated);
Mark Lobodzinski3d63a042017-03-09 16:24:13 -07001270 }
1271
1272 write_entry.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1273 write_entry.pNext = NULL;
1274 write_entry.dstSet = descriptorSet;
1275 write_entry.dstBinding = binding_being_updated;
1276 write_entry.dstArrayElement = dst_array_element;
1277 write_entry.descriptorCount = 1;
1278 write_entry.descriptorType = create_info.pDescriptorUpdateEntries[i].descriptorType;
1279
1280 switch (create_info.pDescriptorUpdateEntries[i].descriptorType) {
1281 case VK_DESCRIPTOR_TYPE_SAMPLER:
1282 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1283 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1284 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1285 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1286 write_entry.pImageInfo = reinterpret_cast<VkDescriptorImageInfo *>(update_entry);
1287 break;
1288
1289 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1290 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1291 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1292 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1293 write_entry.pBufferInfo = reinterpret_cast<VkDescriptorBufferInfo *>(update_entry);
1294 break;
1295
1296 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1297 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1298 write_entry.pTexelBufferView = reinterpret_cast<VkBufferView *>(update_entry);
1299 break;
1300 default:
1301 assert(0);
1302 break;
1303 }
1304 dst_array_element++;
1305 }
1306 }
1307 PerformUpdateDescriptorSets(device_data, static_cast<uint32_t>(desc_writes.size()), desc_writes.data(), 0, NULL);
1308}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001309// Validate the state for a given write update but don't actually perform the update
1310// If an error would occur for this update, return false and fill in details in error_msg string
1311bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001312 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001313 // Verify idle ds
1314 if (in_use.load()) {
Tobin Ehlis2cb8eb22017-01-03 14:09:57 -07001315 // TODO : Re-using Free Idle error code, need write update idle error code
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001316 *error_code = VALIDATION_ERROR_2860026a;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001317 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001318 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001319 << " that is in use by a command buffer";
Tobin Ehlis300888c2016-05-18 13:43:26 -06001320 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001321 return false;
1322 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001323 // Verify dst binding exists
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001324 if (!p_layout_->HasBinding(update->dstBinding)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001325 *error_code = VALIDATION_ERROR_15c00276;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001326 std::stringstream error_str;
Tobin Ehlis1d81edd2016-11-21 09:50:49 -07001327 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001328 *error_msg = error_str.str();
1329 return false;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001330 } else {
1331 // Make sure binding isn't empty
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001332 if (0 == p_layout_->GetDescriptorCountFromBinding(update->dstBinding)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001333 *error_code = VALIDATION_ERROR_15c00278;
Tobin Ehlis59a5efc2016-11-21 09:41:57 -07001334 std::stringstream error_str;
1335 error_str << "DescriptorSet " << set_ << " cannot updated binding " << update->dstBinding << " that has 0 descriptors";
1336 *error_msg = error_str.str();
1337 return false;
1338 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001339 }
1340 // We know that binding is valid, verify update and do update on each descriptor
John Zulaufc483f442017-12-15 14:02:06 -07001341 auto start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001342 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001343 if (type != update->descriptorType) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001344 *error_code = VALIDATION_ERROR_15c0027e;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001345 std::stringstream error_str;
1346 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1347 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1348 *error_msg = error_str.str();
1349 return false;
1350 }
Tobin Ehlis7b402352016-12-15 07:51:20 -07001351 if (update->descriptorCount > (descriptors_.size() - start_idx)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001352 *error_code = VALIDATION_ERROR_15c00282;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001353 std::stringstream error_str;
1354 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
Tobin Ehlis7b402352016-12-15 07:51:20 -07001355 << descriptors_.size() - start_idx
Tobin Ehlisf922ef82016-11-30 10:19:14 -07001356 << " descriptors in that binding and all successive bindings of the set, but update of "
1357 << update->descriptorCount << " descriptors combined with update array element offset of "
1358 << update->dstArrayElement << " oversteps the available number of consecutive descriptors";
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001359 *error_msg = error_str.str();
1360 return false;
1361 }
1362 // Verify consecutive bindings match (if needed)
Tobin Ehlis7cd8c792017-06-20 08:30:39 -06001363 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001364 set_, error_msg)) {
Tobin Ehlis48fbd692017-01-04 09:17:01 -07001365 // TODO : Should break out "consecutive binding updates" language into valid usage statements
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001366 *error_code = VALIDATION_ERROR_15c00282;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001367 return false;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001368 }
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001369 // Update is within bounds and consistent so last step is to validate update contents
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001370 if (!VerifyWriteUpdateContents(update, start_idx, error_code, error_msg)) {
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001371 std::stringstream error_str;
1372 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1373 << " failed with error message: " << error_msg->c_str();
1374 *error_msg = error_str.str();
1375 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001376 }
1377 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001378 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001379}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001380// For the given buffer, verify that its creation parameters are appropriate for the given type
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001381// 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 -07001382bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_STATE const *buffer_node, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001383 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001384 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001385 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001386 std::string error_usage_bit;
1387 switch (type) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001388 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1389 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001390 *error_code = VALIDATION_ERROR_15c0029c;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001391 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1392 }
1393 break;
1394 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1395 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001396 *error_code = VALIDATION_ERROR_15c0029e;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001397 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1398 }
1399 break;
1400 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1401 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1402 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001403 *error_code = VALIDATION_ERROR_15c00292;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001404 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1405 }
1406 break;
1407 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1408 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1409 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001410 *error_code = VALIDATION_ERROR_15c00296;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001411 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1412 }
1413 break;
1414 default:
1415 break;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001416 }
1417 if (!error_usage_bit.empty()) {
1418 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001419 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1420 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1421 << error_usage_bit << " set.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001422 *error_msg = error_str.str();
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001423 return false;
1424 }
1425 return true;
1426}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001427// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1428// 1. buffer is valid
1429// 2. buffer was created with correct usage flags
1430// 3. offset is less than buffer size
1431// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001432// 5. range and offset are within the device's limits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001433// 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 -06001434bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001435 UNIQUE_VALIDATION_ERROR_CODE *error_code, std::string *error_msg) const {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001436 // First make sure that buffer is valid
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001437 auto buffer_node = GetBufferState(device_data_, buffer_info->buffer);
Tobin Ehlisfa8b6182016-12-22 13:40:45 -07001438 // Any invalid buffer should already be caught by object_tracker
1439 assert(buffer_node);
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001440 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()", VALIDATION_ERROR_15c00294)) {
1441 *error_code = VALIDATION_ERROR_15c00294;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001442 *error_msg = "No memory bound to buffer.";
Tobin Ehlis81280962016-07-20 14:04:20 -06001443 return false;
Tobin Ehlisfed999f2016-09-21 15:09:45 -06001444 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001445 // Verify usage bits
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001446 if (!ValidateBufferUsage(buffer_node, type, error_code, error_msg)) {
1447 // error_msg will have been updated by ValidateBufferUsage()
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001448 return false;
1449 }
1450 // offset must be less than buffer size
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001451 if (buffer_info->offset >= buffer_node->createInfo.size) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001452 *error_code = VALIDATION_ERROR_044002a8;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001453 std::stringstream error_str;
Jeremy Hayesd1a6a822017-03-09 14:39:45 -07001454 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than or equal to buffer "
1455 << buffer_node->buffer << " size of " << buffer_node->createInfo.size;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001456 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001457 return false;
1458 }
1459 if (buffer_info->range != VK_WHOLE_SIZE) {
1460 // Range must be VK_WHOLE_SIZE or > 0
1461 if (!buffer_info->range) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001462 *error_code = VALIDATION_ERROR_044002aa;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001463 std::stringstream error_str;
1464 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001465 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001466 return false;
1467 }
1468 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1469 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001470 *error_code = VALIDATION_ERROR_044002ac;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001471 std::stringstream error_str;
1472 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1473 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001474 *error_msg = error_str.str();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001475 return false;
1476 }
1477 }
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001478 // Check buffer update sizes against device limits
1479 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER == type || VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
1480 auto max_ub_range = limits_.maxUniformBufferRange;
1481 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1482 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_ub_range) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001483 *error_code = VALIDATION_ERROR_15c00298;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001484 std::stringstream error_str;
1485 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1486 << " which is greater than this device's maxUniformBufferRange (" << max_ub_range << ")";
1487 *error_msg = error_str.str();
1488 return false;
1489 }
1490 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type || VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
1491 auto max_sb_range = limits_.maxStorageBufferRange;
1492 // TODO : If range is WHOLE_SIZE, need to make sure underlying buffer size doesn't exceed device max
1493 if (buffer_info->range != VK_WHOLE_SIZE && buffer_info->range > max_sb_range) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001494 *error_code = VALIDATION_ERROR_15c0029a;
Tobin Ehlisc3b6c4c2017-02-02 17:26:40 -07001495 std::stringstream error_str;
1496 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range
1497 << " which is greater than this device's maxStorageBufferRange (" << max_sb_range << ")";
1498 *error_msg = error_str.str();
1499 return false;
1500 }
1501 }
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001502 return true;
1503}
1504
Tobin Ehlis300888c2016-05-18 13:43:26 -06001505// Verify that the contents of the update are ok, but don't perform actual update
1506bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001507 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1508 std::string *error_msg) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001509 switch (update->descriptorType) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001510 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1511 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1512 // Validate image
1513 auto image_view = update->pImageInfo[di].imageView;
1514 auto image_layout = update->pImageInfo[di].imageLayout;
1515 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001516 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001517 error_str << "Attempted write update to combined image sampler descriptor failed due to: "
1518 << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001519 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001520 return false;
1521 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001522 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001523 // Intentional fall-through to validate sampler
Tobin Ehlis300888c2016-05-18 13:43:26 -06001524 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001525 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1526 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1527 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
1528 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001529 *error_code = VALIDATION_ERROR_15c0028a;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001530 std::stringstream error_str;
1531 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1532 << update->pImageInfo[di].sampler << ".";
1533 *error_msg = error_str.str();
1534 return false;
1535 }
1536 } else {
1537 // TODO : Warn here
1538 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001539 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001540 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001541 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001542 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1543 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1544 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1545 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1546 auto image_view = update->pImageInfo[di].imageView;
1547 auto image_layout = update->pImageInfo[di].imageLayout;
1548 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error_code, error_msg)) {
1549 std::stringstream error_str;
1550 error_str << "Attempted write update to image descriptor failed due to: " << error_msg->c_str();
1551 *error_msg = error_str.str();
1552 return false;
1553 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001554 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001555 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001556 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001557 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1558 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1559 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1560 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001561 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001562 if (!bv_state) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001563 *error_code = VALIDATION_ERROR_15c00286;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001564 std::stringstream error_str;
1565 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1566 *error_msg = error_str.str();
1567 return false;
1568 }
1569 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisdf0d62a2017-10-11 08:48:00 -06001570 auto buffer_state = GetBufferState(device_data_, buffer);
1571 // Verify that buffer underlying the view hasn't been destroyed prematurely
1572 if (!buffer_state) {
1573 *error_code = VALIDATION_ERROR_15c00286;
1574 std::stringstream error_str;
1575 error_str << "Attempted write update to texel buffer descriptor failed because underlying buffer (" << buffer
1576 << ") has been destroyed: " << error_msg->c_str();
1577 *error_msg = error_str.str();
1578 return false;
1579 } else if (!ValidateBufferUsage(buffer_state, update->descriptorType, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001580 std::stringstream error_str;
1581 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error_msg->c_str();
1582 *error_msg = error_str.str();
1583 return false;
1584 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001585 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001586 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001587 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001588 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1589 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1590 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1591 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1592 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1593 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error_code, error_msg)) {
1594 std::stringstream error_str;
1595 error_str << "Attempted write update to buffer descriptor failed due to: " << error_msg->c_str();
1596 *error_msg = error_str.str();
1597 return false;
1598 }
1599 }
1600 break;
1601 }
1602 default:
1603 assert(0); // We've already verified update type so should never get here
1604 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001605 }
1606 // All checks passed so update contents are good
1607 return true;
1608}
1609// Verify that the contents of the update are ok, but don't perform actual update
1610bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001611 VkDescriptorType type, uint32_t index,
1612 UNIQUE_VALIDATION_ERROR_CODE *error_code,
1613 std::string *error_msg) const {
1614 // Note : Repurposing some Write update error codes here as specific details aren't called out for copy updates like they are
1615 // for write updates
Tobin Ehlis300888c2016-05-18 13:43:26 -06001616 switch (src_set->descriptors_[index]->descriptor_class) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001617 case PlainSampler: {
1618 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001619 const auto src_desc = src_set->descriptors_[index + di].get();
1620 if (!src_desc->updated) continue;
1621 if (!src_desc->IsImmutableSampler()) {
1622 auto update_sampler = static_cast<SamplerDescriptor *>(src_desc)->GetSampler();
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001623 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001624 *error_code = VALIDATION_ERROR_15c0028a;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001625 std::stringstream error_str;
1626 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1627 *error_msg = error_str.str();
1628 return false;
1629 }
1630 } else {
1631 // TODO : Warn here
1632 }
1633 }
1634 break;
1635 }
1636 case ImageSampler: {
1637 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001638 const auto src_desc = src_set->descriptors_[index + di].get();
1639 if (!src_desc->updated) continue;
1640 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001641 // First validate sampler
1642 if (!img_samp_desc->IsImmutableSampler()) {
1643 auto update_sampler = img_samp_desc->GetSampler();
1644 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001645 *error_code = VALIDATION_ERROR_15c0028a;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001646 std::stringstream error_str;
1647 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1648 *error_msg = error_str.str();
1649 return false;
1650 }
1651 } else {
1652 // TODO : Warn here
1653 }
1654 // Validate image
1655 auto image_view = img_samp_desc->GetImageView();
1656 auto image_layout = img_samp_desc->GetImageLayout();
1657 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001658 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001659 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001660 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001661 return false;
1662 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001663 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001664 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001665 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001666 case Image: {
1667 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001668 const auto src_desc = src_set->descriptors_[index + di].get();
1669 if (!src_desc->updated) continue;
1670 auto img_desc = static_cast<const ImageDescriptor *>(src_desc);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001671 auto image_view = img_desc->GetImageView();
1672 auto image_layout = img_desc->GetImageLayout();
1673 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error_code, error_msg)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001674 std::stringstream error_str;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001675 error_str << "Attempted copy update to image descriptor failed due to: " << error_msg->c_str();
Tobin Ehlis75f04ec2016-10-06 17:43:11 -06001676 *error_msg = error_str.str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001677 return false;
1678 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001679 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001680 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001681 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001682 case TexelBuffer: {
1683 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001684 const auto src_desc = src_set->descriptors_[index + di].get();
1685 if (!src_desc->updated) continue;
1686 auto buffer_view = static_cast<TexelDescriptor *>(src_desc)->GetBufferView();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001687 auto bv_state = GetBufferViewState(device_data_, buffer_view);
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001688 if (!bv_state) {
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001689 *error_code = VALIDATION_ERROR_15c00286;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001690 std::stringstream error_str;
1691 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1692 *error_msg = error_str.str();
1693 return false;
1694 }
1695 auto buffer = bv_state->create_info.buffer;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001696 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001697 std::stringstream error_str;
1698 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error_msg->c_str();
1699 *error_msg = error_str.str();
1700 return false;
1701 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001702 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001703 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001704 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001705 case GeneralBuffer: {
1706 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Józef Kucia5297e372017-10-13 22:31:34 +02001707 const auto src_desc = src_set->descriptors_[index + di].get();
1708 if (!src_desc->updated) continue;
1709 auto buffer = static_cast<BufferDescriptor *>(src_desc)->GetBuffer();
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001710 if (!ValidateBufferUsage(GetBufferState(device_data_, buffer), type, error_code, error_msg)) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001711 std::stringstream error_str;
1712 error_str << "Attempted copy update to buffer descriptor failed due to: " << error_msg->c_str();
1713 *error_msg = error_str.str();
1714 return false;
1715 }
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001716 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001717 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001718 }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -07001719 default:
1720 assert(0); // We've already verified update type so should never get here
1721 break;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001722 }
1723 // All checks passed so update contents are good
1724 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001725}
Tobin Ehlisf320b192017-03-14 11:22:50 -06001726// Update the common AllocateDescriptorSetsData
1727void cvdescriptorset::UpdateAllocateDescriptorSetsData(const layer_data *dev_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
1728 AllocateDescriptorSetsData *ds_data) {
1729 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
1730 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1731 if (layout) {
1732 ds_data->layout_nodes[i] = layout;
1733 // Count total descriptors required per type
1734 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1735 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
1736 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1737 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1738 }
1739 }
1740 // Any unknown layouts will be flagged as errors during ValidateAllocateDescriptorSets() call
1741 }
Petr Kraus13c98a62017-12-09 00:22:39 +01001742}
Tobin Ehlisee471462016-05-26 11:21:59 -06001743// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlisf320b192017-03-14 11:22:50 -06001744bool cvdescriptorset::ValidateAllocateDescriptorSets(const core_validation::layer_data *dev_data,
1745 const VkDescriptorSetAllocateInfo *p_alloc_info,
1746 const AllocateDescriptorSetsData *ds_data) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001747 bool skip = false;
Tobin Ehlisf320b192017-03-14 11:22:50 -06001748 auto report_data = core_validation::GetReportData(dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001749
1750 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001751 auto layout = GetDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
Tobin Ehlis815e8132016-06-02 13:02:17 -06001752 if (!layout) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001753 skip |=
Tobin Ehlisee471462016-05-26 11:21:59 -06001754 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
Petr Krausbc7f5442017-05-14 23:43:38 +02001755 HandleToUint64(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
Tobin Ehlisee471462016-05-26 11:21:59 -06001756 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
Petr Krausbc7f5442017-05-14 23:43:38 +02001757 HandleToUint64(p_alloc_info->pSetLayouts[i]));
Tobin Ehlisee471462016-05-26 11:21:59 -06001758 }
1759 }
Mark Lobodzinski28426ae2017-06-01 07:56:38 -06001760 if (!GetDeviceExtensions(dev_data)->vk_khr_maintenance1) {
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001761 auto pool_state = GetDescriptorPoolState(dev_data, p_alloc_info->descriptorPool);
1762 // Track number of descriptorSets allowable in this pool
1763 if (pool_state->availableSets < p_alloc_info->descriptorSetCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001764 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001765 HandleToUint64(pool_state->pool), __LINE__, VALIDATION_ERROR_04c00264, "DS",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001766 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1767 ". This pool only has %d descriptorSets remaining. %s",
Petr Krausbc7f5442017-05-14 23:43:38 +02001768 p_alloc_info->descriptorSetCount, HandleToUint64(pool_state->pool), pool_state->availableSets,
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001769 validation_error_map[VALIDATION_ERROR_04c00264]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001770 }
1771 // Determine whether descriptor counts are satisfiable
1772 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1773 if (ds_data->required_descriptors_by_type[i] > pool_state->availableDescriptorTypeCount[i]) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001774 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001775 HandleToUint64(pool_state->pool), __LINE__, VALIDATION_ERROR_04c00266, "DS",
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001776 "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1777 ". This pool only has %d descriptors of this type remaining. %s",
1778 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Petr Krausbc7f5442017-05-14 23:43:38 +02001779 HandleToUint64(pool_state->pool), pool_state->availableDescriptorTypeCount[i],
Tobin Ehlis3c37fb32017-05-24 09:31:13 -06001780 validation_error_map[VALIDATION_ERROR_04c00266]);
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001781 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001782 }
1783 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001784
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -06001785 return skip;
Tobin Ehlisee471462016-05-26 11:21:59 -06001786}
1787// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001788void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1789 const VkDescriptorSet *descriptor_sets,
1790 const AllocateDescriptorSetsData *ds_data,
Tobin Ehlisbd711bd2016-10-12 14:27:30 -06001791 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_STATE *> *pool_map,
Tobin Ehlis4e380592016-06-02 12:41:47 -06001792 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
John Zulauf48a6a702017-12-22 17:14:54 -07001793 layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001794 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Mark Lobodzinskic9430182017-06-13 13:00:05 -06001795 // Account for sets and individual descriptors allocated from pool
Tobin Ehlisee471462016-05-26 11:21:59 -06001796 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001797 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1798 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1799 }
Mark Lobodzinskic9430182017-06-13 13:00:05 -06001800 // Create tracking object for each descriptor set; insert into global map and the pool's set.
Tobin Ehlisee471462016-05-26 11:21:59 -06001801 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis93f22372016-10-12 14:34:12 -06001802 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], p_alloc_info->descriptorPool, ds_data->layout_nodes[i],
1803 dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001804
1805 pool_state->sets.insert(new_ds);
1806 new_ds->in_use.store(0);
1807 (*set_map)[descriptor_sets[i]] = new_ds;
1808 }
1809}
John Zulauf48a6a702017-12-22 17:14:54 -07001810
1811cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
1812 GLOBAL_CB_NODE *cb_state)
1813 : filtered_map_(), orig_map_(in_map) {
1814 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
1815 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
1816 ds.FilterAndTrackBindingReqs(cb_state, orig_map_, filtered_map_.get());
1817 }
1818}
1819cvdescriptorset::PrefilterBindRequestMap::PrefilterBindRequestMap(cvdescriptorset::DescriptorSet &ds, const BindingReqMap &in_map,
1820 GLOBAL_CB_NODE *cb_state, PIPELINE_STATE *pipeline)
1821 : filtered_map_(), orig_map_(in_map) {
1822 if (ds.GetTotalDescriptorCount() > kManyDescriptors_) {
1823 filtered_map_.reset(new std::map<uint32_t, descriptor_req>());
1824 ds.FilterAndTrackBindingReqs(cb_state, pipeline, orig_map_, filtered_map_.get());
1825 }
1826}