blob: 2393d308ef4f5246e9298f71cd7228b66968e4d0 [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<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
Tobin Ehlis969a5262016-06-02 12:13:32 -0600269 : some_update_(false), set_(set), p_layout_(layout), device_data_(dev_data), swapchain_map_(swapchain_map) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600270 // Foreach binding, create default descriptors of given type
271 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
272 auto type = p_layout_->GetTypeFromIndex(i);
273 switch (type) {
274 case VK_DESCRIPTOR_TYPE_SAMPLER: {
275 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
276 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
277 if (immut_sampler)
Chris Forbescb621ea2016-05-30 11:47:31 +1200278 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600279 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200280 descriptors_.emplace_back(new SamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600281 }
282 break;
283 }
284 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
285 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
286 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
287 if (immut)
Chris Forbescb621ea2016-05-30 11:47:31 +1200288 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600289 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200290 descriptors_.emplace_back(new ImageSamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600291 }
292 break;
293 }
294 // ImageDescriptors
295 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
296 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
297 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
298 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200299 descriptors_.emplace_back(new ImageDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600300 break;
301 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
302 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
303 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200304 descriptors_.emplace_back(new TexelDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600305 break;
306 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
307 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
308 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
309 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
310 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200311 descriptors_.emplace_back(new BufferDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600312 break;
313 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600314 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600315 break;
316 }
317 }
318}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600319
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600320cvdescriptorset::DescriptorSet::~DescriptorSet() {
321 InvalidateBoundCmdBuffers();
322 // Remove link to any cmd buffers
323 for (auto cb : bound_cmd_buffers_) {
324 for (uint32_t i=0; i<VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
325 cb->lastBound[i].uniqueBoundSets.erase(this);
326 }
327 }
328}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600329// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
330bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
331 return layout->IsCompatible(p_layout_, error);
332}
333// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
334// This includes validating that all descriptors in the given bindings are updated,
335// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
336// Return true if state is acceptable, or false and write an error message into error string
337bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
338 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
iostrows5eb97652016-06-02 15:56:26 +0200339 auto dyn_offset_index = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600340 for (auto binding : bindings) {
341 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600342 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600343 // Nothing to do for strictly immutable sampler
344 } else {
345 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600346 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600347 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600348 std::stringstream error_str;
349 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
350 << " is being used in draw but has not been updated.";
351 *error = error_str.str();
352 return false;
353 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600354 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600355 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600356 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600357 auto buffer_node = getBufferNode(device_data_, buffer);
358 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600359 std::stringstream error_str;
360 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
361 << " references invalid buffer " << buffer << ".";
362 *error = error_str.str();
363 return false;
364 } else {
Tobin Ehlis997b2582016-06-02 08:43:37 -0600365 auto mem_entry = getMemObjInfo(device_data_, buffer_node->mem);
366 if (!mem_entry) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600367 std::stringstream error_str;
368 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600369 << " uses buffer " << buffer << " that references invalid memory " << buffer_node->mem
370 << ".";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600371 *error = error_str.str();
372 return false;
373 }
374 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600375 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600376 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600377 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600378 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
379 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600380 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
381 if (VK_WHOLE_SIZE == range) {
382 if ((dyn_offset + desc_offset) > buffer_size) {
383 std::stringstream error_str;
384 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
385 << " uses buffer " << buffer
386 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
387 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
388 << buffer_size << ".";
389 *error = error_str.str();
390 return false;
391 }
392 } else {
393 if ((dyn_offset + desc_offset + range) > buffer_size) {
394 std::stringstream error_str;
395 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
396 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
397 << " combined with offset " << desc_offset << " and range " << range
398 << " that oversteps the buffer size of " << buffer_size << ".";
399 *error = error_str.str();
400 return false;
401 }
402 }
403 }
404 }
405 }
406 }
407 }
408 }
409 return true;
410}
411// For given bindings, place any update buffers or images into the passed-in unordered_sets
412uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
413 std::unordered_set<VkBuffer> *buffer_set,
414 std::unordered_set<VkImageView> *image_set) const {
415 auto num_updates = 0;
416 for (auto binding : bindings) {
417 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600418 if (descriptors_[start_idx]->IsStorage()) {
419 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600420 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600421 if (descriptors_[start_idx + i]->updated) {
422 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600423 num_updates++;
424 }
425 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600426 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600427 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600428 if (descriptors_[start_idx + i]->updated) {
429 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -0600430 auto bv_info = getBufferViewInfo(device_data_, bufferview);
431 if (bv_info) {
432 buffer_set->insert(bv_info->buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600433 num_updates++;
434 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600435 }
436 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600437 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600438 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600439 if (descriptors_[start_idx + i]->updated) {
440 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600441 num_updates++;
442 }
443 }
444 }
445 }
446 }
447 return num_updates;
448}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600449// Set is being deleted or updates so invalidate all bound cmd buffers
450void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
451 for (auto cb_node : bound_cmd_buffers_) {
452 cb_node->state = CB_INVALID;
453 }
454}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600455// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600456void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600457 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
458 // perform update
459 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
460 descriptors_[start_idx + di]->WriteUpdate(update, di);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600461 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600462 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600463 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600464
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600465 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600466}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600467// Validate Copy update
468bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
469 const DescriptorSet *src_set, std::string *error) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600470 // Verify idle ds
471 if (in_use.load()) {
472 std::stringstream error_str;
473 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
474 << " that is in use by a command buffer.";
475 *error = error_str.str();
476 return false;
477 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600478 if (!p_layout_->HasBinding(update->dstBinding)) {
479 std::stringstream error_str;
480 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
481 *error = error_str.str();
482 return false;
483 }
484 if (!src_set->HasBinding(update->srcBinding)) {
485 std::stringstream error_str;
486 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
487 *error = error_str.str();
488 return false;
489 }
490 // src & dst set bindings are valid
491 // Check bounds of src & dst
492 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
493 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
494 // SRC update out of bounds
495 std::stringstream error_str;
496 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
497 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
498 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
499 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
500 *error = error_str.str();
501 return false;
502 }
503 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
504 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
505 // DST update out of bounds
506 std::stringstream error_str;
507 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
508 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
509 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
510 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
511 *error = error_str.str();
512 return false;
513 }
514 // Check that types match
515 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
516 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
517 if (src_type != dst_type) {
518 std::stringstream error_str;
519 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
520 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
521 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
522 *error = error_str.str();
523 return false;
524 }
525 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600526 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
527 "copy update from", src_set->GetSet(), error)) ||
528 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
529 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600530 return false;
531 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600532 // First make sure source descriptors are updated
533 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
534 if (!src_set->descriptors_[src_start_idx + i]) {
535 std::stringstream error_str;
536 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
537 << update->srcArrayElement + i << " has not been updated.";
538 *error = error_str.str();
539 return false;
540 }
541 }
542 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600543 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600544 return false;
545
546 // All checks passed so update is good
547 return true;
548}
549// Perform Copy update
550void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600551 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
552 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600553 // Update parameters all look good so perform update
554 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600555 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600556 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600557 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600558 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600559
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600560 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600561}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600562
Tobin Ehlis300888c2016-05-18 13:43:26 -0600563cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600564 updated = false;
565 descriptor_class = PlainSampler;
566};
567
Tobin Ehlis300888c2016-05-18 13:43:26 -0600568cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600569 updated = false;
570 descriptor_class = PlainSampler;
571 if (immut) {
572 sampler_ = *immut;
573 immutable_ = true;
574 updated = true;
575 }
576}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600577// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
578bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
579 return (getSamplerNode(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600580}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600581
Tobin Ehlis554bf382016-05-24 11:14:43 -0600582bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600583 const core_validation::layer_data *dev_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600584 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
585 std::string *error) {
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600586 auto iv_data = getImageViewData(dev_data, image_view);
587 if (!iv_data) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600588 std::stringstream error_str;
589 error_str << "Invalid VkImageView: " << image_view;
590 *error = error_str.str();
591 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600592 }
593 // Validate that imageLayout is compatible with aspect_mask and image format
594 // and validate that image usage bits are correct for given usage
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600595 VkImageAspectFlags aspect_mask = iv_data->subresourceRange.aspectMask;
596 VkImage image = iv_data->image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600597 VkFormat format = VK_FORMAT_MAX_ENUM;
598 VkImageUsageFlags usage = 0;
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600599 auto image_node = getImageNode(dev_data, image);
600 if (image_node) {
601 format = image_node->createInfo.format;
602 usage = image_node->createInfo.usage;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600603 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600604 // Also need to check the swapchains.
Tobin Ehlis969a5262016-06-02 12:13:32 -0600605 auto swapchain = getSwapchainFromImage(dev_data, image);
606 if (swapchain) {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600607 auto swapchain_pair = swapchain_map->find(swapchain);
608 if (swapchain_pair != swapchain_map->end()) {
609 format = swapchain_pair->second->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600610 }
611 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600612 }
613 // First validate that format and layout are compatible
614 if (format == VK_FORMAT_MAX_ENUM) {
615 std::stringstream error_str;
616 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
617 *error = error_str.str();
618 return false;
619 }
620 bool ds = vk_format_is_depth_or_stencil(format);
621 switch (image_layout) {
622 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
623 // Only Color bit must be set
624 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600625 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600626 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
627 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis554bf382016-05-24 11:14:43 -0600628 *error = error_str.str();
629 return false;
630 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600631 // format must NOT be DS
632 if (ds) {
633 std::stringstream error_str;
634 error_str << "ImageView (" << image_view
635 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
636 << string_VkFormat(format) << " which is not a color format.";
637 *error = error_str.str();
638 return false;
639 }
640 break;
641 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
642 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
643 // Depth or stencil bit must be set, but both must NOT be set
644 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
645 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
646 // both must NOT be set
647 std::stringstream error_str;
648 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
649 *error = error_str.str();
650 return false;
651 }
652 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
653 // Neither were set
654 std::stringstream error_str;
655 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
656 << " but does not have STENCIL or DEPTH aspects set";
657 *error = error_str.str();
658 return false;
659 }
660 // format must be DS
661 if (!ds) {
662 std::stringstream error_str;
663 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
664 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
665 *error = error_str.str();
666 return false;
667 }
668 break;
669 default:
670 // anything to check for other layouts?
671 break;
672 }
673 // Now validate that usage flags are correctly set for given type of update
674 std::string error_usage_bit;
675 switch (type) {
676 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
677 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
678 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
679 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
680 }
681 break;
682 }
683 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
684 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
685 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
686 }
687 break;
688 }
689 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
690 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
691 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
692 }
693 break;
694 }
695 default:
696 break;
697 }
698 if (!error_usage_bit.empty()) {
699 std::stringstream error_str;
700 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
701 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
702 << error_usage_bit << " set.";
703 *error = error_str.str();
704 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600705 }
706 return true;
707}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600708
Tobin Ehlis300888c2016-05-18 13:43:26 -0600709void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
710 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600711 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600712}
713
Tobin Ehlis300888c2016-05-18 13:43:26 -0600714void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600715 if (!immutable_) {
716 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600717 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600718 }
719 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600720}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600721
Tobin Ehlis300888c2016-05-18 13:43:26 -0600722cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
723 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600724 updated = false;
725 descriptor_class = ImageSampler;
726}
727
Tobin Ehlis300888c2016-05-18 13:43:26 -0600728cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
729 : sampler_(VK_NULL_HANDLE), immutable_(true), 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 if (immut) {
733 sampler_ = *immut;
734 immutable_ = true;
735 updated = true;
736 }
737}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600738
Tobin Ehlis300888c2016-05-18 13:43:26 -0600739void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600740 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600741 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600742 sampler_ = image_info.sampler;
743 image_view_ = image_info.imageView;
744 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600745}
746
Tobin Ehlis300888c2016-05-18 13:43:26 -0600747void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600748 if (!immutable_) {
749 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600750 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600751 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600752 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
753 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600754 updated = true;
755 image_view_ = image_view;
756 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600757}
758
Tobin Ehlis300888c2016-05-18 13:43:26 -0600759cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
760 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600761 updated = false;
762 descriptor_class = Image;
763 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
764 storage_ = true;
765};
766
Tobin Ehlis300888c2016-05-18 13:43:26 -0600767void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600768 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600769 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600770 image_view_ = image_info.imageView;
771 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600772}
773
Tobin Ehlis300888c2016-05-18 13:43:26 -0600774void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600775 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
776 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600777 updated = true;
778 image_view_ = image_view;
779 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600780}
781
Tobin Ehlis300888c2016-05-18 13:43:26 -0600782cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
783 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600784 updated = false;
785 descriptor_class = GeneralBuffer;
786 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
787 dynamic_ = true;
788 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
789 storage_ = true;
790 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
791 dynamic_ = true;
792 storage_ = true;
793 }
794}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600795void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600796 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600797 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600798 buffer_ = buffer_info.buffer;
799 offset_ = buffer_info.offset;
800 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600801}
802
Tobin Ehlis300888c2016-05-18 13:43:26 -0600803void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
804 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600805 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600806 buffer_ = buff_desc->buffer_;
807 offset_ = buff_desc->offset_;
808 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600809}
810
Tobin Ehlis300888c2016-05-18 13:43:26 -0600811cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600812 updated = false;
813 descriptor_class = TexelBuffer;
814 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
815 storage_ = true;
816};
Tobin Ehlis56a30942016-05-19 08:00:00 -0600817
Tobin Ehlis300888c2016-05-18 13:43:26 -0600818void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600819 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600820 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600821}
822
Tobin Ehlis300888c2016-05-18 13:43:26 -0600823void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
824 updated = true;
825 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
826}
827// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
828// sets, and then calls their respective Validate[Write|Copy]Update functions.
829// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
830// be skipped, then true is returned.
831// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600832bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
833 const core_validation::layer_data *dev_data, uint32_t write_count,
834 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
835 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600836 bool skip_call = false;
837 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600838 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600839 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600840 auto set_node = core_validation::getSetNode(dev_data, dest_set);
841 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600842 skip_call |=
843 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 -0600844 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600845 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
846 reinterpret_cast<uint64_t &>(dest_set));
847 } else {
848 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600849 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600850 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
851 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
852 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
853 " with error: %s",
854 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
855 }
856 }
857 }
858 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600859 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600860 auto dst_set = p_cds[i].dstSet;
861 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600862 auto src_node = core_validation::getSetNode(dev_data, src_set);
863 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
864 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600865 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 -0600866 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600867 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
868 " that has not been allocated.",
869 reinterpret_cast<uint64_t &>(src_set));
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600870 } else if (!dst_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 &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600873 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
874 " that has not been allocated.",
875 reinterpret_cast<uint64_t &>(dst_set));
876 } else {
877 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600878 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600879 skip_call |=
880 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
881 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
882 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
883 " to Descriptor Set 0x%" PRIx64 " with error: %s",
884 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
885 }
886 }
887 }
888 return skip_call;
889}
890// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
891// sets, and then calls their respective Perform[Write|Copy]Update functions.
892// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
893// with the same set of updates.
894// This is split from the validate code to allow validation prior to calling down the chain, and then update after
895// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600896void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
897 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
898 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600899 // Write updates first
900 uint32_t i = 0;
901 for (i = 0; i < write_count; ++i) {
902 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600903 auto set_node = core_validation::getSetNode(dev_data, dest_set);
904 if (set_node) {
905 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600906 }
907 }
908 // Now copy updates
909 for (i = 0; i < copy_count; ++i) {
910 auto dst_set = p_cds[i].dstSet;
911 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600912 auto src_node = core_validation::getSetNode(dev_data, src_set);
913 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
914 if (src_node && dst_node) {
915 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600916 }
917 }
918}
919// Validate the state for a given write update but don't actually perform the update
920// If an error would occur for this update, return false and fill in details in error_msg string
921bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
922 std::string *error_msg) {
923 // Verify idle ds
924 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600925 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600926 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
927 << " that is in use by a command buffer.";
928 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600929 return false;
930 }
Tobin Ehlis300888c2016-05-18 13:43:26 -0600931 // Verify dst binding exists
932 if (!p_layout_->HasBinding(update->dstBinding)) {
933 std::stringstream error_str;
934 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
935 *error_msg = error_str.str();
936 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -0600937 }
938 // We know that binding is valid, verify update and do update on each descriptor
939 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
940 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
941 if (type != update->descriptorType) {
942 std::stringstream error_str;
943 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
944 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
945 *error_msg = error_str.str();
946 return false;
947 }
948 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
949 std::stringstream error_str;
950 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
951 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
952 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
953 << " combined with update array element offset of " << update->dstArrayElement
954 << " oversteps the size of this descriptor set.";
955 *error_msg = error_str.str();
956 return false;
957 }
958 // Verify consecutive bindings match (if needed)
959 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
960 set_, error_msg))
961 return false;
962 // Update is within bounds and consistent so last step is to validate update contents
963 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
964 std::stringstream error_str;
965 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
966 << " failed with error message: " << error_msg->c_str();
967 *error_msg = error_str.str();
968 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600969 }
970 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600971 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600972}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600973// For the given buffer, verify that its creation parameters are appropriate for the given type
974// If there's an error, update the error string with details and return false, else return true
975bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkBuffer buffer, VkDescriptorType type, std::string *error) const {
976 // First make sure that buffer is valid
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600977 auto buffer_node = getBufferNode(device_data_, buffer);
978 if (!buffer_node) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600979 std::stringstream error_str;
980 error_str << "Invalid VkBuffer: " << buffer;
981 *error = error_str.str();
982 return false;
983 }
984 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600985 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -0600986 std::string error_usage_bit;
987 switch (type) {
988 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
989 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
990 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
991 }
992 break;
993 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
994 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
995 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
996 }
997 break;
998 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
999 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1000 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1001 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1002 }
1003 break;
1004 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1005 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1006 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1007 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1008 }
1009 break;
1010 default:
1011 break;
1012 }
1013 if (!error_usage_bit.empty()) {
1014 std::stringstream error_str;
1015 error_str << "Buffer (" << buffer << ") with usage mask 0x" << usage << " being used for a descriptor update of type "
1016 << string_VkDescriptorType(type) << " does not have " << error_usage_bit << " set.";
1017 *error = error_str.str();
1018 return false;
1019 }
1020 return true;
1021}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001022// Verify that the contents of the update are ok, but don't perform actual update
1023bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
1024 std::string *error) const {
1025 switch (update->descriptorType) {
1026 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1027 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1028 // Validate image
1029 auto image_view = update->pImageInfo[di].imageView;
1030 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis969a5262016-06-02 12:13:32 -06001031 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001032 std::stringstream error_str;
1033 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1034 *error = error_str.str();
1035 return false;
1036 }
1037 }
1038 // Intentional fall-through to validate sampler
1039 }
1040 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1041 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1042 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001043 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001044 std::stringstream error_str;
1045 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1046 << update->pImageInfo[di].sampler << ".";
1047 *error = error_str.str();
1048 return false;
1049 }
1050 } else {
1051 // TODO : Warn here
1052 }
1053 }
1054 break;
1055 }
1056 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1057 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1058 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1059 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1060 auto image_view = update->pImageInfo[di].imageView;
1061 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis969a5262016-06-02 12:13:32 -06001062 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001063 std::stringstream error_str;
1064 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1065 *error = error_str.str();
1066 return false;
1067 }
1068 }
1069 break;
1070 }
1071 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1072 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1073 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1074 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis424859c2016-06-02 09:43:11 -06001075 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1076 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001077 std::stringstream error_str;
1078 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1079 *error = error_str.str();
1080 return false;
1081 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001082 auto buffer = bv_info->buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001083 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
1084 std::stringstream error_str;
1085 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str();
1086 *error = error_str.str();
1087 return false;
1088 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001089 }
1090 break;
1091 }
1092 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1093 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1094 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1095 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1096 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1097 auto buffer = update->pBufferInfo[di].buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001098 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001099 std::stringstream error_str;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001100 error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001101 *error = error_str.str();
1102 return false;
1103 }
1104 }
1105 break;
1106 }
1107 default:
1108 assert(0); // We've already verified update type so should never get here
1109 break;
1110 }
1111 // All checks passed so update contents are good
1112 return true;
1113}
1114// Verify that the contents of the update are ok, but don't perform actual update
1115bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001116 VkDescriptorType type, uint32_t index, std::string *error) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001117 switch (src_set->descriptors_[index]->descriptor_class) {
1118 case PlainSampler: {
1119 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1120 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1121 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001122 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001123 std::stringstream error_str;
1124 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1125 *error = error_str.str();
1126 return false;
1127 }
1128 } else {
1129 // TODO : Warn here
1130 }
1131 }
1132 break;
1133 }
1134 case ImageSampler: {
1135 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1136 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1137 // First validate sampler
1138 if (!img_samp_desc->IsImmutableSampler()) {
1139 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001140 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001141 std::stringstream error_str;
1142 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1143 *error = error_str.str();
1144 return false;
1145 }
1146 } else {
1147 // TODO : Warn here
1148 }
1149 // Validate image
1150 auto image_view = img_samp_desc->GetImageView();
1151 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlis969a5262016-06-02 12:13:32 -06001152 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001153 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001154 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001155 *error = error_str.str();
1156 return false;
1157 }
1158 }
1159 }
1160 case Image: {
1161 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1162 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1163 auto image_view = img_desc->GetImageView();
1164 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlis969a5262016-06-02 12:13:32 -06001165 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, swapchain_map_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001166 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001167 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001168 *error = error_str.str();
1169 return false;
1170 }
1171 }
1172 break;
1173 }
1174 case TexelBuffer: {
1175 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1176 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -06001177 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1178 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001179 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001180 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1181 *error = error_str.str();
1182 return false;
1183 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001184 auto buffer = bv_info->buffer;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001185 if (!ValidateBufferUpdate(buffer, type, error)) {
1186 std::stringstream error_str;
1187 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001188 *error = error_str.str();
1189 return false;
1190 }
1191 }
1192 break;
1193 }
1194 case GeneralBuffer: {
1195 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1196 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001197 if (!ValidateBufferUpdate(buffer, type, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001198 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001199 error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001200 *error = error_str.str();
1201 return false;
1202 }
1203 }
1204 break;
1205 }
1206 default:
1207 assert(0); // We've already verified update type so should never get here
1208 break;
1209 }
1210 // All checks passed so update contents are good
1211 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001212}
Tobin Ehlisee471462016-05-26 11:21:59 -06001213// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
1214bool cvdescriptorset::ValidateAllocateDescriptorSets(
1215 const debug_report_data *report_data, const VkDescriptorSetAllocateInfo *p_alloc_info,
1216 const std::unordered_map<VkDescriptorSetLayout, cvdescriptorset::DescriptorSetLayout *> &set_layout_map,
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001217 const std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> &pool_map, AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001218 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001219
1220 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
1221 auto layout_it = set_layout_map.find(p_alloc_info->pSetLayouts[i]);
1222 if (layout_it == set_layout_map.end()) {
1223 skip_call |=
1224 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1225 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1226 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1227 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001228 } else {
1229 ds_data->layout_nodes[i] = layout_it->second;
1230 // Count total descriptors required per type
1231 for (uint32_t j = 0; j < layout_it->second->GetBindingCount(); ++j) {
1232 const auto &binding_layout = layout_it->second->GetDescriptorSetLayoutBindingPtrFromIndex(j);
1233 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1234 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1235 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001236 }
1237 }
1238 auto pool_it = pool_map.find(p_alloc_info->descriptorPool);
1239 if (pool_it == pool_map.end()) {
1240 skip_call |=
1241 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1242 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool), __LINE__, DRAWSTATE_INVALID_POOL, "DS",
1243 "Unable to find pool node for pool 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1244 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool));
1245 } else { // Make sure pool has all the available descriptors before calling down chain
1246 // Track number of descriptorSets allowable in this pool
1247 if (pool_it->second->availableSets < p_alloc_info->descriptorSetCount) {
1248 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1249 reinterpret_cast<uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1250 "DS", "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1251 ". This pool only has %d descriptorSets remaining.",
1252 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_it->second->pool),
1253 pool_it->second->availableSets);
1254 }
1255 // Determine whether descriptor counts are satisfiable
1256 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001257 if (ds_data->required_descriptors_by_type[i] > pool_it->second->availableDescriptorTypeCount[i]) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001258 skip_call |=
1259 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1260 reinterpret_cast<const uint64_t &>(pool_it->second->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1261 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1262 ". This pool only has %d descriptors of this type remaining.",
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001263 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
Tobin Ehlisee471462016-05-26 11:21:59 -06001264 reinterpret_cast<uint64_t &>(pool_it->second->pool), pool_it->second->availableDescriptorTypeCount[i]);
1265 }
1266 }
1267 }
1268 return skip_call;
1269}
1270// Decrement allocated sets from the pool and insert new sets into set_map
1271void cvdescriptorset::PerformAllocateDescriptorSets(
1272 const VkDescriptorSetAllocateInfo *p_alloc_info, const VkDescriptorSet *descriptor_sets,
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001273 const AllocateDescriptorSetsData *ds_data, std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map,
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001274 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map, const core_validation::layer_data *dev_data,
Tobin Ehlisee471462016-05-26 11:21:59 -06001275 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> &swapchain_map) {
1276 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001277 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001278 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001279 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1280 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1281 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001282 /* Create tracking object for each descriptor set; insert into
1283 * global map and the pool's set.
1284 */
1285 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis969a5262016-06-02 12:13:32 -06001286 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], ds_data->layout_nodes[i], dev_data, &swapchain_map);
Tobin Ehlisee471462016-05-26 11:21:59 -06001287
1288 pool_state->sets.insert(new_ds);
1289 new_ds->in_use.store(0);
1290 (*set_map)[descriptor_sets[i]] = new_ds;
1291 }
1292}