blob: 2955a6a352310e9ffeb8f8eab3b8600bbe5100fd [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 Ehlis300888c2016-05-18 13:43:26 -0600546 // Update parameters all look good so verify update contents
547 if (!VerifyCopyUpdateContents(update, src_set, src_start_idx, error))
548 return false;
549
550 // All checks passed so update is good
551 return true;
552}
553// Perform Copy update
554void cvdescriptorset::DescriptorSet::PerformCopyUpdate(const VkCopyDescriptorSet *update, const DescriptorSet *src_set) {
555 auto num_updates = 0;
556 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
557 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600558 // Update parameters all look good so perform update
559 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis300888c2016-05-18 13:43:26 -0600560 descriptors_[dst_start_idx + di]->CopyUpdate(src_set->descriptors_[src_start_idx + di].get());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600561 ++num_updates;
562 }
563 if (num_updates != 0) {
564 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600565 }
Tobin Ehlis9906d9d2016-05-17 14:23:46 -0600566 InvalidateBoundCmdBuffers();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600567}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600568cvdescriptorset::SamplerDescriptor::SamplerDescriptor() : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600569 updated = false;
570 descriptor_class = PlainSampler;
571};
572
Tobin Ehlis300888c2016-05-18 13:43:26 -0600573cvdescriptorset::SamplerDescriptor::SamplerDescriptor(const VkSampler *immut) : sampler_(VK_NULL_HANDLE), immutable_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600574 updated = false;
575 descriptor_class = PlainSampler;
576 if (immut) {
577 sampler_ = *immut;
578 immutable_ = true;
579 updated = true;
580 }
581}
582bool cvdescriptorset::ValidateSampler(const VkSampler sampler,
583 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600584 return (sampler_map->count(sampler) != 0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600585}
586bool cvdescriptorset::ValidateImageUpdate(const VkImageView image_view, const VkImageLayout image_layout,
587 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
588 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
589 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
590 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
591 std::string *error) {
592 auto image_pair = image_view_map->find(image_view);
593 if (image_pair == image_view_map->end()) {
594 std::stringstream error_str;
595 error_str << "Invalid VkImageView: " << image_view;
596 *error = error_str.str();
597 return false;
598 } else {
599 // Validate that imageLayout is compatible with aspect_mask and image format
600 VkImageAspectFlags aspect_mask = image_pair->second.subresourceRange.aspectMask;
601 VkImage image = image_pair->second.image;
602 VkFormat format = VK_FORMAT_MAX_ENUM;
603 auto img_pair = image_map->find(image);
604 if (img_pair != image_map->end()) {
605 format = img_pair->second.createInfo.format;
606 } else {
607 // Also need to check the swapchains.
608 auto swapchain_pair = image_to_swapchain_map->find(image);
609 if (swapchain_pair != image_to_swapchain_map->end()) {
610 VkSwapchainKHR swapchain = swapchain_pair->second;
611 auto swapchain_pair = swapchain_map->find(swapchain);
612 if (swapchain_pair != swapchain_map->end()) {
613 format = swapchain_pair->second->createInfo.imageFormat;
614 }
615 }
616 }
617 if (format == VK_FORMAT_MAX_ENUM) {
618 std::stringstream error_str;
619 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
620 *error = error_str.str();
621 return false;
622 } else {
623 bool ds = vk_format_is_depth_or_stencil(format);
624 switch (image_layout) {
625 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
626 // Only Color bit must be set
627 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
628 std::stringstream error_str;
629 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
630 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
631 *error = error_str.str();
632 return false;
633 }
634 // format must NOT be DS
635 if (ds) {
636 std::stringstream error_str;
637 error_str << "ImageView (" << image_view
638 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
639 << string_VkFormat(format) << " which is not a color format.";
640 *error = error_str.str();
641 return false;
642 }
643 break;
644 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
645 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
646 // Depth or stencil bit must be set, but both must NOT be set
647 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
648 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
649 // both must NOT be set
650 std::stringstream error_str;
651 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
652 *error = error_str.str();
653 return false;
654 }
655 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
656 // Neither were set
657 std::stringstream error_str;
658 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
659 << " but does not have STENCIL or DEPTH aspects set";
660 *error = error_str.str();
661 return false;
662 }
663 // format must be DS
664 if (!ds) {
665 std::stringstream error_str;
666 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
667 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
668 *error = error_str.str();
669 return false;
670 }
671 break;
672 default:
673 // anything to check for other layouts?
674 break;
675 }
676 }
677 }
678 return true;
679}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600680void cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
681 sampler_ = update->pImageInfo[index].sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600682 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600683}
684
Tobin Ehlis300888c2016-05-18 13:43:26 -0600685void cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600686 if (!immutable_) {
687 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600688 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600689 }
690 updated = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600691}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600692cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor()
693 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600694 updated = false;
695 descriptor_class = ImageSampler;
696}
697
Tobin Ehlis300888c2016-05-18 13:43:26 -0600698cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(const VkSampler *immut)
699 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600700 updated = false;
701 descriptor_class = ImageSampler;
702 if (immut) {
703 sampler_ = *immut;
704 immutable_ = true;
705 updated = true;
706 }
707}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600708void cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600709 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600710 auto image_info = update->pImageInfo[index];
711 sampler_ = image_info.sampler;
712 image_view_ = image_info.imageView;
713 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600714}
715
Tobin Ehlis300888c2016-05-18 13:43:26 -0600716void cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600717 if (!immutable_) {
718 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600719 sampler_ = update_sampler;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600720 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600721 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
722 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600723 updated = true;
724 image_view_ = image_view;
725 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600726}
727
Tobin Ehlis300888c2016-05-18 13:43:26 -0600728cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type)
729 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600730 updated = false;
731 descriptor_class = Image;
732 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
733 storage_ = true;
734};
735
Tobin Ehlis300888c2016-05-18 13:43:26 -0600736void cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600737 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600738 auto image_info = update->pImageInfo[index];
739 image_view_ = image_info.imageView;
740 image_layout_ = image_info.imageLayout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600741}
742
Tobin Ehlis300888c2016-05-18 13:43:26 -0600743void cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600744 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
745 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600746 updated = true;
747 image_view_ = image_view;
748 image_layout_ = image_layout;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600749}
750
Tobin Ehlis300888c2016-05-18 13:43:26 -0600751cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type)
752 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600753 updated = false;
754 descriptor_class = GeneralBuffer;
755 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
756 dynamic_ = true;
757 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
758 storage_ = true;
759 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
760 dynamic_ = true;
761 storage_ = true;
762 }
763}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600764void cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600765 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600766 auto buffer_info = update->pBufferInfo[index];
767 buffer_ = buffer_info.buffer;
768 offset_ = buffer_info.offset;
769 range_ = buffer_info.range;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600770}
771
Tobin Ehlis300888c2016-05-18 13:43:26 -0600772void cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src) {
773 auto buff_desc = static_cast<const BufferDescriptor *>(src);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600774 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600775 buffer_ = buff_desc->buffer_;
776 offset_ = buff_desc->offset_;
777 range_ = buff_desc->range_;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600778}
779
Tobin Ehlis300888c2016-05-18 13:43:26 -0600780cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type) : buffer_view_(VK_NULL_HANDLE), storage_(false) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600781 updated = false;
782 descriptor_class = TexelBuffer;
783 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
784 storage_ = true;
785};
Tobin Ehlis300888c2016-05-18 13:43:26 -0600786void cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600787 updated = true;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600788 buffer_view_ = update->pTexelBufferView[index];
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600789}
790
Tobin Ehlis300888c2016-05-18 13:43:26 -0600791void cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src) {
792 updated = true;
793 buffer_view_ = static_cast<const TexelDescriptor *>(src)->buffer_view_;
794}
795// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
796// sets, and then calls their respective Validate[Write|Copy]Update functions.
797// If the update hits an issue for which the callback returns "true", meaning that the call down the chain should
798// be skipped, then true is returned.
799// If there is no issue with the update, then false is returned.
800bool cvdescriptorset::ValidateUpdateDescriptorSets(
801 const debug_report_data *report_data, const std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> &set_map,
802 const uint32_t write_count, const VkWriteDescriptorSet *p_wds, const uint32_t copy_count, const VkCopyDescriptorSet *p_cds) {
803 bool skip_call = false;
804 // Validate Write updates
805 uint32_t i = 0;
806 for (i = 0; i < write_count; i++) {
807 auto dest_set = p_wds[i].dstSet;
808 auto set_pair = set_map.find(dest_set);
809 if (set_pair == set_map.end()) {
810 skip_call |=
811 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
812 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
813 "Cannot call vkUpdateDescriptorSets() on descriptor set 0x%" PRIxLEAST64 " that has not been allocated.",
814 reinterpret_cast<uint64_t &>(dest_set));
815 } else {
816 std::string error_str;
817 if (!set_pair->second->ValidateWriteUpdate(report_data, &p_wds[i], &error_str)) {
818 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
819 reinterpret_cast<uint64_t &>(dest_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
820 "vkUpdateDescriptorsSets() failed write update validation for Descriptor Set 0x%" PRIx64
821 " with error: %s",
822 reinterpret_cast<uint64_t &>(dest_set), error_str.c_str());
823 }
824 }
825 }
826 // Now validate copy updates
827 for (i = 0; i < copy_count; ++i) {
828 auto dst_set = p_cds[i].dstSet;
829 auto src_set = p_cds[i].srcSet;
830 auto src_pair = set_map.find(src_set);
831 auto dst_pair = set_map.find(dst_set);
832 if (src_pair == set_map.end()) {
833 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
834 reinterpret_cast<uint64_t &>(src_set), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
835 "Cannot call vkUpdateDescriptorSets() to copy from descriptor set 0x%" PRIxLEAST64
836 " that has not been allocated.",
837 reinterpret_cast<uint64_t &>(src_set));
838 } else if (dst_pair == set_map.end()) {
839 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
840 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
841 "Cannot call vkUpdateDescriptorSets() to copy to descriptor set 0x%" PRIxLEAST64
842 " that has not been allocated.",
843 reinterpret_cast<uint64_t &>(dst_set));
844 } else {
845 std::string error_str;
846 if (!dst_pair->second->ValidateCopyUpdate(report_data, &p_cds[i], src_pair->second, &error_str)) {
847 skip_call |=
848 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT,
849 reinterpret_cast<uint64_t &>(dst_set), __LINE__, DRAWSTATE_INVALID_UPDATE_INDEX, "DS",
850 "vkUpdateDescriptorsSets() failed copy update from Descriptor Set 0x%" PRIx64
851 " to Descriptor Set 0x%" PRIx64 " with error: %s",
852 reinterpret_cast<uint64_t &>(src_set), reinterpret_cast<uint64_t &>(dst_set), error_str.c_str());
853 }
854 }
855 }
856 return skip_call;
857}
858// This is a helper function that iterates over a set of Write and Copy updates, pulls the DescriptorSet* for updated
859// sets, and then calls their respective Perform[Write|Copy]Update functions.
860// Prerequisite : ValidateUpdateDescriptorSets() should be called and return "false" prior to calling PerformUpdateDescriptorSets()
861// with the same set of updates.
862// This is split from the validate code to allow validation prior to calling down the chain, and then update after
863// calling down the chain.
864void cvdescriptorset::PerformUpdateDescriptorSets(
865 const std::unordered_map<VkDescriptorSet, cvdescriptorset::DescriptorSet *> &set_map, const uint32_t write_count,
866 const VkWriteDescriptorSet *p_wds, const uint32_t copy_count, const VkCopyDescriptorSet *p_cds) {
867 // Write updates first
868 uint32_t i = 0;
869 for (i = 0; i < write_count; ++i) {
870 auto dest_set = p_wds[i].dstSet;
871 auto set_pair = set_map.find(dest_set);
872 if (set_pair != set_map.end()) {
873 set_pair->second->PerformWriteUpdate(&p_wds[i]);
874 }
875 }
876 // Now copy updates
877 for (i = 0; i < copy_count; ++i) {
878 auto dst_set = p_cds[i].dstSet;
879 auto src_set = p_cds[i].srcSet;
880 auto src_pair = set_map.find(src_set);
881 auto dst_pair = set_map.find(dst_set);
882 if (src_pair != set_map.end() && dst_pair != set_map.end()) {
883 dst_pair->second->PerformCopyUpdate(&p_cds[i], src_pair->second);
884 }
885 }
886}
887// Validate the state for a given write update but don't actually perform the update
888// If an error would occur for this update, return false and fill in details in error_msg string
889bool cvdescriptorset::DescriptorSet::ValidateWriteUpdate(const debug_report_data *report_data, const VkWriteDescriptorSet *update,
890 std::string *error_msg) {
891 // Verify idle ds
892 if (in_use.load()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600893 std::stringstream error_str;
Tobin Ehlis300888c2016-05-18 13:43:26 -0600894 error_str << "Cannot call vkUpdateDescriptorSets() to perform write update on descriptor set " << set_
895 << " that is in use by a command buffer.";
896 *error_msg = error_str.str();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600897 return false;
898 }
Tobin Ehlis300888c2016-05-18 13:43:26 -0600899 // Verify dst binding exists
900 if (!p_layout_->HasBinding(update->dstBinding)) {
901 std::stringstream error_str;
902 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
903 *error_msg = error_str.str();
904 return false;
905 } else {
906 // We know that binding is valid, verify update and do update on each descriptor
907 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
908 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
909 if (type != update->descriptorType) {
910 std::stringstream error_str;
911 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
912 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
913 *error_msg = error_str.str();
914 return false;
915 }
916 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
917 std::stringstream error_str;
918 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
919 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
920 << " descriptors starting at binding offset of "
921 << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
922 << " combined with update array element offset of " << update->dstArrayElement
923 << " oversteps the size of this descriptor set.";
924 *error_msg = error_str.str();
925 return false;
926 }
927 // Verify consecutive bindings match (if needed)
928 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount,
929 "write update to", set_, error_msg))
930 return false;
931 // Update is within bounds and consistent so last step is to validate update contents
932 if (!VerifyWriteUpdateContents(update, start_idx, error_msg)) {
933 std::stringstream error_str;
934 error_str << "Write update to descriptor in set " << set_ << " binding #" << update->dstBinding
935 << " failed with error message: " << error_msg->c_str();
936 *error_msg = error_str.str();
937 return false;
938 }
939 }
940 // All checks passed, update is clean
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600941 return true;
Tobin Ehlis03d61de2016-05-17 08:31:46 -0600942}
Tobin Ehlis300888c2016-05-18 13:43:26 -0600943// Verify that the contents of the update are ok, but don't perform actual update
944bool cvdescriptorset::DescriptorSet::VerifyWriteUpdateContents(const VkWriteDescriptorSet *update, const uint32_t index,
945 std::string *error) const {
946 switch (update->descriptorType) {
947 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
948 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
949 // Validate image
950 auto image_view = update->pImageInfo[di].imageView;
951 auto image_layout = update->pImageInfo[di].imageLayout;
952 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
953 error)) {
954 std::stringstream error_str;
955 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
956 *error = error_str.str();
957 return false;
958 }
959 }
960 // Intentional fall-through to validate sampler
961 }
962 case VK_DESCRIPTOR_TYPE_SAMPLER: {
963 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
964 if (!descriptors_[index + di].get()->IsImmutableSampler()) {
965 if (!ValidateSampler(update->pImageInfo[di].sampler, sampler_map_)) {
966 std::stringstream error_str;
967 error_str << "Attempted write update to sampler descriptor with invalid sampler: "
968 << update->pImageInfo[di].sampler << ".";
969 *error = error_str.str();
970 return false;
971 }
972 } else {
973 // TODO : Warn here
974 }
975 }
976 break;
977 }
978 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
979 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
980 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
981 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
982 auto image_view = update->pImageInfo[di].imageView;
983 auto image_layout = update->pImageInfo[di].imageLayout;
984 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
985 error)) {
986 std::stringstream error_str;
987 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
988 *error = error_str.str();
989 return false;
990 }
991 }
992 break;
993 }
994 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
995 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
996 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
997 auto buffer_view = update->pTexelBufferView[di];
998 if (!buffer_view_map_->count(buffer_view)) {
999 std::stringstream error_str;
1000 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1001 *error = error_str.str();
1002 return false;
1003 }
1004 }
1005 break;
1006 }
1007 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1008 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1009 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1010 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
1011 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1012 auto buffer = update->pBufferInfo[di].buffer;
1013 if (!buffer_map_->count(buffer)) {
1014 std::stringstream error_str;
1015 error_str << "Attempted write update to buffer descriptor with invalid buffer: " << buffer;
1016 *error = error_str.str();
1017 return false;
1018 }
1019 }
1020 break;
1021 }
1022 default:
1023 assert(0); // We've already verified update type so should never get here
1024 break;
1025 }
1026 // All checks passed so update contents are good
1027 return true;
1028}
1029// Verify that the contents of the update are ok, but don't perform actual update
1030bool cvdescriptorset::DescriptorSet::VerifyCopyUpdateContents(const VkCopyDescriptorSet *update, const DescriptorSet *src_set,
1031 const uint32_t index, std::string *error) const {
1032 switch (src_set->descriptors_[index]->descriptor_class) {
1033 case PlainSampler: {
1034 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1035 if (!src_set->descriptors_[index + di]->IsImmutableSampler()) {
1036 auto update_sampler = static_cast<SamplerDescriptor *>(src_set->descriptors_[index + di].get())->GetSampler();
1037 if (!ValidateSampler(update_sampler, sampler_map_)) {
1038 std::stringstream error_str;
1039 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1040 *error = error_str.str();
1041 return false;
1042 }
1043 } else {
1044 // TODO : Warn here
1045 }
1046 }
1047 break;
1048 }
1049 case ImageSampler: {
1050 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1051 auto img_samp_desc = static_cast<const ImageSamplerDescriptor *>(src_set->descriptors_[index + di].get());
1052 // First validate sampler
1053 if (!img_samp_desc->IsImmutableSampler()) {
1054 auto update_sampler = img_samp_desc->GetSampler();
1055 if (!ValidateSampler(update_sampler, sampler_map_)) {
1056 std::stringstream error_str;
1057 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
1058 *error = error_str.str();
1059 return false;
1060 }
1061 } else {
1062 // TODO : Warn here
1063 }
1064 // Validate image
1065 auto image_view = img_samp_desc->GetImageView();
1066 auto image_layout = img_samp_desc->GetImageLayout();
1067 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
1068 error)) {
1069 std::stringstream error_str;
1070 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
1071 *error = error_str.str();
1072 return false;
1073 }
1074 }
1075 }
1076 case Image: {
1077 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1078 auto img_desc = static_cast<const ImageDescriptor *>(src_set->descriptors_[index + di].get());
1079 auto image_view = img_desc->GetImageView();
1080 auto image_layout = img_desc->GetImageLayout();
1081 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
1082 error)) {
1083 std::stringstream error_str;
1084 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
1085 *error = error_str.str();
1086 return false;
1087 }
1088 }
1089 break;
1090 }
1091 case TexelBuffer: {
1092 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1093 auto buffer_view = static_cast<TexelDescriptor *>(src_set->descriptors_[index + di].get())->GetBufferView();
1094 if (!buffer_view_map_->count(buffer_view)) {
1095 std::stringstream error_str;
1096 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
1097 *error = error_str.str();
1098 return false;
1099 }
1100 }
1101 break;
1102 }
1103 case GeneralBuffer: {
1104 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
1105 auto buffer = static_cast<BufferDescriptor *>(src_set->descriptors_[index + di].get())->GetBuffer();
1106 if (!buffer_map_->count(buffer)) {
1107 std::stringstream error_str;
1108 error_str << "Attempted write update to buffer descriptor with invalid buffer: " << buffer;
1109 *error = error_str.str();
1110 return false;
1111 }
1112 }
1113 break;
1114 }
1115 default:
1116 assert(0); // We've already verified update type so should never get here
1117 break;
1118 }
1119 // All checks passed so update contents are good
1120 return true;
1121}