blob: ed25030f7d65585d63f30ff8a1a0584f675d65f1 [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<VkImageView, VkImageViewCreateInfo> *image_view_map,
269 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
270 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
271 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
Tobin Ehlise2f80292016-06-02 10:08:53 -0600272 : some_update_(false), set_(set), p_layout_(layout), device_data_(dev_data), image_view_map_(image_view_map),
273 image_map_(image_map), image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600274 // Foreach binding, create default descriptors of given type
275 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
276 auto type = p_layout_->GetTypeFromIndex(i);
277 switch (type) {
278 case VK_DESCRIPTOR_TYPE_SAMPLER: {
279 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
280 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
281 if (immut_sampler)
Chris Forbescb621ea2016-05-30 11:47:31 +1200282 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600283 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200284 descriptors_.emplace_back(new SamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600285 }
286 break;
287 }
288 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
289 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
290 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
291 if (immut)
Chris Forbescb621ea2016-05-30 11:47:31 +1200292 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600293 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200294 descriptors_.emplace_back(new ImageSamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600295 }
296 break;
297 }
298 // ImageDescriptors
299 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
300 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
301 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
302 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200303 descriptors_.emplace_back(new ImageDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600304 break;
305 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
306 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
307 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200308 descriptors_.emplace_back(new TexelDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600309 break;
310 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
311 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
312 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
313 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
314 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200315 descriptors_.emplace_back(new BufferDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600316 break;
317 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600318 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600319 break;
320 }
321 }
322}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600323
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600324cvdescriptorset::DescriptorSet::~DescriptorSet() {
325 InvalidateBoundCmdBuffers();
326 // Remove link to any cmd buffers
327 for (auto cb : bound_cmd_buffers_) {
328 for (uint32_t i=0; i<VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
329 cb->lastBound[i].uniqueBoundSets.erase(this);
330 }
331 }
332}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600333// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
334bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
335 return layout->IsCompatible(p_layout_, error);
336}
337// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
338// This includes validating that all descriptors in the given bindings are updated,
339// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
340// Return true if state is acceptable, or false and write an error message into error string
341bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
342 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
iostrows5eb97652016-06-02 15:56:26 +0200343 auto dyn_offset_index = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600344 for (auto binding : bindings) {
345 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600346 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600347 // Nothing to do for strictly immutable sampler
348 } else {
349 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600350 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600351 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600352 std::stringstream error_str;
353 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
354 << " is being used in draw but has not been updated.";
355 *error = error_str.str();
356 return false;
357 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600358 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600359 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600360 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600361 auto buffer_node = getBufferNode(device_data_, buffer);
362 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600363 std::stringstream error_str;
364 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
365 << " references invalid buffer " << buffer << ".";
366 *error = error_str.str();
367 return false;
368 } else {
Tobin Ehlis997b2582016-06-02 08:43:37 -0600369 auto mem_entry = getMemObjInfo(device_data_, buffer_node->mem);
370 if (!mem_entry) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600371 std::stringstream error_str;
372 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600373 << " uses buffer " << buffer << " that references invalid memory " << buffer_node->mem
374 << ".";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600375 *error = error_str.str();
376 return false;
377 }
378 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600379 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600380 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600381 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600382 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
383 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600384 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
385 if (VK_WHOLE_SIZE == range) {
386 if ((dyn_offset + desc_offset) > buffer_size) {
387 std::stringstream error_str;
388 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
389 << " uses buffer " << buffer
390 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
391 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
392 << buffer_size << ".";
393 *error = error_str.str();
394 return false;
395 }
396 } else {
397 if ((dyn_offset + desc_offset + range) > buffer_size) {
398 std::stringstream error_str;
399 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
400 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
401 << " combined with offset " << desc_offset << " and range " << range
402 << " that oversteps the buffer size of " << buffer_size << ".";
403 *error = error_str.str();
404 return false;
405 }
406 }
407 }
408 }
409 }
410 }
411 }
412 }
413 return true;
414}
415// For given bindings, place any update buffers or images into the passed-in unordered_sets
416uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
417 std::unordered_set<VkBuffer> *buffer_set,
418 std::unordered_set<VkImageView> *image_set) const {
419 auto num_updates = 0;
420 for (auto binding : bindings) {
421 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600422 if (descriptors_[start_idx]->IsStorage()) {
423 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600424 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600425 if (descriptors_[start_idx + i]->updated) {
426 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600427 num_updates++;
428 }
429 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600430 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600431 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600432 if (descriptors_[start_idx + i]->updated) {
433 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -0600434 auto bv_info = getBufferViewInfo(device_data_, bufferview);
435 if (bv_info) {
436 buffer_set->insert(bv_info->buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600437 num_updates++;
438 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600439 }
440 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600441 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600442 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600443 if (descriptors_[start_idx + i]->updated) {
444 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600445 num_updates++;
446 }
447 }
448 }
449 }
450 }
451 return num_updates;
452}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600453// Set is being deleted or updates so invalidate all bound cmd buffers
454void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
455 for (auto cb_node : bound_cmd_buffers_) {
456 cb_node->state = CB_INVALID;
457 }
458}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600459// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600460void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600461 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
462 // perform update
463 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
464 descriptors_[start_idx + di]->WriteUpdate(update, di);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600465 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600466 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600467 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600468
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600469 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600470}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600471// Validate Copy update
472bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
473 const DescriptorSet *src_set, std::string *error) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600474 // Verify idle ds
475 if (in_use.load()) {
476 std::stringstream error_str;
477 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
478 << " that is in use by a command buffer.";
479 *error = error_str.str();
480 return false;
481 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600482 if (!p_layout_->HasBinding(update->dstBinding)) {
483 std::stringstream error_str;
484 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
485 *error = error_str.str();
486 return false;
487 }
488 if (!src_set->HasBinding(update->srcBinding)) {
489 std::stringstream error_str;
490 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
491 *error = error_str.str();
492 return false;
493 }
494 // src & dst set bindings are valid
495 // Check bounds of src & dst
496 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
497 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
498 // SRC update out of bounds
499 std::stringstream error_str;
500 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
501 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
502 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
503 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
504 *error = error_str.str();
505 return false;
506 }
507 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
508 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
509 // DST update out of bounds
510 std::stringstream error_str;
511 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
512 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
513 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
514 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
515 *error = error_str.str();
516 return false;
517 }
518 // Check that types match
519 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
520 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
521 if (src_type != dst_type) {
522 std::stringstream error_str;
523 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
524 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
525 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
526 *error = error_str.str();
527 return false;
528 }
529 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600530 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
531 "copy update from", src_set->GetSet(), error)) ||
532 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
533 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600534 return false;
535 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600536 // First make sure source descriptors are updated
537 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
538 if (!src_set->descriptors_[src_start_idx + i]) {
539 std::stringstream error_str;
540 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
541 << update->srcArrayElement + i << " has not been updated.";
542 *error = error_str.str();
543 return false;
544 }
545 }
546 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600547 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600548 return false;
549
550 // All checks passed so update is good
551 return true;
552}
553// Perform Copy update
554void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600555 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
556 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600557 // Update parameters all look good so perform update
558 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600559 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600560 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600561 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600562 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600563
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600564 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600565}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600566
Tobin Ehlis300888c2016-05-18 13:43:26 -0600567cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600568 updated = false;
569 descriptor_class = PlainSampler;
570};
571
Tobin Ehlis300888c2016-05-18 13:43:26 -0600572cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600573 updated = false;
574 descriptor_class = PlainSampler;
575 if (immut) {
576 sampler_ = *immut;
577 immutable_ = true;
578 updated = true;
579 }
580}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600581// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
582bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
583 return (getSamplerNode(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600584}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600585
Tobin Ehlis554bf382016-05-24 11:14:43 -0600586bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600587 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
588 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
589 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
590 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
591 std::string *error) {
592 auto image_pair = image_view_map->find(image_view);
593 if (image_pair == image_view_map->end()) {
594 std::stringstream error_str;
595 error_str << "Invalid VkImageView: " << image_view;
596 *error = error_str.str();
597 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600598 }
599 // Validate that imageLayout is compatible with aspect_mask and image format
600 // and validate that image usage bits are correct for given usage
601 VkImageAspectFlags aspect_mask = image_pair->second.subresourceRange.aspectMask;
602 VkImage image = image_pair->second.image;
603 VkFormat format = VK_FORMAT_MAX_ENUM;
604 VkImageUsageFlags usage = 0;
605 auto img_pair = image_map->find(image);
606 if (img_pair != image_map->end()) {
607 format = img_pair->second.createInfo.format;
608 usage = img_pair->second.createInfo.usage;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600609 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600610 // Also need to check the swapchains.
611 auto swapchain_pair = image_to_swapchain_map->find(image);
612 if (swapchain_pair != image_to_swapchain_map->end()) {
613 VkSwapchainKHR swapchain = swapchain_pair->second;
614 auto swapchain_pair = swapchain_map->find(swapchain);
615 if (swapchain_pair != swapchain_map->end()) {
616 format = swapchain_pair->second->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600617 }
618 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600619 }
620 // First validate that format and layout are compatible
621 if (format == VK_FORMAT_MAX_ENUM) {
622 std::stringstream error_str;
623 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
624 *error = error_str.str();
625 return false;
626 }
627 bool ds = vk_format_is_depth_or_stencil(format);
628 switch (image_layout) {
629 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
630 // Only Color bit must be set
631 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600632 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600633 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
634 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis554bf382016-05-24 11:14:43 -0600635 *error = error_str.str();
636 return false;
637 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600638 // format must NOT be DS
639 if (ds) {
640 std::stringstream error_str;
641 error_str << "ImageView (" << image_view
642 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
643 << string_VkFormat(format) << " which is not a color format.";
644 *error = error_str.str();
645 return false;
646 }
647 break;
648 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
649 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
650 // Depth or stencil bit must be set, but both must NOT be set
651 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
652 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
653 // both must NOT be set
654 std::stringstream error_str;
655 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
656 *error = error_str.str();
657 return false;
658 }
659 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
660 // Neither were set
661 std::stringstream error_str;
662 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
663 << " but does not have STENCIL or DEPTH aspects set";
664 *error = error_str.str();
665 return false;
666 }
667 // format must be DS
668 if (!ds) {
669 std::stringstream error_str;
670 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
671 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
672 *error = error_str.str();
673 return false;
674 }
675 break;
676 default:
677 // anything to check for other layouts?
678 break;
679 }
680 // Now validate that usage flags are correctly set for given type of update
681 std::string error_usage_bit;
682 switch (type) {
683 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
684 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
685 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
686 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
687 }
688 break;
689 }
690 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
691 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
692 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
693 }
694 break;
695 }
696 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
697 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
698 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
699 }
700 break;
701 }
702 default:
703 break;
704 }
705 if (!error_usage_bit.empty()) {
706 std::stringstream error_str;
707 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
708 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
709 << error_usage_bit << " set.";
710 *error = error_str.str();
711 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600712 }
713 return true;
714}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600715
Tobin Ehlis300888c2016-05-18 13:43:26 -0600716void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
717 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600718 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600719}
720
Tobin Ehlis300888c2016-05-18 13:43:26 -0600721void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600722 if (!immutable_) {
723 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600724 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600725 }
726 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600727}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600728
Tobin Ehlis300888c2016-05-18 13:43:26 -0600729cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
730 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600731 updated = false;
732 descriptor_class = ImageSampler;
733}
734
Tobin Ehlis300888c2016-05-18 13:43:26 -0600735cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
736 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600737 updated = false;
738 descriptor_class = ImageSampler;
739 if (immut) {
740 sampler_ = *immut;
741 immutable_ = true;
742 updated = true;
743 }
744}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600745
Tobin Ehlis300888c2016-05-18 13:43:26 -0600746void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600747 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600748 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600749 sampler_ = image_info.sampler;
750 image_view_ = image_info.imageView;
751 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600752}
753
Tobin Ehlis300888c2016-05-18 13:43:26 -0600754void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600755 if (!immutable_) {
756 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600757 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600758 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600759 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
760 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600761 updated = true;
762 image_view_ = image_view;
763 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600764}
765
Tobin Ehlis300888c2016-05-18 13:43:26 -0600766cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
767 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600768 updated = false;
769 descriptor_class = Image;
770 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
771 storage_ = true;
772};
773
Tobin Ehlis300888c2016-05-18 13:43:26 -0600774void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600775 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600776 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600777 image_view_ = image_info.imageView;
778 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600779}
780
Tobin Ehlis300888c2016-05-18 13:43:26 -0600781void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600782 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
783 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600784 updated = true;
785 image_view_ = image_view;
786 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600787}
788
Tobin Ehlis300888c2016-05-18 13:43:26 -0600789cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
790 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600791 updated = false;
792 descriptor_class = GeneralBuffer;
793 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
794 dynamic_ = true;
795 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
796 storage_ = true;
797 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
798 dynamic_ = true;
799 storage_ = true;
800 }
801}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600802void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600803 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600804 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600805 buffer_ = buffer_info.buffer;
806 offset_ = buffer_info.offset;
807 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600808}
809
Tobin Ehlis300888c2016-05-18 13:43:26 -0600810void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
811 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600812 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600813 buffer_ = buff_desc->buffer_;
814 offset_ = buff_desc->offset_;
815 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600816}
817
Tobin Ehlis300888c2016-05-18 13:43:26 -0600818cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600819 updated = false;
820 descriptor_class = TexelBuffer;
821 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
822 storage_ = true;
823};
Tobin Ehlis56a30942016-05-19 08:00:00 -0600824
Tobin Ehlis300888c2016-05-18 13:43:26 -0600825void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600826 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600827 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600828}
829
Tobin Ehlis300888c2016-05-18 13:43:26 -0600830void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
831 updated = true;
832 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
833}
834// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
835// sets, and then calls their respective Validate[Write|Copy]Update functions.
836// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
837// be skipped, then true is returned.
838// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600839bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
840 const core_validation::layer_data *dev_data, uint32_t write_count,
841 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
842 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600843 bool skip_call = false;
844 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600845 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600846 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600847 auto set_node = core_validation::getSetNode(dev_data, dest_set);
848 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600849 skip_call |=
850 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 -0600851 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600852 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
853 reinterpret_cast<uint64_t &>(dest_set));
854 } else {
855 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600856 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600857 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
858 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
859 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
860 " with error: %s",
861 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
862 }
863 }
864 }
865 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600866 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600867 auto dst_set = p_cds[i].dstSet;
868 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600869 auto src_node = core_validation::getSetNode(dev_data, src_set);
870 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
871 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600872 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 -0600873 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600874 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
875 " that has not been allocated.",
876 reinterpret_cast<uint64_t &>(src_set));
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600877 } else if (!dst_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600878 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 -0600879 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600880 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
881 " that has not been allocated.",
882 reinterpret_cast<uint64_t &>(dst_set));
883 } else {
884 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600885 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600886 skip_call |=
887 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
888 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
889 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
890 " to Descriptor Set 0x%" PRIx64 " with error: %s",
891 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
892 }
893 }
894 }
895 return skip_call;
896}
897// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
898// sets, and then calls their respective Perform[Write|Copy]Update functions.
899// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
900// with the same set of updates.
901// This is split from the validate code to allow validation prior to calling down the chain, and then update after
902// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600903void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
904 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
905 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600906 // Write updates first
907 uint32_t i = 0;
908 for (i = 0; i < write_count; ++i) {
909 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600910 auto set_node = core_validation::getSetNode(dev_data, dest_set);
911 if (set_node) {
912 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600913 }
914 }
915 // Now copy updates
916 for (i = 0; i < copy_count; ++i) {
917 auto dst_set = p_cds[i].dstSet;
918 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600919 auto src_node = core_validation::getSetNode(dev_data, src_set);
920 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
921 if (src_node && dst_node) {
922 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600923 }
924 }
925}
926// Validate the state for a given write update but don't actually perform the update
927// If an error would occur for this update, return false and fill in details in error_msg string
928bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
929 std::string *error_msg) {
930 // Verify idle ds
931 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600932 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600933 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
934 << " that is in use by a command buffer.";
935 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600936 return false;
937 }
Tobin Ehlis300888c2016-05-18 13:43:26 -0600938 // Verify dst binding exists
939 if (!p_layout_->HasBinding(update->dstBinding)) {
940 std::stringstream error_str;
941 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
942 *error_msg = error_str.str();
943 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -0600944 }
945 // We know that binding is valid, verify update and do update on each descriptor
946 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
947 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
948 if (type != update->descriptorType) {
949 std::stringstream error_str;
950 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
951 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
952 *error_msg = error_str.str();
953 return false;
954 }
955 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
956 std::stringstream error_str;
957 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
958 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
959 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
960 << " combined with update array element offset of " << update->dstArrayElement
961 << " oversteps the size of this descriptor set.";
962 *error_msg = error_str.str();
963 return false;
964 }
965 // Verify consecutive bindings match (if needed)
966 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
967 set_, error_msg))
968 return false;
969 // Update is within bounds and consistent so last step is to validate update contents
970 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
971 std::stringstream error_str;
972 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
973 << " failed with error message: " << error_msg->c_str();
974 *error_msg = error_str.str();
975 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600976 }
977 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600978 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600979}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600980// For the given buffer, verify that its creation parameters are appropriate for the given type
981// If there's an error, update the error string with details and return false, else return true
982bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkBuffer buffer, VkDescriptorType type, std::string *error) const {
983 // First make sure that buffer is valid
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600984 auto buffer_node = getBufferNode(device_data_, buffer);
985 if (!buffer_node) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600986 std::stringstream error_str;
987 error_str << "Invalid VkBuffer: " << buffer;
988 *error = error_str.str();
989 return false;
990 }
991 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600992 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600993 std::string error_usage_bit;
994 switch (type) {
995 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
996 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
997 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
998 }
999 break;
1000 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1001 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1002 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1003 }
1004 break;
1005 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1006 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1007 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1008 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1009 }
1010 break;
1011 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1012 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1013 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1014 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1015 }
1016 break;
1017 default:
1018 break;
1019 }
1020 if (!error_usage_bit.empty()) {
1021 std::stringstream error_str;
1022 error_str << "Buffer (" << buffer << ") with usage mask 0x" << usage << " being used for a descriptor update of type "
1023 << string_VkDescriptorType(type) << " does not have " << error_usage_bit << " set.";
1024 *error = error_str.str();
1025 return false;
1026 }
1027 return true;
1028}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001029// Verify that the contents of the update are ok, but don't perform actual update
1030bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
1031 std::string *error) const {
1032 switch (update->descriptorType) {
1033 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1034 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1035 // Validate image
1036 auto image_view = update->pImageInfo[di].imageView;
1037 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis554bf382016-05-24 11:14:43 -06001038 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, image_view_map_, image_map_,
1039 image_to_swapchain_map_, swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001040 std::stringstream error_str;
1041 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1042 *error = error_str.str();
1043 return false;
1044 }
1045 }
1046 // Intentional fall-through to validate sampler
1047 }
1048 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1049 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1050 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001051 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001052 std::stringstream error_str;
1053 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1054 << update->pImageInfo[di].sampler << ".";
1055 *error = error_str.str();
1056 return false;
1057 }
1058 } else {
1059 // TODO : Warn here
1060 }
1061 }
1062 break;
1063 }
1064 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1065 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1066 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1067 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1068 auto image_view = update->pImageInfo[di].imageView;
1069 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis554bf382016-05-24 11:14:43 -06001070 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, image_view_map_, image_map_,
1071 image_to_swapchain_map_, swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001072 std::stringstream error_str;
1073 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1074 *error = error_str.str();
1075 return false;
1076 }
1077 }
1078 break;
1079 }
1080 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1081 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1082 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1083 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis424859c2016-06-02 09:43:11 -06001084 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1085 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001086 std::stringstream error_str;
1087 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1088 *error = error_str.str();
1089 return false;
1090 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001091 auto buffer = bv_info->buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001092 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
1093 std::stringstream error_str;
1094 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str();
1095 *error = error_str.str();
1096 return false;
1097 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001098 }
1099 break;
1100 }
1101 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1102 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1103 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1104 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1105 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1106 auto buffer = update->pBufferInfo[di].buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001107 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001108 std::stringstream error_str;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001109 error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001110 *error = error_str.str();
1111 return false;
1112 }
1113 }
1114 break;
1115 }
1116 default:
1117 assert(0); // We've already verified update type so should never get here
1118 break;
1119 }
1120 // All checks passed so update contents are good
1121 return true;
1122}
1123// Verify that the contents of the update are ok, but don't perform actual update
1124bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001125 VkDescriptorType type, uint32_t index, std::string *error) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001126 switch (src_set->descriptors_[index]->descriptor_class) {
1127 case PlainSampler: {
1128 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1129 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1130 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001131 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001132 std::stringstream error_str;
1133 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1134 *error = error_str.str();
1135 return false;
1136 }
1137 } else {
1138 // TODO : Warn here
1139 }
1140 }
1141 break;
1142 }
1143 case ImageSampler: {
1144 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1145 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1146 // First validate sampler
1147 if (!img_samp_desc->IsImmutableSampler()) {
1148 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001149 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001150 std::stringstream error_str;
1151 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1152 *error = error_str.str();
1153 return false;
1154 }
1155 } else {
1156 // TODO : Warn here
1157 }
1158 // Validate image
1159 auto image_view = img_samp_desc->GetImageView();
1160 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001161 if (!ValidateImageUpdate(image_view, image_layout, type, image_view_map_, image_map_, image_to_swapchain_map_,
1162 swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001163 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001164 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001165 *error = error_str.str();
1166 return false;
1167 }
1168 }
1169 }
1170 case Image: {
1171 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1172 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1173 auto image_view = img_desc->GetImageView();
1174 auto image_layout = img_desc->GetImageLayout();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001175 if (!ValidateImageUpdate(image_view, image_layout, type, image_view_map_, image_map_, image_to_swapchain_map_,
1176 swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001177 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001178 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001179 *error = error_str.str();
1180 return false;
1181 }
1182 }
1183 break;
1184 }
1185 case TexelBuffer: {
1186 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1187 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -06001188 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1189 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001190 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001191 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1192 *error = error_str.str();
1193 return false;
1194 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001195 auto buffer = bv_info->buffer;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001196 if (!ValidateBufferUpdate(buffer, type, error)) {
1197 std::stringstream error_str;
1198 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001199 *error = error_str.str();
1200 return false;
1201 }
1202 }
1203 break;
1204 }
1205 case GeneralBuffer: {
1206 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1207 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001208 if (!ValidateBufferUpdate(buffer, type, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001209 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001210 error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001211 *error = error_str.str();
1212 return false;
1213 }
1214 }
1215 break;
1216 }
1217 default:
1218 assert(0); // We've already verified update type so should never get here
1219 break;
1220 }
1221 // All checks passed so update contents are good
1222 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001223}
Tobin Ehlisee471462016-05-26 11:21:59 -06001224// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
1225bool cvdescriptorset::ValidateAllocateDescriptorSets(
1226 const debug_report_data *report_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
1227 const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &set_layout_map,
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001228 const std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> &pool_map, AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001229 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001230
1231 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
1232 auto layout_it = set_layout_map.find(p_alloc_info->pSetLayouts[i]);
1233 if (layout_it == set_layout_map.end()) {
1234 skip_call |=
1235 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1236 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1237 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1238 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001239 } else {
1240 ds_data->layout_nodes[i] = layout_it->second;
1241 // Count total descriptors required per type
1242 for (uint32_t j = 0; j < layout_it->second->GetBindingCount(); ++j) {
1243 const auto &binding_layout = layout_it->second->GetDescriptorSetLayoutBindingPtrFromIndex(j);
1244 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1245 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1246 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001247 }
1248 }
1249 auto pool_it = pool_map.find(p_alloc_info->descriptorPool);
1250 if (pool_it == pool_map.end()) {
1251 skip_call |=
1252 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1253 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool), __LINE__, DRAWSTATE_INVALID_POOL, "DS",
1254 "Unable to find pool node for pool 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1255 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool));
1256 } else { // Make sure pool has all the available descriptors before calling down chain
1257 // Track number of descriptorSets allowable in this pool
1258 if (pool_it->second->availableSets < p_alloc_info->descriptorSetCount) {
1259 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1260 reinterpret_cast<uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1261 "DS", "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1262 ". This pool only has %d descriptorSets remaining.",
1263 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_it->second->pool),
1264 pool_it->second->availableSets);
1265 }
1266 // Determine whether descriptor counts are satisfiable
1267 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001268 if (ds_data->required_descriptors_by_type[i] > pool_it->second->availableDescriptorTypeCount[i]) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001269 skip_call |=
1270 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1271 reinterpret_cast<const uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1272 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1273 ". This pool only has %d descriptors of this type remaining.",
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001274 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Tobin Ehlisee471462016-05-26 11:21:59 -06001275 reinterpret_cast<uint64_t &>(pool_it->second->pool), pool_it->second->availableDescriptorTypeCount[i]);
1276 }
1277 }
1278 }
1279 return skip_call;
1280}
1281// Decrement allocated sets from the pool and insert new sets into set_map
1282void cvdescriptorset::PerformAllocateDescriptorSets(
1283 const VkDescriptorSetAllocateInfo *p_alloc_info, const VkDescriptorSet *descriptor_sets,
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001284 const AllocateDescriptorSetsData *ds_data, std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001285 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map, const core_validation::layer_data *dev_data,
Tobin Ehlisee471462016-05-26 11:21:59 -06001286 const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &layout_map,
Tobin Ehlisee471462016-05-26 11:21:59 -06001287 const std::unordered_map<VkImageView, VkImageViewCreateInfo> &image_view_map,
1288 const std::unordered_map<VkImage, IMAGE_NODE> &image_map,
1289 const std::unordered_map<VkImage, VkSwapchainKHR> &image_to_swapchain_map,
1290 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> &swapchain_map) {
1291 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001292 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001293 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001294 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1295 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1296 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001297 /* Create tracking object for each descriptor set; insert into
1298 * global map and the pool's set.
1299 */
1300 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001301 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], ds_data->layout_nodes[i], dev_data, &image_view_map,
1302 &image_map, &image_to_swapchain_map, &swapchain_map);
Tobin Ehlisee471462016-05-26 11:21:59 -06001303
1304 pool_state->sets.insert(new_ds);
1305 new_ds->in_use.store(0);
1306 (*set_map)[descriptor_sets[i]] = new_ds;
1307 }
1308}