blob: b3a4967025e5ffafc2b018bed1460ca51cee7c99 [file] [log] [blame]
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
4 * Copyright (C) 2015-2016 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Tobin Ehlis <tobine@google.com>
19 */
20
21#include "descriptor_sets.h"
22#include "vk_enum_string_helper.h"
23#include "vk_safe_struct.h"
24#include <sstream>
25
26// Construct DescriptorSetLayout instance from given create info
27cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(debug_report_data *report_data,
28 const VkDescriptorSetLayoutCreateInfo *p_create_info,
29 const VkDescriptorSetLayout layout)
30 : layout_(layout), binding_count_(p_create_info->bindingCount), descriptor_count_(0), dynamic_descriptor_count_(0) {
31 uint32_t global_index = 0;
32 for (uint32_t i = 0; i < binding_count_; ++i) {
33 descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
34 if (!binding_to_index_map_.emplace(p_create_info->pBindings[i].binding, i).second) {
35 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
36 reinterpret_cast<uint64_t &>(layout_), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
37 "duplicated binding number in "
38 "VkDescriptorSetLayoutBinding");
39 }
40 binding_to_global_start_index_map_[p_create_info->pBindings[i].binding] = global_index;
41 global_index += p_create_info->pBindings[i].descriptorCount ? p_create_info->pBindings[i].descriptorCount - 1 : 0;
42 binding_to_global_end_index_map_[p_create_info->pBindings[i].binding] = global_index;
43 global_index++;
Tobin Ehlis664e6012016-05-05 11:04:44 -060044 bindings_.push_back(safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i]));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060045 // In cases where we should ignore pImmutableSamplers make sure it's NULL
46 if ((p_create_info->pBindings[i].pImmutableSamplers) &&
47 ((p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_SAMPLER) &&
48 (p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER))) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060049 bindings_.back().pImmutableSamplers = nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060050 }
51 if (p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
52 p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
Tobin Ehlisef0de162016-06-20 13:07:34 -060053 dynamic_descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060054 }
55 }
56}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060057// put all bindings into the given set
58void cvdescriptorset::DescriptorSetLayout::FillBindingSet(std::unordered_set<uint32_t> *binding_set) const {
59 for (auto binding_index_pair : binding_to_index_map_)
60 binding_set->insert(binding_index_pair.first);
61}
Tobin Ehlis56a30942016-05-19 08:00:00 -060062
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060063VkDescriptorSetLayoutBinding const *
64cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060065 const auto &bi_itr = binding_to_index_map_.find(binding);
66 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060067 return bindings_[bi_itr->second].ptr();
Tobin Ehlis0bc30632016-05-05 10:16:02 -060068 }
69 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060070}
71VkDescriptorSetLayoutBinding const *
72cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const {
73 if (index >= bindings_.size())
74 return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -060075 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060076}
77// Return descriptorCount for given binding, 0 if index is unavailable
78uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060079 const auto &bi_itr = binding_to_index_map_.find(binding);
80 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060081 return bindings_[bi_itr->second].descriptorCount;
Tobin Ehlis0bc30632016-05-05 10:16:02 -060082 }
83 return 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060084}
85// Return descriptorCount for given index, 0 if index is unavailable
86uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
87 if (index >= bindings_.size())
88 return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -060089 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060090}
91// For the given binding, return descriptorType
92VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const {
93 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -060094 const auto &bi_itr = binding_to_index_map_.find(binding);
95 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060096 return bindings_[bi_itr->second].descriptorType;
Tobin Ehlis0bc30632016-05-05 10:16:02 -060097 }
98 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060099}
100// For the given index, return descriptorType
101VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
102 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600103 return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600104}
105// For the given global index, return descriptorType
106// Currently just counting up through bindings_, may improve this in future
107VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const {
108 uint32_t global_offset = 0;
109 for (auto binding : bindings_) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600110 global_offset += binding.descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600111 if (index < global_offset)
Tobin Ehlis664e6012016-05-05 11:04:44 -0600112 return binding.descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600113 }
114 assert(0); // requested global index is out of bounds
115 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
116}
117// For the given binding, return stageFlags
118VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const {
119 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600120 const auto &bi_itr = binding_to_index_map_.find(binding);
121 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600122 return bindings_[bi_itr->second].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600123 }
124 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600125}
126// For the given binding, return start index
127uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const {
128 assert(binding_to_global_start_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600129 const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding);
130 if (btgsi_itr != binding_to_global_start_index_map_.end()) {
131 return btgsi_itr->second;
132 }
133 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600134 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600135 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600136}
137// For the given binding, return end index
138uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
139 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600140 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
141 if (btgei_itr != binding_to_global_end_index_map_.end()) {
142 return btgei_itr->second;
143 }
144 // In error case max uint32_t so index is out of bounds to break ASAP
Tobin Ehlis58c59582016-06-21 12:34:33 -0600145 assert(0);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600146 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600147}
148// For given binding, return ptr to ImmutableSampler array
149VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
150 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600151 const auto &bi_itr = binding_to_index_map_.find(binding);
152 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600153 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600154 }
155 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600156}
157// For given index, return ptr to ImmutableSampler array
158VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
159 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600160 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600161}
162// If our layout is compatible with rh_ds_layout, return true,
163// else return false and fill in error_msg will description of what causes incompatibility
164bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
165 // Trivial case
166 if (layout_ == rh_ds_layout->GetDescriptorSetLayout())
167 return true;
168 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
169 std::stringstream error_str;
170 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
171 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
172 *error_msg = error_str.str();
173 return false; // trivial fail case
174 }
175 // Descriptor counts match so need to go through bindings one-by-one
176 // and verify that type and stageFlags match
177 for (auto binding : bindings_) {
178 // TODO : Do we also need to check immutable samplers?
179 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600180 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600181 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600182 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
183 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600184 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600185 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600186 *error_msg = error_str.str();
187 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600188 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600189 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600190 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
191 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600192 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600193 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600194 *error_msg = error_str.str();
195 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600196 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600197 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600198 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
199 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600200 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600201 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600202 *error_msg = error_str.str();
203 return false;
204 }
205 }
206 return true;
207}
208
209bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
210 if (!binding_to_index_map_.count(binding + 1))
211 return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600212 auto const &bi_itr = binding_to_index_map_.find(binding);
213 if (bi_itr != binding_to_index_map_.end()) {
214 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
215 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600216 auto type = bindings_[bi_itr->second].descriptorType;
217 auto stage_flags = bindings_[bi_itr->second].stageFlags;
218 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
219 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
220 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
221 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600222 return false;
223 }
224 return true;
225 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600226 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600227 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600228}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600229// Starting at offset descriptor of given binding, parse over update_count
230// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
231// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
232// If so, return true. If not, fill in error_msg and return false
233bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
234 const char *type, const VkDescriptorSet set,
235 std::string *error_msg) const {
236 // Verify consecutive bindings match (if needed)
237 auto orig_binding = current_binding;
238 // Track count of descriptors in the current_bindings that are remaining to be updated
239 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
240 // First, it's legal to offset beyond your own binding so handle that case
241 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
242 while (offset >= binding_remaining) {
243 // Advance to next binding, decrement offset by binding size
244 offset -= binding_remaining;
245 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
246 }
247 binding_remaining -= offset;
248 while (update_count > binding_remaining) { // While our updates overstep current binding
249 // Verify next consecutive binding matches type, stage flags & immutable sampler use
250 if (!IsNextBindingConsistent(current_binding++)) {
251 std::stringstream error_str;
252 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
253 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
254 "next binding is not consistent with current binding so this update is invalid.";
255 *error_msg = error_str.str();
256 return false;
257 }
258 // For sake of this check consider the bindings updated and grab count for next binding
259 update_count -= binding_remaining;
260 binding_remaining = GetDescriptorCountFromBinding(current_binding);
261 }
262 return true;
263}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600264
Tobin Ehlis68d0adf2016-06-01 11:33:50 -0600265cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
266 : required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
267
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600268cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const DescriptorSetLayout *layout,
Tobin Ehlis4e380592016-06-02 12:41:47 -0600269 const core_validation::layer_data *dev_data)
270 : some_update_(false), set_(set), p_layout_(layout), device_data_(dev_data) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600271 // Foreach binding, create default descriptors of given type
272 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
273 auto type = p_layout_->GetTypeFromIndex(i);
274 switch (type) {
275 case VK_DESCRIPTOR_TYPE_SAMPLER: {
276 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
277 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
278 if (immut_sampler)
Chris Forbescb621ea2016-05-30 11:47:31 +1200279 descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600280 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200281 descriptors_.emplace_back(new SamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600282 }
283 break;
284 }
285 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
286 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
287 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
288 if (immut)
Chris Forbescb621ea2016-05-30 11:47:31 +1200289 descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600290 else
Chris Forbescb621ea2016-05-30 11:47:31 +1200291 descriptors_.emplace_back(new ImageSamplerDescriptor());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600292 }
293 break;
294 }
295 // ImageDescriptors
296 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
297 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
298 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
299 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200300 descriptors_.emplace_back(new ImageDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600301 break;
302 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
303 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
304 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200305 descriptors_.emplace_back(new TexelDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600306 break;
307 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
308 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
309 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
310 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
311 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Chris Forbescb621ea2016-05-30 11:47:31 +1200312 descriptors_.emplace_back(new BufferDescriptor(type));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600313 break;
314 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600315 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600316 break;
317 }
318 }
319}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600320
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600321cvdescriptorset::DescriptorSet::~DescriptorSet() {
322 InvalidateBoundCmdBuffers();
323 // Remove link to any cmd buffers
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600324 for (auto cb : cb_bindings) {
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600325 for (uint32_t i=0; i<VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
326 cb->lastBound[i].uniqueBoundSets.erase(this);
327 }
328 }
329}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600330// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
331bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
332 return layout->IsCompatible(p_layout_, error);
333}
334// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
335// This includes validating that all descriptors in the given bindings are updated,
336// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
337// Return true if state is acceptable, or false and write an error message into error string
Chris Forbesc7090a82016-07-25 18:10:41 +1200338bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600339 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
iostrows5eb97652016-06-02 15:56:26 +0200340 auto dyn_offset_index = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200341 for (auto binding_pair : bindings) {
342 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600343 if (!p_layout_->HasBinding(binding)) {
344 std::stringstream error_str;
345 error_str << "Attempting to validate DrawState for binding #" << binding
346 << " which is an invalid binding for this descriptor set.";
347 *error = error_str.str();
348 return false;
349 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600350 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600351 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600352 // Nothing to do for strictly immutable sampler
353 } else {
354 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600355 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600356 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600357 std::stringstream error_str;
358 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
359 << " is being used in draw but has not been updated.";
360 *error = error_str.str();
361 return false;
362 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600363 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600364 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600365 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600366 auto buffer_node = getBufferNode(device_data_, buffer);
367 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600368 std::stringstream error_str;
369 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
370 << " references invalid buffer " << buffer << ".";
371 *error = error_str.str();
372 return false;
373 } else {
Tobin Ehlis997b2582016-06-02 08:43:37 -0600374 auto mem_entry = getMemObjInfo(device_data_, buffer_node->mem);
375 if (!mem_entry) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600376 std::stringstream error_str;
377 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600378 << " uses buffer " << buffer << " that references invalid memory " << buffer_node->mem
379 << ".";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600380 *error = error_str.str();
381 return false;
382 }
383 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600384 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600385 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600386 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600387 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
388 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600389 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
390 if (VK_WHOLE_SIZE == range) {
391 if ((dyn_offset + desc_offset) > buffer_size) {
392 std::stringstream error_str;
393 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
394 << " uses buffer " << buffer
395 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
396 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
397 << buffer_size << ".";
398 *error = error_str.str();
399 return false;
400 }
401 } else {
402 if ((dyn_offset + desc_offset + range) > buffer_size) {
403 std::stringstream error_str;
404 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
405 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
406 << " combined with offset " << desc_offset << " and range " << range
407 << " that oversteps the buffer size of " << buffer_size << ".";
408 *error = error_str.str();
409 return false;
410 }
411 }
412 }
413 }
414 }
415 }
416 }
417 }
418 return true;
419}
420// For given bindings, place any update buffers or images into the passed-in unordered_sets
Chris Forbesc7090a82016-07-25 18:10:41 +1200421uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600422 std::unordered_set<VkBuffer> *buffer_set,
423 std::unordered_set<VkImageView> *image_set) const {
424 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200425 for (auto binding_pair : bindings) {
426 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600427 // If a binding doesn't exist, skip it
428 if (!p_layout_->HasBinding(binding)) {
429 continue;
430 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600431 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600432 if (descriptors_[start_idx]->IsStorage()) {
433 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600434 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600435 if (descriptors_[start_idx + i]->updated) {
436 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600437 num_updates++;
438 }
439 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600440 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600441 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600442 if (descriptors_[start_idx + i]->updated) {
443 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -0600444 auto bv_info = getBufferViewInfo(device_data_, bufferview);
445 if (bv_info) {
446 buffer_set->insert(bv_info->buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600447 num_updates++;
448 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600449 }
450 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600451 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600452 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600453 if (descriptors_[start_idx + i]->updated) {
454 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600455 num_updates++;
456 }
457 }
458 }
459 }
460 }
461 return num_updates;
462}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600463// Set is being deleted or updates so invalidate all bound cmd buffers
464void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600465 core_validation::invalidateCommandBuffers(cb_bindings,
466 {reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600467}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600468// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600469void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600470 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
471 // perform update
472 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
473 descriptors_[start_idx + di]->WriteUpdate(update, di);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600474 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600475 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600476 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600477
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600478 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600479}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600480// Validate Copy update
481bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
482 const DescriptorSet *src_set, std::string *error) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600483 // Verify idle ds
484 if (in_use.load()) {
485 std::stringstream error_str;
486 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
487 << " that is in use by a command buffer.";
488 *error = error_str.str();
489 return false;
490 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600491 if (!p_layout_->HasBinding(update->dstBinding)) {
492 std::stringstream error_str;
493 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
494 *error = error_str.str();
495 return false;
496 }
497 if (!src_set->HasBinding(update->srcBinding)) {
498 std::stringstream error_str;
499 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
500 *error = error_str.str();
501 return false;
502 }
503 // src & dst set bindings are valid
504 // Check bounds of src & dst
505 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
506 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
507 // SRC update out of bounds
508 std::stringstream error_str;
509 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
510 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
511 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
512 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
513 *error = error_str.str();
514 return false;
515 }
516 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
517 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
518 // DST update out of bounds
519 std::stringstream error_str;
520 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
521 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
522 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
523 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
524 *error = error_str.str();
525 return false;
526 }
527 // Check that types match
528 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
529 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
530 if (src_type != dst_type) {
531 std::stringstream error_str;
532 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
533 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
534 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
535 *error = error_str.str();
536 return false;
537 }
538 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600539 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
540 "copy update from", src_set->GetSet(), error)) ||
541 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
542 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600543 return false;
544 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600545 // First make sure source descriptors are updated
546 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
547 if (!src_set->descriptors_[src_start_idx + i]) {
548 std::stringstream error_str;
549 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
550 << update->srcArrayElement + i << " has not been updated.";
551 *error = error_str.str();
552 return false;
553 }
554 }
555 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600556 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600557 return false;
558
559 // All checks passed so update is good
560 return true;
561}
562// Perform Copy update
563void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600564 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
565 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600566 // Update parameters all look good so perform update
567 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600568 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600569 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600570 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600571 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600572
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600573 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600574}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600575
Tobin Ehlis300888c2016-05-18 13:43:26 -0600576cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600577 updated = false;
578 descriptor_class = PlainSampler;
579};
580
Tobin Ehlis300888c2016-05-18 13:43:26 -0600581cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600582 updated = false;
583 descriptor_class = PlainSampler;
584 if (immut) {
585 sampler_ = *immut;
586 immutable_ = true;
587 updated = true;
588 }
589}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600590// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
591bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
592 return (getSamplerNode(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600593}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600594
Tobin Ehlis554bf382016-05-24 11:14:43 -0600595bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600596 const core_validation::layer_data *dev_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600597 std::string *error) {
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600598 auto iv_data = getImageViewData(dev_data, image_view);
599 if (!iv_data) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600600 std::stringstream error_str;
601 error_str << "Invalid VkImageView: " << image_view;
602 *error = error_str.str();
603 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600604 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600605 // Note that when an imageview is created, we validated that memory is bound so no need to re-check here
Tobin Ehlis1809f912016-05-25 09:24:36 -0600606 // Validate that imageLayout is compatible with aspect_mask and image format
607 // and validate that image usage bits are correct for given usage
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600608 VkImageAspectFlags aspect_mask = iv_data->subresourceRange.aspectMask;
609 VkImage image = iv_data->image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600610 VkFormat format = VK_FORMAT_MAX_ENUM;
611 VkImageUsageFlags usage = 0;
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600612 auto image_node = getImageNode(dev_data, image);
613 if (image_node) {
614 format = image_node->createInfo.format;
615 usage = image_node->createInfo.usage;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600616 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600617 // Also need to check the swapchains.
Tobin Ehlis969a5262016-06-02 12:13:32 -0600618 auto swapchain = getSwapchainFromImage(dev_data, image);
619 if (swapchain) {
Tobin Ehlis4e380592016-06-02 12:41:47 -0600620 auto swapchain_node = getSwapchainNode(dev_data, swapchain);
621 if (swapchain_node) {
622 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600623 }
624 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600625 }
626 // First validate that format and layout are compatible
627 if (format == VK_FORMAT_MAX_ENUM) {
628 std::stringstream error_str;
629 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
630 *error = error_str.str();
631 return false;
632 }
633 bool ds = vk_format_is_depth_or_stencil(format);
634 switch (image_layout) {
635 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
636 // Only Color bit must be set
637 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600638 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600639 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
640 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis554bf382016-05-24 11:14:43 -0600641 *error = error_str.str();
642 return false;
643 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600644 // format must NOT be DS
645 if (ds) {
646 std::stringstream error_str;
647 error_str << "ImageView (" << image_view
648 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
649 << string_VkFormat(format) << " which is not a color format.";
650 *error = error_str.str();
651 return false;
652 }
653 break;
654 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
655 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
656 // Depth or stencil bit must be set, but both must NOT be set
657 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
658 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
659 // both must NOT be set
660 std::stringstream error_str;
661 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
662 *error = error_str.str();
663 return false;
664 }
665 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
666 // Neither were set
667 std::stringstream error_str;
668 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
669 << " but does not have STENCIL or DEPTH aspects set";
670 *error = error_str.str();
671 return false;
672 }
673 // format must be DS
674 if (!ds) {
675 std::stringstream error_str;
676 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
677 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
678 *error = error_str.str();
679 return false;
680 }
681 break;
682 default:
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600683 // For other layouts if the source is ds image, both aspect bits must not be set
684 if (ds) {
685 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
686 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
687 // both must NOT be set
688 std::stringstream error_str;
689 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
690 << " and is using depth/stencil image of format " << string_VkFormat(format)
691 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
692 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
693 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
694 "reads respectively.";
695 *error = error_str.str();
696 return false;
697 }
698 }
699 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600700 break;
701 }
702 // Now validate that usage flags are correctly set for given type of update
703 std::string error_usage_bit;
704 switch (type) {
705 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
706 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
707 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
708 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
709 }
710 break;
711 }
712 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
713 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
714 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
715 }
716 break;
717 }
718 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
719 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
720 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
721 }
722 break;
723 }
724 default:
725 break;
726 }
727 if (!error_usage_bit.empty()) {
728 std::stringstream error_str;
729 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
730 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
731 << error_usage_bit << " set.";
732 *error = error_str.str();
733 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600734 }
735 return true;
736}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600737
Tobin Ehlis300888c2016-05-18 13:43:26 -0600738void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
739 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600740 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600741}
742
Tobin Ehlis300888c2016-05-18 13:43:26 -0600743void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600744 if (!immutable_) {
745 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600746 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600747 }
748 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600749}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600750
Tobin Ehlis300888c2016-05-18 13:43:26 -0600751cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
752 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600753 updated = false;
754 descriptor_class = ImageSampler;
755}
756
Tobin Ehlis300888c2016-05-18 13:43:26 -0600757cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
758 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600759 updated = false;
760 descriptor_class = ImageSampler;
761 if (immut) {
762 sampler_ = *immut;
763 immutable_ = true;
764 updated = true;
765 }
766}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600767
Tobin Ehlis300888c2016-05-18 13:43:26 -0600768void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600769 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600770 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600771 sampler_ = image_info.sampler;
772 image_view_ = image_info.imageView;
773 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600774}
775
Tobin Ehlis300888c2016-05-18 13:43:26 -0600776void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600777 if (!immutable_) {
778 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600779 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600780 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600781 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
782 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600783 updated = true;
784 image_view_ = image_view;
785 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600786}
787
Tobin Ehlis300888c2016-05-18 13:43:26 -0600788cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
789 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600790 updated = false;
791 descriptor_class = Image;
792 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
793 storage_ = true;
794};
795
Tobin Ehlis300888c2016-05-18 13:43:26 -0600796void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600797 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600798 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600799 image_view_ = image_info.imageView;
800 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600801}
802
Tobin Ehlis300888c2016-05-18 13:43:26 -0600803void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600804 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
805 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600806 updated = true;
807 image_view_ = image_view;
808 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600809}
810
Tobin Ehlis300888c2016-05-18 13:43:26 -0600811cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
812 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600813 updated = false;
814 descriptor_class = GeneralBuffer;
815 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
816 dynamic_ = true;
817 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
818 storage_ = true;
819 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
820 dynamic_ = true;
821 storage_ = true;
822 }
823}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600824void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600825 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600826 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600827 buffer_ = buffer_info.buffer;
828 offset_ = buffer_info.offset;
829 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600830}
831
Tobin Ehlis300888c2016-05-18 13:43:26 -0600832void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
833 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600834 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600835 buffer_ = buff_desc->buffer_;
836 offset_ = buff_desc->offset_;
837 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600838}
839
Tobin Ehlis300888c2016-05-18 13:43:26 -0600840cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600841 updated = false;
842 descriptor_class = TexelBuffer;
843 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
844 storage_ = true;
845};
Tobin Ehlis56a30942016-05-19 08:00:00 -0600846
Tobin Ehlis300888c2016-05-18 13:43:26 -0600847void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600848 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600849 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600850}
851
Tobin Ehlis300888c2016-05-18 13:43:26 -0600852void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
853 updated = true;
854 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
855}
856// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
857// sets, and then calls their respective Validate[Write|Copy]Update functions.
858// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
859// be skipped, then true is returned.
860// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600861bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
862 const core_validation::layer_data *dev_data, uint32_t write_count,
863 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
864 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600865 bool skip_call = false;
866 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600867 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600868 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600869 auto set_node = core_validation::getSetNode(dev_data, dest_set);
870 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600871 skip_call |=
872 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 &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600874 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
875 reinterpret_cast<uint64_t &>(dest_set));
876 } else {
877 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600878 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600879 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis585f66d2016-07-01 18:23:58 -0600880 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_WRITE_UPDATE, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600881 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
882 " with error: %s",
883 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
884 }
885 }
886 }
887 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600888 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600889 auto dst_set = p_cds[i].dstSet;
890 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600891 auto src_node = core_validation::getSetNode(dev_data, src_set);
892 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
893 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600894 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 -0600895 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600896 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
897 " that has not been allocated.",
898 reinterpret_cast<uint64_t &>(src_set));
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600899 } else if (!dst_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600900 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 -0600901 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600902 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
903 " that has not been allocated.",
904 reinterpret_cast<uint64_t &>(dst_set));
905 } else {
906 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600907 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600908 skip_call |=
909 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
Tobin Ehlis585f66d2016-07-01 18:23:58 -0600910 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_COPY_UPDATE, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600911 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
912 " to Descriptor Set 0x%" PRIx64 " with error: %s",
913 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
914 }
915 }
916 }
917 return skip_call;
918}
919// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
920// sets, and then calls their respective Perform[Write|Copy]Update functions.
921// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
922// with the same set of updates.
923// This is split from the validate code to allow validation prior to calling down the chain, and then update after
924// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600925void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
926 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
927 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600928 // Write updates first
929 uint32_t i = 0;
930 for (i = 0; i < write_count; ++i) {
931 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600932 auto set_node = core_validation::getSetNode(dev_data, dest_set);
933 if (set_node) {
934 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600935 }
936 }
937 // Now copy updates
938 for (i = 0; i < copy_count; ++i) {
939 auto dst_set = p_cds[i].dstSet;
940 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600941 auto src_node = core_validation::getSetNode(dev_data, src_set);
942 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
943 if (src_node && dst_node) {
944 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600945 }
946 }
947}
948// Validate the state for a given write update but don't actually perform the update
949// If an error would occur for this update, return false and fill in details in error_msg string
950bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
951 std::string *error_msg) {
952 // Verify idle ds
953 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600954 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600955 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
956 << " that is in use by a command buffer.";
957 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600958 return false;
959 }
Tobin Ehlis300888c2016-05-18 13:43:26 -0600960 // Verify dst binding exists
961 if (!p_layout_->HasBinding(update->dstBinding)) {
962 std::stringstream error_str;
963 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
964 *error_msg = error_str.str();
965 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -0600966 }
967 // We know that binding is valid, verify update and do update on each descriptor
968 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
969 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
970 if (type != update->descriptorType) {
971 std::stringstream error_str;
972 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
973 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
974 *error_msg = error_str.str();
975 return false;
976 }
977 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
978 std::stringstream error_str;
979 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
980 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
981 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
982 << " combined with update array element offset of " << update->dstArrayElement
983 << " oversteps the size of this descriptor set.";
984 *error_msg = error_str.str();
985 return false;
986 }
987 // Verify consecutive bindings match (if needed)
988 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
989 set_, error_msg))
990 return false;
991 // Update is within bounds and consistent so last step is to validate update contents
992 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
993 std::stringstream error_str;
994 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
995 << " failed with error message: " << error_msg->c_str();
996 *error_msg = error_str.str();
997 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600998 }
999 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001000 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001001}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001002// For the given buffer, verify that its creation parameters are appropriate for the given type
1003// If there's an error, update the error string with details and return false, else return true
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001004bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_NODE const *buffer_node, VkDescriptorType type,
1005 std::string *error) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001006 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001007 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001008 std::string error_usage_bit;
1009 switch (type) {
1010 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1011 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
1012 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1013 }
1014 break;
1015 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1016 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1017 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1018 }
1019 break;
1020 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1021 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1022 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1023 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1024 }
1025 break;
1026 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1027 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1028 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1029 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1030 }
1031 break;
1032 default:
1033 break;
1034 }
1035 if (!error_usage_bit.empty()) {
1036 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001037 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1038 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1039 << error_usage_bit << " set.";
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001040 *error = error_str.str();
1041 return false;
1042 }
1043 return true;
1044}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001045// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1046// 1. buffer is valid
1047// 2. buffer was created with correct usage flags
1048// 3. offset is less than buffer size
1049// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
1050// If there's an error, update the error string with details and return false, else return true
1051bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
1052 std::string *error) const {
1053 // First make sure that buffer is valid
1054 auto buffer_node = getBufferNode(device_data_, buffer_info->buffer);
1055 if (!buffer_node) {
1056 std::stringstream error_str;
1057 error_str << "Invalid VkBuffer: " << buffer_info->buffer;
1058 *error = error_str.str();
1059 return false;
1060 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001061 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()"))
1062 return false;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001063 // Verify usage bits
1064 if (!ValidateBufferUsage(buffer_node, type, error)) {
1065 // error will have been updated by ValidateBufferUsage()
1066 return false;
1067 }
1068 // offset must be less than buffer size
1069 if (buffer_info->offset > buffer_node->createInfo.size) {
1070 std::stringstream error_str;
1071 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than buffer " << buffer_node->buffer
1072 << " size of " << buffer_node->createInfo.size;
1073 *error = error_str.str();
1074 return false;
1075 }
1076 if (buffer_info->range != VK_WHOLE_SIZE) {
1077 // Range must be VK_WHOLE_SIZE or > 0
1078 if (!buffer_info->range) {
1079 std::stringstream error_str;
1080 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
1081 *error = error_str.str();
1082 return false;
1083 }
1084 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1085 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
1086 std::stringstream error_str;
1087 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1088 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
1089 *error = error_str.str();
1090 return false;
1091 }
1092 }
1093 return true;
1094}
1095
Tobin Ehlis300888c2016-05-18 13:43:26 -06001096// Verify that the contents of the update are ok, but don't perform actual update
1097bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
1098 std::string *error) const {
1099 switch (update->descriptorType) {
1100 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1101 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1102 // Validate image
1103 auto image_view = update->pImageInfo[di].imageView;
1104 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis4e380592016-06-02 12:41:47 -06001105 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001106 std::stringstream error_str;
1107 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1108 *error = error_str.str();
1109 return false;
1110 }
1111 }
1112 // Intentional fall-through to validate sampler
1113 }
1114 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1115 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1116 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001117 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001118 std::stringstream error_str;
1119 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1120 << update->pImageInfo[di].sampler << ".";
1121 *error = error_str.str();
1122 return false;
1123 }
1124 } else {
1125 // TODO : Warn here
1126 }
1127 }
1128 break;
1129 }
1130 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1131 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1132 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1133 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1134 auto image_view = update->pImageInfo[di].imageView;
1135 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis4e380592016-06-02 12:41:47 -06001136 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001137 std::stringstream error_str;
1138 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1139 *error = error_str.str();
1140 return false;
1141 }
1142 }
1143 break;
1144 }
1145 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1146 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1147 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1148 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis424859c2016-06-02 09:43:11 -06001149 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1150 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001151 std::stringstream error_str;
1152 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1153 *error = error_str.str();
1154 return false;
1155 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001156 auto buffer = bv_info->buffer;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001157 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), update->descriptorType, error)) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001158 std::stringstream error_str;
1159 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str();
1160 *error = error_str.str();
1161 return false;
1162 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001163 }
1164 break;
1165 }
1166 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1167 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1168 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1169 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1170 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001171 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001172 std::stringstream error_str;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001173 error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001174 *error = error_str.str();
1175 return false;
1176 }
1177 }
1178 break;
1179 }
1180 default:
1181 assert(0); // We've already verified update type so should never get here
1182 break;
1183 }
1184 // All checks passed so update contents are good
1185 return true;
1186}
1187// Verify that the contents of the update are ok, but don't perform actual update
1188bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001189 VkDescriptorType type, uint32_t index, std::string *error) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001190 switch (src_set->descriptors_[index]->descriptor_class) {
1191 case PlainSampler: {
1192 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1193 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1194 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001195 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001196 std::stringstream error_str;
1197 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1198 *error = error_str.str();
1199 return false;
1200 }
1201 } else {
1202 // TODO : Warn here
1203 }
1204 }
1205 break;
1206 }
1207 case ImageSampler: {
1208 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1209 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1210 // First validate sampler
1211 if (!img_samp_desc->IsImmutableSampler()) {
1212 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001213 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001214 std::stringstream error_str;
1215 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1216 *error = error_str.str();
1217 return false;
1218 }
1219 } else {
1220 // TODO : Warn here
1221 }
1222 // Validate image
1223 auto image_view = img_samp_desc->GetImageView();
1224 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlis4e380592016-06-02 12:41:47 -06001225 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001226 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001227 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001228 *error = error_str.str();
1229 return false;
1230 }
1231 }
1232 }
1233 case Image: {
1234 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1235 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1236 auto image_view = img_desc->GetImageView();
1237 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlis4e380592016-06-02 12:41:47 -06001238 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001239 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001240 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001241 *error = error_str.str();
1242 return false;
1243 }
1244 }
1245 break;
1246 }
1247 case TexelBuffer: {
1248 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1249 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -06001250 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1251 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001252 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001253 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1254 *error = error_str.str();
1255 return false;
1256 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001257 auto buffer = bv_info->buffer;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001258 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), type, error)) {
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001259 std::stringstream error_str;
1260 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001261 *error = error_str.str();
1262 return false;
1263 }
1264 }
1265 break;
1266 }
1267 case GeneralBuffer: {
1268 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1269 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001270 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), type, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001271 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001272 error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001273 *error = error_str.str();
1274 return false;
1275 }
1276 }
1277 break;
1278 }
1279 default:
1280 assert(0); // We've already verified update type so should never get here
1281 break;
1282 }
1283 // All checks passed so update contents are good
1284 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001285}
Tobin Ehlisee471462016-05-26 11:21:59 -06001286// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlis815e8132016-06-02 13:02:17 -06001287bool cvdescriptorset::ValidateAllocateDescriptorSets(const debug_report_data *report_data,
1288 const VkDescriptorSetAllocateInfo *p_alloc_info,
1289 const core_validation::layer_data *dev_data,
1290 AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001291 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001292
1293 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001294 auto layout = getDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1295 if (!layout) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001296 skip_call |=
1297 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1298 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1299 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1300 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001301 } else {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001302 ds_data->layout_nodes[i] = layout;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001303 // Count total descriptors required per type
Tobin Ehlis815e8132016-06-02 13:02:17 -06001304 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1305 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001306 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1307 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1308 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001309 }
1310 }
Tobin Ehlis815e8132016-06-02 13:02:17 -06001311 auto pool_node = getPoolNode(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001312 // Track number of descriptorSets allowable in this pool
1313 if (pool_node->availableSets < p_alloc_info->descriptorSetCount) {
1314 skip_call |= log_msg(
1315 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1316 reinterpret_cast<uint64_t &>(pool_node->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY, "DS",
1317 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64 ". This pool only has %d descriptorSets remaining.",
1318 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_node->pool), pool_node->availableSets);
1319 }
1320 // Determine whether descriptor counts are satisfiable
1321 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1322 if (ds_data->required_descriptors_by_type[i] > pool_node->availableDescriptorTypeCount[i]) {
1323 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1324 reinterpret_cast<const uint64_t &>(pool_node->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1325 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1326 ". This pool only has %d descriptors of this type remaining.",
1327 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
1328 reinterpret_cast<uint64_t &>(pool_node->pool), pool_node->availableDescriptorTypeCount[i]);
Tobin Ehlisee471462016-05-26 11:21:59 -06001329 }
1330 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001331
Tobin Ehlisee471462016-05-26 11:21:59 -06001332 return skip_call;
1333}
1334// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001335void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1336 const VkDescriptorSet *descriptor_sets,
1337 const AllocateDescriptorSetsData *ds_data,
1338 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map,
1339 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
1340 const core_validation::layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001341 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001342 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001343 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001344 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1345 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1346 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001347 /* Create tracking object for each descriptor set; insert into
1348 * global map and the pool's set.
1349 */
1350 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis4e380592016-06-02 12:41:47 -06001351 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], ds_data->layout_nodes[i], dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001352
1353 pool_state->sets.insert(new_ds);
1354 new_ds->in_use.store(0);
1355 (*set_map)[descriptor_sets[i]] = new_ds;
1356 }
1357}