blob: 1f72d447019934c6c0ba6353da793021014e6fb7 [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
324 for (auto cb : bound_cmd_buffers_) {
325 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
338bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
339 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
iostrows5eb97652016-06-02 15:56:26 +0200340 auto dyn_offset_index = 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600341 for (auto binding : bindings) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600342 if (!p_layout_->HasBinding(binding)) {
343 std::stringstream error_str;
344 error_str << "Attempting to validate DrawState for binding #" << binding
345 << " which is an invalid binding for this descriptor set.";
346 *error = error_str.str();
347 return false;
348 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600349 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600350 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600351 // Nothing to do for strictly immutable sampler
352 } else {
353 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600354 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600355 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600356 std::stringstream error_str;
357 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
358 << " is being used in draw but has not been updated.";
359 *error = error_str.str();
360 return false;
361 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600362 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600363 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600364 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600365 auto buffer_node = getBufferNode(device_data_, buffer);
366 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600367 std::stringstream error_str;
368 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
369 << " references invalid buffer " << buffer << ".";
370 *error = error_str.str();
371 return false;
372 } else {
Tobin Ehlis997b2582016-06-02 08:43:37 -0600373 auto mem_entry = getMemObjInfo(device_data_, buffer_node->mem);
374 if (!mem_entry) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600375 std::stringstream error_str;
376 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600377 << " uses buffer " << buffer << " that references invalid memory " << buffer_node->mem
378 << ".";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600379 *error = error_str.str();
380 return false;
381 }
382 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600383 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600384 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600385 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600386 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
387 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600388 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
389 if (VK_WHOLE_SIZE == range) {
390 if ((dyn_offset + desc_offset) > buffer_size) {
391 std::stringstream error_str;
392 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
393 << " uses buffer " << buffer
394 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
395 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
396 << buffer_size << ".";
397 *error = error_str.str();
398 return false;
399 }
400 } else {
401 if ((dyn_offset + desc_offset + range) > buffer_size) {
402 std::stringstream error_str;
403 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
404 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
405 << " combined with offset " << desc_offset << " and range " << range
406 << " that oversteps the buffer size of " << buffer_size << ".";
407 *error = error_str.str();
408 return false;
409 }
410 }
411 }
412 }
413 }
414 }
415 }
416 }
417 return true;
418}
419// For given bindings, place any update buffers or images into the passed-in unordered_sets
420uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
421 std::unordered_set<VkBuffer> *buffer_set,
422 std::unordered_set<VkImageView> *image_set) const {
423 auto num_updates = 0;
424 for (auto binding : bindings) {
Tobin Ehlis58c59582016-06-21 12:34:33 -0600425 // If a binding doesn't exist, skip it
426 if (!p_layout_->HasBinding(binding)) {
427 continue;
428 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600429 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600430 if (descriptors_[start_idx]->IsStorage()) {
431 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600432 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600433 if (descriptors_[start_idx + i]->updated) {
434 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600435 num_updates++;
436 }
437 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600438 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600439 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600440 if (descriptors_[start_idx + i]->updated) {
441 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -0600442 auto bv_info = getBufferViewInfo(device_data_, bufferview);
443 if (bv_info) {
444 buffer_set->insert(bv_info->buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600445 num_updates++;
446 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600447 }
448 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600449 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600450 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600451 if (descriptors_[start_idx + i]->updated) {
452 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600453 num_updates++;
454 }
455 }
456 }
457 }
458 }
459 return num_updates;
460}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600461// Set is being deleted or updates so invalidate all bound cmd buffers
462void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
463 for (auto cb_node : bound_cmd_buffers_) {
464 cb_node->state = CB_INVALID;
465 }
466}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600467// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600468void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600469 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
470 // perform update
471 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
472 descriptors_[start_idx + di]->WriteUpdate(update, di);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600473 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600474 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600475 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600476
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600477 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600478}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600479// Validate Copy update
480bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
481 const DescriptorSet *src_set, std::string *error) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600482 // Verify idle ds
483 if (in_use.load()) {
484 std::stringstream error_str;
485 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
486 << " that is in use by a command buffer.";
487 *error = error_str.str();
488 return false;
489 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600490 if (!p_layout_->HasBinding(update->dstBinding)) {
491 std::stringstream error_str;
492 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
493 *error = error_str.str();
494 return false;
495 }
496 if (!src_set->HasBinding(update->srcBinding)) {
497 std::stringstream error_str;
498 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
499 *error = error_str.str();
500 return false;
501 }
502 // src & dst set bindings are valid
503 // Check bounds of src & dst
504 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
505 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
506 // SRC update out of bounds
507 std::stringstream error_str;
508 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
509 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
510 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
511 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
512 *error = error_str.str();
513 return false;
514 }
515 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
516 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
517 // DST update out of bounds
518 std::stringstream error_str;
519 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
520 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
521 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
522 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
523 *error = error_str.str();
524 return false;
525 }
526 // Check that types match
527 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
528 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
529 if (src_type != dst_type) {
530 std::stringstream error_str;
531 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
532 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
533 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
534 *error = error_str.str();
535 return false;
536 }
537 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600538 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
539 "copy update from", src_set->GetSet(), error)) ||
540 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
541 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600542 return false;
543 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600544 // First make sure source descriptors are updated
545 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
546 if (!src_set->descriptors_[src_start_idx + i]) {
547 std::stringstream error_str;
548 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
549 << update->srcArrayElement + i << " has not been updated.";
550 *error = error_str.str();
551 return false;
552 }
553 }
554 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600555 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600556 return false;
557
558 // All checks passed so update is good
559 return true;
560}
561// Perform Copy update
562void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600563 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
564 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600565 // Update parameters all look good so perform update
566 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600567 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600568 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600569 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600570 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600571
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600572 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600573}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600574
Tobin Ehlis300888c2016-05-18 13:43:26 -0600575cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600576 updated = false;
577 descriptor_class = PlainSampler;
578};
579
Tobin Ehlis300888c2016-05-18 13:43:26 -0600580cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600581 updated = false;
582 descriptor_class = PlainSampler;
583 if (immut) {
584 sampler_ = *immut;
585 immutable_ = true;
586 updated = true;
587 }
588}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600589// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
590bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
591 return (getSamplerNode(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600592}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600593
Tobin Ehlis554bf382016-05-24 11:14:43 -0600594bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600595 const core_validation::layer_data *dev_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600596 std::string *error) {
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600597 auto iv_data = getImageViewData(dev_data, image_view);
598 if (!iv_data) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600599 std::stringstream error_str;
600 error_str << "Invalid VkImageView: " << image_view;
601 *error = error_str.str();
602 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600603 }
604 // Validate that imageLayout is compatible with aspect_mask and image format
605 // and validate that image usage bits are correct for given usage
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600606 VkImageAspectFlags aspect_mask = iv_data->subresourceRange.aspectMask;
607 VkImage image = iv_data->image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600608 VkFormat format = VK_FORMAT_MAX_ENUM;
609 VkImageUsageFlags usage = 0;
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600610 auto image_node = getImageNode(dev_data, image);
611 if (image_node) {
612 format = image_node->createInfo.format;
613 usage = image_node->createInfo.usage;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600614 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600615 // Also need to check the swapchains.
Tobin Ehlis969a5262016-06-02 12:13:32 -0600616 auto swapchain = getSwapchainFromImage(dev_data, image);
617 if (swapchain) {
Tobin Ehlis4e380592016-06-02 12:41:47 -0600618 auto swapchain_node = getSwapchainNode(dev_data, swapchain);
619 if (swapchain_node) {
620 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600621 }
622 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600623 }
624 // First validate that format and layout are compatible
625 if (format == VK_FORMAT_MAX_ENUM) {
626 std::stringstream error_str;
627 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
628 *error = error_str.str();
629 return false;
630 }
631 bool ds = vk_format_is_depth_or_stencil(format);
632 switch (image_layout) {
633 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
634 // Only Color bit must be set
635 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600636 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600637 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
638 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis554bf382016-05-24 11:14:43 -0600639 *error = error_str.str();
640 return false;
641 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600642 // format must NOT be DS
643 if (ds) {
644 std::stringstream error_str;
645 error_str << "ImageView (" << image_view
646 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
647 << string_VkFormat(format) << " which is not a color format.";
648 *error = error_str.str();
649 return false;
650 }
651 break;
652 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
653 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
654 // Depth or stencil bit must be set, but both must NOT be set
655 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
656 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
657 // both must NOT be set
658 std::stringstream error_str;
659 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
660 *error = error_str.str();
661 return false;
662 }
663 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
664 // Neither were set
665 std::stringstream error_str;
666 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
667 << " but does not have STENCIL or DEPTH aspects set";
668 *error = error_str.str();
669 return false;
670 }
671 // format must be DS
672 if (!ds) {
673 std::stringstream error_str;
674 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
675 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
676 *error = error_str.str();
677 return false;
678 }
679 break;
680 default:
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600681 // For other layouts if the source is ds image, both aspect bits must not be set
682 if (ds) {
683 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
684 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
685 // both must NOT be set
686 std::stringstream error_str;
687 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
688 << " and is using depth/stencil image of format " << string_VkFormat(format)
689 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
690 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
691 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
692 "reads respectively.";
693 *error = error_str.str();
694 return false;
695 }
696 }
697 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600698 break;
699 }
700 // Now validate that usage flags are correctly set for given type of update
701 std::string error_usage_bit;
702 switch (type) {
703 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
704 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
705 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
706 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
707 }
708 break;
709 }
710 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
711 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
712 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
713 }
714 break;
715 }
716 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
717 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
718 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
719 }
720 break;
721 }
722 default:
723 break;
724 }
725 if (!error_usage_bit.empty()) {
726 std::stringstream error_str;
727 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
728 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
729 << error_usage_bit << " set.";
730 *error = error_str.str();
731 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600732 }
733 return true;
734}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600735
Tobin Ehlis300888c2016-05-18 13:43:26 -0600736void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
737 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600738 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600739}
740
Tobin Ehlis300888c2016-05-18 13:43:26 -0600741void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600742 if (!immutable_) {
743 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600744 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600745 }
746 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600747}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600748
Tobin Ehlis300888c2016-05-18 13:43:26 -0600749cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
750 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600751 updated = false;
752 descriptor_class = ImageSampler;
753}
754
Tobin Ehlis300888c2016-05-18 13:43:26 -0600755cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
756 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600757 updated = false;
758 descriptor_class = ImageSampler;
759 if (immut) {
760 sampler_ = *immut;
761 immutable_ = true;
762 updated = true;
763 }
764}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600765
Tobin Ehlis300888c2016-05-18 13:43:26 -0600766void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600767 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600768 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600769 sampler_ = image_info.sampler;
770 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::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600775 if (!immutable_) {
776 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600777 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600778 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600779 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
780 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600781 updated = true;
782 image_view_ = image_view;
783 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600784}
785
Tobin Ehlis300888c2016-05-18 13:43:26 -0600786cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
787 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600788 updated = false;
789 descriptor_class = Image;
790 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
791 storage_ = true;
792};
793
Tobin Ehlis300888c2016-05-18 13:43:26 -0600794void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600795 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600796 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600797 image_view_ = image_info.imageView;
798 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600799}
800
Tobin Ehlis300888c2016-05-18 13:43:26 -0600801void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600802 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
803 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600804 updated = true;
805 image_view_ = image_view;
806 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600807}
808
Tobin Ehlis300888c2016-05-18 13:43:26 -0600809cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
810 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600811 updated = false;
812 descriptor_class = GeneralBuffer;
813 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
814 dynamic_ = true;
815 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
816 storage_ = true;
817 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
818 dynamic_ = true;
819 storage_ = true;
820 }
821}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600822void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600823 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600824 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600825 buffer_ = buffer_info.buffer;
826 offset_ = buffer_info.offset;
827 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600828}
829
Tobin Ehlis300888c2016-05-18 13:43:26 -0600830void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
831 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600832 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600833 buffer_ = buff_desc->buffer_;
834 offset_ = buff_desc->offset_;
835 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600836}
837
Tobin Ehlis300888c2016-05-18 13:43:26 -0600838cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600839 updated = false;
840 descriptor_class = TexelBuffer;
841 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
842 storage_ = true;
843};
Tobin Ehlis56a30942016-05-19 08:00:00 -0600844
Tobin Ehlis300888c2016-05-18 13:43:26 -0600845void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600846 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600847 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600848}
849
Tobin Ehlis300888c2016-05-18 13:43:26 -0600850void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
851 updated = true;
852 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
853}
854// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
855// sets, and then calls their respective Validate[Write|Copy]Update functions.
856// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
857// be skipped, then true is returned.
858// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600859bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
860 const core_validation::layer_data *dev_data, uint32_t write_count,
861 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
862 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600863 bool skip_call = false;
864 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600865 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600866 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600867 auto set_node = core_validation::getSetNode(dev_data, dest_set);
868 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600869 skip_call |=
870 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 -0600871 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600872 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
873 reinterpret_cast<uint64_t &>(dest_set));
874 } else {
875 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600876 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600877 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
878 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
879 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
880 " with error: %s",
881 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
882 }
883 }
884 }
885 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600886 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600887 auto dst_set = p_cds[i].dstSet;
888 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600889 auto src_node = core_validation::getSetNode(dev_data, src_set);
890 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
891 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600892 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 -0600893 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600894 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
895 " that has not been allocated.",
896 reinterpret_cast<uint64_t &>(src_set));
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600897 } else if (!dst_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600898 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 -0600899 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -0600900 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
901 " that has not been allocated.",
902 reinterpret_cast<uint64_t &>(dst_set));
903 } else {
904 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600905 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600906 skip_call |=
907 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
908 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
909 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
910 " to Descriptor Set 0x%" PRIx64 " with error: %s",
911 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
912 }
913 }
914 }
915 return skip_call;
916}
917// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
918// sets, and then calls their respective Perform[Write|Copy]Update functions.
919// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
920// with the same set of updates.
921// This is split from the validate code to allow validation prior to calling down the chain, and then update after
922// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600923void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
924 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
925 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600926 // Write updates first
927 uint32_t i = 0;
928 for (i = 0; i < write_count; ++i) {
929 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600930 auto set_node = core_validation::getSetNode(dev_data, dest_set);
931 if (set_node) {
932 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600933 }
934 }
935 // Now copy updates
936 for (i = 0; i < copy_count; ++i) {
937 auto dst_set = p_cds[i].dstSet;
938 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600939 auto src_node = core_validation::getSetNode(dev_data, src_set);
940 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
941 if (src_node && dst_node) {
942 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -0600943 }
944 }
945}
946// Validate the state for a given write update but don't actually perform the update
947// If an error would occur for this update, return false and fill in details in error_msg string
948bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
949 std::string *error_msg) {
950 // Verify idle ds
951 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600952 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600953 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
954 << " that is in use by a command buffer.";
955 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600956 return false;
957 }
Tobin Ehlis300888c2016-05-18 13:43:26 -0600958 // Verify dst binding exists
959 if (!p_layout_->HasBinding(update->dstBinding)) {
960 std::stringstream error_str;
961 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
962 *error_msg = error_str.str();
963 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -0600964 }
965 // We know that binding is valid, verify update and do update on each descriptor
966 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
967 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
968 if (type != update->descriptorType) {
969 std::stringstream error_str;
970 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
971 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
972 *error_msg = error_str.str();
973 return false;
974 }
975 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
976 std::stringstream error_str;
977 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
978 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
979 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
980 << " combined with update array element offset of " << update->dstArrayElement
981 << " oversteps the size of this descriptor set.";
982 *error_msg = error_str.str();
983 return false;
984 }
985 // Verify consecutive bindings match (if needed)
986 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
987 set_, error_msg))
988 return false;
989 // Update is within bounds and consistent so last step is to validate update contents
990 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
991 std::stringstream error_str;
992 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
993 << " failed with error message: " << error_msg->c_str();
994 *error_msg = error_str.str();
995 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600996 }
997 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600998 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600999}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001000// For the given buffer, verify that its creation parameters are appropriate for the given type
1001// If there's an error, update the error string with details and return false, else return true
1002bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkBuffer buffer, VkDescriptorType type, std::string *error) const {
1003 // First make sure that buffer is valid
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001004 auto buffer_node = getBufferNode(device_data_, buffer);
1005 if (!buffer_node) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001006 std::stringstream error_str;
1007 error_str << "Invalid VkBuffer: " << buffer;
1008 *error = error_str.str();
1009 return false;
1010 }
1011 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001012 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001013 std::string error_usage_bit;
1014 switch (type) {
1015 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1016 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
1017 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1018 }
1019 break;
1020 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1021 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1022 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1023 }
1024 break;
1025 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1026 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1027 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1028 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1029 }
1030 break;
1031 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1032 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1033 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1034 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1035 }
1036 break;
1037 default:
1038 break;
1039 }
1040 if (!error_usage_bit.empty()) {
1041 std::stringstream error_str;
1042 error_str << "Buffer (" << buffer << ") with usage mask 0x" << usage << " being used for a descriptor update of type "
1043 << string_VkDescriptorType(type) << " does not have " << error_usage_bit << " set.";
1044 *error = error_str.str();
1045 return false;
1046 }
1047 return true;
1048}
Tobin Ehlis300888c2016-05-18 13:43:26 -06001049// Verify that the contents of the update are ok, but don't perform actual update
1050bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
1051 std::string *error) const {
1052 switch (update->descriptorType) {
1053 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1054 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1055 // Validate image
1056 auto image_view = update->pImageInfo[di].imageView;
1057 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis4e380592016-06-02 12:41:47 -06001058 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001059 std::stringstream error_str;
1060 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1061 *error = error_str.str();
1062 return false;
1063 }
1064 }
1065 // Intentional fall-through to validate sampler
1066 }
1067 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1068 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1069 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001070 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001071 std::stringstream error_str;
1072 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1073 << update->pImageInfo[di].sampler << ".";
1074 *error = error_str.str();
1075 return false;
1076 }
1077 } else {
1078 // TODO : Warn here
1079 }
1080 }
1081 break;
1082 }
1083 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1084 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1085 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1086 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1087 auto image_view = update->pImageInfo[di].imageView;
1088 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis4e380592016-06-02 12:41:47 -06001089 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001090 std::stringstream error_str;
1091 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1092 *error = error_str.str();
1093 return false;
1094 }
1095 }
1096 break;
1097 }
1098 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1099 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1100 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1101 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis424859c2016-06-02 09:43:11 -06001102 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1103 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001104 std::stringstream error_str;
1105 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1106 *error = error_str.str();
1107 return false;
1108 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001109 auto buffer = bv_info->buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001110 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
1111 std::stringstream error_str;
1112 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str();
1113 *error = error_str.str();
1114 return false;
1115 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001116 }
1117 break;
1118 }
1119 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1120 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1121 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1122 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1123 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1124 auto buffer = update->pBufferInfo[di].buffer;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001125 if (!ValidateBufferUpdate(buffer, update->descriptorType, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001126 std::stringstream error_str;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001127 error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001128 *error = error_str.str();
1129 return false;
1130 }
1131 }
1132 break;
1133 }
1134 default:
1135 assert(0); // We've already verified update type so should never get here
1136 break;
1137 }
1138 // All checks passed so update contents are good
1139 return true;
1140}
1141// Verify that the contents of the update are ok, but don't perform actual update
1142bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001143 VkDescriptorType type, uint32_t index, std::string *error) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001144 switch (src_set->descriptors_[index]->descriptor_class) {
1145 case PlainSampler: {
1146 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1147 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1148 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001149 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001150 std::stringstream error_str;
1151 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1152 *error = error_str.str();
1153 return false;
1154 }
1155 } else {
1156 // TODO : Warn here
1157 }
1158 }
1159 break;
1160 }
1161 case ImageSampler: {
1162 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1163 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1164 // First validate sampler
1165 if (!img_samp_desc->IsImmutableSampler()) {
1166 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001167 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001168 std::stringstream error_str;
1169 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1170 *error = error_str.str();
1171 return false;
1172 }
1173 } else {
1174 // TODO : Warn here
1175 }
1176 // Validate image
1177 auto image_view = img_samp_desc->GetImageView();
1178 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlis4e380592016-06-02 12:41:47 -06001179 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001180 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001181 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001182 *error = error_str.str();
1183 return false;
1184 }
1185 }
1186 }
1187 case Image: {
1188 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1189 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1190 auto image_view = img_desc->GetImageView();
1191 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlis4e380592016-06-02 12:41:47 -06001192 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001193 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001194 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001195 *error = error_str.str();
1196 return false;
1197 }
1198 }
1199 break;
1200 }
1201 case TexelBuffer: {
1202 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1203 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis424859c2016-06-02 09:43:11 -06001204 auto bv_info = getBufferViewInfo(device_data_, buffer_view);
1205 if (!bv_info) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001206 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001207 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1208 *error = error_str.str();
1209 return false;
1210 }
Tobin Ehlis424859c2016-06-02 09:43:11 -06001211 auto buffer = bv_info->buffer;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001212 if (!ValidateBufferUpdate(buffer, type, error)) {
1213 std::stringstream error_str;
1214 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001215 *error = error_str.str();
1216 return false;
1217 }
1218 }
1219 break;
1220 }
1221 case GeneralBuffer: {
1222 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1223 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001224 if (!ValidateBufferUpdate(buffer, type, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001225 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001226 error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001227 *error = error_str.str();
1228 return false;
1229 }
1230 }
1231 break;
1232 }
1233 default:
1234 assert(0); // We've already verified update type so should never get here
1235 break;
1236 }
1237 // All checks passed so update contents are good
1238 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001239}
Tobin Ehlisee471462016-05-26 11:21:59 -06001240// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlis815e8132016-06-02 13:02:17 -06001241bool cvdescriptorset::ValidateAllocateDescriptorSets(const debug_report_data *report_data,
1242 const VkDescriptorSetAllocateInfo *p_alloc_info,
1243 const core_validation::layer_data *dev_data,
1244 AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001245 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001246
1247 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001248 auto layout = getDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1249 if (!layout) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001250 skip_call |=
1251 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1252 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1253 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1254 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001255 } else {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001256 ds_data->layout_nodes[i] = layout;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001257 // Count total descriptors required per type
Tobin Ehlis815e8132016-06-02 13:02:17 -06001258 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1259 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001260 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1261 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1262 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001263 }
1264 }
Tobin Ehlis815e8132016-06-02 13:02:17 -06001265 auto pool_node = getPoolNode(dev_data, p_alloc_info->descriptorPool);
1266 if (!pool_node) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001267 skip_call |=
1268 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1269 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool), __LINE__, DRAWSTATE_INVALID_POOL, "DS",
1270 "Unable to find pool node for pool 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1271 reinterpret_cast<const uint64_t &>(p_alloc_info->descriptorPool));
1272 } else { // Make sure pool has all the available descriptors before calling down chain
1273 // Track number of descriptorSets allowable in this pool
Tobin Ehlis815e8132016-06-02 13:02:17 -06001274 if (pool_node->availableSets < p_alloc_info->descriptorSetCount) {
1275 skip_call |=
1276 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1277 reinterpret_cast<uint64_t &>(pool_node->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY, "DS",
1278 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64
1279 ". This pool only has %d descriptorSets remaining.",
1280 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_node->pool), pool_node->availableSets);
Tobin Ehlisee471462016-05-26 11:21:59 -06001281 }
1282 // Determine whether descriptor counts are satisfiable
1283 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001284 if (ds_data->required_descriptors_by_type[i] > pool_node->availableDescriptorTypeCount[i]) {
1285 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1286 reinterpret_cast<const uint64_t &>(pool_node->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1287 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1288 ". This pool only has %d descriptors of this type remaining.",
1289 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
1290 reinterpret_cast<uint64_t &>(pool_node->pool), pool_node->availableDescriptorTypeCount[i]);
Tobin Ehlisee471462016-05-26 11:21:59 -06001291 }
1292 }
1293 }
1294 return skip_call;
1295}
1296// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001297void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1298 const VkDescriptorSet *descriptor_sets,
1299 const AllocateDescriptorSetsData *ds_data,
1300 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map,
1301 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
1302 const core_validation::layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001303 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001304 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001305 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001306 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1307 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1308 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001309 /* Create tracking object for each descriptor set; insert into
1310 * global map and the pool's set.
1311 */
1312 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis4e380592016-06-02 12:41:47 -06001313 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], ds_data->layout_nodes[i], dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001314
1315 pool_state->sets.insert(new_ds);
1316 new_ds->in_use.store(0);
1317 (*set_map)[descriptor_sets[i]] = new_ds;
1318 }
1319}