blob: c47cb50f0c76f80d5df40e2780983fe2235189db [file] [log] [blame]
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
4 * Copyright (C) 2015-2016 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Tobin Ehlis <tobine@google.com>
19 */
20
21#include "descriptor_sets.h"
22#include "vk_enum_string_helper.h"
23#include "vk_safe_struct.h"
24#include <sstream>
25
26// Construct DescriptorSetLayout instance from given create info
27cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(debug_report_data *report_data,
28 const VkDescriptorSetLayoutCreateInfo *p_create_info,
29 const VkDescriptorSetLayout layout)
30 : layout_(layout), binding_count_(p_create_info->bindingCount), descriptor_count_(0), dynamic_descriptor_count_(0) {
31 uint32_t global_index = 0;
32 for (uint32_t i = 0; i < binding_count_; ++i) {
33 descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
34 if (!binding_to_index_map_.emplace(p_create_info->pBindings[i].binding, i).second) {
35 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
36 reinterpret_cast<uint64_t &>(layout_), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
37 "duplicated binding number in "
38 "VkDescriptorSetLayoutBinding");
39 }
40 binding_to_global_start_index_map_[p_create_info->pBindings[i].binding] = global_index;
41 global_index += p_create_info->pBindings[i].descriptorCount ? p_create_info->pBindings[i].descriptorCount - 1 : 0;
42 binding_to_global_end_index_map_[p_create_info->pBindings[i].binding] = global_index;
43 global_index++;
Tobin Ehlis664e6012016-05-05 11:04:44 -060044 bindings_.push_back(safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i]));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060045 // In cases where we should ignore pImmutableSamplers make sure it's NULL
46 if ((p_create_info->pBindings[i].pImmutableSamplers) &&
47 ((p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_SAMPLER) &&
48 (p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER))) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060049 bindings_.back().pImmutableSamplers = nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060050 }
51 if (p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
52 p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
Tobin Ehlisef0de162016-06-20 13:07:34 -060053 dynamic_descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060054 }
55 }
56}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060057// put all bindings into the given set
58void cvdescriptorset::DescriptorSetLayout::FillBindingSet(std::unordered_set<uint32_t> *binding_set) const {
59 for (auto binding_index_pair : binding_to_index_map_)
60 binding_set->insert(binding_index_pair.first);
61}
Tobin Ehlis56a30942016-05-19 08:00:00 -060062
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060063VkDescriptorSetLayoutBinding const *
64cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060065 const auto &bi_itr = binding_to_index_map_.find(binding);
66 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060067 return bindings_[bi_itr->second].ptr();
Tobin Ehlis0bc30632016-05-05 10:16:02 -060068 }
69 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060070}
71VkDescriptorSetLayoutBinding const *
72cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const {
73 if (index >= bindings_.size())
74 return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -060075 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060076}
77// Return descriptorCount for given binding, 0 if index is unavailable
78uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060079 const auto &bi_itr = binding_to_index_map_.find(binding);
80 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060081 return bindings_[bi_itr->second].descriptorCount;
Tobin Ehlis0bc30632016-05-05 10:16:02 -060082 }
83 return 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060084}
85// Return descriptorCount for given index, 0 if index is unavailable
86uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
87 if (index >= bindings_.size())
88 return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -060089 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060090}
91// For the given binding, return descriptorType
92VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const {
93 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -060094 const auto &bi_itr = binding_to_index_map_.find(binding);
95 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060096 return bindings_[bi_itr->second].descriptorType;
Tobin Ehlis0bc30632016-05-05 10:16:02 -060097 }
98 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060099}
100// For the given index, return descriptorType
101VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
102 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600103 return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600104}
105// For the given global index, return descriptorType
106// Currently just counting up through bindings_, may improve this in future
107VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const {
108 uint32_t global_offset = 0;
109 for (auto binding : bindings_) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600110 global_offset += binding.descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600111 if (index < global_offset)
Tobin Ehlis664e6012016-05-05 11:04:44 -0600112 return binding.descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600113 }
114 assert(0); // requested global index is out of bounds
115 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
116}
117// For the given binding, return stageFlags
118VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const {
119 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600120 const auto &bi_itr = binding_to_index_map_.find(binding);
121 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600122 return bindings_[bi_itr->second].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600123 }
124 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600125}
126// For the given binding, return start index
127uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const {
128 assert(binding_to_global_start_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600129 const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding);
130 if (btgsi_itr != binding_to_global_start_index_map_.end()) {
131 return btgsi_itr->second;
132 }
133 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600134 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600135 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600136}
137// For the given binding, return end index
138uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
139 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600140 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
141 if (btgei_itr != binding_to_global_end_index_map_.end()) {
142 return btgei_itr->second;
143 }
144 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600145 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600146 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600147}
148// For given binding, return ptr to ImmutableSampler array
149VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
150 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600151 const auto &bi_itr = binding_to_index_map_.find(binding);
152 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600153 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600154 }
155 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600156}
157// For given index, return ptr to ImmutableSampler array
158VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
159 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600160 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600161}
162// If our layout is compatible with rh_ds_layout, return true,
163// else return false and fill in error_msg will description of what causes incompatibility
164bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
165 // Trivial case
166 if (layout_ == rh_ds_layout->GetDescriptorSetLayout())
167 return true;
168 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
169 std::stringstream error_str;
170 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
171 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
172 *error_msg = error_str.str();
173 return false; // trivial fail case
174 }
175 // Descriptor counts match so need to go through bindings one-by-one
176 // and verify that type and stageFlags match
177 for (auto binding : bindings_) {
178 // TODO : Do we also need to check immutable samplers?
179 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600180 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600181 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600182 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
183 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600184 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600185 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600186 *error_msg = error_str.str();
187 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600188 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600189 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600190 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
191 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600192 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600193 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600194 *error_msg = error_str.str();
195 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600196 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600197 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600198 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
199 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600200 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600201 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600202 *error_msg = error_str.str();
203 return false;
204 }
205 }
206 return true;
207}
208
209bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
210 if (!binding_to_index_map_.count(binding + 1))
211 return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600212 auto const &bi_itr = binding_to_index_map_.find(binding);
213 if (bi_itr != binding_to_index_map_.end()) {
214 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
215 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600216 auto type = bindings_[bi_itr->second].descriptorType;
217 auto stage_flags = bindings_[bi_itr->second].stageFlags;
218 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
219 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
220 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
221 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600222 return false;
223 }
224 return true;
225 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600226 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600227 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600228}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600229// Starting at offset descriptor of given binding, parse over update_count
230// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
231// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
232// If so, return true. If not, fill in error_msg and return false
233bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
234 const char *type, const VkDescriptorSet set,
235 std::string *error_msg) const {
236 // Verify consecutive bindings match (if needed)
237 auto orig_binding = current_binding;
238 // Track count of descriptors in the current_bindings that are remaining to be updated
239 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
240 // First, it's legal to offset beyond your own binding so handle that case
241 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
242 while (offset >= binding_remaining) {
243 // Advance to next binding, decrement offset by binding size
244 offset -= binding_remaining;
245 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
246 }
247 binding_remaining -= offset;
248 while (update_count > binding_remaining) { // While our updates overstep current binding
249 // Verify next consecutive binding matches type, stage flags & immutable sampler use
250 if (!IsNextBindingConsistent(current_binding++)) {
251 std::stringstream error_str;
252 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
253 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
254 "next binding is not consistent with current binding so this update is invalid.";
255 *error_msg = error_str.str();
256 return false;
257 }
258 // For sake of this check consider the bindings updated and grab count for next binding
259 update_count -= binding_remaining;
260 binding_remaining = GetDescriptorCountFromBinding(current_binding);
261 }
262 return true;
263}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600264
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600265cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
266 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
267
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600268cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const DescriptorSetLayout *layout,
Tobin Ehlis4e380592016-06-02 12:41:47 -0600269 const core_validation::layer_data *dev_data)
270 : some_update_(false), set_(set), p_layout_(layout), device_data_(dev_data) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600271 // Foreach binding, create default descriptors of given type
272 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
273 auto type = p_layout_->GetTypeFromIndex(i);
274 switch (type) {
275 case VK_DESCRIPTOR_TYPE_SAMPLER: {
276 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
277 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
278 if (immut_sampler)
Chris Forbescb621ea2016-05-30 11:47:31 +1200279 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600280 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200281 descriptors_.emplace_back(new SamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600282 }
283 break;
284 }
285 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
286 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
287 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
288 if (immut)
Chris Forbescb621ea2016-05-30 11:47:31 +1200289 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600290 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200291 descriptors_.emplace_back(new ImageSamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600292 }
293 break;
294 }
295 // ImageDescriptors
296 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
297 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
298 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
299 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200300 descriptors_.emplace_back(new ImageDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600301 break;
302 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
303 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
304 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200305 descriptors_.emplace_back(new TexelDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600306 break;
307 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
308 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
309 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
310 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
311 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200312 descriptors_.emplace_back(new BufferDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600313 break;
314 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600315 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600316 break;
317 }
318 }
319}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600320
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600321cvdescriptorset::DescriptorSet::~DescriptorSet() {
322 InvalidateBoundCmdBuffers();
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600323}
Chris Forbes57989132016-07-26 17:06:10 +1200324
325
326static char const * string_descriptor_req_view_type(descriptor_req req) {
327 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
328 if (req & (1 << i)) {
329 return string_VkImageViewType(VkImageViewType(i));
330 }
331 }
332
333 return "(none)";
334}
335
336
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600337// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
338bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
339 return layout->IsCompatible(p_layout_, error);
340}
Chris Forbes57989132016-07-26 17:06:10 +1200341
Tobin Ehlis3066db62016-08-22 08:12:23 -0600342// 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 -0600343// This includes validating that all descriptors in the given bindings are updated,
344// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
345// Return true if state is acceptable, or false and write an error message into error string
Chris Forbesc7090a82016-07-25 18:10:41 +1200346bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600347 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
iostrows5eb97652016-06-02 15:56:26 +0200348 auto dyn_offset_index = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200349 for (auto binding_pair : bindings) {
350 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600351 if (!p_layout_->HasBinding(binding)) {
352 std::stringstream error_str;
353 error_str << "Attempting to validate DrawState for binding #" << binding
354 << " which is an invalid binding for this descriptor set.";
355 *error = error_str.str();
356 return false;
357 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600358 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600359 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600360 // Nothing to do for strictly immutable sampler
361 } else {
362 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600363 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600364 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600365 std::stringstream error_str;
366 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
367 << " is being used in draw but has not been updated.";
368 *error = error_str.str();
369 return false;
370 } else {
Chris Forbes57989132016-07-26 17:06:10 +1200371 auto descriptor_class = descriptors_[i]->GetClass();
372 if (descriptor_class == GeneralBuffer) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600373 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600374 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600375 auto buffer_node = getBufferNode(device_data_, buffer);
376 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600377 std::stringstream error_str;
378 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
379 << " references invalid buffer " << buffer << ".";
380 *error = error_str.str();
381 return false;
382 } else {
Tobin Ehlis997b2582016-06-02 08:43:37 -0600383 auto mem_entry = getMemObjInfo(device_data_, buffer_node->mem);
384 if (!mem_entry) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600385 std::stringstream error_str;
386 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600387 << " uses buffer " << buffer << " that references invalid memory " << buffer_node->mem
388 << ".";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600389 *error = error_str.str();
390 return false;
391 }
392 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600393 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600394 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600395 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600396 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
397 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600398 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
399 if (VK_WHOLE_SIZE == range) {
400 if ((dyn_offset + desc_offset) > buffer_size) {
401 std::stringstream error_str;
402 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
403 << " uses buffer " << buffer
404 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
405 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
406 << buffer_size << ".";
407 *error = error_str.str();
408 return false;
409 }
410 } else {
411 if ((dyn_offset + desc_offset + range) > buffer_size) {
412 std::stringstream error_str;
413 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
414 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
415 << " combined with offset " << desc_offset << " and range " << range
416 << " that oversteps the buffer size of " << buffer_size << ".";
417 *error = error_str.str();
418 return false;
419 }
420 }
421 }
422 }
Chris Forbes57989132016-07-26 17:06:10 +1200423 else if (descriptor_class == ImageSampler || descriptor_class == Image) {
424 auto image_view = (descriptor_class == ImageSampler)
425 ? static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView()
426 : static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
427 auto reqs = binding_pair.second;
428
429 auto image_view_data = getImageViewData(device_data_, image_view);
430 assert(image_view_data);
431
432 if (~reqs & (1 << image_view_data->viewType)) {
433 // bad view type
434 std::stringstream error_str;
435 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
436 << " requires an image view of type " << string_descriptor_req_view_type(reqs)
437 << " but got " << string_VkImageViewType(image_view_data->viewType) << ".";
438 *error = error_str.str();
439 return false;
440 }
441
442 auto image_node = getImageNode(device_data_, image_view_data->image);
443 assert(image_node);
444
445 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) &&
446 image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
447 std::stringstream error_str;
448 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
449 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
450 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
451 *error = error_str.str();
452 return false;
453 }
454
455 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) &&
456 image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
457 std::stringstream error_str;
458 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
459 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
460 *error = error_str.str();
461 return false;
462 }
463 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600464 }
465 }
466 }
467 }
468 return true;
469}
Chris Forbes57989132016-07-26 17:06:10 +1200470
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600471// For given bindings, place any update buffers or images into the passed-in unordered_sets
Chris Forbesc7090a82016-07-25 18:10:41 +1200472uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600473 std::unordered_set<VkBuffer> *buffer_set,
474 std::unordered_set<VkImageView> *image_set) const {
475 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200476 for (auto binding_pair : bindings) {
477 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600478 // If a binding doesn't exist, skip it
479 if (!p_layout_->HasBinding(binding)) {
480 continue;
481 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600482 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600483 if (descriptors_[start_idx]->IsStorage()) {
484 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600485 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600486 if (descriptors_[start_idx + i]->updated) {
487 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600488 num_updates++;
489 }
490 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600491 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600492 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600493 if (descriptors_[start_idx + i]->updated) {
494 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -0600495 auto bv_info = getBufferViewInfo(device_data_, bufferview);
496 if (bv_info) {
497 buffer_set->insert(bv_info->buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600498 num_updates++;
499 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600500 }
501 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600502 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600503 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600504 if (descriptors_[start_idx + i]->updated) {
505 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600506 num_updates++;
507 }
508 }
509 }
510 }
511 }
512 return num_updates;
513}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600514// Set is being deleted or updates so invalidate all bound cmd buffers
515void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600516 core_validation::invalidateCommandBuffers(cb_bindings,
517 {reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600518}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600519// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600520void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600521 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
522 // perform update
523 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
524 descriptors_[start_idx + di]->WriteUpdate(update, di);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600525 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600526 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600527 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600528
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600529 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600530}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600531// Validate Copy update
532bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
533 const DescriptorSet *src_set, std::string *error) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600534 // Verify idle ds
535 if (in_use.load()) {
536 std::stringstream error_str;
537 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
538 << " that is in use by a command buffer.";
539 *error = error_str.str();
540 return false;
541 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600542 if (!p_layout_->HasBinding(update->dstBinding)) {
543 std::stringstream error_str;
544 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
545 *error = error_str.str();
546 return false;
547 }
548 if (!src_set->HasBinding(update->srcBinding)) {
549 std::stringstream error_str;
550 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
551 *error = error_str.str();
552 return false;
553 }
554 // src & dst set bindings are valid
555 // Check bounds of src & dst
556 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
557 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
558 // SRC update out of bounds
559 std::stringstream error_str;
560 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
561 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
562 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
563 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
564 *error = error_str.str();
565 return false;
566 }
567 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
568 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
569 // DST update out of bounds
570 std::stringstream error_str;
571 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
572 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
573 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
574 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
575 *error = error_str.str();
576 return false;
577 }
578 // Check that types match
579 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
580 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
581 if (src_type != dst_type) {
582 std::stringstream error_str;
583 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
584 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
585 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
586 *error = error_str.str();
587 return false;
588 }
589 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600590 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
591 "copy update from", src_set->GetSet(), error)) ||
592 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
593 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600594 return false;
595 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600596 // First make sure source descriptors are updated
597 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
598 if (!src_set->descriptors_[src_start_idx + i]) {
599 std::stringstream error_str;
600 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
601 << update->srcArrayElement + i << " has not been updated.";
602 *error = error_str.str();
603 return false;
604 }
605 }
606 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600607 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600608 return false;
609
610 // All checks passed so update is good
611 return true;
612}
613// Perform Copy update
614void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600615 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
616 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600617 // Update parameters all look good so perform update
618 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600619 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600620 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600621 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600622 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600623
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600624 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600625}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600626
Tobin Ehlisf9519102016-08-17 09:49:13 -0600627// Bind cb_node to this set and this set to cb_node.
628// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
629// to be used in a draw by the given cb_node
Tobin Ehlisf9519102016-08-17 09:49:13 -0600630void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node, const std::unordered_set<uint32_t> &bindings) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600631 // bind cb to this descriptor set
632 cb_bindings.insert(cb_node);
633 // Add bindings for descriptor set and individual objects in the set
634 cb_node->object_bindings.insert({reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlisf9519102016-08-17 09:49:13 -0600635 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
636 // resources
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600637 for (auto binding : bindings) {
638 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
639 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
640 for (uint32_t i = start_idx; i <= end_idx; ++i) {
641 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
642 }
643 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600644}
645
Tobin Ehlis300888c2016-05-18 13:43:26 -0600646cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600647 updated = false;
648 descriptor_class = PlainSampler;
649};
650
Tobin Ehlis300888c2016-05-18 13:43:26 -0600651cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600652 updated = false;
653 descriptor_class = PlainSampler;
654 if (immut) {
655 sampler_ = *immut;
656 immutable_ = true;
657 updated = true;
658 }
659}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600660// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
661bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
662 return (getSamplerNode(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600663}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600664
Tobin Ehlis554bf382016-05-24 11:14:43 -0600665bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600666 const core_validation::layer_data *dev_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600667 std::string *error) {
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600668 auto iv_data = getImageViewData(dev_data, image_view);
669 if (!iv_data) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600670 std::stringstream error_str;
671 error_str << "Invalid VkImageView: " << image_view;
672 *error = error_str.str();
673 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600674 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600675 // 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 -0600676 // Validate that imageLayout is compatible with aspect_mask and image format
677 // and validate that image usage bits are correct for given usage
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600678 VkImageAspectFlags aspect_mask = iv_data->subresourceRange.aspectMask;
679 VkImage image = iv_data->image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600680 VkFormat format = VK_FORMAT_MAX_ENUM;
681 VkImageUsageFlags usage = 0;
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600682 auto image_node = getImageNode(dev_data, image);
683 if (image_node) {
684 format = image_node->createInfo.format;
685 usage = image_node->createInfo.usage;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600686 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600687 // Also need to check the swapchains.
Tobin Ehlis969a5262016-06-02 12:13:32 -0600688 auto swapchain = getSwapchainFromImage(dev_data, image);
689 if (swapchain) {
Tobin Ehlis4e380592016-06-02 12:41:47 -0600690 auto swapchain_node = getSwapchainNode(dev_data, swapchain);
691 if (swapchain_node) {
692 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600693 }
694 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600695 }
696 // First validate that format and layout are compatible
697 if (format == VK_FORMAT_MAX_ENUM) {
698 std::stringstream error_str;
699 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
700 *error = error_str.str();
701 return false;
702 }
703 bool ds = vk_format_is_depth_or_stencil(format);
704 switch (image_layout) {
705 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
706 // Only Color bit must be set
707 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600708 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600709 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
710 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis554bf382016-05-24 11:14:43 -0600711 *error = error_str.str();
712 return false;
713 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600714 // format must NOT be DS
715 if (ds) {
716 std::stringstream error_str;
717 error_str << "ImageView (" << image_view
718 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
719 << string_VkFormat(format) << " which is not a color format.";
720 *error = error_str.str();
721 return false;
722 }
723 break;
724 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
725 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
726 // Depth or stencil bit must be set, but both must NOT be set
727 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
728 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
729 // both must NOT be set
730 std::stringstream error_str;
731 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
732 *error = error_str.str();
733 return false;
734 }
735 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
736 // Neither were set
737 std::stringstream error_str;
738 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
739 << " but does not have STENCIL or DEPTH aspects set";
740 *error = error_str.str();
741 return false;
742 }
743 // format must be DS
744 if (!ds) {
745 std::stringstream error_str;
746 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
747 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
748 *error = error_str.str();
749 return false;
750 }
751 break;
752 default:
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600753 // For other layouts if the source is ds image, both aspect bits must not be set
754 if (ds) {
755 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
756 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
757 // both must NOT be set
758 std::stringstream error_str;
759 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
760 << " and is using depth/stencil image of format " << string_VkFormat(format)
761 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
762 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
763 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
764 "reads respectively.";
765 *error = error_str.str();
766 return false;
767 }
768 }
769 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600770 break;
771 }
772 // Now validate that usage flags are correctly set for given type of update
773 std::string error_usage_bit;
774 switch (type) {
775 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
776 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
777 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
778 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
779 }
780 break;
781 }
782 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
783 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
784 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
785 }
786 break;
787 }
788 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
789 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
790 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
791 }
792 break;
793 }
794 default:
795 break;
796 }
797 if (!error_usage_bit.empty()) {
798 std::stringstream error_str;
799 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
800 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
801 << error_usage_bit << " set.";
802 *error = error_str.str();
803 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600804 }
805 return true;
806}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600807
Tobin Ehlis300888c2016-05-18 13:43:26 -0600808void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
809 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600810 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600811}
812
Tobin Ehlis300888c2016-05-18 13:43:26 -0600813void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600814 if (!immutable_) {
815 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600816 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600817 }
818 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600819}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600820
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600821void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
822 if (!immutable_) {
823 auto sampler_node = getSamplerNode(dev_data, sampler_);
824 if (sampler_node)
825 core_validation::AddCommandBufferBindingSampler(cb_node, sampler_node);
826 }
827}
828
Tobin Ehlis300888c2016-05-18 13:43:26 -0600829cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
830 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600831 updated = false;
832 descriptor_class = ImageSampler;
833}
834
Tobin Ehlis300888c2016-05-18 13:43:26 -0600835cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
836 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600837 updated = false;
838 descriptor_class = ImageSampler;
839 if (immut) {
840 sampler_ = *immut;
841 immutable_ = true;
842 updated = true;
843 }
844}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600845
Tobin Ehlis300888c2016-05-18 13:43:26 -0600846void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600847 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600848 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600849 sampler_ = image_info.sampler;
850 image_view_ = image_info.imageView;
851 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600852}
853
Tobin Ehlis300888c2016-05-18 13:43:26 -0600854void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600855 if (!immutable_) {
856 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600857 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600858 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600859 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
860 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600861 updated = true;
862 image_view_ = image_view;
863 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600864}
865
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600866void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data,
867 GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600868 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600869 if (!immutable_) {
870 auto sampler_node = getSamplerNode(dev_data, sampler_);
871 if (sampler_node)
872 core_validation::AddCommandBufferBindingSampler(cb_node, sampler_node);
873 }
Tobin Ehlis81e46372016-08-17 13:33:44 -0600874 // Add binding for image
875 auto iv_data = getImageViewData(dev_data, image_view_);
876 if (iv_data) {
877 auto image_node = getImageNode(dev_data, iv_data->image);
878 if (image_node)
879 core_validation::AddCommandBufferBindingImage(dev_data, cb_node, image_node);
880 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600881}
882
Tobin Ehlis300888c2016-05-18 13:43:26 -0600883cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
884 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600885 updated = false;
886 descriptor_class = Image;
887 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
888 storage_ = true;
889};
890
Tobin Ehlis300888c2016-05-18 13:43:26 -0600891void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600892 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600893 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600894 image_view_ = image_info.imageView;
895 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600896}
897
Tobin Ehlis300888c2016-05-18 13:43:26 -0600898void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600899 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
900 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600901 updated = true;
902 image_view_ = image_view;
903 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600904}
905
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600906void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600907 // Add binding for image
908 auto iv_data = getImageViewData(dev_data, image_view_);
909 if (iv_data) {
910 auto image_node = getImageNode(dev_data, iv_data->image);
911 if (image_node)
912 core_validation::AddCommandBufferBindingImage(dev_data, cb_node, image_node);
913 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600914}
915
Tobin Ehlis300888c2016-05-18 13:43:26 -0600916cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
917 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600918 updated = false;
919 descriptor_class = GeneralBuffer;
920 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
921 dynamic_ = true;
922 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
923 storage_ = true;
924 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
925 dynamic_ = true;
926 storage_ = true;
927 }
928}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600929void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600930 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600931 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600932 buffer_ = buffer_info.buffer;
933 offset_ = buffer_info.offset;
934 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600935}
936
Tobin Ehlis300888c2016-05-18 13:43:26 -0600937void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
938 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600939 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600940 buffer_ = buff_desc->buffer_;
941 offset_ = buff_desc->offset_;
942 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600943}
944
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600945void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600946 auto buffer_node = getBufferNode(dev_data, buffer_);
947 if (buffer_node)
948 core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600949}
950
Tobin Ehlis300888c2016-05-18 13:43:26 -0600951cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600952 updated = false;
953 descriptor_class = TexelBuffer;
954 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
955 storage_ = true;
956};
Tobin Ehlis56a30942016-05-19 08:00:00 -0600957
Tobin Ehlis300888c2016-05-18 13:43:26 -0600958void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600959 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600960 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600961}
962
Tobin Ehlis300888c2016-05-18 13:43:26 -0600963void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
964 updated = true;
965 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
966}
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600967
968void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600969 auto bv_info = getBufferViewInfo(dev_data, buffer_view_);
970 if (bv_info) {
971 auto buffer_node = getBufferNode(dev_data, bv_info->buffer);
972 if (buffer_node)
973 core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
974 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600975}
976
Tobin Ehlis300888c2016-05-18 13:43:26 -0600977// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
978// sets, and then calls their respective Validate[Write|Copy]Update functions.
979// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
980// be skipped, then true is returned.
981// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600982bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
983 const core_validation::layer_data *dev_data, uint32_t write_count,
984 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
985 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600986 bool skip_call = false;
987 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600988 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600989 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600990 auto set_node = core_validation::getSetNode(dev_data, dest_set);
991 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600992 skip_call |=
993 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis56a30942016-05-19 08:00:00 -0600994 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600995 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
996 reinterpret_cast<uint64_t &>(dest_set));
997 } else {
998 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600999 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001000 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis585f66d2016-07-01 18:23:58 -06001001 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_WRITE_UPDATE, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001002 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
1003 " with error: %s",
1004 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
1005 }
1006 }
1007 }
1008 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001009 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001010 auto dst_set = p_cds[i].dstSet;
1011 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001012 auto src_node = core_validation::getSetNode(dev_data, src_set);
1013 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
1014 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001015 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis56a30942016-05-19 08:00:00 -06001016 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001017 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
1018 " that has not been allocated.",
1019 reinterpret_cast<uint64_t &>(src_set));
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001020 } else if (!dst_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001021 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis56a30942016-05-19 08:00:00 -06001022 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001023 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
1024 " that has not been allocated.",
1025 reinterpret_cast<uint64_t &>(dst_set));
1026 } else {
1027 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001028 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001029 skip_call |=
1030 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis585f66d2016-07-01 18:23:58 -06001031 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_COPY_UPDATE, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001032 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
1033 " to Descriptor Set 0x%" PRIx64 " with error: %s",
1034 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
1035 }
1036 }
1037 }
1038 return skip_call;
1039}
1040// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1041// sets, and then calls their respective Perform[Write|Copy]Update functions.
1042// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1043// with the same set of updates.
1044// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1045// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001046void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
1047 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1048 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001049 // Write updates first
1050 uint32_t i = 0;
1051 for (i = 0; i < write_count; ++i) {
1052 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001053 auto set_node = core_validation::getSetNode(dev_data, dest_set);
1054 if (set_node) {
1055 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001056 }
1057 }
1058 // Now copy updates
1059 for (i = 0; i < copy_count; ++i) {
1060 auto dst_set = p_cds[i].dstSet;
1061 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001062 auto src_node = core_validation::getSetNode(dev_data, src_set);
1063 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
1064 if (src_node && dst_node) {
1065 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001066 }
1067 }
1068}
1069// Validate the state for a given write update but don't actually perform the update
1070// If an error would occur for this update, return false and fill in details in error_msg string
1071bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
1072 std::string *error_msg) {
1073 // Verify idle ds
1074 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001075 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001076 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
1077 << " that is in use by a command buffer.";
1078 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001079 return false;
1080 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001081 // Verify dst binding exists
1082 if (!p_layout_->HasBinding(update->dstBinding)) {
1083 std::stringstream error_str;
1084 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
1085 *error_msg = error_str.str();
1086 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001087 }
1088 // We know that binding is valid, verify update and do update on each descriptor
1089 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
1090 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
1091 if (type != update->descriptorType) {
1092 std::stringstream error_str;
1093 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1094 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1095 *error_msg = error_str.str();
1096 return false;
1097 }
1098 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
1099 std::stringstream error_str;
1100 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
1101 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
1102 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
1103 << " combined with update array element offset of " << update->dstArrayElement
1104 << " oversteps the size of this descriptor set.";
1105 *error_msg = error_str.str();
1106 return false;
1107 }
1108 // Verify consecutive bindings match (if needed)
1109 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
1110 set_, error_msg))
1111 return false;
1112 // Update is within bounds and consistent so last step is to validate update contents
1113 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
1114 std::stringstream error_str;
1115 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1116 << " failed with error message: " << error_msg->c_str();
1117 *error_msg = error_str.str();
1118 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001119 }
1120 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001121 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001122}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001123// For the given buffer, verify that its creation parameters are appropriate for the given type
1124// If there's an error, update the error string with details and return false, else return true
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001125bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_NODE const *buffer_node, VkDescriptorType type,
1126 std::string *error) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001127 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001128 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001129 std::string error_usage_bit;
1130 switch (type) {
1131 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1132 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
1133 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1134 }
1135 break;
1136 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1137 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1138 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1139 }
1140 break;
1141 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1142 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1143 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1144 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1145 }
1146 break;
1147 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1148 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1149 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1150 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1151 }
1152 break;
1153 default:
1154 break;
1155 }
1156 if (!error_usage_bit.empty()) {
1157 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001158 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1159 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1160 << error_usage_bit << " set.";
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001161 *error = error_str.str();
1162 return false;
1163 }
1164 return true;
1165}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001166// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1167// 1. buffer is valid
1168// 2. buffer was created with correct usage flags
1169// 3. offset is less than buffer size
1170// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
1171// If there's an error, update the error string with details and return false, else return true
1172bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
1173 std::string *error) const {
1174 // First make sure that buffer is valid
1175 auto buffer_node = getBufferNode(device_data_, buffer_info->buffer);
1176 if (!buffer_node) {
1177 std::stringstream error_str;
1178 error_str << "Invalid VkBuffer: " << buffer_info->buffer;
1179 *error = error_str.str();
1180 return false;
1181 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001182 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()"))
1183 return false;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001184 // Verify usage bits
1185 if (!ValidateBufferUsage(buffer_node, type, error)) {
1186 // error will have been updated by ValidateBufferUsage()
1187 return false;
1188 }
1189 // offset must be less than buffer size
1190 if (buffer_info->offset > buffer_node->createInfo.size) {
1191 std::stringstream error_str;
1192 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than buffer " << buffer_node->buffer
1193 << " size of " << buffer_node->createInfo.size;
1194 *error = error_str.str();
1195 return false;
1196 }
1197 if (buffer_info->range != VK_WHOLE_SIZE) {
1198 // Range must be VK_WHOLE_SIZE or > 0
1199 if (!buffer_info->range) {
1200 std::stringstream error_str;
1201 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
1202 *error = error_str.str();
1203 return false;
1204 }
1205 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1206 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
1207 std::stringstream error_str;
1208 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1209 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
1210 *error = error_str.str();
1211 return false;
1212 }
1213 }
1214 return true;
1215}
1216
Tobin Ehlis300888c2016-05-18 13:43:26 -06001217// Verify that the contents of the update are ok, but don't perform actual update
1218bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
1219 std::string *error) const {
1220 switch (update->descriptorType) {
1221 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1222 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1223 // Validate image
1224 auto image_view = update->pImageInfo[di].imageView;
1225 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis4e380592016-06-02 12:41:47 -06001226 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001227 std::stringstream error_str;
1228 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1229 *error = error_str.str();
1230 return false;
1231 }
1232 }
1233 // Intentional fall-through to validate sampler
1234 }
1235 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1236 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1237 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001238 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001239 std::stringstream error_str;
1240 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1241 << update->pImageInfo[di].sampler << ".";
1242 *error = error_str.str();
1243 return false;
1244 }
1245 } else {
1246 // TODO : Warn here
1247 }
1248 }
1249 break;
1250 }
1251 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1252 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1253 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1254 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1255 auto image_view = update->pImageInfo[di].imageView;
1256 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis4e380592016-06-02 12:41:47 -06001257 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001258 std::stringstream error_str;
1259 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1260 *error = error_str.str();
1261 return false;
1262 }
1263 }
1264 break;
1265 }
1266 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1267 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1268 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1269 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis424859c2016-06-02 09:43:11 -06001270 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1271 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001272 std::stringstream error_str;
1273 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1274 *error = error_str.str();
1275 return false;
1276 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001277 auto buffer = bv_info->buffer;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001278 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), update->descriptorType, error)) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001279 std::stringstream error_str;
1280 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str();
1281 *error = error_str.str();
1282 return false;
1283 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001284 }
1285 break;
1286 }
1287 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1288 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1289 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1290 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1291 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001292 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001293 std::stringstream error_str;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001294 error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001295 *error = error_str.str();
1296 return false;
1297 }
1298 }
1299 break;
1300 }
1301 default:
1302 assert(0); // We've already verified update type so should never get here
1303 break;
1304 }
1305 // All checks passed so update contents are good
1306 return true;
1307}
1308// Verify that the contents of the update are ok, but don't perform actual update
1309bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001310 VkDescriptorType type, uint32_t index, std::string *error) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001311 switch (src_set->descriptors_[index]->descriptor_class) {
1312 case PlainSampler: {
1313 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1314 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1315 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001316 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001317 std::stringstream error_str;
1318 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1319 *error = error_str.str();
1320 return false;
1321 }
1322 } else {
1323 // TODO : Warn here
1324 }
1325 }
1326 break;
1327 }
1328 case ImageSampler: {
1329 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1330 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1331 // First validate sampler
1332 if (!img_samp_desc->IsImmutableSampler()) {
1333 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001334 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001335 std::stringstream error_str;
1336 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1337 *error = error_str.str();
1338 return false;
1339 }
1340 } else {
1341 // TODO : Warn here
1342 }
1343 // Validate image
1344 auto image_view = img_samp_desc->GetImageView();
1345 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlis4e380592016-06-02 12:41:47 -06001346 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001347 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001348 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001349 *error = error_str.str();
1350 return false;
1351 }
1352 }
1353 }
1354 case Image: {
1355 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1356 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1357 auto image_view = img_desc->GetImageView();
1358 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlis4e380592016-06-02 12:41:47 -06001359 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001360 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001361 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001362 *error = error_str.str();
1363 return false;
1364 }
1365 }
1366 break;
1367 }
1368 case TexelBuffer: {
1369 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1370 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -06001371 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1372 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001373 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001374 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1375 *error = error_str.str();
1376 return false;
1377 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001378 auto buffer = bv_info->buffer;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001379 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), type, error)) {
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001380 std::stringstream error_str;
1381 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001382 *error = error_str.str();
1383 return false;
1384 }
1385 }
1386 break;
1387 }
1388 case GeneralBuffer: {
1389 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1390 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001391 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), type, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001392 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001393 error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001394 *error = error_str.str();
1395 return false;
1396 }
1397 }
1398 break;
1399 }
1400 default:
1401 assert(0); // We've already verified update type so should never get here
1402 break;
1403 }
1404 // All checks passed so update contents are good
1405 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001406}
Tobin Ehlisee471462016-05-26 11:21:59 -06001407// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlis815e8132016-06-02 13:02:17 -06001408bool cvdescriptorset::ValidateAllocateDescriptorSets(const debug_report_data *report_data,
1409 const VkDescriptorSetAllocateInfo *p_alloc_info,
1410 const core_validation::layer_data *dev_data,
1411 AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001412 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001413
1414 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001415 auto layout = getDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1416 if (!layout) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001417 skip_call |=
1418 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1419 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1420 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1421 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001422 } else {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001423 ds_data->layout_nodes[i] = layout;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001424 // Count total descriptors required per type
Tobin Ehlis815e8132016-06-02 13:02:17 -06001425 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1426 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001427 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1428 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1429 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001430 }
1431 }
Tobin Ehlis815e8132016-06-02 13:02:17 -06001432 auto pool_node = getPoolNode(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001433 // Track number of descriptorSets allowable in this pool
1434 if (pool_node->availableSets < p_alloc_info->descriptorSetCount) {
1435 skip_call |= log_msg(
1436 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1437 reinterpret_cast<uint64_t &>(pool_node->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY, "DS",
1438 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64 ". This pool only has %d descriptorSets remaining.",
1439 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_node->pool), pool_node->availableSets);
1440 }
1441 // Determine whether descriptor counts are satisfiable
1442 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1443 if (ds_data->required_descriptors_by_type[i] > pool_node->availableDescriptorTypeCount[i]) {
1444 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1445 reinterpret_cast<const uint64_t &>(pool_node->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1446 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1447 ". This pool only has %d descriptors of this type remaining.",
1448 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
1449 reinterpret_cast<uint64_t &>(pool_node->pool), pool_node->availableDescriptorTypeCount[i]);
Tobin Ehlisee471462016-05-26 11:21:59 -06001450 }
1451 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001452
Tobin Ehlisee471462016-05-26 11:21:59 -06001453 return skip_call;
1454}
1455// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001456void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1457 const VkDescriptorSet *descriptor_sets,
1458 const AllocateDescriptorSetsData *ds_data,
1459 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map,
1460 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
1461 const core_validation::layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001462 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001463 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001464 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001465 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1466 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1467 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001468 /* Create tracking object for each descriptor set; insert into
1469 * global map and the pool's set.
1470 */
1471 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis4e380592016-06-02 12:41:47 -06001472 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], ds_data->layout_nodes[i], dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001473
1474 pool_state->sets.insert(new_ds);
1475 new_ds->in_use.store(0);
1476 (*set_map)[descriptor_sets[i]] = new_ds;
1477 }
1478}