blob: 38915b684f7bc5b7b80011f7561eee74ed14e037 [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
630// TODO: For all of the active bindings, bind their underlying image/buffer/sampler resources to the cb_node
631void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node, const std::unordered_set<uint32_t> &bindings) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600632 // bind cb to this descriptor set
633 cb_bindings.insert(cb_node);
634 // Add bindings for descriptor set and individual objects in the set
635 cb_node->object_bindings.insert({reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
636 // TODO : Can bind individual objects from within each descriptor : buffers/images and their views, samplers & memory
637 // The trick is we should only bind the objects actually "in use" by the cmd buffer, meaning that we need to
638 // check active descriptor slots based on last bound state for this CB
Tobin Ehlisf9519102016-08-17 09:49:13 -0600639 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
640 // resources
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600641}
642
Tobin Ehlis300888c2016-05-18 13:43:26 -0600643cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600644 updated = false;
645 descriptor_class = PlainSampler;
646};
647
Tobin Ehlis300888c2016-05-18 13:43:26 -0600648cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600649 updated = false;
650 descriptor_class = PlainSampler;
651 if (immut) {
652 sampler_ = *immut;
653 immutable_ = true;
654 updated = true;
655 }
656}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600657// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
658bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
659 return (getSamplerNode(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600660}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600661
Tobin Ehlis554bf382016-05-24 11:14:43 -0600662bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600663 const core_validation::layer_data *dev_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600664 std::string *error) {
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600665 auto iv_data = getImageViewData(dev_data, image_view);
666 if (!iv_data) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600667 std::stringstream error_str;
668 error_str << "Invalid VkImageView: " << image_view;
669 *error = error_str.str();
670 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600671 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600672 // 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 -0600673 // Validate that imageLayout is compatible with aspect_mask and image format
674 // and validate that image usage bits are correct for given usage
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600675 VkImageAspectFlags aspect_mask = iv_data->subresourceRange.aspectMask;
676 VkImage image = iv_data->image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600677 VkFormat format = VK_FORMAT_MAX_ENUM;
678 VkImageUsageFlags usage = 0;
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600679 auto image_node = getImageNode(dev_data, image);
680 if (image_node) {
681 format = image_node->createInfo.format;
682 usage = image_node->createInfo.usage;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600683 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600684 // Also need to check the swapchains.
Tobin Ehlis969a5262016-06-02 12:13:32 -0600685 auto swapchain = getSwapchainFromImage(dev_data, image);
686 if (swapchain) {
Tobin Ehlis4e380592016-06-02 12:41:47 -0600687 auto swapchain_node = getSwapchainNode(dev_data, swapchain);
688 if (swapchain_node) {
689 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600690 }
691 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600692 }
693 // First validate that format and layout are compatible
694 if (format == VK_FORMAT_MAX_ENUM) {
695 std::stringstream error_str;
696 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
697 *error = error_str.str();
698 return false;
699 }
700 bool ds = vk_format_is_depth_or_stencil(format);
701 switch (image_layout) {
702 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
703 // Only Color bit must be set
704 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600705 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600706 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
707 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis554bf382016-05-24 11:14:43 -0600708 *error = error_str.str();
709 return false;
710 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600711 // format must NOT be DS
712 if (ds) {
713 std::stringstream error_str;
714 error_str << "ImageView (" << image_view
715 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
716 << string_VkFormat(format) << " which is not a color format.";
717 *error = error_str.str();
718 return false;
719 }
720 break;
721 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
722 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
723 // Depth or stencil bit must be set, but both must NOT be set
724 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
725 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
726 // both must NOT be set
727 std::stringstream error_str;
728 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
729 *error = error_str.str();
730 return false;
731 }
732 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
733 // Neither were set
734 std::stringstream error_str;
735 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
736 << " but does not have STENCIL or DEPTH aspects set";
737 *error = error_str.str();
738 return false;
739 }
740 // format must be DS
741 if (!ds) {
742 std::stringstream error_str;
743 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
744 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
745 *error = error_str.str();
746 return false;
747 }
748 break;
749 default:
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600750 // For other layouts if the source is ds image, both aspect bits must not be set
751 if (ds) {
752 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
753 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
754 // both must NOT be set
755 std::stringstream error_str;
756 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
757 << " and is using depth/stencil image of format " << string_VkFormat(format)
758 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
759 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
760 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
761 "reads respectively.";
762 *error = error_str.str();
763 return false;
764 }
765 }
766 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600767 break;
768 }
769 // Now validate that usage flags are correctly set for given type of update
770 std::string error_usage_bit;
771 switch (type) {
772 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
773 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
774 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
775 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
776 }
777 break;
778 }
779 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
780 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
781 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
782 }
783 break;
784 }
785 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
786 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
787 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
788 }
789 break;
790 }
791 default:
792 break;
793 }
794 if (!error_usage_bit.empty()) {
795 std::stringstream error_str;
796 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
797 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
798 << error_usage_bit << " set.";
799 *error = error_str.str();
800 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600801 }
802 return true;
803}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600804
Tobin Ehlis300888c2016-05-18 13:43:26 -0600805void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
806 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600807 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600808}
809
Tobin Ehlis300888c2016-05-18 13:43:26 -0600810void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600811 if (!immutable_) {
812 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600813 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600814 }
815 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600816}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600817
Tobin Ehlis300888c2016-05-18 13:43:26 -0600818cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
819 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600820 updated = false;
821 descriptor_class = ImageSampler;
822}
823
Tobin Ehlis300888c2016-05-18 13:43:26 -0600824cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
825 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600826 updated = false;
827 descriptor_class = ImageSampler;
828 if (immut) {
829 sampler_ = *immut;
830 immutable_ = true;
831 updated = true;
832 }
833}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600834
Tobin Ehlis300888c2016-05-18 13:43:26 -0600835void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600836 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600837 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600838 sampler_ = image_info.sampler;
839 image_view_ = image_info.imageView;
840 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600841}
842
Tobin Ehlis300888c2016-05-18 13:43:26 -0600843void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600844 if (!immutable_) {
845 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600846 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600847 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600848 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
849 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600850 updated = true;
851 image_view_ = image_view;
852 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600853}
854
Tobin Ehlis300888c2016-05-18 13:43:26 -0600855cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
856 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600857 updated = false;
858 descriptor_class = Image;
859 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
860 storage_ = true;
861};
862
Tobin Ehlis300888c2016-05-18 13:43:26 -0600863void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600864 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600865 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600866 image_view_ = image_info.imageView;
867 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600868}
869
Tobin Ehlis300888c2016-05-18 13:43:26 -0600870void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600871 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
872 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600873 updated = true;
874 image_view_ = image_view;
875 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600876}
877
Tobin Ehlis300888c2016-05-18 13:43:26 -0600878cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
879 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600880 updated = false;
881 descriptor_class = GeneralBuffer;
882 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
883 dynamic_ = true;
884 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
885 storage_ = true;
886 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
887 dynamic_ = true;
888 storage_ = true;
889 }
890}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600891void cvdescriptorset::BufferDescriptor::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 &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600894 buffer_ = buffer_info.buffer;
895 offset_ = buffer_info.offset;
896 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600897}
898
Tobin Ehlis300888c2016-05-18 13:43:26 -0600899void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
900 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600901 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600902 buffer_ = buff_desc->buffer_;
903 offset_ = buff_desc->offset_;
904 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600905}
906
Tobin Ehlis300888c2016-05-18 13:43:26 -0600907cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600908 updated = false;
909 descriptor_class = TexelBuffer;
910 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
911 storage_ = true;
912};
Tobin Ehlis56a30942016-05-19 08:00:00 -0600913
Tobin Ehlis300888c2016-05-18 13:43:26 -0600914void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600915 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600916 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600917}
918
Tobin Ehlis300888c2016-05-18 13:43:26 -0600919void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
920 updated = true;
921 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
922}
923// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
924// sets, and then calls their respective Validate[Write|Copy]Update functions.
925// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
926// be skipped, then true is returned.
927// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600928bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
929 const core_validation::layer_data *dev_data, uint32_t write_count,
930 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
931 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600932 bool skip_call = false;
933 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600934 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600935 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600936 auto set_node = core_validation::getSetNode(dev_data, dest_set);
937 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600938 skip_call |=
939 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 -0600940 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600941 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
942 reinterpret_cast<uint64_t &>(dest_set));
943 } else {
944 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600945 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600946 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 -0600947 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_WRITE_UPDATE, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600948 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
949 " with error: %s",
950 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
951 }
952 }
953 }
954 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600955 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600956 auto dst_set = p_cds[i].dstSet;
957 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600958 auto src_node = core_validation::getSetNode(dev_data, src_set);
959 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
960 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600961 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 -0600962 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600963 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
964 " that has not been allocated.",
965 reinterpret_cast<uint64_t &>(src_set));
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600966 } else if (!dst_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600967 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 -0600968 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600969 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
970 " that has not been allocated.",
971 reinterpret_cast<uint64_t &>(dst_set));
972 } else {
973 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600974 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600975 skip_call |=
976 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 -0600977 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_COPY_UPDATE, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600978 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
979 " to Descriptor Set 0x%" PRIx64 " with error: %s",
980 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
981 }
982 }
983 }
984 return skip_call;
985}
986// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
987// sets, and then calls their respective Perform[Write|Copy]Update functions.
988// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
989// with the same set of updates.
990// This is split from the validate code to allow validation prior to calling down the chain, and then update after
991// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600992void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
993 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
994 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600995 // Write updates first
996 uint32_t i = 0;
997 for (i = 0; i < write_count; ++i) {
998 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600999 auto set_node = core_validation::getSetNode(dev_data, dest_set);
1000 if (set_node) {
1001 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001002 }
1003 }
1004 // Now copy updates
1005 for (i = 0; i < copy_count; ++i) {
1006 auto dst_set = p_cds[i].dstSet;
1007 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001008 auto src_node = core_validation::getSetNode(dev_data, src_set);
1009 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
1010 if (src_node && dst_node) {
1011 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001012 }
1013 }
1014}
1015// Validate the state for a given write update but don't actually perform the update
1016// If an error would occur for this update, return false and fill in details in error_msg string
1017bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
1018 std::string *error_msg) {
1019 // Verify idle ds
1020 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001021 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001022 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
1023 << " that is in use by a command buffer.";
1024 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001025 return false;
1026 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001027 // Verify dst binding exists
1028 if (!p_layout_->HasBinding(update->dstBinding)) {
1029 std::stringstream error_str;
1030 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
1031 *error_msg = error_str.str();
1032 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001033 }
1034 // We know that binding is valid, verify update and do update on each descriptor
1035 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
1036 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
1037 if (type != update->descriptorType) {
1038 std::stringstream error_str;
1039 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1040 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1041 *error_msg = error_str.str();
1042 return false;
1043 }
1044 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
1045 std::stringstream error_str;
1046 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
1047 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
1048 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
1049 << " combined with update array element offset of " << update->dstArrayElement
1050 << " oversteps the size of this descriptor set.";
1051 *error_msg = error_str.str();
1052 return false;
1053 }
1054 // Verify consecutive bindings match (if needed)
1055 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
1056 set_, error_msg))
1057 return false;
1058 // Update is within bounds and consistent so last step is to validate update contents
1059 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
1060 std::stringstream error_str;
1061 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1062 << " failed with error message: " << error_msg->c_str();
1063 *error_msg = error_str.str();
1064 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001065 }
1066 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001067 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001068}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001069// For the given buffer, verify that its creation parameters are appropriate for the given type
1070// If there's an error, update the error string with details and return false, else return true
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001071bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_NODE const *buffer_node, VkDescriptorType type,
1072 std::string *error) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001073 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001074 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001075 std::string error_usage_bit;
1076 switch (type) {
1077 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1078 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
1079 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1080 }
1081 break;
1082 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1083 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1084 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1085 }
1086 break;
1087 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1088 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1089 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1090 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1091 }
1092 break;
1093 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1094 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1095 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1096 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1097 }
1098 break;
1099 default:
1100 break;
1101 }
1102 if (!error_usage_bit.empty()) {
1103 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001104 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1105 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1106 << error_usage_bit << " set.";
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001107 *error = error_str.str();
1108 return false;
1109 }
1110 return true;
1111}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001112// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1113// 1. buffer is valid
1114// 2. buffer was created with correct usage flags
1115// 3. offset is less than buffer size
1116// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
1117// If there's an error, update the error string with details and return false, else return true
1118bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
1119 std::string *error) const {
1120 // First make sure that buffer is valid
1121 auto buffer_node = getBufferNode(device_data_, buffer_info->buffer);
1122 if (!buffer_node) {
1123 std::stringstream error_str;
1124 error_str << "Invalid VkBuffer: " << buffer_info->buffer;
1125 *error = error_str.str();
1126 return false;
1127 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001128 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()"))
1129 return false;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001130 // Verify usage bits
1131 if (!ValidateBufferUsage(buffer_node, type, error)) {
1132 // error will have been updated by ValidateBufferUsage()
1133 return false;
1134 }
1135 // offset must be less than buffer size
1136 if (buffer_info->offset > buffer_node->createInfo.size) {
1137 std::stringstream error_str;
1138 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than buffer " << buffer_node->buffer
1139 << " size of " << buffer_node->createInfo.size;
1140 *error = error_str.str();
1141 return false;
1142 }
1143 if (buffer_info->range != VK_WHOLE_SIZE) {
1144 // Range must be VK_WHOLE_SIZE or > 0
1145 if (!buffer_info->range) {
1146 std::stringstream error_str;
1147 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
1148 *error = error_str.str();
1149 return false;
1150 }
1151 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1152 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
1153 std::stringstream error_str;
1154 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1155 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
1156 *error = error_str.str();
1157 return false;
1158 }
1159 }
1160 return true;
1161}
1162
Tobin Ehlis300888c2016-05-18 13:43:26 -06001163// Verify that the contents of the update are ok, but don't perform actual update
1164bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
1165 std::string *error) const {
1166 switch (update->descriptorType) {
1167 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1168 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1169 // Validate image
1170 auto image_view = update->pImageInfo[di].imageView;
1171 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis4e380592016-06-02 12:41:47 -06001172 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001173 std::stringstream error_str;
1174 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1175 *error = error_str.str();
1176 return false;
1177 }
1178 }
1179 // Intentional fall-through to validate sampler
1180 }
1181 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1182 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1183 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001184 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001185 std::stringstream error_str;
1186 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1187 << update->pImageInfo[di].sampler << ".";
1188 *error = error_str.str();
1189 return false;
1190 }
1191 } else {
1192 // TODO : Warn here
1193 }
1194 }
1195 break;
1196 }
1197 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1198 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1199 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1200 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1201 auto image_view = update->pImageInfo[di].imageView;
1202 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis4e380592016-06-02 12:41:47 -06001203 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001204 std::stringstream error_str;
1205 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1206 *error = error_str.str();
1207 return false;
1208 }
1209 }
1210 break;
1211 }
1212 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1213 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1214 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1215 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis424859c2016-06-02 09:43:11 -06001216 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1217 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001218 std::stringstream error_str;
1219 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1220 *error = error_str.str();
1221 return false;
1222 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001223 auto buffer = bv_info->buffer;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001224 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), update->descriptorType, error)) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001225 std::stringstream error_str;
1226 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str();
1227 *error = error_str.str();
1228 return false;
1229 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001230 }
1231 break;
1232 }
1233 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1234 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1235 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1236 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1237 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001238 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001239 std::stringstream error_str;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001240 error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001241 *error = error_str.str();
1242 return false;
1243 }
1244 }
1245 break;
1246 }
1247 default:
1248 assert(0); // We've already verified update type so should never get here
1249 break;
1250 }
1251 // All checks passed so update contents are good
1252 return true;
1253}
1254// Verify that the contents of the update are ok, but don't perform actual update
1255bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001256 VkDescriptorType type, uint32_t index, std::string *error) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001257 switch (src_set->descriptors_[index]->descriptor_class) {
1258 case PlainSampler: {
1259 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1260 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1261 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001262 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001263 std::stringstream error_str;
1264 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1265 *error = error_str.str();
1266 return false;
1267 }
1268 } else {
1269 // TODO : Warn here
1270 }
1271 }
1272 break;
1273 }
1274 case ImageSampler: {
1275 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1276 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1277 // First validate sampler
1278 if (!img_samp_desc->IsImmutableSampler()) {
1279 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001280 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001281 std::stringstream error_str;
1282 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1283 *error = error_str.str();
1284 return false;
1285 }
1286 } else {
1287 // TODO : Warn here
1288 }
1289 // Validate image
1290 auto image_view = img_samp_desc->GetImageView();
1291 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlis4e380592016-06-02 12:41:47 -06001292 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001293 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001294 error_str << "Attempted copy update to combined image sampler 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 }
1300 case Image: {
1301 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1302 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1303 auto image_view = img_desc->GetImageView();
1304 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlis4e380592016-06-02 12:41:47 -06001305 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001306 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001307 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001308 *error = error_str.str();
1309 return false;
1310 }
1311 }
1312 break;
1313 }
1314 case TexelBuffer: {
1315 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1316 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -06001317 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1318 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001319 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001320 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1321 *error = error_str.str();
1322 return false;
1323 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001324 auto buffer = bv_info->buffer;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001325 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), type, error)) {
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001326 std::stringstream error_str;
1327 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001328 *error = error_str.str();
1329 return false;
1330 }
1331 }
1332 break;
1333 }
1334 case GeneralBuffer: {
1335 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1336 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001337 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), type, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001338 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001339 error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001340 *error = error_str.str();
1341 return false;
1342 }
1343 }
1344 break;
1345 }
1346 default:
1347 assert(0); // We've already verified update type so should never get here
1348 break;
1349 }
1350 // All checks passed so update contents are good
1351 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001352}
Tobin Ehlisee471462016-05-26 11:21:59 -06001353// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlis815e8132016-06-02 13:02:17 -06001354bool cvdescriptorset::ValidateAllocateDescriptorSets(const debug_report_data *report_data,
1355 const VkDescriptorSetAllocateInfo *p_alloc_info,
1356 const core_validation::layer_data *dev_data,
1357 AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001358 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001359
1360 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001361 auto layout = getDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1362 if (!layout) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001363 skip_call |=
1364 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1365 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1366 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1367 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001368 } else {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001369 ds_data->layout_nodes[i] = layout;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001370 // Count total descriptors required per type
Tobin Ehlis815e8132016-06-02 13:02:17 -06001371 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1372 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001373 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1374 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1375 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001376 }
1377 }
Tobin Ehlis815e8132016-06-02 13:02:17 -06001378 auto pool_node = getPoolNode(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001379 // Track number of descriptorSets allowable in this pool
1380 if (pool_node->availableSets < p_alloc_info->descriptorSetCount) {
1381 skip_call |= log_msg(
1382 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1383 reinterpret_cast<uint64_t &>(pool_node->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY, "DS",
1384 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64 ". This pool only has %d descriptorSets remaining.",
1385 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_node->pool), pool_node->availableSets);
1386 }
1387 // Determine whether descriptor counts are satisfiable
1388 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1389 if (ds_data->required_descriptors_by_type[i] > pool_node->availableDescriptorTypeCount[i]) {
1390 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1391 reinterpret_cast<const uint64_t &>(pool_node->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1392 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1393 ". This pool only has %d descriptors of this type remaining.",
1394 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
1395 reinterpret_cast<uint64_t &>(pool_node->pool), pool_node->availableDescriptorTypeCount[i]);
Tobin Ehlisee471462016-05-26 11:21:59 -06001396 }
1397 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001398
Tobin Ehlisee471462016-05-26 11:21:59 -06001399 return skip_call;
1400}
1401// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001402void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1403 const VkDescriptorSet *descriptor_sets,
1404 const AllocateDescriptorSetsData *ds_data,
1405 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map,
1406 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
1407 const core_validation::layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001408 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001409 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001410 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001411 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1412 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1413 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001414 /* Create tracking object for each descriptor set; insert into
1415 * global map and the pool's set.
1416 */
1417 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis4e380592016-06-02 12:41:47 -06001418 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], ds_data->layout_nodes[i], dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001419
1420 pool_state->sets.insert(new_ds);
1421 new_ds->in_use.store(0);
1422 (*set_map)[descriptor_sets[i]] = new_ds;
1423 }
1424}