blob: e017f0e08e654eb055c40e0fdad0051b45324ea8 [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();
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600323}
Chris Forbes57989132016-07-26 17:06:10 +1200324
325
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700326static std::string string_descriptor_req_view_type(descriptor_req req) {
327 std::string result("");
Chris Forbes57989132016-07-26 17:06:10 +1200328 for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
329 if (req & (1 << i)) {
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700330 if (result.size()) result += ", ";
331 result += string_VkImageViewType(VkImageViewType(i));
Chris Forbes57989132016-07-26 17:06:10 +1200332 }
333 }
334
Chris Forbes6e58ebd2016-08-31 12:58:14 -0700335 if (!result.size())
336 result = "(none)";
337
338 return result;
Chris Forbes57989132016-07-26 17:06:10 +1200339}
340
341
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600342// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
343bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
344 return layout->IsCompatible(p_layout_, error);
345}
Chris Forbes57989132016-07-26 17:06:10 +1200346
Tobin Ehlis3066db62016-08-22 08:12:23 -0600347// Validate that the state of this set is appropriate for the given bindings and dynamic_offsets at Draw time
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600348// This includes validating that all descriptors in the given bindings are updated,
349// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
350// Return true if state is acceptable, or false and write an error message into error string
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600351bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600352 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
iostrows5eb97652016-06-02 15:56:26 +0200353 auto dyn_offset_index = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200354 for (auto binding_pair : bindings) {
355 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600356 if (!p_layout_->HasBinding(binding)) {
357 std::stringstream error_str;
358 error_str << "Attempting to validate DrawState for binding #" << binding
359 << " which is an invalid binding for this descriptor set.";
360 *error = error_str.str();
361 return false;
362 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600363 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600364 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600365 // Nothing to do for strictly immutable sampler
366 } else {
367 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600368 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600369 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600370 std::stringstream error_str;
371 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
372 << " is being used in draw but has not been updated.";
373 *error = error_str.str();
374 return false;
375 } else {
Chris Forbes57989132016-07-26 17:06:10 +1200376 auto descriptor_class = descriptors_[i]->GetClass();
377 if (descriptor_class == GeneralBuffer) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600378 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600379 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600380 auto buffer_node = getBufferNode(device_data_, buffer);
381 if (!buffer_node) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600382 std::stringstream error_str;
383 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
384 << " references invalid buffer " << buffer << ".";
385 *error = error_str.str();
386 return false;
387 } else {
Tobin Ehlis997b2582016-06-02 08:43:37 -0600388 auto mem_entry = getMemObjInfo(device_data_, buffer_node->mem);
389 if (!mem_entry) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600390 std::stringstream error_str;
391 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600392 << " uses buffer " << buffer << " that references invalid memory " << buffer_node->mem
393 << ".";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600394 *error = error_str.str();
395 return false;
396 }
397 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600398 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600399 // Validate that dynamic offsets are within the buffer
Tobin Ehlis94bc5d22016-06-02 07:46:52 -0600400 auto buffer_size = buffer_node->createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600401 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
402 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600403 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
404 if (VK_WHOLE_SIZE == range) {
405 if ((dyn_offset + desc_offset) > buffer_size) {
406 std::stringstream error_str;
407 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
408 << " uses buffer " << buffer
409 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
410 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
411 << buffer_size << ".";
412 *error = error_str.str();
413 return false;
414 }
415 } else {
416 if ((dyn_offset + desc_offset + range) > buffer_size) {
417 std::stringstream error_str;
418 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
419 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
420 << " combined with offset " << desc_offset << " and range " << range
421 << " that oversteps the buffer size of " << buffer_size << ".";
422 *error = error_str.str();
423 return false;
424 }
425 }
426 }
427 }
Chris Forbes57989132016-07-26 17:06:10 +1200428 else if (descriptor_class == ImageSampler || descriptor_class == Image) {
429 auto image_view = (descriptor_class == ImageSampler)
430 ? static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView()
431 : static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
432 auto reqs = binding_pair.second;
433
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600434 auto image_view_state = getImageViewState(device_data_, image_view);
435 assert(image_view_state);
436 auto image_view_ci = image_view_state->create_info;
Chris Forbes57989132016-07-26 17:06:10 +1200437
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600438 if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
Chris Forbes57989132016-07-26 17:06:10 +1200439 // bad view type
440 std::stringstream error_str;
441 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
442 << " requires an image view of type " << string_descriptor_req_view_type(reqs)
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600443 << " but got " << string_VkImageViewType(image_view_ci.viewType) << ".";
Chris Forbes57989132016-07-26 17:06:10 +1200444 *error = error_str.str();
445 return false;
446 }
447
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600448 auto image_node = getImageNode(device_data_, image_view_ci.image);
Chris Forbes57989132016-07-26 17:06:10 +1200449 assert(image_node);
450
451 if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) &&
452 image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
453 std::stringstream error_str;
454 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
455 << " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
456 << string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
457 *error = error_str.str();
458 return false;
459 }
460
461 if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) &&
462 image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
463 std::stringstream error_str;
464 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
465 << " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
466 *error = error_str.str();
467 return false;
468 }
469 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600470 }
471 }
472 }
473 }
474 return true;
475}
Chris Forbes57989132016-07-26 17:06:10 +1200476
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600477// For given bindings, place any update buffers or images into the passed-in unordered_sets
Tobin Ehliscebc4c02016-08-22 10:10:43 -0600478uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600479 std::unordered_set<VkBuffer> *buffer_set,
480 std::unordered_set<VkImageView> *image_set) const {
481 auto num_updates = 0;
Chris Forbesc7090a82016-07-25 18:10:41 +1200482 for (auto binding_pair : bindings) {
483 auto binding = binding_pair.first;
Tobin Ehlis58c59582016-06-21 12:34:33 -0600484 // If a binding doesn't exist, skip it
485 if (!p_layout_->HasBinding(binding)) {
486 continue;
487 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600488 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600489 if (descriptors_[start_idx]->IsStorage()) {
490 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600491 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600492 if (descriptors_[start_idx + i]->updated) {
493 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600494 num_updates++;
495 }
496 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600497 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600498 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600499 if (descriptors_[start_idx + i]->updated) {
500 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis8b872462016-09-14 08:12:08 -0600501 auto bv_state = getBufferViewState(device_data_, bufferview);
502 if (bv_state) {
503 buffer_set->insert(bv_state->create_info.buffer);
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600504 num_updates++;
505 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600506 }
507 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600508 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600509 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600510 if (descriptors_[start_idx + i]->updated) {
511 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600512 num_updates++;
513 }
514 }
515 }
516 }
517 }
518 return num_updates;
519}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600520// Set is being deleted or updates so invalidate all bound cmd buffers
521void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
Tobin Ehlis2556f5b2016-06-24 17:22:16 -0600522 core_validation::invalidateCommandBuffers(cb_bindings,
523 {reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600524}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600525// Perform write update in given update struct
Tobin Ehlis300888c2016-05-18 13:43:26 -0600526void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600527 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
528 // perform update
529 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
530 descriptors_[start_idx + di]->WriteUpdate(update, di);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600531 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600532 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600533 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600534
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600535 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600536}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600537// Validate Copy update
538bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
539 const DescriptorSet *src_set, std::string *error) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600540 // Verify idle ds
541 if (in_use.load()) {
542 std::stringstream error_str;
543 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
544 << " that is in use by a command buffer.";
545 *error = error_str.str();
546 return false;
547 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600548 if (!p_layout_->HasBinding(update->dstBinding)) {
549 std::stringstream error_str;
550 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
551 *error = error_str.str();
552 return false;
553 }
554 if (!src_set->HasBinding(update->srcBinding)) {
555 std::stringstream error_str;
556 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
557 *error = error_str.str();
558 return false;
559 }
560 // src & dst set bindings are valid
561 // Check bounds of src & dst
562 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
563 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
564 // SRC update out of bounds
565 std::stringstream error_str;
566 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
567 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
568 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
569 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
570 *error = error_str.str();
571 return false;
572 }
573 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
574 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
575 // DST update out of bounds
576 std::stringstream error_str;
577 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
578 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
579 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
580 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
581 *error = error_str.str();
582 return false;
583 }
584 // Check that types match
585 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
586 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
587 if (src_type != dst_type) {
588 std::stringstream error_str;
589 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
590 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
591 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
592 *error = error_str.str();
593 return false;
594 }
595 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600596 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
597 "copy update from", src_set->GetSet(), error)) ||
598 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
599 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600600 return false;
601 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600602 // First make sure source descriptors are updated
603 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
604 if (!src_set->descriptors_[src_start_idx + i]) {
605 std::stringstream error_str;
606 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
607 << update->srcArrayElement + i << " has not been updated.";
608 *error = error_str.str();
609 return false;
610 }
611 }
612 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehliscbcf2342016-05-24 13:07:12 -0600613 if (!VerifyCopyUpdateContents(update, src_set, src_type, src_start_idx, error))
Tobin Ehlis300888c2016-05-18 13:43:26 -0600614 return false;
615
616 // All checks passed so update is good
617 return true;
618}
619// Perform Copy update
620void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600621 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
622 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600623 // Update parameters all look good so perform update
624 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600625 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600626 }
Tobin Ehlis56a30942016-05-19 08:00:00 -0600627 if (update->descriptorCount)
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600628 some_update_ = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600629
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600630 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600631}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600632
Tobin Ehlisf9519102016-08-17 09:49:13 -0600633// Bind cb_node to this set and this set to cb_node.
634// Prereq: This should be called for a set that has been confirmed to be active for the given cb_node, meaning it's going
635// to be used in a draw by the given cb_node
Tobin Ehlisf9519102016-08-17 09:49:13 -0600636void cvdescriptorset::DescriptorSet::BindCommandBuffer(GLOBAL_CB_NODE *cb_node, const std::unordered_set<uint32_t> &bindings) {
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600637 // bind cb to this descriptor set
638 cb_bindings.insert(cb_node);
639 // Add bindings for descriptor set and individual objects in the set
640 cb_node->object_bindings.insert({reinterpret_cast<uint64_t &>(set_), VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT});
Tobin Ehlisf9519102016-08-17 09:49:13 -0600641 // For the active slots, use set# to look up descriptorSet from boundDescriptorSets, and bind all of that descriptor set's
642 // resources
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600643 for (auto binding : bindings) {
644 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
645 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
646 for (uint32_t i = start_idx; i <= end_idx; ++i) {
647 descriptors_[i]->BindCommandBuffer(device_data_, cb_node);
648 }
649 }
Tobin Ehlis9252c2b2016-07-21 14:40:22 -0600650}
651
Tobin Ehlis300888c2016-05-18 13:43:26 -0600652cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600653 updated = false;
654 descriptor_class = PlainSampler;
655};
656
Tobin Ehlis300888c2016-05-18 13:43:26 -0600657cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600658 updated = false;
659 descriptor_class = PlainSampler;
660 if (immut) {
661 sampler_ = *immut;
662 immutable_ = true;
663 updated = true;
664 }
665}
Tobin Ehlise2f80292016-06-02 10:08:53 -0600666// Validate given sampler. Currently this only checks to make sure it exists in the samplerMap
667bool cvdescriptorset::ValidateSampler(const VkSampler sampler, const core_validation::layer_data *dev_data) {
668 return (getSamplerNode(dev_data, sampler) != nullptr);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600669}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600670
Tobin Ehlis554bf382016-05-24 11:14:43 -0600671bool cvdescriptorset::ValidateImageUpdate(VkImageView image_view, VkImageLayout image_layout, VkDescriptorType type,
Tobin Ehlisd5fb09e2016-06-02 10:54:09 -0600672 const core_validation::layer_data *dev_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600673 std::string *error) {
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600674 auto iv_state = getImageViewState(dev_data, image_view);
675 if (!iv_state) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600676 std::stringstream error_str;
677 error_str << "Invalid VkImageView: " << image_view;
678 *error = error_str.str();
679 return false;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600680 }
Tobin Ehlis81280962016-07-20 14:04:20 -0600681 // 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 -0600682 // Validate that imageLayout is compatible with aspect_mask and image format
683 // and validate that image usage bits are correct for given usage
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600684 VkImageAspectFlags aspect_mask = iv_state->create_info.subresourceRange.aspectMask;
685 VkImage image = iv_state->create_info.image;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600686 VkFormat format = VK_FORMAT_MAX_ENUM;
687 VkImageUsageFlags usage = 0;
Tobin Ehlis1c9c55f2016-06-02 11:49:22 -0600688 auto image_node = getImageNode(dev_data, image);
689 if (image_node) {
690 format = image_node->createInfo.format;
691 usage = image_node->createInfo.usage;
Tobin Ehlis029d2fe2016-09-21 09:19:15 -0600692 // Validate that memory is bound to image
693 if (ValidateMemoryIsBoundToImage(dev_data, image_node, "vkUpdateDescriptorSets()"))
694 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600695 } else {
Tobin Ehlis1809f912016-05-25 09:24:36 -0600696 // Also need to check the swapchains.
Tobin Ehlis969a5262016-06-02 12:13:32 -0600697 auto swapchain = getSwapchainFromImage(dev_data, image);
698 if (swapchain) {
Tobin Ehlis4e380592016-06-02 12:41:47 -0600699 auto swapchain_node = getSwapchainNode(dev_data, swapchain);
700 if (swapchain_node) {
701 format = swapchain_node->createInfo.imageFormat;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600702 }
703 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600704 }
705 // First validate that format and layout are compatible
706 if (format == VK_FORMAT_MAX_ENUM) {
707 std::stringstream error_str;
708 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
709 *error = error_str.str();
710 return false;
711 }
712 bool ds = vk_format_is_depth_or_stencil(format);
713 switch (image_layout) {
714 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
715 // Only Color bit must be set
716 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600717 std::stringstream error_str;
Tobin Ehlis1809f912016-05-25 09:24:36 -0600718 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
719 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
Tobin Ehlis554bf382016-05-24 11:14:43 -0600720 *error = error_str.str();
721 return false;
722 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600723 // format must NOT be DS
724 if (ds) {
725 std::stringstream error_str;
726 error_str << "ImageView (" << image_view
727 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
728 << string_VkFormat(format) << " which is not a color format.";
729 *error = error_str.str();
730 return false;
731 }
732 break;
733 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
734 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
735 // Depth or stencil bit must be set, but both must NOT be set
736 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
737 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
738 // both must NOT be set
739 std::stringstream error_str;
740 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
741 *error = error_str.str();
742 return false;
743 }
744 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
745 // Neither were set
746 std::stringstream error_str;
747 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
748 << " but does not have STENCIL or DEPTH aspects set";
749 *error = error_str.str();
750 return false;
751 }
752 // format must be DS
753 if (!ds) {
754 std::stringstream error_str;
755 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
756 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
757 *error = error_str.str();
758 return false;
759 }
760 break;
761 default:
Tobin Ehlisbbf3f912016-06-15 13:03:58 -0600762 // For other layouts if the source is ds image, both aspect bits must not be set
763 if (ds) {
764 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
765 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
766 // both must NOT be set
767 std::stringstream error_str;
768 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
769 << " and is using depth/stencil image of format " << string_VkFormat(format)
770 << " but it has both STENCIL and DEPTH aspects set, which is illegal. When using a depth/stencil "
771 "image in a descriptor set, please only set either VK_IMAGE_ASPECT_DEPTH_BIT or "
772 "VK_IMAGE_ASPECT_STENCIL_BIT depending on whether it will be used for depth reads or stencil "
773 "reads respectively.";
774 *error = error_str.str();
775 return false;
776 }
777 }
778 }
Tobin Ehlis1809f912016-05-25 09:24:36 -0600779 break;
780 }
781 // Now validate that usage flags are correctly set for given type of update
782 std::string error_usage_bit;
783 switch (type) {
784 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
785 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
786 if (!(usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
787 error_usage_bit = "VK_IMAGE_USAGE_SAMPLED_BIT";
788 }
789 break;
790 }
791 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
792 if (!(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
793 error_usage_bit = "VK_IMAGE_USAGE_STORAGE_BIT";
794 }
795 break;
796 }
797 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
798 if (!(usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) {
799 error_usage_bit = "VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT";
800 }
801 break;
802 }
803 default:
804 break;
805 }
806 if (!error_usage_bit.empty()) {
807 std::stringstream error_str;
808 error_str << "ImageView (" << image_view << ") with usage mask 0x" << usage
809 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
810 << error_usage_bit << " set.";
811 *error = error_str.str();
812 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600813 }
814 return true;
815}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600816
Tobin Ehlis300888c2016-05-18 13:43:26 -0600817void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
818 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600819 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600820}
821
Tobin Ehlis300888c2016-05-18 13:43:26 -0600822void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600823 if (!immutable_) {
824 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600825 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600826 }
827 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600828}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600829
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600830void cvdescriptorset::SamplerDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
831 if (!immutable_) {
832 auto sampler_node = getSamplerNode(dev_data, sampler_);
833 if (sampler_node)
834 core_validation::AddCommandBufferBindingSampler(cb_node, sampler_node);
835 }
836}
837
Tobin Ehlis300888c2016-05-18 13:43:26 -0600838cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
839 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600840 updated = false;
841 descriptor_class = ImageSampler;
842}
843
Tobin Ehlis300888c2016-05-18 13:43:26 -0600844cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
845 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600846 updated = false;
847 descriptor_class = ImageSampler;
848 if (immut) {
849 sampler_ = *immut;
850 immutable_ = true;
851 updated = true;
852 }
853}
Tobin Ehlis56a30942016-05-19 08:00:00 -0600854
Tobin Ehlis300888c2016-05-18 13:43:26 -0600855void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600856 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600857 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600858 sampler_ = image_info.sampler;
859 image_view_ = image_info.imageView;
860 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600861}
862
Tobin Ehlis300888c2016-05-18 13:43:26 -0600863void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600864 if (!immutable_) {
865 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600866 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600867 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600868 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
869 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600870 updated = true;
871 image_view_ = image_view;
872 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600873}
874
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600875void cvdescriptorset::ImageSamplerDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data,
876 GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600877 // First add binding for any non-immutable sampler
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600878 if (!immutable_) {
879 auto sampler_node = getSamplerNode(dev_data, sampler_);
880 if (sampler_node)
881 core_validation::AddCommandBufferBindingSampler(cb_node, sampler_node);
882 }
Tobin Ehlis81e46372016-08-17 13:33:44 -0600883 // Add binding for image
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600884 auto iv_state = getImageViewState(dev_data, image_view_);
885 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -0600886 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -0600887 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600888}
889
Tobin Ehlis300888c2016-05-18 13:43:26 -0600890cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
891 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600892 updated = false;
893 descriptor_class = Image;
894 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
895 storage_ = true;
896};
897
Tobin Ehlis300888c2016-05-18 13:43:26 -0600898void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600899 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600900 const auto &image_info = update->pImageInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600901 image_view_ = image_info.imageView;
902 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600903}
904
Tobin Ehlis300888c2016-05-18 13:43:26 -0600905void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600906 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
907 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600908 updated = true;
909 image_view_ = image_view;
910 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600911}
912
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600913void cvdescriptorset::ImageDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600914 // Add binding for image
Tobin Ehlis8b26a382016-09-14 08:02:49 -0600915 auto iv_state = getImageViewState(dev_data, image_view_);
916 if (iv_state) {
Tobin Ehlis15b8ea02016-09-19 14:02:58 -0600917 core_validation::AddCommandBufferBindingImageView(dev_data, cb_node, iv_state);
Tobin Ehlis81e46372016-08-17 13:33:44 -0600918 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600919}
920
Tobin Ehlis300888c2016-05-18 13:43:26 -0600921cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
922 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600923 updated = false;
924 descriptor_class = GeneralBuffer;
925 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
926 dynamic_ = true;
927 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
928 storage_ = true;
929 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
930 dynamic_ = true;
931 storage_ = true;
932 }
933}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600934void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600935 updated = true;
Tobin Ehlis56a30942016-05-19 08:00:00 -0600936 const auto &buffer_info = update->pBufferInfo[index];
Tobin Ehlis300888c2016-05-18 13:43:26 -0600937 buffer_ = buffer_info.buffer;
938 offset_ = buffer_info.offset;
939 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600940}
941
Tobin Ehlis300888c2016-05-18 13:43:26 -0600942void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
943 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600944 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600945 buffer_ = buff_desc->buffer_;
946 offset_ = buff_desc->offset_;
947 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600948}
949
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600950void cvdescriptorset::BufferDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis81e46372016-08-17 13:33:44 -0600951 auto buffer_node = getBufferNode(dev_data, buffer_);
952 if (buffer_node)
953 core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600954}
955
Tobin Ehlis300888c2016-05-18 13:43:26 -0600956cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600957 updated = false;
958 descriptor_class = TexelBuffer;
959 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
960 storage_ = true;
961};
Tobin Ehlis56a30942016-05-19 08:00:00 -0600962
Tobin Ehlis300888c2016-05-18 13:43:26 -0600963void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600964 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600965 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600966}
967
Tobin Ehlis300888c2016-05-18 13:43:26 -0600968void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
969 updated = true;
970 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
971}
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600972
973void cvdescriptorset::TexelDescriptor::BindCommandBuffer(const core_validation::layer_data *dev_data, GLOBAL_CB_NODE *cb_node) {
Tobin Ehlis8b872462016-09-14 08:12:08 -0600974 auto bv_state = getBufferViewState(dev_data, buffer_view_);
975 if (bv_state) {
976 auto buffer_node = getBufferNode(dev_data, bv_state->create_info.buffer);
Tobin Ehlis81e46372016-08-17 13:33:44 -0600977 if (buffer_node)
978 core_validation::AddCommandBufferBindingBuffer(dev_data, cb_node, buffer_node);
979 }
Tobin Ehlis8020eea2016-08-17 11:10:41 -0600980}
981
Tobin Ehlis300888c2016-05-18 13:43:26 -0600982// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
983// sets, and then calls their respective Validate[Write|Copy]Update functions.
984// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
985// be skipped, then true is returned.
986// If there is no issue with the update, then false is returned.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600987bool cvdescriptorset::ValidateUpdateDescriptorSets(const debug_report_data *report_data,
988 const core_validation::layer_data *dev_data, uint32_t write_count,
989 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
990 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600991 bool skip_call = false;
992 // Validate Write updates
Tobin Ehlis56a30942016-05-19 08:00:00 -0600993 for (uint32_t i = 0; i < write_count; i++) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600994 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -0600995 auto set_node = core_validation::getSetNode(dev_data, dest_set);
996 if (!set_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600997 skip_call |=
998 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 -0600999 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001000 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
1001 reinterpret_cast<uint64_t &>(dest_set));
1002 } else {
1003 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001004 if (!set_node->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001005 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 -06001006 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_WRITE_UPDATE, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001007 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
1008 " with error: %s",
1009 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
1010 }
1011 }
1012 }
1013 // Now validate copy updates
Tobin Ehlis56a30942016-05-19 08:00:00 -06001014 for (uint32_t i = 0; i < copy_count; ++i) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001015 auto dst_set = p_cds[i].dstSet;
1016 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001017 auto src_node = core_validation::getSetNode(dev_data, src_set);
1018 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
1019 if (!src_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001020 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 -06001021 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001022 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
1023 " that has not been allocated.",
1024 reinterpret_cast<uint64_t &>(src_set));
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001025 } else if (!dst_node) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001026 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 -06001027 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_DESCRIPTOR_SET, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001028 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
1029 " that has not been allocated.",
1030 reinterpret_cast<uint64_t &>(dst_set));
1031 } else {
1032 std::string error_str;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001033 if (!dst_node->ValidateCopyUpdate(report_data, &p_cds[i], src_node, &error_str)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001034 skip_call |=
1035 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 -06001036 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_COPY_UPDATE, "DS",
Tobin Ehlis300888c2016-05-18 13:43:26 -06001037 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
1038 " to Descriptor Set 0x%" PRIx64 " with error: %s",
1039 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
1040 }
1041 }
1042 }
1043 return skip_call;
1044}
1045// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
1046// sets, and then calls their respective Perform[Write|Copy]Update functions.
1047// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
1048// with the same set of updates.
1049// This is split from the validate code to allow validation prior to calling down the chain, and then update after
1050// calling down the chain.
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001051void cvdescriptorset::PerformUpdateDescriptorSets(const core_validation::layer_data *dev_data, uint32_t write_count,
1052 const VkWriteDescriptorSet *p_wds, uint32_t copy_count,
1053 const VkCopyDescriptorSet *p_cds) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001054 // Write updates first
1055 uint32_t i = 0;
1056 for (i = 0; i < write_count; ++i) {
1057 auto dest_set = p_wds[i].dstSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001058 auto set_node = core_validation::getSetNode(dev_data, dest_set);
1059 if (set_node) {
1060 set_node->PerformWriteUpdate(&p_wds[i]);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001061 }
1062 }
1063 // Now copy updates
1064 for (i = 0; i < copy_count; ++i) {
1065 auto dst_set = p_cds[i].dstSet;
1066 auto src_set = p_cds[i].srcSet;
Tobin Ehlis6a72dc72016-06-01 16:41:17 -06001067 auto src_node = core_validation::getSetNode(dev_data, src_set);
1068 auto dst_node = core_validation::getSetNode(dev_data, dst_set);
1069 if (src_node && dst_node) {
1070 dst_node->PerformCopyUpdate(&p_cds[i], src_node);
Tobin Ehlis300888c2016-05-18 13:43:26 -06001071 }
1072 }
1073}
1074// Validate the state for a given write update but don't actually perform the update
1075// If an error would occur for this update, return false and fill in details in error_msg string
1076bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
1077 std::string *error_msg) {
1078 // Verify idle ds
1079 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001080 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001081 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
1082 << " that is in use by a command buffer.";
1083 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001084 return false;
1085 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001086 // Verify dst binding exists
1087 if (!p_layout_->HasBinding(update->dstBinding)) {
1088 std::stringstream error_str;
1089 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
1090 *error_msg = error_str.str();
1091 return false;
Tobin Ehlis57ae28f2016-05-24 12:35:57 -06001092 }
1093 // We know that binding is valid, verify update and do update on each descriptor
1094 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
1095 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
1096 if (type != update->descriptorType) {
1097 std::stringstream error_str;
1098 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
1099 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
1100 *error_msg = error_str.str();
1101 return false;
1102 }
1103 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
1104 std::stringstream error_str;
1105 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
1106 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
1107 << " descriptors starting at binding offset of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
1108 << " combined with update array element offset of " << update->dstArrayElement
1109 << " oversteps the size of this descriptor set.";
1110 *error_msg = error_str.str();
1111 return false;
1112 }
1113 // Verify consecutive bindings match (if needed)
1114 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "write update to",
1115 set_, error_msg))
1116 return false;
1117 // Update is within bounds and consistent so last step is to validate update contents
1118 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
1119 std::stringstream error_str;
1120 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
1121 << " failed with error message: " << error_msg->c_str();
1122 *error_msg = error_str.str();
1123 return false;
Tobin Ehlis300888c2016-05-18 13:43:26 -06001124 }
1125 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001126 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -06001127}
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001128// For the given buffer, verify that its creation parameters are appropriate for the given type
1129// If there's an error, update the error string with details and return false, else return true
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001130bool cvdescriptorset::DescriptorSet::ValidateBufferUsage(BUFFER_NODE const *buffer_node, VkDescriptorType type,
1131 std::string *error) const {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001132 // Verify that usage bits set correctly for given type
Tobin Ehlis94bc5d22016-06-02 07:46:52 -06001133 auto usage = buffer_node->createInfo.usage;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001134 std::string error_usage_bit;
1135 switch (type) {
1136 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1137 if (!(usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT)) {
1138 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT";
1139 }
1140 break;
1141 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1142 if (!(usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)) {
1143 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT";
1144 }
1145 break;
1146 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1147 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1148 if (!(usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)) {
1149 error_usage_bit = "VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT";
1150 }
1151 break;
1152 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1153 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1154 if (!(usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)) {
1155 error_usage_bit = "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT";
1156 }
1157 break;
1158 default:
1159 break;
1160 }
1161 if (!error_usage_bit.empty()) {
1162 std::stringstream error_str;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001163 error_str << "Buffer (" << buffer_node->buffer << ") with usage mask 0x" << usage
1164 << " being used for a descriptor update of type " << string_VkDescriptorType(type) << " does not have "
1165 << error_usage_bit << " set.";
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001166 *error = error_str.str();
1167 return false;
1168 }
1169 return true;
1170}
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001171// For buffer descriptor updates, verify the buffer usage and VkDescriptorBufferInfo struct which includes:
1172// 1. buffer is valid
1173// 2. buffer was created with correct usage flags
1174// 3. offset is less than buffer size
1175// 4. range is either VK_WHOLE_SIZE or falls in (0, (buffer size - offset)]
1176// If there's an error, update the error string with details and return false, else return true
1177bool cvdescriptorset::DescriptorSet::ValidateBufferUpdate(VkDescriptorBufferInfo const *buffer_info, VkDescriptorType type,
1178 std::string *error) const {
1179 // First make sure that buffer is valid
1180 auto buffer_node = getBufferNode(device_data_, buffer_info->buffer);
1181 if (!buffer_node) {
1182 std::stringstream error_str;
1183 error_str << "Invalid VkBuffer: " << buffer_info->buffer;
1184 *error = error_str.str();
1185 return false;
1186 }
Tobin Ehlis81280962016-07-20 14:04:20 -06001187 if (ValidateMemoryIsBoundToBuffer(device_data_, buffer_node, "vkUpdateDescriptorSets()"))
1188 return false;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001189 // Verify usage bits
1190 if (!ValidateBufferUsage(buffer_node, type, error)) {
1191 // error will have been updated by ValidateBufferUsage()
1192 return false;
1193 }
1194 // offset must be less than buffer size
1195 if (buffer_info->offset > buffer_node->createInfo.size) {
1196 std::stringstream error_str;
1197 error_str << "VkDescriptorBufferInfo offset of " << buffer_info->offset << " is greater than buffer " << buffer_node->buffer
1198 << " size of " << buffer_node->createInfo.size;
1199 *error = error_str.str();
1200 return false;
1201 }
1202 if (buffer_info->range != VK_WHOLE_SIZE) {
1203 // Range must be VK_WHOLE_SIZE or > 0
1204 if (!buffer_info->range) {
1205 std::stringstream error_str;
1206 error_str << "VkDescriptorBufferInfo range is not VK_WHOLE_SIZE and is zero, which is not allowed.";
1207 *error = error_str.str();
1208 return false;
1209 }
1210 // Range must be VK_WHOLE_SIZE or <= (buffer size - offset)
1211 if (buffer_info->range > (buffer_node->createInfo.size - buffer_info->offset)) {
1212 std::stringstream error_str;
1213 error_str << "VkDescriptorBufferInfo range is " << buffer_info->range << " which is greater than buffer size ("
1214 << buffer_node->createInfo.size << ") minus requested offset of " << buffer_info->offset;
1215 *error = error_str.str();
1216 return false;
1217 }
1218 }
1219 return true;
1220}
1221
Tobin Ehlis300888c2016-05-18 13:43:26 -06001222// Verify that the contents of the update are ok, but don't perform actual update
1223bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
1224 std::string *error) const {
1225 switch (update->descriptorType) {
1226 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
1227 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1228 // Validate image
1229 auto image_view = update->pImageInfo[di].imageView;
1230 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis4e380592016-06-02 12:41:47 -06001231 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001232 std::stringstream error_str;
1233 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1234 *error = error_str.str();
1235 return false;
1236 }
1237 }
1238 // Intentional fall-through to validate sampler
1239 }
1240 case VK_DESCRIPTOR_TYPE_SAMPLER: {
1241 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1242 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
Tobin Ehlise2f80292016-06-02 10:08:53 -06001243 if (!ValidateSampler(update->pImageInfo[di].sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001244 std::stringstream error_str;
1245 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
1246 << update->pImageInfo[di].sampler << ".";
1247 *error = error_str.str();
1248 return false;
1249 }
1250 } else {
1251 // TODO : Warn here
1252 }
1253 }
1254 break;
1255 }
1256 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1257 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1258 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
1259 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1260 auto image_view = update->pImageInfo[di].imageView;
1261 auto image_layout = update->pImageInfo[di].imageLayout;
Tobin Ehlis4e380592016-06-02 12:41:47 -06001262 if (!ValidateImageUpdate(image_view, image_layout, update->descriptorType, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001263 std::stringstream error_str;
1264 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1265 *error = error_str.str();
1266 return false;
1267 }
1268 }
1269 break;
1270 }
1271 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1272 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1273 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1274 auto buffer_view = update->pTexelBufferView[di];
Tobin Ehlis8b872462016-09-14 08:12:08 -06001275 auto bv_state = getBufferViewState(device_data_, buffer_view);
1276 if (!bv_state) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001277 std::stringstream error_str;
1278 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1279 *error = error_str.str();
1280 return false;
1281 }
Tobin Ehlis8b872462016-09-14 08:12:08 -06001282 auto buffer = bv_state->create_info.buffer;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001283 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), update->descriptorType, error)) {
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001284 std::stringstream error_str;
1285 error_str << "Attempted write update to texel buffer descriptor failed due to: " << error->c_str();
1286 *error = error_str.str();
1287 return false;
1288 }
Tobin Ehlis300888c2016-05-18 13:43:26 -06001289 }
1290 break;
1291 }
1292 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1293 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1294 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1295 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1296 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001297 if (!ValidateBufferUpdate(update->pBufferInfo + di, update->descriptorType, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001298 std::stringstream error_str;
Tobin Ehlis6bd2b982016-05-24 12:33:42 -06001299 error_str << "Attempted write update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001300 *error = error_str.str();
1301 return false;
1302 }
1303 }
1304 break;
1305 }
1306 default:
1307 assert(0); // We've already verified update type so should never get here
1308 break;
1309 }
1310 // All checks passed so update contents are good
1311 return true;
1312}
1313// Verify that the contents of the update are ok, but don't perform actual update
1314bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001315 VkDescriptorType type, uint32_t index, std::string *error) const {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001316 switch (src_set->descriptors_[index]->descriptor_class) {
1317 case PlainSampler: {
1318 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1319 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1320 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001321 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001322 std::stringstream error_str;
1323 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1324 *error = error_str.str();
1325 return false;
1326 }
1327 } else {
1328 // TODO : Warn here
1329 }
1330 }
1331 break;
1332 }
1333 case ImageSampler: {
1334 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1335 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1336 // First validate sampler
1337 if (!img_samp_desc->IsImmutableSampler()) {
1338 auto update_sampler = img_samp_desc->GetSampler();
Tobin Ehlise2f80292016-06-02 10:08:53 -06001339 if (!ValidateSampler(update_sampler, device_data_)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001340 std::stringstream error_str;
1341 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1342 *error = error_str.str();
1343 return false;
1344 }
1345 } else {
1346 // TODO : Warn here
1347 }
1348 // Validate image
1349 auto image_view = img_samp_desc->GetImageView();
1350 auto image_layout = img_samp_desc->GetImageLayout();
Tobin Ehlis4e380592016-06-02 12:41:47 -06001351 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001352 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001353 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001354 *error = error_str.str();
1355 return false;
1356 }
1357 }
1358 }
1359 case Image: {
1360 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1361 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1362 auto image_view = img_desc->GetImageView();
1363 auto image_layout = img_desc->GetImageLayout();
Tobin Ehlis4e380592016-06-02 12:41:47 -06001364 if (!ValidateImageUpdate(image_view, image_layout, type, device_data_, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001365 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001366 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001367 *error = error_str.str();
1368 return false;
1369 }
1370 }
1371 break;
1372 }
1373 case TexelBuffer: {
1374 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1375 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
Tobin Ehlis8b872462016-09-14 08:12:08 -06001376 auto bv_state = getBufferViewState(device_data_, buffer_view);
1377 if (!bv_state) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001378 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001379 error_str << "Attempted copy update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1380 *error = error_str.str();
1381 return false;
1382 }
Tobin Ehlis8b872462016-09-14 08:12:08 -06001383 auto buffer = bv_state->create_info.buffer;
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001384 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), type, error)) {
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001385 std::stringstream error_str;
1386 error_str << "Attempted copy update to texel buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001387 *error = error_str.str();
1388 return false;
1389 }
1390 }
1391 break;
1392 }
1393 case GeneralBuffer: {
1394 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1395 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
Tobin Ehlis3d38f082016-07-01 17:36:48 -06001396 if (!ValidateBufferUsage(getBufferNode(device_data_, buffer), type, error)) {
Tobin Ehlis300888c2016-05-18 13:43:26 -06001397 std::stringstream error_str;
Tobin Ehliscbcf2342016-05-24 13:07:12 -06001398 error_str << "Attempted copy update to buffer descriptor failed due to: " << error->c_str();
Tobin Ehlis300888c2016-05-18 13:43:26 -06001399 *error = error_str.str();
1400 return false;
1401 }
1402 }
1403 break;
1404 }
1405 default:
1406 assert(0); // We've already verified update type so should never get here
1407 break;
1408 }
1409 // All checks passed so update contents are good
1410 return true;
Chris Forbesb4e0bdb2016-05-31 16:34:40 +12001411}
Tobin Ehlisee471462016-05-26 11:21:59 -06001412// Verify that the state at allocate time is correct, but don't actually allocate the sets yet
Tobin Ehlis815e8132016-06-02 13:02:17 -06001413bool cvdescriptorset::ValidateAllocateDescriptorSets(const debug_report_data *report_data,
1414 const VkDescriptorSetAllocateInfo *p_alloc_info,
1415 const core_validation::layer_data *dev_data,
1416 AllocateDescriptorSetsData *ds_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001417 bool skip_call = false;
Tobin Ehlisee471462016-05-26 11:21:59 -06001418
1419 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001420 auto layout = getDescriptorSetLayout(dev_data, p_alloc_info->pSetLayouts[i]);
1421 if (!layout) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001422 skip_call |=
1423 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
1424 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
1425 "Unable to find set layout node for layout 0x%" PRIxLEAST64 " specified in vkAllocateDescriptorSets() call",
1426 reinterpret_cast<const uint64_t &>(p_alloc_info->pSetLayouts[i]));
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001427 } else {
Tobin Ehlis815e8132016-06-02 13:02:17 -06001428 ds_data->layout_nodes[i] = layout;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001429 // Count total descriptors required per type
Tobin Ehlis815e8132016-06-02 13:02:17 -06001430 for (uint32_t j = 0; j < layout->GetBindingCount(); ++j) {
1431 const auto &binding_layout = layout->GetDescriptorSetLayoutBindingPtrFromIndex(j);
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001432 uint32_t typeIndex = static_cast<uint32_t>(binding_layout->descriptorType);
1433 ds_data->required_descriptors_by_type[typeIndex] += binding_layout->descriptorCount;
1434 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001435 }
1436 }
Tobin Ehlis815e8132016-06-02 13:02:17 -06001437 auto pool_node = getPoolNode(dev_data, p_alloc_info->descriptorPool);
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001438 // Track number of descriptorSets allowable in this pool
1439 if (pool_node->availableSets < p_alloc_info->descriptorSetCount) {
1440 skip_call |= log_msg(
1441 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1442 reinterpret_cast<uint64_t &>(pool_node->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY, "DS",
1443 "Unable to allocate %u descriptorSets from pool 0x%" PRIxLEAST64 ". This pool only has %d descriptorSets remaining.",
1444 p_alloc_info->descriptorSetCount, reinterpret_cast<uint64_t &>(pool_node->pool), pool_node->availableSets);
1445 }
1446 // Determine whether descriptor counts are satisfiable
1447 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1448 if (ds_data->required_descriptors_by_type[i] > pool_node->availableDescriptorTypeCount[i]) {
1449 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_POOL_EXT,
1450 reinterpret_cast<const uint64_t &>(pool_node->pool), __LINE__, DRAWSTATE_DESCRIPTOR_POOL_EMPTY,
1451 "DS", "Unable to allocate %u descriptors of type %s from pool 0x%" PRIxLEAST64
1452 ". This pool only has %d descriptors of this type remaining.",
1453 ds_data->required_descriptors_by_type[i], string_VkDescriptorType(VkDescriptorType(i)),
1454 reinterpret_cast<uint64_t &>(pool_node->pool), pool_node->availableDescriptorTypeCount[i]);
Tobin Ehlisee471462016-05-26 11:21:59 -06001455 }
1456 }
Tobin Ehlis5d749ea2016-07-18 13:14:01 -06001457
Tobin Ehlisee471462016-05-26 11:21:59 -06001458 return skip_call;
1459}
1460// Decrement allocated sets from the pool and insert new sets into set_map
Tobin Ehlis4e380592016-06-02 12:41:47 -06001461void cvdescriptorset::PerformAllocateDescriptorSets(const VkDescriptorSetAllocateInfo *p_alloc_info,
1462 const VkDescriptorSet *descriptor_sets,
1463 const AllocateDescriptorSetsData *ds_data,
1464 std::unordered_map<VkDescriptorPool, DESCRIPTOR_POOL_NODE *> *pool_map,
1465 std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> *set_map,
1466 const core_validation::layer_data *dev_data) {
Tobin Ehlisee471462016-05-26 11:21:59 -06001467 auto pool_state = (*pool_map)[p_alloc_info->descriptorPool];
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001468 /* Account for sets and individual descriptors allocated from pool */
Tobin Ehlisee471462016-05-26 11:21:59 -06001469 pool_state->availableSets -= p_alloc_info->descriptorSetCount;
Tobin Ehlis68d0adf2016-06-01 11:33:50 -06001470 for (uint32_t i = 0; i < VK_DESCRIPTOR_TYPE_RANGE_SIZE; i++) {
1471 pool_state->availableDescriptorTypeCount[i] -= ds_data->required_descriptors_by_type[i];
1472 }
Tobin Ehlisee471462016-05-26 11:21:59 -06001473 /* Create tracking object for each descriptor set; insert into
1474 * global map and the pool's set.
1475 */
1476 for (uint32_t i = 0; i < p_alloc_info->descriptorSetCount; i++) {
Tobin Ehlis4e380592016-06-02 12:41:47 -06001477 auto new_ds = new cvdescriptorset::DescriptorSet(descriptor_sets[i], ds_data->layout_nodes[i], dev_data);
Tobin Ehlisee471462016-05-26 11:21:59 -06001478
1479 pool_state->sets.insert(new_ds);
1480 new_ds->in_use.store(0);
1481 (*set_map)[descriptor_sets[i]] = new_ds;
1482 }
1483}