blob: d178a9167134fe540a5f9047f4b3bdbd43de22e7 [file] [log] [blame]
Tobin Ehlis0a43bde2016-05-03 08:31:08 -06001/* Copyright (c) 2015-2016 The Khronos Group Inc.
2 * Copyright (c) 2015-2016 Valve Corporation
3 * Copyright (c) 2015-2016 LunarG, Inc.
4 * Copyright (C) 2015-2016 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Tobin Ehlis <tobine@google.com>
19 */
20
21#include "descriptor_sets.h"
22#include "vk_enum_string_helper.h"
23#include "vk_safe_struct.h"
24#include <sstream>
25
26// Construct DescriptorSetLayout instance from given create info
27cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(debug_report_data *report_data,
28 const VkDescriptorSetLayoutCreateInfo *p_create_info,
29 const VkDescriptorSetLayout layout)
30 : layout_(layout), binding_count_(p_create_info->bindingCount), descriptor_count_(0), dynamic_descriptor_count_(0) {
31 uint32_t global_index = 0;
32 for (uint32_t i = 0; i < binding_count_; ++i) {
33 descriptor_count_ += p_create_info->pBindings[i].descriptorCount;
34 if (!binding_to_index_map_.emplace(p_create_info->pBindings[i].binding, i).second) {
35 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT_EXT,
36 reinterpret_cast<uint64_t &>(layout_), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
37 "duplicated binding number in "
38 "VkDescriptorSetLayoutBinding");
39 }
40 binding_to_global_start_index_map_[p_create_info->pBindings[i].binding] = global_index;
41 global_index += p_create_info->pBindings[i].descriptorCount ? p_create_info->pBindings[i].descriptorCount - 1 : 0;
42 binding_to_global_end_index_map_[p_create_info->pBindings[i].binding] = global_index;
43 global_index++;
Tobin Ehlis664e6012016-05-05 11:04:44 -060044 bindings_.push_back(safe_VkDescriptorSetLayoutBinding(&p_create_info->pBindings[i]));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060045 // In cases where we should ignore pImmutableSamplers make sure it's NULL
46 if ((p_create_info->pBindings[i].pImmutableSamplers) &&
47 ((p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_SAMPLER) &&
48 (p_create_info->pBindings[i].descriptorType != VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER))) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060049 bindings_.back().pImmutableSamplers = nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060050 }
51 if (p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
52 p_create_info->pBindings[i].descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
53 dynamic_descriptor_count_++;
54 }
55 }
56}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060057// put all bindings into the given set
58void cvdescriptorset::DescriptorSetLayout::FillBindingSet(std::unordered_set<uint32_t> *binding_set) const {
59 for (auto binding_index_pair : binding_to_index_map_)
60 binding_set->insert(binding_index_pair.first);
61}
62VkDescriptorSetLayoutBinding const *
63cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060064 const auto &bi_itr = binding_to_index_map_.find(binding);
65 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060066 return bindings_[bi_itr->second].ptr();
Tobin Ehlis0bc30632016-05-05 10:16:02 -060067 }
68 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060069}
70VkDescriptorSetLayoutBinding const *
71cvdescriptorset::DescriptorSetLayout::GetDescriptorSetLayoutBindingPtrFromIndex(const uint32_t index) const {
72 if (index >= bindings_.size())
73 return nullptr;
Tobin Ehlis664e6012016-05-05 11:04:44 -060074 return bindings_[index].ptr();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060075}
76// Return descriptorCount for given binding, 0 if index is unavailable
77uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromBinding(const uint32_t binding) const {
Tobin Ehlis0bc30632016-05-05 10:16:02 -060078 const auto &bi_itr = binding_to_index_map_.find(binding);
79 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060080 return bindings_[bi_itr->second].descriptorCount;
Tobin Ehlis0bc30632016-05-05 10:16:02 -060081 }
82 return 0;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060083}
84// Return descriptorCount for given index, 0 if index is unavailable
85uint32_t cvdescriptorset::DescriptorSetLayout::GetDescriptorCountFromIndex(const uint32_t index) const {
86 if (index >= bindings_.size())
87 return 0;
Tobin Ehlis664e6012016-05-05 11:04:44 -060088 return bindings_[index].descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060089}
90// For the given binding, return descriptorType
91VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromBinding(const uint32_t binding) const {
92 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -060093 const auto &bi_itr = binding_to_index_map_.find(binding);
94 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -060095 return bindings_[bi_itr->second].descriptorType;
Tobin Ehlis0bc30632016-05-05 10:16:02 -060096 }
97 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -060098}
99// For the given index, return descriptorType
100VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromIndex(const uint32_t index) const {
101 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600102 return bindings_[index].descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600103}
104// For the given global index, return descriptorType
105// Currently just counting up through bindings_, may improve this in future
106VkDescriptorType cvdescriptorset::DescriptorSetLayout::GetTypeFromGlobalIndex(const uint32_t index) const {
107 uint32_t global_offset = 0;
108 for (auto binding : bindings_) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600109 global_offset += binding.descriptorCount;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600110 if (index < global_offset)
Tobin Ehlis664e6012016-05-05 11:04:44 -0600111 return binding.descriptorType;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600112 }
113 assert(0); // requested global index is out of bounds
114 return VK_DESCRIPTOR_TYPE_MAX_ENUM;
115}
116// For the given binding, return stageFlags
117VkShaderStageFlags cvdescriptorset::DescriptorSetLayout::GetStageFlagsFromBinding(const uint32_t binding) const {
118 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600119 const auto &bi_itr = binding_to_index_map_.find(binding);
120 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600121 return bindings_[bi_itr->second].stageFlags;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600122 }
123 return VkShaderStageFlags(0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600124}
125// For the given binding, return start index
126uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalStartIndexFromBinding(const uint32_t binding) const {
127 assert(binding_to_global_start_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600128 const auto &btgsi_itr = binding_to_global_start_index_map_.find(binding);
129 if (btgsi_itr != binding_to_global_start_index_map_.end()) {
130 return btgsi_itr->second;
131 }
132 // In error case max uint32_t so index is out of bounds to break ASAP
133 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600134}
135// For the given binding, return end index
136uint32_t cvdescriptorset::DescriptorSetLayout::GetGlobalEndIndexFromBinding(const uint32_t binding) const {
137 assert(binding_to_global_end_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600138 const auto &btgei_itr = binding_to_global_end_index_map_.find(binding);
139 if (btgei_itr != binding_to_global_end_index_map_.end()) {
140 return btgei_itr->second;
141 }
142 // In error case max uint32_t so index is out of bounds to break ASAP
143 return 0xFFFFFFFF;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600144}
145// For given binding, return ptr to ImmutableSampler array
146VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
147 assert(binding_to_index_map_.count(binding));
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600148 const auto &bi_itr = binding_to_index_map_.find(binding);
149 if (bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600150 return bindings_[bi_itr->second].pImmutableSamplers;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600151 }
152 return nullptr;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600153}
154// For given index, return ptr to ImmutableSampler array
155VkSampler const *cvdescriptorset::DescriptorSetLayout::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
156 assert(index < bindings_.size());
Tobin Ehlis664e6012016-05-05 11:04:44 -0600157 return bindings_[index].pImmutableSamplers;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600158}
159// If our layout is compatible with rh_ds_layout, return true,
160// else return false and fill in error_msg will description of what causes incompatibility
161bool cvdescriptorset::DescriptorSetLayout::IsCompatible(const DescriptorSetLayout *rh_ds_layout, std::string *error_msg) const {
162 // Trivial case
163 if (layout_ == rh_ds_layout->GetDescriptorSetLayout())
164 return true;
165 if (descriptor_count_ != rh_ds_layout->descriptor_count_) {
166 std::stringstream error_str;
167 error_str << "DescriptorSetLayout " << layout_ << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
168 << rh_ds_layout->GetDescriptorSetLayout() << " has " << rh_ds_layout->descriptor_count_ << " descriptors.";
169 *error_msg = error_str.str();
170 return false; // trivial fail case
171 }
172 // Descriptor counts match so need to go through bindings one-by-one
173 // and verify that type and stageFlags match
174 for (auto binding : bindings_) {
175 // TODO : Do we also need to check immutable samplers?
176 // VkDescriptorSetLayoutBinding *rh_binding;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600177 if (binding.descriptorCount != rh_ds_layout->GetDescriptorCountFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600178 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600179 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has a descriptorCount of "
180 << binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600181 << rh_ds_layout->GetDescriptorSetLayout() << " has a descriptorCount of "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600182 << rh_ds_layout->GetDescriptorCountFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600183 *error_msg = error_str.str();
184 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600185 } else if (binding.descriptorType != rh_ds_layout->GetTypeFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600186 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600187 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " is type '"
188 << string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600189 << " for DescriptorSetLayout " << rh_ds_layout->GetDescriptorSetLayout() << " is type '"
Tobin Ehlis664e6012016-05-05 11:04:44 -0600190 << string_VkDescriptorType(rh_ds_layout->GetTypeFromBinding(binding.binding)) << "'";
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600191 *error_msg = error_str.str();
192 return false;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600193 } else if (binding.stageFlags != rh_ds_layout->GetStageFlagsFromBinding(binding.binding)) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600194 std::stringstream error_str;
Tobin Ehlis664e6012016-05-05 11:04:44 -0600195 error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << layout_ << " has stageFlags "
196 << binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout "
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600197 << rh_ds_layout->GetDescriptorSetLayout() << " has stageFlags "
Tobin Ehlis664e6012016-05-05 11:04:44 -0600198 << rh_ds_layout->GetStageFlagsFromBinding(binding.binding);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600199 *error_msg = error_str.str();
200 return false;
201 }
202 }
203 return true;
204}
205
206bool cvdescriptorset::DescriptorSetLayout::IsNextBindingConsistent(const uint32_t binding) const {
207 if (!binding_to_index_map_.count(binding + 1))
208 return false;
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600209 auto const &bi_itr = binding_to_index_map_.find(binding);
210 if (bi_itr != binding_to_index_map_.end()) {
211 const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
212 if (next_bi_itr != binding_to_index_map_.end()) {
Tobin Ehlis664e6012016-05-05 11:04:44 -0600213 auto type = bindings_[bi_itr->second].descriptorType;
214 auto stage_flags = bindings_[bi_itr->second].stageFlags;
215 auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
216 if ((type != bindings_[next_bi_itr->second].descriptorType) ||
217 (stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
218 (immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false))) {
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600219 return false;
220 }
221 return true;
222 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600223 }
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600224 return false;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600225}
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600226// Starting at offset descriptor of given binding, parse over update_count
227// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
228// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
229// If so, return true. If not, fill in error_msg and return false
230bool cvdescriptorset::DescriptorSetLayout::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset, uint32_t update_count,
231 const char *type, const VkDescriptorSet set,
232 std::string *error_msg) const {
233 // Verify consecutive bindings match (if needed)
234 auto orig_binding = current_binding;
235 // Track count of descriptors in the current_bindings that are remaining to be updated
236 auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
237 // First, it's legal to offset beyond your own binding so handle that case
238 // Really this is just searching for the binding in which the update begins and adjusting offset accordingly
239 while (offset >= binding_remaining) {
240 // Advance to next binding, decrement offset by binding size
241 offset -= binding_remaining;
242 binding_remaining = GetDescriptorCountFromBinding(++current_binding);
243 }
244 binding_remaining -= offset;
245 while (update_count > binding_remaining) { // While our updates overstep current binding
246 // Verify next consecutive binding matches type, stage flags & immutable sampler use
247 if (!IsNextBindingConsistent(current_binding++)) {
248 std::stringstream error_str;
249 error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
250 << update_count << " descriptors being updated but this update oversteps the bounds of this binding and the "
251 "next binding is not consistent with current binding so this update is invalid.";
252 *error_msg = error_str.str();
253 return false;
254 }
255 // For sake of this check consider the bindings updated and grab count for next binding
256 update_count -= binding_remaining;
257 binding_remaining = GetDescriptorCountFromBinding(current_binding);
258 }
259 return true;
260}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600261
262cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const DescriptorSetLayout *layout,
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600263 const debug_report_data *debug_report_data,
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600264 const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map,
265 const std::unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> *memory_map,
266 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map,
267 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map,
268 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
269 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
270 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
271 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600272 : some_update_(false), set_(set), p_layout_(layout), report_data_(debug_report_data), buffer_map_(buffer_map),
273 memory_map_(memory_map), buffer_view_map_(buffer_view_map), sampler_map_(sampler_map), image_view_map_(image_view_map),
274 image_map_(image_map), image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600275 // Foreach binding, create default descriptors of given type
276 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
277 auto type = p_layout_->GetTypeFromIndex(i);
278 switch (type) {
279 case VK_DESCRIPTOR_TYPE_SAMPLER: {
280 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
281 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
282 if (immut_sampler)
Tobin Ehlis300888c2016-05-18 13:43:26 -0600283 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new SamplerDescriptor(immut_sampler + di)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600284 else
Tobin Ehlis300888c2016-05-18 13:43:26 -0600285 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new SamplerDescriptor()));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600286 }
287 break;
288 }
289 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
290 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
291 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
292 if (immut)
Tobin Ehlis300888c2016-05-18 13:43:26 -0600293 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new ImageSamplerDescriptor(immut + di)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600294 else
Tobin Ehlis300888c2016-05-18 13:43:26 -0600295 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new ImageSamplerDescriptor()));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600296 }
297 break;
298 }
299 // ImageDescriptors
300 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
301 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
302 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
303 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Tobin Ehlis300888c2016-05-18 13:43:26 -0600304 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new ImageDescriptor(type)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600305 break;
306 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
307 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
308 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Tobin Ehlis300888c2016-05-18 13:43:26 -0600309 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new TexelDescriptor(type)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600310 break;
311 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
312 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
313 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
314 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
315 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Tobin Ehlis300888c2016-05-18 13:43:26 -0600316 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new BufferDescriptor(type)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600317 break;
318 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600319 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600320 break;
321 }
322 }
323}
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600324cvdescriptorset::DescriptorSet::~DescriptorSet() {
325 InvalidateBoundCmdBuffers();
326 // Remove link to any cmd buffers
327 for (auto cb : bound_cmd_buffers_) {
328 for (uint32_t i=0; i<VK_PIPELINE_BIND_POINT_RANGE_SIZE; ++i) {
329 cb->lastBound[i].uniqueBoundSets.erase(this);
330 }
331 }
332}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600333// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
334bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
335 return layout->IsCompatible(p_layout_, error);
336}
337// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
338// This includes validating that all descriptors in the given bindings are updated,
339// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
340// Return true if state is acceptable, or false and write an error message into error string
341bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
342 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
343 for (auto binding : bindings) {
344 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600345 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600346 // Nothing to do for strictly immutable sampler
347 } else {
348 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
349 auto dyn_offset_index = 0;
350 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600351 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600352 std::stringstream error_str;
353 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
354 << " is being used in draw but has not been updated.";
355 *error = error_str.str();
356 return false;
357 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600358 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600359 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600360 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600361 auto buffer_node = buffer_map_->find(buffer);
362 if (buffer_node == buffer_map_->end()) {
363 std::stringstream error_str;
364 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
365 << " references invalid buffer " << buffer << ".";
366 *error = error_str.str();
367 return false;
368 } else {
369 auto mem_entry = memory_map_->find(buffer_node->second.mem);
370 if (mem_entry == memory_map_->end()) {
371 std::stringstream error_str;
372 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
373 << " uses buffer " << buffer << " that references invalid memory "
374 << buffer_node->second.mem << ".";
375 *error = error_str.str();
376 return false;
377 }
378 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600379 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600380 // Validate that dynamic offsets are within the buffer
381 auto buffer_size = buffer_node->second.createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600382 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
383 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600384 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
385 if (VK_WHOLE_SIZE == range) {
386 if ((dyn_offset + desc_offset) > buffer_size) {
387 std::stringstream error_str;
388 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
389 << " uses buffer " << buffer
390 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
391 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
392 << buffer_size << ".";
393 *error = error_str.str();
394 return false;
395 }
396 } else {
397 if ((dyn_offset + desc_offset + range) > buffer_size) {
398 std::stringstream error_str;
399 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
400 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
401 << " combined with offset " << desc_offset << " and range " << range
402 << " that oversteps the buffer size of " << buffer_size << ".";
403 *error = error_str.str();
404 return false;
405 }
406 }
407 }
408 }
409 }
410 }
411 }
412 }
413 return true;
414}
415// For given bindings, place any update buffers or images into the passed-in unordered_sets
416uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
417 std::unordered_set<VkBuffer> *buffer_set,
418 std::unordered_set<VkImageView> *image_set) const {
419 auto num_updates = 0;
420 for (auto binding : bindings) {
421 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600422 if (descriptors_[start_idx]->IsStorage()) {
423 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600424 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600425 if (descriptors_[start_idx + i]->updated) {
426 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600427 num_updates++;
428 }
429 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600430 } else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600431 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600432 if (descriptors_[start_idx + i]->updated) {
433 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600434 const auto &buff_pair = buffer_view_map_->find(bufferview);
435 if (buff_pair != buffer_view_map_->end()) {
436 buffer_set->insert(buff_pair->second.buffer);
437 num_updates++;
438 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600439 }
440 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600441 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600442 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600443 if (descriptors_[start_idx + i]->updated) {
444 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600445 num_updates++;
446 }
447 }
448 }
449 }
450 }
451 return num_updates;
452}
453// This is a special case for compute shaders that should eventually be removed once we have proper valid binding info for compute
454// case
455uint32_t cvdescriptorset::DescriptorSet::GetAllStorageUpdates(std::unordered_set<VkBuffer> *buffer_set,
456 std::unordered_set<VkImageView> *image_set) const {
457 std::unordered_set<uint32_t> binding_set;
458 p_layout_->FillBindingSet(&binding_set);
459 return GetStorageUpdates(binding_set, buffer_set, image_set);
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 Ehlis0a43bde2016-05-03 08:31:08 -0600469 auto num_updates = 0;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600470 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
471 // perform update
472 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
473 descriptors_[start_idx + di]->WriteUpdate(update, di);
474 ++num_updates;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600475 }
476 if (num_updates != 0) {
477 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600478 }
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600479 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600480}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600481// Validate Copy update
482bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
483 const DescriptorSet *src_set, std::string *error) {
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600484 // Verify idle ds
485 if (in_use.load()) {
486 std::stringstream error_str;
487 error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
488 << " that is in use by a command buffer.";
489 *error = error_str.str();
490 return false;
491 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600492 if (!p_layout_->HasBinding(update->dstBinding)) {
493 std::stringstream error_str;
494 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
495 *error = error_str.str();
496 return false;
497 }
498 if (!src_set->HasBinding(update->srcBinding)) {
499 std::stringstream error_str;
500 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
501 *error = error_str.str();
502 return false;
503 }
504 // src & dst set bindings are valid
505 // Check bounds of src & dst
506 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
507 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
508 // SRC update out of bounds
509 std::stringstream error_str;
510 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
511 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
512 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
513 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
514 *error = error_str.str();
515 return false;
516 }
517 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
518 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
519 // DST update out of bounds
520 std::stringstream error_str;
521 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
522 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
523 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
524 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
525 *error = error_str.str();
526 return false;
527 }
528 // Check that types match
529 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
530 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
531 if (src_type != dst_type) {
532 std::stringstream error_str;
533 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
534 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
535 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
536 *error = error_str.str();
537 return false;
538 }
539 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600540 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
541 "copy update from", src_set->GetSet(), error)) ||
542 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
543 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600544 return false;
545 }
Tobin Ehlisd41e7b62016-05-19 07:56:18 -0600546 // First make sure source descriptors are updated
547 for (uint32_t i = 0; i < update->descriptorCount; ++i) {
548 if (!src_set->descriptors_[src_start_idx + i]) {
549 std::stringstream error_str;
550 error_str << "Attempting copy update from descriptorSet " << src_set << " binding #" << update->srcBinding << " but descriptor at array offset "
551 << update->srcArrayElement + i << " has not been updated.";
552 *error = error_str.str();
553 return false;
554 }
555 }
556 // Update parameters all look good and descriptor updated so verify update contents
Tobin Ehlis300888c2016-05-18 13:43:26 -0600557 if (!VerifyCopyUpdateContents(update, src_set, src_start_idx, error))
558 return false;
559
560 // All checks passed so update is good
561 return true;
562}
563// Perform Copy update
564void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
565 auto num_updates = 0;
566 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
567 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600568 // Update parameters all look good so perform update
569 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600570 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600571 ++num_updates;
572 }
573 if (num_updates != 0) {
574 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600575 }
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600576 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600577}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600578cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600579 updated = false;
580 descriptor_class = PlainSampler;
581};
582
Tobin Ehlis300888c2016-05-18 13:43:26 -0600583cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600584 updated = false;
585 descriptor_class = PlainSampler;
586 if (immut) {
587 sampler_ = *immut;
588 immutable_ = true;
589 updated = true;
590 }
591}
592bool cvdescriptorset::ValidateSampler(const VkSampler sampler,
593 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600594 return (sampler_map->count(sampler) != 0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600595}
596bool cvdescriptorset::ValidateImageUpdate(const VkImageView image_view, const VkImageLayout image_layout,
597 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
598 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
599 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
600 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
601 std::string *error) {
602 auto image_pair = image_view_map->find(image_view);
603 if (image_pair == image_view_map->end()) {
604 std::stringstream error_str;
605 error_str << "Invalid VkImageView: " << image_view;
606 *error = error_str.str();
607 return false;
608 } else {
609 // Validate that imageLayout is compatible with aspect_mask and image format
610 VkImageAspectFlags aspect_mask = image_pair->second.subresourceRange.aspectMask;
611 VkImage image = image_pair->second.image;
612 VkFormat format = VK_FORMAT_MAX_ENUM;
613 auto img_pair = image_map->find(image);
614 if (img_pair != image_map->end()) {
615 format = img_pair->second.createInfo.format;
616 } else {
617 // Also need to check the swapchains.
618 auto swapchain_pair = image_to_swapchain_map->find(image);
619 if (swapchain_pair != image_to_swapchain_map->end()) {
620 VkSwapchainKHR swapchain = swapchain_pair->second;
621 auto swapchain_pair = swapchain_map->find(swapchain);
622 if (swapchain_pair != swapchain_map->end()) {
623 format = swapchain_pair->second->createInfo.imageFormat;
624 }
625 }
626 }
627 if (format == VK_FORMAT_MAX_ENUM) {
628 std::stringstream error_str;
629 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
630 *error = error_str.str();
631 return false;
632 } else {
633 bool ds = vk_format_is_depth_or_stencil(format);
634 switch (image_layout) {
635 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
636 // Only Color bit must be set
637 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
638 std::stringstream error_str;
639 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
640 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
641 *error = error_str.str();
642 return false;
643 }
644 // format must NOT be DS
645 if (ds) {
646 std::stringstream error_str;
647 error_str << "ImageView (" << image_view
648 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
649 << string_VkFormat(format) << " which is not a color format.";
650 *error = error_str.str();
651 return false;
652 }
653 break;
654 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
655 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
656 // Depth or stencil bit must be set, but both must NOT be set
657 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
658 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
659 // both must NOT be set
660 std::stringstream error_str;
661 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
662 *error = error_str.str();
663 return false;
664 }
665 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
666 // Neither were set
667 std::stringstream error_str;
668 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
669 << " but does not have STENCIL or DEPTH aspects set";
670 *error = error_str.str();
671 return false;
672 }
673 // format must be DS
674 if (!ds) {
675 std::stringstream error_str;
676 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
677 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
678 *error = error_str.str();
679 return false;
680 }
681 break;
682 default:
683 // anything to check for other layouts?
684 break;
685 }
686 }
687 }
688 return true;
689}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600690void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
691 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600692 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600693}
694
Tobin Ehlis300888c2016-05-18 13:43:26 -0600695void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600696 if (!immutable_) {
697 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600698 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600699 }
700 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600701}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600702cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
703 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600704 updated = false;
705 descriptor_class = ImageSampler;
706}
707
Tobin Ehlis300888c2016-05-18 13:43:26 -0600708cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
709 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600710 updated = false;
711 descriptor_class = ImageSampler;
712 if (immut) {
713 sampler_ = *immut;
714 immutable_ = true;
715 updated = true;
716 }
717}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600718void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600719 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600720 auto image_info = update->pImageInfo[index];
721 sampler_ = image_info.sampler;
722 image_view_ = image_info.imageView;
723 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600724}
725
Tobin Ehlis300888c2016-05-18 13:43:26 -0600726void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600727 if (!immutable_) {
728 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600729 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600730 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600731 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
732 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600733 updated = true;
734 image_view_ = image_view;
735 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600736}
737
Tobin Ehlis300888c2016-05-18 13:43:26 -0600738cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
739 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600740 updated = false;
741 descriptor_class = Image;
742 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
743 storage_ = true;
744};
745
Tobin Ehlis300888c2016-05-18 13:43:26 -0600746void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600747 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600748 auto image_info = update->pImageInfo[index];
749 image_view_ = image_info.imageView;
750 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600751}
752
Tobin Ehlis300888c2016-05-18 13:43:26 -0600753void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600754 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
755 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600756 updated = true;
757 image_view_ = image_view;
758 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600759}
760
Tobin Ehlis300888c2016-05-18 13:43:26 -0600761cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
762 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600763 updated = false;
764 descriptor_class = GeneralBuffer;
765 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
766 dynamic_ = true;
767 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
768 storage_ = true;
769 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
770 dynamic_ = true;
771 storage_ = true;
772 }
773}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600774void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600775 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600776 auto buffer_info = update->pBufferInfo[index];
777 buffer_ = buffer_info.buffer;
778 offset_ = buffer_info.offset;
779 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600780}
781
Tobin Ehlis300888c2016-05-18 13:43:26 -0600782void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
783 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600784 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600785 buffer_ = buff_desc->buffer_;
786 offset_ = buff_desc->offset_;
787 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600788}
789
Tobin Ehlis300888c2016-05-18 13:43:26 -0600790cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600791 updated = false;
792 descriptor_class = TexelBuffer;
793 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
794 storage_ = true;
795};
Tobin Ehlis300888c2016-05-18 13:43:26 -0600796void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600797 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600798 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600799}
800
Tobin Ehlis300888c2016-05-18 13:43:26 -0600801void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
802 updated = true;
803 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
804}
805// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
806// sets, and then calls their respective Validate[Write|Copy]Update functions.
807// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
808// be skipped, then true is returned.
809// If there is no issue with the update, then false is returned.
810bool cvdescriptorset::ValidateUpdateDescriptorSets(
811 const debug_report_data *report_data, const std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> &set_map,
812 const uint32_t write_count, const VkWriteDescriptorSet *p_wds, const uint32_t copy_count, const VkCopyDescriptorSet *p_cds) {
813 bool skip_call = false;
814 // Validate Write updates
815 uint32_t i = 0;
816 for (i = 0; i < write_count; i++) {
817 auto dest_set = p_wds[i].dstSet;
818 auto set_pair = set_map.find(dest_set);
819 if (set_pair == set_map.end()) {
820 skip_call |=
821 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
822 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
823 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
824 reinterpret_cast<uint64_t &>(dest_set));
825 } else {
826 std::string error_str;
827 if (!set_pair->second->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
828 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
829 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
830 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
831 " with error: %s",
832 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
833 }
834 }
835 }
836 // Now validate copy updates
837 for (i = 0; i < copy_count; ++i) {
838 auto dst_set = p_cds[i].dstSet;
839 auto src_set = p_cds[i].srcSet;
840 auto src_pair = set_map.find(src_set);
841 auto dst_pair = set_map.find(dst_set);
842 if (src_pair == set_map.end()) {
843 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
844 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
845 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
846 " that has not been allocated.",
847 reinterpret_cast<uint64_t &>(src_set));
848 } else if (dst_pair == set_map.end()) {
849 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
850 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
851 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
852 " that has not been allocated.",
853 reinterpret_cast<uint64_t &>(dst_set));
854 } else {
855 std::string error_str;
856 if (!dst_pair->second->ValidateCopyUpdate(report_data, &p_cds[i], src_pair->second, &error_str)) {
857 skip_call |=
858 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
859 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
860 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
861 " to Descriptor Set 0x%" PRIx64 " with error: %s",
862 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
863 }
864 }
865 }
866 return skip_call;
867}
868// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
869// sets, and then calls their respective Perform[Write|Copy]Update functions.
870// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
871// with the same set of updates.
872// This is split from the validate code to allow validation prior to calling down the chain, and then update after
873// calling down the chain.
874void cvdescriptorset::PerformUpdateDescriptorSets(
875 const std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> &set_map, const uint32_t write_count,
876 const VkWriteDescriptorSet *p_wds, const uint32_t copy_count, const VkCopyDescriptorSet *p_cds) {
877 // Write updates first
878 uint32_t i = 0;
879 for (i = 0; i < write_count; ++i) {
880 auto dest_set = p_wds[i].dstSet;
881 auto set_pair = set_map.find(dest_set);
882 if (set_pair != set_map.end()) {
883 set_pair->second->PerformWriteUpdate(&p_wds[i]);
884 }
885 }
886 // Now copy updates
887 for (i = 0; i < copy_count; ++i) {
888 auto dst_set = p_cds[i].dstSet;
889 auto src_set = p_cds[i].srcSet;
890 auto src_pair = set_map.find(src_set);
891 auto dst_pair = set_map.find(dst_set);
892 if (src_pair != set_map.end() && dst_pair != set_map.end()) {
893 dst_pair->second->PerformCopyUpdate(&p_cds[i], src_pair->second);
894 }
895 }
896}
897// Validate the state for a given write update but don't actually perform the update
898// If an error would occur for this update, return false and fill in details in error_msg string
899bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
900 std::string *error_msg) {
901 // Verify idle ds
902 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600903 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600904 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
905 << " that is in use by a command buffer.";
906 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600907 return false;
908 }
Tobin Ehlis300888c2016-05-18 13:43:26 -0600909 // Verify dst binding exists
910 if (!p_layout_->HasBinding(update->dstBinding)) {
911 std::stringstream error_str;
912 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
913 *error_msg = error_str.str();
914 return false;
915 } else {
916 // We know that binding is valid, verify update and do update on each descriptor
917 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
918 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
919 if (type != update->descriptorType) {
920 std::stringstream error_str;
921 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
922 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
923 *error_msg = error_str.str();
924 return false;
925 }
926 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
927 std::stringstream error_str;
928 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
929 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
930 << " descriptors starting at binding offset of "
931 << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
932 << " combined with update array element offset of " << update->dstArrayElement
933 << " oversteps the size of this descriptor set.";
934 *error_msg = error_str.str();
935 return false;
936 }
937 // Verify consecutive bindings match (if needed)
938 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount,
939 "write update to", set_, error_msg))
940 return false;
941 // Update is within bounds and consistent so last step is to validate update contents
942 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
943 std::stringstream error_str;
944 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
945 << " failed with error message: " << error_msg->c_str();
946 *error_msg = error_str.str();
947 return false;
948 }
949 }
950 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600951 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600952}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600953// Verify that the contents of the update are ok, but don't perform actual update
954bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
955 std::string *error) const {
956 switch (update->descriptorType) {
957 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
958 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
959 // Validate image
960 auto image_view = update->pImageInfo[di].imageView;
961 auto image_layout = update->pImageInfo[di].imageLayout;
962 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
963 error)) {
964 std::stringstream error_str;
965 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
966 *error = error_str.str();
967 return false;
968 }
969 }
970 // Intentional fall-through to validate sampler
971 }
972 case VK_DESCRIPTOR_TYPE_SAMPLER: {
973 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
974 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
975 if (!ValidateSampler(update->pImageInfo[di].sampler, sampler_map_)) {
976 std::stringstream error_str;
977 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
978 << update->pImageInfo[di].sampler << ".";
979 *error = error_str.str();
980 return false;
981 }
982 } else {
983 // TODO : Warn here
984 }
985 }
986 break;
987 }
988 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
989 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
990 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
991 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
992 auto image_view = update->pImageInfo[di].imageView;
993 auto image_layout = update->pImageInfo[di].imageLayout;
994 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
995 error)) {
996 std::stringstream error_str;
997 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
998 *error = error_str.str();
999 return false;
1000 }
1001 }
1002 break;
1003 }
1004 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1005 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
1006 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1007 auto buffer_view = update->pTexelBufferView[di];
1008 if (!buffer_view_map_->count(buffer_view)) {
1009 std::stringstream error_str;
1010 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1011 *error = error_str.str();
1012 return false;
1013 }
1014 }
1015 break;
1016 }
1017 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1018 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1019 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1020 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1021 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1022 auto buffer = update->pBufferInfo[di].buffer;
1023 if (!buffer_map_->count(buffer)) {
1024 std::stringstream error_str;
1025 error_str << "Attempted write update to buffer descriptor with invalid buffer: " << buffer;
1026 *error = error_str.str();
1027 return false;
1028 }
1029 }
1030 break;
1031 }
1032 default:
1033 assert(0); // We've already verified update type so should never get here
1034 break;
1035 }
1036 // All checks passed so update contents are good
1037 return true;
1038}
1039// Verify that the contents of the update are ok, but don't perform actual update
1040bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
1041 const uint32_t index, std::string *error) const {
1042 switch (src_set->descriptors_[index]->descriptor_class) {
1043 case PlainSampler: {
1044 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1045 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1046 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
1047 if (!ValidateSampler(update_sampler, sampler_map_)) {
1048 std::stringstream error_str;
1049 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1050 *error = error_str.str();
1051 return false;
1052 }
1053 } else {
1054 // TODO : Warn here
1055 }
1056 }
1057 break;
1058 }
1059 case ImageSampler: {
1060 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1061 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1062 // First validate sampler
1063 if (!img_samp_desc->IsImmutableSampler()) {
1064 auto update_sampler = img_samp_desc->GetSampler();
1065 if (!ValidateSampler(update_sampler, sampler_map_)) {
1066 std::stringstream error_str;
1067 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1068 *error = error_str.str();
1069 return false;
1070 }
1071 } else {
1072 // TODO : Warn here
1073 }
1074 // Validate image
1075 auto image_view = img_samp_desc->GetImageView();
1076 auto image_layout = img_samp_desc->GetImageLayout();
1077 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
1078 error)) {
1079 std::stringstream error_str;
1080 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1081 *error = error_str.str();
1082 return false;
1083 }
1084 }
1085 }
1086 case Image: {
1087 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1088 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1089 auto image_view = img_desc->GetImageView();
1090 auto image_layout = img_desc->GetImageLayout();
1091 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
1092 error)) {
1093 std::stringstream error_str;
1094 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1095 *error = error_str.str();
1096 return false;
1097 }
1098 }
1099 break;
1100 }
1101 case TexelBuffer: {
1102 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1103 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
1104 if (!buffer_view_map_->count(buffer_view)) {
1105 std::stringstream error_str;
1106 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1107 *error = error_str.str();
1108 return false;
1109 }
1110 }
1111 break;
1112 }
1113 case GeneralBuffer: {
1114 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1115 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
1116 if (!buffer_map_->count(buffer)) {
1117 std::stringstream error_str;
1118 error_str << "Attempted write update to buffer descriptor with invalid buffer: " << buffer;
1119 *error = error_str.str();
1120 return false;
1121 }
1122 }
1123 break;
1124 }
1125 default:
1126 assert(0); // We've already verified update type so should never get here
1127 break;
1128 }
1129 // All checks passed so update contents are good
1130 return true;
1131}