blob: e3f6e9d4e417a5d51712af3dd293a7c4b496fd64 [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, IMAGE_NODE> *image_map,
269 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
270 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600271 : some_update_(false), set_(set), p_layout_(layout), device_data_(dev_data), image_map_(image_map),
272 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600273 // Foreach binding, create default descriptors of given type
274 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
275 auto type = p_layout_->GetTypeFromIndex(i);
276 switch (type) {
277 case VK_DESCRIPTOR_TYPE_SAMPLER: {
278 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
279 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
280 if (immut_sampler)
Chris Forbescb621ea2016-05-30 11:47:31 +1200281 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600282 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200283 descriptors_.emplace_back(new SamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600284 }
285 break;
286 }
287 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
288 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
289 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
290 if (immut)
Chris Forbescb621ea2016-05-30 11:47:31 +1200291 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600292 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200293 descriptors_.emplace_back(new ImageSamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600294 }
295 break;
296 }
297 // ImageDescriptors
298 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
299 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
300 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
301 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200302 descriptors_.emplace_back(new ImageDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600303 break;
304 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
305 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
306 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200307 descriptors_.emplace_back(new TexelDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600308 break;
309 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
310 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
311 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
312 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
313 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200314 descriptors_.emplace_back(new BufferDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600315 break;
316 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600317 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600318 break;
319 }
320 }
321}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600322
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600323cvdescriptorset::DescriptorSet::~DescriptorSet() {
324 InvalidateBoundCmdBuffers();
325 // Remove link to any cmd buffers
326 for (auto cb : bound_cmd_buffers_) {
327 for (uint32_t i=0; i<VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
328 cb->lastBound[i].uniqueBoundSets.erase(this);
329 }
330 }
331}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600332// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
333bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
334 return layout->IsCompatible(p_layout_, error);
335}
336// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
337// This includes validating that all descriptors in the given bindings are updated,
338// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
339// Return true if state is acceptable, or false and write an error message into error string
340bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
341 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
iostrows5eb97652016-06-02 15:56:26 +0200342 auto dyn_offset_index = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600343 for (auto binding : bindings) {
344 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600345 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600346 // Nothing to do for strictly immutable sampler
347 } else {
348 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600349 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600350 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600351 std::stringstream error_str;
352 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
353 << " is being used in draw but has not been updated.";
354 *error = error_str.str();
355 return false;
356 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600357 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600358 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600359 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600360 auto buffer_node = getBufferNode(device_data_, buffer);
361 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600362 std::stringstream error_str;
363 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
364 << " references invalid buffer " << buffer << ".";
365 *error = error_str.str();
366 return false;
367 } else {
Tobin Ehlis997b2582016-06-02 08:43:37 -0600368 auto mem_entry = getMemObjInfo(device_data_, buffer_node->mem);
369 if (!mem_entry) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600370 std::stringstream error_str;
371 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600372 << " uses buffer " << buffer << " that references invalid memory " << buffer_node->mem
373 << ".";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600374 *error = error_str.str();
375 return false;
376 }
377 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600378 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600379 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600380 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600381 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
382 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600383 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
384 if (VK_WHOLE_SIZE == range) {
385 if ((dyn_offset + desc_offset) > buffer_size) {
386 std::stringstream error_str;
387 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
388 << " uses buffer " << buffer
389 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
390 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
391 << buffer_size << ".";
392 *error = error_str.str();
393 return false;
394 }
395 } else {
396 if ((dyn_offset + desc_offset + range) > buffer_size) {
397 std::stringstream error_str;
398 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
399 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
400 << " combined with offset " << desc_offset << " and range " << range
401 << " that oversteps the buffer size of " << buffer_size << ".";
402 *error = error_str.str();
403 return false;
404 }
405 }
406 }
407 }
408 }
409 }
410 }
411 }
412 return true;
413}
414// For given bindings, place any update buffers or images into the passed-in unordered_sets
415uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
416 std::unordered_set<VkBuffer> *buffer_set,
417 std::unordered_set<VkImageView> *image_set) const {
418 auto num_updates = 0;
419 for (auto binding : bindings) {
420 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600421 if (descriptors_[start_idx]->IsStorage()) {
422 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600423 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600424 if (descriptors_[start_idx + i]->updated) {
425 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600426 num_updates++;
427 }
428 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600429 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600430 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600431 if (descriptors_[start_idx + i]->updated) {
432 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -0600433 auto bv_info = getBufferViewInfo(device_data_, bufferview);
434 if (bv_info) {
435 buffer_set->insert(bv_info->buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600436 num_updates++;
437 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600438 }
439 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600440 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600441 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600442 if (descriptors_[start_idx + i]->updated) {
443 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600444 num_updates++;
445 }
446 }
447 }
448 }
449 }
450 return num_updates;
451}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600452// Set is being deleted or updates so invalidate all bound cmd buffers
453void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
454 for (auto cb_node : bound_cmd_buffers_) {
455 cb_node->state = CB_INVALID;
456 }
457}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600458// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600459void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600460 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
461 // perform update
462 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
463 descriptors_[start_idx + di]->WriteUpdate(update, di);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600464 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600465 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600466 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600467
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600468 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600469}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600470// Validate Copy update
471bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
472 const DescriptorSet *src_set, std::string *error) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600473 // Verify idle ds
474 if (in_use.load()) {
475 std::stringstream error_str;
476 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
477 << " that is in use by a command buffer.";
478 *error = error_str.str();
479 return false;
480 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600481 if (!p_layout_->HasBinding(update->dstBinding)) {
482 std::stringstream error_str;
483 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
484 *error = error_str.str();
485 return false;
486 }
487 if (!src_set->HasBinding(update->srcBinding)) {
488 std::stringstream error_str;
489 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
490 *error = error_str.str();
491 return false;
492 }
493 // src & dst set bindings are valid
494 // Check bounds of src & dst
495 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
496 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
497 // SRC update out of bounds
498 std::stringstream error_str;
499 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
500 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
501 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
502 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
503 *error = error_str.str();
504 return false;
505 }
506 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
507 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
508 // DST update out of bounds
509 std::stringstream error_str;
510 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
511 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
512 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
513 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
514 *error = error_str.str();
515 return false;
516 }
517 // Check that types match
518 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
519 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
520 if (src_type != dst_type) {
521 std::stringstream error_str;
522 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
523 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
524 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
525 *error = error_str.str();
526 return false;
527 }
528 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600529 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
530 "copy update from", src_set->GetSet(), error)) ||
531 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
532 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600533 return false;
534 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600535 // First make sure source descriptors are updated
536 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
537 if (!src_set->descriptors_[src_start_idx + i]) {
538 std::stringstream error_str;
539 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
540 << update->srcArrayElement + i << " has not been updated.";
541 *error = error_str.str();
542 return false;
543 }
544 }
545 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600546 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600547 return false;
548
549 // All checks passed so update is good
550 return true;
551}
552// Perform Copy update
553void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600554 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
555 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600556 // Update parameters all look good so perform update
557 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600558 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600559 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600560 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600561 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600562
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600563 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600564}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600565
Tobin Ehlis300888c2016-05-18 13:43:26 -0600566cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600567 updated = false;
568 descriptor_class = PlainSampler;
569};
570
Tobin Ehlis300888c2016-05-18 13:43:26 -0600571cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600572 updated = false;
573 descriptor_class = PlainSampler;
574 if (immut) {
575 sampler_ = *immut;
576 immutable_ = true;
577 updated = true;
578 }
579}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600580// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
581bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
582 return (getSamplerNode(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600583}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600584
Tobin Ehlis554bf382016-05-24 11:14:43 -0600585bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600586 const core_validation::layer_data *dev_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600587 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
588 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
589 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
590 std::string *error) {
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600591 auto iv_data = getImageViewData(dev_data, image_view);
592 if (!iv_data) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600593 std::stringstream error_str;
594 error_str << "Invalid VkImageView: " << image_view;
595 *error = error_str.str();
596 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600597 }
598 // Validate that imageLayout is compatible with aspect_mask and image format
599 // and validate that image usage bits are correct for given usage
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600600 VkImageAspectFlags aspect_mask = iv_data->subresourceRange.aspectMask;
601 VkImage image = iv_data->image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600602 VkFormat format = VK_FORMAT_MAX_ENUM;
603 VkImageUsageFlags usage = 0;
604 auto img_pair = image_map->find(image);
605 if (img_pair != image_map->end()) {
606 format = img_pair->second.createInfo.format;
607 usage = img_pair->second.createInfo.usage;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600608 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600609 // Also need to check the swapchains.
610 auto swapchain_pair = image_to_swapchain_map->find(image);
611 if (swapchain_pair != image_to_swapchain_map->end()) {
612 VkSwapchainKHR swapchain = swapchain_pair->second;
613 auto swapchain_pair = swapchain_map->find(swapchain);
614 if (swapchain_pair != swapchain_map->end()) {
615 format = swapchain_pair->second->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600616 }
617 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600618 }
619 // First validate that format and layout are compatible
620 if (format == VK_FORMAT_MAX_ENUM) {
621 std::stringstream error_str;
622 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
623 *error = error_str.str();
624 return false;
625 }
626 bool ds = vk_format_is_depth_or_stencil(format);
627 switch (image_layout) {
628 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
629 // Only Color bit must be set
630 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600631 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600632 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
633 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis554bf382016-05-24 11:14:43 -0600634 *error = error_str.str();
635 return false;
636 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600637 // format must NOT be DS
638 if (ds) {
639 std::stringstream error_str;
640 error_str << "ImageView (" << image_view
641 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
642 << string_VkFormat(format) << " which is not a color format.";
643 *error = error_str.str();
644 return false;
645 }
646 break;
647 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
648 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
649 // Depth or stencil bit must be set, but both must NOT be set
650 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
651 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
652 // both must NOT be set
653 std::stringstream error_str;
654 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
655 *error = error_str.str();
656 return false;
657 }
658 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
659 // Neither were set
660 std::stringstream error_str;
661 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
662 << " but does not have STENCIL or DEPTH aspects set";
663 *error = error_str.str();
664 return false;
665 }
666 // format must be DS
667 if (!ds) {
668 std::stringstream error_str;
669 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
670 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
671 *error = error_str.str();
672 return false;
673 }
674 break;
675 default:
676 // anything to check for other layouts?
677 break;
678 }
679 // Now validate that usage flags are correctly set for given type of update
680 std::string error_usage_bit;
681 switch (type) {
682 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
683 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
684 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
685 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
686 }
687 break;
688 }
689 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
690 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
691 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
692 }
693 break;
694 }
695 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
696 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
697 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
698 }
699 break;
700 }
701 default:
702 break;
703 }
704 if (!error_usage_bit.empty()) {
705 std::stringstream error_str;
706 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
707 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
708 << error_usage_bit << " set.";
709 *error = error_str.str();
710 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600711 }
712 return true;
713}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600714
Tobin Ehlis300888c2016-05-18 13:43:26 -0600715void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
716 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600717 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600718}
719
Tobin Ehlis300888c2016-05-18 13:43:26 -0600720void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600721 if (!immutable_) {
722 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600723 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600724 }
725 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600726}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600727
Tobin Ehlis300888c2016-05-18 13:43:26 -0600728cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
729 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600730 updated = false;
731 descriptor_class = ImageSampler;
732}
733
Tobin Ehlis300888c2016-05-18 13:43:26 -0600734cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
735 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600736 updated = false;
737 descriptor_class = ImageSampler;
738 if (immut) {
739 sampler_ = *immut;
740 immutable_ = true;
741 updated = true;
742 }
743}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600744
Tobin Ehlis300888c2016-05-18 13:43:26 -0600745void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600746 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600747 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600748 sampler_ = image_info.sampler;
749 image_view_ = image_info.imageView;
750 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600751}
752
Tobin Ehlis300888c2016-05-18 13:43:26 -0600753void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600754 if (!immutable_) {
755 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600756 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600757 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600758 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
759 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600760 updated = true;
761 image_view_ = image_view;
762 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600763}
764
Tobin Ehlis300888c2016-05-18 13:43:26 -0600765cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
766 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600767 updated = false;
768 descriptor_class = Image;
769 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
770 storage_ = true;
771};
772
Tobin Ehlis300888c2016-05-18 13:43:26 -0600773void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600774 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600775 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600776 image_view_ = image_info.imageView;
777 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600778}
779
Tobin Ehlis300888c2016-05-18 13:43:26 -0600780void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600781 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
782 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600783 updated = true;
784 image_view_ = image_view;
785 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600786}
787
Tobin Ehlis300888c2016-05-18 13:43:26 -0600788cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
789 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600790 updated = false;
791 descriptor_class = GeneralBuffer;
792 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
793 dynamic_ = true;
794 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
795 storage_ = true;
796 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
797 dynamic_ = true;
798 storage_ = true;
799 }
800}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600801void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600802 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600803 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600804 buffer_ = buffer_info.buffer;
805 offset_ = buffer_info.offset;
806 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600807}
808
Tobin Ehlis300888c2016-05-18 13:43:26 -0600809void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
810 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600811 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600812 buffer_ = buff_desc->buffer_;
813 offset_ = buff_desc->offset_;
814 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600815}
816
Tobin Ehlis300888c2016-05-18 13:43:26 -0600817cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600818 updated = false;
819 descriptor_class = TexelBuffer;
820 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
821 storage_ = true;
822};
Tobin Ehlis56a30942016-05-19 08:00:00 -0600823
Tobin Ehlis300888c2016-05-18 13:43:26 -0600824void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600825 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600826 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600827}
828
Tobin Ehlis300888c2016-05-18 13:43:26 -0600829void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
830 updated = true;
831 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
832}
833// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
834// sets, and then calls their respective Validate[Write|Copy]Update functions.
835// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
836// be skipped, then true is returned.
837// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600838bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
839 const core_validation::layer_data *dev_data, uint32_t write_count,
840 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
841 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600842 bool skip_call = false;
843 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600844 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600845 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600846 auto set_node = core_validation::getSetNode(dev_data, dest_set);
847 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600848 skip_call |=
849 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 -0600850 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600851 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
852 reinterpret_cast<uint64_t &>(dest_set));
853 } else {
854 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600855 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600856 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
857 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
858 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
859 " with error: %s",
860 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
861 }
862 }
863 }
864 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600865 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600866 auto dst_set = p_cds[i].dstSet;
867 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600868 auto src_node = core_validation::getSetNode(dev_data, src_set);
869 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
870 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600871 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 -0600872 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600873 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
874 " that has not been allocated.",
875 reinterpret_cast<uint64_t &>(src_set));
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600876 } else if (!dst_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600877 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 -0600878 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600879 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
880 " that has not been allocated.",
881 reinterpret_cast<uint64_t &>(dst_set));
882 } else {
883 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600884 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600885 skip_call |=
886 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
887 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
888 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
889 " to Descriptor Set 0x%" PRIx64 " with error: %s",
890 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
891 }
892 }
893 }
894 return skip_call;
895}
896// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
897// sets, and then calls their respective Perform[Write|Copy]Update functions.
898// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
899// with the same set of updates.
900// This is split from the validate code to allow validation prior to calling down the chain, and then update after
901// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600902void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
903 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
904 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600905 // Write updates first
906 uint32_t i = 0;
907 for (i = 0; i < write_count; ++i) {
908 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600909 auto set_node = core_validation::getSetNode(dev_data, dest_set);
910 if (set_node) {
911 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600912 }
913 }
914 // Now copy updates
915 for (i = 0; i < copy_count; ++i) {
916 auto dst_set = p_cds[i].dstSet;
917 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600918 auto src_node = core_validation::getSetNode(dev_data, src_set);
919 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
920 if (src_node && dst_node) {
921 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600922 }
923 }
924}
925// Validate the state for a given write update but don't actually perform the update
926// If an error would occur for this update, return false and fill in details in error_msg string
927bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
928 std::string *error_msg) {
929 // Verify idle ds
930 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600931 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600932 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
933 << " that is in use by a command buffer.";
934 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600935 return false;
936 }
Tobin Ehlis300888c2016-05-18 13:43:26 -0600937 // Verify dst binding exists
938 if (!p_layout_->HasBinding(update->dstBinding)) {
939 std::stringstream error_str;
940 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
941 *error_msg = error_str.str();
942 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -0600943 }
944 // We know that binding is valid, verify update and do update on each descriptor
945 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
946 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
947 if (type != update->descriptorType) {
948 std::stringstream error_str;
949 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
950 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
951 *error_msg = error_str.str();
952 return false;
953 }
954 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
955 std::stringstream error_str;
956 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
957 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
958 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
959 << " combined with update array element offset of " << update->dstArrayElement
960 << " oversteps the size of this descriptor set.";
961 *error_msg = error_str.str();
962 return false;
963 }
964 // Verify consecutive bindings match (if needed)
965 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
966 set_, error_msg))
967 return false;
968 // Update is within bounds and consistent so last step is to validate update contents
969 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
970 std::stringstream error_str;
971 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
972 << " failed with error message: " << error_msg->c_str();
973 *error_msg = error_str.str();
974 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600975 }
976 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600977 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600978}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600979// For the given buffer, verify that its creation parameters are appropriate for the given type
980// If there's an error, update the error string with details and return false, else return true
981bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkBuffer buffer, VkDescriptorType type, std::string *error) const {
982 // First make sure that buffer is valid
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600983 auto buffer_node = getBufferNode(device_data_, buffer);
984 if (!buffer_node) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600985 std::stringstream error_str;
986 error_str << "Invalid VkBuffer: " << buffer;
987 *error = error_str.str();
988 return false;
989 }
990 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600991 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600992 std::string error_usage_bit;
993 switch (type) {
994 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
995 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
996 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
997 }
998 break;
999 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1000 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1001 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1002 }
1003 break;
1004 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1005 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1006 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1007 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1008 }
1009 break;
1010 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1011 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1012 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1013 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1014 }
1015 break;
1016 default:
1017 break;
1018 }
1019 if (!error_usage_bit.empty()) {
1020 std::stringstream error_str;
1021 error_str << "Buffer (" << buffer << ") with usage mask 0x" << usage << " being used for a descriptor update of type "
1022 << string_VkDescriptorType(type) << " does not have " << error_usage_bit << " set.";
1023 *error = error_str.str();
1024 return false;
1025 }
1026 return true;
1027}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001028// Verify that the contents of the update are ok, but don't perform actual update
1029bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
1030 std::string *error) const {
1031 switch (update->descriptorType) {
1032 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1033 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1034 // Validate image
1035 auto image_view = update->pImageInfo[di].imageView;
1036 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -06001037 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, image_map_,
Tobin Ehlis554bf382016-05-24 11:14:43 -06001038 image_to_swapchain_map_, swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001039 std::stringstream error_str;
1040 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1041 *error = error_str.str();
1042 return false;
1043 }
1044 }
1045 // Intentional fall-through to validate sampler
1046 }
1047 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1048 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1049 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001050 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001051 std::stringstream error_str;
1052 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1053 << update->pImageInfo[di].sampler << ".";
1054 *error = error_str.str();
1055 return false;
1056 }
1057 } else {
1058 // TODO : Warn here
1059 }
1060 }
1061 break;
1062 }
1063 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1064 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1065 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1066 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1067 auto image_view = update->pImageInfo[di].imageView;
1068 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -06001069 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, image_map_,
Tobin Ehlis554bf382016-05-24 11:14:43 -06001070 image_to_swapchain_map_, swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001071 std::stringstream error_str;
1072 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1073 *error = error_str.str();
1074 return false;
1075 }
1076 }
1077 break;
1078 }
1079 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1080 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1081 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1082 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis424859c2016-06-02 09:43:11 -06001083 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1084 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001085 std::stringstream error_str;
1086 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1087 *error = error_str.str();
1088 return false;
1089 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001090 auto buffer = bv_info->buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001091 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
1092 std::stringstream error_str;
1093 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str();
1094 *error = error_str.str();
1095 return false;
1096 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001097 }
1098 break;
1099 }
1100 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1101 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1102 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1103 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1104 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1105 auto buffer = update->pBufferInfo[di].buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001106 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001107 std::stringstream error_str;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001108 error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001109 *error = error_str.str();
1110 return false;
1111 }
1112 }
1113 break;
1114 }
1115 default:
1116 assert(0); // We've already verified update type so should never get here
1117 break;
1118 }
1119 // All checks passed so update contents are good
1120 return true;
1121}
1122// Verify that the contents of the update are ok, but don't perform actual update
1123bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001124 VkDescriptorType type, uint32_t index, std::string *error) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001125 switch (src_set->descriptors_[index]->descriptor_class) {
1126 case PlainSampler: {
1127 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1128 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1129 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001130 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001131 std::stringstream error_str;
1132 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1133 *error = error_str.str();
1134 return false;
1135 }
1136 } else {
1137 // TODO : Warn here
1138 }
1139 }
1140 break;
1141 }
1142 case ImageSampler: {
1143 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1144 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1145 // First validate sampler
1146 if (!img_samp_desc->IsImmutableSampler()) {
1147 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001148 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001149 std::stringstream error_str;
1150 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1151 *error = error_str.str();
1152 return false;
1153 }
1154 } else {
1155 // TODO : Warn here
1156 }
1157 // Validate image
1158 auto image_view = img_samp_desc->GetImageView();
1159 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -06001160 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, image_map_, image_to_swapchain_map_,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001161 swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001162 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001163 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001164 *error = error_str.str();
1165 return false;
1166 }
1167 }
1168 }
1169 case Image: {
1170 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1171 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1172 auto image_view = img_desc->GetImageView();
1173 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -06001174 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, image_map_, image_to_swapchain_map_,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001175 swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001176 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001177 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001178 *error = error_str.str();
1179 return false;
1180 }
1181 }
1182 break;
1183 }
1184 case TexelBuffer: {
1185 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1186 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -06001187 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1188 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001189 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001190 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1191 *error = error_str.str();
1192 return false;
1193 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001194 auto buffer = bv_info->buffer;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001195 if (!ValidateBufferUpdate(buffer, type, error)) {
1196 std::stringstream error_str;
1197 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001198 *error = error_str.str();
1199 return false;
1200 }
1201 }
1202 break;
1203 }
1204 case GeneralBuffer: {
1205 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1206 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001207 if (!ValidateBufferUpdate(buffer, type, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001208 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001209 error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001210 *error = error_str.str();
1211 return false;
1212 }
1213 }
1214 break;
1215 }
1216 default:
1217 assert(0); // We've already verified update type so should never get here
1218 break;
1219 }
1220 // All checks passed so update contents are good
1221 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001222}
Tobin Ehlisee471462016-05-26 11:21:59 -06001223// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
1224bool cvdescriptorset::ValidateAllocateDescriptorSets(
1225 const debug_report_data *report_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
1226 const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &set_layout_map,
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001227 const std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> &pool_map, AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001228 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001229
1230 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
1231 auto layout_it = set_layout_map.find(p_alloc_info->pSetLayouts[i]);
1232 if (layout_it == set_layout_map.end()) {
1233 skip_call |=
1234 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1235 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1236 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1237 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001238 } else {
1239 ds_data->layout_nodes[i] = layout_it->second;
1240 // Count total descriptors required per type
1241 for (uint32_t j = 0; j < layout_it->second->GetBindingCount(); ++j) {
1242 const auto &binding_layout = layout_it->second->GetDescriptorSetLayoutBindingPtrFromIndex(j);
1243 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1244 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1245 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001246 }
1247 }
1248 auto pool_it = pool_map.find(p_alloc_info->descriptorPool);
1249 if (pool_it == pool_map.end()) {
1250 skip_call |=
1251 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1252 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool), __LINE__, DRAWSTATE_INVALID_POOL, "DS",
1253 "Unable to find pool node for pool 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1254 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool));
1255 } else { // Make sure pool has all the available descriptors before calling down chain
1256 // Track number of descriptorSets allowable in this pool
1257 if (pool_it->second->availableSets < p_alloc_info->descriptorSetCount) {
1258 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1259 reinterpret_cast<uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1260 "DS", "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1261 ". This pool only has %d descriptorSets remaining.",
1262 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_it->second->pool),
1263 pool_it->second->availableSets);
1264 }
1265 // Determine whether descriptor counts are satisfiable
1266 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001267 if (ds_data->required_descriptors_by_type[i] > pool_it->second->availableDescriptorTypeCount[i]) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001268 skip_call |=
1269 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1270 reinterpret_cast<const uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1271 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1272 ". This pool only has %d descriptors of this type remaining.",
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001273 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Tobin Ehlisee471462016-05-26 11:21:59 -06001274 reinterpret_cast<uint64_t &>(pool_it->second->pool), pool_it->second->availableDescriptorTypeCount[i]);
1275 }
1276 }
1277 }
1278 return skip_call;
1279}
1280// Decrement allocated sets from the pool and insert new sets into set_map
1281void cvdescriptorset::PerformAllocateDescriptorSets(
1282 const VkDescriptorSetAllocateInfo *p_alloc_info, const VkDescriptorSet *descriptor_sets,
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001283 const AllocateDescriptorSetsData *ds_data, std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001284 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map, const core_validation::layer_data *dev_data,
Tobin Ehlisee471462016-05-26 11:21:59 -06001285 const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &layout_map,
Tobin Ehlisee471462016-05-26 11:21:59 -06001286 const std::unordered_map<VkImage, IMAGE_NODE> &image_map,
1287 const std::unordered_map<VkImage, VkSwapchainKHR> &image_to_swapchain_map,
1288 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> &swapchain_map) {
1289 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001290 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001291 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001292 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1293 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1294 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001295 /* Create tracking object for each descriptor set; insert into
1296 * global map and the pool's set.
1297 */
1298 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -06001299 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], ds_data->layout_nodes[i], dev_data, &image_map,
1300 &image_to_swapchain_map, &swapchain_map);
Tobin Ehlisee471462016-05-26 11:21:59 -06001301
1302 pool_state->sets.insert(new_ds);
1303 new_ds->in_use.store(0);
1304 (*set_map)[descriptor_sets[i]] = new_ds;
1305 }
1306}