blob: 9039a1229ac1c1ad3e6c38a16f81ddef419912a1 [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) {
53 dynamic_descriptor_count_++;
54 }
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
134 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600135}
136// For the given binding, return end index
137uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
138 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600139 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
140 if (btgei_itr != binding_to_global_end_index_map_.end()) {
141 return btgei_itr->second;
142 }
143 // In error case max uint32_t so index is out of bounds to break ASAP
144 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600145}
146// For given binding, return ptr to ImmutableSampler array
147VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
148 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600149 const auto &bi_itr = binding_to_index_map_.find(binding);
150 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600151 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600152 }
153 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600154}
155// For given index, return ptr to ImmutableSampler array
156VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
157 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600158 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600159}
160// If our layout is compatible with rh_ds_layout, return true,
161// else return false and fill in error_msg will description of what causes incompatibility
162bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
163 // Trivial case
164 if (layout_ == rh_ds_layout->GetDescriptorSetLayout())
165 return true;
166 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
167 std::stringstream error_str;
168 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
169 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
170 *error_msg = error_str.str();
171 return false; // trivial fail case
172 }
173 // Descriptor counts match so need to go through bindings one-by-one
174 // and verify that type and stageFlags match
175 for (auto binding : bindings_) {
176 // TODO : Do we also need to check immutable samplers?
177 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600178 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600179 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600180 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
181 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600182 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600183 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600184 *error_msg = error_str.str();
185 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600186 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600187 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600188 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
189 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600190 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600191 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600192 *error_msg = error_str.str();
193 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600194 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600195 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600196 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
197 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600198 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600199 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600200 *error_msg = error_str.str();
201 return false;
202 }
203 }
204 return true;
205}
206
207bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
208 if (!binding_to_index_map_.count(binding + 1))
209 return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600210 auto const &bi_itr = binding_to_index_map_.find(binding);
211 if (bi_itr != binding_to_index_map_.end()) {
212 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
213 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600214 auto type = bindings_[bi_itr->second].descriptorType;
215 auto stage_flags = bindings_[bi_itr->second].stageFlags;
216 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
217 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
218 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
219 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600220 return false;
221 }
222 return true;
223 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600224 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600225 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600226}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600227// Starting at offset descriptor of given binding, parse over update_count
228// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
229// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
230// If so, return true. If not, fill in error_msg and return false
231bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
232 const char *type, const VkDescriptorSet set,
233 std::string *error_msg) const {
234 // Verify consecutive bindings match (if needed)
235 auto orig_binding = current_binding;
236 // Track count of descriptors in the current_bindings that are remaining to be updated
237 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
238 // First, it's legal to offset beyond your own binding so handle that case
239 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
240 while (offset >= binding_remaining) {
241 // Advance to next binding, decrement offset by binding size
242 offset -= binding_remaining;
243 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
244 }
245 binding_remaining -= offset;
246 while (update_count > binding_remaining) { // While our updates overstep current binding
247 // Verify next consecutive binding matches type, stage flags & immutable sampler use
248 if (!IsNextBindingConsistent(current_binding++)) {
249 std::stringstream error_str;
250 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
251 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
252 "next binding is not consistent with current binding so this update is invalid.";
253 *error_msg = error_str.str();
254 return false;
255 }
256 // For sake of this check consider the bindings updated and grab count for next binding
257 update_count -= binding_remaining;
258 binding_remaining = GetDescriptorCountFromBinding(current_binding);
259 }
260 return true;
261}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600262
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600263cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
264 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
265
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600266cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const DescriptorSetLayout *layout,
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600267 const core_validation::layer_data *dev_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600268 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
269 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600270 : some_update_(false), set_(set), p_layout_(layout), device_data_(dev_data), image_to_swapchain_map_(image_to_swapchain_map),
271 swapchain_map_(swapchain_map) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600272 // Foreach binding, create default descriptors of given type
273 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
274 auto type = p_layout_->GetTypeFromIndex(i);
275 switch (type) {
276 case VK_DESCRIPTOR_TYPE_SAMPLER: {
277 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
278 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
279 if (immut_sampler)
Chris Forbescb621ea2016-05-30 11:47:31 +1200280 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600281 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200282 descriptors_.emplace_back(new SamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600283 }
284 break;
285 }
286 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
287 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
288 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
289 if (immut)
Chris Forbescb621ea2016-05-30 11:47:31 +1200290 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600291 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200292 descriptors_.emplace_back(new ImageSamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600293 }
294 break;
295 }
296 // ImageDescriptors
297 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
298 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
299 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
300 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200301 descriptors_.emplace_back(new ImageDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600302 break;
303 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
304 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
305 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200306 descriptors_.emplace_back(new TexelDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600307 break;
308 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
309 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
310 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
311 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
312 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200313 descriptors_.emplace_back(new BufferDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600314 break;
315 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600316 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600317 break;
318 }
319 }
320}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600321
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600322cvdescriptorset::DescriptorSet::~DescriptorSet() {
323 InvalidateBoundCmdBuffers();
324 // Remove link to any cmd buffers
325 for (auto cb : bound_cmd_buffers_) {
326 for (uint32_t i=0; i<VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
327 cb->lastBound[i].uniqueBoundSets.erase(this);
328 }
329 }
330}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600331// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
332bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
333 return layout->IsCompatible(p_layout_, error);
334}
335// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
336// This includes validating that all descriptors in the given bindings are updated,
337// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
338// Return true if state is acceptable, or false and write an error message into error string
339bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
340 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
iostrows5eb97652016-06-02 15:56:26 +0200341 auto dyn_offset_index = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600342 for (auto binding : bindings) {
343 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600344 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600345 // Nothing to do for strictly immutable sampler
346 } else {
347 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600348 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600349 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600350 std::stringstream error_str;
351 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
352 << " is being used in draw but has not been updated.";
353 *error = error_str.str();
354 return false;
355 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600356 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600357 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600358 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600359 auto buffer_node = getBufferNode(device_data_, buffer);
360 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600361 std::stringstream error_str;
362 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
363 << " references invalid buffer " << buffer << ".";
364 *error = error_str.str();
365 return false;
366 } else {
Tobin Ehlis997b2582016-06-02 08:43:37 -0600367 auto mem_entry = getMemObjInfo(device_data_, buffer_node->mem);
368 if (!mem_entry) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600369 std::stringstream error_str;
370 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600371 << " uses buffer " << buffer << " that references invalid memory " << buffer_node->mem
372 << ".";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600373 *error = error_str.str();
374 return false;
375 }
376 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600377 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600378 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600379 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600380 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
381 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600382 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
383 if (VK_WHOLE_SIZE == range) {
384 if ((dyn_offset + desc_offset) > buffer_size) {
385 std::stringstream error_str;
386 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
387 << " uses buffer " << buffer
388 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
389 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
390 << buffer_size << ".";
391 *error = error_str.str();
392 return false;
393 }
394 } else {
395 if ((dyn_offset + desc_offset + range) > buffer_size) {
396 std::stringstream error_str;
397 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
398 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
399 << " combined with offset " << desc_offset << " and range " << range
400 << " that oversteps the buffer size of " << buffer_size << ".";
401 *error = error_str.str();
402 return false;
403 }
404 }
405 }
406 }
407 }
408 }
409 }
410 }
411 return true;
412}
413// For given bindings, place any update buffers or images into the passed-in unordered_sets
414uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
415 std::unordered_set<VkBuffer> *buffer_set,
416 std::unordered_set<VkImageView> *image_set) const {
417 auto num_updates = 0;
418 for (auto binding : bindings) {
419 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600420 if (descriptors_[start_idx]->IsStorage()) {
421 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600422 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600423 if (descriptors_[start_idx + i]->updated) {
424 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600425 num_updates++;
426 }
427 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600428 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600429 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600430 if (descriptors_[start_idx + i]->updated) {
431 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -0600432 auto bv_info = getBufferViewInfo(device_data_, bufferview);
433 if (bv_info) {
434 buffer_set->insert(bv_info->buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600435 num_updates++;
436 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600437 }
438 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600439 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600440 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600441 if (descriptors_[start_idx + i]->updated) {
442 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600443 num_updates++;
444 }
445 }
446 }
447 }
448 }
449 return num_updates;
450}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600451// Set is being deleted or updates so invalidate all bound cmd buffers
452void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
453 for (auto cb_node : bound_cmd_buffers_) {
454 cb_node->state = CB_INVALID;
455 }
456}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600457// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600458void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600459 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
460 // perform update
461 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
462 descriptors_[start_idx + di]->WriteUpdate(update, di);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600463 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600464 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600465 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600466
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600467 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600468}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600469// Validate Copy update
470bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
471 const DescriptorSet *src_set, std::string *error) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600472 // Verify idle ds
473 if (in_use.load()) {
474 std::stringstream error_str;
475 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
476 << " that is in use by a command buffer.";
477 *error = error_str.str();
478 return false;
479 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600480 if (!p_layout_->HasBinding(update->dstBinding)) {
481 std::stringstream error_str;
482 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
483 *error = error_str.str();
484 return false;
485 }
486 if (!src_set->HasBinding(update->srcBinding)) {
487 std::stringstream error_str;
488 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
489 *error = error_str.str();
490 return false;
491 }
492 // src & dst set bindings are valid
493 // Check bounds of src & dst
494 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
495 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
496 // SRC update out of bounds
497 std::stringstream error_str;
498 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
499 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
500 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
501 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
502 *error = error_str.str();
503 return false;
504 }
505 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
506 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
507 // DST update out of bounds
508 std::stringstream error_str;
509 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
510 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
511 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
512 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
513 *error = error_str.str();
514 return false;
515 }
516 // Check that types match
517 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
518 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
519 if (src_type != dst_type) {
520 std::stringstream error_str;
521 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
522 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
523 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
524 *error = error_str.str();
525 return false;
526 }
527 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600528 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
529 "copy update from", src_set->GetSet(), error)) ||
530 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
531 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600532 return false;
533 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600534 // First make sure source descriptors are updated
535 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
536 if (!src_set->descriptors_[src_start_idx + i]) {
537 std::stringstream error_str;
538 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
539 << update->srcArrayElement + i << " has not been updated.";
540 *error = error_str.str();
541 return false;
542 }
543 }
544 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600545 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600546 return false;
547
548 // All checks passed so update is good
549 return true;
550}
551// Perform Copy update
552void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600553 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
554 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600555 // Update parameters all look good so perform update
556 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600557 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600558 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600559 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600560 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600561
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600562 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600563}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600564
Tobin Ehlis300888c2016-05-18 13:43:26 -0600565cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600566 updated = false;
567 descriptor_class = PlainSampler;
568};
569
Tobin Ehlis300888c2016-05-18 13:43:26 -0600570cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600571 updated = false;
572 descriptor_class = PlainSampler;
573 if (immut) {
574 sampler_ = *immut;
575 immutable_ = true;
576 updated = true;
577 }
578}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600579// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
580bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
581 return (getSamplerNode(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600582}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600583
Tobin Ehlis554bf382016-05-24 11:14:43 -0600584bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600585 const core_validation::layer_data *dev_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600586 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
587 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
588 std::string *error) {
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600589 auto iv_data = getImageViewData(dev_data, image_view);
590 if (!iv_data) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600591 std::stringstream error_str;
592 error_str << "Invalid VkImageView: " << image_view;
593 *error = error_str.str();
594 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600595 }
596 // Validate that imageLayout is compatible with aspect_mask and image format
597 // and validate that image usage bits are correct for given usage
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600598 VkImageAspectFlags aspect_mask = iv_data->subresourceRange.aspectMask;
599 VkImage image = iv_data->image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600600 VkFormat format = VK_FORMAT_MAX_ENUM;
601 VkImageUsageFlags usage = 0;
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600602 auto image_node = getImageNode(dev_data, image);
603 if (image_node) {
604 format = image_node->createInfo.format;
605 usage = image_node->createInfo.usage;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600606 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600607 // Also need to check the swapchains.
608 auto swapchain_pair = image_to_swapchain_map->find(image);
609 if (swapchain_pair != image_to_swapchain_map->end()) {
610 VkSwapchainKHR swapchain = swapchain_pair->second;
611 auto swapchain_pair = swapchain_map->find(swapchain);
612 if (swapchain_pair != swapchain_map->end()) {
613 format = swapchain_pair->second->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600614 }
615 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600616 }
617 // First validate that format and layout are compatible
618 if (format == VK_FORMAT_MAX_ENUM) {
619 std::stringstream error_str;
620 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
621 *error = error_str.str();
622 return false;
623 }
624 bool ds = vk_format_is_depth_or_stencil(format);
625 switch (image_layout) {
626 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
627 // Only Color bit must be set
628 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600629 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600630 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
631 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis554bf382016-05-24 11:14:43 -0600632 *error = error_str.str();
633 return false;
634 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600635 // format must NOT be DS
636 if (ds) {
637 std::stringstream error_str;
638 error_str << "ImageView (" << image_view
639 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
640 << string_VkFormat(format) << " which is not a color format.";
641 *error = error_str.str();
642 return false;
643 }
644 break;
645 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
646 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
647 // Depth or stencil bit must be set, but both must NOT be set
648 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
649 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
650 // both must NOT be set
651 std::stringstream error_str;
652 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
653 *error = error_str.str();
654 return false;
655 }
656 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
657 // Neither were set
658 std::stringstream error_str;
659 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
660 << " but does not have STENCIL or DEPTH aspects set";
661 *error = error_str.str();
662 return false;
663 }
664 // format must be DS
665 if (!ds) {
666 std::stringstream error_str;
667 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
668 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
669 *error = error_str.str();
670 return false;
671 }
672 break;
673 default:
674 // anything to check for other layouts?
675 break;
676 }
677 // Now validate that usage flags are correctly set for given type of update
678 std::string error_usage_bit;
679 switch (type) {
680 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
681 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
682 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
683 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
684 }
685 break;
686 }
687 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
688 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
689 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
690 }
691 break;
692 }
693 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
694 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
695 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
696 }
697 break;
698 }
699 default:
700 break;
701 }
702 if (!error_usage_bit.empty()) {
703 std::stringstream error_str;
704 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
705 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
706 << error_usage_bit << " set.";
707 *error = error_str.str();
708 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600709 }
710 return true;
711}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600712
Tobin Ehlis300888c2016-05-18 13:43:26 -0600713void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
714 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600715 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600716}
717
Tobin Ehlis300888c2016-05-18 13:43:26 -0600718void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600719 if (!immutable_) {
720 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600721 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600722 }
723 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600724}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600725
Tobin Ehlis300888c2016-05-18 13:43:26 -0600726cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
727 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600728 updated = false;
729 descriptor_class = ImageSampler;
730}
731
Tobin Ehlis300888c2016-05-18 13:43:26 -0600732cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
733 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600734 updated = false;
735 descriptor_class = ImageSampler;
736 if (immut) {
737 sampler_ = *immut;
738 immutable_ = true;
739 updated = true;
740 }
741}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600742
Tobin Ehlis300888c2016-05-18 13:43:26 -0600743void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600744 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600745 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600746 sampler_ = image_info.sampler;
747 image_view_ = image_info.imageView;
748 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600749}
750
Tobin Ehlis300888c2016-05-18 13:43:26 -0600751void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600752 if (!immutable_) {
753 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600754 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600755 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600756 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
757 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600758 updated = true;
759 image_view_ = image_view;
760 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600761}
762
Tobin Ehlis300888c2016-05-18 13:43:26 -0600763cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
764 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600765 updated = false;
766 descriptor_class = Image;
767 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
768 storage_ = true;
769};
770
Tobin Ehlis300888c2016-05-18 13:43:26 -0600771void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600772 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600773 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600774 image_view_ = image_info.imageView;
775 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600776}
777
Tobin Ehlis300888c2016-05-18 13:43:26 -0600778void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600779 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
780 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600781 updated = true;
782 image_view_ = image_view;
783 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600784}
785
Tobin Ehlis300888c2016-05-18 13:43:26 -0600786cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
787 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600788 updated = false;
789 descriptor_class = GeneralBuffer;
790 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
791 dynamic_ = true;
792 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
793 storage_ = true;
794 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
795 dynamic_ = true;
796 storage_ = true;
797 }
798}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600799void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600800 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600801 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600802 buffer_ = buffer_info.buffer;
803 offset_ = buffer_info.offset;
804 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600805}
806
Tobin Ehlis300888c2016-05-18 13:43:26 -0600807void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
808 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600809 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600810 buffer_ = buff_desc->buffer_;
811 offset_ = buff_desc->offset_;
812 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600813}
814
Tobin Ehlis300888c2016-05-18 13:43:26 -0600815cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600816 updated = false;
817 descriptor_class = TexelBuffer;
818 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
819 storage_ = true;
820};
Tobin Ehlis56a30942016-05-19 08:00:00 -0600821
Tobin Ehlis300888c2016-05-18 13:43:26 -0600822void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600823 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600824 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600825}
826
Tobin Ehlis300888c2016-05-18 13:43:26 -0600827void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
828 updated = true;
829 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
830}
831// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
832// sets, and then calls their respective Validate[Write|Copy]Update functions.
833// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
834// be skipped, then true is returned.
835// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600836bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
837 const core_validation::layer_data *dev_data, uint32_t write_count,
838 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
839 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600840 bool skip_call = false;
841 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600842 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600843 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600844 auto set_node = core_validation::getSetNode(dev_data, dest_set);
845 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600846 skip_call |=
847 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 -0600848 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600849 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
850 reinterpret_cast<uint64_t &>(dest_set));
851 } else {
852 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600853 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600854 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
855 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
856 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
857 " with error: %s",
858 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
859 }
860 }
861 }
862 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600863 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600864 auto dst_set = p_cds[i].dstSet;
865 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600866 auto src_node = core_validation::getSetNode(dev_data, src_set);
867 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
868 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600869 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 -0600870 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600871 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
872 " that has not been allocated.",
873 reinterpret_cast<uint64_t &>(src_set));
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600874 } else if (!dst_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600875 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 -0600876 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600877 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
878 " that has not been allocated.",
879 reinterpret_cast<uint64_t &>(dst_set));
880 } else {
881 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600882 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600883 skip_call |=
884 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
885 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
886 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
887 " to Descriptor Set 0x%" PRIx64 " with error: %s",
888 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
889 }
890 }
891 }
892 return skip_call;
893}
894// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
895// sets, and then calls their respective Perform[Write|Copy]Update functions.
896// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
897// with the same set of updates.
898// This is split from the validate code to allow validation prior to calling down the chain, and then update after
899// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600900void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
901 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
902 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600903 // Write updates first
904 uint32_t i = 0;
905 for (i = 0; i < write_count; ++i) {
906 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600907 auto set_node = core_validation::getSetNode(dev_data, dest_set);
908 if (set_node) {
909 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600910 }
911 }
912 // Now copy updates
913 for (i = 0; i < copy_count; ++i) {
914 auto dst_set = p_cds[i].dstSet;
915 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600916 auto src_node = core_validation::getSetNode(dev_data, src_set);
917 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
918 if (src_node && dst_node) {
919 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600920 }
921 }
922}
923// Validate the state for a given write update but don't actually perform the update
924// If an error would occur for this update, return false and fill in details in error_msg string
925bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
926 std::string *error_msg) {
927 // Verify idle ds
928 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600929 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600930 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
931 << " that is in use by a command buffer.";
932 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600933 return false;
934 }
Tobin Ehlis300888c2016-05-18 13:43:26 -0600935 // Verify dst binding exists
936 if (!p_layout_->HasBinding(update->dstBinding)) {
937 std::stringstream error_str;
938 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
939 *error_msg = error_str.str();
940 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -0600941 }
942 // We know that binding is valid, verify update and do update on each descriptor
943 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
944 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
945 if (type != update->descriptorType) {
946 std::stringstream error_str;
947 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
948 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
949 *error_msg = error_str.str();
950 return false;
951 }
952 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
953 std::stringstream error_str;
954 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
955 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
956 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
957 << " combined with update array element offset of " << update->dstArrayElement
958 << " oversteps the size of this descriptor set.";
959 *error_msg = error_str.str();
960 return false;
961 }
962 // Verify consecutive bindings match (if needed)
963 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
964 set_, error_msg))
965 return false;
966 // Update is within bounds and consistent so last step is to validate update contents
967 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
968 std::stringstream error_str;
969 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
970 << " failed with error message: " << error_msg->c_str();
971 *error_msg = error_str.str();
972 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600973 }
974 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600975 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600976}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600977// For the given buffer, verify that its creation parameters are appropriate for the given type
978// If there's an error, update the error string with details and return false, else return true
979bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkBuffer buffer, VkDescriptorType type, std::string *error) const {
980 // First make sure that buffer is valid
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600981 auto buffer_node = getBufferNode(device_data_, buffer);
982 if (!buffer_node) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600983 std::stringstream error_str;
984 error_str << "Invalid VkBuffer: " << buffer;
985 *error = error_str.str();
986 return false;
987 }
988 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600989 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600990 std::string error_usage_bit;
991 switch (type) {
992 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
993 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
994 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
995 }
996 break;
997 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
998 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
999 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1000 }
1001 break;
1002 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1003 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1004 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1005 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1006 }
1007 break;
1008 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1009 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1010 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1011 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1012 }
1013 break;
1014 default:
1015 break;
1016 }
1017 if (!error_usage_bit.empty()) {
1018 std::stringstream error_str;
1019 error_str << "Buffer (" << buffer << ") with usage mask 0x" << usage << " being used for a descriptor update of type "
1020 << string_VkDescriptorType(type) << " does not have " << error_usage_bit << " set.";
1021 *error = error_str.str();
1022 return false;
1023 }
1024 return true;
1025}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001026// Verify that the contents of the update are ok, but don't perform actual update
1027bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
1028 std::string *error) const {
1029 switch (update->descriptorType) {
1030 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1031 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1032 // Validate image
1033 auto image_view = update->pImageInfo[di].imageView;
1034 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001035 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, image_to_swapchain_map_,
1036 swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001037 std::stringstream error_str;
1038 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1039 *error = error_str.str();
1040 return false;
1041 }
1042 }
1043 // Intentional fall-through to validate sampler
1044 }
1045 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1046 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1047 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001048 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001049 std::stringstream error_str;
1050 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1051 << update->pImageInfo[di].sampler << ".";
1052 *error = error_str.str();
1053 return false;
1054 }
1055 } else {
1056 // TODO : Warn here
1057 }
1058 }
1059 break;
1060 }
1061 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1062 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1063 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1064 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1065 auto image_view = update->pImageInfo[di].imageView;
1066 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001067 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, image_to_swapchain_map_,
1068 swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001069 std::stringstream error_str;
1070 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1071 *error = error_str.str();
1072 return false;
1073 }
1074 }
1075 break;
1076 }
1077 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1078 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1079 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1080 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis424859c2016-06-02 09:43:11 -06001081 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1082 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001083 std::stringstream error_str;
1084 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1085 *error = error_str.str();
1086 return false;
1087 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001088 auto buffer = bv_info->buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001089 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
1090 std::stringstream error_str;
1091 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str();
1092 *error = error_str.str();
1093 return false;
1094 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001095 }
1096 break;
1097 }
1098 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1099 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1100 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1101 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1102 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1103 auto buffer = update->pBufferInfo[di].buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001104 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001105 std::stringstream error_str;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001106 error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001107 *error = error_str.str();
1108 return false;
1109 }
1110 }
1111 break;
1112 }
1113 default:
1114 assert(0); // We've already verified update type so should never get here
1115 break;
1116 }
1117 // All checks passed so update contents are good
1118 return true;
1119}
1120// Verify that the contents of the update are ok, but don't perform actual update
1121bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001122 VkDescriptorType type, uint32_t index, std::string *error) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001123 switch (src_set->descriptors_[index]->descriptor_class) {
1124 case PlainSampler: {
1125 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1126 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1127 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001128 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001129 std::stringstream error_str;
1130 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1131 *error = error_str.str();
1132 return false;
1133 }
1134 } else {
1135 // TODO : Warn here
1136 }
1137 }
1138 break;
1139 }
1140 case ImageSampler: {
1141 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1142 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1143 // First validate sampler
1144 if (!img_samp_desc->IsImmutableSampler()) {
1145 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001146 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001147 std::stringstream error_str;
1148 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1149 *error = error_str.str();
1150 return false;
1151 }
1152 } else {
1153 // TODO : Warn here
1154 }
1155 // Validate image
1156 auto image_view = img_samp_desc->GetImageView();
1157 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001158 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, image_to_swapchain_map_, swapchain_map_,
1159 error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001160 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001161 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001162 *error = error_str.str();
1163 return false;
1164 }
1165 }
1166 }
1167 case Image: {
1168 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1169 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1170 auto image_view = img_desc->GetImageView();
1171 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001172 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, image_to_swapchain_map_, swapchain_map_,
1173 error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001174 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001175 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001176 *error = error_str.str();
1177 return false;
1178 }
1179 }
1180 break;
1181 }
1182 case TexelBuffer: {
1183 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1184 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -06001185 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1186 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001187 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001188 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1189 *error = error_str.str();
1190 return false;
1191 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001192 auto buffer = bv_info->buffer;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001193 if (!ValidateBufferUpdate(buffer, type, error)) {
1194 std::stringstream error_str;
1195 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001196 *error = error_str.str();
1197 return false;
1198 }
1199 }
1200 break;
1201 }
1202 case GeneralBuffer: {
1203 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1204 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001205 if (!ValidateBufferUpdate(buffer, type, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001206 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001207 error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001208 *error = error_str.str();
1209 return false;
1210 }
1211 }
1212 break;
1213 }
1214 default:
1215 assert(0); // We've already verified update type so should never get here
1216 break;
1217 }
1218 // All checks passed so update contents are good
1219 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001220}
Tobin Ehlisee471462016-05-26 11:21:59 -06001221// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
1222bool cvdescriptorset::ValidateAllocateDescriptorSets(
1223 const debug_report_data *report_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
1224 const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &set_layout_map,
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001225 const std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> &pool_map, AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001226 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001227
1228 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
1229 auto layout_it = set_layout_map.find(p_alloc_info->pSetLayouts[i]);
1230 if (layout_it == set_layout_map.end()) {
1231 skip_call |=
1232 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1233 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1234 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1235 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001236 } else {
1237 ds_data->layout_nodes[i] = layout_it->second;
1238 // Count total descriptors required per type
1239 for (uint32_t j = 0; j < layout_it->second->GetBindingCount(); ++j) {
1240 const auto &binding_layout = layout_it->second->GetDescriptorSetLayoutBindingPtrFromIndex(j);
1241 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1242 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1243 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001244 }
1245 }
1246 auto pool_it = pool_map.find(p_alloc_info->descriptorPool);
1247 if (pool_it == pool_map.end()) {
1248 skip_call |=
1249 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1250 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool), __LINE__, DRAWSTATE_INVALID_POOL, "DS",
1251 "Unable to find pool node for pool 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1252 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool));
1253 } else { // Make sure pool has all the available descriptors before calling down chain
1254 // Track number of descriptorSets allowable in this pool
1255 if (pool_it->second->availableSets < p_alloc_info->descriptorSetCount) {
1256 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1257 reinterpret_cast<uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1258 "DS", "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1259 ". This pool only has %d descriptorSets remaining.",
1260 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_it->second->pool),
1261 pool_it->second->availableSets);
1262 }
1263 // Determine whether descriptor counts are satisfiable
1264 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001265 if (ds_data->required_descriptors_by_type[i] > pool_it->second->availableDescriptorTypeCount[i]) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001266 skip_call |=
1267 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1268 reinterpret_cast<const uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1269 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1270 ". This pool only has %d descriptors of this type remaining.",
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001271 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Tobin Ehlisee471462016-05-26 11:21:59 -06001272 reinterpret_cast<uint64_t &>(pool_it->second->pool), pool_it->second->availableDescriptorTypeCount[i]);
1273 }
1274 }
1275 }
1276 return skip_call;
1277}
1278// Decrement allocated sets from the pool and insert new sets into set_map
1279void cvdescriptorset::PerformAllocateDescriptorSets(
1280 const VkDescriptorSetAllocateInfo *p_alloc_info, const VkDescriptorSet *descriptor_sets,
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001281 const AllocateDescriptorSetsData *ds_data, std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001282 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map, const core_validation::layer_data *dev_data,
Tobin Ehlisee471462016-05-26 11:21:59 -06001283 const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &layout_map,
Tobin Ehlisee471462016-05-26 11:21:59 -06001284 const std::unordered_map<VkImage, VkSwapchainKHR> &image_to_swapchain_map,
1285 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> &swapchain_map) {
1286 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001287 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001288 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001289 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1290 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1291 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001292 /* Create tracking object for each descriptor set; insert into
1293 * global map and the pool's set.
1294 */
1295 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -06001296 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], ds_data->layout_nodes[i], dev_data,
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -06001297 &image_to_swapchain_map, &swapchain_map);
Tobin Ehlisee471462016-05-26 11:21:59 -06001298
1299 pool_state->sets.insert(new_ds);
1300 new_ds->in_use.store(0);
1301 (*set_map)[descriptor_sets[i]] = new_ds;
1302 }
1303}