blob: 89c44c680e542086237aeb14b9e4ff20140c6bdf [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,
263 const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map,
264 const std::unordered_map<VkDeviceMemory, DEVICE_MEM_INFO> *memory_map,
265 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map,
266 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map,
267 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
268 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
269 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
270 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
Tobin Ehlisad412f42016-05-05 10:30:46 -0600271 : some_update_(false), set_(set), p_layout_(layout), buffer_map_(buffer_map), memory_map_(memory_map),
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600272 buffer_view_map_(buffer_view_map), sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map),
273 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
274 // Foreach binding, create default descriptors of given type
275 for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
276 auto type = p_layout_->GetTypeFromIndex(i);
277 switch (type) {
278 case VK_DESCRIPTOR_TYPE_SAMPLER: {
279 auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
280 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
281 if (immut_sampler)
Tobin Ehlis81f17852016-05-05 09:04:33 -0600282 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new SamplerDescriptor(immut_sampler + di, sampler_map_)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600283 else
Tobin Ehlis81f17852016-05-05 09:04:33 -0600284 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new SamplerDescriptor(sampler_map_)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600285 }
286 break;
287 }
288 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
289 auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
290 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
291 if (immut)
Tobin Ehlis81f17852016-05-05 09:04:33 -0600292 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new ImageSamplerDescriptor(
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600293 immut + di, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_, sampler_map_)));
294 else
Tobin Ehlis81f17852016-05-05 09:04:33 -0600295 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new ImageSamplerDescriptor(
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600296 image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_, sampler_map_)));
297 }
298 break;
299 }
300 // ImageDescriptors
301 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
302 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
303 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
304 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Tobin Ehlis81f17852016-05-05 09:04:33 -0600305 descriptors_.emplace_back(std::unique_ptr<Descriptor>(
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600306 new ImageDescriptor(type, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_)));
307 break;
308 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
309 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
310 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Tobin Ehlis81f17852016-05-05 09:04:33 -0600311 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new TexelDescriptor(type, buffer_view_map_)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600312 break;
313 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
314 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
315 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
316 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
317 for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
Tobin Ehlis81f17852016-05-05 09:04:33 -0600318 descriptors_.emplace_back(std::unique_ptr<Descriptor>(new BufferDescriptor(type, buffer_map_)));
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600319 break;
320 default:
Tobin Ehlis81f17852016-05-05 09:04:33 -0600321 assert(0); // Bad descriptor type specified
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600322 break;
323 }
324 }
325}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600326// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
327bool cvdescriptorset::DescriptorSet::IsCompatible(const DescriptorSetLayout *layout, std::string *error) const {
328 return layout->IsCompatible(p_layout_, error);
329}
330// Validate that the state of this set is appropriate for the given bindings and dynami_offsets at Draw time
331// This includes validating that all descriptors in the given bindings are updated,
332// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
333// Return true if state is acceptable, or false and write an error message into error string
334bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::unordered_set<uint32_t> &bindings,
335 const std::vector<uint32_t> &dynamic_offsets, std::string *error) const {
336 for (auto binding : bindings) {
337 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600338 if (descriptors_[start_idx]->IsImmutableSampler()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600339 // Nothing to do for strictly immutable sampler
340 } else {
341 auto end_idx = p_layout_->GetGlobalEndIndexFromBinding(binding);
342 auto dyn_offset_index = 0;
343 for (uint32_t i = start_idx; i <= end_idx; ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600344 if (!descriptors_[i]->updated) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600345 std::stringstream error_str;
346 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
347 << " is being used in draw but has not been updated.";
348 *error = error_str.str();
349 return false;
350 } else {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600351 if (GeneralBuffer == descriptors_[i]->GetClass()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600352 // Verify that buffers are valid
Tobin Ehlis81f17852016-05-05 09:04:33 -0600353 auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600354 auto buffer_node = buffer_map_->find(buffer);
355 if (buffer_node == buffer_map_->end()) {
356 std::stringstream error_str;
357 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
358 << " references invalid buffer " << buffer << ".";
359 *error = error_str.str();
360 return false;
361 } else {
362 auto mem_entry = memory_map_->find(buffer_node->second.mem);
363 if (mem_entry == memory_map_->end()) {
364 std::stringstream error_str;
365 error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
366 << " uses buffer " << buffer << " that references invalid memory "
367 << buffer_node->second.mem << ".";
368 *error = error_str.str();
369 return false;
370 }
371 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600372 if (descriptors_[i]->IsDynamic()) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600373 // Validate that dynamic offsets are within the buffer
374 auto buffer_size = buffer_node->second.createInfo.size;
Tobin Ehlis81f17852016-05-05 09:04:33 -0600375 auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
376 auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600377 auto dyn_offset = dynamic_offsets[dyn_offset_index++];
378 if (VK_WHOLE_SIZE == range) {
379 if ((dyn_offset + desc_offset) > buffer_size) {
380 std::stringstream error_str;
381 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
382 << " uses buffer " << buffer
383 << " with update range of VK_WHOLE_SIZE has dynamic offset " << dyn_offset
384 << " combined with offset " << desc_offset << " that oversteps the buffer size of "
385 << buffer_size << ".";
386 *error = error_str.str();
387 return false;
388 }
389 } else {
390 if ((dyn_offset + desc_offset + range) > buffer_size) {
391 std::stringstream error_str;
392 error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
393 << " uses buffer " << buffer << " with dynamic offset " << dyn_offset
394 << " combined with offset " << desc_offset << " and range " << range
395 << " that oversteps the buffer size of " << buffer_size << ".";
396 *error = error_str.str();
397 return false;
398 }
399 }
400 }
401 }
402 }
403 }
404 }
405 }
406 return true;
407}
408// For given bindings, place any update buffers or images into the passed-in unordered_sets
409uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::unordered_set<uint32_t> &bindings,
410 std::unordered_set<VkBuffer> *buffer_set,
411 std::unordered_set<VkImageView> *image_set) const {
412 auto num_updates = 0;
413 for (auto binding : bindings) {
414 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(binding);
Tobin Ehlis81f17852016-05-05 09:04:33 -0600415 if (descriptors_[start_idx]->IsStorage()) {
416 if (Image == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600417 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600418 if (descriptors_[start_idx + i]->updated) {
419 image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600420 num_updates++;
421 }
422 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600423 } else if (TexelBuffer == 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 auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
Tobin Ehlis0bc30632016-05-05 10:16:02 -0600427 const auto &buff_pair = buffer_view_map_->find(bufferview);
428 if (buff_pair != buffer_view_map_->end()) {
429 buffer_set->insert(buff_pair->second.buffer);
430 num_updates++;
431 }
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600432 }
433 }
Tobin Ehlis81f17852016-05-05 09:04:33 -0600434 } else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600435 for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600436 if (descriptors_[start_idx + i]->updated) {
437 buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600438 num_updates++;
439 }
440 }
441 }
442 }
443 }
444 return num_updates;
445}
446// This is a special case for compute shaders that should eventually be removed once we have proper valid binding info for compute
447// case
448uint32_t cvdescriptorset::DescriptorSet::GetAllStorageUpdates(std::unordered_set<VkBuffer> *buffer_set,
449 std::unordered_set<VkImageView> *image_set) const {
450 std::unordered_set<uint32_t> binding_set;
451 p_layout_->FillBindingSet(&binding_set);
452 return GetStorageUpdates(binding_set, buffer_set, image_set);
453}
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600454// Perform write update in given update struct
455// If an error occurs, return false and fill in details in error_msg string
456bool cvdescriptorset::DescriptorSet::WriteUpdate(debug_report_data *report_data, const VkWriteDescriptorSet *update,
457 std::string *error_msg) {
458 auto num_updates = 0;
459 // Verify dst binding exists
460 if (!p_layout_->HasBinding(update->dstBinding)) {
461 std::stringstream error_str;
462 error_str << "DescriptorSet " << set_ << " does not have binding " << update->dstBinding << ".";
463 *error_msg = error_str.str();
464 return false;
465 } else {
466 // We know that binding is valid, verify update and do update on each descriptor
467 auto start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
468 auto type = p_layout_->GetTypeFromBinding(update->dstBinding);
469 if (type != update->descriptorType) {
470 std::stringstream error_str;
471 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with type "
472 << string_VkDescriptorType(type) << " but update type is " << string_VkDescriptorType(update->descriptorType);
473 *error_msg = error_str.str();
474 return false;
475 }
476 if ((start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
477 std::stringstream error_str;
478 error_str << "Attempting write update to descriptor set " << set_ << " binding #" << update->dstBinding << " with "
479 << p_layout_->GetTotalDescriptorCount() << " total descriptors but update of " << update->descriptorCount
480 << " descriptors starting at binding offset of "
481 << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
482 << " combined with update array element offset of " << update->dstArrayElement
483 << " oversteps the size of this descriptor set.";
484 *error_msg = error_str.str();
485 return false;
486 }
487 // Verify consecutive bindings match (if needed)
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600488 if (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount,
489 "write update to", set_, error_msg))
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600490 return false;
491 // Update is within bounds and consistent so perform update
492 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600493 // TODO : Can we break this into a set-level "Validate" followed by Descriptor updating itself
494 // if the validate passes? That saves us all the map ptrs in each descriptor instance
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600495 if (!descriptors_[start_idx + di]->WriteUpdate(update, di, error_msg)) {
496 std::stringstream error_str;
497 error_str << "Write update to descriptor at global index " << start_idx + di << " within set " << set_
498 << " binding #" << update->dstBinding << " failed with error message: " << error_msg->c_str();
499 *error_msg = error_str.str();
500 return false;
501 }
502 ++num_updates;
503 }
504 }
505 if (num_updates != 0) {
506 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600507 }
508 return true;
509}
510// Copy update
511bool cvdescriptorset::DescriptorSet::CopyUpdate(debug_report_data *report_data, const VkCopyDescriptorSet *update,
512 const DescriptorSet *src_set, std::string *error) {
513 auto num_updates = 0;
514 if (!p_layout_->HasBinding(update->dstBinding)) {
515 std::stringstream error_str;
516 error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding << ".";
517 *error = error_str.str();
518 return false;
519 }
520 if (!src_set->HasBinding(update->srcBinding)) {
521 std::stringstream error_str;
522 error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding << ".";
523 *error = error_str.str();
524 return false;
525 }
526 // src & dst set bindings are valid
527 // Check bounds of src & dst
528 auto src_start_idx = src_set->GetGlobalStartIndexFromBinding(update->srcBinding) + update->srcArrayElement;
529 if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
530 // SRC update out of bounds
531 std::stringstream error_str;
532 error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
533 << " with offset index of " << src_set->GetGlobalStartIndexFromBinding(update->srcBinding)
534 << " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
535 << " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount() << ".";
536 *error = error_str.str();
537 return false;
538 }
539 auto dst_start_idx = p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding) + update->dstArrayElement;
540 if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
541 // DST update out of bounds
542 std::stringstream error_str;
543 error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
544 << " with offset index of " << p_layout_->GetGlobalStartIndexFromBinding(update->dstBinding)
545 << " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
546 << " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount() << ".";
547 *error = error_str.str();
548 return false;
549 }
550 // Check that types match
551 auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
552 auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
553 if (src_type != dst_type) {
554 std::stringstream error_str;
555 error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
556 << string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
557 << update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match.";
558 *error = error_str.str();
559 return false;
560 }
561 // Verify consistency of src & dst bindings if update crosses binding boundaries
Tobin Ehlis1f946f82016-05-05 12:03:44 -0600562 if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
563 "copy update from", src_set->GetSet(), error)) ||
564 (!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
565 set_, error))) {
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600566 return false;
567 }
568 // Update parameters all look good so perform update
569 for (uint32_t di = 0; di < update->descriptorCount; ++di) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600570 if (!descriptors_[dst_start_idx]->CopyUpdate(src_set->descriptors_[src_start_idx].get(), error))
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600571 return false;
572 ++num_updates;
573 }
574 if (num_updates != 0) {
575 some_update_ = true;
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600576 }
577 return true;
578}
579cvdescriptorset::SamplerDescriptor::SamplerDescriptor(
580 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
581 : sampler_(VK_NULL_HANDLE), immutable_(false), sampler_map_(sampler_map) {
582 updated = false;
583 descriptor_class = PlainSampler;
584};
585
586cvdescriptorset::SamplerDescriptor::SamplerDescriptor(
587 const VkSampler *immut, const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
588 : sampler_(VK_NULL_HANDLE), immutable_(false), sampler_map_(sampler_map) {
589 updated = false;
590 descriptor_class = PlainSampler;
591 if (immut) {
592 sampler_ = *immut;
593 immutable_ = true;
594 updated = true;
595 }
596}
597bool cvdescriptorset::ValidateSampler(const VkSampler sampler,
598 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map) {
Tobin Ehlis81f17852016-05-05 09:04:33 -0600599 return (sampler_map->count(sampler) != 0);
Tobin Ehlis0a43bde2016-05-03 08:31:08 -0600600}
601bool cvdescriptorset::ValidateImageUpdate(const VkImageView image_view, const VkImageLayout image_layout,
602 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
603 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
604 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
605 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
606 std::string *error) {
607 auto image_pair = image_view_map->find(image_view);
608 if (image_pair == image_view_map->end()) {
609 std::stringstream error_str;
610 error_str << "Invalid VkImageView: " << image_view;
611 *error = error_str.str();
612 return false;
613 } else {
614 // Validate that imageLayout is compatible with aspect_mask and image format
615 VkImageAspectFlags aspect_mask = image_pair->second.subresourceRange.aspectMask;
616 VkImage image = image_pair->second.image;
617 VkFormat format = VK_FORMAT_MAX_ENUM;
618 auto img_pair = image_map->find(image);
619 if (img_pair != image_map->end()) {
620 format = img_pair->second.createInfo.format;
621 } else {
622 // Also need to check the swapchains.
623 auto swapchain_pair = image_to_swapchain_map->find(image);
624 if (swapchain_pair != image_to_swapchain_map->end()) {
625 VkSwapchainKHR swapchain = swapchain_pair->second;
626 auto swapchain_pair = swapchain_map->find(swapchain);
627 if (swapchain_pair != swapchain_map->end()) {
628 format = swapchain_pair->second->createInfo.imageFormat;
629 }
630 }
631 }
632 if (format == VK_FORMAT_MAX_ENUM) {
633 std::stringstream error_str;
634 error_str << "Invalid image (" << image << ") in imageView (" << image_view << ").";
635 *error = error_str.str();
636 return false;
637 } else {
638 bool ds = vk_format_is_depth_or_stencil(format);
639 switch (image_layout) {
640 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
641 // Only Color bit must be set
642 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
643 std::stringstream error_str;
644 error_str << "ImageView (" << image_view << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but does "
645 "not have VK_IMAGE_ASPECT_COLOR_BIT set.";
646 *error = error_str.str();
647 return false;
648 }
649 // format must NOT be DS
650 if (ds) {
651 std::stringstream error_str;
652 error_str << "ImageView (" << image_view
653 << ") uses layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL but the image format is "
654 << string_VkFormat(format) << " which is not a color format.";
655 *error = error_str.str();
656 return false;
657 }
658 break;
659 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
660 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
661 // Depth or stencil bit must be set, but both must NOT be set
662 if (aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) {
663 if (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) {
664 // both must NOT be set
665 std::stringstream error_str;
666 error_str << "ImageView (" << image_view << ") has both STENCIL and DEPTH aspects set";
667 *error = error_str.str();
668 return false;
669 }
670 } else if (!(aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
671 // Neither were set
672 std::stringstream error_str;
673 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
674 << " but does not have STENCIL or DEPTH aspects set";
675 *error = error_str.str();
676 return false;
677 }
678 // format must be DS
679 if (!ds) {
680 std::stringstream error_str;
681 error_str << "ImageView (" << image_view << ") has layout " << string_VkImageLayout(image_layout)
682 << " but the image format is " << string_VkFormat(format) << " which is not a depth/stencil format.";
683 *error = error_str.str();
684 return false;
685 }
686 break;
687 default:
688 // anything to check for other layouts?
689 break;
690 }
691 }
692 }
693 return true;
694}
695bool cvdescriptorset::SamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
696 if (!immutable_) {
697 if (!ValidateSampler(update->pImageInfo[index].sampler, sampler_map_)) {
698 std::stringstream error_str;
699 error_str << "Attempted write update to sampler descriptor with invalid sampler: " << update->pImageInfo[index].sampler
700 << ".";
701 *error = error_str.str();
702 return false;
703 }
704 sampler_ = update->pImageInfo[index].sampler;
705 } else {
706 if (!ValidateSampler(sampler_, sampler_map_)) {
707 std::stringstream error_str;
708 error_str << "Attempted write update to immutable sampler descriptor which has invalid immutable sampler: " << sampler_
709 << ".";
710 *error = error_str.str();
711 return false;
712 }
713 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
714 }
715 updated = true;
716 return true;
717}
718
719bool cvdescriptorset::SamplerDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
720 if (!immutable_) {
721 auto update_sampler = static_cast<const SamplerDescriptor *>(src)->sampler_;
722 if (!ValidateSampler(update_sampler, sampler_map_)) {
723 std::stringstream error_str;
724 error_str << "Attempted copy update to sampler descriptor with invalid sampler: " << update_sampler << ".";
725 *error = error_str.str();
726 return false;
727 }
728 sampler_ = update_sampler;
729 } else {
730 if (!ValidateSampler(sampler_, sampler_map_)) {
731 std::stringstream error_str;
732 error_str << "Attempted copy update to immutable sampler descriptor which has invalid immutable sampler: " << sampler_
733 << ".";
734 *error = error_str.str();
735 return false;
736 }
737 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
738 }
739 updated = true;
740 return true;
741}
742cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(
743 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
744 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
745 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
746 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
747 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
748 : sampler_(VK_NULL_HANDLE), immutable_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED),
749 sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map),
750 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
751 updated = false;
752 descriptor_class = ImageSampler;
753}
754
755cvdescriptorset::ImageSamplerDescriptor::ImageSamplerDescriptor(
756 const VkSampler *immut, const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
757 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
758 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
759 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map,
760 const std::unordered_map<VkSampler, std::unique_ptr<SAMPLER_NODE>> *sampler_map)
761 : sampler_(VK_NULL_HANDLE), immutable_(true), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED),
762 sampler_map_(sampler_map), image_view_map_(image_view_map), image_map_(image_map),
763 image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
764 updated = false;
765 descriptor_class = ImageSampler;
766 if (immut) {
767 sampler_ = *immut;
768 immutable_ = true;
769 updated = true;
770 }
771}
772bool cvdescriptorset::ImageSamplerDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index,
773 std::string *error) {
774 // First check sampler
775 if (!immutable_) {
776 if (!ValidateSampler(update->pImageInfo[index].sampler, sampler_map_)) {
777 std::stringstream error_str;
778 error_str << "Attempted write update to combined image sampler descriptor with invalid sampler: "
779 << update->pImageInfo[index].sampler << ".";
780 *error = error_str.str();
781 return false;
782 }
783 sampler_ = update->pImageInfo[index].sampler;
784 } else {
785 if (!ValidateSampler(sampler_, sampler_map_)) {
786 std::stringstream error_str;
787 error_str << "Attempted write update to combined image sampler descriptor with immutable sampler which has invalid "
788 "immutable sampler: "
789 << sampler_ << ".";
790 *error = error_str.str();
791 return false;
792 }
793 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
794 }
795 // Now validate images
796 auto image_view = update->pImageInfo[index].imageView;
797 auto image_layout = update->pImageInfo[index].imageLayout;
798 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
799 error)) {
800 std::stringstream error_str;
801 error_str << "Attempted write update to combined image sampler descriptor failed due to: " << error->c_str();
802 *error = error_str.str();
803 return false;
804 }
805 updated = true;
806 image_view_ = image_view;
807 image_layout_ = image_layout;
808 return true;
809}
810
811bool cvdescriptorset::ImageSamplerDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
812 if (!immutable_) {
813 auto update_sampler = static_cast<const ImageSamplerDescriptor *>(src)->sampler_;
814 if (!ValidateSampler(update_sampler, sampler_map_)) {
815 std::stringstream error_str;
816 error_str << "Attempted copy update to combined image sampler descriptor with invalid sampler: " << update_sampler
817 << ".";
818 *error = error_str.str();
819 return false;
820 }
821 sampler_ = update_sampler;
822 } else {
823 if (!ValidateSampler(sampler_, sampler_map_)) {
824 std::stringstream error_str;
825 error_str << "Attempted copy update to combined image sampler descriptor with immutable sampler that has invalid "
826 "immutable sampler: "
827 << sampler_ << ".";
828 *error = error_str.str();
829 return false;
830 }
831 // TODO : Do we want a way to warn here in case of updating immutable sampler (which can't be updated)?
832 }
833 // Now validate images
834 auto image_view = static_cast<const ImageSamplerDescriptor *>(src)->image_view_;
835 auto image_layout = static_cast<const ImageSamplerDescriptor *>(src)->image_layout_;
836 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
837 error)) {
838 std::stringstream error_str;
839 error_str << "Attempted copy update to combined image sampler descriptor failed due to: " << error->c_str();
840 *error = error_str.str();
841 return false;
842 }
843 updated = true;
844 image_view_ = image_view;
845 image_layout_ = image_layout;
846 return true;
847}
848
849cvdescriptorset::ImageDescriptor::ImageDescriptor(const VkDescriptorType type,
850 const std::unordered_map<VkImageView, VkImageViewCreateInfo> *image_view_map,
851 const std::unordered_map<VkImage, IMAGE_NODE> *image_map,
852 const std::unordered_map<VkImage, VkSwapchainKHR> *image_to_swapchain_map,
853 const std::unordered_map<VkSwapchainKHR, SWAPCHAIN_NODE *> *swapchain_map)
854 : storage_(false), image_view_(VK_NULL_HANDLE), image_layout_(VK_IMAGE_LAYOUT_UNDEFINED), image_view_map_(image_view_map),
855 image_map_(image_map), image_to_swapchain_map_(image_to_swapchain_map), swapchain_map_(swapchain_map) {
856 updated = false;
857 descriptor_class = Image;
858 if (VK_DESCRIPTOR_TYPE_STORAGE_IMAGE == type)
859 storage_ = true;
860};
861
862bool cvdescriptorset::ImageDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
863 // First validate that the write update is valid
864 auto image_view = update->pImageInfo[index].imageView;
865 auto image_layout = update->pImageInfo[index].imageLayout;
866 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
867 error)) {
868 std::stringstream error_str;
869 error_str << "Attempted write update to image descriptor failed due to: " << error->c_str();
870 *error = error_str.str();
871 return false;
872 }
873 // Update is clean so process it
874 updated = true;
875 image_view_ = image_view;
876 image_layout_ = image_layout;
877 return true;
878}
879
880bool cvdescriptorset::ImageDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
881 // First validate update
882 auto image_view = static_cast<const ImageDescriptor *>(src)->image_view_;
883 auto image_layout = static_cast<const ImageDescriptor *>(src)->image_layout_;
884 if (!ValidateImageUpdate(image_view, image_layout, image_view_map_, image_map_, image_to_swapchain_map_, swapchain_map_,
885 error)) {
886 std::stringstream error_str;
887 error_str << "Attempted copy update to image descriptor failed due to: " << error->c_str();
888 *error = error_str.str();
889 return false;
890 }
891 updated = true;
892 image_view_ = image_view;
893 image_layout_ = image_layout;
894 return true;
895}
896
897cvdescriptorset::BufferDescriptor::BufferDescriptor(const VkDescriptorType type,
898 const std::unordered_map<VkBuffer, BUFFER_NODE> *buffer_map)
899 : storage_(false), dynamic_(false), buffer_(VK_NULL_HANDLE), offset_(0), range_(0), buffer_map_(buffer_map) {
900 updated = false;
901 descriptor_class = GeneralBuffer;
902 if (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC == type) {
903 dynamic_ = true;
904 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER == type) {
905 storage_ = true;
906 } else if (VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC == type) {
907 dynamic_ = true;
908 storage_ = true;
909 }
910}
911bool cvdescriptorset::BufferDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
912 // First validate bufferinfo
913 auto buffer = update->pBufferInfo[index].buffer;
914 if (!buffer_map_->count(buffer)) {
915 std::stringstream error_str;
916 error_str << "Attempted write update to buffer descriptor with invalid buffer: " << buffer;
917 *error = error_str.str();
918 return false;
919 }
920 updated = true;
921 buffer_ = buffer;
922 offset_ = update->pBufferInfo[index].offset;
923 range_ = update->pBufferInfo[index].range;
924 return true;
925}
926
927bool cvdescriptorset::BufferDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
928 // First validate bufferinfo
929 auto buffer = static_cast<const BufferDescriptor *>(src)->buffer_;
930 if (!buffer_map_->count(buffer)) {
931 std::stringstream error_str;
932 error_str << "Attempted copy update to buffer descriptor with invalid buffer: " << buffer;
933 *error = error_str.str();
934 return false;
935 }
936 updated = true;
937 buffer_ = buffer;
938 offset_ = static_cast<const BufferDescriptor *>(src)->offset_;
939 range_ = static_cast<const BufferDescriptor *>(src)->range_;
940 return true;
941}
942
943cvdescriptorset::TexelDescriptor::TexelDescriptor(const VkDescriptorType type,
944 const std::unordered_map<VkBufferView, VkBufferViewCreateInfo> *buffer_view_map)
945 : buffer_view_(VK_NULL_HANDLE), storage_(false), buffer_view_map_(buffer_view_map) {
946 updated = false;
947 descriptor_class = TexelBuffer;
948 if (VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER == type)
949 storage_ = true;
950};
951bool cvdescriptorset::TexelDescriptor::WriteUpdate(const VkWriteDescriptorSet *update, const uint32_t index, std::string *error) {
952 // First validate buffer view
953 auto buffer_view = update->pTexelBufferView[index];
954 if (!buffer_view_map_->count(buffer_view)) {
955 std::stringstream error_str;
956 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
957 *error = error_str.str();
958 return false;
959 }
960 updated = true;
961 buffer_view_ = buffer_view;
962 return true;
963}
964
965bool cvdescriptorset::TexelDescriptor::CopyUpdate(const Descriptor *src, std::string *error) {
966 // First validate buffer view
967 auto buffer_view = static_cast<const TexelDescriptor *>(src)->buffer_view_;
968 if (!buffer_view_map_->count(buffer_view)) {
969 std::stringstream error_str;
970 error_str << "Attempted write update to texel buffer descriptor with invalid buffer view: " << buffer_view;
971 *error = error_str.str();
972 return false;
973 }
974 updated = true;
975 buffer_view_ = buffer_view;
976 return true;
977}